FontPlatformData FontCustomPlatformData::fontPlatformData(const FontDescription& fontDescription, bool bold, bool italic) { int size = fontDescription.computedPixelSize(); FontRenderingMode renderingMode = fontDescription.renderingMode(); LOGFONT logFont; memset(&logFont, 0, sizeof(LOGFONT)); wcsncpy(logFont.lfFaceName, m_name.charactersWithNullTermination().data(), LF_FACESIZE - 1); logFont.lfHeight = -size; if (renderingMode == FontRenderingMode::Normal) logFont.lfHeight *= 32; logFont.lfWidth = 0; logFont.lfEscapement = 0; logFont.lfOrientation = 0; logFont.lfUnderline = false; logFont.lfStrikeOut = false; logFont.lfCharSet = DEFAULT_CHARSET; logFont.lfOutPrecision = OUT_TT_ONLY_PRECIS; logFont.lfQuality = CLEARTYPE_QUALITY; logFont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE; logFont.lfItalic = italic; logFont.lfWeight = bold ? 700 : 400; auto hfont = adoptGDIObject(::CreateFontIndirect(&logFont)); cairo_font_face_t* fontFace = cairo_win32_font_face_create_for_hfont(hfont.get()); FontPlatformData fontPlatformData(WTFMove(hfont), fontFace, size, bold, italic); cairo_font_face_destroy(fontFace); return fontPlatformData; }
bool MCPDFPrintingDevice::create_cairo_font_from_custom_printer_font(const MCCustomPrinterFont &p_cp_font, cairo_font_face_t* &r_cairo_font) { bool t_success = true; cairo_font_face_t *t_font; t_font = cairo_win32_font_face_create_for_hfont((HFONT)(p_cp_font.handle)); t_success = (m_status = cairo_font_face_status(t_font)) == CAIRO_STATUS_SUCCESS; if (t_success) r_cairo_font = t_font; return t_success; }
FontCustomPlatformData* createFontCustomPlatformData(SharedBuffer* buffer) { ASSERT_ARG(buffer, buffer); buffer->ref(); HFONT font = reinterpret_cast<HFONT>(buffer); cairo_font_face_t* fontFace = cairo_win32_font_face_create_for_hfont(font); if (!fontFace) return 0; static cairo_user_data_key_t bufferKey; cairo_font_face_set_user_data(fontFace, &bufferKey, buffer, releaseData); return new FontCustomPlatformData(fontFace); }
void FontPlatformData::platformDataInit(HFONT font, float size, HDC hdc, WCHAR* faceName) { m_fontFace = cairo_win32_font_face_create_for_hfont(font); cairo_matrix_t sizeMatrix, ctm; cairo_matrix_init_identity(&ctm); cairo_matrix_init_scale(&sizeMatrix, size, size); static cairo_font_options_t* fontOptions = 0; if (!fontOptions) { fontOptions = cairo_font_options_create(); cairo_font_options_set_antialias(fontOptions, CAIRO_ANTIALIAS_SUBPIXEL); } m_scaledFont = cairo_scaled_font_create(m_fontFace, &sizeMatrix, &ctm, fontOptions); }
void FontPlatformData::platformDataInit(HFONT font, float size, HDC hdc, WCHAR* faceName) { cairo_font_face_t* fontFace = cairo_win32_font_face_create_for_hfont(font); cairo_matrix_t sizeMatrix, ctm; cairo_matrix_init_identity(&ctm); cairo_matrix_init_scale(&sizeMatrix, size, size); static cairo_font_options_t* fontOptions = nullptr; if (!fontOptions) { fontOptions = cairo_font_options_create(); cairo_font_options_set_antialias(fontOptions, CAIRO_ANTIALIAS_SUBPIXEL); } m_scaledFont = adoptRef(cairo_scaled_font_create(fontFace, &sizeMatrix, &ctm, fontOptions)); cairo_font_face_destroy(fontFace); if (!m_useGDI && m_size) m_isSystemFont = !wcscmp(faceName, L"Lucida Grande"); }
void FontPlatformData::platformDataInit(HFONT font, float size, HDC hdc, WCHAR* faceName) { m_fontFace = cairo_win32_font_face_create_for_hfont(font); cairo_matrix_t sizeMatrix, ctm; cairo_matrix_init_identity(&ctm); cairo_matrix_init_scale(&sizeMatrix, size, size); static cairo_font_options_t* fontOptions = 0; if (!fontOptions) { fontOptions = cairo_font_options_create(); #if PLATFORM(APOLLO) // In Apollo use the anti-aliasing mode selected by the user. See bug 2404418. cairo_font_options_set_antialias(fontOptions, CAIRO_ANTIALIAS_DEFAULT); #else cairo_font_options_set_antialias(fontOptions, CAIRO_ANTIALIAS_SUBPIXEL); #endif } m_scaledFont = cairo_scaled_font_create(m_fontFace, &sizeMatrix, &ctm, fontOptions); }
FontFace::FontFace( const std::string &faceName ) { #if defined( CAIRO_HAS_QUARTZ_FONT ) CFStringRef nameRef = CFStringCreateWithCString( kCFAllocatorDefault, faceName.c_str(), kCFStringEncodingUTF8 ); CGFontRef fontRef = CGFontCreateWithFontName( nameRef ); if( ! fontRef ) throw; mCairoFontFace = cairo_quartz_font_face_create_for_cgfont( fontRef ); // Cairo releases the CGFontRef CFRelease( nameRef ); #elif defined( CINDER_MSW ) HFONT hf; HDC hdc; long lfHeight; hdc = ::GetDC( NULL ); lfHeight = -MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72); ReleaseDC( NULL, hdc ); hf = CreateFontA( lfHeight, 0, 0, 0, 0, TRUE, 0, 0, 0, 0, 0, 0, 0, faceName.c_str() ); mCairoFontFace = cairo_win32_font_face_create_for_hfont( hf ); #endif }
void wxSVGCanvasTextCairo::InitText(const wxString& text, const wxCSSStyleDeclaration& style, wxSVGMatrix* matrix) { BeginChar(matrix); // create path from text cairo_t* cr = ((wxSVGCanvasPathCairo*) m_char->path)->GetCr(); #if defined(__WXMSW__) || defined(__WXMAC__) int size = (int) style.GetFontSize(); int fstyle = style.GetFontStyle() == wxCSS_VALUE_ITALIC ? wxFONTSTYLE_ITALIC : (style.GetFontStyle() == wxCSS_VALUE_OBLIQUE ? wxFONTSTYLE_SLANT : wxFONTSTYLE_NORMAL); wxFontWeight weight = style.GetFontWeight() == wxCSS_VALUE_BOLD ? wxFONTWEIGHT_BOLD : style.GetFontWeight() == wxCSS_VALUE_BOLDER ? wxFONTWEIGHT_MAX : style.GetFontWeight() == wxCSS_VALUE_LIGHTER ? wxFONTWEIGHT_LIGHT : wxFONTWEIGHT_NORMAL; wxFont fnt(size, wxFONTFAMILY_DEFAULT, fstyle, weight, false, style.GetFontFamily()); #ifdef __WXMSW__ HFONT hfont = (HFONT) fnt.GetResourceHandle(); cairo_set_font_face(cr, cairo_win32_font_face_create_for_hfont(hfont)); #else CGFontRef cgFont = fnt.OSXGetCGFont(); cairo_set_font_face(cr, cairo_quartz_font_face_create_for_cgfont(cgFont)); #endif cairo_set_font_size(cr, style.GetFontSize()); cairo_font_extents_t fextents; cairo_font_extents(cr, &fextents); double maxWidth = 0; if (style.GetTextAnchor() == wxCSS_VALUE_MIDDLE || style.GetTextAnchor() == wxCSS_VALUE_END) { wxStringTokenizer tokenzr(text, wxT("\n")); while (tokenzr.HasMoreTokens()) { wxString token = tokenzr.GetNextToken(); cairo_text_extents_t extents; cairo_text_extents(cr, (const char*) token.utf8_str(), &extents); if (maxWidth < extents.width) maxWidth = extents.width; } } wxStringTokenizer tokenzr(text, wxT("\n")); double x_advance = 0; double width = 0; double height = 0; double y = 0; while (tokenzr.HasMoreTokens()) { wxString token = tokenzr.GetNextToken(); // get text extents cairo_text_extents_t extents; cairo_text_extents(cr, (const char*) token.utf8_str(), &extents); double x = style.GetTextAnchor() == wxCSS_VALUE_END ? maxWidth - extents.width : style.GetTextAnchor() == wxCSS_VALUE_MIDDLE ? (maxWidth - extents.width) / 2 : 0; m_char->path->MoveTo(m_tx + x, m_ty + y); cairo_text_path(cr, (const char*) token.utf8_str()); if (x_advance < extents.x_advance) x_advance = extents.x_advance; if (width < extents.width) width = extents.width; height += fextents.height; if (tokenzr.HasMoreTokens()) y += fextents.height; } // set bbox m_char->bbox = wxSVGRect(m_tx, m_ty, width, height); // increase current position (m_tx) if (style.GetTextAnchor() == wxCSS_VALUE_MIDDLE || style.GetTextAnchor() == wxCSS_VALUE_END) { wxSVGRect bbox = m_char->path->GetResultBBox(style); m_tx += x_advance > bbox.GetWidth() ? x_advance : bbox.GetWidth(); } else m_tx += x_advance; #else PangoLayout* layout = pango_cairo_create_layout(cr); PangoFontDescription* font = pango_font_description_new(); pango_font_description_set_family(font, style.GetFontFamily().ToAscii()); pango_font_description_set_weight(font, style.GetFontWeight() == wxCSS_VALUE_BOLD ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL); pango_font_description_set_style(font, style.GetFontStyle() == wxCSS_VALUE_ITALIC ? PANGO_STYLE_ITALIC : (style.GetFontStyle() == wxCSS_VALUE_OBLIQUE ? PANGO_STYLE_OBLIQUE : PANGO_STYLE_NORMAL)); pango_font_description_set_absolute_size(font, style.GetFontSize() * PANGO_SCALE); PangoContext* ctx = pango_layout_get_context(layout); PangoFont* f = pango_context_load_font(ctx, font); if (f == NULL) pango_font_description_set_style(font, PANGO_STYLE_NORMAL); pango_layout_set_font_description(layout, font); if (style.GetTextAnchor() != wxCSS_VALUE_START) pango_layout_set_alignment(layout, style.GetTextAnchor() == wxCSS_VALUE_MIDDLE ? PANGO_ALIGN_CENTER : PANGO_ALIGN_RIGHT); pango_layout_set_text(layout, (const char*) text.utf8_str(), -1); int baseline = pango_layout_get_baseline(layout); m_char->path->MoveTo(m_tx, m_ty - ((double)baseline / PANGO_SCALE)); pango_cairo_layout_path(cr, layout); // set bbox and increase current position (m_tx) int lwidth, lheight; pango_layout_get_size(layout, &lwidth, &lheight); double width = ((double)lwidth / PANGO_SCALE); double height = ((double)lheight / PANGO_SCALE); m_char->bbox = wxSVGRect(m_tx, m_ty, width, height); if (style.GetTextAnchor() == wxCSS_VALUE_MIDDLE || style.GetTextAnchor() == wxCSS_VALUE_END) { wxSVGRect bbox = m_char->path->GetResultBBox(style); m_tx += width > bbox.GetWidth() ? width : bbox.GetWidth(); } else m_tx += width; g_object_unref(layout); pango_font_description_free(font); #endif }
void drawTextWithSpacing(GraphicsContext* graphicsContext, const SimpleFontData* font, const wxColour& color, const GlyphBuffer& glyphBuffer, int from, int numGlyphs, const FloatPoint& point) { #if USE(WXGC) wxGCDC* dc = static_cast<wxGCDC*>(graphicsContext->platformContext()); wxGraphicsContext* gc = dc->GetGraphicsContext(); gc->PushState(); cairo_t* cr = (cairo_t*)gc->GetNativeContext(); wxFont* wxfont = font->getWxFont(); cairo_scaled_font_t* scaled_font = 0; #if wxUSE_PANGO PangoFont* pangoFont = createPangoFontForFont(wxfont); PangoFontMap* fontMap = pangoFontMap(); PangoContext* pangoContext = pango_cairo_font_map_create_context(PANGO_CAIRO_FONT_MAP(fontMap)); scaled_font = createScaledFontForFont(wxfont); #elif __WXMSW__ cairo_matrix_t sizeMatrix, ctm; cairo_matrix_init_identity(&ctm); int size = font->platformData().size(); cairo_matrix_init_scale(&sizeMatrix, size, size); cairo_font_options_t* fontOptions = cairo_font_options_create(); cairo_font_options_set_antialias(fontOptions, CAIRO_ANTIALIAS_SUBPIXEL); cairo_font_face_t* win_face = cairo_win32_font_face_create_for_hfont((HFONT)wxfont->GetHFONT()); scaled_font = cairo_scaled_font_create(win_face, &sizeMatrix, &ctm, fontOptions); #endif ASSERT(scaled_font); GlyphBufferGlyph* glyphs = const_cast<GlyphBufferGlyph*>(glyphBuffer.glyphs(from)); float offset = point.x(); for (int i = 0; i < numGlyphs; i++) { #if wxUSE_PANGO glyphs[i].index = pango_font_get_glyph(pangoFont, pangoContext, glyphBuffer.glyphAt(from + i)); #endif glyphs[i].x = offset; glyphs[i].y = point.y(); offset += glyphBuffer.advanceAt(from + i); } cairo_set_source_rgba(cr, color.Red()/255.0, color.Green()/255.0, color.Blue()/255.0, color.Alpha()/255.0); cairo_set_scaled_font(cr, scaled_font); cairo_show_glyphs(cr, glyphs, numGlyphs); cairo_scaled_font_destroy(scaled_font); gc->PopState(); #else wxDC* dc = graphicsContext->platformContext(); wxFont* wxfont = font->getWxFont(); if (wxfont && wxfont->IsOk()) dc->SetFont(*wxfont); dc->SetTextForeground(color); // convert glyphs to wxString GlyphBufferGlyph* glyphs = const_cast<GlyphBufferGlyph*>(glyphBuffer.glyphs(from)); int offset = point.x(); wxString text = wxEmptyString; for (unsigned i = 0; i < numGlyphs; i++) { text = text.Append((wxChar)glyphs[i]); offset += glyphBuffer.advanceAt(from + i); } // the y point is actually the bottom point of the text, turn it into the top float height = font->ascent() - font->descent(); wxCoord ypoint = (wxCoord) (point.y() - height); dc->DrawText(text, (wxCoord)point.x(), ypoint); #endif }