michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "gfxFT2FontBase.h" michael@0: #include "gfxFT2Utils.h" michael@0: #include "harfbuzz/hb.h" michael@0: #include "mozilla/Likely.h" michael@0: #include "gfxFontConstants.h" michael@0: #include "gfxFontUtils.h" michael@0: michael@0: using namespace mozilla::gfx; michael@0: michael@0: gfxFT2FontBase::gfxFT2FontBase(cairo_scaled_font_t *aScaledFont, michael@0: gfxFontEntry *aFontEntry, michael@0: const gfxFontStyle *aFontStyle) michael@0: : gfxFont(aFontEntry, aFontStyle, kAntialiasDefault, aScaledFont), michael@0: mSpaceGlyph(0), michael@0: mHasMetrics(false) michael@0: { michael@0: cairo_scaled_font_reference(mScaledFont); michael@0: ConstructFontOptions(); michael@0: } michael@0: michael@0: gfxFT2FontBase::~gfxFT2FontBase() michael@0: { michael@0: cairo_scaled_font_destroy(mScaledFont); michael@0: } michael@0: michael@0: uint32_t michael@0: gfxFT2FontBase::GetGlyph(uint32_t aCharCode) michael@0: { michael@0: // FcFreeTypeCharIndex needs to lock the FT_Face and can end up searching michael@0: // through all the postscript glyph names in the font. Therefore use a michael@0: // lightweight cache, which is stored on the cairo_font_face_t. michael@0: michael@0: cairo_font_face_t *face = michael@0: cairo_scaled_font_get_font_face(CairoScaledFont()); michael@0: michael@0: if (cairo_font_face_status(face) != CAIRO_STATUS_SUCCESS) michael@0: return 0; michael@0: michael@0: // This cache algorithm and size is based on what is done in michael@0: // cairo_scaled_font_text_to_glyphs and pango_fc_font_real_get_glyph. I michael@0: // think the concept is that adjacent characters probably come mostly from michael@0: // one Unicode block. This assumption is probably not so valid with michael@0: // scripts with large character sets as used for East Asian languages. michael@0: michael@0: struct CmapCacheSlot { michael@0: uint32_t mCharCode; michael@0: uint32_t mGlyphIndex; michael@0: }; michael@0: const uint32_t kNumSlots = 256; michael@0: static cairo_user_data_key_t sCmapCacheKey; michael@0: michael@0: CmapCacheSlot *slots = static_cast michael@0: (cairo_font_face_get_user_data(face, &sCmapCacheKey)); michael@0: michael@0: if (!slots) { michael@0: // cairo's caches can keep some cairo_font_faces alive past our last michael@0: // destroy, so the destroy function (free) for the cache must be michael@0: // callable from cairo without any assumptions about what other michael@0: // modules have not been shutdown. michael@0: slots = static_cast michael@0: (calloc(kNumSlots, sizeof(CmapCacheSlot))); michael@0: if (!slots) michael@0: return 0; michael@0: michael@0: cairo_status_t status = michael@0: cairo_font_face_set_user_data(face, &sCmapCacheKey, slots, free); michael@0: if (status != CAIRO_STATUS_SUCCESS) { // OOM michael@0: free(slots); michael@0: return 0; michael@0: } michael@0: michael@0: // Invalidate slot 0 by setting its char code to something that would michael@0: // never end up in slot 0. All other slots are already invalid michael@0: // because they have mCharCode = 0 and a glyph for char code 0 will michael@0: // always be in the slot 0. michael@0: slots[0].mCharCode = 1; michael@0: } michael@0: michael@0: CmapCacheSlot *slot = &slots[aCharCode % kNumSlots]; michael@0: if (slot->mCharCode != aCharCode) { michael@0: slot->mCharCode = aCharCode; michael@0: slot->mGlyphIndex = gfxFT2LockedFace(this).GetGlyph(aCharCode); michael@0: } michael@0: michael@0: return slot->mGlyphIndex; michael@0: } michael@0: michael@0: void michael@0: gfxFT2FontBase::GetGlyphExtents(uint32_t aGlyph, cairo_text_extents_t* aExtents) michael@0: { michael@0: NS_PRECONDITION(aExtents != nullptr, "aExtents must not be NULL"); michael@0: michael@0: cairo_glyph_t glyphs[1]; michael@0: glyphs[0].index = aGlyph; michael@0: glyphs[0].x = 0.0; michael@0: glyphs[0].y = 0.0; michael@0: // cairo does some caching for us here but perhaps a small gain could be michael@0: // made by caching more. It is usually only the advance that is needed, michael@0: // so caching only the advance could allow many requests to be cached with michael@0: // little memory use. Ideally this cache would be merged with michael@0: // gfxGlyphExtents. michael@0: cairo_scaled_font_glyph_extents(CairoScaledFont(), glyphs, 1, aExtents); michael@0: } michael@0: michael@0: const gfxFont::Metrics& michael@0: gfxFT2FontBase::GetMetrics() michael@0: { michael@0: if (mHasMetrics) michael@0: return mMetrics; michael@0: michael@0: if (MOZ_UNLIKELY(GetStyle()->size <= 0.0)) { michael@0: new(&mMetrics) gfxFont::Metrics(); // zero initialize michael@0: mSpaceGlyph = 0; michael@0: } else { michael@0: gfxFT2LockedFace face(this); michael@0: mFUnitsConvFactor = face.XScale(); michael@0: face.GetMetrics(&mMetrics, &mSpaceGlyph); michael@0: } michael@0: michael@0: SanitizeMetrics(&mMetrics, false); michael@0: michael@0: #if 0 michael@0: // printf("font name: %s %f\n", NS_ConvertUTF16toUTF8(GetName()).get(), GetStyle()->size); michael@0: // printf ("pango font %s\n", pango_font_description_to_string (pango_font_describe (font))); michael@0: michael@0: fprintf (stderr, "Font: %s\n", NS_ConvertUTF16toUTF8(GetName()).get()); michael@0: fprintf (stderr, " emHeight: %f emAscent: %f emDescent: %f\n", mMetrics.emHeight, mMetrics.emAscent, mMetrics.emDescent); michael@0: fprintf (stderr, " maxAscent: %f maxDescent: %f\n", mMetrics.maxAscent, mMetrics.maxDescent); michael@0: fprintf (stderr, " internalLeading: %f externalLeading: %f\n", mMetrics.externalLeading, mMetrics.internalLeading); michael@0: fprintf (stderr, " spaceWidth: %f aveCharWidth: %f xHeight: %f\n", mMetrics.spaceWidth, mMetrics.aveCharWidth, mMetrics.xHeight); michael@0: fprintf (stderr, " uOff: %f uSize: %f stOff: %f stSize: %f suOff: %f suSize: %f\n", mMetrics.underlineOffset, mMetrics.underlineSize, mMetrics.strikeoutOffset, mMetrics.strikeoutSize, mMetrics.superscriptOffset, mMetrics.subscriptOffset); michael@0: #endif michael@0: michael@0: mHasMetrics = true; michael@0: return mMetrics; michael@0: } michael@0: michael@0: // Get the glyphID of a space michael@0: uint32_t michael@0: gfxFT2FontBase::GetSpaceGlyph() michael@0: { michael@0: NS_ASSERTION(GetStyle()->size != 0, michael@0: "forgot to short-circuit a text run with zero-sized font?"); michael@0: GetMetrics(); michael@0: return mSpaceGlyph; michael@0: } michael@0: michael@0: uint32_t michael@0: gfxFT2FontBase::GetGlyph(uint32_t unicode, uint32_t variation_selector) michael@0: { michael@0: if (variation_selector) { michael@0: uint32_t id = michael@0: gfxFT2LockedFace(this).GetUVSGlyph(unicode, variation_selector); michael@0: if (id) michael@0: return id; michael@0: id = gfxFontUtils::GetUVSFallback(unicode, variation_selector); michael@0: if (id) { michael@0: unicode = id; michael@0: } michael@0: } michael@0: michael@0: return GetGlyph(unicode); michael@0: } michael@0: michael@0: int32_t michael@0: gfxFT2FontBase::GetGlyphWidth(gfxContext *aCtx, uint16_t aGID) michael@0: { michael@0: cairo_text_extents_t extents; michael@0: GetGlyphExtents(aGID, &extents); michael@0: // convert to 16.16 fixed point michael@0: return NS_lround(0x10000 * extents.x_advance); michael@0: } michael@0: michael@0: bool michael@0: gfxFT2FontBase::SetupCairoFont(gfxContext *aContext) michael@0: { michael@0: cairo_t *cr = aContext->GetCairo(); michael@0: michael@0: // The scaled font ctm is not relevant right here because michael@0: // cairo_set_scaled_font does not record the scaled font itself, but michael@0: // merely the font_face, font_matrix, font_options. The scaled_font used michael@0: // for the target can be different from the scaled_font passed to michael@0: // cairo_set_scaled_font. (Unfortunately we have measured only for an michael@0: // identity ctm.) michael@0: cairo_scaled_font_t *cairoFont = CairoScaledFont(); michael@0: michael@0: if (cairo_scaled_font_status(cairoFont) != CAIRO_STATUS_SUCCESS) { michael@0: // Don't cairo_set_scaled_font as that would propagate the error to michael@0: // the cairo_t, precluding any further drawing. michael@0: return false; michael@0: } michael@0: // Thoughts on which font_options to set on the context: michael@0: // michael@0: // cairoFont has been created for screen rendering. michael@0: // michael@0: // When the context is being used for screen rendering, we should set michael@0: // font_options such that the same scaled_font gets used (when the ctm is michael@0: // the same). The use of explicit font_options recorded in michael@0: // CreateScaledFont ensures that this will happen. michael@0: // michael@0: // XXXkt: For pdf and ps surfaces, I don't know whether it's better to michael@0: // remove surface-specific options, or try to draw with the same michael@0: // scaled_font that was used to measure. As the same font_face is being michael@0: // used, its font_options will often override some values anyway (unless michael@0: // perhaps we remove those from the FcPattern at face creation). michael@0: // michael@0: // I can't see any significant difference in printing, irrespective of michael@0: // what is set here. It's too late to change things here as measuring has michael@0: // already taken place. We should really be measuring with a different michael@0: // font for pdf and ps surfaces (bug 403513). michael@0: cairo_set_scaled_font(cr, cairoFont); michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: gfxFT2FontBase::ConstructFontOptions() michael@0: { michael@0: NS_LossyConvertUTF16toASCII name(this->GetName()); michael@0: mFontOptions.mName = name.get(); michael@0: michael@0: const gfxFontStyle* style = this->GetStyle(); michael@0: if (style->style == NS_FONT_STYLE_ITALIC) { michael@0: if (style->weight == NS_FONT_WEIGHT_BOLD) { michael@0: mFontOptions.mStyle = FontStyle::BOLD_ITALIC; michael@0: } else { michael@0: mFontOptions.mStyle = FontStyle::ITALIC; michael@0: } michael@0: } else { michael@0: if (style->weight == NS_FONT_WEIGHT_BOLD) { michael@0: mFontOptions.mStyle = FontStyle::BOLD; michael@0: } else { michael@0: mFontOptions.mStyle = FontStyle::NORMAL; michael@0: } michael@0: } michael@0: }