vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 9 | #ifndef SkPDFFont_DEFINED |
| 10 | #define SkPDFFont_DEFINED |
| 11 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 12 | #include "SkAdvancedTypefaceMetrics.h" |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 13 | #include "SkBitSet.h" |
Hal Canary | 8ef78ea | 2018-10-01 13:38:30 -0400 | [diff] [blame^] | 14 | #include "SkPDFCanon.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 15 | #include "SkPDFTypes.h" |
Hal Canary | 8ef78ea | 2018-10-01 13:38:30 -0400 | [diff] [blame^] | 16 | #include "SkStrikeCache.h" |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 17 | #include "SkTypeface.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 18 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 19 | /** \class SkPDFFont |
| 20 | A PDF Object class representing a font. The font may have resources |
| 21 | attached to it in order to embed the font. SkPDFFonts are canonicalized |
| 22 | so that resource deduplication will only include one copy of a font. |
| 23 | This class uses the same pattern as SkPDFGraphicState, a static weak |
| 24 | reference to each instantiated class. |
| 25 | */ |
| 26 | class SkPDFFont : public SkPDFDict { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 27 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 28 | public: |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 29 | ~SkPDFFont() override; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 30 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 31 | /** Returns the typeface represented by this class. Returns nullptr for the |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 32 | * default typeface. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 33 | */ |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 34 | SkTypeface* typeface() const { return fTypeface.get(); } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 35 | |
vandebo@chromium.org | f0ec266 | 2011-05-29 05:55:42 +0000 | [diff] [blame] | 36 | /** Returns the font type represented in this font. For Type0 fonts, |
| 37 | * returns the type of the decendant font. |
| 38 | */ |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 39 | SkAdvancedTypefaceMetrics::FontType getType() const { return fFontType; } |
| 40 | |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 41 | static SkAdvancedTypefaceMetrics::FontType FontType(const SkAdvancedTypefaceMetrics&); |
Hal Canary | 5bdc4d5 | 2018-04-10 11:13:24 -0400 | [diff] [blame] | 42 | static void GetType1GlyphNames(const SkTypeface&, SkString*); |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 43 | |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 44 | static bool IsMultiByte(SkAdvancedTypefaceMetrics::FontType type) { |
| 45 | return type == SkAdvancedTypefaceMetrics::kType1CID_Font || |
| 46 | type == SkAdvancedTypefaceMetrics::kTrueType_Font; |
| 47 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 48 | |
Herb Derby | 1a605cd | 2018-03-22 11:16:25 -0400 | [diff] [blame] | 49 | static SkExclusiveStrikePtr MakeVectorCache(SkTypeface*, int* sizeOut); |
Hal Canary | aa3af7b | 2017-03-06 16:18:49 -0500 | [diff] [blame] | 50 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 51 | /** Returns true if this font encoding supports glyph IDs above 255. |
| 52 | */ |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 53 | bool multiByteGlyphs() const { return SkPDFFont::IsMultiByte(this->getType()); } |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 54 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 55 | /** Return true if this font has an encoding for the passed glyph id. |
| 56 | */ |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 57 | bool hasGlyph(SkGlyphID gid) { |
| 58 | return (gid >= fFirstGlyphID && gid <= fLastGlyphID) || gid == 0; |
| 59 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 60 | |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 61 | /** Convert the input glyph ID into the font encoding. */ |
| 62 | SkGlyphID glyphToPDFFontEncoding(SkGlyphID gid) const { |
| 63 | if (this->multiByteGlyphs() || gid == 0) { |
| 64 | return gid; |
| 65 | } |
| 66 | SkASSERT(gid >= fFirstGlyphID && gid <= fLastGlyphID); |
| 67 | SkASSERT(fFirstGlyphID > 0); |
| 68 | return gid - fFirstGlyphID + 1; |
| 69 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 70 | |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 71 | void noteGlyphUsage(SkGlyphID glyph) { |
| 72 | SkASSERT(this->hasGlyph(glyph)); |
| 73 | fGlyphUsage.set(glyph); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 74 | } |
| 75 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 76 | /** Get the font resource for the passed typeface and glyphID. The |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 77 | * reference count of the object is incremented and it is the caller's |
| 78 | * responsibility to unreference it when done. This is needed to |
| 79 | * accommodate the weak reference pattern used when the returned object |
| 80 | * is new and has no other references. |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 81 | * @param typeface The typeface to find, not nullptr. |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 82 | * @param glyphID Specify which section of a large font is of interest. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 83 | */ |
Hal Canary | 5c1b360 | 2017-04-17 16:30:06 -0400 | [diff] [blame] | 84 | static sk_sp<SkPDFFont> GetFontResource(SkPDFCanon* canon, |
Hal Canary | 8ef78ea | 2018-10-01 13:38:30 -0400 | [diff] [blame^] | 85 | SkGlyphCache* cache, |
Hal Canary | 5c1b360 | 2017-04-17 16:30:06 -0400 | [diff] [blame] | 86 | SkTypeface* typeface, |
| 87 | SkGlyphID glyphID); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 88 | |
Hal Canary | 209e4b1 | 2017-05-04 14:23:55 -0400 | [diff] [blame] | 89 | /** Gets SkAdvancedTypefaceMetrics, and caches the result. |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 90 | * @param typeface can not be nullptr. |
| 91 | * @return nullptr only when typeface is bad. |
| 92 | */ |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 93 | static const SkAdvancedTypefaceMetrics* GetMetrics(SkTypeface* typeface, |
| 94 | SkPDFCanon* canon); |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 95 | |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 96 | static const std::vector<SkUnichar>& GetUnicodeMap(const SkTypeface* typeface, |
| 97 | SkPDFCanon* canon); |
| 98 | |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 99 | /** Subset the font based on current usage. |
| 100 | * Must be called before emitObject(). |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 101 | */ |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 102 | virtual void getFontSubset(SkPDFCanon*) = 0; |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 103 | |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 104 | /** |
| 105 | * Return false iff the typeface has its NotEmbeddable flag set. |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 106 | * typeface is not nullptr |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 107 | */ |
| 108 | static bool CanEmbedTypeface(SkTypeface*, SkPDFCanon*); |
| 109 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 110 | protected: |
| 111 | // Common constructor to handle common members. |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 112 | struct Info { |
| 113 | sk_sp<SkTypeface> fTypeface; |
| 114 | SkGlyphID fFirstGlyphID; |
| 115 | SkGlyphID fLastGlyphID; |
| 116 | SkAdvancedTypefaceMetrics::FontType fFontType; |
| 117 | }; |
| 118 | SkPDFFont(Info); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 119 | |
halcanary | 3287588 | 2016-08-16 09:36:23 -0700 | [diff] [blame] | 120 | SkGlyphID firstGlyphID() const { return fFirstGlyphID; } |
| 121 | SkGlyphID lastGlyphID() const { return fLastGlyphID; } |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 122 | const SkBitSet& glyphUsage() const { return fGlyphUsage; } |
halcanary | 3287588 | 2016-08-16 09:36:23 -0700 | [diff] [blame] | 123 | sk_sp<SkTypeface> refTypeface() const { return fTypeface; } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 124 | |
halcanary | bae235e | 2016-03-21 10:05:23 -0700 | [diff] [blame] | 125 | void drop() override; |
| 126 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 127 | private: |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 128 | sk_sp<SkTypeface> fTypeface; |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 129 | SkBitSet fGlyphUsage; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 130 | |
halcanary | 3287588 | 2016-08-16 09:36:23 -0700 | [diff] [blame] | 131 | // The glyph IDs accessible with this font. For Type1 (non CID) fonts, |
| 132 | // this will be a subset if the font has more than 255 glyphs. |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 133 | const SkGlyphID fFirstGlyphID; |
| 134 | const SkGlyphID fLastGlyphID; |
| 135 | const SkAdvancedTypefaceMetrics::FontType fFontType; |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 136 | |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame] | 137 | typedef SkPDFDict INHERITED; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | #endif |