1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Foundation code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2009
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Vladimir Vukicevic <vladimir@mozilla.com>
23 : * Masayuki Nakano <masayuki@d-toybox.com>
24 : * Behdad Esfahbod <behdad@gnome.org>
25 : * Mats Palmgren <mats.palmgren@bredband.net>
26 : * Karl Tomlinson <karlt+@karlt.net>, Mozilla Corporation
27 : * Takuro Ashie <ashie@clear-code.com>
28 : *
29 : * Alternatively, the contents of this file may be used under the terms of
30 : * either the GNU General Public License Version 2 or later (the "GPL"), or
31 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
32 : * in which case the provisions of the GPL or the LGPL are applicable instead
33 : * of those above. If you wish to allow use of your version of this file only
34 : * under the terms of either the GPL or the LGPL, and not to allow others to
35 : * use your version of this file under the terms of the MPL, indicate your
36 : * decision by deleting the provisions above and replace them with the notice
37 : * and other provisions required by the GPL or the LGPL. If you do not delete
38 : * the provisions above, a recipient may use your version of this file under
39 : * the terms of any one of the MPL, the GPL or the LGPL.
40 : *
41 : * ***** END LICENSE BLOCK ***** */
42 :
43 : #include "gfxFT2FontBase.h"
44 : #include "gfxFT2Utils.h"
45 : #include "harfbuzz/hb-blob.h"
46 :
47 0 : gfxFT2FontBase::gfxFT2FontBase(cairo_scaled_font_t *aScaledFont,
48 : gfxFontEntry *aFontEntry,
49 : const gfxFontStyle *aFontStyle)
50 : : gfxFont(aFontEntry, aFontStyle, kAntialiasDefault, aScaledFont),
51 : mSpaceGlyph(0),
52 0 : mHasMetrics(false)
53 : {
54 0 : cairo_scaled_font_reference(mScaledFont);
55 0 : }
56 :
57 0 : gfxFT2FontBase::~gfxFT2FontBase()
58 : {
59 0 : cairo_scaled_font_destroy(mScaledFont);
60 0 : }
61 :
62 : PRUint32
63 0 : gfxFT2FontBase::GetGlyph(PRUint32 aCharCode)
64 : {
65 : // FcFreeTypeCharIndex needs to lock the FT_Face and can end up searching
66 : // through all the postscript glyph names in the font. Therefore use a
67 : // lightweight cache, which is stored on the cairo_font_face_t.
68 :
69 : cairo_font_face_t *face =
70 0 : cairo_scaled_font_get_font_face(CairoScaledFont());
71 :
72 0 : if (cairo_font_face_status(face) != CAIRO_STATUS_SUCCESS)
73 0 : return 0;
74 :
75 : // This cache algorithm and size is based on what is done in
76 : // cairo_scaled_font_text_to_glyphs and pango_fc_font_real_get_glyph. I
77 : // think the concept is that adjacent characters probably come mostly from
78 : // one Unicode block. This assumption is probably not so valid with
79 : // scripts with large character sets as used for East Asian languages.
80 :
81 : struct CmapCacheSlot {
82 : PRUint32 mCharCode;
83 : PRUint32 mGlyphIndex;
84 : };
85 0 : const PRUint32 kNumSlots = 256;
86 : static cairo_user_data_key_t sCmapCacheKey;
87 :
88 : CmapCacheSlot *slots = static_cast<CmapCacheSlot*>
89 0 : (cairo_font_face_get_user_data(face, &sCmapCacheKey));
90 :
91 0 : if (!slots) {
92 : // cairo's caches can keep some cairo_font_faces alive past our last
93 : // destroy, so the destroy function (free) for the cache must be
94 : // callable from cairo without any assumptions about what other
95 : // modules have not been shutdown.
96 : slots = static_cast<CmapCacheSlot*>
97 0 : (calloc(kNumSlots, sizeof(CmapCacheSlot)));
98 0 : if (!slots)
99 0 : return 0;
100 :
101 : cairo_status_t status =
102 0 : cairo_font_face_set_user_data(face, &sCmapCacheKey, slots, free);
103 0 : if (status != CAIRO_STATUS_SUCCESS) { // OOM
104 0 : free(slots);
105 0 : return 0;
106 : }
107 :
108 : // Invalidate slot 0 by setting its char code to something that would
109 : // never end up in slot 0. All other slots are already invalid
110 : // because they have mCharCode = 0 and a glyph for char code 0 will
111 : // always be in the slot 0.
112 0 : slots[0].mCharCode = 1;
113 : }
114 :
115 0 : CmapCacheSlot *slot = &slots[aCharCode % kNumSlots];
116 0 : if (slot->mCharCode != aCharCode) {
117 0 : slot->mCharCode = aCharCode;
118 0 : slot->mGlyphIndex = gfxFT2LockedFace(this).GetGlyph(aCharCode);
119 : }
120 :
121 0 : return slot->mGlyphIndex;
122 : }
123 :
124 : void
125 0 : gfxFT2FontBase::GetGlyphExtents(PRUint32 aGlyph, cairo_text_extents_t* aExtents)
126 : {
127 0 : NS_PRECONDITION(aExtents != NULL, "aExtents must not be NULL");
128 :
129 : cairo_glyph_t glyphs[1];
130 0 : glyphs[0].index = aGlyph;
131 0 : glyphs[0].x = 0.0;
132 0 : glyphs[0].y = 0.0;
133 : // cairo does some caching for us here but perhaps a small gain could be
134 : // made by caching more. It is usually only the advance that is needed,
135 : // so caching only the advance could allow many requests to be cached with
136 : // little memory use. Ideally this cache would be merged with
137 : // gfxGlyphExtents.
138 0 : cairo_scaled_font_glyph_extents(CairoScaledFont(), glyphs, 1, aExtents);
139 0 : }
140 :
141 : const gfxFont::Metrics&
142 0 : gfxFT2FontBase::GetMetrics()
143 : {
144 0 : if (mHasMetrics)
145 0 : return mMetrics;
146 :
147 0 : if (NS_UNLIKELY(GetStyle()->size <= 0.0)) {
148 0 : new(&mMetrics) gfxFont::Metrics(); // zero initialize
149 0 : mSpaceGlyph = 0;
150 : } else {
151 0 : gfxFT2LockedFace(this).GetMetrics(&mMetrics, &mSpaceGlyph);
152 : }
153 :
154 0 : SanitizeMetrics(&mMetrics, false);
155 :
156 : #if 0
157 : // printf("font name: %s %f\n", NS_ConvertUTF16toUTF8(GetName()).get(), GetStyle()->size);
158 : // printf ("pango font %s\n", pango_font_description_to_string (pango_font_describe (font)));
159 :
160 : fprintf (stderr, "Font: %s\n", NS_ConvertUTF16toUTF8(GetName()).get());
161 : fprintf (stderr, " emHeight: %f emAscent: %f emDescent: %f\n", mMetrics.emHeight, mMetrics.emAscent, mMetrics.emDescent);
162 : fprintf (stderr, " maxAscent: %f maxDescent: %f\n", mMetrics.maxAscent, mMetrics.maxDescent);
163 : fprintf (stderr, " internalLeading: %f externalLeading: %f\n", mMetrics.externalLeading, mMetrics.internalLeading);
164 : fprintf (stderr, " spaceWidth: %f aveCharWidth: %f xHeight: %f\n", mMetrics.spaceWidth, mMetrics.aveCharWidth, mMetrics.xHeight);
165 : 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);
166 : #endif
167 :
168 0 : mHasMetrics = true;
169 0 : return mMetrics;
170 : }
171 :
172 : // Get the glyphID of a space
173 : PRUint32
174 0 : gfxFT2FontBase::GetSpaceGlyph()
175 : {
176 0 : NS_ASSERTION(GetStyle()->size != 0,
177 : "forgot to short-circuit a text run with zero-sized font?");
178 0 : GetMetrics();
179 0 : return mSpaceGlyph;
180 : }
181 :
182 : hb_blob_t *
183 0 : gfxFT2FontBase::GetFontTable(PRUint32 aTag)
184 : {
185 : hb_blob_t *blob;
186 0 : if (mFontEntry->GetExistingFontTable(aTag, &blob))
187 0 : return blob;
188 :
189 0 : FallibleTArray<PRUint8> buffer;
190 0 : bool haveTable = gfxFT2LockedFace(this).GetFontTable(aTag, buffer);
191 :
192 : // Cache even when there is no table to save having to open the FT_Face
193 : // again.
194 : return mFontEntry->ShareFontTableAndGetBlob(aTag,
195 0 : haveTable ? &buffer : nsnull);
196 : }
197 :
198 : PRUint32
199 0 : gfxFT2FontBase::GetGlyph(PRUint32 unicode, PRUint32 variation_selector)
200 : {
201 0 : if (variation_selector) {
202 : PRUint32 id =
203 0 : gfxFT2LockedFace(this).GetUVSGlyph(unicode, variation_selector);
204 0 : if (id)
205 0 : return id;
206 : }
207 :
208 0 : return GetGlyph(unicode);
209 : }
210 :
211 : PRInt32
212 0 : gfxFT2FontBase::GetGlyphWidth(gfxContext *aCtx, PRUint16 aGID)
213 : {
214 : cairo_text_extents_t extents;
215 0 : GetGlyphExtents(aGID, &extents);
216 : // convert to 16.16 fixed point
217 0 : return NS_lround(0x10000 * extents.x_advance);
218 : }
219 :
220 : bool
221 0 : gfxFT2FontBase::SetupCairoFont(gfxContext *aContext)
222 : {
223 0 : cairo_t *cr = aContext->GetCairo();
224 :
225 : // The scaled font ctm is not relevant right here because
226 : // cairo_set_scaled_font does not record the scaled font itself, but
227 : // merely the font_face, font_matrix, font_options. The scaled_font used
228 : // for the target can be different from the scaled_font passed to
229 : // cairo_set_scaled_font. (Unfortunately we have measured only for an
230 : // identity ctm.)
231 0 : cairo_scaled_font_t *cairoFont = CairoScaledFont();
232 :
233 0 : if (cairo_scaled_font_status(cairoFont) != CAIRO_STATUS_SUCCESS) {
234 : // Don't cairo_set_scaled_font as that would propagate the error to
235 : // the cairo_t, precluding any further drawing.
236 0 : return false;
237 : }
238 : // Thoughts on which font_options to set on the context:
239 : //
240 : // cairoFont has been created for screen rendering.
241 : //
242 : // When the context is being used for screen rendering, we should set
243 : // font_options such that the same scaled_font gets used (when the ctm is
244 : // the same). The use of explicit font_options recorded in
245 : // CreateScaledFont ensures that this will happen.
246 : //
247 : // XXXkt: For pdf and ps surfaces, I don't know whether it's better to
248 : // remove surface-specific options, or try to draw with the same
249 : // scaled_font that was used to measure. As the same font_face is being
250 : // used, its font_options will often override some values anyway (unless
251 : // perhaps we remove those from the FcPattern at face creation).
252 : //
253 : // I can't see any significant difference in printing, irrespective of
254 : // what is set here. It's too late to change things here as measuring has
255 : // already taken place. We should really be measuring with a different
256 : // font for pdf and ps surfaces (bug 403513).
257 0 : cairo_set_scaled_font(cr, cairoFont);
258 0 : return true;
259 : }
|