From 62c94537a27a271c67d3593d4548f72c7cb40c66 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Fri, 30 Jan 2015 09:48:01 +0300 Subject: [PATCH] win32 platform freetype support improvements --- src/dlangui/graphics/ftfonts.d | 7 +- src/dlangui/platforms/windows/win32fonts.d | 11 ++- src/dlangui/platforms/windows/winapp.d | 97 +++++++++++++--------- 3 files changed, 72 insertions(+), 43 deletions(-) diff --git a/src/dlangui/graphics/ftfonts.d b/src/dlangui/graphics/ftfonts.d index eef61092..2e4bf77c 100644 --- a/src/dlangui/graphics/ftfonts.d +++ b/src/dlangui/graphics/ftfonts.d @@ -491,7 +491,12 @@ class FreeTypeFontManager : FontManager { this() { // load dynaic library - DerelictFT.load(); + try { + DerelictFT.load(); + } catch (Exception e) { + Log.e("Derelict: cannot load freetype shared library: ", e.msg); + throw new Exception("Cannot load freetype library"); + } // init library int error = FT_Init_FreeType(&_library); if (error) { diff --git a/src/dlangui/platforms/windows/win32fonts.d b/src/dlangui/platforms/windows/win32fonts.d index 14597b37..f551e00a 100644 --- a/src/dlangui/platforms/windows/win32fonts.d +++ b/src/dlangui/platforms/windows/win32fonts.d @@ -29,6 +29,9 @@ import dlangui.platforms.windows.win32drawbuf; import std.string; import std.utf; +/// define debug=FontResources for logging of font file resources creation/freeing +//debug = FontResources; + //auto toUTF16z(S)(S s) //{ //return toUTFz!(const(wchar)*)(s); @@ -450,13 +453,13 @@ class Win32Font : Font { _size = size; _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; _weight = weight; _italic = italic; _face = def.face; _family = def.family; - Log.d("Created font ", _face, " ", _size); + debug(FontResources) Log.d("Created font ", _face, " ", _size); return true; } @@ -532,11 +535,11 @@ class Win32FontManager : FontManager { int index = _activeFonts.find(size, weight, italic, def.family, def.face); if (index >= 0) return _activeFonts.get(index); - Log.d("Creating new font"); + debug(FontResources) Log.d("Creating new font"); Win32Font item = new Win32Font(); if (!item.create(def, size, weight, italic)) return _emptyFontRef; - Log.d("Adding to list of active fonts"); + debug(FontResources) Log.d("Adding to list of active fonts"); return _activeFonts.add(item); } else { return _emptyFontRef; diff --git a/src/dlangui/platforms/windows/winapp.d b/src/dlangui/platforms/windows/winapp.d index e01311d3..e680e9ac 100644 --- a/src/dlangui/platforms/windows/winapp.d +++ b/src/dlangui/platforms/windows/winapp.d @@ -900,46 +900,67 @@ int myWinMain(void* hInstance, void* hPrevInstance, char* lpCmdLine, int iCmdSho Platform.setInstance(w32platform); - /// testing freetype font manager - version(USE_FREETYPE) { - import dlangui.graphics.ftfonts; - import win32.shlobj; - FreeTypeFontManager ftfontMan = new FreeTypeFontManager(); - string fontsPath = "c:\\Windows\\Fonts\\"; - static if (false) { // SHGetFolderPathW not found in shell32.lib - WCHAR[MAX_PATH] szPath; - const CSIDL_FLAG_NO_ALIAS = 0x1000; - const CSIDL_FLAG_DONT_UNEXPAND = 0x2000; - if(SUCCEEDED(SHGetFolderPathW(NULL, - CSIDL_FONTS|CSIDL_FLAG_NO_ALIAS|CSIDL_FLAG_DONT_UNEXPAND, - NULL, - 0, - szPath.ptr))) - { - fontsPath = toUTF8(fromWStringz(szPath)); + + try { + /// testing freetype font manager + version(USE_FREETYPE) { + import dlangui.graphics.ftfonts; + // trying to create font manager + FreeTypeFontManager ftfontMan = new FreeTypeFontManager(); + + import win32.shlobj; + string fontsPath = "c:\\Windows\\Fonts\\"; + static if (true) { // SHGetFolderPathW not found in shell32.lib + WCHAR[MAX_PATH] szPath; + static if (false) { + const CSIDL_FLAG_NO_ALIAS = 0x1000; + const CSIDL_FLAG_DONT_UNEXPAND = 0x2000; + if(SUCCEEDED(SHGetFolderPathW(NULL, + CSIDL_FONTS|CSIDL_FLAG_NO_ALIAS|CSIDL_FLAG_DONT_UNEXPAND, + NULL, + 0, + szPath.ptr))) + { + 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 ~ "arialbd.ttf", FontFamily.SansSerif, "Arial", false, FontWeight.Bold); + ftfontMan.registerFont(fontsPath ~ "arialbi.ttf", FontFamily.SansSerif, "Arial", true, FontWeight.Bold); + ftfontMan.registerFont(fontsPath ~ "ariali.ttf", FontFamily.SansSerif, "Arial", true, FontWeight.Normal); + ftfontMan.registerFont(fontsPath ~ "cour.ttf", FontFamily.MonoSpace, "Courier New", false, FontWeight.Normal); + ftfontMan.registerFont(fontsPath ~ "courbd.ttf", FontFamily.MonoSpace, "Courier New", false, FontWeight.Bold); + ftfontMan.registerFont(fontsPath ~ "courbi.ttf", FontFamily.MonoSpace, "Courier New", true, FontWeight.Bold); + ftfontMan.registerFont(fontsPath ~ "couri.ttf", FontFamily.MonoSpace, "Courier New", true, FontWeight.Normal); + ftfontMan.registerFont(fontsPath ~ "times.ttf", FontFamily.Serif, "Times New Roman", false, FontWeight.Normal); + ftfontMan.registerFont(fontsPath ~ "timesbd.ttf", FontFamily.Serif, "Times New Roman", false, FontWeight.Bold); + ftfontMan.registerFont(fontsPath ~ "timesbi.ttf", FontFamily.Serif, "Times New Roman", true, FontWeight.Bold); + ftfontMan.registerFont(fontsPath ~ "timesi.ttf", FontFamily.Serif, "Times New Roman", true, FontWeight.Normal); + ftfontMan.registerFont(fontsPath ~ "consola.ttf", FontFamily.MonoSpace, "Consolas", false, FontWeight.Normal); + ftfontMan.registerFont(fontsPath ~ "consolab.ttf", FontFamily.MonoSpace, "Consolas", false, FontWeight.Bold); + ftfontMan.registerFont(fontsPath ~ "consolai.ttf", FontFamily.MonoSpace, "Consolas", true, FontWeight.Normal); + ftfontMan.registerFont(fontsPath ~ "consolaz.ttf", FontFamily.MonoSpace, "Consolas", true, FontWeight.Bold); + ftfontMan.registerFont(fontsPath ~ "verdana.ttf", FontFamily.SansSerif, "Verdana", false, FontWeight.Normal); + 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 ~ "verdanaz.ttf", FontFamily.SansSerif, "Verdana", true, FontWeight.Bold); + if (ftfontMan.registeredFontCount()) { + FontManager.instance = ftfontMan; + } else { + Log.w("No fonts registered in FreeType font manager. Disabling FreeType."); + destroy(ftfontMan); } } - 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 ~ "arialbi.ttf", FontFamily.SansSerif, "Arial", true, FontWeight.Bold); - ftfontMan.registerFont(fontsPath ~ "ariali.ttf", FontFamily.SansSerif, "Arial", true, FontWeight.Normal); - ftfontMan.registerFont(fontsPath ~ "cour.ttf", FontFamily.MonoSpace, "Courier New", false, FontWeight.Normal); - ftfontMan.registerFont(fontsPath ~ "courbd.ttf", FontFamily.MonoSpace, "Courier New", false, FontWeight.Bold); - ftfontMan.registerFont(fontsPath ~ "courbi.ttf", FontFamily.MonoSpace, "Courier New", true, FontWeight.Bold); - ftfontMan.registerFont(fontsPath ~ "couri.ttf", FontFamily.MonoSpace, "Courier New", true, FontWeight.Normal); - ftfontMan.registerFont(fontsPath ~ "times.ttf", FontFamily.Serif, "Times New Roman", false, FontWeight.Normal); - ftfontMan.registerFont(fontsPath ~ "timesbd.ttf", FontFamily.Serif, "Times New Roman", false, FontWeight.Bold); - ftfontMan.registerFont(fontsPath ~ "timesbi.ttf", FontFamily.Serif, "Times New Roman", true, FontWeight.Bold); - ftfontMan.registerFont(fontsPath ~ "timesi.ttf", FontFamily.Serif, "Times New Roman", true, FontWeight.Normal); - ftfontMan.registerFont(fontsPath ~ "consola.ttf", FontFamily.MonoSpace, "Consolas", false, FontWeight.Normal); - ftfontMan.registerFont(fontsPath ~ "consolab.ttf", FontFamily.MonoSpace, "Consolas", false, FontWeight.Bold); - ftfontMan.registerFont(fontsPath ~ "consolai.ttf", FontFamily.MonoSpace, "Consolas", true, FontWeight.Normal); - ftfontMan.registerFont(fontsPath ~ "consolaz.ttf", FontFamily.MonoSpace, "Consolas", true, FontWeight.Bold); - ftfontMan.registerFont(fontsPath ~ "verdana.ttf", FontFamily.SansSerif, "Verdana", false, FontWeight.Normal); - 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 ~ "verdanaz.ttf", FontFamily.SansSerif, "Verdana", true, FontWeight.Bold); - FontManager.instance = ftfontMan; + } catch (Exception e) { + Log.e("Cannot create FreeTypeFontManager - falling back to win32"); } // use Win32 font manager