diff --git a/src/dlangui/graphics/fonts.d b/src/dlangui/graphics/fonts.d index 5e0f700a..a4bb2769 100644 --- a/src/dlangui/graphics/fonts.d +++ b/src/dlangui/graphics/fonts.d @@ -226,8 +226,10 @@ class FontManager { static __gshared FontManager _instance; /// sets new font manager singleton instance static @property void instance(FontManager manager) { - if (_instance !is null) + if (_instance !is null) { destroy(_instance); + _instance = null; + } _instance = manager; } /// returns font manager singleton instance diff --git a/src/dlangui/graphics/ftfonts.d b/src/dlangui/graphics/ftfonts.d index 2e8b1086..8374ac65 100644 --- a/src/dlangui/graphics/ftfonts.d +++ b/src/dlangui/graphics/ftfonts.d @@ -266,6 +266,9 @@ private class FreeTypeFontFile { for (int i = 0; i < sz; i++) glyph.glyph[i] = bitmap.buffer[i]; } + version (USE_OPENGL) { + glyph.id = nextGlyphId(); + } } return true; } diff --git a/src/dlangui/platforms/windows/win32fonts.d b/src/dlangui/platforms/windows/win32fonts.d index d0f1ca7c..f8407a33 100644 --- a/src/dlangui/platforms/windows/win32fonts.d +++ b/src/dlangui/platforms/windows/win32fonts.d @@ -203,39 +203,71 @@ class Win32Font : Font { } } - override int measureText(const dchar[] text, ref int[] widths, int maxWidth) { - if (_hfont is null || _drawbuf is null || text.length == 0) - return 0; - wstring utf16text = toUTF16(text); - const wchar * pstr = utf16text.ptr; - uint len = cast(uint)utf16text.length; - GCP_RESULTSW gcpres; - gcpres.lStructSize = gcpres.sizeof; - if (widths.length < len + 1) - widths.length = len + 1; - gcpres.lpDx = widths.ptr; - gcpres.nMaxFit = len; - gcpres.nGlyphs = len; - uint res = GetCharacterPlacementW( - _drawbuf.dc, - pstr, - len, - maxWidth, - &gcpres, - GCP_MAXEXTENT); //|GCP_USEKERNING - if (!res) { - widths[0] = 0; - return 0; - } - uint measured = gcpres.nMaxFit; - int total = 0; - for (int i = 0; i < measured; i++) { - int w = widths[i]; - total += w; - widths[i] = total; - } - return measured; - } + static if (true) { + override int measureText(const dchar[] text, ref int[] widths, int maxWidth) { + if (text.length == 0) + return 0; + const dchar * pstr = text.ptr; + uint len = cast(uint)text.length; + if (widths.length < len) + widths.length = len; + int x = 0; + int charsMeasured = 0; + for (int i = 0; i < len; i++) { + Glyph * glyph = getCharGlyph(text[i], true); // TODO: what is better + if (glyph is null) { + // if no glyph, use previous width - treat as zero width + widths[i] = i > 0 ? widths[i-1] : 0; + continue; + } + int w = x + glyph.width; // using advance + int w2 = x + glyph.originX + glyph.blackBoxX; // using black box + if (w < w2) // choose bigger value + w = w2; + widths[i] = w; + x += glyph.width; + charsMeasured = i + 1; + if (x > maxWidth) + break; + } + return charsMeasured; + } + } else { + + override int measureText(const dchar[] text, ref int[] widths, int maxWidth) { + if (_hfont is null || _drawbuf is null || text.length == 0) + return 0; + wstring utf16text = toUTF16(text); + const wchar * pstr = utf16text.ptr; + uint len = cast(uint)utf16text.length; + GCP_RESULTSW gcpres; + gcpres.lStructSize = gcpres.sizeof; + if (widths.length < len + 1) + widths.length = len + 1; + gcpres.lpDx = widths.ptr; + gcpres.nMaxFit = len; + gcpres.nGlyphs = len; + uint res = GetCharacterPlacementW( + _drawbuf.dc, + pstr, + len, + maxWidth, + &gcpres, + GCP_MAXEXTENT); //|GCP_USEKERNING + if (!res) { + widths[0] = 0; + return 0; + } + uint measured = gcpres.nMaxFit; + int total = 0; + for (int i = 0; i < measured; i++) { + int w = widths[i]; + total += w; + widths[i] = total; + } + return measured; + } + } bool create(FontDef * def, int size, int weight, bool italic) { if (!isNull()) @@ -300,7 +332,7 @@ class Win32FontManager : FontManager { /// initialize in constructor this() { Log.i("Creating Win32FontManager"); - instance = this; + //instance = this; init(); } ~this() { diff --git a/src/dlangui/platforms/windows/winapp.d b/src/dlangui/platforms/windows/winapp.d index b5bc48d8..cca16ecf 100644 --- a/src/dlangui/platforms/windows/winapp.d +++ b/src/dlangui/platforms/windows/winapp.d @@ -201,6 +201,7 @@ class Win32Window : Window { version (USE_OPENGL) { private void paintUsingOpenGL() { + Log.d("paintUsingOpenGL()"); // hack to stop infinite WM_PAINT loop PAINTSTRUCT ps; HDC hdc2 = BeginPaint(_hwnd, &ps); @@ -222,12 +223,14 @@ class Win32Window : Window { float r = ((_backgroundColor >> 16) & 255) / 255.0f; float g = ((_backgroundColor >> 8) & 255) / 255.0f; float b = ((_backgroundColor >> 0) & 255) / 255.0f; + Log.d("paintUsingOpenGL() - clearing buffer"); glClearColor(r, g, b, a); glClear(GL_COLOR_BUFFER_BIT); + Log.d("paintUsingOpenGL() - creating drawbuf"); GLDrawBuf buf = new GLDrawBuf(_dx, _dy, false); buf.beforeDrawing(); - if (false) { + static if (false) { // for testing for render buf.fillRect(Rect(100, 100, 200, 200), 0x704020); buf.fillRect(Rect(40, 70, 100, 120), 0x000000); @@ -241,8 +244,9 @@ class Win32Window : Window { } else { onDraw(buf); } + Log.d("paintUsingOpenGL() - calling buf.afterDrawing"); buf.afterDrawing(); - //Log.d("onPaint() end drawing opengl"); + Log.d("onPaint() end drawing opengl"); SwapBuffers(hdc); wglMakeCurrent(hdc, null); } @@ -595,8 +599,8 @@ int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int // use Win32 font manager if (FontManager.instance is null) { - Win32FontManager fontMan = new Win32FontManager(); - FontManager.instance = fontMan; + //Win32FontManager fontMan = new Win32FontManager(); + FontManager.instance = new Win32FontManager(); } currentTheme = createDefaultTheme();