//TODO: Double check/ensure this function is only called via the gui thread.. SIZE ProjectTreeItem::GetPreferredSize() { SIZE sz = defaultSize; if(!m_size.cx) { m_size.cy = defaultSize.cy; HDC dc = GetDC(m_parentHwnd); if(dc) { HGDIOBJ oldFont = SelectObject(dc, s_hFont); SetTextAlign(dc, TA_LEFT); loadngo::gui::NativeString title(m_pTask->GetTitle()); const TCHAR *pTitle = title; long strLen = _tcslen(pTitle); SCRIPT_STRING_ANALYSIS ssa; SecureZeroMemory(&ssa, sizeof(SCRIPT_STRING_ANALYSIS)); HRESULT hr = ScriptStringAnalyse(dc, pTitle, strLen, 0, -1, SSA_GLYPHS, 0, 0, 0, 0, 0, 0, &ssa); if(SUCCEEDED(hr)) { sz.cx = ScriptString_pSize(ssa)->cx + 20; ScriptStringFree(&ssa); m_size.cx = sz.cx > defaultSize.cx? sz.cx: defaultSize.cx; } SelectObject(dc, oldFont); ReleaseDC(m_parentHwnd, dc); } } return m_size.cx? m_size: sz; }
void unicodeMeasureString(char *utf8, int utf8Length, int *wPtr, int *hPtr) { SCRIPT_STRING_ANALYSIS ssa; HDC hdc; CONST SIZE *pSize; if (utf8Length == 0) return; hdc = CreateCompatibleDC(0); ssa = analyze(hdc, utf8, utf8Length); if (ssa != NULL) { pSize = ScriptString_pSize(ssa); if (pSize != NULL) { *wPtr = pSize->cx; *hPtr = pSize->cy; } ScriptStringFree(&ssa); } DeleteDC(hdc); }
void unicodeDrawString(char *utf8, int utf8Length, int *wPtr, int *hPtr, unsigned int *bitmapPtr) { SCRIPT_STRING_ANALYSIS ssa; HDC hdc; BITMAPINFO bi; HBITMAP hBitmap; unsigned int *dibBits; HGDIOBJ oldObj; CONST SIZE *pSize; int w = *wPtr; int h = *hPtr; unsigned int *src, *dst, *end; *wPtr = *hPtr = 0; if (utf8Length == 0) return; hdc = CreateCompatibleDC(0); ssa = analyze(hdc, utf8, utf8Length); if (ssa == NULL) goto cleanup; // create a device independent bitmap bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bi.bmiHeader.biWidth = w; bi.bmiHeader.biHeight = -h; // negative indicates top-down bitmap bi.bmiHeader.biPlanes = 1; bi.bmiHeader.biBitCount = 32; bi.bmiHeader.biCompression = BI_RGB; bi.bmiHeader.biSizeImage = 0; bi.bmiHeader.biXPelsPerMeter = 0; bi.bmiHeader.biYPelsPerMeter = 0; bi.bmiHeader.biClrUsed = 0; bi.bmiHeader.biClrImportant = 0; hBitmap = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, &dibBits, NULL, 0); if (hBitmap == NULL) goto cleanup; // attached the bitmap to the context oldObj = SelectObject(hdc, hBitmap); if (oldObj != NULL) { // set fg and bg colors and render the string SetBkColor(hdc, g_bgColor); SetTextColor(hdc, g_fgColor); ScriptStringOut(ssa, 0, 0, 0, NULL, 0, 0, FALSE); pSize = ScriptString_pSize(ssa); if (pSize != NULL) { *wPtr = pSize->cx; *hPtr = pSize->cy; } // copy pixels into Squeak's bitmap src = dibBits; dst = (int *) bitmapPtr; end = dst + (w * h); if (g_bgTransparent && (g_bgRGB != 0)) { // if g_bgTransparent was set, map g_bgRGB to 0 (transparent) while (dst < end) { *dst++ = (*src == g_bgRGB) ? 0 : *src; src++; } } else { while (dst < end) *dst++ = *src++; } SelectObject(hdc, oldObj); } ScriptStringFree(&ssa); DeleteObject(hBitmap); cleanup: DeleteDC(hdc); }