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 sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style )
= nullptr; | 30 SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style ) = null
ptr; |
31 void (*gSerializeTypefaceDelegate)(const SkTypeface*, SkWStream* ) = SK_TYPEFACE
_DELEGATE; | 31 void (*gSerializeTypefaceDelegate)(const SkTypeface*, SkWStream* ) = SK_TYPEFACE
_DELEGATE; |
32 sk_sp<SkTypeface> (*gDeserializeTypefaceDelegate)(SkStream* ) = nullptr; | 32 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 sk_sp<SkTypeface> SkTypeface::MakeDefault(Style style) { | 101 SkTypeface* SkTypeface::RefDefault(Style style) { |
102 return sk_ref_sp(GetDefaultTypeface(style)); | 102 return SkRef(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 sk_sp<SkTypeface> SkTypeface::MakeFromName(const char name[], Style style) { | 118 SkTypeface* SkTypeface::CreateFromName(const char name[], Style style) { |
119 if (gCreateTypefaceDelegate) { | 119 if (gCreateTypefaceDelegate) { |
120 sk_sp<SkTypeface> result = (*gCreateTypefaceDelegate)(name, style); | 120 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 MakeDefault(style); | 126 return RefDefault(style); |
127 } | 127 } |
128 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); | 128 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
129 return sk_sp<SkTypeface>(fm->legacyCreateTypeface(name, SkFontStyle::FromOld
Style(style))); | 129 return fm->legacyCreateTypeface(name, SkFontStyle::FromOldStyle(style)); |
130 } | 130 } |
131 | 131 |
132 sk_sp<SkTypeface> SkTypeface::MakeFromTypeface(SkTypeface* family, Style s) { | 132 SkTypeface* SkTypeface::CreateFromTypeface(const SkTypeface* family, Style s) { |
133 if (!family) { | 133 if (!family) { |
134 return SkTypeface::MakeDefault(s); | 134 return SkTypeface::RefDefault(s); |
135 } | 135 } |
136 | 136 |
137 if (family->style() == s) { | 137 if (family->style() == s) { |
138 return sk_ref_sp(family); | 138 family->ref(); |
| 139 return const_cast<SkTypeface*>(family); |
139 } | 140 } |
140 | 141 |
141 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); | 142 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
142 return sk_sp<SkTypeface>(fm->matchFaceStyle(family, SkFontStyle::FromOldStyl
e(s))); | 143 return fm->matchFaceStyle(family, SkFontStyle::FromOldStyle(s)); |
143 } | 144 } |
144 | 145 |
145 sk_sp<SkTypeface> SkTypeface::MakeFromStream(SkStreamAsset* stream, int index) { | 146 SkTypeface* SkTypeface::CreateFromStream(SkStreamAsset* stream, int index) { |
146 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); | 147 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
147 return sk_sp<SkTypeface>(fm->createFromStream(stream, index)); | 148 return fm->createFromStream(stream, index); |
148 } | 149 } |
149 | 150 |
150 sk_sp<SkTypeface> SkTypeface::MakeFromFontData(SkFontData* data) { | 151 SkTypeface* SkTypeface::CreateFromFontData(SkFontData* data) { |
151 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); | 152 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
152 return sk_sp<SkTypeface>(fm->createFromFontData(data)); | 153 return fm->createFromFontData(data); |
153 } | 154 } |
154 | 155 |
155 sk_sp<SkTypeface> SkTypeface::MakeFromFile(const char path[], int index) { | 156 SkTypeface* SkTypeface::CreateFromFile(const char path[], int index) { |
156 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); | 157 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
157 return sk_sp<SkTypeface>(fm->createFromFile(path, index)); | 158 return fm->createFromFile(path, index); |
158 } | 159 } |
159 | 160 |
160 /////////////////////////////////////////////////////////////////////////////// | 161 /////////////////////////////////////////////////////////////////////////////// |
161 | 162 |
162 void SkTypeface::serialize(SkWStream* wstream) const { | 163 void SkTypeface::serialize(SkWStream* wstream) const { |
163 if (gSerializeTypefaceDelegate) { | 164 if (gSerializeTypefaceDelegate) { |
164 (*gSerializeTypefaceDelegate)(this, wstream); | 165 (*gSerializeTypefaceDelegate)(this, wstream); |
165 return; | 166 return; |
166 } | 167 } |
167 bool isLocal = false; | 168 bool isLocal = false; |
168 SkFontDescriptor desc(this->style()); | 169 SkFontDescriptor desc(this->style()); |
169 this->onGetFontDescriptor(&desc, &isLocal); | 170 this->onGetFontDescriptor(&desc, &isLocal); |
170 | 171 |
171 // Embed font data if it's a local font. | 172 // Embed font data if it's a local font. |
172 if (isLocal && !desc.hasFontData()) { | 173 if (isLocal && !desc.hasFontData()) { |
173 desc.setFontData(this->onCreateFontData()); | 174 desc.setFontData(this->onCreateFontData()); |
174 } | 175 } |
175 desc.serialize(wstream); | 176 desc.serialize(wstream); |
176 } | 177 } |
177 | 178 |
178 sk_sp<SkTypeface> SkTypeface::MakeDeserialize(SkStream* stream) { | 179 SkTypeface* SkTypeface::Deserialize(SkStream* stream) { |
179 if (gDeserializeTypefaceDelegate) { | 180 if (gDeserializeTypefaceDelegate) { |
180 return (*gDeserializeTypefaceDelegate)(stream); | 181 return (*gDeserializeTypefaceDelegate)(stream); |
181 } | 182 } |
182 | 183 |
183 SkFontDescriptor desc; | 184 SkFontDescriptor desc; |
184 if (!SkFontDescriptor::Deserialize(stream, &desc)) { | 185 if (!SkFontDescriptor::Deserialize(stream, &desc)) { |
185 return nullptr; | 186 return nullptr; |
186 } | 187 } |
187 | 188 |
188 SkFontData* data = desc.detachFontData(); | 189 SkFontData* data = desc.detachFontData(); |
189 if (data) { | 190 if (data) { |
190 sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFontData(data)); | 191 SkTypeface* typeface = SkTypeface::CreateFromFontData(data); |
191 if (typeface) { | 192 if (typeface) { |
192 return typeface; | 193 return typeface; |
193 } | 194 } |
194 } | 195 } |
195 return SkTypeface::MakeFromName(desc.getFamilyName(), desc.getStyle()); | 196 return SkTypeface::CreateFromName(desc.getFamilyName(), desc.getStyle()); |
196 } | 197 } |
197 | 198 |
198 /////////////////////////////////////////////////////////////////////////////// | 199 /////////////////////////////////////////////////////////////////////////////// |
199 | 200 |
200 int SkTypeface::countTables() const { | 201 int SkTypeface::countTables() const { |
201 return this->onGetTableTags(nullptr); | 202 return this->onGetTableTags(nullptr); |
202 } | 203 } |
203 | 204 |
204 int SkTypeface::getTableTags(SkFontTableTag tags[]) const { | 205 int SkTypeface::getTableTags(SkFontTableTag tags[]) const { |
205 return this->onGetTableTags(tags); | 206 return this->onGetTableTags(tags); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 return fBounds; | 328 return fBounds; |
328 } | 329 } |
329 | 330 |
330 bool SkTypeface::onComputeBounds(SkRect* bounds) const { | 331 bool SkTypeface::onComputeBounds(SkRect* bounds) const { |
331 // we use a big size to ensure lots of significant bits from the scalerconte
xt. | 332 // 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) | 333 // then we scale back down to return our final answer (at 1-pt) |
333 const SkScalar textSize = 2048; | 334 const SkScalar textSize = 2048; |
334 const SkScalar invTextSize = 1 / textSize; | 335 const SkScalar invTextSize = 1 / textSize; |
335 | 336 |
336 SkPaint paint; | 337 SkPaint paint; |
337 paint.setTypeface(sk_ref_sp(const_cast<SkTypeface*>(this))); | 338 paint.setTypeface(const_cast<SkTypeface*>(this)); |
338 paint.setTextSize(textSize); | 339 paint.setTextSize(textSize); |
339 paint.setLinearText(true); | 340 paint.setLinearText(true); |
340 | 341 |
341 SkScalerContext::Rec rec; | 342 SkScalerContext::Rec rec; |
342 SkScalerContext::MakeRec(paint, nullptr, nullptr, &rec); | 343 SkScalerContext::MakeRec(paint, nullptr, nullptr, &rec); |
343 | 344 |
344 SkAutoDescriptor ad(sizeof(rec) + SkDescriptor::ComputeOverhead(1)); | 345 SkAutoDescriptor ad(sizeof(rec) + SkDescriptor::ComputeOverhead(1)); |
345 SkDescriptor* desc = ad.getDesc(); | 346 SkDescriptor* desc = ad.getDesc(); |
346 desc->init(); | 347 desc->init(); |
347 desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec); | 348 desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec); |
348 | 349 |
349 SkScalerContextEffects noeffects; | 350 SkScalerContextEffects noeffects; |
350 SkAutoTDelete<SkScalerContext> ctx(this->createScalerContext(noeffects, desc
, true)); | 351 SkAutoTDelete<SkScalerContext> ctx(this->createScalerContext(noeffects, desc
, true)); |
351 if (ctx.get()) { | 352 if (ctx.get()) { |
352 SkPaint::FontMetrics fm; | 353 SkPaint::FontMetrics fm; |
353 ctx->getFontMetrics(&fm); | 354 ctx->getFontMetrics(&fm); |
354 bounds->set(fm.fXMin * invTextSize, fm.fTop * invTextSize, | 355 bounds->set(fm.fXMin * invTextSize, fm.fTop * invTextSize, |
355 fm.fXMax * invTextSize, fm.fBottom * invTextSize); | 356 fm.fXMax * invTextSize, fm.fBottom * invTextSize); |
356 return true; | 357 return true; |
357 } | 358 } |
358 return false; | 359 return false; |
359 } | 360 } |
OLD | NEW |