fix win32 font drawing

This commit is contained in:
Vadim Lopatin 2014-03-25 12:36:33 +04:00
parent 7eaf08aec3
commit 4187da1a2d
4 changed files with 80 additions and 39 deletions

View File

@ -226,8 +226,10 @@ class FontManager {
static __gshared FontManager _instance; static __gshared FontManager _instance;
/// sets new font manager singleton instance /// sets new font manager singleton instance
static @property void instance(FontManager manager) { static @property void instance(FontManager manager) {
if (_instance !is null) if (_instance !is null) {
destroy(_instance); destroy(_instance);
_instance = null;
}
_instance = manager; _instance = manager;
} }
/// returns font manager singleton instance /// returns font manager singleton instance

View File

@ -266,6 +266,9 @@ private class FreeTypeFontFile {
for (int i = 0; i < sz; i++) for (int i = 0; i < sz; i++)
glyph.glyph[i] = bitmap.buffer[i]; glyph.glyph[i] = bitmap.buffer[i];
} }
version (USE_OPENGL) {
glyph.id = nextGlyphId();
}
} }
return true; return true;
} }

View File

@ -203,6 +203,37 @@ class Win32Font : Font {
} }
} }
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) { override int measureText(const dchar[] text, ref int[] widths, int maxWidth) {
if (_hfont is null || _drawbuf is null || text.length == 0) if (_hfont is null || _drawbuf is null || text.length == 0)
return 0; return 0;
@ -236,6 +267,7 @@ class Win32Font : Font {
} }
return measured; return measured;
} }
}
bool create(FontDef * def, int size, int weight, bool italic) { bool create(FontDef * def, int size, int weight, bool italic) {
if (!isNull()) if (!isNull())
@ -300,7 +332,7 @@ class Win32FontManager : FontManager {
/// initialize in constructor /// initialize in constructor
this() { this() {
Log.i("Creating Win32FontManager"); Log.i("Creating Win32FontManager");
instance = this; //instance = this;
init(); init();
} }
~this() { ~this() {

View File

@ -201,6 +201,7 @@ class Win32Window : Window {
version (USE_OPENGL) { version (USE_OPENGL) {
private void paintUsingOpenGL() { private void paintUsingOpenGL() {
Log.d("paintUsingOpenGL()");
// hack to stop infinite WM_PAINT loop // hack to stop infinite WM_PAINT loop
PAINTSTRUCT ps; PAINTSTRUCT ps;
HDC hdc2 = BeginPaint(_hwnd, &ps); HDC hdc2 = BeginPaint(_hwnd, &ps);
@ -222,12 +223,14 @@ class Win32Window : Window {
float r = ((_backgroundColor >> 16) & 255) / 255.0f; float r = ((_backgroundColor >> 16) & 255) / 255.0f;
float g = ((_backgroundColor >> 8) & 255) / 255.0f; float g = ((_backgroundColor >> 8) & 255) / 255.0f;
float b = ((_backgroundColor >> 0) & 255) / 255.0f; float b = ((_backgroundColor >> 0) & 255) / 255.0f;
Log.d("paintUsingOpenGL() - clearing buffer");
glClearColor(r, g, b, a); glClearColor(r, g, b, a);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
Log.d("paintUsingOpenGL() - creating drawbuf");
GLDrawBuf buf = new GLDrawBuf(_dx, _dy, false); GLDrawBuf buf = new GLDrawBuf(_dx, _dy, false);
buf.beforeDrawing(); buf.beforeDrawing();
if (false) { static if (false) {
// for testing for render // for testing for render
buf.fillRect(Rect(100, 100, 200, 200), 0x704020); buf.fillRect(Rect(100, 100, 200, 200), 0x704020);
buf.fillRect(Rect(40, 70, 100, 120), 0x000000); buf.fillRect(Rect(40, 70, 100, 120), 0x000000);
@ -241,8 +244,9 @@ class Win32Window : Window {
} else { } else {
onDraw(buf); onDraw(buf);
} }
Log.d("paintUsingOpenGL() - calling buf.afterDrawing");
buf.afterDrawing(); buf.afterDrawing();
//Log.d("onPaint() end drawing opengl"); Log.d("onPaint() end drawing opengl");
SwapBuffers(hdc); SwapBuffers(hdc);
wglMakeCurrent(hdc, null); wglMakeCurrent(hdc, null);
} }
@ -595,8 +599,8 @@ int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int
// use Win32 font manager // use Win32 font manager
if (FontManager.instance is null) { if (FontManager.instance is null) {
Win32FontManager fontMan = new Win32FontManager(); //Win32FontManager fontMan = new Win32FontManager();
FontManager.instance = fontMan; FontManager.instance = new Win32FontManager();
} }
currentTheme = createDefaultTheme(); currentTheme = createDefaultTheme();