Chromium Code Reviews| 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 "SkOncePtr.h" | 14 #include "SkOncePtr.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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 // CreateTypeface is happening at any given time. | 89 // CreateTypeface is happening at any given time. |
| 90 // TODO(bungeman, mtklein): This is sad. Make our fontconfig code safe? | 90 // TODO(bungeman, mtklein): This is sad. Make our fontconfig code safe? |
| 91 SkAutoMutexAcquire lock(&gCreateDefaultMutex); | 91 SkAutoMutexAcquire lock(&gCreateDefaultMutex); |
| 92 | 92 |
| 93 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); | 93 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 94 SkTypeface* t = fm->legacyCreateTypeface(nullptr, SkFontStyle::FromOldSt yle(style)); | 94 SkTypeface* t = fm->legacyCreateTypeface(nullptr, SkFontStyle::FromOldSt yle(style)); |
| 95 return t ? t : SkEmptyTypeface::Create(); | 95 return t ? t : SkEmptyTypeface::Create(); |
| 96 }); | 96 }); |
| 97 } | 97 } |
| 98 | 98 |
| 99 SkTypeface* SkTypeface::RefDefault(Style style) { | 99 sk_sp<SkTypeface> SkTypeface::MakeDefault(Style style) { |
| 100 return SkRef(GetDefaultTypeface(style)); | 100 return sk_ref_sp(GetDefaultTypeface(style)); |
| 101 } | 101 } |
| 102 | 102 |
| 103 uint32_t SkTypeface::UniqueID(const SkTypeface* face) { | 103 uint32_t SkTypeface::UniqueID(const SkTypeface* face) { |
| 104 if (nullptr == face) { | 104 if (nullptr == face) { |
| 105 face = GetDefaultTypeface(); | 105 face = GetDefaultTypeface(); |
| 106 } | 106 } |
| 107 return face->uniqueID(); | 107 return face->uniqueID(); |
| 108 } | 108 } |
| 109 | 109 |
| 110 bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) { | 110 bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) { |
| 111 return facea == faceb || SkTypeface::UniqueID(facea) == SkTypeface::UniqueID (faceb); | 111 return facea == faceb || SkTypeface::UniqueID(facea) == SkTypeface::UniqueID (faceb); |
| 112 } | 112 } |
| 113 | 113 |
| 114 /////////////////////////////////////////////////////////////////////////////// | 114 /////////////////////////////////////////////////////////////////////////////// |
| 115 | 115 |
| 116 SkTypeface* SkTypeface::CreateFromName(const char name[], Style style) { | 116 sk_sp<SkTypeface> SkTypeface::MakeFromName(const char name[], Style style) { |
| 117 if (gCreateTypefaceDelegate) { | 117 if (gCreateTypefaceDelegate) { |
| 118 SkTypeface* result = (*gCreateTypefaceDelegate)(name, style); | 118 sk_sp<SkTypeface> result = (*gCreateTypefaceDelegate)(name, style); |
| 119 if (result) { | 119 if (result) { |
| 120 return result; | 120 return result; |
| 121 } | 121 } |
| 122 } | 122 } |
| 123 if (nullptr == name) { | 123 if (nullptr == name) { |
| 124 return RefDefault(style); | 124 return MakeDefault(style); |
| 125 } | 125 } |
| 126 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); | 126 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 127 return fm->legacyCreateTypeface(name, SkFontStyle::FromOldStyle(style)); | 127 return sk_sp<SkTypeface>(fm->legacyCreateTypeface(name, SkFontStyle::FromOld Style(style))); |
| 128 } | 128 } |
| 129 | 129 |
| 130 SkTypeface* SkTypeface::CreateFromTypeface(const SkTypeface* family, Style s) { | 130 sk_sp<SkTypeface> SkTypeface::MakeFromTypeface(SkTypeface* family, Style s) { |
| 131 if (!family) { | 131 if (!family) { |
| 132 return SkTypeface::RefDefault(s); | 132 return SkTypeface::MakeDefault(s); |
| 133 } | 133 } |
| 134 | 134 |
| 135 if (family->style() == s) { | 135 if (family->style() == s) { |
| 136 family->ref(); | 136 return sk_ref_sp(family); |
| 137 return const_cast<SkTypeface*>(family); | |
| 138 } | 137 } |
| 139 | 138 |
| 140 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); | 139 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 141 return fm->matchFaceStyle(family, SkFontStyle::FromOldStyle(s)); | 140 return sk_sp<SkTypeface>(fm->matchFaceStyle(family, SkFontStyle::FromOldStyl e(s))); |
| 142 } | 141 } |
| 143 | 142 |
| 144 SkTypeface* SkTypeface::CreateFromStream(SkStreamAsset* stream, int index) { | 143 sk_sp<SkTypeface> SkTypeface::MakeFromStream(SkStreamAsset* stream, int index) { |
| 145 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); | 144 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 146 return fm->createFromStream(stream, index); | 145 return sk_sp<SkTypeface>(fm->createFromStream(stream, index)); |
| 147 } | 146 } |
| 148 | 147 |
| 149 SkTypeface* SkTypeface::CreateFromFontData(SkFontData* data) { | 148 sk_sp<SkTypeface> SkTypeface::MakeFromFontData(SkFontData* data) { |
| 150 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); | 149 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 151 return fm->createFromFontData(data); | 150 return sk_sp<SkTypeface>(fm->createFromFontData(data)); |
| 152 } | 151 } |
| 153 | 152 |
| 154 SkTypeface* SkTypeface::CreateFromFile(const char path[], int index) { | 153 sk_sp<SkTypeface> SkTypeface::MakeFromFile(const char path[], int index) { |
| 155 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); | 154 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 156 return fm->createFromFile(path, index); | 155 return sk_sp<SkTypeface>(fm->createFromFile(path, index)); |
| 157 } | 156 } |
| 158 | 157 |
| 159 /////////////////////////////////////////////////////////////////////////////// | 158 /////////////////////////////////////////////////////////////////////////////// |
| 160 | 159 |
| 161 void SkTypeface::serialize(SkWStream* wstream) const { | 160 void SkTypeface::serialize(SkWStream* wstream) const { |
| 162 if (gSerializeTypefaceDelegate) { | 161 if (gSerializeTypefaceDelegate) { |
| 163 (*gSerializeTypefaceDelegate)(this, wstream); | 162 (*gSerializeTypefaceDelegate)(this, wstream); |
| 164 return; | 163 return; |
| 165 } | 164 } |
| 166 bool isLocal = false; | 165 bool isLocal = false; |
| 167 SkFontDescriptor desc(this->style()); | 166 SkFontDescriptor desc(this->style()); |
| 168 this->onGetFontDescriptor(&desc, &isLocal); | 167 this->onGetFontDescriptor(&desc, &isLocal); |
| 169 | 168 |
| 170 // Embed font data if it's a local font. | 169 // Embed font data if it's a local font. |
| 171 if (isLocal && !desc.hasFontData()) { | 170 if (isLocal && !desc.hasFontData()) { |
| 172 desc.setFontData(this->onCreateFontData()); | 171 desc.setFontData(this->onCreateFontData()); |
| 173 } | 172 } |
| 174 desc.serialize(wstream); | 173 desc.serialize(wstream); |
| 175 } | 174 } |
| 176 | 175 |
| 177 SkTypeface* SkTypeface::Deserialize(SkStream* stream) { | 176 sk_sp<SkTypeface> SkTypeface::MakeDeserialize(SkStream* stream) { |
| 178 if (gDeserializeTypefaceDelegate) { | 177 if (gDeserializeTypefaceDelegate) { |
| 179 return (*gDeserializeTypefaceDelegate)(stream); | 178 return (*gDeserializeTypefaceDelegate)(stream); |
| 180 } | 179 } |
| 181 | 180 |
| 182 SkFontDescriptor desc; | 181 SkFontDescriptor desc; |
| 183 if (!SkFontDescriptor::Deserialize(stream, &desc)) { | 182 if (!SkFontDescriptor::Deserialize(stream, &desc)) { |
| 184 return nullptr; | 183 return nullptr; |
| 185 } | 184 } |
| 186 | 185 |
| 187 SkFontData* data = desc.detachFontData(); | 186 SkFontData* data = desc.detachFontData(); |
| 188 if (data) { | 187 if (data) { |
| 189 SkTypeface* typeface = SkTypeface::CreateFromFontData(data); | 188 auto typeface = SkTypeface::MakeFromFontData(data); |
|
f(malita)
2016/05/02 13:46:35
Nit: avoid auto for sk_sp?
bungeman-skia
2016/05/02 20:24:54
Done.
| |
| 190 if (typeface) { | 189 if (typeface) { |
| 191 return typeface; | 190 return typeface; |
| 192 } | 191 } |
| 193 } | 192 } |
| 194 return SkTypeface::CreateFromName(desc.getFamilyName(), desc.getStyle()); | 193 return SkTypeface::MakeFromName(desc.getFamilyName(), desc.getStyle()); |
| 195 } | 194 } |
| 196 | 195 |
| 197 /////////////////////////////////////////////////////////////////////////////// | 196 /////////////////////////////////////////////////////////////////////////////// |
| 198 | 197 |
| 199 int SkTypeface::countTables() const { | 198 int SkTypeface::countTables() const { |
| 200 return this->onGetTableTags(nullptr); | 199 return this->onGetTableTags(nullptr); |
| 201 } | 200 } |
| 202 | 201 |
| 203 int SkTypeface::getTableTags(SkFontTableTag tags[]) const { | 202 int SkTypeface::getTableTags(SkFontTableTag tags[]) const { |
| 204 return this->onGetTableTags(tags); | 203 return this->onGetTableTags(tags); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 327 }); | 326 }); |
| 328 } | 327 } |
| 329 | 328 |
| 330 bool SkTypeface::onComputeBounds(SkRect* bounds) const { | 329 bool SkTypeface::onComputeBounds(SkRect* bounds) const { |
| 331 // we use a big size to ensure lots of significant bits from the scalerconte xt. | 330 // we use a big size to ensure lots of significant bits from the scalerconte xt. |
| 332 // then we scale back down to return our final answer (at 1-pt) | 331 // then we scale back down to return our final answer (at 1-pt) |
| 333 const SkScalar textSize = 2048; | 332 const SkScalar textSize = 2048; |
| 334 const SkScalar invTextSize = 1 / textSize; | 333 const SkScalar invTextSize = 1 / textSize; |
| 335 | 334 |
| 336 SkPaint paint; | 335 SkPaint paint; |
| 337 paint.setTypeface(const_cast<SkTypeface*>(this)); | 336 paint.setTypeface(sk_ref_sp(const_cast<SkTypeface*>(this))); |
| 338 paint.setTextSize(textSize); | 337 paint.setTextSize(textSize); |
| 339 paint.setLinearText(true); | 338 paint.setLinearText(true); |
| 340 | 339 |
| 341 SkScalerContext::Rec rec; | 340 SkScalerContext::Rec rec; |
| 342 SkScalerContext::MakeRec(paint, nullptr, nullptr, &rec); | 341 SkScalerContext::MakeRec(paint, nullptr, nullptr, &rec); |
| 343 | 342 |
| 344 SkAutoDescriptor ad(sizeof(rec) + SkDescriptor::ComputeOverhead(1)); | 343 SkAutoDescriptor ad(sizeof(rec) + SkDescriptor::ComputeOverhead(1)); |
| 345 SkDescriptor* desc = ad.getDesc(); | 344 SkDescriptor* desc = ad.getDesc(); |
| 346 desc->init(); | 345 desc->init(); |
| 347 desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec); | 346 desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec); |
| 348 | 347 |
| 349 SkScalerContextEffects noeffects; | 348 SkScalerContextEffects noeffects; |
| 350 SkAutoTDelete<SkScalerContext> ctx(this->createScalerContext(noeffects, desc , true)); | 349 SkAutoTDelete<SkScalerContext> ctx(this->createScalerContext(noeffects, desc , true)); |
| 351 if (ctx.get()) { | 350 if (ctx.get()) { |
| 352 SkPaint::FontMetrics fm; | 351 SkPaint::FontMetrics fm; |
| 353 ctx->getFontMetrics(&fm); | 352 ctx->getFontMetrics(&fm); |
| 354 bounds->set(fm.fXMin * invTextSize, fm.fTop * invTextSize, | 353 bounds->set(fm.fXMin * invTextSize, fm.fTop * invTextSize, |
| 355 fm.fXMax * invTextSize, fm.fBottom * invTextSize); | 354 fm.fXMax * invTextSize, fm.fBottom * invTextSize); |
| 356 return true; | 355 return true; |
| 357 } | 356 } |
| 358 return false; | 357 return false; |
| 359 } | 358 } |
| OLD | NEW |