win32 platform freetype support improvements

This commit is contained in:
Vadim Lopatin 2015-01-30 09:48:01 +03:00
parent 08de99de59
commit 62c94537a2
3 changed files with 72 additions and 43 deletions

View File

@ -491,7 +491,12 @@ class FreeTypeFontManager : FontManager {
this() { this() {
// load dynaic library // load dynaic library
try {
DerelictFT.load(); DerelictFT.load();
} catch (Exception e) {
Log.e("Derelict: cannot load freetype shared library: ", e.msg);
throw new Exception("Cannot load freetype library");
}
// init library // init library
int error = FT_Init_FreeType(&_library); int error = FT_Init_FreeType(&_library);
if (error) { if (error) {

View File

@ -29,6 +29,9 @@ import dlangui.platforms.windows.win32drawbuf;
import std.string; import std.string;
import std.utf; import std.utf;
/// define debug=FontResources for logging of font file resources creation/freeing
//debug = FontResources;
//auto toUTF16z(S)(S s) //auto toUTF16z(S)(S s)
//{ //{
//return toUTFz!(const(wchar)*)(s); //return toUTFz!(const(wchar)*)(s);
@ -450,13 +453,13 @@ class Win32Font : Font {
_size = size; _size = size;
_height = tm.tmHeight; _height = tm.tmHeight;
Log.d("Win32Font.create: height=", _height, " for size=", _size, " points=", lf.lfHeight, " dpi=", _dpi); debug(FontResources) Log.d("Win32Font.create: height=", _height, " for size=", _size, " points=", lf.lfHeight, " dpi=", _dpi);
_baseline = _height - tm.tmDescent; _baseline = _height - tm.tmDescent;
_weight = weight; _weight = weight;
_italic = italic; _italic = italic;
_face = def.face; _face = def.face;
_family = def.family; _family = def.family;
Log.d("Created font ", _face, " ", _size); debug(FontResources) Log.d("Created font ", _face, " ", _size);
return true; return true;
} }
@ -532,11 +535,11 @@ class Win32FontManager : FontManager {
int index = _activeFonts.find(size, weight, italic, def.family, def.face); int index = _activeFonts.find(size, weight, italic, def.family, def.face);
if (index >= 0) if (index >= 0)
return _activeFonts.get(index); return _activeFonts.get(index);
Log.d("Creating new font"); debug(FontResources) Log.d("Creating new font");
Win32Font item = new Win32Font(); Win32Font item = new Win32Font();
if (!item.create(def, size, weight, italic)) if (!item.create(def, size, weight, italic))
return _emptyFontRef; return _emptyFontRef;
Log.d("Adding to list of active fonts"); debug(FontResources) Log.d("Adding to list of active fonts");
return _activeFonts.add(item); return _activeFonts.add(item);
} else { } else {
return _emptyFontRef; return _emptyFontRef;

View File

@ -900,14 +900,19 @@ int myWinMain(void* hInstance, void* hPrevInstance, char* lpCmdLine, int iCmdSho
Platform.setInstance(w32platform); Platform.setInstance(w32platform);
try {
/// testing freetype font manager /// testing freetype font manager
version(USE_FREETYPE) { version(USE_FREETYPE) {
import dlangui.graphics.ftfonts; import dlangui.graphics.ftfonts;
import win32.shlobj; // trying to create font manager
FreeTypeFontManager ftfontMan = new FreeTypeFontManager(); FreeTypeFontManager ftfontMan = new FreeTypeFontManager();
import win32.shlobj;
string fontsPath = "c:\\Windows\\Fonts\\"; string fontsPath = "c:\\Windows\\Fonts\\";
static if (false) { // SHGetFolderPathW not found in shell32.lib static if (true) { // SHGetFolderPathW not found in shell32.lib
WCHAR[MAX_PATH] szPath; WCHAR[MAX_PATH] szPath;
static if (false) {
const CSIDL_FLAG_NO_ALIAS = 0x1000; const CSIDL_FLAG_NO_ALIAS = 0x1000;
const CSIDL_FLAG_DONT_UNEXPAND = 0x2000; const CSIDL_FLAG_DONT_UNEXPAND = 0x2000;
if(SUCCEEDED(SHGetFolderPathW(NULL, if(SUCCEEDED(SHGetFolderPathW(NULL,
@ -918,6 +923,14 @@ int myWinMain(void* hInstance, void* hPrevInstance, char* lpCmdLine, int iCmdSho
{ {
fontsPath = toUTF8(fromWStringz(szPath)); fontsPath = toUTF8(fromWStringz(szPath));
} }
} else {
if (GetWindowsDirectory(szPath.ptr, MAX_PATH - 1)) {
fontsPath = toUTF8(fromWStringz(szPath));
Log.i("Windows directory: ", fontsPath);
fontsPath ~= "\\Fonts\\";
Log.i("Fonts directory: ", fontsPath);
}
}
} }
ftfontMan.registerFont(fontsPath ~ "arial.ttf", FontFamily.SansSerif, "Arial", false, FontWeight.Normal); ftfontMan.registerFont(fontsPath ~ "arial.ttf", FontFamily.SansSerif, "Arial", false, FontWeight.Normal);
ftfontMan.registerFont(fontsPath ~ "arialbd.ttf", FontFamily.SansSerif, "Arial", false, FontWeight.Bold); ftfontMan.registerFont(fontsPath ~ "arialbd.ttf", FontFamily.SansSerif, "Arial", false, FontWeight.Bold);
@ -939,7 +952,15 @@ int myWinMain(void* hInstance, void* hPrevInstance, char* lpCmdLine, int iCmdSho
ftfontMan.registerFont(fontsPath ~ "verdanab.ttf", FontFamily.SansSerif, "Verdana", false, FontWeight.Bold); ftfontMan.registerFont(fontsPath ~ "verdanab.ttf", FontFamily.SansSerif, "Verdana", false, FontWeight.Bold);
ftfontMan.registerFont(fontsPath ~ "verdanai.ttf", FontFamily.SansSerif, "Verdana", true, FontWeight.Normal); ftfontMan.registerFont(fontsPath ~ "verdanai.ttf", FontFamily.SansSerif, "Verdana", true, FontWeight.Normal);
ftfontMan.registerFont(fontsPath ~ "verdanaz.ttf", FontFamily.SansSerif, "Verdana", true, FontWeight.Bold); ftfontMan.registerFont(fontsPath ~ "verdanaz.ttf", FontFamily.SansSerif, "Verdana", true, FontWeight.Bold);
if (ftfontMan.registeredFontCount()) {
FontManager.instance = ftfontMan; FontManager.instance = ftfontMan;
} else {
Log.w("No fonts registered in FreeType font manager. Disabling FreeType.");
destroy(ftfontMan);
}
}
} catch (Exception e) {
Log.e("Cannot create FreeTypeFontManager - falling back to win32");
} }
// use Win32 font manager // use Win32 font manager