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
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 // 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,46 +900,67 @@ int myWinMain(void* hInstance, void* hPrevInstance, char* lpCmdLine, int iCmdSho
Platform.setInstance(w32platform); Platform.setInstance(w32platform);
/// testing freetype font manager
version(USE_FREETYPE) { try {
import dlangui.graphics.ftfonts; /// testing freetype font manager
import win32.shlobj; version(USE_FREETYPE) {
FreeTypeFontManager ftfontMan = new FreeTypeFontManager(); import dlangui.graphics.ftfonts;
string fontsPath = "c:\\Windows\\Fonts\\"; // trying to create font manager
static if (false) { // SHGetFolderPathW not found in shell32.lib FreeTypeFontManager ftfontMan = new FreeTypeFontManager();
WCHAR[MAX_PATH] szPath;
const CSIDL_FLAG_NO_ALIAS = 0x1000; import win32.shlobj;
const CSIDL_FLAG_DONT_UNEXPAND = 0x2000; string fontsPath = "c:\\Windows\\Fonts\\";
if(SUCCEEDED(SHGetFolderPathW(NULL, static if (true) { // SHGetFolderPathW not found in shell32.lib
CSIDL_FONTS|CSIDL_FLAG_NO_ALIAS|CSIDL_FLAG_DONT_UNEXPAND, WCHAR[MAX_PATH] szPath;
NULL, static if (false) {
0, const CSIDL_FLAG_NO_ALIAS = 0x1000;
szPath.ptr))) const CSIDL_FLAG_DONT_UNEXPAND = 0x2000;
{ if(SUCCEEDED(SHGetFolderPathW(NULL,
fontsPath = toUTF8(fromWStringz(szPath)); 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); } catch (Exception e) {
ftfontMan.registerFont(fontsPath ~ "arialbd.ttf", FontFamily.SansSerif, "Arial", false, FontWeight.Bold); Log.e("Cannot create FreeTypeFontManager - falling back to win32");
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;
} }
// use Win32 font manager // use Win32 font manager