blob: b86cfa7cafe223978300a00a1213e77195101a57 [file] [log] [blame]
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 The Android Open Source Project
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00006 */
7
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +00008#include "SkAdvancedTypefaceMetrics.h"
vandebo0f9bad02014-06-19 11:05:39 -07009#include "SkEndian.h"
reed@google.com5526ede2013-03-25 13:03:37 +000010#include "SkFontDescriptor.h"
bungemanf91c47d2014-09-10 15:49:48 -070011#include "SkFontMgr.h"
mtklein78358bf2014-06-02 08:44:27 -070012#include "SkLazyPtr.h"
vandebo0f9bad02014-06-19 11:05:39 -070013#include "SkOTTable_OS_2.h"
reed@google.com5526ede2013-03-25 13:03:37 +000014#include "SkStream.h"
15#include "SkTypeface.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016
bungemana4c4a2d2014-10-20 13:33:19 -070017SkTypeface::SkTypeface(const SkFontStyle& style, SkFontID fontID, bool isFixedPitch)
18 : fUniqueID(fontID), fStyle(style), fIsFixedPitch(isFixedPitch) { }
reed@google.com2f3dc9d2011-05-02 17:33:45 +000019
bungemana4c4a2d2014-10-20 13:33:19 -070020SkTypeface::~SkTypeface() { }
reed@google.com2f3dc9d2011-05-02 17:33:45 +000021
22///////////////////////////////////////////////////////////////////////////////
23
bungeman@google.com5c1d88d2013-08-21 17:20:57 +000024class SkEmptyTypeface : public SkTypeface {
25public:
commit-bot@chromium.orgc4df6552014-04-07 19:34:16 +000026 static SkEmptyTypeface* Create() {
27 return SkNEW(SkEmptyTypeface);
28 }
bungeman@google.com5c1d88d2013-08-21 17:20:57 +000029protected:
bungemana4c4a2d2014-10-20 13:33:19 -070030 SkEmptyTypeface() : SkTypeface(SkFontStyle(), 0, true) { }
commit-bot@chromium.orgc4df6552014-04-07 19:34:16 +000031
mtklein72c9faa2015-01-09 10:06:39 -080032 SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { return NULL; }
33 SkScalerContext* onCreateScalerContext(const SkDescriptor*) const SK_OVERRIDE {
bungeman@google.com5c1d88d2013-08-21 17:20:57 +000034 return NULL;
35 }
mtklein72c9faa2015-01-09 10:06:39 -080036 void onFilterRec(SkScalerContextRec*) const SK_OVERRIDE { }
bungeman@google.com5c1d88d2013-08-21 17:20:57 +000037 virtual SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics(
38 SkAdvancedTypefaceMetrics::PerGlyphInfo,
39 const uint32_t*, uint32_t) const SK_OVERRIDE { return NULL; }
mtklein72c9faa2015-01-09 10:06:39 -080040 void onGetFontDescriptor(SkFontDescriptor*, bool*) const SK_OVERRIDE { }
bungeman@google.com3c996f82013-10-24 21:39:35 +000041 virtual int onCharsToGlyphs(const void* chars, Encoding encoding,
42 uint16_t glyphs[], int glyphCount) const SK_OVERRIDE {
43 if (glyphs && glyphCount > 0) {
44 sk_bzero(glyphs, glyphCount * sizeof(glyphs[0]));
45 }
46 return 0;
47 }
mtklein72c9faa2015-01-09 10:06:39 -080048 int onCountGlyphs() const SK_OVERRIDE { return 0; };
49 int onGetUPEM() const SK_OVERRIDE { return 0; };
bungeman@google.com5c1d88d2013-08-21 17:20:57 +000050 class EmptyLocalizedStrings : public SkTypeface::LocalizedStrings {
51 public:
mtklein72c9faa2015-01-09 10:06:39 -080052 bool next(SkTypeface::LocalizedString*) SK_OVERRIDE { return false; }
bungeman@google.com5c1d88d2013-08-21 17:20:57 +000053 };
mtklein72c9faa2015-01-09 10:06:39 -080054 void onGetFamilyName(SkString* familyName) const SK_OVERRIDE {
bungemanb374d6a2014-09-17 07:48:59 -070055 familyName->reset();
56 }
mtklein72c9faa2015-01-09 10:06:39 -080057 SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const SK_OVERRIDE {
bungeman@google.com5c1d88d2013-08-21 17:20:57 +000058 return SkNEW(EmptyLocalizedStrings);
59 };
mtklein72c9faa2015-01-09 10:06:39 -080060 int onGetTableTags(SkFontTableTag tags[]) const SK_OVERRIDE { return 0; }
61 size_t onGetTableData(SkFontTableTag, size_t, size_t, void*) const SK_OVERRIDE {
bungeman@google.com5c1d88d2013-08-21 17:20:57 +000062 return 0;
63 }
bungeman@google.com5c1d88d2013-08-21 17:20:57 +000064};
65
mtklein148ec592014-10-13 13:17:56 -070066namespace {
67
bungemand6aeb6d2014-07-25 11:52:47 -070068SK_DECLARE_STATIC_MUTEX(gCreateDefaultMutex);
mtklein148ec592014-10-13 13:17:56 -070069
70// As a template arguments, these must have external linkage.
71SkTypeface* sk_create_default_typeface(int style) {
mtklein78358bf2014-06-02 08:44:27 -070072 // If backed by fontconfig, it's not safe to call SkFontHost::CreateTypeface concurrently.
73 // To be safe, we serialize here with a mutex so only one call to
74 // CreateTypeface is happening at any given time.
75 // TODO(bungeman, mtklein): This is sad. Make our fontconfig code safe?
bungemand6aeb6d2014-07-25 11:52:47 -070076 SkAutoMutexAcquire lock(&gCreateDefaultMutex);
commit-bot@chromium.orgf6351a02014-05-30 21:00:52 +000077
bungemanf91c47d2014-09-10 15:49:48 -070078 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
79 SkTypeface* t = fm->legacyCreateTypeface(NULL, style);;
mtklein78358bf2014-06-02 08:44:27 -070080 return t ? t : SkEmptyTypeface::Create();
81}
82
mtklein148ec592014-10-13 13:17:56 -070083void sk_unref_typeface(SkTypeface* ptr) { SkSafeUnref(ptr); }
84
85} // namespace
86
87SK_DECLARE_STATIC_LAZY_PTR_ARRAY(SkTypeface, defaults, 4,
88 sk_create_default_typeface, sk_unref_typeface);
bungeman@google.com5c1d88d2013-08-21 17:20:57 +000089
bungeman@google.com18b75e52014-01-09 17:13:32 +000090SkTypeface* SkTypeface::GetDefaultTypeface(Style style) {
mtklein78358bf2014-06-02 08:44:27 -070091 SkASSERT((int)style < 4);
92 return defaults[style];
reed@google.com538f7842011-12-16 17:56:23 +000093}
94
djsollen@google.com4fa748d2013-06-05 14:20:25 +000095SkTypeface* SkTypeface::RefDefault(Style style) {
96 return SkRef(GetDefaultTypeface(style));
reed@google.comfed86bd2013-03-14 15:04:57 +000097}
98
reed@android.comef772dd2009-03-03 21:20:49 +000099uint32_t SkTypeface::UniqueID(const SkTypeface* face) {
reed@google.com538f7842011-12-16 17:56:23 +0000100 if (NULL == face) {
bungeman@google.comcb1bbb32012-10-12 18:48:35 +0000101 face = GetDefaultTypeface();
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000102 }
reed@google.com538f7842011-12-16 17:56:23 +0000103 return face->uniqueID();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104}
105
reed@android.comef772dd2009-03-03 21:20:49 +0000106bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000107 return SkTypeface::UniqueID(facea) == SkTypeface::UniqueID(faceb);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108}
109
110///////////////////////////////////////////////////////////////////////////////
111
reed@android.com069b8272009-03-04 15:31:48 +0000112SkTypeface* SkTypeface::CreateFromName(const char name[], Style style) {
djsollen@google.com4fa748d2013-06-05 14:20:25 +0000113 if (NULL == name) {
114 return RefDefault(style);
115 }
bungemanf91c47d2014-09-10 15:49:48 -0700116 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
117 return fm->legacyCreateTypeface(name, style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118}
119
reed@android.comef772dd2009-03-03 21:20:49 +0000120SkTypeface* SkTypeface::CreateFromTypeface(const SkTypeface* family, Style s) {
bungemanb14e4a02014-09-18 13:57:20 -0700121 if (!family) {
122 return SkTypeface::RefDefault(s);
123 }
124
125 if (family->style() == s) {
reed@google.com49690442013-03-04 14:39:47 +0000126 family->ref();
127 return const_cast<SkTypeface*>(family);
128 }
bungemanb14e4a02014-09-18 13:57:20 -0700129
bungemanf91c47d2014-09-10 15:49:48 -0700130 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
131 bool bold = s & SkTypeface::kBold;
132 bool italic = s & SkTypeface::kItalic;
133 SkFontStyle newStyle = SkFontStyle(bold ? SkFontStyle::kBold_Weight
134 : SkFontStyle::kNormal_Weight,
135 SkFontStyle::kNormal_Width,
136 italic ? SkFontStyle::kItalic_Slant
bungemanb14e4a02014-09-18 13:57:20 -0700137 : SkFontStyle::kUpright_Slant);
bungemanf91c47d2014-09-10 15:49:48 -0700138 return fm->matchFaceStyle(family, newStyle);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139}
140
bungemand71b7572014-09-18 10:55:32 -0700141SkTypeface* SkTypeface::CreateFromStream(SkStream* stream, int index) {
bungemanf91c47d2014-09-10 15:49:48 -0700142 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
bungemand71b7572014-09-18 10:55:32 -0700143 return fm->createFromStream(stream, index);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144}
145
bungemand71b7572014-09-18 10:55:32 -0700146SkTypeface* SkTypeface::CreateFromFile(const char path[], int index) {
bungemanf91c47d2014-09-10 15:49:48 -0700147 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
bungemand71b7572014-09-18 10:55:32 -0700148 return fm->createFromFile(path, index);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149}
150
151///////////////////////////////////////////////////////////////////////////////
152
reed@google.com5526ede2013-03-25 13:03:37 +0000153void SkTypeface::serialize(SkWStream* wstream) const {
154 bool isLocal = false;
155 SkFontDescriptor desc(this->style());
156 this->onGetFontDescriptor(&desc, &isLocal);
157
mtklein1a4900e2014-12-11 11:06:00 -0800158 // Embed font data if it's a local font.
scroggoa1193e42015-01-21 12:09:53 -0800159 if (isLocal && !desc.hasFontData()) {
bungemand71b7572014-09-18 10:55:32 -0700160 int ttcIndex;
161 desc.setFontData(this->onOpenStream(&ttcIndex));
162 desc.setFontIndex(ttcIndex);
reed@google.com5526ede2013-03-25 13:03:37 +0000163 }
mtklein1a4900e2014-12-11 11:06:00 -0800164 desc.serialize(wstream);
165}
bungemand71b7572014-09-18 10:55:32 -0700166
mtklein1a4900e2014-12-11 11:06:00 -0800167void SkTypeface::serializeForcingEmbedding(SkWStream* wstream) const {
168 bool ignoredIsLocal;
169 SkFontDescriptor desc(this->style());
170 this->onGetFontDescriptor(&desc, &ignoredIsLocal);
171
172 // Always embed font data.
scroggoa1193e42015-01-21 12:09:53 -0800173 if (!desc.hasFontData()) {
mtklein1a4900e2014-12-11 11:06:00 -0800174 int ttcIndex;
175 desc.setFontData(this->onOpenStream(&ttcIndex));
176 desc.setFontIndex(ttcIndex);
177 }
bungemand71b7572014-09-18 10:55:32 -0700178 desc.serialize(wstream);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179}
180
181SkTypeface* SkTypeface::Deserialize(SkStream* stream) {
reed@google.com5526ede2013-03-25 13:03:37 +0000182 SkFontDescriptor desc(stream);
scroggoa1193e42015-01-21 12:09:53 -0800183 SkStream* data = desc.transferFontData();
bungemand71b7572014-09-18 10:55:32 -0700184 if (data) {
185 SkTypeface* typeface = SkTypeface::CreateFromStream(data, desc.getFontIndex());
186 if (typeface) {
187 return typeface;
reed@google.com5526ede2013-03-25 13:03:37 +0000188 }
reed@google.com5526ede2013-03-25 13:03:37 +0000189 }
reed@google.com5526ede2013-03-25 13:03:37 +0000190 return SkTypeface::CreateFromName(desc.getFamilyName(), desc.getStyle());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000191}
reed@google.comf11508d2012-04-17 18:01:31 +0000192
193///////////////////////////////////////////////////////////////////////////////
194
195int SkTypeface::countTables() const {
reed@google.com6c66d2f2013-03-21 20:34:27 +0000196 return this->onGetTableTags(NULL);
reed@google.comf11508d2012-04-17 18:01:31 +0000197}
198
199int SkTypeface::getTableTags(SkFontTableTag tags[]) const {
reed@google.com6c66d2f2013-03-21 20:34:27 +0000200 return this->onGetTableTags(tags);
reed@google.comf11508d2012-04-17 18:01:31 +0000201}
202
203size_t SkTypeface::getTableSize(SkFontTableTag tag) const {
reed@google.com6c66d2f2013-03-21 20:34:27 +0000204 return this->onGetTableData(tag, 0, ~0U, NULL);
reed@google.comf11508d2012-04-17 18:01:31 +0000205}
206
207size_t SkTypeface::getTableData(SkFontTableTag tag, size_t offset, size_t length,
208 void* data) const {
reed@google.com6c66d2f2013-03-21 20:34:27 +0000209 return this->onGetTableData(tag, offset, length, data);
reed@google.comf11508d2012-04-17 18:01:31 +0000210}
211
reed@google.comfed86bd2013-03-14 15:04:57 +0000212SkStream* SkTypeface::openStream(int* ttcIndex) const {
reed@google.com2cdc6712013-03-21 18:22:00 +0000213 int ttcIndexStorage;
214 if (NULL == ttcIndex) {
215 // So our subclasses don't need to check for null param
216 ttcIndex = &ttcIndexStorage;
reed@google.comfed86bd2013-03-14 15:04:57 +0000217 }
reed@google.com2cdc6712013-03-21 18:22:00 +0000218 return this->onOpenStream(ttcIndex);
reed@google.comfed86bd2013-03-14 15:04:57 +0000219}
220
reed@google.combcb42ae2013-07-02 13:56:39 +0000221int SkTypeface::charsToGlyphs(const void* chars, Encoding encoding,
222 uint16_t glyphs[], int glyphCount) const {
223 if (glyphCount <= 0) {
224 return 0;
225 }
226 if (NULL == chars || (unsigned)encoding > kUTF32_Encoding) {
227 if (glyphs) {
228 sk_bzero(glyphs, glyphCount * sizeof(glyphs[0]));
229 }
230 return 0;
231 }
232 return this->onCharsToGlyphs(chars, encoding, glyphs, glyphCount);
233}
234
235int SkTypeface::countGlyphs() const {
236 return this->onCountGlyphs();
237}
238
reed@google.com4b2af9c2012-07-31 17:24:44 +0000239int SkTypeface::getUnitsPerEm() const {
reed@google.com38c37dd2013-03-21 15:36:26 +0000240 // should we try to cache this in the base-class?
241 return this->onGetUPEM();
reed@google.com4b2af9c2012-07-31 17:24:44 +0000242}
mike@reedtribe.orgdc09f072013-03-03 01:15:48 +0000243
reed@google.com35fe7372013-10-30 15:07:03 +0000244bool SkTypeface::getKerningPairAdjustments(const uint16_t glyphs[], int count,
245 int32_t adjustments[]) const {
246 SkASSERT(count >= 0);
247 // check for the only legal way to pass a NULL.. everything is 0
248 // in which case they just want to know if this face can possibly support
249 // kerning (true) or never (false).
250 if (NULL == glyphs || NULL == adjustments) {
251 SkASSERT(NULL == glyphs);
252 SkASSERT(0 == count);
253 SkASSERT(NULL == adjustments);
254 }
255 return this->onGetKerningPairAdjustments(glyphs, count, adjustments);
256}
257
bungeman@google.com839702b2013-08-07 17:09:22 +0000258SkTypeface::LocalizedStrings* SkTypeface::createFamilyNameIterator() const {
259 return this->onCreateFamilyNameIterator();
bungeman@google.coma9802692013-08-07 02:45:25 +0000260}
261
reed@google.com08df48d2013-07-29 18:54:08 +0000262void SkTypeface::getFamilyName(SkString* name) const {
bungemanb374d6a2014-09-17 07:48:59 -0700263 SkASSERT(name);
264 this->onGetFamilyName(name);
reed@google.com08df48d2013-07-29 18:54:08 +0000265}
266
reed@google.com5526ede2013-03-25 13:03:37 +0000267SkAdvancedTypefaceMetrics* SkTypeface::getAdvancedTypefaceMetrics(
268 SkAdvancedTypefaceMetrics::PerGlyphInfo info,
269 const uint32_t* glyphIDs,
270 uint32_t glyphIDsCount) const {
vandebo0f9bad02014-06-19 11:05:39 -0700271 SkAdvancedTypefaceMetrics* result =
272 this->onGetAdvancedTypefaceMetrics(info, glyphIDs, glyphIDsCount);
273 if (result && result->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
274 struct SkOTTableOS2 os2table;
275 if (this->getTableData(SkTEndian_SwapBE32(SkOTTableOS2::TAG), 0,
276 sizeof(os2table), &os2table) > 0) {
277 if (os2table.version.v2.fsType.field.Bitmap ||
278 (os2table.version.v2.fsType.field.Restricted &&
279 !(os2table.version.v2.fsType.field.PreviewPrint ||
280 os2table.version.v2.fsType.field.Editable))) {
281 result->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>(
282 result->fFlags,
283 SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag);
284 }
285 if (os2table.version.v2.fsType.field.NoSubsetting) {
286 result->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>(
287 result->fFlags,
288 SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag);
289 }
290 }
291 }
292 return result;
reed@google.com5526ede2013-03-25 13:03:37 +0000293}
mike@reedtribe.orgdc09f072013-03-03 01:15:48 +0000294
reed@google.com35fe7372013-10-30 15:07:03 +0000295bool SkTypeface::onGetKerningPairAdjustments(const uint16_t glyphs[], int count,
296 int32_t adjustments[]) const {
297 return false;
298}
reeda0c814c2014-10-22 13:20:58 -0700299
300///////////////////////////////////////////////////////////////////////////////
301
302#include "SkDescriptor.h"
303#include "SkPaint.h"
304
305struct SkTypeface::BoundsComputer {
306 const SkTypeface& fTypeface;
307
308 BoundsComputer(const SkTypeface& tf) : fTypeface(tf) {}
309
310 SkRect* operator()() const {
311 SkRect* rect = SkNEW(SkRect);
312 if (!fTypeface.onComputeBounds(rect)) {
313 rect->setEmpty();
314 }
315 return rect;
316 }
317};
318
319SkRect SkTypeface::getBounds() const {
320 return *fLazyBounds.get(BoundsComputer(*this));
321}
322
323bool SkTypeface::onComputeBounds(SkRect* bounds) const {
324 // we use a big size to ensure lots of significant bits from the scalercontext.
325 // then we scale back down to return our final answer (at 1-pt)
326 const SkScalar textSize = 2048;
327 const SkScalar invTextSize = 1 / textSize;
328
329 SkPaint paint;
330 paint.setTypeface(const_cast<SkTypeface*>(this));
331 paint.setTextSize(textSize);
332 paint.setLinearText(true);
333
334 SkScalerContext::Rec rec;
335 SkScalerContext::MakeRec(paint, NULL, NULL, &rec);
336
337 SkAutoDescriptor ad(sizeof(rec) + SkDescriptor::ComputeOverhead(1));
338 SkDescriptor* desc = ad.getDesc();
339 desc->init();
340 desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec);
341
342 SkAutoTDelete<SkScalerContext> ctx(this->createScalerContext(desc, true));
343 if (ctx.get()) {
344 SkPaint::FontMetrics fm;
345 ctx->getFontMetrics(&fm);
346 bounds->set(fm.fXMin * invTextSize, fm.fTop * invTextSize,
347 fm.fXMax * invTextSize, fm.fBottom * invTextSize);
348 return true;
349 }
350 return false;
351}
352