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 | |
Kaylee Lubick | bea3840 | 2024-05-03 18:11:24 -0400 | [diff] [blame] | 8 | #include "src/pdf/SkPDFFont.h" |
| 9 | |
Ben Wagner | 0b9b1f1 | 2019-04-04 18:00:05 -0400 | [diff] [blame] | 10 | #include "include/core/SkBitmap.h" |
Kaylee Lubick | bea3840 | 2024-05-03 18:11:24 -0400 | [diff] [blame] | 11 | #include "include/core/SkCanvas.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkData.h" |
Kaylee Lubick | bea3840 | 2024-05-03 18:11:24 -0400 | [diff] [blame] | 13 | #include "include/core/SkDrawable.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/core/SkFont.h" |
Ben Wagner | 0b9b1f1 | 2019-04-04 18:00:05 -0400 | [diff] [blame] | 15 | #include "include/core/SkFontMetrics.h" |
Kaylee Lubick | bea3840 | 2024-05-03 18:11:24 -0400 | [diff] [blame] | 16 | #include "include/core/SkFontStyle.h" |
Ben Wagner | 0b9b1f1 | 2019-04-04 18:00:05 -0400 | [diff] [blame] | 17 | #include "include/core/SkFontTypes.h" |
| 18 | #include "include/core/SkImage.h" |
| 19 | #include "include/core/SkImageInfo.h" |
| 20 | #include "include/core/SkMatrix.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "include/core/SkPaint.h" |
Ben Wagner | 0b9b1f1 | 2019-04-04 18:00:05 -0400 | [diff] [blame] | 22 | #include "include/core/SkPath.h" |
| 23 | #include "include/core/SkPoint.h" |
| 24 | #include "include/core/SkRect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "include/core/SkRefCnt.h" |
| 26 | #include "include/core/SkScalar.h" |
| 27 | #include "include/core/SkStream.h" |
Ben Wagner | 0b9b1f1 | 2019-04-04 18:00:05 -0400 | [diff] [blame] | 28 | #include "include/core/SkString.h" |
| 29 | #include "include/core/SkSurfaceProps.h" |
Kaylee Lubick | bea3840 | 2024-05-03 18:11:24 -0400 | [diff] [blame] | 30 | #include "include/private/base/SkDebug.h" |
| 31 | #include "include/private/base/SkTPin.h" |
Kevin Lubick | 19936eb | 2023-01-05 09:00:37 -0500 | [diff] [blame] | 32 | #include "include/private/base/SkTo.h" |
John Stiles | 99e8dc5 | 2023-07-24 11:56:16 -0400 | [diff] [blame] | 33 | #include "src/base/SkBitmaskEnum.h" |
Kevin Lubick | 1b3aa8b | 2023-01-19 14:03:31 -0500 | [diff] [blame] | 34 | #include "src/base/SkUTF.h" |
Kaylee Lubick | bea3840 | 2024-05-03 18:11:24 -0400 | [diff] [blame] | 35 | #include "src/core/SkDevice.h" |
Ben Wagner | 0b9b1f1 | 2019-04-04 18:00:05 -0400 | [diff] [blame] | 36 | #include "src/core/SkGlyph.h" |
Ben Wagner | 0b9b1f1 | 2019-04-04 18:00:05 -0400 | [diff] [blame] | 37 | #include "src/core/SkMask.h" |
| 38 | #include "src/core/SkScalerContext.h" |
Herb Derby | 14c62c6 | 2023-01-20 10:32:02 -0500 | [diff] [blame] | 39 | #include "src/core/SkStrike.h" |
Herb Derby | 2b35b7d | 2019-05-22 16:11:13 -0400 | [diff] [blame] | 40 | #include "src/core/SkStrikeSpec.h" |
Kevin Lubick | 1b3aa8b | 2023-01-19 14:03:31 -0500 | [diff] [blame] | 41 | #include "src/core/SkTHash.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 42 | #include "src/pdf/SkPDFBitmap.h" |
Ben Wagner | 4e8b235 | 2023-01-31 17:00:56 -0500 | [diff] [blame] | 43 | #include "src/pdf/SkPDFDevice.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 44 | #include "src/pdf/SkPDFDocumentPriv.h" |
Ben Wagner | 4e8b235 | 2023-01-31 17:00:56 -0500 | [diff] [blame] | 45 | #include "src/pdf/SkPDFFormXObject.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 46 | #include "src/pdf/SkPDFMakeCIDGlyphWidthsArray.h" |
| 47 | #include "src/pdf/SkPDFMakeToUnicodeCmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 48 | #include "src/pdf/SkPDFSubsetFont.h" |
Ben Wagner | 0b9b1f1 | 2019-04-04 18:00:05 -0400 | [diff] [blame] | 49 | #include "src/pdf/SkPDFType1Font.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 50 | #include "src/pdf/SkPDFUtils.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 51 | |
Ben Wagner | 0b9b1f1 | 2019-04-04 18:00:05 -0400 | [diff] [blame] | 52 | #include <limits.h> |
Kaylee Lubick | bea3840 | 2024-05-03 18:11:24 -0400 | [diff] [blame] | 53 | #include <algorithm> |
| 54 | #include <cstddef> |
Ben Wagner | 0b9b1f1 | 2019-04-04 18:00:05 -0400 | [diff] [blame] | 55 | #include <initializer_list> |
| 56 | #include <memory> |
| 57 | #include <utility> |
| 58 | |
Hal Canary | 2e904bc | 2019-04-18 15:40:27 -0400 | [diff] [blame] | 59 | void SkPDFFont::GetType1GlyphNames(const SkTypeface& face, SkString* dst) { |
| 60 | face.getPostScriptGlyphNames(dst); |
| 61 | } |
| 62 | |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 63 | namespace { |
vandebo@chromium.org | dcf9c19 | 2013-03-13 20:01:51 +0000 | [diff] [blame] | 64 | // PDF's notion of symbolic vs non-symbolic is related to the character set, not |
| 65 | // symbols vs. characters. Rarely is a font the right character set to call it |
| 66 | // non-symbolic, so always call it symbolic. (PDF 1.4 spec, section 5.7.1) |
halcanary | 3d01c62 | 2016-08-31 12:52:35 -0700 | [diff] [blame] | 67 | static const int32_t kPdfSymbolic = 4; |
Dominik Röttsches | 2ed0bae | 2021-11-15 15:14:03 +0200 | [diff] [blame] | 68 | static const SkFontTableTag kCOLRTableTag = SkSetFourByteTag('C', 'O', 'L', 'R'); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 69 | |
reed@google.com | 3f0dcf9 | 2011-03-18 21:23:45 +0000 | [diff] [blame] | 70 | // scale from em-units to base-1000, returning as a SkScalar |
Hal Canary | 2e904bc | 2019-04-18 15:40:27 -0400 | [diff] [blame] | 71 | inline SkScalar from_font_units(SkScalar scaled, uint16_t emSize) { |
| 72 | return emSize == 1000 ? scaled : scaled * 1000 / emSize; |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Hal Canary | 2e904bc | 2019-04-18 15:40:27 -0400 | [diff] [blame] | 75 | inline SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) { |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 76 | return from_font_units(SkIntToScalar(val), emSize); |
| 77 | } |
| 78 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 79 | void setGlyphWidthAndBoundingBox(SkScalar width, SkIRect box, |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 80 | SkDynamicMemoryWStream* content) { |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 81 | // Specify width and bounding box for the glyph. |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 82 | SkPDFUtils::AppendScalar(width, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 83 | content->writeText(" 0 "); |
Cary Clark | 60ebf14 | 2018-09-06 12:22:33 +0000 | [diff] [blame] | 84 | content->writeDecAsText(box.fLeft); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 85 | content->writeText(" "); |
Cary Clark | 60ebf14 | 2018-09-06 12:22:33 +0000 | [diff] [blame] | 86 | content->writeDecAsText(box.fTop); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 87 | content->writeText(" "); |
Cary Clark | 60ebf14 | 2018-09-06 12:22:33 +0000 | [diff] [blame] | 88 | content->writeDecAsText(box.fRight); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 89 | content->writeText(" "); |
Cary Clark | 60ebf14 | 2018-09-06 12:22:33 +0000 | [diff] [blame] | 90 | content->writeDecAsText(box.fBottom); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 91 | content->writeText(" d1\n"); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 92 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 93 | } // namespace |
| 94 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 95 | /////////////////////////////////////////////////////////////////////////////// |
| 96 | // class SkPDFFont |
| 97 | /////////////////////////////////////////////////////////////////////////////// |
| 98 | |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 99 | /* Resources are canonicalized and uniqueified by pointer so there has to be |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 100 | * some additional state indicating which subset of the font is used. It |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 101 | * must be maintained at the document granularity. |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 102 | */ |
| 103 | |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 104 | SkPDFFont::~SkPDFFont() = default; |
| 105 | |
| 106 | SkPDFFont::SkPDFFont(SkPDFFont&&) = default; |
| 107 | |
| 108 | SkPDFFont& SkPDFFont::operator=(SkPDFFont&&) = default; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 109 | |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 110 | static bool can_embed(const SkAdvancedTypefaceMetrics& metrics) { |
| 111 | return !SkToBool(metrics.fFlags & SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag); |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 114 | const SkAdvancedTypefaceMetrics* SkPDFFont::GetMetrics(const SkTypeface* typeface, |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 115 | SkPDFDocument* canon) { |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 116 | SkASSERT(typeface); |
Herb Derby | 2562144 | 2022-02-07 12:12:45 -0500 | [diff] [blame] | 117 | SkTypefaceID id = typeface->uniqueID(); |
Hal Canary | 209e4b1 | 2017-05-04 14:23:55 -0400 | [diff] [blame] | 118 | if (std::unique_ptr<SkAdvancedTypefaceMetrics>* ptr = canon->fTypefaceMetrics.find(id)) { |
Hal Canary | 5c1b360 | 2017-04-17 16:30:06 -0400 | [diff] [blame] | 119 | return ptr->get(); // canon retains ownership. |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 120 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 121 | int count = typeface->countGlyphs(); |
Ben Wagner | 1adfbd5 | 2018-06-15 17:10:25 -0400 | [diff] [blame] | 122 | if (count <= 0 || count > 1 + SkTo<int>(UINT16_MAX)) { |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 123 | // Cache nullptr to skip this check. Use SkSafeUnref(). |
| 124 | canon->fTypefaceMetrics.set(id, nullptr); |
| 125 | return nullptr; |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 126 | } |
Hal Canary | 209e4b1 | 2017-05-04 14:23:55 -0400 | [diff] [blame] | 127 | std::unique_ptr<SkAdvancedTypefaceMetrics> metrics = typeface->getAdvancedMetrics(); |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 128 | if (!metrics) { |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 129 | metrics = std::make_unique<SkAdvancedTypefaceMetrics>(); |
halcanary | 1748432 | 2016-08-29 09:47:48 -0700 | [diff] [blame] | 130 | } |
Hal Canary | 3223e27 | 2017-07-05 16:47:59 -0400 | [diff] [blame] | 131 | |
| 132 | if (0 == metrics->fStemV || 0 == metrics->fCapHeight) { |
Mike Reed | ab8f297 | 2018-12-05 13:20:29 -0500 | [diff] [blame] | 133 | SkFont font; |
Ben Wagner | 5785e4a | 2019-05-07 16:50:29 -0400 | [diff] [blame] | 134 | font.setHinting(SkFontHinting::kNone); |
Mike Reed | ab8f297 | 2018-12-05 13:20:29 -0500 | [diff] [blame] | 135 | font.setTypeface(sk_ref_sp(typeface)); |
| 136 | font.setSize(1000); // glyph coordinate system |
Hal Canary | 3223e27 | 2017-07-05 16:47:59 -0400 | [diff] [blame] | 137 | if (0 == metrics->fStemV) { |
| 138 | // Figure out a good guess for StemV - Min width of i, I, !, 1. |
| 139 | // This probably isn't very good with an italic font. |
| 140 | int16_t stemV = SHRT_MAX; |
| 141 | for (char c : {'i', 'I', '!', '1'}) { |
Mike Reed | ab8f297 | 2018-12-05 13:20:29 -0500 | [diff] [blame] | 142 | uint16_t g = font.unicharToGlyph(c); |
Hal Canary | 3223e27 | 2017-07-05 16:47:59 -0400 | [diff] [blame] | 143 | SkRect bounds; |
Mike Reed | ab8f297 | 2018-12-05 13:20:29 -0500 | [diff] [blame] | 144 | font.getBounds(&g, 1, &bounds, nullptr); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 145 | stemV = std::min(stemV, SkToS16(SkScalarRoundToInt(bounds.width()))); |
Hal Canary | 3223e27 | 2017-07-05 16:47:59 -0400 | [diff] [blame] | 146 | } |
| 147 | metrics->fStemV = stemV; |
| 148 | } |
| 149 | if (0 == metrics->fCapHeight) { |
| 150 | // Figure out a good guess for CapHeight: average the height of M and X. |
| 151 | SkScalar capHeight = 0; |
| 152 | for (char c : {'M', 'X'}) { |
Mike Reed | ab8f297 | 2018-12-05 13:20:29 -0500 | [diff] [blame] | 153 | uint16_t g = font.unicharToGlyph(c); |
Hal Canary | 3223e27 | 2017-07-05 16:47:59 -0400 | [diff] [blame] | 154 | SkRect bounds; |
Mike Reed | ab8f297 | 2018-12-05 13:20:29 -0500 | [diff] [blame] | 155 | font.getBounds(&g, 1, &bounds, nullptr); |
Hal Canary | 3223e27 | 2017-07-05 16:47:59 -0400 | [diff] [blame] | 156 | capHeight += bounds.height(); |
| 157 | } |
| 158 | metrics->fCapHeight = SkToS16(SkScalarRoundToInt(capHeight / 2)); |
| 159 | } |
| 160 | } |
Ben Wagner | 17667b0 | 2021-11-24 14:52:12 -0500 | [diff] [blame] | 161 | // Fonts are always subset, so always prepend the subset tag. |
| 162 | metrics->fPostScriptName.prepend(canon->nextFontSubsetTag()); |
Hal Canary | 5c1b360 | 2017-04-17 16:30:06 -0400 | [diff] [blame] | 163 | return canon->fTypefaceMetrics.set(id, std::move(metrics))->get(); |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 166 | const std::vector<SkUnichar>& SkPDFFont::GetUnicodeMap(const SkTypeface* typeface, |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 167 | SkPDFDocument* canon) { |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 168 | SkASSERT(typeface); |
| 169 | SkASSERT(canon); |
Herb Derby | 2562144 | 2022-02-07 12:12:45 -0500 | [diff] [blame] | 170 | SkTypefaceID id = typeface->uniqueID(); |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 171 | if (std::vector<SkUnichar>* ptr = canon->fToUnicodeMap.find(id)) { |
| 172 | return *ptr; |
| 173 | } |
| 174 | std::vector<SkUnichar> buffer(typeface->countGlyphs()); |
| 175 | typeface->getGlyphToUnicodeMap(buffer.data()); |
| 176 | return *canon->fToUnicodeMap.set(id, std::move(buffer)); |
| 177 | } |
| 178 | |
Dominik Röttsches | 2ed0bae | 2021-11-15 15:14:03 +0200 | [diff] [blame] | 179 | SkAdvancedTypefaceMetrics::FontType SkPDFFont::FontType(const SkTypeface& typeface, |
| 180 | const SkAdvancedTypefaceMetrics& metrics) { |
Ben Wagner | 997c907 | 2020-08-04 13:36:52 -0400 | [diff] [blame] | 181 | if (SkToBool(metrics.fFlags & SkAdvancedTypefaceMetrics::kVariable_FontFlag) || |
Ben Wagner | 2bc5447 | 2022-05-25 14:00:11 -0400 | [diff] [blame] | 182 | // PDF is actually interested in the encoding of the data, not just the logical format. |
| 183 | // If the TrueType is actually wOFF or wOF2 then it should not be directly embedded in PDF. |
| 184 | // For now export these as Type3 until the subsetter can handle table based fonts. |
| 185 | // See https://github.com/harfbuzz/harfbuzz/issues/3609 and |
| 186 | // https://skia-review.googlesource.com/c/skia/+/543485 |
| 187 | SkToBool(metrics.fFlags & SkAdvancedTypefaceMetrics::kAltDataFormat_FontFlag) || |
Hal Canary | b3480e1 | 2016-09-30 14:53:15 -0400 | [diff] [blame] | 188 | SkToBool(metrics.fFlags & SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag)) { |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 189 | // force Type3 fallback. |
| 190 | return SkAdvancedTypefaceMetrics::kOther_Font; |
| 191 | } |
Dominik Röttsches | 2ed0bae | 2021-11-15 15:14:03 +0200 | [diff] [blame] | 192 | if (typeface.getTableSize(kCOLRTableTag)) { |
| 193 | // https://bugs.chromium.org/p/skia/issues/detail?id=12650 |
| 194 | // Don't embed COLRv0 / COLRv1 fonts, fall back to bitmaps. |
| 195 | return SkAdvancedTypefaceMetrics::kOther_Font; |
| 196 | } |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 197 | return metrics.fType; |
| 198 | } |
| 199 | |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 200 | static SkGlyphID first_nonzero_glyph_for_single_byte_encoding(SkGlyphID gid) { |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 201 | return gid != 0 ? gid - (gid - 1) % 255 : 1; |
| 202 | } |
| 203 | |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 204 | SkPDFFont* SkPDFFont::GetFontResource(SkPDFDocument* doc, |
Herb Derby | 02e9150 | 2019-07-03 10:57:33 -0400 | [diff] [blame] | 205 | const SkGlyph* glyph, |
| 206 | SkTypeface* face) { |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 207 | SkASSERT(doc); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 208 | SkASSERT(face); // All SkPDFDevice::internalDrawText ensures this. |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 209 | const SkAdvancedTypefaceMetrics* fontMetrics = SkPDFFont::GetMetrics(face, doc); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 210 | SkASSERT(fontMetrics); // SkPDFDevice::internalDrawText ensures the typeface is good. |
| 211 | // GetMetrics only returns null to signify a bad typeface. |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 212 | const SkAdvancedTypefaceMetrics& metrics = *fontMetrics; |
Dominik Röttsches | 2ed0bae | 2021-11-15 15:14:03 +0200 | [diff] [blame] | 213 | SkAdvancedTypefaceMetrics::FontType type = SkPDFFont::FontType(*face, metrics); |
Herb Derby | 02e9150 | 2019-07-03 10:57:33 -0400 | [diff] [blame] | 214 | if (!(glyph->isEmpty() || glyph->path())) { |
Hal Canary | 8ef78ea | 2018-10-01 13:38:30 -0400 | [diff] [blame] | 215 | type = SkAdvancedTypefaceMetrics::kOther_Font; |
| 216 | } |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 217 | bool multibyte = SkPDFFont::IsMultiByte(type); |
Herb Derby | 02e9150 | 2019-07-03 10:57:33 -0400 | [diff] [blame] | 218 | SkGlyphID subsetCode = |
| 219 | multibyte ? 0 : first_nonzero_glyph_for_single_byte_encoding(glyph->getGlyphID()); |
Kevin Lubick | 04449cd | 2023-10-19 09:37:19 -0400 | [diff] [blame] | 220 | uint64_t typefaceID = (static_cast<uint64_t>(face->uniqueID()) << 16) | subsetCode; |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 221 | |
Herb Derby | 2562144 | 2022-02-07 12:12:45 -0500 | [diff] [blame] | 222 | if (SkPDFFont* found = doc->fFontMap.find(typefaceID)) { |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 223 | SkASSERT(multibyte == found->multiByteGlyphs()); |
| 224 | return found; |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 225 | } |
| 226 | |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 227 | sk_sp<SkTypeface> typeface(sk_ref_sp(face)); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 228 | SkASSERT(typeface); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 229 | |
Hal Canary | aa3af7b | 2017-03-06 16:18:49 -0500 | [diff] [blame] | 230 | SkGlyphID lastGlyph = SkToU16(typeface->countGlyphs() - 1); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 231 | |
| 232 | // should be caught by SkPDFDevice::internalDrawText |
Herb Derby | 02e9150 | 2019-07-03 10:57:33 -0400 | [diff] [blame] | 233 | SkASSERT(glyph->getGlyphID() <= lastGlyph); |
halcanary | 3287588 | 2016-08-16 09:36:23 -0700 | [diff] [blame] | 234 | |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 235 | SkGlyphID firstNonZeroGlyph; |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 236 | if (multibyte) { |
| 237 | firstNonZeroGlyph = 1; |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 238 | } else { |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 239 | firstNonZeroGlyph = subsetCode; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 240 | lastGlyph = SkToU16(std::min<int>((int)lastGlyph, 254 + (int)subsetCode)); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 241 | } |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 242 | auto ref = doc->reserveRef(); |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 243 | return doc->fFontMap.set( |
Herb Derby | 2562144 | 2022-02-07 12:12:45 -0500 | [diff] [blame] | 244 | typefaceID, SkPDFFont(std::move(typeface), firstNonZeroGlyph, lastGlyph, type, ref)); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 247 | SkPDFFont::SkPDFFont(sk_sp<SkTypeface> typeface, |
| 248 | SkGlyphID firstGlyphID, |
| 249 | SkGlyphID lastGlyphID, |
| 250 | SkAdvancedTypefaceMetrics::FontType fontType, |
| 251 | SkPDFIndirectReference indirectReference) |
| 252 | : fTypeface(std::move(typeface)) |
| 253 | , fGlyphUsage(firstGlyphID, lastGlyphID) |
| 254 | , fIndirectReference(indirectReference) |
Ben Wagner | e91f2235 | 2019-08-30 19:28:09 -0400 | [diff] [blame] | 255 | , fFontType(fontType) |
| 256 | { |
| 257 | // Always include glyph 0 |
| 258 | this->noteGlyphUsage(0); |
| 259 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 260 | |
Hal Canary | 2e904bc | 2019-04-18 15:40:27 -0400 | [diff] [blame] | 261 | void SkPDFFont::PopulateCommonFontDescriptor(SkPDFDict* descriptor, |
| 262 | const SkAdvancedTypefaceMetrics& metrics, |
| 263 | uint16_t emSize, |
| 264 | int16_t defaultWidth) { |
Hal Canary | e1fec19 | 2018-03-29 10:59:02 -0700 | [diff] [blame] | 265 | descriptor->insertName("FontName", metrics.fPostScriptName); |
halcanary | 28c6d83 | 2016-08-16 10:29:38 -0700 | [diff] [blame] | 266 | descriptor->insertInt("Flags", (size_t)(metrics.fStyle | kPdfSymbolic)); |
| 267 | descriptor->insertScalar("Ascent", |
| 268 | scaleFromFontUnits(metrics.fAscent, emSize)); |
| 269 | descriptor->insertScalar("Descent", |
| 270 | scaleFromFontUnits(metrics.fDescent, emSize)); |
| 271 | descriptor->insertScalar("StemV", |
| 272 | scaleFromFontUnits(metrics.fStemV, emSize)); |
| 273 | descriptor->insertScalar("CapHeight", |
| 274 | scaleFromFontUnits(metrics.fCapHeight, emSize)); |
| 275 | descriptor->insertInt("ItalicAngle", metrics.fItalicAngle); |
Hal Canary | e650b85 | 2018-09-12 09:12:36 -0400 | [diff] [blame] | 276 | descriptor->insertObject("FontBBox", |
| 277 | SkPDFMakeArray(scaleFromFontUnits(metrics.fBBox.left(), emSize), |
| 278 | scaleFromFontUnits(metrics.fBBox.bottom(), emSize), |
| 279 | scaleFromFontUnits(metrics.fBBox.right(), emSize), |
| 280 | scaleFromFontUnits(metrics.fBBox.top(), emSize))); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 281 | if (defaultWidth > 0) { |
halcanary | 28c6d83 | 2016-08-16 10:29:38 -0700 | [diff] [blame] | 282 | descriptor->insertScalar("MissingWidth", |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 283 | scaleFromFontUnits(defaultWidth, emSize)); |
| 284 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 285 | } |
| 286 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 287 | /////////////////////////////////////////////////////////////////////////////// |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 288 | // Type0Font |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 289 | /////////////////////////////////////////////////////////////////////////////// |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 290 | |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 291 | static void emit_subset_type0(const SkPDFFont& font, SkPDFDocument* doc) { |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 292 | const SkAdvancedTypefaceMetrics* metricsPtr = |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 293 | SkPDFFont::GetMetrics(font.typeface(), doc); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 294 | SkASSERT(metricsPtr); |
| 295 | if (!metricsPtr) { return; } |
| 296 | const SkAdvancedTypefaceMetrics& metrics = *metricsPtr; |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 297 | SkASSERT(can_embed(metrics)); |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 298 | SkAdvancedTypefaceMetrics::FontType type = font.getType(); |
| 299 | SkTypeface* face = font.typeface(); |
halcanary | 28c6d83 | 2016-08-16 10:29:38 -0700 | [diff] [blame] | 300 | SkASSERT(face); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 301 | |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 302 | auto descriptor = SkPDFMakeDict("FontDescriptor"); |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 303 | uint16_t emSize = SkToU16(font.typeface()->getUnitsPerEm()); |
Ben Wagner | e91f2235 | 2019-08-30 19:28:09 -0400 | [diff] [blame] | 304 | SkPDFFont::PopulateCommonFontDescriptor(descriptor.get(), metrics, emSize, 0); |
halcanary | fe8f0e0 | 2016-07-27 14:14:04 -0700 | [diff] [blame] | 305 | |
halcanary | 426a245 | 2016-09-08 15:04:38 -0700 | [diff] [blame] | 306 | int ttcIndex; |
Ben Wagner | ff84d8a | 2019-02-26 15:39:41 -0500 | [diff] [blame] | 307 | std::unique_ptr<SkStreamAsset> fontAsset = face->openStream(&ttcIndex); |
halcanary | 426a245 | 2016-09-08 15:04:38 -0700 | [diff] [blame] | 308 | size_t fontSize = fontAsset ? fontAsset->getLength() : 0; |
halcanary | 2a4d1e1 | 2016-09-22 14:13:16 -0700 | [diff] [blame] | 309 | if (0 == fontSize) { |
| 310 | SkDebugf("Error: (SkTypeface)(%p)::openStream() returned " |
| 311 | "empty stream (%p) when identified as kType1CID_Font " |
| 312 | "or kTrueType_Font.\n", face, fontAsset.get()); |
| 313 | } else { |
halcanary | 426a245 | 2016-09-08 15:04:38 -0700 | [diff] [blame] | 314 | switch (type) { |
Ben Wagner | 45eeedd | 2024-04-08 15:59:43 -0400 | [diff] [blame] | 315 | case SkAdvancedTypefaceMetrics::kTrueType_Font: |
| 316 | case SkAdvancedTypefaceMetrics::kCFF_Font: { |
halcanary | 426a245 | 2016-09-08 15:04:38 -0700 | [diff] [blame] | 317 | if (!SkToBool(metrics.fFlags & |
| 318 | SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag)) { |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 319 | SkASSERT(font.firstGlyphID() == 1); |
Ben Wagner | 84acad6 | 2022-05-25 09:41:01 -0400 | [diff] [blame] | 320 | sk_sp<SkData> subsetFontData = SkPDFSubsetFont(*face, font.glyphUsage()); |
Hal Canary | 42d6c59 | 2018-11-15 14:01:49 -0500 | [diff] [blame] | 321 | if (subsetFontData) { |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 322 | std::unique_ptr<SkPDFDict> tmp = SkPDFMakeDict(); |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 323 | tmp->insertInt("Length1", SkToInt(subsetFontData->size())); |
| 324 | descriptor->insertRef( |
| 325 | "FontFile2", |
| 326 | SkPDFStreamOut(std::move(tmp), |
| 327 | SkMemoryStream::Make(std::move(subsetFontData)), |
Ben Wagner | 6456057 | 2022-11-17 17:18:33 -0500 | [diff] [blame] | 328 | doc, SkPDFSteamCompressionEnabled::Yes)); |
halcanary | 426a245 | 2016-09-08 15:04:38 -0700 | [diff] [blame] | 329 | break; |
| 330 | } |
| 331 | // If subsetting fails, fall back to original font data. |
halcanary | 28c6d83 | 2016-08-16 10:29:38 -0700 | [diff] [blame] | 332 | } |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 333 | std::unique_ptr<SkPDFDict> tmp = SkPDFMakeDict(); |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 334 | tmp->insertInt("Length1", fontSize); |
| 335 | descriptor->insertRef("FontFile2", |
| 336 | SkPDFStreamOut(std::move(tmp), std::move(fontAsset), |
Ben Wagner | 6456057 | 2022-11-17 17:18:33 -0500 | [diff] [blame] | 337 | doc, SkPDFSteamCompressionEnabled::Yes)); |
halcanary | 426a245 | 2016-09-08 15:04:38 -0700 | [diff] [blame] | 338 | break; |
halcanary | fe8f0e0 | 2016-07-27 14:14:04 -0700 | [diff] [blame] | 339 | } |
halcanary | 426a245 | 2016-09-08 15:04:38 -0700 | [diff] [blame] | 340 | case SkAdvancedTypefaceMetrics::kType1CID_Font: { |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 341 | std::unique_ptr<SkPDFDict> tmp = SkPDFMakeDict(); |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 342 | tmp->insertName("Subtype", "CIDFontType0C"); |
| 343 | descriptor->insertRef("FontFile3", |
| 344 | SkPDFStreamOut(std::move(tmp), std::move(fontAsset), |
Ben Wagner | 6456057 | 2022-11-17 17:18:33 -0500 | [diff] [blame] | 345 | doc, SkPDFSteamCompressionEnabled::Yes)); |
halcanary | 426a245 | 2016-09-08 15:04:38 -0700 | [diff] [blame] | 346 | break; |
halcanary | 8e3f54d | 2016-08-30 11:58:52 -0700 | [diff] [blame] | 347 | } |
halcanary | 426a245 | 2016-09-08 15:04:38 -0700 | [diff] [blame] | 348 | default: |
| 349 | SkASSERT(false); |
halcanary | 8e3f54d | 2016-08-30 11:58:52 -0700 | [diff] [blame] | 350 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 351 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 352 | |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 353 | auto newCIDFont = SkPDFMakeDict("Font"); |
Hal Canary | f34131a | 2018-12-18 15:11:03 -0500 | [diff] [blame] | 354 | newCIDFont->insertRef("FontDescriptor", doc->emit(*descriptor)); |
Hal Canary | e1fec19 | 2018-03-29 10:59:02 -0700 | [diff] [blame] | 355 | newCIDFont->insertName("BaseFont", metrics.fPostScriptName); |
halcanary | 28c6d83 | 2016-08-16 10:29:38 -0700 | [diff] [blame] | 356 | |
halcanary | 426a245 | 2016-09-08 15:04:38 -0700 | [diff] [blame] | 357 | switch (type) { |
| 358 | case SkAdvancedTypefaceMetrics::kType1CID_Font: |
| 359 | newCIDFont->insertName("Subtype", "CIDFontType0"); |
| 360 | break; |
| 361 | case SkAdvancedTypefaceMetrics::kTrueType_Font: |
Ben Wagner | 45eeedd | 2024-04-08 15:59:43 -0400 | [diff] [blame] | 362 | case SkAdvancedTypefaceMetrics::kCFF_Font: |
halcanary | 426a245 | 2016-09-08 15:04:38 -0700 | [diff] [blame] | 363 | newCIDFont->insertName("Subtype", "CIDFontType2"); |
| 364 | newCIDFont->insertName("CIDToGIDMap", "Identity"); |
| 365 | break; |
| 366 | default: |
| 367 | SkASSERT(false); |
halcanary | 462d014 | 2016-08-05 13:51:46 -0700 | [diff] [blame] | 368 | } |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 369 | auto sysInfo = SkPDFMakeDict(); |
Ben Wagner | 225c886 | 2022-05-23 17:45:13 -0400 | [diff] [blame] | 370 | // These are actually ASCII strings. |
| 371 | sysInfo->insertByteString("Registry", "Adobe"); |
| 372 | sysInfo->insertByteString("Ordering", "Identity"); |
halcanary | 28c6d83 | 2016-08-16 10:29:38 -0700 | [diff] [blame] | 373 | sysInfo->insertInt("Supplement", 0); |
| 374 | newCIDFont->insertObject("CIDSystemInfo", std::move(sysInfo)); |
| 375 | |
Ben Wagner | 53da9c5 | 2023-06-08 13:57:11 -0400 | [diff] [blame] | 376 | // Unfortunately, poppler enforces DW (default width) must be an integer. |
| 377 | int32_t defaultWidth = 0; |
halcanary | 86b6eab | 2016-08-17 07:57:27 -0700 | [diff] [blame] | 378 | { |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 379 | std::unique_ptr<SkPDFArray> widths = SkPDFMakeCIDGlyphWidthsArray( |
Ben Wagner | e91f2235 | 2019-08-30 19:28:09 -0400 | [diff] [blame] | 380 | *face, font.glyphUsage(), &defaultWidth); |
halcanary | 86b6eab | 2016-08-17 07:57:27 -0700 | [diff] [blame] | 381 | if (widths && widths->size() > 0) { |
| 382 | newCIDFont->insertObject("W", std::move(widths)); |
| 383 | } |
Ben Wagner | 53da9c5 | 2023-06-08 13:57:11 -0400 | [diff] [blame] | 384 | newCIDFont->insertInt("DW", defaultWidth); |
halcanary | 28c6d83 | 2016-08-16 10:29:38 -0700 | [diff] [blame] | 385 | } |
| 386 | |
halcanary | 28c6d83 | 2016-08-16 10:29:38 -0700 | [diff] [blame] | 387 | //////////////////////////////////////////////////////////////////////////// |
| 388 | |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 389 | SkPDFDict fontDict("Font"); |
| 390 | fontDict.insertName("Subtype", "Type0"); |
| 391 | fontDict.insertName("BaseFont", metrics.fPostScriptName); |
| 392 | fontDict.insertName("Encoding", "Identity-H"); |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 393 | auto descendantFonts = SkPDFMakeArray(); |
Hal Canary | f34131a | 2018-12-18 15:11:03 -0500 | [diff] [blame] | 394 | descendantFonts->appendRef(doc->emit(*newCIDFont)); |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 395 | fontDict.insertObject("DescendantFonts", std::move(descendantFonts)); |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 396 | |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 397 | const std::vector<SkUnichar>& glyphToUnicode = |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 398 | SkPDFFont::GetUnicodeMap(font.typeface(), doc); |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 399 | SkASSERT(SkToSizeT(font.typeface()->countGlyphs()) == glyphToUnicode.size()); |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 400 | std::unique_ptr<SkStreamAsset> toUnicode = |
| 401 | SkPDFMakeToUnicodeCmap(glyphToUnicode.data(), |
| 402 | &font.glyphUsage(), |
| 403 | font.multiByteGlyphs(), |
| 404 | font.firstGlyphID(), |
| 405 | font.lastGlyphID()); |
| 406 | fontDict.insertRef("ToUnicode", SkPDFStreamOut(nullptr, std::move(toUnicode), doc)); |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 407 | |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 408 | doc->emit(fontDict, font.indirectReference()); |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 409 | } |
| 410 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 411 | /////////////////////////////////////////////////////////////////////////////// |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 412 | // PDFType3Font |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 413 | /////////////////////////////////////////////////////////////////////////////// |
| 414 | |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 415 | namespace { |
| 416 | // returns [0, first, first+1, ... last-1, last] |
| 417 | struct SingleByteGlyphIdIterator { |
| 418 | SingleByteGlyphIdIterator(SkGlyphID first, SkGlyphID last) |
| 419 | : fFirst(first), fLast(last) { |
| 420 | SkASSERT(fFirst > 0); |
| 421 | SkASSERT(fLast >= first); |
| 422 | } |
| 423 | struct Iter { |
| 424 | void operator++() { |
| 425 | fCurrent = (0 == fCurrent) ? fFirst : fCurrent + 1; |
| 426 | } |
| 427 | // This is an input_iterator |
| 428 | SkGlyphID operator*() const { return (SkGlyphID)fCurrent; } |
| 429 | bool operator!=(const Iter& rhs) const { |
| 430 | return fCurrent != rhs.fCurrent; |
| 431 | } |
| 432 | Iter(SkGlyphID f, int c) : fFirst(f), fCurrent(c) {} |
| 433 | private: |
| 434 | const SkGlyphID fFirst; |
| 435 | int fCurrent; // must be int to make fLast+1 to fit |
| 436 | }; |
| 437 | Iter begin() const { return Iter(fFirst, 0); } |
| 438 | Iter end() const { return Iter(fFirst, (int)fLast + 1); } |
| 439 | private: |
| 440 | const SkGlyphID fFirst; |
| 441 | const SkGlyphID fLast; |
| 442 | }; |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 443 | } // namespace |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 444 | |
Hal Canary | 8ef78ea | 2018-10-01 13:38:30 -0400 | [diff] [blame] | 445 | struct ImageAndOffset { |
| 446 | sk_sp<SkImage> fImage; |
| 447 | SkIPoint fOffset; |
| 448 | }; |
Herb Derby | cb443a5 | 2019-11-11 18:01:36 -0500 | [diff] [blame] | 449 | static ImageAndOffset to_image(SkGlyphID gid, SkBulkGlyphMetricsAndImages* smallGlyphs) { |
| 450 | const SkGlyph* glyph = smallGlyphs->glyph(SkPackedGlyphID{gid}); |
herb | 252cdb7 | 2019-11-07 12:20:10 -0500 | [diff] [blame] | 451 | SkMask mask = glyph->mask(); |
Hal Canary | 8ef78ea | 2018-10-01 13:38:30 -0400 | [diff] [blame] | 452 | if (!mask.fImage) { |
| 453 | return {nullptr, {0, 0}}; |
| 454 | } |
| 455 | SkIRect bounds = mask.fBounds; |
| 456 | SkBitmap bm; |
| 457 | switch (mask.fFormat) { |
| 458 | case SkMask::kBW_Format: |
| 459 | bm.allocPixels(SkImageInfo::MakeA8(bounds.width(), bounds.height())); |
| 460 | for (int y = 0; y < bm.height(); ++y) { |
| 461 | for (int x8 = 0; x8 < bm.width(); x8 += 8) { |
| 462 | uint8_t v = *mask.getAddr1(x8 + bounds.x(), y + bounds.y()); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 463 | int e = std::min(x8 + 8, bm.width()); |
Hal Canary | 8ef78ea | 2018-10-01 13:38:30 -0400 | [diff] [blame] | 464 | for (int x = x8; x < e; ++x) { |
| 465 | *bm.getAddr8(x, y) = (v >> (x & 0x7)) & 0x1 ? 0xFF : 0x00; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | bm.setImmutable(); |
Mike Reed | dc607e3 | 2020-12-23 11:50:36 -0500 | [diff] [blame] | 470 | return {bm.asImage(), {bounds.x(), bounds.y()}}; |
Hal Canary | 8ef78ea | 2018-10-01 13:38:30 -0400 | [diff] [blame] | 471 | case SkMask::kA8_Format: |
Ben Wagner | 6cf423f | 2023-08-01 16:05:08 -0400 | [diff] [blame] | 472 | return {SkImages::RasterFromData( |
| 473 | SkImageInfo::MakeA8(bounds.width(), bounds.height()), |
| 474 | SkData::MakeWithCopy(mask.fImage, mask.computeTotalImageSize()), |
| 475 | mask.fRowBytes), |
Hal Canary | 8ef78ea | 2018-10-01 13:38:30 -0400 | [diff] [blame] | 476 | {bounds.x(), bounds.y()}}; |
| 477 | case SkMask::kARGB32_Format: |
Ben Wagner | 6cf423f | 2023-08-01 16:05:08 -0400 | [diff] [blame] | 478 | return {SkImages::RasterFromData( |
| 479 | SkImageInfo::MakeN32Premul(bounds.width(), bounds.height()), |
| 480 | SkData::MakeWithCopy(mask.fImage, mask.computeTotalImageSize()), |
| 481 | mask.fRowBytes), |
Hal Canary | 8ef78ea | 2018-10-01 13:38:30 -0400 | [diff] [blame] | 482 | {bounds.x(), bounds.y()}}; |
| 483 | case SkMask::k3D_Format: |
| 484 | case SkMask::kLCD16_Format: |
| 485 | default: |
| 486 | SkASSERT(false); |
| 487 | return {nullptr, {0, 0}}; |
| 488 | } |
| 489 | } |
Hal Canary | b3907a8 | 2018-12-17 09:53:30 -0500 | [diff] [blame] | 490 | |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 491 | static SkPDFIndirectReference type3_descriptor(SkPDFDocument* doc, |
| 492 | const SkTypeface* typeface, |
Herb Derby | cb443a5 | 2019-11-11 18:01:36 -0500 | [diff] [blame] | 493 | SkScalar xHeight) { |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 494 | if (SkPDFIndirectReference* ptr = doc->fType3FontDescriptors.find(typeface->uniqueID())) { |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 495 | return *ptr; |
| 496 | } |
| 497 | |
| 498 | SkPDFDict descriptor("FontDescriptor"); |
| 499 | int32_t fontDescriptorFlags = kPdfSymbolic; |
Ben Wagner | 4f6bdf8 | 2024-04-08 17:08:27 -0400 | [diff] [blame] | 500 | |
| 501 | /** PDF32000_2008: FontFamily should be used for Type3 fonts in Tagged PDF documents. */ |
| 502 | SkString familyName; |
| 503 | typeface->getFamilyName(&familyName); |
| 504 | if (!familyName.isEmpty()) { |
| 505 | descriptor.insertByteString("FontFamily", familyName); |
| 506 | } |
| 507 | |
| 508 | /** PDF32000_2008: FontStretch should be used for Type3 fonts in Tagged PDF documents. */ |
| 509 | static constexpr const char* stretchNames[9] = { |
| 510 | "UltraCondensed", |
| 511 | "ExtraCondensed", |
| 512 | "Condensed", |
| 513 | "SemiCondensed", |
| 514 | "Normal", |
| 515 | "SemiExpanded", |
| 516 | "Expanded", |
| 517 | "ExtraExpanded", |
| 518 | "UltraExpanded", |
| 519 | }; |
| 520 | const char* stretchName = stretchNames[typeface->fontStyle().width() - 1]; |
| 521 | descriptor.insertName("FontStretch", stretchName); |
| 522 | |
| 523 | /** PDF32000_2008: FontWeight should be used for Type3 fonts in Tagged PDF documents. */ |
| 524 | int weight = (typeface->fontStyle().weight() + 50) / 100; |
| 525 | descriptor.insertInt("FontWeight", SkTPin(weight, 1, 9) * 100); |
| 526 | |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 527 | if (const SkAdvancedTypefaceMetrics* metrics = SkPDFFont::GetMetrics(typeface, doc)) { |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 528 | // Type3 FontDescriptor does not require all the same fields. |
| 529 | descriptor.insertName("FontName", metrics->fPostScriptName); |
| 530 | descriptor.insertInt("ItalicAngle", metrics->fItalicAngle); |
| 531 | fontDescriptorFlags |= (int32_t)metrics->fStyle; |
| 532 | // Adobe requests CapHeight, XHeight, and StemV be added |
| 533 | // to "greatly help our workflow downstream". |
| 534 | if (metrics->fCapHeight != 0) { descriptor.insertInt("CapHeight", metrics->fCapHeight); } |
| 535 | if (metrics->fStemV != 0) { descriptor.insertInt("StemV", metrics->fStemV); } |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 536 | if (xHeight != 0) { |
| 537 | descriptor.insertScalar("XHeight", xHeight); |
| 538 | } |
| 539 | } |
| 540 | descriptor.insertInt("Flags", fontDescriptorFlags); |
| 541 | SkPDFIndirectReference ref = doc->emit(descriptor); |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 542 | doc->fType3FontDescriptors.set(typeface->uniqueID(), ref); |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 543 | return ref; |
| 544 | } |
| 545 | |
Hal Canary | 5c41f36 | 2019-09-30 10:54:54 -0400 | [diff] [blame] | 546 | #ifdef SK_PDF_BITMAP_GLYPH_RASTER_SIZE |
| 547 | static constexpr float kBitmapFontSize = SK_PDF_BITMAP_GLYPH_RASTER_SIZE; |
| 548 | #else |
| 549 | static constexpr float kBitmapFontSize = 64; |
| 550 | #endif |
| 551 | |
| 552 | SkStrikeSpec make_small_strike(const SkTypeface& typeface) { |
| 553 | SkFont font(sk_ref_sp(&typeface), kBitmapFontSize); |
| 554 | font.setHinting(SkFontHinting::kNone); |
| 555 | font.setEdging(SkFont::Edging::kAlias); |
| 556 | return SkStrikeSpec::MakeMask(font, |
| 557 | SkPaint(), |
Kurt Catti-Schmidt (SCHMIDT) | 0e0e00e | 2023-12-12 16:46:43 -0800 | [diff] [blame] | 558 | SkSurfaceProps(), |
Ben Wagner | 6c630f2 | 2022-03-07 23:06:02 -0500 | [diff] [blame] | 559 | SkScalerContextFlags::kFakeGammaAndBoostContrast, |
Hal Canary | 5c41f36 | 2019-09-30 10:54:54 -0400 | [diff] [blame] | 560 | SkMatrix::I()); |
| 561 | } |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 562 | |
| 563 | static void emit_subset_type3(const SkPDFFont& pdfFont, SkPDFDocument* doc) { |
| 564 | SkTypeface* typeface = pdfFont.typeface(); |
| 565 | SkGlyphID firstGlyphID = pdfFont.firstGlyphID(); |
| 566 | SkGlyphID lastGlyphID = pdfFont.lastGlyphID(); |
| 567 | const SkPDFGlyphUse& subset = pdfFont.glyphUsage(); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 568 | SkASSERT(lastGlyphID >= firstGlyphID); |
halcanary | 1748432 | 2016-08-29 09:47:48 -0700 | [diff] [blame] | 569 | // Remove unused glyphs at the end of the range. |
| 570 | // Keep the lastGlyphID >= firstGlyphID invariant true. |
| 571 | while (lastGlyphID > firstGlyphID && !subset.has(lastGlyphID)) { |
| 572 | --lastGlyphID; |
| 573 | } |
Hal Canary | aa3af7b | 2017-03-06 16:18:49 -0500 | [diff] [blame] | 574 | int unitsPerEm; |
Herb Derby | 36a54c1 | 2019-06-06 10:50:56 -0400 | [diff] [blame] | 575 | SkStrikeSpec strikeSpec = SkStrikeSpec::MakePDFVector(*typeface, &unitsPerEm); |
Herb Derby | a64f5b2 | 2020-02-24 14:35:51 -0500 | [diff] [blame] | 576 | auto strike = strikeSpec.findOrCreateStrike(); |
| 577 | SkASSERT(strike); |
Hal Canary | aa3af7b | 2017-03-06 16:18:49 -0500 | [diff] [blame] | 578 | SkScalar emSize = (SkScalar)unitsPerEm; |
Herb Derby | a64f5b2 | 2020-02-24 14:35:51 -0500 | [diff] [blame] | 579 | SkScalar xHeight = strike->getFontMetrics().fXHeight; |
Ben Wagner | 4e8b235 | 2023-01-31 17:00:56 -0500 | [diff] [blame] | 580 | SkBulkGlyphMetricsAndPaths metricsAndPaths((sk_sp<SkStrike>(strike))); |
| 581 | SkBulkGlyphMetricsAndDrawables metricsAndDrawables(std::move(strike)); |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 582 | |
Hal Canary | 5c41f36 | 2019-09-30 10:54:54 -0400 | [diff] [blame] | 583 | SkStrikeSpec strikeSpecSmall = kBitmapFontSize > 0 ? make_small_strike(*typeface) |
| 584 | : strikeSpec; |
Herb Derby | cb443a5 | 2019-11-11 18:01:36 -0500 | [diff] [blame] | 585 | |
| 586 | SkBulkGlyphMetricsAndImages smallGlyphs(strikeSpecSmall); |
Hal Canary | 5c41f36 | 2019-09-30 10:54:54 -0400 | [diff] [blame] | 587 | float bitmapScale = kBitmapFontSize > 0 ? emSize / kBitmapFontSize : 1.0f; |
| 588 | |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 589 | SkPDFDict font("Font"); |
| 590 | font.insertName("Subtype", "Type3"); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 591 | // Flip about the x-axis and scale by 1/emSize. |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 592 | SkMatrix fontMatrix; |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 593 | fontMatrix.setScale(SkScalarInvert(emSize), -SkScalarInvert(emSize)); |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 594 | font.insertObject("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix)); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 595 | |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 596 | auto charProcs = SkPDFMakeDict(); |
| 597 | auto encoding = SkPDFMakeDict("Encoding"); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 598 | |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 599 | auto encDiffs = SkPDFMakeArray(); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 600 | // length(firstGlyphID .. lastGlyphID) == lastGlyphID - firstGlyphID + 1 |
| 601 | // plus 1 for glyph 0; |
| 602 | SkASSERT(firstGlyphID > 0); |
| 603 | SkASSERT(lastGlyphID >= firstGlyphID); |
| 604 | int glyphCount = lastGlyphID - firstGlyphID + 2; |
| 605 | // one other entry for the index of first glyph. |
| 606 | encDiffs->reserve(glyphCount + 1); |
| 607 | encDiffs->appendInt(0); // index of first glyph |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 608 | |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 609 | auto widthArray = SkPDFMakeArray(); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 610 | widthArray->reserve(glyphCount); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 611 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 612 | SkIRect bbox = SkIRect::MakeEmpty(); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 613 | |
Hal Canary | b3907a8 | 2018-12-17 09:53:30 -0500 | [diff] [blame] | 614 | std::vector<std::pair<SkGlyphID, SkPDFIndirectReference>> imageGlyphs; |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 615 | for (SkGlyphID gID : SingleByteGlyphIdIterator(firstGlyphID, lastGlyphID)) { |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 616 | bool skipGlyph = gID != 0 && !subset.has(gID); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 617 | SkString characterName; |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 618 | SkScalar advance = 0.0f; |
| 619 | SkIRect glyphBBox; |
| 620 | if (skipGlyph) { |
| 621 | characterName.set("g0"); |
| 622 | } else { |
| 623 | characterName.printf("g%X", gID); |
Ben Wagner | 4e8b235 | 2023-01-31 17:00:56 -0500 | [diff] [blame] | 624 | const SkGlyph* pathGlyph = metricsAndPaths.glyph(gID); |
| 625 | const SkGlyph* drawableGlyph = metricsAndDrawables.glyph(gID); |
| 626 | advance = pathGlyph->advanceX(); |
| 627 | glyphBBox = pathGlyph->iRect(); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 628 | bbox.join(glyphBBox); |
Ben Wagner | 4e8b235 | 2023-01-31 17:00:56 -0500 | [diff] [blame] | 629 | const SkPath* path = pathGlyph->path(); |
| 630 | SkDrawable* drawable = drawableGlyph->drawable(); |
Hal Canary | b3907a8 | 2018-12-17 09:53:30 -0500 | [diff] [blame] | 631 | SkDynamicMemoryWStream content; |
Ben Wagner | 4e8b235 | 2023-01-31 17:00:56 -0500 | [diff] [blame] | 632 | if (drawable && !drawable->getBounds().isEmpty()) { |
| 633 | sk_sp<SkPDFDevice> glyphDevice = sk_make_sp<SkPDFDevice>(glyphBBox.size(), doc); |
| 634 | SkCanvas canvas(glyphDevice); |
| 635 | canvas.translate(-glyphBBox.fLeft, -glyphBBox.fTop); |
| 636 | canvas.drawDrawable(drawable); |
| 637 | SkPDFIndirectReference xobject = SkPDFMakeFormXObject( |
| 638 | doc, glyphDevice->content(), |
| 639 | SkPDFMakeArray(0, 0, glyphBBox.width(), glyphBBox.height()), |
| 640 | glyphDevice->makeResourceDict(), |
| 641 | SkMatrix::Translate(glyphBBox.fLeft, glyphBBox.fTop), nullptr); |
| 642 | imageGlyphs.emplace_back(gID, xobject); |
| 643 | SkPDFUtils::AppendScalar(drawableGlyph->advanceX(), &content); |
| 644 | content.writeText(" 0 d0\n1 0 0 1 0 0 cm\n/X"); |
| 645 | content.write(characterName.c_str(), characterName.size()); |
| 646 | content.writeText(" Do\n"); |
| 647 | } else if (path && !path->isEmpty()) { |
| 648 | setGlyphWidthAndBoundingBox(pathGlyph->advanceX(), glyphBBox, &content); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 649 | SkPDFUtils::EmitPath(*path, SkPaint::kFill_Style, &content); |
Mike Reed | cf0e3c6 | 2019-12-03 16:26:15 -0500 | [diff] [blame] | 650 | SkPDFUtils::PaintPath(SkPaint::kFill_Style, path->getFillType(), &content); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 651 | } else { |
Herb Derby | cb443a5 | 2019-11-11 18:01:36 -0500 | [diff] [blame] | 652 | auto pimg = to_image(gID, &smallGlyphs); |
Hal Canary | 8ef78ea | 2018-10-01 13:38:30 -0400 | [diff] [blame] | 653 | if (!pimg.fImage) { |
Ben Wagner | 4e8b235 | 2023-01-31 17:00:56 -0500 | [diff] [blame] | 654 | setGlyphWidthAndBoundingBox(pathGlyph->advanceX(), glyphBBox, &content); |
Hal Canary | 8ef78ea | 2018-10-01 13:38:30 -0400 | [diff] [blame] | 655 | } else { |
Hal Canary | 5c41f36 | 2019-09-30 10:54:54 -0400 | [diff] [blame] | 656 | using SkPDFUtils::AppendScalar; |
Hal Canary | b3907a8 | 2018-12-17 09:53:30 -0500 | [diff] [blame] | 657 | imageGlyphs.emplace_back(gID, SkPDFSerializeImage(pimg.fImage.get(), doc)); |
Ben Wagner | 4e8b235 | 2023-01-31 17:00:56 -0500 | [diff] [blame] | 658 | AppendScalar(pathGlyph->advanceX(), &content); |
Hal Canary | 8ef78ea | 2018-10-01 13:38:30 -0400 | [diff] [blame] | 659 | content.writeText(" 0 d0\n"); |
Hal Canary | 5c41f36 | 2019-09-30 10:54:54 -0400 | [diff] [blame] | 660 | AppendScalar(pimg.fImage->width() * bitmapScale, &content); |
Hal Canary | 8ef78ea | 2018-10-01 13:38:30 -0400 | [diff] [blame] | 661 | content.writeText(" 0 0 "); |
Hal Canary | 5c41f36 | 2019-09-30 10:54:54 -0400 | [diff] [blame] | 662 | AppendScalar(-pimg.fImage->height() * bitmapScale, &content); |
Hal Canary | 8ef78ea | 2018-10-01 13:38:30 -0400 | [diff] [blame] | 663 | content.writeText(" "); |
Hal Canary | 5c41f36 | 2019-09-30 10:54:54 -0400 | [diff] [blame] | 664 | AppendScalar(pimg.fOffset.x() * bitmapScale, &content); |
Hal Canary | 8ef78ea | 2018-10-01 13:38:30 -0400 | [diff] [blame] | 665 | content.writeText(" "); |
Hal Canary | 5c41f36 | 2019-09-30 10:54:54 -0400 | [diff] [blame] | 666 | AppendScalar((pimg.fImage->height() + pimg.fOffset.y()) * bitmapScale, |
| 667 | &content); |
Hal Canary | b3907a8 | 2018-12-17 09:53:30 -0500 | [diff] [blame] | 668 | content.writeText(" cm\n/X"); |
| 669 | content.write(characterName.c_str(), characterName.size()); |
| 670 | content.writeText(" Do\n"); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 671 | } |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 672 | } |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 673 | charProcs->insertRef(characterName, SkPDFStreamOut(nullptr, |
| 674 | content.detachAsStream(), doc)); |
halcanary | 5bf60ad | 2016-08-11 13:59:18 -0700 | [diff] [blame] | 675 | } |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 676 | encDiffs->appendName(std::move(characterName)); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 677 | widthArray->appendScalar(advance); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Hal Canary | b3907a8 | 2018-12-17 09:53:30 -0500 | [diff] [blame] | 680 | if (!imageGlyphs.empty()) { |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 681 | auto d0 = SkPDFMakeDict(); |
Hal Canary | b3907a8 | 2018-12-17 09:53:30 -0500 | [diff] [blame] | 682 | for (const auto& pair : imageGlyphs) { |
| 683 | d0->insertRef(SkStringPrintf("Xg%X", pair.first), pair.second); |
| 684 | } |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 685 | auto d1 = SkPDFMakeDict(); |
Hal Canary | b3907a8 | 2018-12-17 09:53:30 -0500 | [diff] [blame] | 686 | d1->insertObject("XObject", std::move(d0)); |
| 687 | font.insertObject("Resources", std::move(d1)); |
| 688 | } |
| 689 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 690 | encoding->insertObject("Differences", std::move(encDiffs)); |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 691 | font.insertInt("FirstChar", 0); |
| 692 | font.insertInt("LastChar", lastGlyphID - firstGlyphID + 1); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 693 | /* FontBBox: "A rectangle expressed in the glyph coordinate |
| 694 | system, specifying the font bounding box. This is the smallest |
| 695 | rectangle enclosing the shape that would result if all of the |
| 696 | glyphs of the font were placed with their origins coincident and |
| 697 | then filled." */ |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 698 | font.insertObject("FontBBox", SkPDFMakeArray(bbox.left(), |
Hal Canary | e650b85 | 2018-09-12 09:12:36 -0400 | [diff] [blame] | 699 | bbox.bottom(), |
| 700 | bbox.right(), |
| 701 | bbox.top())); |
| 702 | |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 703 | font.insertName("CIDToGIDMap", "Identity"); |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 704 | |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 705 | const std::vector<SkUnichar>& glyphToUnicode = SkPDFFont::GetUnicodeMap(typeface, doc); |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 706 | SkASSERT(glyphToUnicode.size() == SkToSizeT(typeface->countGlyphs())); |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 707 | auto toUnicodeCmap = SkPDFMakeToUnicodeCmap(glyphToUnicode.data(), |
| 708 | &subset, |
| 709 | false, |
| 710 | firstGlyphID, |
| 711 | lastGlyphID); |
| 712 | font.insertRef("ToUnicode", SkPDFStreamOut(nullptr, std::move(toUnicodeCmap), doc)); |
Herb Derby | cb443a5 | 2019-11-11 18:01:36 -0500 | [diff] [blame] | 713 | font.insertRef("FontDescriptor", type3_descriptor(doc, typeface, xHeight)); |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 714 | font.insertObject("Widths", std::move(widthArray)); |
| 715 | font.insertObject("Encoding", std::move(encoding)); |
| 716 | font.insertObject("CharProcs", std::move(charProcs)); |
| 717 | |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 718 | doc->emit(font, pdfFont.indirectReference()); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 719 | } |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 720 | |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 721 | void SkPDFFont::emitSubset(SkPDFDocument* doc) const { |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 722 | switch (fFontType) { |
| 723 | case SkAdvancedTypefaceMetrics::kType1CID_Font: |
| 724 | case SkAdvancedTypefaceMetrics::kTrueType_Font: |
Ben Wagner | 45eeedd | 2024-04-08 15:59:43 -0400 | [diff] [blame] | 725 | case SkAdvancedTypefaceMetrics::kCFF_Font: |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 726 | return emit_subset_type0(*this, doc); |
Hal Canary | 2e904bc | 2019-04-18 15:40:27 -0400 | [diff] [blame] | 727 | #ifndef SK_PDF_DO_NOT_SUPPORT_TYPE_1_FONTS |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 728 | case SkAdvancedTypefaceMetrics::kType1_Font: |
Hal Canary | 2e904bc | 2019-04-18 15:40:27 -0400 | [diff] [blame] | 729 | return SkPDFEmitType1Font(*this, doc); |
| 730 | #endif |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 731 | default: |
| 732 | return emit_subset_type3(*this, doc); |
| 733 | } |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 734 | } |
| 735 | |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 736 | //////////////////////////////////////////////////////////////////////////////// |
| 737 | |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 738 | bool SkPDFFont::CanEmbedTypeface(SkTypeface* typeface, SkPDFDocument* doc) { |
| 739 | const SkAdvancedTypefaceMetrics* metrics = SkPDFFont::GetMetrics(typeface, doc); |
halcanary | cee1342 | 2016-08-18 09:52:48 -0700 | [diff] [blame] | 740 | return metrics && can_embed(*metrics); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 741 | } |
halcanary | bae235e | 2016-03-21 10:05:23 -0700 | [diff] [blame] | 742 | |