blob: 4d3ee9873f6e443a9d7fda96e7730a26a6405ffd [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 */
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00007#ifndef SkPDFFont_DEFINED
8#define SkPDFFont_DEFINED
9
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000010#include "SkAdvancedTypefaceMetrics.h"
Hal Canary8ef78ea2018-10-01 13:38:30 -040011#include "SkPDFCanon.h"
Hal Canary31355982018-10-19 12:21:41 -040012#include "SkPDFGlyphUse.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000013#include "SkPDFTypes.h"
Hal Canary8ef78ea2018-10-01 13:38:30 -040014#include "SkStrikeCache.h"
vandebo@chromium.org98594282011-07-25 22:34:12 +000015#include "SkTypeface.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000016
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000017/** \class SkPDFFont
18 A PDF Object class representing a font. The font may have resources
19 attached to it in order to embed the font. SkPDFFonts are canonicalized
20 so that resource deduplication will only include one copy of a font.
21 This class uses the same pattern as SkPDFGraphicState, a static weak
22 reference to each instantiated class.
23*/
Hal Canaryb10f92e2018-11-16 17:01:50 -050024class SkPDFFont {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000025public:
Hal Canaryb10f92e2018-11-16 17:01:50 -050026 SkPDFFont() {}
27 ~SkPDFFont();
28 SkPDFFont(SkPDFFont&&);
29 SkPDFFont& operator=(SkPDFFont&&);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000030
halcanary96fcdcc2015-08-27 07:41:13 -070031 /** Returns the typeface represented by this class. Returns nullptr for the
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000032 * default typeface.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000033 */
halcanarycee13422016-08-18 09:52:48 -070034 SkTypeface* typeface() const { return fTypeface.get(); }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000035
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +000036 /** Returns the font type represented in this font. For Type0 fonts,
37 * returns the type of the decendant font.
38 */
halcanarycee13422016-08-18 09:52:48 -070039 SkAdvancedTypefaceMetrics::FontType getType() const { return fFontType; }
40
halcanaryc2f9ec12016-09-12 08:55:29 -070041 static SkAdvancedTypefaceMetrics::FontType FontType(const SkAdvancedTypefaceMetrics&);
Hal Canary5bdc4d52018-04-10 11:13:24 -040042 static void GetType1GlyphNames(const SkTypeface&, SkString*);
halcanaryc2f9ec12016-09-12 08:55:29 -070043
halcanarycee13422016-08-18 09:52:48 -070044 static bool IsMultiByte(SkAdvancedTypefaceMetrics::FontType type) {
45 return type == SkAdvancedTypefaceMetrics::kType1CID_Font ||
46 type == SkAdvancedTypefaceMetrics::kTrueType_Font;
47 }
vandebo@chromium.org98594282011-07-25 22:34:12 +000048
Herb Derby1a605cd2018-03-22 11:16:25 -040049 static SkExclusiveStrikePtr MakeVectorCache(SkTypeface*, int* sizeOut);
Hal Canaryaa3af7b2017-03-06 16:18:49 -050050
vandebo@chromium.org98594282011-07-25 22:34:12 +000051 /** Returns true if this font encoding supports glyph IDs above 255.
52 */
halcanarycee13422016-08-18 09:52:48 -070053 bool multiByteGlyphs() const { return SkPDFFont::IsMultiByte(this->getType()); }
vandebo0f9bad02014-06-19 11:05:39 -070054
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000055 /** Return true if this font has an encoding for the passed glyph id.
56 */
halcanarycee13422016-08-18 09:52:48 -070057 bool hasGlyph(SkGlyphID gid) {
Hal Canary31355982018-10-19 12:21:41 -040058 return (gid >= this->firstGlyphID() && gid <= this->lastGlyphID()) || gid == 0;
halcanarycee13422016-08-18 09:52:48 -070059 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000060
halcanary4871f222016-08-26 13:17:44 -070061 /** 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 }
Hal Canary31355982018-10-19 12:21:41 -040066 SkASSERT(gid >= this->firstGlyphID() && gid <= this->lastGlyphID());
67 SkASSERT(this->firstGlyphID() > 0);
68 return gid - this->firstGlyphID() + 1;
halcanary4871f222016-08-26 13:17:44 -070069 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000070
halcanary4871f222016-08-26 13:17:44 -070071 void noteGlyphUsage(SkGlyphID glyph) {
72 SkASSERT(this->hasGlyph(glyph));
73 fGlyphUsage.set(glyph);
halcanary530032a2016-08-18 14:22:52 -070074 }
75
Hal Canaryb10f92e2018-11-16 17:01:50 -050076 SkPDFIndirectReference indirectReference() const { return fIndirectReference; }
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 */
Hal Canaryb10f92e2018-11-16 17:01:50 -050086 static SkPDFFont* GetFontResource(SkPDFDocument* doc,
87 SkGlyphCache* cache,
88 SkTypeface* typeface,
89 SkGlyphID glyphID);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000090
Hal Canary209e4b12017-05-04 14:23:55 -040091 /** Gets SkAdvancedTypefaceMetrics, and caches the result.
halcanary4871f222016-08-26 13:17:44 -070092 * @param typeface can not be nullptr.
93 * @return nullptr only when typeface is bad.
94 */
Hal Canaryb10f92e2018-11-16 17:01:50 -050095 static const SkAdvancedTypefaceMetrics* GetMetrics(const SkTypeface* typeface,
halcanarycee13422016-08-18 09:52:48 -070096 SkPDFCanon* canon);
halcanary8eccc302016-08-09 13:04:34 -070097
Hal Canary46cc3da2018-05-09 11:50:34 -040098 static const std::vector<SkUnichar>& GetUnicodeMap(const SkTypeface* typeface,
99 SkPDFCanon* canon);
100
Hal Canaryb10f92e2018-11-16 17:01:50 -0500101 void emitSubset(SkPDFDocument*) const;
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
Hal Canary31355982018-10-19 12:21:41 -0400109 SkGlyphID firstGlyphID() const { return fGlyphUsage.firstNonZero(); }
110 SkGlyphID lastGlyphID() const { return fGlyphUsage.lastGlyph(); }
111 const SkPDFGlyphUse& glyphUsage() const { return fGlyphUsage; }
halcanary32875882016-08-16 09:36:23 -0700112 sk_sp<SkTypeface> refTypeface() const { return fTypeface; }
vandebo@chromium.org98594282011-07-25 22:34:12 +0000113
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000114private:
halcanary48810a02016-03-07 14:57:50 -0800115 sk_sp<SkTypeface> fTypeface;
Hal Canary31355982018-10-19 12:21:41 -0400116 SkPDFGlyphUse fGlyphUsage;
Hal Canaryb10f92e2018-11-16 17:01:50 -0500117 SkPDFIndirectReference fIndirectReference;
118 SkAdvancedTypefaceMetrics::FontType fFontType = (SkAdvancedTypefaceMetrics::FontType)(-1);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000119
Hal Canaryb10f92e2018-11-16 17:01:50 -0500120 SkPDFFont(sk_sp<SkTypeface>,
121 SkGlyphID firstGlyphID,
122 SkGlyphID lastGlyphID,
123 SkAdvancedTypefaceMetrics::FontType fontType,
124 SkPDFIndirectReference indirectReference);
halcanary32875882016-08-16 09:36:23 -0700125 // The glyph IDs accessible with this font. For Type1 (non CID) fonts,
126 // this will be a subset if the font has more than 255 glyphs.
vandebo@chromium.org98594282011-07-25 22:34:12 +0000127
Hal Canaryb10f92e2018-11-16 17:01:50 -0500128 SkPDFFont(const SkPDFFont&) = delete;
129 SkPDFFont& operator=(const SkPDFFont&) = delete;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000130};
131
132#endif