float SimpleFontData::platformWidthForGlyph(Glyph glyph) const { if (m_platformData.useGDI()) return widthForGDIGlyph(glyph); if (!m_platformData.size()) return 0; HDC hdc = GetDC(0); SaveDC(hdc); cairo_scaled_font_t* scaledFont = m_platformData.scaledFont(); cairo_win32_scaled_font_select_font(scaledFont, hdc); int width; GetCharWidthI(hdc, glyph, 1, 0, &width); cairo_win32_scaled_font_done_font(scaledFont); RestoreDC(hdc, -1); ReleaseDC(0, hdc); const double metricsMultiplier = cairo_win32_scaled_font_get_metrics_factor(scaledFont) * m_platformData.size(); return width * metricsMultiplier; }
static gboolean pango_cairo_win32_font_select_font (PangoFont *font, HDC hdc) { cairo_scaled_font_t *scaled_font = pango_cairo_win32_font_get_scaled_font (PANGO_CAIRO_FONT (font)); return cairo_win32_scaled_font_select_font (scaled_font, hdc) == CAIRO_STATUS_SUCCESS; }
void SimpleFontData::platformInit() { m_scriptCache = 0; m_scriptFontProperties = 0; m_isSystemFont = false; m_syntheticBoldOffset = m_platformData.syntheticBold() ? 1.0f : 0.f; if (m_platformData.useGDI()) return initGDIFont(); HDC hdc = GetDC(0); SaveDC(hdc); cairo_scaled_font_t* scaledFont = m_platformData.scaledFont(); const double metricsMultiplier = cairo_win32_scaled_font_get_metrics_factor(scaledFont) * m_platformData.size(); cairo_win32_scaled_font_select_font(scaledFont, hdc); TEXTMETRIC textMetrics; GetTextMetrics(hdc, &textMetrics); float ascent = textMetrics.tmAscent * metricsMultiplier; float descent = textMetrics.tmDescent * metricsMultiplier; float xHeight = ascent * 0.56f; // Best guess for xHeight for non-Truetype fonts. float lineGap = textMetrics.tmExternalLeading * metricsMultiplier; m_fontMetrics.setAscent(ascent); m_fontMetrics.setDescent(descent); m_fontMetrics.setLineGap(lineGap); m_fontMetrics.setLineSpacing(lroundf(ascent) + lroundf(descent) + lroundf(lineGap)); m_avgCharWidth = textMetrics.tmAveCharWidth * metricsMultiplier; m_maxCharWidth = textMetrics.tmMaxCharWidth * metricsMultiplier; OUTLINETEXTMETRIC metrics; if (GetOutlineTextMetrics(hdc, sizeof(metrics), &metrics) > 0) { // This is a TrueType font. We might be able to get an accurate xHeight GLYPHMETRICS gm; MAT2 mat = { 1, 0, 0, 1 }; DWORD len = GetGlyphOutline(hdc, 'x', GGO_METRICS, &gm, 0, 0, &mat); if (len != GDI_ERROR && gm.gmptGlyphOrigin.y > 0) xHeight = gm.gmptGlyphOrigin.y * metricsMultiplier; } m_fontMetrics.setXHeight(xHeight); cairo_win32_scaled_font_done_font(scaledFont); m_isSystemFont = false; m_scriptCache = 0; m_scriptFontProperties = 0; RestoreDC(hdc, -1); ReleaseDC(0, hdc); }
void SelectFont() { if (mFontSelected) return; cairo_t *cr = mContext->GetCairo(); cairo_set_font_face(cr, mShaper->GetFont()->CairoFontFace()); cairo_set_font_size(cr, mShaper->GetFont()->GetAdjustedSize()); cairo_scaled_font_t *scaledFont = mShaper->GetFont()->CairoScaledFont(); cairo_win32_scaled_font_select_font(scaledFont, mDC); mFontSelected = true; }
PRBool gfxGDIFont::SetupCairoFont(gfxContext *aContext) { if (!mMetrics) { Initialize(); } if (!mScaledFont || cairo_scaled_font_status(mScaledFont) != CAIRO_STATUS_SUCCESS) { // Don't cairo_set_scaled_font as that would propagate the error to // the cairo_t, precluding any further drawing. return PR_FALSE; } cairo_set_scaled_font(aContext->GetCairo(), mScaledFont); cairo_win32_scaled_font_select_font(mScaledFont, DCFromContext(aContext)); return PR_TRUE; }
void Font::platformInit() { m_syntheticBoldOffset = m_platformData.syntheticBold() ? 1.0f : 0.f; m_scriptCache = 0; m_scriptFontProperties = 0; m_isSystemFont = false; if (m_platformData.useGDI()) return initGDIFont(); if (!m_platformData.size()) { m_fontMetrics.reset(); m_avgCharWidth = 0; m_maxCharWidth = 0; m_isSystemFont = false; m_scriptCache = 0; m_scriptFontProperties = 0; return; } HWndDC dc(0); SaveDC(dc); cairo_scaled_font_t* scaledFont = m_platformData.scaledFont(); const double metricsMultiplier = cairo_win32_scaled_font_get_metrics_factor(scaledFont) * m_platformData.size(); cairo_win32_scaled_font_select_font(scaledFont, dc); TEXTMETRIC textMetrics; GetTextMetrics(dc, &textMetrics); float ascent = textMetrics.tmAscent * metricsMultiplier; float descent = textMetrics.tmDescent * metricsMultiplier; float xHeight = ascent * 0.56f; // Best guess for xHeight for non-Truetype fonts. float lineGap = textMetrics.tmExternalLeading * metricsMultiplier; int faceLength = ::GetTextFace(dc, 0, 0); Vector<WCHAR> faceName(faceLength); ::GetTextFace(dc, faceLength, faceName.data()); m_isSystemFont = !wcscmp(faceName.data(), L"Lucida Grande"); ascent = ascentConsideringMacAscentHack(faceName.data(), ascent, descent); m_fontMetrics.setAscent(ascent); m_fontMetrics.setDescent(descent); m_fontMetrics.setLineGap(lineGap); m_fontMetrics.setLineSpacing(lroundf(ascent) + lroundf(descent) + lroundf(lineGap)); m_avgCharWidth = textMetrics.tmAveCharWidth * metricsMultiplier; m_maxCharWidth = textMetrics.tmMaxCharWidth * metricsMultiplier; cairo_text_extents_t extents; cairo_scaled_font_text_extents(scaledFont, "x", &extents); xHeight = -extents.y_bearing; m_fontMetrics.setXHeight(xHeight); cairo_win32_scaled_font_done_font(scaledFont); m_scriptCache = 0; m_scriptFontProperties = 0; RestoreDC(dc, -1); }
cairo_int_status_t _cairo_win32_surface_emit_glyphs (cairo_win32_surface_t *dst, const cairo_pattern_t *source, cairo_glyph_t *glyphs, int num_glyphs, cairo_scaled_font_t *scaled_font, cairo_bool_t glyph_indexing) { #if CAIRO_HAS_WIN32_FONT WORD glyph_buf_stack[STACK_GLYPH_SIZE]; WORD *glyph_buf = glyph_buf_stack; int dxy_buf_stack[2 * STACK_GLYPH_SIZE]; int *dxy_buf = dxy_buf_stack; BOOL win_result = 0; int i, j; cairo_solid_pattern_t *solid_pattern; COLORREF color; cairo_matrix_t device_to_logical; int start_x, start_y; double user_x, user_y; int logical_x, logical_y; unsigned int glyph_index_option; /* We can only handle win32 fonts */ assert (cairo_scaled_font_get_type (scaled_font) == CAIRO_FONT_TYPE_WIN32); /* We can only handle opaque solid color sources and destinations */ assert (_cairo_pattern_is_opaque_solid(source)); assert (dst->format == CAIRO_FORMAT_RGB24); solid_pattern = (cairo_solid_pattern_t *)source; color = RGB(((int)solid_pattern->color.red_short) >> 8, ((int)solid_pattern->color.green_short) >> 8, ((int)solid_pattern->color.blue_short) >> 8); cairo_win32_scaled_font_get_device_to_logical(scaled_font, &device_to_logical); SaveDC(dst->dc); cairo_win32_scaled_font_select_font(scaled_font, dst->dc); SetTextColor(dst->dc, color); SetTextAlign(dst->dc, TA_BASELINE | TA_LEFT); SetBkMode(dst->dc, TRANSPARENT); if (num_glyphs > STACK_GLYPH_SIZE) { glyph_buf = (WORD *) _cairo_malloc_ab (num_glyphs, sizeof(WORD)); dxy_buf = (int *) _cairo_malloc_abc (num_glyphs, sizeof(int), 2); } /* It is vital that dx values for dxy_buf are calculated from the delta of * _logical_ x coordinates (not user x coordinates) or else the sum of all * previous dx values may start to diverge from the current glyph's x * coordinate due to accumulated rounding error. As a result strings could * be painted shorter or longer than expected. */ user_x = glyphs[0].x; user_y = glyphs[0].y; cairo_matrix_transform_point(&device_to_logical, &user_x, &user_y); logical_x = _cairo_lround (user_x); logical_y = _cairo_lround (user_y); start_x = logical_x; start_y = logical_y; for (i = 0, j = 0; i < num_glyphs; ++i, j = 2 * i) { glyph_buf[i] = (WORD) glyphs[i].index; if (i == num_glyphs - 1) { dxy_buf[j] = 0; dxy_buf[j+1] = 0; } else { double next_user_x = glyphs[i+1].x; double next_user_y = glyphs[i+1].y; int next_logical_x, next_logical_y; cairo_matrix_transform_point(&device_to_logical, &next_user_x, &next_user_y); next_logical_x = _cairo_lround (next_user_x); next_logical_y = _cairo_lround (next_user_y); dxy_buf[j] = _cairo_lround (next_logical_x - logical_x); dxy_buf[j+1] = _cairo_lround (next_logical_y - logical_y); logical_x = next_logical_x; logical_y = next_logical_y; } } if (glyph_indexing) glyph_index_option = ETO_GLYPH_INDEX; else glyph_index_option = 0; win_result = ExtTextOutW(dst->dc, start_x, start_y, glyph_index_option | ETO_PDY, NULL, glyph_buf, num_glyphs, dxy_buf); if (!win_result) { _cairo_win32_print_gdi_error("_cairo_win32_surface_show_glyphs(ExtTextOutW failed)"); } RestoreDC(dst->dc, -1); if (glyph_buf != glyph_buf_stack) { free(glyph_buf); free(dxy_buf); } return (win_result) ? CAIRO_STATUS_SUCCESS : CAIRO_INT_STATUS_UNSUPPORTED; #else return CAIRO_INT_STATUS_UNSUPPORTED; #endif }