static void text_layout_extents(struct text_layout *layout, cairo_text_extents_t *extents) { cairo_scaled_font_glyph_extents(layout->font, layout->glyphs, layout->num_glyphs, extents); }
static void text_layout_index_to_pos(struct text_layout *layout, uint32_t index, cairo_rectangle_t *pos) { cairo_text_extents_t extents; int glyph_index = glyphs_from_bytes(layout, index); if (!pos) return; cairo_scaled_font_glyph_extents(layout->font, layout->glyphs, layout->num_glyphs, &extents); if (glyph_index >= layout->num_glyphs) { pos->x = extents.x_advance; pos->y = layout->num_glyphs ? layout->glyphs[layout->num_glyphs - 1].y : 0; pos->width = 1; pos->height = extents.height; return; } pos->x = layout->glyphs[glyph_index].x; pos->y = layout->glyphs[glyph_index].y; pos->width = glyph_index < layout->num_glyphs - 1 ? layout->glyphs[glyph_index + 1].x : extents.x_advance - pos->x; pos->height = extents.height; }
static int text_layout_xy_to_index(struct text_layout *layout, double x, double y) { cairo_text_extents_t extents; int i; double d; if (layout->num_glyphs == 0) return 0; cairo_scaled_font_glyph_extents(layout->font, layout->glyphs, layout->num_glyphs, &extents); if (x < 0) return 0; for (i = 0; i < layout->num_glyphs - 1; ++i) { d = layout->glyphs[i + 1].x - layout->glyphs[i].x; if (x < layout->glyphs[i].x + d/2) return bytes_from_glyphs(layout, i); } d = extents.width - layout->glyphs[layout->num_glyphs - 1].x; if (x < layout->glyphs[layout->num_glyphs - 1].x + d/2) return bytes_from_glyphs(layout, layout->num_glyphs - 1); return bytes_from_glyphs(layout, layout->num_glyphs); }
static void drawGlyphsShadow(GraphicsContext* graphicsContext, const FloatPoint& point, const SimpleFontData* font, GlyphBufferGlyph* glyphs, int numGlyphs) { ShadowBlur& shadow = graphicsContext->platformContext()->shadowBlur(); if (!(graphicsContext->textDrawingMode() & TextModeFill) || shadow.type() == ShadowBlur::NoShadow) return; if (!graphicsContext->mustUseShadowBlur()) { // Optimize non-blurry shadows, by just drawing text without the ShadowBlur. cairo_t* context = graphicsContext->platformContext()->cr(); cairo_save(context); FloatSize shadowOffset(graphicsContext->state().shadowOffset); cairo_translate(context, shadowOffset.width(), shadowOffset.height()); setSourceRGBAFromColor(context, graphicsContext->state().shadowColor); drawGlyphsToContext(context, font, glyphs, numGlyphs); cairo_restore(context); return; } cairo_text_extents_t extents; cairo_scaled_font_glyph_extents(font->platformData().scaledFont(), glyphs, numGlyphs, &extents); FloatRect fontExtentsRect(point.x() + extents.x_bearing, point.y() + extents.y_bearing, extents.width, extents.height); if (GraphicsContext* shadowContext = shadow.beginShadowLayer(graphicsContext, fontExtentsRect)) { drawGlyphsToContext(shadowContext->platformContext()->cr(), font, glyphs, numGlyphs); shadow.endShadowLayer(graphicsContext); } }
size_t render(const string& text, double x = 0.0, double y = 0.0) override { cairo_glyph_t* glyphs{nullptr}; cairo_text_cluster_t* clusters{nullptr}; cairo_text_cluster_flags_t cf{}; int nglyphs = 0, nclusters = 0; string utf8 = string(text); auto status = cairo_scaled_font_text_to_glyphs( m_scaled, x, y, utf8.c_str(), utf8.size(), &glyphs, &nglyphs, &clusters, &nclusters, &cf); if (status != CAIRO_STATUS_SUCCESS) { throw application_error(sstream() << "cairo_scaled_font_text_to_glyphs()" << cairo_status_to_string(status)); } size_t bytes = 0; for (int g = 0; g < nglyphs; g++) { if (glyphs[g].index) { bytes += clusters[g].num_bytes; } else { break; } } if (bytes && bytes < text.size()) { cairo_glyph_free(glyphs); cairo_text_cluster_free(clusters); utf8 = text.substr(0, bytes); auto status = cairo_scaled_font_text_to_glyphs( m_scaled, x, y, utf8.c_str(), utf8.size(), &glyphs, &nglyphs, &clusters, &nclusters, &cf); if (status != CAIRO_STATUS_SUCCESS) { throw application_error(sstream() << "cairo_scaled_font_text_to_glyphs()" << cairo_status_to_string(status)); } } if (bytes) { // auto lock = make_unique<utils::device_lock>(cairo_surface_get_device(cairo_get_target(m_cairo))); // if (lock.get()) { // cairo_glyph_path(m_cairo, glyphs, nglyphs); // } cairo_text_extents_t extents{}; cairo_scaled_font_glyph_extents(m_scaled, glyphs, nglyphs, &extents); cairo_show_text_glyphs(m_cairo, utf8.c_str(), utf8.size(), glyphs, nglyphs, clusters, nclusters, cf); cairo_fill(m_cairo); cairo_move_to(m_cairo, x + extents.x_advance, 0.0); } cairo_glyph_free(glyphs); cairo_text_cluster_free(clusters); return bytes; }
float SimpleFontData::platformWidthForGlyph(Glyph glyph) const { ASSERT(m_platformData.m_scaledFont); cairo_glyph_t cglyph = { glyph, 0, 0 }; cairo_text_extents_t extents; cairo_scaled_font_glyph_extents(m_platformData.m_scaledFont, &cglyph, 1, &extents); float w = (float)m_spaceWidth; if (cairo_scaled_font_status(m_platformData.m_scaledFont) == CAIRO_STATUS_SUCCESS && extents.x_advance != 0) w = (float)extents.x_advance; return w; }
float SimpleFontData::platformWidthForGlyph(Glyph glyph) const { if (!m_platformData.size()) return 0; if (cairo_scaled_font_status(m_platformData.scaledFont()) != CAIRO_STATUS_SUCCESS) return m_spaceWidth; cairo_glyph_t cairoGlyph = { glyph, 0, 0 }; cairo_text_extents_t extents; cairo_scaled_font_glyph_extents(m_platformData.scaledFont(), &cairoGlyph, 1, &extents); float width = platformData().orientation() == Horizontal ? extents.x_advance : -extents.y_advance; return width ? width : m_spaceWidth; }
FloatRect SimpleFontData::platformBoundsForGlyph(Glyph glyph) const { if (!m_platformData.size()) return FloatRect(); cairo_glyph_t cglyph = { glyph, 0, 0 }; cairo_text_extents_t extents; cairo_scaled_font_glyph_extents(m_platformData.scaledFont(), &cglyph, 1, &extents); if (cairo_scaled_font_status(m_platformData.scaledFont()) == CAIRO_STATUS_SUCCESS) return FloatRect(extents.x_bearing, extents.y_bearing, extents.width, extents.height); return FloatRect(); }
void ScaledFontBase::GetGlyphDesignMetrics(const uint16_t* aGlyphs, uint32_t aNumGlyphs, GlyphMetrics* aGlyphMetrics) { #ifdef USE_CAIRO_SCALED_FONT if (mScaledFont) { for (uint32_t i = 0; i < aNumGlyphs; i++) { cairo_glyph_t glyph; cairo_text_extents_t extents; glyph.index = aGlyphs[i]; glyph.x = 0; glyph.y = 0; cairo_scaled_font_glyph_extents(mScaledFont, &glyph, 1, &extents); aGlyphMetrics[i].mXBearing = extents.x_bearing; aGlyphMetrics[i].mXAdvance = extents.x_advance; aGlyphMetrics[i].mYBearing = extents.y_bearing; aGlyphMetrics[i].mYAdvance = extents.y_advance; aGlyphMetrics[i].mWidth = extents.width; aGlyphMetrics[i].mHeight = extents.height; cairo_font_options_t *options = cairo_font_options_create(); cairo_scaled_font_get_font_options(mScaledFont, options); if (cairo_font_options_get_antialias(options) != CAIRO_ANTIALIAS_NONE) { if (cairo_scaled_font_get_type(mScaledFont) == CAIRO_FONT_TYPE_WIN32) { if (aGlyphMetrics[i].mWidth > 0 && aGlyphMetrics[i].mHeight > 0) { aGlyphMetrics[i].mWidth -= 3.0f; aGlyphMetrics[i].mXBearing += 1.0f; } } #if defined(MOZ2D_HAS_MOZ_CAIRO) && defined(CAIRO_HAS_DWRITE_FONT) else if (cairo_scaled_font_get_type(mScaledFont) == CAIRO_FONT_TYPE_DWRITE) { if (aGlyphMetrics[i].mWidth > 0 && aGlyphMetrics[i].mHeight > 0) { aGlyphMetrics[i].mWidth -= 2.0f; aGlyphMetrics[i].mXBearing += 1.0f; } } #endif } cairo_font_options_destroy(options); } } #endif // Don't know how to get the glyph metrics... MOZ_CRASH("The specific backend type is not supported for GetGlyphDesignMetrics."); }
static void compute_glyph_extents (PangoFont *font, PangoGlyph glyph, PangoRectangle *ink_rect, PangoRectangle *logical_rect) { PangoCairoFont *cfont = (PangoCairoFont *)font; cairo_scaled_font_t *scaled_font = pango_cairo_win32_font_get_scaled_font (PANGO_CAIRO_FONT (cfont)); /* It may well be worth caching the font_extents here, since getting them * is pretty expensive. */ cairo_font_extents_t font_extents; cairo_text_extents_t extents; cairo_glyph_t cairo_glyph; cairo_scaled_font_extents (scaled_font, &font_extents); logical_rect->x = 0; logical_rect->y = - font_extents.ascent * PANGO_SCALE; logical_rect->width = 0; logical_rect->height = (font_extents.ascent + font_extents.descent) * PANGO_SCALE; if (glyph == PANGO_GLYPH_EMPTY) { /* already initialized above */ } else if (glyph & PANGO_GLYPH_UNKNOWN_FLAG) { /* space for the hex box */ _pango_cairo_get_glyph_extents_missing(cfont, glyph, ink_rect, logical_rect); } else { cairo_glyph.index = glyph; cairo_glyph.x = 0; cairo_glyph.y = 0; cairo_scaled_font_glyph_extents (scaled_font, &cairo_glyph, 1, &extents); ink_rect->x = extents.x_bearing * PANGO_SCALE; ink_rect->y = extents.y_bearing * PANGO_SCALE; ink_rect->width = extents.width * PANGO_SCALE; ink_rect->height = extents.height * PANGO_SCALE; logical_rect->width = extents.x_advance * PANGO_SCALE; } }
void gfxFT2FontBase::GetGlyphExtents(PRUint32 aGlyph, cairo_text_extents_t* aExtents) { NS_PRECONDITION(aExtents != NULL, "aExtents must not be NULL"); cairo_glyph_t glyphs[1]; glyphs[0].index = aGlyph; glyphs[0].x = 0.0; glyphs[0].y = 0.0; // cairo does some caching for us here but perhaps a small gain could be // made by caching more. It is usually only the advance that is needed, // so caching only the advance could allow many requests to be cached with // little memory use. Ideally this cache would be merged with // gfxGlyphExtents. cairo_scaled_font_glyph_extents(CairoScaledFont(), glyphs, 1, aExtents); }
void SkScalerContext_CairoFT::generateMetrics(SkGlyph* glyph) { SkASSERT(fScaledFont != NULL); cairo_text_extents_t extents; cairo_glyph_t cairoGlyph = { glyph->getGlyphID(), 0.0, 0.0 }; cairo_scaled_font_glyph_extents(fScaledFont, &cairoGlyph, 1, &extents); glyph->fAdvanceX = SkDoubleToFixed(extents.x_advance); glyph->fAdvanceY = SkDoubleToFixed(extents.y_advance); glyph->fWidth = SkToU16(SkScalarCeilToInt(extents.width)); glyph->fHeight = SkToU16(SkScalarCeilToInt(extents.height)); glyph->fLeft = SkToS16(SkScalarCeilToInt(extents.x_bearing)); glyph->fTop = SkToS16(SkScalarCeilToInt(extents.y_bearing)); glyph->fLsbDelta = 0; glyph->fRsbDelta = 0; }
/** * cairo_scaled_font_text_extents: * @scaled_font: a #cairo_scaled_font_t * @utf8: a string of text, encoded in UTF-8 * @extents: a #cairo_text_extents_t which to store the retrieved extents. * * Gets the extents for a string of text. The extents describe a * user-space rectangle that encloses the "inked" portion of the text * drawn at the origin (0,0) (as it would be drawn by cairo_show_text() * if the cairo graphics state were set to the same font_face, * font_matrix, ctm, and font_options as @scaled_font). Additionally, * the x_advance and y_advance values indicate the amount by which the * current point would be advanced by cairo_show_text(). * * Note that whitespace characters do not directly contribute to the * size of the rectangle (extents.width and extents.height). They do * contribute indirectly by changing the position of non-whitespace * characters. In particular, trailing whitespace characters are * likely to not affect the size of the rectangle, though they will * affect the x_advance and y_advance values. **/ void cairo_scaled_font_text_extents (cairo_scaled_font_t *scaled_font, const char *utf8, cairo_text_extents_t *extents) { cairo_status_t status = CAIRO_STATUS_SUCCESS; cairo_glyph_t *glyphs; int num_glyphs; status = _cairo_scaled_font_text_to_glyphs (scaled_font, 0., 0., utf8, &glyphs, &num_glyphs); if (status) { _cairo_scaled_font_set_error (scaled_font, status); return; } cairo_scaled_font_glyph_extents (scaled_font, glyphs, num_glyphs, extents); free (glyphs); }
static double count_glyphs (double font_size, cairo_antialias_t antialias, cairo_t *cr, int width, int height) { const char text[] = "the jay, pig, fox, zebra and my wolves quack"; cairo_scaled_font_t *scaled_font; cairo_glyph_t *glyphs = NULL; cairo_text_extents_t extents; cairo_font_options_t *options; cairo_status_t status; int num_glyphs; int glyphs_per_line, lines_per_loop; options = cairo_font_options_create (); cairo_font_options_set_antialias (options, antialias); cairo_set_font_options (cr, options); cairo_font_options_destroy (options); cairo_select_font_face (cr, "@cairo:", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); cairo_set_font_size (cr, font_size); scaled_font = cairo_get_scaled_font (cr); status = cairo_scaled_font_text_to_glyphs (scaled_font, 0., 0., text, -1, &glyphs, &num_glyphs, NULL, NULL, NULL); if (status) return 0; cairo_scaled_font_glyph_extents (scaled_font, glyphs, num_glyphs, &extents); cairo_glyph_free (glyphs); glyphs_per_line = num_glyphs * width / extents.width + 1; lines_per_loop = height / extents.height + 1; return glyphs_per_line * lines_per_loop / 1000.; /* kiloglyphs */ }
/* Fills in the glyph extents cache entry */ static void compute_glyph_extents (PangoCairoFontPrivate *cf_priv, PangoGlyph glyph, PangoCairoFontGlyphExtentsCacheEntry *entry) { cairo_text_extents_t extents; cairo_glyph_t cairo_glyph; cairo_glyph.index = glyph; cairo_glyph.x = 0; cairo_glyph.y = 0; cairo_scaled_font_glyph_extents (_pango_cairo_font_private_get_scaled_font (cf_priv), &cairo_glyph, 1, &extents); entry->glyph = glyph; entry->width = pango_units_from_double (extents.x_advance); entry->ink_rect.x = pango_units_from_double (extents.x_bearing); entry->ink_rect.y = pango_units_from_double (extents.y_bearing); entry->ink_rect.width = pango_units_from_double (extents.width); entry->ink_rect.height = pango_units_from_double (extents.height); }
/** * layout a text with provided parameters * * @param cr: the cairo instance * @param text: the text to layout * @param font: the font to use to layout to * @param size: the font size size of each char to layout * @param bounds: the bounds of layouting * @param alignment: the text alignment on cairo's buffer * @param layout: the returned layouted text instance * @param-opt breakOnOverflow: break the layouting if size overflow the provided bounds */ void TextLayouter::layout(cairo_t *cr, const char *text, Font_t *font, int32_t size, Rectangle bounds, const TextAlignment &alignment, LayoutedText *layout, bool breakOnOverflow) { // check font validity if (!font) return; // get text length size_t textLen = strlen(text); // starting coordinates int x = bounds.x; int y = bounds.y; int lineStartX = x; // created the scaled font face cairo_set_font_face(cr, font->getFace()); cairo_set_font_size(cr, size); cairo_scaled_font_t *scaledFace = cairo_get_scaled_font(cr); int line = 0; int lineHeight = size; // create glyphs for the text cairo_glyph_t *previousGlyphBuffer = layout->glyphBuffer; cairo_text_cluster_t *previousClusterBuffer = layout->clusterBuffer; cairo_text_cluster_flags_t clusterFlags; cairo_status_t stat = cairo_scaled_font_text_to_glyphs(scaledFace, 0, 0, text, textLen, &layout->glyphBuffer, &layout->glyphCount, &layout->clusterBuffer, &layout->clusterCount, &clusterFlags); // free old buffer if (previousGlyphBuffer != nullptr && layout->glyphBuffer != previousGlyphBuffer) free(previousGlyphBuffer); if (previousClusterBuffer != nullptr && layout->clusterBuffer != previousClusterBuffer) free(previousClusterBuffer); // clear layout entries layout->positions.clear(); // perform layouting if (stat == CAIRO_STATUS_SUCCESS) { // positions in bytes and glyphs size_t bytePos = 0; size_t glyphPos = 0; // text extents cairo_text_extents_t extents; for (int i = 0; i < layout->clusterCount; i++) { cairo_text_cluster_t *cluster = &layout->clusterBuffer[i]; cairo_glyph_t *glyphs = &layout->glyphBuffer[glyphPos]; // create new position PositionedGlyph positioned; positioned.glyph = glyphs; positioned.glyphCount = cluster->num_glyphs; cairo_scaled_font_glyph_extents(scaledFace, positioned.glyph, positioned.glyphCount, &extents); positioned.advance.x = extents.x_advance; positioned.advance.y = extents.y_advance; positioned.size.width = extents.width; positioned.size.height = extents.height; // check if newline bool isNewline = false; if (cluster->num_bytes == 1 && text[bytePos] == '\n') isNewline = true; bool invisible = false; // Wouldn't match in line or is break character? Start next line if (isNewline || (breakOnOverflow && (x + positioned.size.width > bounds.width))) { if (isNewline) invisible = true; if (alignment == TextAlignment::RIGHT) rightAlign(layout, line, x - lineStartX, bounds); else if (alignment == TextAlignment::CENTER) centerAlign(layout, line, x - lineStartX, bounds); ++line; x = bounds.x; lineStartX = x; y += lineHeight; } if (!invisible) { // Position positioned.line = line; positioned.position.x = x; positioned.position.y = y + lineHeight; // Add position layout->positions.push_back(positioned); // Jump to next x += positioned.advance.x; } // increase positions glyphPos += cluster->num_glyphs; bytePos += cluster->num_bytes; } } if (alignment == TextAlignment::RIGHT) rightAlign(layout, line, x - lineStartX, bounds); else if (alignment == TextAlignment::CENTER) centerAlign(layout, line, x - lineStartX, bounds); // Set text bounds #define BOUNDS_EMPTY 0xFFFFFF int tbTop = BOUNDS_EMPTY; int tbLeft = BOUNDS_EMPTY; int tbRight = 0; int tbBottom = 0; for (PositionedGlyph &p : layout->positions) { if (p.position.x < tbLeft) tbLeft = p.position.x; if (p.position.y < tbTop) tbTop = p.position.y; // get extents again int r = p.position.x + p.size.width; if (r > tbRight) tbRight = r; int b = p.position.y + p.size.height; if (b > tbBottom) tbBottom = b; } if (tbTop != BOUNDS_EMPTY && tbLeft != BOUNDS_EMPTY) { layout->textBounds.x = tbLeft; layout->textBounds.y = tbTop; layout->textBounds.width = tbRight - tbLeft; layout->textBounds.height = tbBottom - tbTop; } }
static cairo_test_status_t draw (cairo_t *cr, int width, int height) { const cairo_test_context_t *ctx = cairo_test_get_context (cr); cairo_text_extents_t extents, nil_extents; cairo_font_extents_t font_extents, nil_font_extents; cairo_scaled_font_t *scaled_font; cairo_select_font_face (cr, "Bitstream Vera Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); cairo_set_font_size (cr, 16); cairo_move_to (cr, 10, 25); cairo_show_text (cr, NULL); cairo_show_text (cr, ""); cairo_show_glyphs (cr, NULL, 0); cairo_show_glyphs (cr, (void*)8, 0); cairo_move_to (cr, 10, 55); cairo_text_path (cr, NULL); cairo_text_path (cr, ""); cairo_glyph_path (cr, (void*)8, 0); cairo_fill (cr); memset (&nil_extents, 0, sizeof (cairo_text_extents_t)); memset (&extents, 0xff, sizeof (cairo_text_extents_t)); cairo_text_extents (cr, "", &extents); if (! text_extents_equal (&extents, &nil_extents)) { cairo_test_log (ctx, "Error: cairo_text_extents(\"\"); extents (%g, %g, %g, %g, %g, %g)\n", extents.x_bearing, extents.y_bearing, extents.width, extents.height, extents.x_advance, extents.y_advance); return CAIRO_TEST_FAILURE; } memset (&extents, 0xff, sizeof (cairo_text_extents_t)); cairo_text_extents (cr, NULL, &extents); if (! text_extents_equal (&extents, &nil_extents)) { cairo_test_log (ctx, "Error: cairo_text_extents(NULL); extents (%g, %g, %g, %g, %g, %g)\n", extents.x_bearing, extents.y_bearing, extents.width, extents.height, extents.x_advance, extents.y_advance); return CAIRO_TEST_FAILURE; } memset (&extents, 0xff, sizeof (cairo_text_extents_t)); cairo_glyph_extents (cr, (void*)8, 0, &extents); if (! text_extents_equal (&extents, &nil_extents)) { cairo_test_log (ctx, "Error: cairo_glyph_extents(); extents (%g, %g, %g, %g, %g, %g)\n", extents.x_bearing, extents.y_bearing, extents.width, extents.height, extents.x_advance, extents.y_advance); return CAIRO_TEST_FAILURE; } scaled_font = cairo_get_scaled_font (cr); memset (&extents, 0xff, sizeof (cairo_text_extents_t)); cairo_scaled_font_text_extents (scaled_font, "", &extents); if (! text_extents_equal (&extents, &nil_extents)) { cairo_test_log (ctx, "Error: cairo_scaled_font_text_extents(\"\"); extents (%g, %g, %g, %g, %g, %g)\n", extents.x_bearing, extents.y_bearing, extents.width, extents.height, extents.x_advance, extents.y_advance); return CAIRO_TEST_FAILURE; } memset (&extents, 0xff, sizeof (cairo_text_extents_t)); cairo_scaled_font_text_extents (scaled_font, NULL, &extents); if (! text_extents_equal (&extents, &nil_extents)) { cairo_test_log (ctx, "Error: cairo_scaled_font_text_extents(NULL); extents (%g, %g, %g, %g, %g, %g)\n", extents.x_bearing, extents.y_bearing, extents.width, extents.height, extents.x_advance, extents.y_advance); return CAIRO_TEST_FAILURE; } memset (&extents, 0xff, sizeof (cairo_text_extents_t)); cairo_scaled_font_glyph_extents (scaled_font, (void*)8, 0, &extents); if (! text_extents_equal (&extents, &nil_extents)) { cairo_test_log (ctx, "Error: cairo_scaled_font_glyph_extents(NULL); extents (%g, %g, %g, %g, %g, %g)\n", extents.x_bearing, extents.y_bearing, extents.width, extents.height, extents.x_advance, extents.y_advance); return CAIRO_TEST_FAILURE; } /* Lets also try font size 0 while here */ cairo_set_font_size (cr, 0); memset (&extents, 0xff, sizeof (cairo_text_extents_t)); cairo_text_extents (cr, "test", &extents); if (! text_extents_equal (&extents, &nil_extents)) { cairo_test_log (ctx, "Error: cairo_set_font_size(0); cairo_text_extents(\"test\"); extents (%g, %g, %g, %g, %g, %g)\n", extents.x_bearing, extents.y_bearing, extents.width, extents.height, extents.x_advance, extents.y_advance); return CAIRO_TEST_FAILURE; } memset (&nil_font_extents, 0, sizeof (cairo_font_extents_t)); memset (&font_extents, 0xff, sizeof (cairo_font_extents_t)); cairo_font_extents (cr, &font_extents); if (! font_extents_equal (&font_extents, &nil_font_extents)) { cairo_test_log (ctx, "Error: cairo_set_font_size(0); cairo_font_extents(); extents (%g, %g, %g, %g, %g)\n", font_extents.ascent, font_extents.descent, font_extents.height, font_extents.max_x_advance, font_extents.max_y_advance); return CAIRO_TEST_FAILURE; } scaled_font = cairo_get_scaled_font (cr); memset (&font_extents, 0xff, sizeof (cairo_font_extents_t)); cairo_scaled_font_extents (scaled_font, &font_extents); if (! font_extents_equal (&font_extents, &nil_font_extents)) { cairo_test_log (ctx, "Error: cairo_set_font_size(0); cairo_scaled_font_extents(); extents (%g, %g, %g, %g, %g)\n", font_extents.ascent, font_extents.descent, font_extents.height, font_extents.max_x_advance, font_extents.max_y_advance); return CAIRO_TEST_FAILURE; } return CAIRO_TEST_SUCCESS; }
static cairo_time_t do_glyphs (double font_size, cairo_antialias_t antialias, cairo_t *cr, int width, int height, int loops) { const char text[] = "the jay, pig, fox, zebra and my wolves quack"; cairo_scaled_font_t *scaled_font; cairo_glyph_t *glyphs = NULL, *glyphs_copy; cairo_text_extents_t extents; cairo_font_options_t *options; cairo_status_t status; double x, y; int num_glyphs, n; options = cairo_font_options_create (); cairo_font_options_set_antialias (options, antialias); cairo_set_font_options (cr, options); cairo_font_options_destroy (options); cairo_select_font_face (cr, "@cairo:", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); cairo_set_font_size (cr, font_size); scaled_font = cairo_get_scaled_font (cr); status = cairo_scaled_font_text_to_glyphs (scaled_font, 0., 0., text, -1, &glyphs, &num_glyphs, NULL, NULL, NULL); if (status) return 0; glyphs_copy = cairo_glyph_allocate (num_glyphs); if (glyphs_copy == NULL) { cairo_glyph_free (glyphs); return 0; } cairo_scaled_font_glyph_extents (scaled_font, glyphs, num_glyphs, &extents); cairo_perf_timer_start (); while (loops--) { y = 0; do { x = 0; do { for (n = 0; n < num_glyphs; n++) { glyphs_copy[n] = glyphs[n]; glyphs_copy[n].x += x; glyphs_copy[n].y += y; } cairo_show_glyphs (cr, glyphs_copy, num_glyphs); x += extents.width; } while (x < width); y += extents.height; } while (y < height); } cairo_perf_timer_stop (); cairo_glyph_free (glyphs); cairo_glyph_free (glyphs_copy); return cairo_perf_timer_elapsed (); }
void Font::drawGlyphs(GraphicsContext* context, const SimpleFontData* font, const GlyphBuffer& glyphBuffer, int from, int numGlyphs, const FloatPoint& point) const { cairo_t* cr = context->platformContext(); cairo_save(cr); cairo_set_scaled_font(cr, font->platformData().scaledFont()); GlyphBufferGlyph* glyphs = (GlyphBufferGlyph*)glyphBuffer.glyphs(from); float offset = 0.0f; for (int i = 0; i < numGlyphs; i++) { glyphs[i].x = offset; glyphs[i].y = 0.0f; offset += glyphBuffer.advanceAt(from + i); } Color fillColor = context->fillColor(); // Synthetic Oblique if(font->platformData().syntheticOblique()) { cairo_matrix_t mat = {1, 0, -tanf(SYNTHETIC_OBLIQUE_ANGLE * acosf(0) / 90), 1, point.x(), point.y()}; cairo_transform(cr, &mat); } else { cairo_translate(cr, point.x(), point.y()); } // Text shadow, inspired by FontMac FloatSize shadowOffset; float shadowBlur = 0; Color shadowColor; bool hasShadow = context->textDrawingMode() & cTextFill && context->getShadow(shadowOffset, shadowBlur, shadowColor); // TODO: Blur support if (hasShadow) { // Disable graphics context shadows (not yet implemented) and paint them manually context->clearShadow(); Color shadowFillColor(shadowColor.red(), shadowColor.green(), shadowColor.blue(), shadowColor.alpha() * fillColor.alpha() / 255); cairo_save(cr); float red, green, blue, alpha; shadowFillColor.getRGBA(red, green, blue, alpha); cairo_set_source_rgba(cr, red, green, blue, alpha); #if ENABLE(FILTERS) cairo_text_extents_t extents; cairo_scaled_font_glyph_extents(font->platformData().scaledFont(), glyphs, numGlyphs, &extents); FloatRect rect(FloatPoint(), FloatSize(extents.width, extents.height)); IntSize shadowBufferSize; FloatRect shadowRect; float radius = 0; context->calculateShadowBufferDimensions(shadowBufferSize, shadowRect, radius, rect, shadowOffset, shadowBlur); // Draw shadow into a new ImageBuffer OwnPtr<ImageBuffer> shadowBuffer = ImageBuffer::create(shadowBufferSize); GraphicsContext* shadowContext = shadowBuffer->context(); cairo_t* shadowCr = shadowContext->platformContext(); cairo_translate(shadowCr, radius, extents.height + radius); cairo_set_scaled_font(shadowCr, font->platformData().scaledFont()); cairo_show_glyphs(shadowCr, glyphs, numGlyphs); if (font->syntheticBoldOffset()) { cairo_save(shadowCr); cairo_translate(shadowCr, font->syntheticBoldOffset(), 0); cairo_show_glyphs(shadowCr, glyphs, numGlyphs); cairo_restore(shadowCr); } cairo_translate(cr, 0.0, -extents.height); context->applyPlatformShadow(shadowBuffer.release(), shadowColor, shadowRect, radius); #else cairo_translate(cr, shadowOffset.width(), shadowOffset.height()); cairo_show_glyphs(cr, glyphs, numGlyphs); if (font->syntheticBoldOffset()) { cairo_save(cr); cairo_translate(cr, font->syntheticBoldOffset(), 0); cairo_show_glyphs(cr, glyphs, numGlyphs); cairo_restore(cr); } #endif cairo_restore(cr); } if (context->textDrawingMode() & cTextFill) { if (context->fillGradient()) { cairo_set_source(cr, context->fillGradient()->platformGradient()); if (context->getAlpha() < 1.0f) { cairo_push_group(cr); cairo_paint_with_alpha(cr, context->getAlpha()); cairo_pop_group_to_source(cr); } } else if (context->fillPattern()) { AffineTransform affine; cairo_pattern_t* pattern = context->fillPattern()->createPlatformPattern(affine); cairo_set_source(cr, pattern); if (context->getAlpha() < 1.0f) { cairo_push_group(cr); cairo_paint_with_alpha(cr, context->getAlpha()); cairo_pop_group_to_source(cr); } cairo_pattern_destroy(pattern); } else { float red, green, blue, alpha; fillColor.getRGBA(red, green, blue, alpha); cairo_set_source_rgba(cr, red, green, blue, alpha * context->getAlpha()); } cairo_show_glyphs(cr, glyphs, numGlyphs); if (font->syntheticBoldOffset()) { cairo_save(cr); cairo_translate(cr, font->syntheticBoldOffset(), 0); cairo_show_glyphs(cr, glyphs, numGlyphs); cairo_restore(cr); } } // Prevent running into a long computation within cairo. If the stroke width is // twice the size of the width of the text we will not ask cairo to stroke // the text as even one single stroke would cover the full wdth of the text. // See https://bugs.webkit.org/show_bug.cgi?id=33759. if (context->textDrawingMode() & cTextStroke && context->strokeThickness() < 2 * offset) { if (context->strokeGradient()) { cairo_set_source(cr, context->strokeGradient()->platformGradient()); if (context->getAlpha() < 1.0f) { cairo_push_group(cr); cairo_paint_with_alpha(cr, context->getAlpha()); cairo_pop_group_to_source(cr); } } else if (context->strokePattern()) { AffineTransform affine; cairo_pattern_t* pattern = context->strokePattern()->createPlatformPattern(affine); cairo_set_source(cr, pattern); if (context->getAlpha() < 1.0f) { cairo_push_group(cr); cairo_paint_with_alpha(cr, context->getAlpha()); cairo_pop_group_to_source(cr); } cairo_pattern_destroy(pattern); } else { Color strokeColor = context->strokeColor(); float red, green, blue, alpha; strokeColor.getRGBA(red, green, blue, alpha); cairo_set_source_rgba(cr, red, green, blue, alpha * context->getAlpha()); } cairo_glyph_path(cr, glyphs, numGlyphs); cairo_set_line_width(cr, context->strokeThickness()); cairo_stroke(cr); } // Re-enable the platform shadow we disabled earlier if (hasShadow) context->setShadow(shadowOffset, shadowBlur, shadowColor, DeviceColorSpace); cairo_restore(cr); }
static cairo_perf_ticks_t do_glyphs (cairo_t *cr, int width, int height, int loops) { const char text[] = "the jay, pig, fox, zebra and my wolves quack"; cairo_scaled_font_t *scaled_font; cairo_glyph_t *glyphs = NULL, *glyphs_copy; cairo_text_extents_t extents; cairo_status_t status; double x, y; int num_glyphs, n; cairo_set_font_size (cr, 9); scaled_font = cairo_get_scaled_font (cr); status = cairo_scaled_font_text_to_glyphs (scaled_font, 0., 0., text, -1, &glyphs, &num_glyphs, NULL, NULL, NULL); if (status) return 0; glyphs_copy = cairo_glyph_allocate (num_glyphs); if (glyphs_copy == NULL) { cairo_glyph_free (glyphs); return 0; } cairo_scaled_font_glyph_extents (scaled_font, glyphs, num_glyphs, &extents); y = 0; cairo_perf_timer_start (); while (loops--) { do { x = 0; do { for (n = 0; n < num_glyphs; n++) { glyphs_copy[n] = glyphs[n]; glyphs_copy[n].x += x; glyphs_copy[n].y += y; } cairo_show_glyphs (cr, glyphs_copy, num_glyphs); if (cairo_status (cr) != CAIRO_STATUS_SUCCESS) goto out; x += extents.width; } while (x < width); y += extents.height; } while (y < height); } out: cairo_perf_timer_stop (); cairo_glyph_free (glyphs); cairo_glyph_free (glyphs_copy); return cairo_perf_timer_elapsed (); }