| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 The Android Open Source Project | 2 * Copyright 2011 The Android Open Source Project |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkAdvancedTypefaceMetrics.h" | 8 #include "SkAdvancedTypefaceMetrics.h" |
| 9 #include "SkEndian.h" | 9 #include "SkEndian.h" |
| 10 #include "SkFontDescriptor.h" | 10 #include "SkFontDescriptor.h" |
| 11 #include "SkFontMgr.h" | 11 #include "SkFontMgr.h" |
| 12 #include "SkMutex.h" | 12 #include "SkMutex.h" |
| 13 #include "SkOTTable_OS_2.h" | 13 #include "SkOTTable_OS_2.h" |
| 14 #include "SkOnce.h" | 14 #include "SkOnce.h" |
| 15 #include "SkStream.h" | 15 #include "SkStream.h" |
| 16 #include "SkTypeface.h" | 16 #include "SkTypeface.h" |
| 17 | 17 |
| 18 SkTypeface::SkTypeface(const SkFontStyle& style, SkFontID fontID, bool isFixedPi
tch) | 18 SkTypeface::SkTypeface(const SkFontStyle& style, SkFontID fontID, bool isFixedPi
tch) |
| 19 : fUniqueID(fontID), fStyle(style), fIsFixedPitch(isFixedPitch) { } | 19 : fUniqueID(fontID), fStyle(style), fIsFixedPitch(isFixedPitch) { } |
| 20 | 20 |
| 21 SkTypeface::~SkTypeface() { } | 21 SkTypeface::~SkTypeface() { } |
| 22 | 22 |
| 23 #ifdef SK_WHITELIST_SERIALIZED_TYPEFACES | 23 #ifdef SK_WHITELIST_SERIALIZED_TYPEFACES |
| 24 extern void WhitelistSerializeTypeface(const SkTypeface*, SkWStream* ); | 24 extern void WhitelistSerializeTypeface(const SkTypeface*, SkWStream* ); |
| 25 #define SK_TYPEFACE_DELEGATE WhitelistSerializeTypeface | 25 #define SK_TYPEFACE_DELEGATE WhitelistSerializeTypeface |
| 26 #else | 26 #else |
| 27 #define SK_TYPEFACE_DELEGATE nullptr | 27 #define SK_TYPEFACE_DELEGATE nullptr |
| 28 #endif | 28 #endif |
| 29 | 29 |
| 30 SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style ) = null
ptr; | 30 sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style )
= nullptr; |
| 31 void (*gSerializeTypefaceDelegate)(const SkTypeface*, SkWStream* ) = SK_TYPEFACE
_DELEGATE; | 31 void (*gSerializeTypefaceDelegate)(const SkTypeface*, SkWStream* ) = SK_TYPEFACE
_DELEGATE; |
| 32 SkTypeface* (*gDeserializeTypefaceDelegate)(SkStream* ) = nullptr; | 32 sk_sp<SkTypeface> (*gDeserializeTypefaceDelegate)(SkStream* ) = nullptr; |
| 33 | 33 |
| 34 /////////////////////////////////////////////////////////////////////////////// | 34 /////////////////////////////////////////////////////////////////////////////// |
| 35 | 35 |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 class SkEmptyTypeface : public SkTypeface { | 38 class SkEmptyTypeface : public SkTypeface { |
| 39 public: | 39 public: |
| 40 static SkEmptyTypeface* Create() { return new SkEmptyTypeface; } | 40 static SkEmptyTypeface* Create() { return new SkEmptyTypeface; } |
| 41 protected: | 41 protected: |
| 42 SkEmptyTypeface() : SkTypeface(SkFontStyle(), 0, true) { } | 42 SkEmptyTypeface() : SkTypeface(SkFontStyle(), 0, true) { } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 // CreateTypeface is happening at any given time. | 91 // CreateTypeface is happening at any given time. |
| 92 // TODO(bungeman, mtklein): This is sad. Make our fontconfig code safe? | 92 // TODO(bungeman, mtklein): This is sad. Make our fontconfig code safe? |
| 93 SkAutoMutexAcquire lock(&gCreateDefaultMutex); | 93 SkAutoMutexAcquire lock(&gCreateDefaultMutex); |
| 94 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); | 94 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 95 SkTypeface* t = fm->legacyCreateTypeface(nullptr, SkFontStyle::FromOldSt
yle(style)); | 95 SkTypeface* t = fm->legacyCreateTypeface(nullptr, SkFontStyle::FromOldSt
yle(style)); |
| 96 defaults[style] = t ? t : SkEmptyTypeface::Create(); | 96 defaults[style] = t ? t : SkEmptyTypeface::Create(); |
| 97 }); | 97 }); |
| 98 return defaults[style]; | 98 return defaults[style]; |
| 99 } | 99 } |
| 100 | 100 |
| 101 SkTypeface* SkTypeface::RefDefault(Style style) { | 101 sk_sp<SkTypeface> SkTypeface::MakeDefault(Style style) { |
| 102 return SkRef(GetDefaultTypeface(style)); | 102 return sk_ref_sp(GetDefaultTypeface(style)); |
| 103 } | 103 } |
| 104 | 104 |
| 105 uint32_t SkTypeface::UniqueID(const SkTypeface* face) { | 105 uint32_t SkTypeface::UniqueID(const SkTypeface* face) { |
| 106 if (nullptr == face) { | 106 if (nullptr == face) { |
| 107 face = GetDefaultTypeface(); | 107 face = GetDefaultTypeface(); |
| 108 } | 108 } |
| 109 return face->uniqueID(); | 109 return face->uniqueID(); |
| 110 } | 110 } |
| 111 | 111 |
| 112 bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) { | 112 bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) { |
| 113 return facea == faceb || SkTypeface::UniqueID(facea) == SkTypeface::UniqueID
(faceb); | 113 return facea == faceb || SkTypeface::UniqueID(facea) == SkTypeface::UniqueID
(faceb); |
| 114 } | 114 } |
| 115 | 115 |
| 116 /////////////////////////////////////////////////////////////////////////////// | 116 /////////////////////////////////////////////////////////////////////////////// |
| 117 | 117 |
| 118 SkTypeface* SkTypeface::CreateFromName(const char name[], Style style) { | 118 sk_sp<SkTypeface> SkTypeface::MakeFromName(const char name[], Style style) { |
| 119 if (gCreateTypefaceDelegate) { | 119 if (gCreateTypefaceDelegate) { |
| 120 SkTypeface* result = (*gCreateTypefaceDelegate)(name, style); | 120 sk_sp<SkTypeface> result = (*gCreateTypefaceDelegate)(name, style); |
| 121 if (result) { | 121 if (result) { |
| 122 return result; | 122 return result; |
| 123 } | 123 } |
| 124 } | 124 } |
| 125 if (nullptr == name) { | 125 if (nullptr == name) { |
| 126 return RefDefault(style); | 126 return MakeDefault(style); |
| 127 } | 127 } |
| 128 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); | 128 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 129 return fm->legacyCreateTypeface(name, SkFontStyle::FromOldStyle(style)); | 129 return sk_sp<SkTypeface>(fm->legacyCreateTypeface(name, SkFontStyle::FromOld
Style(style))); |
| 130 } | 130 } |
| 131 | 131 |
| 132 SkTypeface* SkTypeface::CreateFromTypeface(const SkTypeface* family, Style s) { | 132 sk_sp<SkTypeface> SkTypeface::MakeFromTypeface(SkTypeface* family, Style s) { |
| 133 if (!family) { | 133 if (!family) { |
| 134 return SkTypeface::RefDefault(s); | 134 return SkTypeface::MakeDefault(s); |
| 135 } | 135 } |
| 136 | 136 |
| 137 if (family->style() == s) { | 137 if (family->style() == s) { |
| 138 family->ref(); | 138 return sk_ref_sp(family); |
| 139 return const_cast<SkTypeface*>(family); | |
| 140 } | 139 } |
| 141 | 140 |
| 142 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); | 141 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 143 return fm->matchFaceStyle(family, SkFontStyle::FromOldStyle(s)); | 142 return sk_sp<SkTypeface>(fm->matchFaceStyle(family, SkFontStyle::FromOldStyl
e(s))); |
| 144 } | 143 } |
| 145 | 144 |
| 146 SkTypeface* SkTypeface::CreateFromStream(SkStreamAsset* stream, int index) { | 145 sk_sp<SkTypeface> SkTypeface::MakeFromStream(SkStreamAsset* stream, int index) { |
| 147 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); | 146 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 148 return fm->createFromStream(stream, index); | 147 return sk_sp<SkTypeface>(fm->createFromStream(stream, index)); |
| 149 } | 148 } |
| 150 | 149 |
| 151 SkTypeface* SkTypeface::CreateFromFontData(SkFontData* data) { | 150 sk_sp<SkTypeface> SkTypeface::MakeFromFontData(SkFontData* data) { |
| 152 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); | 151 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 153 return fm->createFromFontData(data); | 152 return sk_sp<SkTypeface>(fm->createFromFontData(data)); |
| 154 } | 153 } |
| 155 | 154 |
| 156 SkTypeface* SkTypeface::CreateFromFile(const char path[], int index) { | 155 sk_sp<SkTypeface> SkTypeface::MakeFromFile(const char path[], int index) { |
| 157 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); | 156 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 158 return fm->createFromFile(path, index); | 157 return sk_sp<SkTypeface>(fm->createFromFile(path, index)); |
| 159 } | 158 } |
| 160 | 159 |
| 161 /////////////////////////////////////////////////////////////////////////////// | 160 /////////////////////////////////////////////////////////////////////////////// |
| 162 | 161 |
| 163 void SkTypeface::serialize(SkWStream* wstream) const { | 162 void SkTypeface::serialize(SkWStream* wstream) const { |
| 164 if (gSerializeTypefaceDelegate) { | 163 if (gSerializeTypefaceDelegate) { |
| 165 (*gSerializeTypefaceDelegate)(this, wstream); | 164 (*gSerializeTypefaceDelegate)(this, wstream); |
| 166 return; | 165 return; |
| 167 } | 166 } |
| 168 bool isLocal = false; | 167 bool isLocal = false; |
| 169 SkFontDescriptor desc(this->style()); | 168 SkFontDescriptor desc(this->style()); |
| 170 this->onGetFontDescriptor(&desc, &isLocal); | 169 this->onGetFontDescriptor(&desc, &isLocal); |
| 171 | 170 |
| 172 // Embed font data if it's a local font. | 171 // Embed font data if it's a local font. |
| 173 if (isLocal && !desc.hasFontData()) { | 172 if (isLocal && !desc.hasFontData()) { |
| 174 desc.setFontData(this->onCreateFontData()); | 173 desc.setFontData(this->onCreateFontData()); |
| 175 } | 174 } |
| 176 desc.serialize(wstream); | 175 desc.serialize(wstream); |
| 177 } | 176 } |
| 178 | 177 |
| 179 SkTypeface* SkTypeface::Deserialize(SkStream* stream) { | 178 sk_sp<SkTypeface> SkTypeface::MakeDeserialize(SkStream* stream) { |
| 180 if (gDeserializeTypefaceDelegate) { | 179 if (gDeserializeTypefaceDelegate) { |
| 181 return (*gDeserializeTypefaceDelegate)(stream); | 180 return (*gDeserializeTypefaceDelegate)(stream); |
| 182 } | 181 } |
| 183 | 182 |
| 184 SkFontDescriptor desc; | 183 SkFontDescriptor desc; |
| 185 if (!SkFontDescriptor::Deserialize(stream, &desc)) { | 184 if (!SkFontDescriptor::Deserialize(stream, &desc)) { |
| 186 return nullptr; | 185 return nullptr; |
| 187 } | 186 } |
| 188 | 187 |
| 189 SkFontData* data = desc.detachFontData(); | 188 SkFontData* data = desc.detachFontData(); |
| 190 if (data) { | 189 if (data) { |
| 191 SkTypeface* typeface = SkTypeface::CreateFromFontData(data); | 190 sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFontData(data)); |
| 192 if (typeface) { | 191 if (typeface) { |
| 193 return typeface; | 192 return typeface; |
| 194 } | 193 } |
| 195 } | 194 } |
| 196 return SkTypeface::CreateFromName(desc.getFamilyName(), desc.getStyle()); | 195 return SkTypeface::MakeFromName(desc.getFamilyName(), desc.getStyle()); |
| 197 } | 196 } |
| 198 | 197 |
| 199 /////////////////////////////////////////////////////////////////////////////// | 198 /////////////////////////////////////////////////////////////////////////////// |
| 200 | 199 |
| 201 int SkTypeface::countTables() const { | 200 int SkTypeface::countTables() const { |
| 202 return this->onGetTableTags(nullptr); | 201 return this->onGetTableTags(nullptr); |
| 203 } | 202 } |
| 204 | 203 |
| 205 int SkTypeface::getTableTags(SkFontTableTag tags[]) const { | 204 int SkTypeface::getTableTags(SkFontTableTag tags[]) const { |
| 206 return this->onGetTableTags(tags); | 205 return this->onGetTableTags(tags); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 return fBounds; | 327 return fBounds; |
| 329 } | 328 } |
| 330 | 329 |
| 331 bool SkTypeface::onComputeBounds(SkRect* bounds) const { | 330 bool SkTypeface::onComputeBounds(SkRect* bounds) const { |
| 332 // we use a big size to ensure lots of significant bits from the scalerconte
xt. | 331 // we use a big size to ensure lots of significant bits from the scalerconte
xt. |
| 333 // then we scale back down to return our final answer (at 1-pt) | 332 // then we scale back down to return our final answer (at 1-pt) |
| 334 const SkScalar textSize = 2048; | 333 const SkScalar textSize = 2048; |
| 335 const SkScalar invTextSize = 1 / textSize; | 334 const SkScalar invTextSize = 1 / textSize; |
| 336 | 335 |
| 337 SkPaint paint; | 336 SkPaint paint; |
| 338 paint.setTypeface(const_cast<SkTypeface*>(this)); | 337 paint.setTypeface(sk_ref_sp(const_cast<SkTypeface*>(this))); |
| 339 paint.setTextSize(textSize); | 338 paint.setTextSize(textSize); |
| 340 paint.setLinearText(true); | 339 paint.setLinearText(true); |
| 341 | 340 |
| 342 SkScalerContext::Rec rec; | 341 SkScalerContext::Rec rec; |
| 343 SkScalerContext::MakeRec(paint, nullptr, nullptr, &rec); | 342 SkScalerContext::MakeRec(paint, nullptr, nullptr, &rec); |
| 344 | 343 |
| 345 SkAutoDescriptor ad(sizeof(rec) + SkDescriptor::ComputeOverhead(1)); | 344 SkAutoDescriptor ad(sizeof(rec) + SkDescriptor::ComputeOverhead(1)); |
| 346 SkDescriptor* desc = ad.getDesc(); | 345 SkDescriptor* desc = ad.getDesc(); |
| 347 desc->init(); | 346 desc->init(); |
| 348 desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec); | 347 desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec); |
| 349 | 348 |
| 350 SkScalerContextEffects noeffects; | 349 SkScalerContextEffects noeffects; |
| 351 SkAutoTDelete<SkScalerContext> ctx(this->createScalerContext(noeffects, desc
, true)); | 350 SkAutoTDelete<SkScalerContext> ctx(this->createScalerContext(noeffects, desc
, true)); |
| 352 if (ctx.get()) { | 351 if (ctx.get()) { |
| 353 SkPaint::FontMetrics fm; | 352 SkPaint::FontMetrics fm; |
| 354 ctx->getFontMetrics(&fm); | 353 ctx->getFontMetrics(&fm); |
| 355 bounds->set(fm.fXMin * invTextSize, fm.fTop * invTextSize, | 354 bounds->set(fm.fXMin * invTextSize, fm.fTop * invTextSize, |
| 356 fm.fXMax * invTextSize, fm.fBottom * invTextSize); | 355 fm.fXMax * invTextSize, fm.fBottom * invTextSize); |
| 357 return true; | 356 return true; |
| 358 } | 357 } |
| 359 return false; | 358 return false; |
| 360 } | 359 } |
| OLD | NEW |