| 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 "src/core/SkAdvancedTypefaceMetrics.h" |
| 15 | #include "src/core/SkEndian.h" |
| 16 | #include "src/core/SkFontDescriptor.h" |
| 17 | #include "src/core/SkScalerContext.h" |
| 18 | #include "src/core/SkSurfacePriv.h" |
| 19 | #include "src/core/SkTypefaceCache.h" |
| 20 | #include "src/sfnt/SkOTTable_OS_2.h" |
| 21 | |
| 22 | SkTypeface::SkTypeface(const SkFontStyle& style, bool isFixedPitch) |
| 23 | : fUniqueID(SkTypefaceCache::NewFontID()), fStyle(style), fIsFixedPitch(isFixedPitch) { } |
| 24 | |
| 25 | SkTypeface::~SkTypeface() { } |
| 26 | |
| 27 | #ifdef SK_WHITELIST_SERIALIZED_TYPEFACES |
| 28 | extern void WhitelistSerializeTypeface(const SkTypeface*, SkWStream* ); |
| 29 | #define SK_TYPEFACE_DELEGATE WhitelistSerializeTypeface |
| 30 | #else |
| 31 | #define SK_TYPEFACE_DELEGATE nullptr |
| 32 | #endif |
| 33 | |
| 34 | void (*gSerializeTypefaceDelegate)(const SkTypeface*, SkWStream* ) = SK_TYPEFACE_DELEGATE; |
| 35 | sk_sp<SkTypeface> (*gDeserializeTypefaceDelegate)(SkStream* ) = nullptr; |
| 36 | |
| 37 | /////////////////////////////////////////////////////////////////////////////// |
| 38 | |
| 39 | namespace { |
| 40 | |
| 41 | class SkEmptyTypeface : public SkTypeface { |
| 42 | public: |
| 43 | static sk_sp<SkTypeface> Make() { return sk_sp<SkTypeface>(new SkEmptyTypeface); } |
| 44 | protected: |
| 45 | SkEmptyTypeface() : SkTypeface(SkFontStyle(), true) { } |
| 46 | |
| 47 | std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override { return nullptr; } |
| 48 | sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override { |
| 49 | return sk_ref_sp(this); |
| 50 | } |
| 51 | SkScalerContext* onCreateScalerContext(const SkScalerContextEffects& effects, |
| 52 | const SkDescriptor* desc) const override { |
| 53 | return SkScalerContext::MakeEmptyContext( |
| 54 | sk_ref_sp(const_cast<SkEmptyTypeface*>(this)), effects, desc); |
| 55 | } |
| 56 | void onFilterRec(SkScalerContextRec*) const override { } |
| 57 | std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override { |
| 58 | return nullptr; |
| 59 | } |
| 60 | void onGetFontDescriptor(SkFontDescriptor*, bool*) const override { } |
| 61 | void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const override { |
| 62 | sk_bzero(glyphs, count * sizeof(glyphs[0])); |
| 63 | } |
| 64 | int onCountGlyphs() const override { return 0; } |
| 65 | void getPostScriptGlyphNames(SkString*) const override {} |
| 66 | void getGlyphToUnicodeMap(SkUnichar*) const override {} |
| 67 | int onGetUPEM() const override { return 0; } |
| 68 | class EmptyLocalizedStrings : public SkTypeface::LocalizedStrings { |
| 69 | public: |
| 70 | bool next(SkTypeface::LocalizedString*) override { return false; } |
| 71 | }; |
| 72 | void onGetFamilyName(SkString* familyName) const override { |
| 73 | familyName->reset(); |
| 74 | } |
| 75 | SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override { |
| 76 | return new EmptyLocalizedStrings; |
| 77 | } |
| 78 | int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], |
| 79 | int coordinateCount) const override |
| 80 | { |
| 81 | return 0; |
| 82 | } |
| 83 | int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[], |
| 84 | int parameterCount) const override |
| 85 | { |
| 86 | return 0; |
| 87 | } |
| 88 | int onGetTableTags(SkFontTableTag tags[]) const override { return 0; } |
| 89 | size_t onGetTableData(SkFontTableTag, size_t, size_t, void*) const override { |
| 90 | return 0; |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | } // namespace |
| 95 | |
| 96 | SkFontStyle SkTypeface::FromOldStyle(Style oldStyle) { |
| 97 | return SkFontStyle((oldStyle & SkTypeface::kBold) ? SkFontStyle::kBold_Weight |
| 98 | : SkFontStyle::kNormal_Weight, |
| 99 | SkFontStyle::kNormal_Width, |
| 100 | (oldStyle & SkTypeface::kItalic) ? SkFontStyle::kItalic_Slant |
| 101 | : SkFontStyle::kUpright_Slant); |
| 102 | } |
| 103 | |
| 104 | SkTypeface* SkTypeface::GetDefaultTypeface(Style style) { |
| 105 | static SkOnce once[4]; |
| 106 | static sk_sp<SkTypeface> defaults[4]; |
| 107 | |
| 108 | SkASSERT((int)style < 4); |
| 109 | once[style]([style] { |
| 110 | sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 111 | auto t = fm->legacyMakeTypeface(nullptr, FromOldStyle(style)); |
| 112 | defaults[style] = t ? t : SkEmptyTypeface::Make(); |
| 113 | }); |
| 114 | return defaults[style].get(); |
| 115 | } |
| 116 | |
| 117 | sk_sp<SkTypeface> SkTypeface::MakeDefault() { |
| 118 | return sk_ref_sp(GetDefaultTypeface()); |
| 119 | } |
| 120 | |
| 121 | uint32_t SkTypeface::UniqueID(const SkTypeface* face) { |
| 122 | if (nullptr == face) { |
| 123 | face = GetDefaultTypeface(); |
| 124 | } |
| 125 | return face->uniqueID(); |
| 126 | } |
| 127 | |
| 128 | bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) { |
| 129 | return facea == faceb || SkTypeface::UniqueID(facea) == SkTypeface::UniqueID(faceb); |
| 130 | } |
| 131 | |
| 132 | /////////////////////////////////////////////////////////////////////////////// |
| 133 | |
| 134 | sk_sp<SkTypeface> SkTypeface::MakeFromName(const char name[], |
| 135 | SkFontStyle fontStyle) { |
| 136 | if (nullptr == name && (fontStyle.slant() == SkFontStyle::kItalic_Slant || |
| 137 | fontStyle.slant() == SkFontStyle::kUpright_Slant) && |
| 138 | (fontStyle.weight() == SkFontStyle::kBold_Weight || |
| 139 | fontStyle.weight() == SkFontStyle::kNormal_Weight)) { |
| 140 | return sk_ref_sp(GetDefaultTypeface(static_cast<SkTypeface::Style>( |
| 141 | (fontStyle.slant() == SkFontStyle::kItalic_Slant ? SkTypeface::kItalic : |
| 142 | SkTypeface::kNormal) | |
| 143 | (fontStyle.weight() == SkFontStyle::kBold_Weight ? SkTypeface::kBold : |
| 144 | SkTypeface::kNormal)))); |
| 145 | } |
| 146 | return SkFontMgr::RefDefault()->legacyMakeTypeface(name, fontStyle); |
| 147 | } |
| 148 | |
| 149 | sk_sp<SkTypeface> SkTypeface::MakeFromStream(std::unique_ptr<SkStreamAsset> stream, int index) { |
| 150 | if (!stream) { |
| 151 | return nullptr; |
| 152 | } |
| 153 | return SkFontMgr::RefDefault()->makeFromStream(std::move(stream), index); |
| 154 | } |
| 155 | |
| 156 | sk_sp<SkTypeface> SkTypeface::MakeFromData(sk_sp<SkData> data, int index) { |
| 157 | if (!data) { |
| 158 | return nullptr; |
| 159 | } |
| 160 | return SkFontMgr::RefDefault()->makeFromData(std::move(data), index); |
| 161 | } |
| 162 | |
| 163 | sk_sp<SkTypeface> SkTypeface::MakeFromFontData(std::unique_ptr<SkFontData> data) { |
| 164 | return SkFontMgr::RefDefault()->makeFromFontData(std::move(data)); |
| 165 | } |
| 166 | |
| 167 | sk_sp<SkTypeface> SkTypeface::MakeFromFile(const char path[], int index) { |
| 168 | return SkFontMgr::RefDefault()->makeFromFile(path, index); |
| 169 | } |
| 170 | |
| 171 | sk_sp<SkTypeface> SkTypeface::makeClone(const SkFontArguments& args) const { |
| 172 | return this->onMakeClone(args); |
| 173 | } |
| 174 | |
| 175 | /////////////////////////////////////////////////////////////////////////////// |
| 176 | |
| 177 | void SkTypeface::serialize(SkWStream* wstream, SerializeBehavior behavior) const { |
| 178 | if (gSerializeTypefaceDelegate) { |
| 179 | (*gSerializeTypefaceDelegate)(this, wstream); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | bool isLocalData = false; |
| 184 | SkFontDescriptor desc; |
| 185 | this->onGetFontDescriptor(&desc, &isLocalData); |
| 186 | |
| 187 | bool shouldSerializeData = false; |
| 188 | switch (behavior) { |
| 189 | case SerializeBehavior::kDoIncludeData: shouldSerializeData = true; break; |
| 190 | case SerializeBehavior::kDontIncludeData: shouldSerializeData = false; break; |
| 191 | case SerializeBehavior::kIncludeDataIfLocal: shouldSerializeData = isLocalData; break; |
| 192 | } |
| 193 | |
| 194 | // TODO: why do we check hasFontData() and allow the data to pass through even if the caller |
| 195 | // has said they don't want the fontdata? Does this actually happen (getDescriptor returns |
| 196 | // fontdata as well?) |
| 197 | if (shouldSerializeData && !desc.hasFontData()) { |
| 198 | desc.setFontData(this->onMakeFontData()); |
| 199 | } |
| 200 | desc.serialize(wstream); |
| 201 | } |
| 202 | |
| 203 | sk_sp<SkData> SkTypeface::serialize(SerializeBehavior behavior) const { |
| 204 | SkDynamicMemoryWStream stream; |
| 205 | this->serialize(&stream, behavior); |
| 206 | return stream.detachAsData(); |
| 207 | } |
| 208 | |
| 209 | sk_sp<SkTypeface> SkTypeface::MakeDeserialize(SkStream* stream) { |
| 210 | if (gDeserializeTypefaceDelegate) { |
| 211 | return (*gDeserializeTypefaceDelegate)(stream); |
| 212 | } |
| 213 | |
| 214 | SkFontDescriptor desc; |
| 215 | if (!SkFontDescriptor::Deserialize(stream, &desc)) { |
| 216 | return nullptr; |
| 217 | } |
| 218 | |
| 219 | std::unique_ptr<SkFontData> data = desc.detachFontData(); |
| 220 | if (data) { |
| 221 | sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFontData(std::move(data))); |
| 222 | if (typeface) { |
| 223 | return typeface; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return SkTypeface::MakeFromName(desc.getFamilyName(), desc.getStyle()); |
| 228 | } |
| 229 | |
| 230 | /////////////////////////////////////////////////////////////////////////////// |
| 231 | |
| 232 | int SkTypeface::getVariationDesignPosition( |
| 233 | SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const |
| 234 | { |
| 235 | return this->onGetVariationDesignPosition(coordinates, coordinateCount); |
| 236 | } |
| 237 | |
| 238 | int SkTypeface::getVariationDesignParameters( |
| 239 | SkFontParameters::Variation::Axis parameters[], int parameterCount) const |
| 240 | { |
| 241 | return this->onGetVariationDesignParameters(parameters, parameterCount); |
| 242 | } |
| 243 | |
| 244 | int SkTypeface::countTables() const { |
| 245 | return this->onGetTableTags(nullptr); |
| 246 | } |
| 247 | |
| 248 | int SkTypeface::getTableTags(SkFontTableTag tags[]) const { |
| 249 | return this->onGetTableTags(tags); |
| 250 | } |
| 251 | |
| 252 | size_t SkTypeface::getTableSize(SkFontTableTag tag) const { |
| 253 | return this->onGetTableData(tag, 0, ~0U, nullptr); |
| 254 | } |
| 255 | |
| 256 | size_t SkTypeface::getTableData(SkFontTableTag tag, size_t offset, size_t length, |
| 257 | void* data) const { |
| 258 | return this->onGetTableData(tag, offset, length, data); |
| 259 | } |
| 260 | |
| 261 | sk_sp<SkData> SkTypeface::copyTableData(SkFontTableTag tag) const { |
| 262 | return this->onCopyTableData(tag); |
| 263 | } |
| 264 | |
| 265 | sk_sp<SkData> SkTypeface::onCopyTableData(SkFontTableTag tag) const { |
| 266 | size_t size = this->getTableSize(tag); |
| 267 | if (size) { |
| 268 | sk_sp<SkData> data = SkData::MakeUninitialized(size); |
| 269 | (void)this->getTableData(tag, 0, size, data->writable_data()); |
| 270 | return data; |
| 271 | } |
| 272 | return nullptr; |
| 273 | } |
| 274 | |
| 275 | std::unique_ptr<SkStreamAsset> SkTypeface::openStream(int* ttcIndex) const { |
| 276 | int ttcIndexStorage; |
| 277 | if (nullptr == ttcIndex) { |
| 278 | // So our subclasses don't need to check for null param |
| 279 | ttcIndex = &ttcIndexStorage; |
| 280 | } |
| 281 | return this->onOpenStream(ttcIndex); |
| 282 | } |
| 283 | |
| 284 | std::unique_ptr<SkFontData> SkTypeface::makeFontData() const { |
| 285 | return this->onMakeFontData(); |
| 286 | } |
| 287 | |
| 288 | // This implementation is temporary until this method can be made pure virtual. |
| 289 | std::unique_ptr<SkFontData> SkTypeface::onMakeFontData() const { |
| 290 | int index; |
| 291 | std::unique_ptr<SkStreamAsset> stream(this->onOpenStream(&index)); |
| 292 | if (!stream) { |
| 293 | return nullptr; |
| 294 | } |
| 295 | return std::make_unique<SkFontData>(std::move(stream), index, nullptr, 0); |
| 296 | }; |
| 297 | |
| 298 | void SkTypeface::unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const { |
| 299 | if (count > 0 && glyphs && uni) { |
| 300 | this->onCharsToGlyphs(uni, count, glyphs); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | SkGlyphID SkTypeface::unicharToGlyph(SkUnichar uni) const { |
| 305 | SkGlyphID glyphs[1] = { 0 }; |
| 306 | this->onCharsToGlyphs(&uni, 1, glyphs); |
| 307 | return glyphs[0]; |
| 308 | } |
| 309 | |
| 310 | int SkTypeface::countGlyphs() const { |
| 311 | return this->onCountGlyphs(); |
| 312 | } |
| 313 | |
| 314 | int SkTypeface::getUnitsPerEm() const { |
| 315 | // should we try to cache this in the base-class? |
| 316 | return this->onGetUPEM(); |
| 317 | } |
| 318 | |
| 319 | bool SkTypeface::getKerningPairAdjustments(const uint16_t glyphs[], int count, |
| 320 | int32_t adjustments[]) const { |
| 321 | SkASSERT(count >= 0); |
| 322 | // check for the only legal way to pass a nullptr.. everything is 0 |
| 323 | // in which case they just want to know if this face can possibly support |
| 324 | // kerning (true) or never (false). |
| 325 | if (nullptr == glyphs || nullptr == adjustments) { |
| 326 | SkASSERT(nullptr == glyphs); |
| 327 | SkASSERT(0 == count); |
| 328 | SkASSERT(nullptr == adjustments); |
| 329 | } |
| 330 | return this->onGetKerningPairAdjustments(glyphs, count, adjustments); |
| 331 | } |
| 332 | |
| 333 | SkTypeface::LocalizedStrings* SkTypeface::createFamilyNameIterator() const { |
| 334 | return this->onCreateFamilyNameIterator(); |
| 335 | } |
| 336 | |
| 337 | void SkTypeface::getFamilyName(SkString* name) const { |
| 338 | SkASSERT(name); |
| 339 | this->onGetFamilyName(name); |
| 340 | } |
| 341 | |
| 342 | void SkTypeface::getGlyphToUnicodeMap(SkUnichar* dst) const { |
| 343 | sk_bzero(dst, sizeof(SkUnichar) * this->countGlyphs()); |
| 344 | } |
| 345 | |
| 346 | std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface::getAdvancedMetrics() const { |
| 347 | std::unique_ptr<SkAdvancedTypefaceMetrics> result = this->onGetAdvancedMetrics(); |
| 348 | if (result && result->fPostScriptName.isEmpty()) { |
| 349 | result->fPostScriptName = result->fFontName; |
| 350 | } |
| 351 | if (result && result->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) { |
| 352 | SkOTTableOS2::Version::V2::Type::Field fsType; |
| 353 | constexpr SkFontTableTag os2Tag = SkTEndian_SwapBE32(SkOTTableOS2::TAG); |
| 354 | constexpr size_t fsTypeOffset = offsetof(SkOTTableOS2::Version::V2, fsType); |
| 355 | if (this->getTableData(os2Tag, fsTypeOffset, sizeof(fsType), &fsType) == sizeof(fsType)) { |
| 356 | if (fsType.Bitmap || (fsType.Restricted && !(fsType.PreviewPrint || fsType.Editable))) { |
| 357 | result->fFlags |= SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag; |
| 358 | } |
| 359 | if (fsType.NoSubsetting) { |
| 360 | result->fFlags |= SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag; |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | return result; |
| 365 | } |
| 366 | |
| 367 | bool SkTypeface::onGetKerningPairAdjustments(const uint16_t glyphs[], int count, |
| 368 | int32_t adjustments[]) const { |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | /////////////////////////////////////////////////////////////////////////////// |
| 373 | |
| 374 | #include "include/core/SkPaint.h" |
| 375 | #include "src/core/SkDescriptor.h" |
| 376 | |
| 377 | SkRect SkTypeface::getBounds() const { |
| 378 | fBoundsOnce([this] { |
| 379 | if (!this->onComputeBounds(&fBounds)) { |
| 380 | fBounds.setEmpty(); |
| 381 | } |
| 382 | }); |
| 383 | return fBounds; |
| 384 | } |
| 385 | |
| 386 | bool SkTypeface::onComputeBounds(SkRect* bounds) const { |
| 387 | // we use a big size to ensure lots of significant bits from the scalercontext. |
| 388 | // then we scale back down to return our final answer (at 1-pt) |
| 389 | const SkScalar textSize = 2048; |
| 390 | const SkScalar invTextSize = 1 / textSize; |
| 391 | |
| 392 | SkFont font; |
| 393 | font.setTypeface(sk_ref_sp(const_cast<SkTypeface*>(this))); |
| 394 | font.setSize(textSize); |
| 395 | font.setLinearMetrics(true); |
| 396 | |
| 397 | SkScalerContextRec rec; |
| 398 | SkScalerContextEffects effects; |
| 399 | |
| 400 | SkScalerContext::MakeRecAndEffectsFromFont(font, &rec, &effects); |
| 401 | |
| 402 | SkAutoDescriptor ad; |
| 403 | SkScalerContextEffects noeffects; |
| 404 | SkScalerContext::AutoDescriptorGivenRecAndEffects(rec, noeffects, &ad); |
| 405 | |
| 406 | std::unique_ptr<SkScalerContext> ctx = this->createScalerContext(noeffects, ad.getDesc()); |
| 407 | |
| 408 | SkFontMetrics fm; |
| 409 | ctx->getFontMetrics(&fm); |
| 410 | bounds->setLTRB(fm.fXMin * invTextSize, fm.fTop * invTextSize, |
| 411 | fm.fXMax * invTextSize, fm.fBottom * invTextSize); |
| 412 | return true; |
| 413 | } |
| 414 | |
| 415 | std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface::onGetAdvancedMetrics() const { |
| 416 | SkDEBUGFAIL("Typefaces that need to work with PDF backend must override this." ); |
| 417 | return nullptr; |
| 418 | } |
| 419 | |