1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "gfxFT2FontBase.h"
7 #include "gfxFT2Utils.h"
8 #include "harfbuzz/hb.h"
9 #include "mozilla/Likely.h"
11 using namespace mozilla::gfx
;
13 gfxFT2FontBase::gfxFT2FontBase(cairo_scaled_font_t
*aScaledFont
,
14 gfxFontEntry
*aFontEntry
,
15 const gfxFontStyle
*aFontStyle
)
16 : gfxFont(aFontEntry
, aFontStyle
, kAntialiasDefault
, aScaledFont
),
20 cairo_scaled_font_reference(mScaledFont
);
21 ConstructFontOptions();
24 gfxFT2FontBase::~gfxFT2FontBase()
26 cairo_scaled_font_destroy(mScaledFont
);
30 gfxFT2FontBase::GetGlyph(uint32_t aCharCode
)
32 // FcFreeTypeCharIndex needs to lock the FT_Face and can end up searching
33 // through all the postscript glyph names in the font. Therefore use a
34 // lightweight cache, which is stored on the cairo_font_face_t.
36 cairo_font_face_t
*face
=
37 cairo_scaled_font_get_font_face(CairoScaledFont());
39 if (cairo_font_face_status(face
) != CAIRO_STATUS_SUCCESS
)
42 // This cache algorithm and size is based on what is done in
43 // cairo_scaled_font_text_to_glyphs and pango_fc_font_real_get_glyph. I
44 // think the concept is that adjacent characters probably come mostly from
45 // one Unicode block. This assumption is probably not so valid with
46 // scripts with large character sets as used for East Asian languages.
48 struct CmapCacheSlot
{
52 const uint32_t kNumSlots
= 256;
53 static cairo_user_data_key_t sCmapCacheKey
;
55 CmapCacheSlot
*slots
= static_cast<CmapCacheSlot
*>
56 (cairo_font_face_get_user_data(face
, &sCmapCacheKey
));
59 // cairo's caches can keep some cairo_font_faces alive past our last
60 // destroy, so the destroy function (free) for the cache must be
61 // callable from cairo without any assumptions about what other
62 // modules have not been shutdown.
63 slots
= static_cast<CmapCacheSlot
*>
64 (calloc(kNumSlots
, sizeof(CmapCacheSlot
)));
68 cairo_status_t status
=
69 cairo_font_face_set_user_data(face
, &sCmapCacheKey
, slots
, free
);
70 if (status
!= CAIRO_STATUS_SUCCESS
) { // OOM
75 // Invalidate slot 0 by setting its char code to something that would
76 // never end up in slot 0. All other slots are already invalid
77 // because they have mCharCode = 0 and a glyph for char code 0 will
78 // always be in the slot 0.
79 slots
[0].mCharCode
= 1;
82 CmapCacheSlot
*slot
= &slots
[aCharCode
% kNumSlots
];
83 if (slot
->mCharCode
!= aCharCode
) {
84 slot
->mCharCode
= aCharCode
;
85 slot
->mGlyphIndex
= gfxFT2LockedFace(this).GetGlyph(aCharCode
);
88 return slot
->mGlyphIndex
;
92 gfxFT2FontBase::GetGlyphExtents(uint32_t aGlyph
, cairo_text_extents_t
* aExtents
)
94 NS_PRECONDITION(aExtents
!= NULL
, "aExtents must not be NULL");
96 cairo_glyph_t glyphs
[1];
97 glyphs
[0].index
= aGlyph
;
100 // cairo does some caching for us here but perhaps a small gain could be
101 // made by caching more. It is usually only the advance that is needed,
102 // so caching only the advance could allow many requests to be cached with
103 // little memory use. Ideally this cache would be merged with
105 cairo_scaled_font_glyph_extents(CairoScaledFont(), glyphs
, 1, aExtents
);
108 const gfxFont::Metrics
&
109 gfxFT2FontBase::GetMetrics()
114 if (MOZ_UNLIKELY(GetStyle()->size
<= 0.0)) {
115 new(&mMetrics
) gfxFont::Metrics(); // zero initialize
118 gfxFT2LockedFace(this).GetMetrics(&mMetrics
, &mSpaceGlyph
);
121 SanitizeMetrics(&mMetrics
, false);
124 // printf("font name: %s %f\n", NS_ConvertUTF16toUTF8(GetName()).get(), GetStyle()->size);
125 // printf ("pango font %s\n", pango_font_description_to_string (pango_font_describe (font)));
127 fprintf (stderr
, "Font: %s\n", NS_ConvertUTF16toUTF8(GetName()).get());
128 fprintf (stderr
, " emHeight: %f emAscent: %f emDescent: %f\n", mMetrics
.emHeight
, mMetrics
.emAscent
, mMetrics
.emDescent
);
129 fprintf (stderr
, " maxAscent: %f maxDescent: %f\n", mMetrics
.maxAscent
, mMetrics
.maxDescent
);
130 fprintf (stderr
, " internalLeading: %f externalLeading: %f\n", mMetrics
.externalLeading
, mMetrics
.internalLeading
);
131 fprintf (stderr
, " spaceWidth: %f aveCharWidth: %f xHeight: %f\n", mMetrics
.spaceWidth
, mMetrics
.aveCharWidth
, mMetrics
.xHeight
);
132 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
);
139 // Get the glyphID of a space
141 gfxFT2FontBase::GetSpaceGlyph()
143 NS_ASSERTION(GetStyle()->size
!= 0,
144 "forgot to short-circuit a text run with zero-sized font?");
150 gfxFT2FontBase::GetFontTable(uint32_t aTag
)
153 if (mFontEntry
->GetExistingFontTable(aTag
, &blob
))
156 FallibleTArray
<uint8_t> buffer
;
157 bool haveTable
= gfxFT2LockedFace(this).GetFontTable(aTag
, buffer
);
159 // Cache even when there is no table to save having to open the FT_Face
161 return mFontEntry
->ShareFontTableAndGetBlob(aTag
,
162 haveTable
? &buffer
: nullptr);
166 gfxFT2FontBase::GetGlyph(uint32_t unicode
, uint32_t variation_selector
)
168 if (variation_selector
) {
170 gfxFT2LockedFace(this).GetUVSGlyph(unicode
, variation_selector
);
175 return GetGlyph(unicode
);
179 gfxFT2FontBase::GetGlyphWidth(gfxContext
*aCtx
, uint16_t aGID
)
181 cairo_text_extents_t extents
;
182 GetGlyphExtents(aGID
, &extents
);
183 // convert to 16.16 fixed point
184 return NS_lround(0x10000 * extents
.x_advance
);
188 gfxFT2FontBase::SetupCairoFont(gfxContext
*aContext
)
190 cairo_t
*cr
= aContext
->GetCairo();
192 // The scaled font ctm is not relevant right here because
193 // cairo_set_scaled_font does not record the scaled font itself, but
194 // merely the font_face, font_matrix, font_options. The scaled_font used
195 // for the target can be different from the scaled_font passed to
196 // cairo_set_scaled_font. (Unfortunately we have measured only for an
198 cairo_scaled_font_t
*cairoFont
= CairoScaledFont();
200 if (cairo_scaled_font_status(cairoFont
) != CAIRO_STATUS_SUCCESS
) {
201 // Don't cairo_set_scaled_font as that would propagate the error to
202 // the cairo_t, precluding any further drawing.
205 // Thoughts on which font_options to set on the context:
207 // cairoFont has been created for screen rendering.
209 // When the context is being used for screen rendering, we should set
210 // font_options such that the same scaled_font gets used (when the ctm is
211 // the same). The use of explicit font_options recorded in
212 // CreateScaledFont ensures that this will happen.
214 // XXXkt: For pdf and ps surfaces, I don't know whether it's better to
215 // remove surface-specific options, or try to draw with the same
216 // scaled_font that was used to measure. As the same font_face is being
217 // used, its font_options will often override some values anyway (unless
218 // perhaps we remove those from the FcPattern at face creation).
220 // I can't see any significant difference in printing, irrespective of
221 // what is set here. It's too late to change things here as measuring has
222 // already taken place. We should really be measuring with a different
223 // font for pdf and ps surfaces (bug 403513).
224 cairo_set_scaled_font(cr
, cairoFont
);
229 gfxFT2FontBase::ConstructFontOptions()
231 NS_LossyConvertUTF16toASCII
name(this->GetName());
232 mFontOptions
.mName
= name
.get();
234 const gfxFontStyle
* style
= this->GetStyle();
235 if (style
->style
== NS_FONT_STYLE_ITALIC
) {
236 if (style
->weight
== NS_FONT_WEIGHT_BOLD
) {
237 mFontOptions
.mStyle
= FONT_STYLE_BOLD_ITALIC
;
239 mFontOptions
.mStyle
= FONT_STYLE_ITALIC
;
242 if (style
->weight
== NS_FONT_WEIGHT_BOLD
) {
243 mFontOptions
.mStyle
= FONT_STYLE_BOLD
;
245 mFontOptions
.mStyle
= FONT_STYLE_NORMAL
;