1 /*
2 * Copyright 2011 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "include/core/SkFontMetrics.h"
9 #include "include/core/SkFontMgr.h"
10 #include "include/core/SkStream.h"
11 #include "include/core/SkTypeface.h"
12 #include "include/private/SkMutex.h"
13 #include "include/private/SkOnce.h"
14 #include "include/utils/SkCustomTypeface.h"
15 #include "src/core/SkAdvancedTypefaceMetrics.h"
16 #include "src/core/SkEndian.h"
17 #include "src/core/SkFontDescriptor.h"
18 #include "src/core/SkFontPriv.h"
19 #include "src/core/SkLRUCache.h"
20 #include "src/core/SkScalerContext.h"
21 #include "src/core/SkSurfacePriv.h"
22 #include "src/core/SkTypefaceCache.h"
23 #include "src/sfnt/SkOTTable_OS_2.h"
24 #include "src/utils/SkUTF.h"
25
26 #include <mutex>
27
SkTypeface(const SkFontStyle & style,bool isFixedPitch)28 SkTypeface::SkTypeface(const SkFontStyle& style, bool isFixedPitch)
29 : fUniqueID(SkTypefaceCache::NewFontID()), fStyle(style), fIsFixedPitch(isFixedPitch) { }
30
~SkTypeface()31 SkTypeface::~SkTypeface() { }
32
33 ///////////////////////////////////////////////////////////////////////////////
34
35 namespace {
36
37 class SkEmptyTypeface : public SkTypeface {
38 public:
Make()39 static sk_sp<SkTypeface> Make() { return sk_sp<SkTypeface>(new SkEmptyTypeface); }
40 protected:
SkEmptyTypeface()41 SkEmptyTypeface() : SkTypeface(SkFontStyle(), true) { }
42
onOpenStream(int * ttcIndex) const43 std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override { return nullptr; }
onMakeClone(const SkFontArguments & args) const44 sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override {
45 return sk_ref_sp(this);
46 }
onCreateScalerContext(const SkScalerContextEffects & effects,const SkDescriptor * desc) const47 std::unique_ptr<SkScalerContext> onCreateScalerContext(
48 const SkScalerContextEffects& effects, const SkDescriptor* desc) const override
49 {
50 return SkScalerContext::MakeEmpty(
51 sk_ref_sp(const_cast<SkEmptyTypeface*>(this)), effects, desc);
52 }
onFilterRec(SkScalerContextRec *) const53 void onFilterRec(SkScalerContextRec*) const override { }
onGetAdvancedMetrics() const54 std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override {
55 return nullptr;
56 }
onGetFontDescriptor(SkFontDescriptor *,bool *) const57 void onGetFontDescriptor(SkFontDescriptor*, bool*) const override { }
onCharsToGlyphs(const SkUnichar * chars,int count,SkGlyphID glyphs[]) const58 void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const override {
59 sk_bzero(glyphs, count * sizeof(glyphs[0]));
60 }
onCountGlyphs() const61 int onCountGlyphs() const override { return 0; }
getPostScriptGlyphNames(SkString *) const62 void getPostScriptGlyphNames(SkString*) const override {}
getGlyphToUnicodeMap(SkUnichar *) const63 void getGlyphToUnicodeMap(SkUnichar*) const override {}
onGetUPEM() const64 int onGetUPEM() const override { return 0; }
65 class EmptyLocalizedStrings : public SkTypeface::LocalizedStrings {
66 public:
next(SkTypeface::LocalizedString *)67 bool next(SkTypeface::LocalizedString*) override { return false; }
68 };
onGetFamilyName(SkString * familyName) const69 void onGetFamilyName(SkString* familyName) const override {
70 familyName->reset();
71 }
onGetPostScriptName(SkString *) const72 bool onGetPostScriptName(SkString*) const override {
73 return false;
74 }
onCreateFamilyNameIterator() const75 SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override {
76 return new EmptyLocalizedStrings;
77 }
onGlyphMaskNeedsCurrentColor() const78 bool onGlyphMaskNeedsCurrentColor() const override {
79 return false;
80 }
onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],int coordinateCount) const81 int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
82 int coordinateCount) const override
83 {
84 return 0;
85 }
onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],int parameterCount) const86 int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],
87 int parameterCount) const override
88 {
89 return 0;
90 }
onGetTableTags(SkFontTableTag tags[]) const91 int onGetTableTags(SkFontTableTag tags[]) const override { return 0; }
onGetTableData(SkFontTableTag,size_t,size_t,void *) const92 size_t onGetTableData(SkFontTableTag, size_t, size_t, void*) const override {
93 return 0;
94 }
95 };
96
97 constexpr int MAX_VARFONT_CACHE_SIZE = 64;
98
99 class SkVarFontCache {
100 public:
SkVarFontCache()101 SkVarFontCache() : fLRUCache(MAX_VARFONT_CACHE_SIZE) {}
102
Instance()103 static SkVarFontCache& Instance()
104 {
105 static SkVarFontCache cache;
106 return cache;
107 }
108
GetVarFont(sk_sp<SkTypeface> typeface,const SkFontArguments & args)109 sk_sp<SkTypeface> GetVarFont(sk_sp<SkTypeface> typeface, const SkFontArguments& args)
110 {
111 if (!typeface) {
112 return nullptr;
113 }
114
115 size_t hash = 0;
116 hash ^= std::hash<uint32_t>()(typeface->uniqueID());
117 hash ^= std::hash<int>()(args.getCollectionIndex());
118 const auto& positions = args.getVariationDesignPosition();
119 for (int i = 0; i < positions.coordinateCount; i++) {
120 const auto& coord = positions.coordinates[i];
121 hash ^= std::hash<SkFourByteTag>()(coord.axis);
122 hash ^= std::hash<float>()(coord.value);
123 }
124
125 std::lock_guard<std::mutex> lock(fMutex);
126 auto cached = fLRUCache.find(hash);
127 if (!cached) {
128 int ttcIndex = args.getCollectionIndex();
129 auto varTypeface = SkFontMgr::RefDefault()->makeFromStream(typeface->openStream(&ttcIndex), args);
130 if (!varTypeface) {
131 return typeface;
132 } else {
133 fLRUCache.insert(hash, varTypeface);
134 return varTypeface;
135 }
136 }
137
138 return *cached;
139 }
140
141 private:
142 SkLRUCache<uint32_t, sk_sp<SkTypeface>> fLRUCache;
143 std::mutex fMutex;
144 };
145
146 } // namespace
147
FromOldStyle(Style oldStyle)148 SkFontStyle SkTypeface::FromOldStyle(Style oldStyle) {
149 return SkFontStyle((oldStyle & SkTypeface::kBold) ? SkFontStyle::kBold_Weight
150 : SkFontStyle::kNormal_Weight,
151 SkFontStyle::kNormal_Width,
152 (oldStyle & SkTypeface::kItalic) ? SkFontStyle::kItalic_Slant
153 : SkFontStyle::kUpright_Slant);
154 }
155
GetDefaultTypeface(Style style)156 SkTypeface* SkTypeface::GetDefaultTypeface(Style style) {
157 static SkOnce once[4];
158 static sk_sp<SkTypeface> defaults[4];
159
160 SkASSERT((int)style < 4);
161 once[style]([style] {
162 sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
163 auto t = fm->legacyMakeTypeface(nullptr, FromOldStyle(style));
164 defaults[style] = t ? t : SkEmptyTypeface::Make();
165 });
166 return defaults[style].get();
167 }
168
MakeDefault()169 sk_sp<SkTypeface> SkTypeface::MakeDefault() {
170 return sk_ref_sp(GetDefaultTypeface());
171 }
172
UniqueID(const SkTypeface * face)173 uint32_t SkTypeface::UniqueID(const SkTypeface* face) {
174 if (nullptr == face) {
175 face = GetDefaultTypeface();
176 }
177 return face->uniqueID();
178 }
179
Equal(const SkTypeface * facea,const SkTypeface * faceb)180 bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) {
181 return facea == faceb || SkTypeface::UniqueID(facea) == SkTypeface::UniqueID(faceb);
182 }
183
184 ///////////////////////////////////////////////////////////////////////////////
185
MakeFromName(const char name[],SkFontStyle fontStyle)186 sk_sp<SkTypeface> SkTypeface::MakeFromName(const char name[],
187 SkFontStyle fontStyle) {
188 if (nullptr == name && (fontStyle.slant() == SkFontStyle::kItalic_Slant ||
189 fontStyle.slant() == SkFontStyle::kUpright_Slant) &&
190 (fontStyle.weight() == SkFontStyle::kBold_Weight ||
191 fontStyle.weight() == SkFontStyle::kNormal_Weight)) {
192 return sk_ref_sp(GetDefaultTypeface(static_cast<SkTypeface::Style>(
193 (fontStyle.slant() == SkFontStyle::kItalic_Slant ? SkTypeface::kItalic :
194 SkTypeface::kNormal) |
195 (fontStyle.weight() == SkFontStyle::kBold_Weight ? SkTypeface::kBold :
196 SkTypeface::kNormal))));
197 }
198 return SkFontMgr::RefDefault()->legacyMakeTypeface(name, fontStyle);
199 }
200
MakeFromStream(std::unique_ptr<SkStreamAsset> stream,int index)201 sk_sp<SkTypeface> SkTypeface::MakeFromStream(std::unique_ptr<SkStreamAsset> stream, int index) {
202 if (!stream) {
203 return nullptr;
204 }
205 return SkFontMgr::RefDefault()->makeFromStream(std::move(stream), index);
206 }
207
MakeFromData(sk_sp<SkData> data,int index)208 sk_sp<SkTypeface> SkTypeface::MakeFromData(sk_sp<SkData> data, int index) {
209 if (!data) {
210 return nullptr;
211 }
212 return SkFontMgr::RefDefault()->makeFromData(std::move(data), index);
213 }
214
MakeFromFile(const char path[],int index)215 sk_sp<SkTypeface> SkTypeface::MakeFromFile(const char path[], int index) {
216 return SkFontMgr::RefDefault()->makeFromFile(path, index);
217 }
218
makeClone(const SkFontArguments & args) const219 sk_sp<SkTypeface> SkTypeface::makeClone(const SkFontArguments& args) const {
220 return this->onMakeClone(args);
221 }
222
223 ///////////////////////////////////////////////////////////////////////////////
224
serialize(SkWStream * wstream,SerializeBehavior behavior) const225 void SkTypeface::serialize(SkWStream* wstream, SerializeBehavior behavior) const {
226 bool isLocalData = false;
227 SkFontDescriptor desc;
228 this->onGetFontDescriptor(&desc, &isLocalData);
229
230 bool shouldSerializeData = false;
231 switch (behavior) {
232 case SerializeBehavior::kDoIncludeData: shouldSerializeData = true; break;
233 case SerializeBehavior::kDontIncludeData: shouldSerializeData = false; break;
234 case SerializeBehavior::kIncludeDataIfLocal: shouldSerializeData = isLocalData; break;
235 }
236
237 if (shouldSerializeData) {
238 int index;
239 desc.setStream(this->openStream(&index));
240 if (desc.hasStream()) {
241 desc.setCollectionIndex(index);
242 }
243
244 int numAxes = this->getVariationDesignPosition(nullptr, 0);
245 if (0 < numAxes) {
246 numAxes = this->getVariationDesignPosition(desc.setVariationCoordinates(numAxes), numAxes);
247 if (numAxes <= 0) {
248 desc.setVariationCoordinates(0);
249 }
250 }
251 }
252 desc.serialize(wstream);
253 }
254
serialize(SerializeBehavior behavior) const255 sk_sp<SkData> SkTypeface::serialize(SerializeBehavior behavior) const {
256 SkDynamicMemoryWStream stream;
257 this->serialize(&stream, behavior);
258 return stream.detachAsData();
259 }
260
MakeDeserialize(SkStream * stream)261 sk_sp<SkTypeface> SkTypeface::MakeDeserialize(SkStream* stream) {
262 SkFontDescriptor desc;
263 if (!SkFontDescriptor::Deserialize(stream, &desc)) {
264 return nullptr;
265 }
266
267 if (desc.hasStream()) {
268 if (auto tf = SkCustomTypefaceBuilder::Deserialize(desc.dupStream().get())) {
269 return tf;
270 }
271 }
272
273 if (desc.hasStream()) {
274 SkFontArguments args;
275 args.setCollectionIndex(desc.getCollectionIndex());
276 args.setVariationDesignPosition({desc.getVariation(), desc.getVariationCoordinateCount()});
277 sk_sp<SkFontMgr> defaultFm = SkFontMgr::RefDefault();
278 sk_sp<SkTypeface> typeface = defaultFm->makeFromStream(desc.detachStream(), args);
279 if (typeface) {
280 return typeface;
281 }
282 }
283
284 auto typeface = SkTypeface::MakeFromName(desc.getFamilyName(), desc.getStyle());
285 if (desc.getVariationCoordinateCount() > 0 && typeface) {
286 SkFontArguments args;
287 args.setCollectionIndex(desc.getCollectionIndex());
288 args.setVariationDesignPosition({desc.getVariation(), desc.getVariationCoordinateCount()});
289 typeface = SkVarFontCache::Instance().GetVarFont(typeface, args);
290 }
291
292 return typeface;
293 }
294
isCustomTypeface() const295 bool SkTypeface::isCustomTypeface() const {
296 return fIsCustom;
297 }
298
setIsCustomTypeface(bool isCustom)299 void SkTypeface::setIsCustomTypeface(bool isCustom) {
300 fIsCustom = isCustom;
301 }
302
openExistingStream(int * ttcIndex) const303 std::unique_ptr<SkStreamAsset> SkTypeface::openExistingStream(int* ttcIndex) const {
304 int ttcIndexStorage;
305 if (nullptr == ttcIndex) {
306 // So our subclasses don't need to check for null param
307 ttcIndex = &ttcIndexStorage;
308 }
309 return this->onOpenExistingStream(ttcIndex);
310 }
311
onOpenExistingStream(int * ttcIndex) const312 std::unique_ptr<SkStreamAsset> SkTypeface::onOpenExistingStream(int* ttcIndex) const {
313 return this->onOpenStream(ttcIndex);
314 }
315
316 ///////////////////////////////////////////////////////////////////////////////
317
glyphMaskNeedsCurrentColor() const318 bool SkTypeface::glyphMaskNeedsCurrentColor() const {
319 return this->onGlyphMaskNeedsCurrentColor();
320 }
321
getVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],int coordinateCount) const322 int SkTypeface::getVariationDesignPosition(
323 SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const
324 {
325 return this->onGetVariationDesignPosition(coordinates, coordinateCount);
326 }
327
getVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],int parameterCount) const328 int SkTypeface::getVariationDesignParameters(
329 SkFontParameters::Variation::Axis parameters[], int parameterCount) const
330 {
331 return this->onGetVariationDesignParameters(parameters, parameterCount);
332 }
333
countTables() const334 int SkTypeface::countTables() const {
335 return this->onGetTableTags(nullptr);
336 }
337
getTableTags(SkFontTableTag tags[]) const338 int SkTypeface::getTableTags(SkFontTableTag tags[]) const {
339 return this->onGetTableTags(tags);
340 }
341
getTableSize(SkFontTableTag tag) const342 size_t SkTypeface::getTableSize(SkFontTableTag tag) const {
343 return this->onGetTableData(tag, 0, ~0U, nullptr);
344 }
345
getTableData(SkFontTableTag tag,size_t offset,size_t length,void * data) const346 size_t SkTypeface::getTableData(SkFontTableTag tag, size_t offset, size_t length,
347 void* data) const {
348 return this->onGetTableData(tag, offset, length, data);
349 }
350
copyTableData(SkFontTableTag tag) const351 sk_sp<SkData> SkTypeface::copyTableData(SkFontTableTag tag) const {
352 return this->onCopyTableData(tag);
353 }
354
onCopyTableData(SkFontTableTag tag) const355 sk_sp<SkData> SkTypeface::onCopyTableData(SkFontTableTag tag) const {
356 size_t size = this->getTableSize(tag);
357 if (size) {
358 sk_sp<SkData> data = SkData::MakeUninitialized(size);
359 (void)this->getTableData(tag, 0, size, data->writable_data());
360 return data;
361 }
362 return nullptr;
363 }
364
openStream(int * ttcIndex) const365 std::unique_ptr<SkStreamAsset> SkTypeface::openStream(int* ttcIndex) const {
366 int ttcIndexStorage;
367 if (nullptr == ttcIndex) {
368 // So our subclasses don't need to check for null param
369 ttcIndex = &ttcIndexStorage;
370 }
371 return this->onOpenStream(ttcIndex);
372 }
373
createScalerContext(const SkScalerContextEffects & effects,const SkDescriptor * desc) const374 std::unique_ptr<SkScalerContext> SkTypeface::createScalerContext(
375 const SkScalerContextEffects& effects, const SkDescriptor* desc) const {
376 std::unique_ptr<SkScalerContext> scalerContext = this->onCreateScalerContext(effects, desc);
377 SkASSERT(scalerContext);
378 return scalerContext;
379 }
380
unicharsToGlyphs(const SkUnichar uni[],int count,SkGlyphID glyphs[]) const381 void SkTypeface::unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const {
382 if (count > 0 && glyphs && uni) {
383 this->onCharsToGlyphs(uni, count, glyphs);
384 }
385 }
386
unicharToGlyph(SkUnichar uni) const387 SkGlyphID SkTypeface::unicharToGlyph(SkUnichar uni) const {
388 SkGlyphID glyphs[1] = { 0 };
389 this->onCharsToGlyphs(&uni, 1, glyphs);
390 return glyphs[0];
391 }
392
393 namespace {
394 class SkConvertToUTF32 {
395 public:
SkConvertToUTF32()396 SkConvertToUTF32() {}
397
convert(const void * text,size_t byteLength,SkTextEncoding encoding)398 const SkUnichar* convert(const void* text, size_t byteLength, SkTextEncoding encoding) {
399 const SkUnichar* uni;
400 switch (encoding) {
401 case SkTextEncoding::kUTF8: {
402 uni = fStorage.reset(byteLength);
403 const char* ptr = (const char*)text;
404 const char* end = ptr + byteLength;
405 for (int i = 0; ptr < end; ++i) {
406 fStorage[i] = SkUTF::NextUTF8(&ptr, end);
407 }
408 } break;
409 case SkTextEncoding::kUTF16: {
410 uni = fStorage.reset(byteLength);
411 const uint16_t* ptr = (const uint16_t*)text;
412 const uint16_t* end = ptr + (byteLength >> 1);
413 for (int i = 0; ptr < end; ++i) {
414 fStorage[i] = SkUTF::NextUTF16(&ptr, end);
415 }
416 } break;
417 case SkTextEncoding::kUTF32:
418 uni = (const SkUnichar*)text;
419 break;
420 default:
421 SK_ABORT("unexpected enum");
422 }
423 return uni;
424 }
425
426 private:
427 SkAutoSTMalloc<256, SkUnichar> fStorage;
428 };
429 }
430
textToGlyphs(const void * text,size_t byteLength,SkTextEncoding encoding,SkGlyphID glyphs[],int maxGlyphCount) const431 int SkTypeface::textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding,
432 SkGlyphID glyphs[], int maxGlyphCount) const {
433 if (0 == byteLength) {
434 return 0;
435 }
436
437 SkASSERT(text);
438
439 int count = SkFontPriv::CountTextElements(text, byteLength, encoding);
440 if (!glyphs || count > maxGlyphCount) {
441 return count;
442 }
443
444 if (encoding == SkTextEncoding::kGlyphID) {
445 memcpy(glyphs, text, count << 1);
446 return count;
447 }
448
449 SkConvertToUTF32 storage;
450 const SkUnichar* uni = storage.convert(text, byteLength, encoding);
451
452 this->unicharsToGlyphs(uni, count, glyphs);
453 return count;
454 }
455
countGlyphs() const456 int SkTypeface::countGlyphs() const {
457 return this->onCountGlyphs();
458 }
459
getUnitsPerEm() const460 int SkTypeface::getUnitsPerEm() const {
461 // should we try to cache this in the base-class?
462 return this->onGetUPEM();
463 }
464
getKerningPairAdjustments(const uint16_t glyphs[],int count,int32_t adjustments[]) const465 bool SkTypeface::getKerningPairAdjustments(const uint16_t glyphs[], int count,
466 int32_t adjustments[]) const {
467 SkASSERT(count >= 0);
468 // check for the only legal way to pass a nullptr.. everything is 0
469 // in which case they just want to know if this face can possibly support
470 // kerning (true) or never (false).
471 if (nullptr == glyphs || nullptr == adjustments) {
472 SkASSERT(nullptr == glyphs);
473 SkASSERT(0 == count);
474 SkASSERT(nullptr == adjustments);
475 }
476 return this->onGetKerningPairAdjustments(glyphs, count, adjustments);
477 }
478
createFamilyNameIterator() const479 SkTypeface::LocalizedStrings* SkTypeface::createFamilyNameIterator() const {
480 return this->onCreateFamilyNameIterator();
481 }
482
getFamilyName(SkString * name) const483 void SkTypeface::getFamilyName(SkString* name) const {
484 SkASSERT(name);
485 this->onGetFamilyName(name);
486 }
487
getPostScriptName(SkString * name) const488 bool SkTypeface::getPostScriptName(SkString* name) const {
489 return this->onGetPostScriptName(name);
490 }
491
getGlyphToUnicodeMap(SkUnichar * dst) const492 void SkTypeface::getGlyphToUnicodeMap(SkUnichar* dst) const {
493 sk_bzero(dst, sizeof(SkUnichar) * this->countGlyphs());
494 }
495
getAdvancedMetrics() const496 std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface::getAdvancedMetrics() const {
497 std::unique_ptr<SkAdvancedTypefaceMetrics> result = this->onGetAdvancedMetrics();
498 if (result && result->fPostScriptName.isEmpty()) {
499 result->fPostScriptName = result->fFontName;
500 }
501 if (result && result->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
502 SkOTTableOS2::Version::V2::Type::Field fsType;
503 constexpr SkFontTableTag os2Tag = SkTEndian_SwapBE32(SkOTTableOS2::TAG);
504 constexpr size_t fsTypeOffset = offsetof(SkOTTableOS2::Version::V2, fsType);
505 if (this->getTableData(os2Tag, fsTypeOffset, sizeof(fsType), &fsType) == sizeof(fsType)) {
506 if (fsType.Bitmap || (fsType.Restricted && !(fsType.PreviewPrint || fsType.Editable))) {
507 result->fFlags |= SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag;
508 }
509 if (fsType.NoSubsetting) {
510 result->fFlags |= SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag;
511 }
512 }
513 }
514 return result;
515 }
516
onGetKerningPairAdjustments(const uint16_t glyphs[],int count,int32_t adjustments[]) const517 bool SkTypeface::onGetKerningPairAdjustments(const uint16_t glyphs[], int count,
518 int32_t adjustments[]) const {
519 return false;
520 }
521
522 ///////////////////////////////////////////////////////////////////////////////
523
524 #include "include/core/SkPaint.h"
525 #include "src/core/SkDescriptor.h"
526
getBounds() const527 SkRect SkTypeface::getBounds() const {
528 fBoundsOnce([this] {
529 if (!this->onComputeBounds(&fBounds)) {
530 fBounds.setEmpty();
531 }
532 });
533 return fBounds;
534 }
535
onComputeBounds(SkRect * bounds) const536 bool SkTypeface::onComputeBounds(SkRect* bounds) const {
537 // we use a big size to ensure lots of significant bits from the scalercontext.
538 // then we scale back down to return our final answer (at 1-pt)
539 const SkScalar textSize = 2048;
540 const SkScalar invTextSize = 1 / textSize;
541
542 SkFont font;
543 font.setTypeface(sk_ref_sp(const_cast<SkTypeface*>(this)));
544 font.setSize(textSize);
545 font.setLinearMetrics(true);
546
547 SkScalerContextRec rec;
548 SkScalerContextEffects effects;
549
550 SkScalerContext::MakeRecAndEffectsFromFont(font, &rec, &effects);
551
552 SkAutoDescriptor ad;
553 SkScalerContextEffects noeffects;
554 SkScalerContext::AutoDescriptorGivenRecAndEffects(rec, noeffects, &ad);
555
556 std::unique_ptr<SkScalerContext> ctx = this->createScalerContext(noeffects, ad.getDesc());
557
558 SkFontMetrics fm;
559 ctx->getFontMetrics(&fm);
560 if (!fm.hasBounds()) {
561 return false;
562 }
563 bounds->setLTRB(fm.fXMin * invTextSize, fm.fTop * invTextSize,
564 fm.fXMax * invTextSize, fm.fBottom * invTextSize);
565 return true;
566 }
567
onGetAdvancedMetrics() const568 std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface::onGetAdvancedMetrics() const {
569 SkDEBUGFAIL("Typefaces that need to work with PDF backend must override this.");
570 return nullptr;
571 }
572