From 347eb1e720ab2f09284221a01ac74cb810bcd5b3 Mon Sep 17 00:00:00 2001 From: foudfou Date: Mon, 17 Feb 2014 22:24:36 +0100 Subject: [PATCH] iron out text icon creation --- src/modules/FiretrayHandler.jsm | 1 + src/modules/ctypes/winnt/gdi32.jsm | 9 +++++ src/modules/winnt/FiretrayStatusIcon.jsm | 42 ++++++++++++++---------- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/modules/FiretrayHandler.jsm b/src/modules/FiretrayHandler.jsm index 0b9e599..9c28084 100644 --- a/src/modules/FiretrayHandler.jsm +++ b/src/modules/FiretrayHandler.jsm @@ -517,6 +517,7 @@ firetray.PrefListener = new PrefListener( } break; case 'mail_notification_type': + case 'icon_text_color': firetray.Messaging.updateIcon(); break; case 'new_mail_icon_names': diff --git a/src/modules/ctypes/winnt/gdi32.jsm b/src/modules/ctypes/winnt/gdi32.jsm index 72ea262..bb05592 100644 --- a/src/modules/ctypes/winnt/gdi32.jsm +++ b/src/modules/ctypes/winnt/gdi32.jsm @@ -104,6 +104,15 @@ function gdi32_defines(lib) { this.THAI_CHARSET = 222; this.EASTEUROPE_CHARSET = 238; this.RUSSIAN_CHARSET = 204; + this.DEFAULT_QUALITY = 0; + this.DRAFT_QUALITY = 1; + this.PROOF_QUALITY = 2; + this.NONANTIALIASED_QUALITY = 3; + this.ANTIALIASED_QUALITY = 4; + this.CLEARTYPE_QUALITY = 5; + this.CLEARTYPE_NATURAL_QUALITY = 6; + + lib.lazy_bind("GetTextFaceW", ctypes.int, win32.HDC, ctypes.int, win32.LPTSTR); lib.lazy_bind("SetTextColor", win32.COLORREF, win32.HDC, win32.COLORREF); lib.lazy_bind("SetBkMode", ctypes.int, win32.HDC, ctypes.int); this.TRANSPARENT = 1; diff --git a/src/modules/winnt/FiretrayStatusIcon.jsm b/src/modules/winnt/FiretrayStatusIcon.jsm index fb13abd..6138e19 100644 --- a/src/modules/winnt/FiretrayStatusIcon.jsm +++ b/src/modules/winnt/FiretrayStatusIcon.jsm @@ -285,29 +285,37 @@ firetray.StatusIcon = { // http://forums.codeguru.com/showthread.php?379565-Windows-SDK-GDI-How-do-I-choose-a-font-size-to-exactly-fit-a-string-in-a - let nHeight = 32, fnWeight = gdi32.FW_BOLD; - let hFont = gdi32.CreateFontW(nHeight, 0, 0, 0, fnWeight, 0, 0, 0, + let fnHeight = firetray.js.floatToInt(height); + let hFont = gdi32.CreateFontW(fnHeight, 0, 0, 0, gdi32.FW_MEDIUM, 0, 0, 0, gdi32.ANSI_CHARSET, 0, 0, 0, gdi32.FF_SWISS, "Sans"); // get font - hFont = ctypes.cast(gdi32.SelectObject(hdcMem, hFont), win32.HFONT); // replace font in bitmap by hFont - gdi32.SetTextColor(hdcMem, win32.COLORREF(this.cssColorToCOLORREF(color))); - gdi32.SetBkMode(hdcMem, gdi32.TRANSPARENT); // VERY IMPORTANT - // gdi32.SetTextAlign(hdcMem, gdi32.GetTextAlign(hdcMem) & (~gdi32.TA_CENTER)); - // gdi32.SetTextAlign(hdcMem, gdi32.TA_CENTER); - log.debug(" ___ALIGN=(winLastError="+ctypes.winLastError+") "+gdi32.GetTextAlign(hdcMem)); + ctypes.cast(gdi32.SelectObject(hdcMem, hFont), win32.HFONT); // replace font in bitmap by hFont + let faceName = ctypes.jschar.array()(32); + gdi32.GetTextFaceW(hdcMem, 32, faceName); + log.debug(" font="+faceName); let size = new gdi32.SIZE(); - // GetTextExtentPoint32 is known as more reliable than DrawText(DT_CALCRECT) + gdi32.GetTextExtentPoint32W(hdcMem, text, text.length, size.address()); // more reliable than DrawText(DT_CALCRECT) + + while (size.cx > width - 6 || size.cy > height - 4) { + fnHeight -= 1; + hFont = gdi32.CreateFontW(fnHeight, 0, 0, 0, gdi32.FW_SEMIBOLD, 0, 0, 0, + gdi32.ANSI_CHARSET, 0, 0, gdi32.PROOF_QUALITY, + gdi32.FF_SWISS, "Arial"); + ctypes.cast(gdi32.SelectObject(hdcMem, hFont), win32.HFONT); + + gdi32.GetTextExtentPoint32W(hdcMem, text, text.length, size.address()); // more reliable than DrawText(DT_CALCRECT) + log.debug(" fnHeight="+fnHeight+" width="+size.cx); + } gdi32.GetTextExtentPoint32W(hdcMem, text, text.length, size.address()); - let nWidth = size.cx; - log.debug(" WIDTH="+nWidth); - // let rect = new win32.RECT(); - // let height = user32.DrawTextW(hdcMem, text, text.length, rect.address(), user32.DT_SINGLELINE | user32.DT_CENTER | user32.DT_VCENTER | user32.DT_CALCRECT); - // log.debug(" HEIGHT="+height+", rect="+rect); + gdi32.SetTextColor(hdcMem, win32.COLORREF(this.cssColorToCOLORREF(color))); + gdi32.SetBkMode(hdcMem, gdi32.TRANSPARENT); // VERY IMPORTANT + gdi32.SetTextAlign(hdcMem, gdi32.TA_TOP|gdi32.TA_CENTER); + log.debug(" ___ALIGN=(winLastError="+ctypes.winLastError+") "+gdi32.GetTextAlign(hdcMem)); - let nXStart = firetray.js.floatToInt((width - nWidth)/2), - nYStart = firetray.js.floatToInt((height - nHeight)/2); - gdi32.TextOutW(hdcMem, nXStart, nYStart, text, text.length); // ref point for alignment + let nXStart = firetray.js.floatToInt((width - size.cx)/2), + nYStart = firetray.js.floatToInt((height - size.cy)/2); + gdi32.TextOutW(hdcMem, width/2, nYStart+2, text, text.length); // ref point for alignment gdi32.SelectObject(hdcMem, hBitmapOrig);