mirror of https://github.com/OpenTTD/OpenTTD
(svn r7333) -Codechange: [win32] Extend the OTTD2FS and FS2OTTD functions to also accept conversions
into a predefined buffer insted of using the static (global) buffer. This is useful if the converted value will be used later on; no need to copy it somewhere else to save it. -CodeChange: [win32] Added appropiate macros WIDE_TO_MB_BUFFER, MB_TO_WIDE_BUFFER next to existing WIDE_TO_MB and MB_TO_WIDE that only do work when UNICODE is defined, saves #ifdefs all over the placerelease/0.5
parent
5fffa66b71
commit
b789cacb40
141
fontcache.c
141
fontcache.c
|
@ -34,23 +34,104 @@ enum {
|
||||||
SHADOW_COLOUR = 2,
|
SHADOW_COLOUR = 2,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#include <tchar.h>
|
||||||
|
#include <shlobj.h> // SHGetFolderPath
|
||||||
|
#include "win32.h"
|
||||||
|
|
||||||
/**
|
#define FONT_DIR_NT "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"
|
||||||
* Loads the freetype font.
|
#define FONT_DIR_9X "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Fonts"
|
||||||
* First type to load the fontname as if it were a path. If that fails,
|
static FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
|
||||||
* try to resolve the filename of the font using fontconfig, where the
|
|
||||||
* format is 'font family name' or 'font family name, font style'.
|
|
||||||
*/
|
|
||||||
static void LoadFreeTypeFont(const char *font_name, FT_Face *face, const char *type)
|
|
||||||
{
|
{
|
||||||
FT_Error error;
|
FT_Error err = FT_Err_Cannot_Open_Resource;
|
||||||
|
HKEY hKey;
|
||||||
|
LONG ret;
|
||||||
|
TCHAR vbuffer[MAX_PATH], dbuffer[256];
|
||||||
|
char *font_path;
|
||||||
|
uint index;
|
||||||
|
|
||||||
if (strlen(font_name) == 0) return;
|
/* On windows NT (2000, NT3.5, XP, etc.) the fonts are stored in the,
|
||||||
|
* "Windows NT" key, on Windows 9x in the Windows key. To save us having
|
||||||
|
* to retrieve the windows version, we'll just query both */
|
||||||
|
ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T(FONT_DIR_NT), 0, KEY_READ, &hKey);
|
||||||
|
if (ret != ERROR_SUCCESS) ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T(FONT_DIR_9X), 0, KEY_READ, &hKey);
|
||||||
|
|
||||||
error = FT_New_Face(_library, font_name, 0, face);
|
if (ret != ERROR_SUCCESS) {
|
||||||
|
DEBUG(freetype, 0) ("Cannot open registry key HKLM\\SOFTWARE\\Microsoft\\Windows (NT)\\CurrentVersion\\Fonts");
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (index = 0;; index++) {
|
||||||
|
char *s;
|
||||||
|
uint vbuflen = lengthof(vbuffer);
|
||||||
|
uint dbuflen = lengthof(dbuffer);
|
||||||
|
|
||||||
|
ret = RegEnumValue(hKey, index, vbuffer, &vbuflen, NULL, NULL, dbuffer, &dbuflen);
|
||||||
|
if (ret != ERROR_SUCCESS) break;
|
||||||
|
|
||||||
|
/* The font names in the registry are of the following 3 forms:
|
||||||
|
* - ADMUI3.fon
|
||||||
|
* - Book Antiqua Bold (TrueType)
|
||||||
|
* - Batang & BatangChe & Gungsuh & GungsuhChe (TrueType)
|
||||||
|
* We will strip the font-type '()' if any and work with the font name
|
||||||
|
* itself, which must match exactly; if...
|
||||||
|
* TTC files, font files which contain more than one font are seperated
|
||||||
|
* byt '&'. Our best bet will be to do substr match for the fontname
|
||||||
|
* and then let FreeType figure out which index to load */
|
||||||
|
s = _tcschr(vbuffer, '(');
|
||||||
|
if (s != NULL) s[-1] = '\0';
|
||||||
|
|
||||||
|
if (_tcschr(vbuffer, '&') == NULL) {
|
||||||
|
if (_tcsicmp(vbuffer, font_name) == 0) break;
|
||||||
|
} else {
|
||||||
|
if (_tcsstr(vbuffer, font_name) != NULL) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_FONTS, NULL, SHGFP_TYPE_CURRENT, vbuffer))) {
|
||||||
|
DEBUG(freetype, 0) ("SHGetFolderPath cannot return fonts directory");
|
||||||
|
goto folder_error;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Some fonts are contained in .ttc files, TrueType Collection fonts. These
|
||||||
|
* contain multiple fonts inside this single file. GetFontData however
|
||||||
|
* returns the whole file, so we need to check each font inside to get the
|
||||||
|
* proper font. If not found, we will use the last font in the ttc.
|
||||||
|
* Also note that FreeType does not support UNICODE filesnames! */
|
||||||
|
#if defined(UNICODE)
|
||||||
|
font_path = malloc(MAX_PATH);
|
||||||
|
font_path = convert_from_fs(vbuffer, font_path, MAX_PATH);
|
||||||
|
#else
|
||||||
|
font_path = vbuffer;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ttd_strlcat(font_path, "\\", MAX_PATH);
|
||||||
|
ttd_strlcat(font_path, WIDE_TO_MB(dbuffer), MAX_PATH);
|
||||||
|
index = 0;
|
||||||
|
do {
|
||||||
|
err = FT_New_Face(_library, font_path, index, face);
|
||||||
|
if (err != FT_Err_Ok) break;
|
||||||
|
|
||||||
|
if (strncasecmp(font_name, (*face)->family_name, strlen((*face)->family_name)) == 0) break;
|
||||||
|
err = FT_Err_Cannot_Open_Resource;
|
||||||
|
|
||||||
|
} while (++index != (*face)->num_faces);
|
||||||
|
|
||||||
|
#if defined(UNICODE)
|
||||||
|
free(font_path);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
folder_error:
|
||||||
|
RegCloseKey(hKey);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
#else
|
||||||
# ifdef WITH_FONTCONFIG
|
# ifdef WITH_FONTCONFIG
|
||||||
/* Failed to load the font, so try it with fontconfig */
|
static FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
|
||||||
if (error != FT_Err_Ok) {
|
{
|
||||||
|
FT_Error err = FT_Err_Cannot_Open_Resource;
|
||||||
|
|
||||||
if (!FcInit()) {
|
if (!FcInit()) {
|
||||||
ShowInfoF("Unable to load font configuration");
|
ShowInfoF("Unable to load font configuration");
|
||||||
} else {
|
} else {
|
||||||
|
@ -85,7 +166,7 @@ static void LoadFreeTypeFont(const char *font_name, FT_Face *face, const char *t
|
||||||
FcChar8 *file;
|
FcChar8 *file;
|
||||||
FcFontSetAdd(fs, match);
|
FcFontSetAdd(fs, match);
|
||||||
|
|
||||||
for (i = 0; error != FT_Err_Ok && i < fs->nfont; i++) {
|
for (i = 0; err != FT_Err_Ok && i < fs->nfont; i++) {
|
||||||
/* Try the new filename */
|
/* Try the new filename */
|
||||||
if (FcPatternGetString(fs->fonts[i], FC_FILE, 0, &file) == FcResultMatch &&
|
if (FcPatternGetString(fs->fonts[i], FC_FILE, 0, &file) == FcResultMatch &&
|
||||||
FcPatternGetString(fs->fonts[i], FC_FAMILY, 0, &family) == FcResultMatch &&
|
FcPatternGetString(fs->fonts[i], FC_FAMILY, 0, &family) == FcResultMatch &&
|
||||||
|
@ -98,7 +179,7 @@ static void LoadFreeTypeFont(const char *font_name, FT_Face *face, const char *t
|
||||||
* wrongly a 'random' font, so check whether the family name is the
|
* wrongly a 'random' font, so check whether the family name is the
|
||||||
* same as the supplied name */
|
* same as the supplied name */
|
||||||
if (strcasecmp(font_family, (char*)family) == 0) {
|
if (strcasecmp(font_family, (char*)family) == 0) {
|
||||||
error = FT_New_Face(_library, (char *)file, 0, face);
|
err = FT_New_Face(_library, (char *)file, 0, face);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -109,15 +190,39 @@ static void LoadFreeTypeFont(const char *font_name, FT_Face *face, const char *t
|
||||||
FcFontSetDestroy(fs);
|
FcFontSetDestroy(fs);
|
||||||
FcFini();
|
FcFini();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return error;
|
||||||
}
|
}
|
||||||
|
# else
|
||||||
|
FT_Error GetFontByFaceName(const char *font_name, FT_Face *face) {return FT_Err_Cannot_Open_Resource;}
|
||||||
|
# endif /* WITH_FONTCONFIG */
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads the freetype font.
|
||||||
|
* First type to load the fontname as if it were a path. If that fails,
|
||||||
|
* try to resolve the filename of the font using fontconfig, where the
|
||||||
|
* format is 'font family name' or 'font family name, font style'.
|
||||||
|
*/
|
||||||
|
static void LoadFreeTypeFont(const char *font_name, FT_Face *face, const char *type)
|
||||||
|
{
|
||||||
|
FT_Error error;
|
||||||
|
|
||||||
|
if (strlen(font_name) == 0) return;
|
||||||
|
|
||||||
|
error = FT_New_Face(_library, font_name, 0, face);
|
||||||
|
|
||||||
|
if (error != FT_Err_Ok) error = GetFontByFaceName(font_name, face);
|
||||||
|
|
||||||
if (error == FT_Err_Ok) {
|
if (error == FT_Err_Ok) {
|
||||||
|
DEBUG(freetype, 2) ("Requested font '%s', found '%s %s'", font_name, (*face)->family_name, (*face)->style_name);
|
||||||
|
|
||||||
/* Attempt to select the unicode character map */
|
/* Attempt to select the unicode character map */
|
||||||
error = FT_Select_Charmap(*face, ft_encoding_unicode);
|
error = FT_Select_Charmap(*face, ft_encoding_unicode);
|
||||||
if (error == FT_Err_Ok) {
|
if (error == FT_Err_Ok) return; // Success
|
||||||
/* Success */
|
|
||||||
return;
|
if (error == FT_Err_Invalid_CharMap_Handle) {
|
||||||
} else if (error == FT_Err_Invalid_CharMap_Handle) {
|
|
||||||
/* Try to pick a different character map instead. We default to
|
/* Try to pick a different character map instead. We default to
|
||||||
* the first map, but platform_id 0 encoding_id 0 should also
|
* the first map, but platform_id 0 encoding_id 0 should also
|
||||||
* be unicode (strange system...) */
|
* be unicode (strange system...) */
|
||||||
|
|
77
win32.c
77
win32.c
|
@ -368,14 +368,10 @@ static INT_PTR CALLBACK CrashDialogFunc(HWND wnd,UINT msg,WPARAM wParam,LPARAM l
|
||||||
switch (msg) {
|
switch (msg) {
|
||||||
case WM_INITDIALOG: {
|
case WM_INITDIALOG: {
|
||||||
#if defined(UNICODE)
|
#if defined(UNICODE)
|
||||||
# define crash_msg crash_msgW
|
wchar_t crash_msgW[8096];
|
||||||
TCHAR crash_msgW[8096];
|
|
||||||
MultiByteToWideChar(CP_ACP, 0, _crash_msg, -1, crash_msgW, lengthof(crash_msgW));
|
|
||||||
#else
|
|
||||||
# define crash_msg _crash_msg
|
|
||||||
#endif
|
#endif
|
||||||
SetDlgItemText(wnd, 10, _crash_desc);
|
SetDlgItemText(wnd, 10, _crash_desc);
|
||||||
SetDlgItemText(wnd, 11, crash_msg);
|
SetDlgItemText(wnd, 11, MB_TO_WIDE_BUFFER(_crash_msg, crash_msgW, lengthof(crash_msgW)));
|
||||||
SendDlgItemMessage(wnd, 11, WM_SETFONT, (WPARAM)GetStockObject(ANSI_FIXED_FONT), FALSE);
|
SendDlgItemMessage(wnd, 11, WM_SETFONT, (WPARAM)GetStockObject(ANSI_FIXED_FONT), FALSE);
|
||||||
SetWndSize(wnd, -1);
|
SetWndSize(wnd, -1);
|
||||||
} return TRUE;
|
} return TRUE;
|
||||||
|
@ -917,7 +913,7 @@ void DeterminePaths(void)
|
||||||
|
|
||||||
_path.personal_dir = _path.game_data_dir = cfg = malloc(MAX_PATH);
|
_path.personal_dir = _path.game_data_dir = cfg = malloc(MAX_PATH);
|
||||||
GetCurrentDirectoryW(MAX_PATH - 1, path);
|
GetCurrentDirectoryW(MAX_PATH - 1, path);
|
||||||
WideCharToMultiByte(CP_UTF8, 0, path, -1, cfg, MAX_PATH, NULL, NULL);
|
convert_from_fs(path, cfg, MAX_PATH);
|
||||||
|
|
||||||
cfg[0] = toupper(cfg[0]);
|
cfg[0] = toupper(cfg[0]);
|
||||||
s = strchr(cfg, '\0');
|
s = strchr(cfg, '\0');
|
||||||
|
@ -1037,37 +1033,62 @@ int64 GetTS(void)
|
||||||
return (__int64)(value * freq);
|
return (__int64)(value * freq);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Convert from OpenTTD's encoding to that of the local environment
|
/** Convert from OpenTTD's encoding to that of the local environment in
|
||||||
* First convert from UTF8 to wide-char, then to local
|
* UNICODE. OpenTTD encoding is UTF8, local is wide-char
|
||||||
* @param name pointer to a valid string that will be converted
|
* @param name pointer to a valid string that will be converted
|
||||||
* @return pointer to a new stringbuffer that contains the converted string */
|
* @param utf16_buf pointer to a valid wide-char buffer that will receive the
|
||||||
const wchar_t *OTTD2FS(const char *name)
|
* converted string
|
||||||
|
* @param buflen length in wide characters of the receiving buffer
|
||||||
|
* @return pointer to utf16_buf. If conversion fails the string is of zero-length */
|
||||||
|
wchar_t *convert_to_fs(const char *name, wchar_t *utf16_buf, size_t buflen)
|
||||||
{
|
{
|
||||||
static wchar_t ucs2_buf[MAX_PATH];
|
int len = MultiByteToWideChar(CP_UTF8, 0, name, -1, utf16_buf, buflen);
|
||||||
int len;
|
|
||||||
|
|
||||||
len = MultiByteToWideChar(CP_UTF8, 0, name, -1, ucs2_buf, lengthof(ucs2_buf));
|
|
||||||
if (len == 0) {
|
if (len == 0) {
|
||||||
DEBUG(misc, 0) ("[utf8] Error converting '%s'. Errno %d", name, GetLastError());
|
DEBUG(misc, 0) ("[utf8] Error converting '%s'. Errno %d", name, GetLastError());
|
||||||
return L"";
|
utf16_buf[0] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
return (const wchar_t*)ucs2_buf;
|
return utf16_buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Convert to OpenTTD's encoding from that of the local environment
|
/** Convert from OpenTTD's encoding to that of the local environment in
|
||||||
|
* UNICODE. OpenTTD encoding is UTF8, local is wide-char.
|
||||||
|
* The returned value's contents can only be guaranteed until the next call to
|
||||||
|
* this function. So if the value is needed for anything else, use convert_from_fs
|
||||||
* @param name pointer to a valid string that will be converted
|
* @param name pointer to a valid string that will be converted
|
||||||
* @return pointer to a new stringbuffer that contains the converted string */
|
* @return pointer to the converted string; if failed string is of zero-length */
|
||||||
|
const wchar_t *OTTD2FS(const char *name)
|
||||||
|
{
|
||||||
|
static wchar_t utf16_buf[MAX_PATH];
|
||||||
|
return convert_to_fs(name, utf16_buf, lengthof(utf16_buf));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Convert to OpenTTD's encoding from that of the local environment in
|
||||||
|
* UNICODE. OpenTTD encoding is UTF8, local is wide-char
|
||||||
|
* @param name pointer to a valid string that will be converted
|
||||||
|
* @param utf8_buf pointer to a valid buffer that will receive the converted string
|
||||||
|
* @param buflen length in characters of the receiving buffer
|
||||||
|
* @return pointer to utf8_buf. If conversion fails the string is of zero-length */
|
||||||
|
char *convert_from_fs(const wchar_t *name, char *utf8_buf, size_t buflen)
|
||||||
|
{
|
||||||
|
int len = WideCharToMultiByte(CP_UTF8, 0, name, -1, utf8_buf, buflen, NULL, NULL);
|
||||||
|
if (len == 0) {
|
||||||
|
DEBUG(misc, 0) ("[utf8] Error converting string. Errno %d", GetLastError());
|
||||||
|
utf8_buf[0] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
return utf8_buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Convert to OpenTTD's encoding from that of the local environment in
|
||||||
|
* UNICODE. OpenTTD encoding is UTF8, local is wide-char.
|
||||||
|
* The returned value's contents can only be guaranteed until the next call to
|
||||||
|
* this function. So if the value is needed for anything else, use convert_from_fs
|
||||||
|
* @param name pointer to a valid string that will be converted
|
||||||
|
* @return pointer to the converted string; if failed string is of zero-length */
|
||||||
const char *FS2OTTD(const wchar_t *name)
|
const char *FS2OTTD(const wchar_t *name)
|
||||||
{
|
{
|
||||||
static char utf8_buf[512];
|
static char utf8_buf[512];
|
||||||
int len;
|
return convert_from_fs(name, utf8_buf, lengthof(utf8_buf));
|
||||||
|
|
||||||
len = WideCharToMultiByte(CP_UTF8, 0, name, -1, utf8_buf, lengthof(utf8_buf), NULL, NULL);
|
|
||||||
if (len == 0) {
|
|
||||||
DEBUG(misc, 0) ("[utf8] Error converting string. Errno %d", GetLastError());
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
return (const char*)utf8_buf;
|
|
||||||
}
|
}
|
||||||
|
|
15
win32.h
15
win32.h
|
@ -8,12 +8,19 @@ bool MyShowCursor(bool show);
|
||||||
typedef void (*Function)(int);
|
typedef void (*Function)(int);
|
||||||
bool LoadLibraryList(Function proc[], const char *dll);
|
bool LoadLibraryList(Function proc[], const char *dll);
|
||||||
|
|
||||||
|
char *convert_from_fs(const wchar_t *name, char *utf8_buf, size_t buflen);
|
||||||
|
wchar_t *convert_to_fs(const char *name, wchar_t *utf16_buf, size_t buflen);
|
||||||
|
|
||||||
#if defined(UNICODE)
|
#if defined(UNICODE)
|
||||||
# define MB_TO_WIDE(x) OTTD2FS(x)
|
# define MB_TO_WIDE(str) OTTD2FS(str)
|
||||||
# define WIDE_TO_MB(x) FS2OTTD(x)
|
# define MB_TO_WIDE_BUFFER(str, buffer, buflen) convert_to_fs(str, buffer, buflen)
|
||||||
|
# define WIDE_TO_MB(str) FS2OTTD(str)
|
||||||
|
# define WIDE_TO_MB_BUFFER(str, buffer, buflen) convert_from_fs(str, buffer, buflen)
|
||||||
#else
|
#else
|
||||||
# define MB_TO_WIDE(x) (x)
|
# define MB_TO_WIDE(str) (str)
|
||||||
# define WIDE_TO_MB(x) (x)
|
# define MB_TO_WIDE_BUFFER(str, buffer, buflen) (str)
|
||||||
|
# define WIDE_TO_MB(str) (str)
|
||||||
|
# define WIDE_TO_MB_BUFFER(str, buffer, buflen) (str)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /* WIN32_H */
|
#endif /* WIN32_H */
|
||||||
|
|
Loading…
Reference in New Issue