blob: 3858b64ce1fbcc968f5af70811a4cb3991c755df [file] [log] [blame]
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +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.org28be72b2010-11-11 21:37:00 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00009#ifndef SkPDFFont_DEFINED
10#define SkPDFFont_DEFINED
11
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000012#include "SkAdvancedTypefaceMetrics.h"
vandebo@chromium.org98594282011-07-25 22:34:12 +000013#include "SkBitSet.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000014#include "SkPDFTypes.h"
15#include "SkTDArray.h"
vandebo@chromium.org98594282011-07-25 22:34:12 +000016#include "SkTypeface.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000017
Hal Canaryaa3af7b2017-03-06 16:18:49 -050018class SkAutoGlyphCache;
halcanary792c80f2015-02-20 07:21:05 -080019class SkPDFCanon;
vandebo@chromium.org98594282011-07-25 22:34:12 +000020class SkPDFFont;
21
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000022/** \class SkPDFFont
23 A PDF Object class representing a font. The font may have resources
24 attached to it in order to embed the font. SkPDFFonts are canonicalized
25 so that resource deduplication will only include one copy of a font.
26 This class uses the same pattern as SkPDFGraphicState, a static weak
27 reference to each instantiated class.
28*/
29class SkPDFFont : public SkPDFDict {
halcanary9d524f22016-03-29 09:03:52 -070030
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000031public:
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +000032 virtual ~SkPDFFont();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000033
halcanary96fcdcc2015-08-27 07:41:13 -070034 /** Returns the typeface represented by this class. Returns nullptr for the
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000035 * default typeface.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000036 */
halcanarycee13422016-08-18 09:52:48 -070037 SkTypeface* typeface() const { return fTypeface.get(); }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000038
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +000039 /** Returns the font type represented in this font. For Type0 fonts,
40 * returns the type of the decendant font.
41 */
halcanarycee13422016-08-18 09:52:48 -070042 SkAdvancedTypefaceMetrics::FontType getType() const { return fFontType; }
43
halcanaryc2f9ec12016-09-12 08:55:29 -070044 static SkAdvancedTypefaceMetrics::FontType FontType(const SkAdvancedTypefaceMetrics&);
45
halcanarycee13422016-08-18 09:52:48 -070046 static bool IsMultiByte(SkAdvancedTypefaceMetrics::FontType type) {
47 return type == SkAdvancedTypefaceMetrics::kType1CID_Font ||
48 type == SkAdvancedTypefaceMetrics::kTrueType_Font;
49 }
vandebo@chromium.org98594282011-07-25 22:34:12 +000050
Hal Canaryaa3af7b2017-03-06 16:18:49 -050051 static SkAutoGlyphCache MakeVectorCache(SkTypeface*, int* sizeOut);
52
vandebo@chromium.org98594282011-07-25 22:34:12 +000053 /** Returns true if this font encoding supports glyph IDs above 255.
54 */
halcanarycee13422016-08-18 09:52:48 -070055 bool multiByteGlyphs() const { return SkPDFFont::IsMultiByte(this->getType()); }
vandebo0f9bad02014-06-19 11:05:39 -070056
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000057 /** Return true if this font has an encoding for the passed glyph id.
58 */
halcanarycee13422016-08-18 09:52:48 -070059 bool hasGlyph(SkGlyphID gid) {
60 return (gid >= fFirstGlyphID && gid <= fLastGlyphID) || gid == 0;
61 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000062
halcanary4871f222016-08-26 13:17:44 -070063 /** Convert the input glyph ID into the font encoding. */
64 SkGlyphID glyphToPDFFontEncoding(SkGlyphID gid) const {
65 if (this->multiByteGlyphs() || gid == 0) {
66 return gid;
67 }
68 SkASSERT(gid >= fFirstGlyphID && gid <= fLastGlyphID);
69 SkASSERT(fFirstGlyphID > 0);
70 return gid - fFirstGlyphID + 1;
71 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000072
halcanary4871f222016-08-26 13:17:44 -070073 void noteGlyphUsage(SkGlyphID glyph) {
74 SkASSERT(this->hasGlyph(glyph));
75 fGlyphUsage.set(glyph);
halcanary530032a2016-08-18 14:22:52 -070076 }
77
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000078 /** Get the font resource for the passed typeface and glyphID. The
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000079 * reference count of the object is incremented and it is the caller's
80 * responsibility to unreference it when done. This is needed to
81 * accommodate the weak reference pattern used when the returned object
82 * is new and has no other references.
halcanary4871f222016-08-26 13:17:44 -070083 * @param typeface The typeface to find, not nullptr.
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000084 * @param glyphID Specify which section of a large font is of interest.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000085 */
halcanary792c80f2015-02-20 07:21:05 -080086 static SkPDFFont* GetFontResource(SkPDFCanon* canon,
87 SkTypeface* typeface,
halcanarycee13422016-08-18 09:52:48 -070088 SkGlyphID glyphID);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000089
halcanary4871f222016-08-26 13:17:44 -070090 /** Uses (kGlyphNames_PerGlyphInfo | kToUnicode_PerGlyphInfo) to get
91 * SkAdvancedTypefaceMetrics, and caches the result.
92 * @param typeface can not be nullptr.
93 * @return nullptr only when typeface is bad.
94 */
halcanarycee13422016-08-18 09:52:48 -070095 static const SkAdvancedTypefaceMetrics* GetMetrics(SkTypeface* typeface,
96 SkPDFCanon* canon);
halcanary8eccc302016-08-09 13:04:34 -070097
halcanary530032a2016-08-18 14:22:52 -070098 /** Subset the font based on current usage.
99 * Must be called before emitObject().
vandebo@chromium.org98594282011-07-25 22:34:12 +0000100 */
halcanary530032a2016-08-18 14:22:52 -0700101 virtual void getFontSubset(SkPDFCanon*) = 0;
halcanaryfb62b3d2015-01-21 09:59:14 -0800102
halcanary66a82f32015-10-12 13:05:04 -0700103 /**
104 * Return false iff the typeface has its NotEmbeddable flag set.
halcanary4871f222016-08-26 13:17:44 -0700105 * typeface is not nullptr
halcanary66a82f32015-10-12 13:05:04 -0700106 */
107 static bool CanEmbedTypeface(SkTypeface*, SkPDFCanon*);
108
vandebo@chromium.org98594282011-07-25 22:34:12 +0000109protected:
110 // Common constructor to handle common members.
halcanary530032a2016-08-18 14:22:52 -0700111 struct Info {
112 sk_sp<SkTypeface> fTypeface;
113 SkGlyphID fFirstGlyphID;
114 SkGlyphID fLastGlyphID;
115 SkAdvancedTypefaceMetrics::FontType fFontType;
116 };
117 SkPDFFont(Info);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000118
halcanary32875882016-08-16 09:36:23 -0700119 SkGlyphID firstGlyphID() const { return fFirstGlyphID; }
120 SkGlyphID lastGlyphID() const { return fLastGlyphID; }
halcanary530032a2016-08-18 14:22:52 -0700121 const SkBitSet& glyphUsage() const { return fGlyphUsage; }
halcanary32875882016-08-16 09:36:23 -0700122 sk_sp<SkTypeface> refTypeface() const { return fTypeface; }
vandebo@chromium.org98594282011-07-25 22:34:12 +0000123
halcanarybae235e2016-03-21 10:05:23 -0700124 void drop() override;
125
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000126private:
halcanary48810a02016-03-07 14:57:50 -0800127 sk_sp<SkTypeface> fTypeface;
halcanary530032a2016-08-18 14:22:52 -0700128 SkBitSet fGlyphUsage;
vandebo@chromium.org98594282011-07-25 22:34:12 +0000129
halcanary32875882016-08-16 09:36:23 -0700130 // The glyph IDs accessible with this font. For Type1 (non CID) fonts,
131 // this will be a subset if the font has more than 255 glyphs.
halcanary4871f222016-08-26 13:17:44 -0700132 const SkGlyphID fFirstGlyphID;
133 const SkGlyphID fLastGlyphID;
134 const SkAdvancedTypefaceMetrics::FontType fFontType;
vandebo@chromium.org98594282011-07-25 22:34:12 +0000135
commit-bot@chromium.orgab1c1382013-12-05 12:08:12 +0000136 typedef SkPDFDict INHERITED;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000137};
138
139#endif