| 1 |
/* |
| 2 |
* Copyright 2011 Google Inc. |
| 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 "src/pdf/SkPDFFont.h" |
| 9 |
|
| 10 |
#include "include/codec/SkCodec.h" |
| 11 |
#include "include/core/SkAlphaType.h" |
| 12 |
#include "include/core/SkBitmap.h" |
| 13 |
#include "include/core/SkCanvas.h" |
| 14 |
#include "include/core/SkColorType.h" |
| 15 |
#include "include/core/SkData.h" |
| 16 |
#include "include/core/SkDrawable.h" |
| 17 |
#include "include/core/SkFont.h" |
| 18 |
#include "include/core/SkFontMetrics.h" |
| 19 |
#include "include/core/SkFontStyle.h" |
| 20 |
#include "include/core/SkFontTypes.h" |
| 21 |
#include "include/core/SkImage.h" |
| 22 |
#include "include/core/SkImageInfo.h" |
| 23 |
#include "include/core/SkMaskFilter.h" |
| 24 |
#include "include/core/SkMatrix.h" |
| 25 |
#include "include/core/SkPaint.h" |
| 26 |
#include "include/core/SkPath.h" |
| 27 |
#include "include/core/SkPathTypes.h" |
| 28 |
#include "include/core/SkPixmap.h" |
| 29 |
#include "include/core/SkPoint.h" |
| 30 |
#include "include/core/SkRect.h" |
| 31 |
#include "include/core/SkRefCnt.h" |
| 32 |
#include "include/core/SkScalar.h" |
| 33 |
#include "include/core/SkSize.h" |
| 34 |
#include "include/core/SkSpan.h" |
| 35 |
#include "include/core/SkStream.h" |
| 36 |
#include "include/core/SkString.h" |
| 37 |
#include "include/core/SkTypeface.h" |
| 38 |
#include "include/docs/SkPDFDocument.h" |
| 39 |
#include "include/effects/SkDashPathEffect.h" |
| 40 |
#include "include/private/base/SkDebug.h" |
| 41 |
#include "include/private/base/SkTPin.h" |
| 42 |
#include "include/private/base/SkTemplates.h" |
| 43 |
#include "include/private/base/SkTo.h" |
| 44 |
#include "src/base/SkBitmaskEnum.h" |
| 45 |
#include "src/core/SkDescriptor.h" |
| 46 |
#include "src/core/SkDevice.h" |
| 47 |
#include "src/core/SkGlyph.h" |
| 48 |
#include "src/core/SkMask.h" |
| 49 |
#include "src/core/SkMaskFilterBase.h" |
| 50 |
#include "src/core/SkPathEffectBase.h" |
| 51 |
#include "src/core/SkStrike.h" |
| 52 |
#include "src/core/SkStrikeSpec.h" |
| 53 |
#include "src/core/SkTHash.h" |
| 54 |
#include "src/pdf/SkPDFBitmap.h" |
| 55 |
#include "src/pdf/SkPDFDevice.h" |
| 56 |
#include "src/pdf/SkPDFDocumentPriv.h" |
| 57 |
#include "src/pdf/SkPDFFormXObject.h" |
| 58 |
#include "src/pdf/SkPDFGraphicState.h" |
| 59 |
#include "src/pdf/SkPDFMakeCIDGlyphWidthsArray.h" |
| 60 |
#include "src/pdf/SkPDFMakeToUnicodeCmap.h" |
| 61 |
#include "src/pdf/SkPDFSubsetFont.h" |
| 62 |
#include "src/pdf/SkPDFType1Font.h" |
| 63 |
#include "src/pdf/SkPDFUtils.h" |
| 64 |
|
| 65 |
#include <limits.h> |
| 66 |
#include <algorithm> |
| 67 |
#include <cstddef> |
| 68 |
#include <initializer_list> |
| 69 |
#include <memory> |
| 70 |
#include <optional> |
| 71 |
#include <utility> |
| 72 |
|
| 73 |
using namespace skia_private; |
| 74 |
|
| 75 |
void SkPDFFont::GetType1GlyphNames(const SkTypeface& face, SkString* dst) { |
| 76 |
face.getPostScriptGlyphNames(dst); |
| 77 |
} |
| 78 |
|
| 79 |
namespace { |
| 80 |
// PDF's notion of symbolic vs non-symbolic is related to the character set, not |
| 81 |
// symbols vs. characters. Rarely is a font the right character set to call it |
| 82 |
// non-symbolic, so always call it symbolic. (PDF 1.4 spec, section 5.7.1) |
| 83 |
static const int32_t kPdfSymbolic = 4; |
| 84 |
|
| 85 |
// scale from em-units to base-1000, returning as a SkScalar |
| 86 |
inline SkScalar from_font_units(SkScalar scaled, uint16_t emSize) { |
| 87 |
return emSize == 1000 ? scaled : scaled * 1000 / emSize; |
| 88 |
} |
| 89 |
|
| 90 |
inline SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) { |
| 91 |
return from_font_units(SkIntToScalar(val), emSize); |
| 92 |
} |
| 93 |
|
| 94 |
void setGlyphWidthAndBoundingBox(SkScalar width, SkIRect box, |
| 95 |
SkDynamicMemoryWStream* content) { |
| 96 |
// Specify width and bounding box for the glyph. |
| 97 |
SkPDFUtils::AppendScalar(width, content); |
| 98 |
content->writeText(" 0 "); |
| 99 |
content->writeDecAsText(box.fLeft); |
| 100 |
content->writeText(" "); |
| 101 |
content->writeDecAsText(box.fTop); |
| 102 |
content->writeText(" "); |
| 103 |
content->writeDecAsText(box.fRight); |
| 104 |
content->writeText(" "); |
| 105 |
content->writeDecAsText(box.fBottom); |
| 106 |
content->writeText(" d1\n"); |
| 107 |
} |
| 108 |
|
| 109 |
} // namespace |
| 110 |
|
| 111 |
static bool scale_paint(SkPaint& paint, SkScalar fontToEMScale) { |
| 112 |
// What we really want here is a way ask the path effect or mask filter for a scaled |
| 113 |
// version of itself (if it is linearly scalable). |
| 114 |
|
| 115 |
if (SkMaskFilterBase* mfb = as_MFB(paint.getMaskFilter())) { |
| 116 |
SkMaskFilterBase::BlurRec blurRec; |
| 117 |
if (mfb->asABlur(&blurRec)) { |
| 118 |
// asABlur returns false if ignoring the CTM |
| 119 |
blurRec.fSigma *= fontToEMScale; |
| 120 |
paint.setMaskFilter(SkMaskFilter::MakeBlur(blurRec.fStyle, blurRec.fSigma, true)); |
| 121 |
} else { |
| 122 |
return false; |
| 123 |
} |
| 124 |
} |
| 125 |
if (SkPathEffectBase* peb = as_PEB(paint.getPathEffect())) { |
| 126 |
if (auto dashInfo = peb->asADash()) { |
| 127 |
SkSpan<const SkScalar> src = dashInfo->fIntervals; |
| 128 |
AutoSTArray<4, SkScalar> dst(src.size()); |
| 129 |
for (size_t i = 0; i < src.size(); ++i) { |
| 130 |
dst[i] = src[i] * fontToEMScale; |
| 131 |
} |
| 132 |
paint.setPathEffect(SkDashPathEffect::Make(dst, dashInfo->fPhase * fontToEMScale)); |
| 133 |
} else { |
| 134 |
return false; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
if (paint.getStyle() != SkPaint::kFill_Style && paint.getStrokeWidth() > 0) { |
| 139 |
paint.setStrokeWidth(paint.getStrokeWidth() * fontToEMScale); |
| 140 |
} |
| 141 |
|
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
SkPDFStrikeSpec::SkPDFStrikeSpec(SkStrikeSpec strikeSpec, SkScalar em) |
| 147 |
: fStrikeSpec(std::move(strikeSpec)) |
| 148 |
, fUnitsPerEM(em) |
| 149 |
{} |
| 150 |
|
| 151 |
sk_sp<SkPDFStrike> SkPDFStrike::Make(SkPDFDocument* doc, const SkFont& font, const SkPaint& paint) { |
| 152 |
#ifdef SK_PDF_BITMAP_GLYPH_RASTER_SIZE |
| 153 |
static constexpr float kBitmapFontSize = SK_PDF_BITMAP_GLYPH_RASTER_SIZE; |
| 154 |
#else |
| 155 |
static constexpr float kBitmapFontSize = 64; |
| 156 |
#endif |
| 157 |
|
| 158 |
SkScalar unitsPerEm = static_cast<SkScalar>(font.getTypeface()->getUnitsPerEm()); |
| 159 |
SkASSERT(0 < unitsPerEm); |
| 160 |
|
| 161 |
SkFont canonFont(font); |
| 162 |
canonFont.setBaselineSnap(false); // canonicalize |
| 163 |
canonFont.setEdging(SkFont::Edging::kAntiAlias); // canonicalize |
| 164 |
canonFont.setEmbeddedBitmaps(false); // canonicalize |
| 165 |
//canonFont.setEmbolden(); // applied by scaler context, sets glyph path to modified |
| 166 |
canonFont.setForceAutoHinting(false); // canonicalize |
| 167 |
canonFont.setHinting(SkFontHinting::kNone); // canonicalize |
| 168 |
canonFont.setLinearMetrics(true); // canonicalize |
| 169 |
canonFont.setScaleX(1.0f); // original value applied by SkPDFDevice |
| 170 |
//canonFont.setSize(unitsPerEm); // canonicalize below, adjusted by SkPDFDevice |
| 171 |
canonFont.setSkewX(0.0f); // original value applied by SkPDFDevice |
| 172 |
canonFont.setSubpixel(false); // canonicalize |
| 173 |
//canonFont.setTypeface(); |
| 174 |
|
| 175 |
SkPaint pathPaint(paint); |
| 176 |
if (scale_paint(pathPaint, unitsPerEm / font.getSize())) { |
| 177 |
canonFont.setSize(unitsPerEm); |
| 178 |
} else { |
| 179 |
canonFont.setSize(font.getSize()); |
| 180 |
} |
| 181 |
SkScalar pathStrikeEM = canonFont.getSize(); |
| 182 |
SkStrikeSpec pathStrikeSpec = SkStrikeSpec::MakeWithNoDevice(canonFont, &pathPaint, |
| 183 |
SkScalerContextFlags::kNone); |
| 184 |
|
| 185 |
if (sk_sp<SkPDFStrike>* strike = doc->fStrikes.find(pathStrikeSpec.descriptor())) { |
| 186 |
return *strike; |
| 187 |
} |
| 188 |
|
| 189 |
if (kBitmapFontSize <= 0) { |
| 190 |
// old code path compatibility |
| 191 |
sk_sp<SkPDFStrike> strike(new SkPDFStrike(SkPDFStrikeSpec(pathStrikeSpec, pathStrikeEM), |
| 192 |
SkPDFStrikeSpec(pathStrikeSpec, pathStrikeEM), |
| 193 |
pathPaint.getMaskFilter(), doc)); |
| 194 |
doc->fStrikes.set(strike); |
| 195 |
return strike; |
| 196 |
} |
| 197 |
|
| 198 |
SkPaint imagePaint(paint); |
| 199 |
if (scale_paint(imagePaint, kBitmapFontSize / font.getSize())) { |
| 200 |
canonFont.setSize(kBitmapFontSize); |
| 201 |
} else { |
| 202 |
canonFont.setSize(font.getSize()); |
| 203 |
} |
| 204 |
SkScalar imageStrikeEM = canonFont.getSize(); |
| 205 |
SkStrikeSpec imageStrikeSpec = SkStrikeSpec::MakeWithNoDevice(canonFont, &imagePaint, |
| 206 |
SkScalerContextFlags::kNone); |
| 207 |
|
| 208 |
sk_sp<SkPDFStrike> strike(new SkPDFStrike(SkPDFStrikeSpec(pathStrikeSpec, pathStrikeEM), |
| 209 |
SkPDFStrikeSpec(imageStrikeSpec, imageStrikeEM), |
| 210 |
pathPaint.getMaskFilter(), doc)); |
| 211 |
doc->fStrikes.set(strike); |
| 212 |
return strike; |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
SkPDFStrike::SkPDFStrike(SkPDFStrikeSpec path, SkPDFStrikeSpec image, bool hasMaskFilter, |
| 217 |
SkPDFDocument* doc) |
| 218 |
: fPath(std::move(path)) |
| 219 |
, fImage(std::move(image)) |
| 220 |
, fHasMaskFilter(hasMaskFilter) |
| 221 |
, fDoc(doc) |
| 222 |
{ |
| 223 |
SkASSERT(fDoc); |
| 224 |
} |
| 225 |
|
| 226 |
const SkDescriptor& SkPDFStrike::Traits::GetKey(const sk_sp<SkPDFStrike>& strike) { |
| 227 |
return strike->fPath.fStrikeSpec.descriptor(); |
| 228 |
} |
| 229 |
uint32_t SkPDFStrike::Traits::Hash(const SkDescriptor& descriptor) { |
| 230 |
return descriptor.getChecksum(); |
| 231 |
} |
| 232 |
|
| 233 |
/////////////////////////////////////////////////////////////////////////////// |
| 234 |
// class SkPDFFont |
| 235 |
/////////////////////////////////////////////////////////////////////////////// |
| 236 |
|
| 237 |
/* Resources are canonicalized and uniqueified by pointer so there has to be |
| 238 |
* some additional state indicating which subset of the font is used. It |
| 239 |
* must be maintained at the document granularity. |
| 240 |
*/ |
| 241 |
|
| 242 |
SkPDFFont::~SkPDFFont() = default; |
| 243 |
|
| 244 |
SkPDFFont::SkPDFFont(SkPDFFont&&) = default; |
| 245 |
|
| 246 |
static bool can_embed(const SkAdvancedTypefaceMetrics& metrics) { |
| 247 |
return !SkToBool(metrics.fFlags & SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag); |
| 248 |
} |
| 249 |
|
| 250 |
static bool can_subset(const SkAdvancedTypefaceMetrics& metrics) { |
| 251 |
return !SkToBool(metrics.fFlags & SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag); |
| 252 |
} |
| 253 |
|
| 254 |
const SkAdvancedTypefaceMetrics* SkPDFFont::GetMetrics(const SkTypeface& typeface, |
| 255 |
SkPDFDocument* canon) { |
| 256 |
SkTypefaceID id = typeface.uniqueID(); |
| 257 |
if (std::unique_ptr<SkAdvancedTypefaceMetrics>* ptr = canon->fTypefaceMetrics.find(id)) { |
| 258 |
return ptr->get(); // canon retains ownership. |
| 259 |
} |
| 260 |
|
| 261 |
int count = typeface.countGlyphs(); |
| 262 |
if (count <= 0 || count > 1 + SkTo<int>(UINT16_MAX)) { |
| 263 |
// Cache nullptr to skip this check. Use SkSafeUnref(). |
| 264 |
canon->fTypefaceMetrics.set(id, nullptr); |
| 265 |
return nullptr; |
| 266 |
} |
| 267 |
|
| 268 |
std::unique_ptr<SkAdvancedTypefaceMetrics> metrics = typeface.getAdvancedMetrics(); |
| 269 |
if (!metrics) { |
| 270 |
metrics = std::make_unique<SkAdvancedTypefaceMetrics>(); |
| 271 |
} |
| 272 |
if (0 == metrics->fStemV || 0 == metrics->fCapHeight) { |
| 273 |
SkFont font; |
| 274 |
font.setHinting(SkFontHinting::kNone); |
| 275 |
font.setTypeface(sk_ref_sp(&typeface)); |
| 276 |
font.setSize(1000); // glyph coordinate system |
| 277 |
if (0 == metrics->fStemV) { |
| 278 |
// Figure out a good guess for StemV - Min width of i, I, !, 1. |
| 279 |
// This probably isn't very good with an italic font. |
| 280 |
int16_t stemV = SHRT_MAX; |
| 281 |
for (char c : {'i', 'I', '!', '1'}) { |
| 282 |
SkGlyphID g = font.unicharToGlyph(c); |
| 283 |
SkRect bounds; |
| 284 |
font.getBounds({&g, 1}, {&bounds, 1}, nullptr); |
| 285 |
stemV = std::min(stemV, SkToS16(SkScalarRoundToInt(bounds.width()))); |
| 286 |
} |
| 287 |
metrics->fStemV = stemV; |
| 288 |
} |
| 289 |
if (0 == metrics->fCapHeight) { |
| 290 |
// Figure out a good guess for CapHeight: average the height of M and X. |
| 291 |
SkScalar capHeight = 0; |
| 292 |
for (char c : {'M', 'X'}) { |
| 293 |
SkGlyphID g = font.unicharToGlyph(c); |
| 294 |
SkRect bounds; |
| 295 |
font.getBounds({&g, 1}, {&bounds, 1}, nullptr); |
| 296 |
capHeight += bounds.height(); |
| 297 |
} |
| 298 |
metrics->fCapHeight = SkToS16(SkScalarRoundToInt(capHeight / 2)); |
| 299 |
} |
| 300 |
} |
| 301 |
// Fonts are always subset, so always prepend the subset tag. |
| 302 |
metrics->fPostScriptName.prepend(canon->nextFontSubsetTag()); |
| 303 |
return canon->fTypefaceMetrics.set(id, std::move(metrics))->get(); |
| 304 |
} |
| 305 |
|
| 306 |
const std::vector<SkUnichar>& SkPDFFont::GetUnicodeMap(const SkTypeface& typeface, |
| 307 |
SkPDFDocument* canon) { |
| 308 |
SkASSERT(canon); |
| 309 |
SkTypefaceID id = typeface.uniqueID(); |
| 310 |
if (std::vector<SkUnichar>* ptr = canon->fToUnicodeMap.find(id)) { |
| 311 |
return *ptr; |
| 312 |
} |
| 313 |
std::vector<SkUnichar> buffer(typeface.countGlyphs()); |
| 314 |
typeface.getGlyphToUnicodeMap(buffer); |
| 315 |
return *canon->fToUnicodeMap.set(id, std::move(buffer)); |
| 316 |
} |
| 317 |
|
| 318 |
THashMap<SkGlyphID, SkString>& SkPDFFont::GetUnicodeMapEx(const SkTypeface& typeface, |
| 319 |
SkPDFDocument* canon) { |
| 320 |
SkASSERT(canon); |
| 321 |
SkTypefaceID id = typeface.uniqueID(); |
| 322 |
if (THashMap<SkGlyphID, SkString>* ptr = canon->fToUnicodeMapEx.find(id)) { |
| 323 |
return *ptr; |
| 324 |
} |
| 325 |
return *canon->fToUnicodeMapEx.set(id, THashMap<SkGlyphID, SkString>()); |
| 326 |
} |
| 327 |
|
| 328 |
SkAdvancedTypefaceMetrics::FontType SkPDFFont::FontType(const SkPDFStrike& pdfStrike, |
| 329 |
const SkAdvancedTypefaceMetrics& metrics) { |
| 330 |
if (SkToBool(metrics.fFlags & SkAdvancedTypefaceMetrics::kVariable_FontFlag) || |
| 331 |
// PDF is actually interested in the encoding of the data, not just the logical format. |
| 332 |
// If the TrueType is actually wOFF or wOF2 then it should not be directly embedded in PDF. |
| 333 |
// For now export these as Type3 until the subsetter can handle table based fonts. |
| 334 |
// See https://github.com/harfbuzz/harfbuzz/issues/3609 and |
| 335 |
// https://skia-review.googlesource.com/c/skia/+/543485 |
| 336 |
SkToBool(metrics.fFlags & SkAdvancedTypefaceMetrics::kAltDataFormat_FontFlag) || |
| 337 |
SkToBool(metrics.fFlags & SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag) || |
| 338 |
// Something like 45eeeddb00741493 and 7c86e7641b348ca7b0 to output OpenType should work, |
| 339 |
// but requires PDF 1.6 which is still not supported by all printers. One could fix this by |
| 340 |
// using bare CFF like 31a170226c22244cbd00497b67f6ae181f0f3e76 which is only PDF 1.3, |
| 341 |
// but this only works when the CFF CIDs == CFF index == GlyphID as PDF bare CFF prefers |
| 342 |
// CFF CIDs instead of GlyphIDs and Skia doesn't know the CIDs. |
| 343 |
metrics.fType == SkAdvancedTypefaceMetrics::kCFF_Font || |
| 344 |
pdfStrike.fHasMaskFilter) |
| 345 |
{ |
| 346 |
// force Type3 fallback. |
| 347 |
return SkAdvancedTypefaceMetrics::kOther_Font; |
| 348 |
} |
| 349 |
return metrics.fType; |
| 350 |
} |
| 351 |
|
| 352 |
static SkGlyphID first_nonzero_glyph_for_single_byte_encoding(SkGlyphID gid) { |
| 353 |
return gid != 0 ? gid - (gid - 1) % 255 : 1; |
| 354 |
} |
| 355 |
|
| 356 |
SkPDFFont* SkPDFStrike::getFontResource(const SkGlyph* glyph) { |
| 357 |
const SkTypeface& typeface = fPath.fStrikeSpec.typeface(); |
| 358 |
const SkAdvancedTypefaceMetrics* fontMetrics = SkPDFFont::GetMetrics(typeface, fDoc); |
| 359 |
SkASSERT(fontMetrics); // SkPDFDevice::internalDrawText ensures the typeface is good. |
| 360 |
// GetMetrics only returns null to signify a bad typeface. |
| 361 |
const SkAdvancedTypefaceMetrics& metrics = *fontMetrics; |
| 362 |
|
| 363 |
// Determine the FontType. |
| 364 |
// 1. Can the "original" font data be used directly |
| 365 |
// (simple OpenType, no non-default variations, not WOFF, etc). |
| 366 |
// 2. Is the glyph to be drawn unmodified from the font data |
| 367 |
// (no path effect, stroking, fake bolding, extra matrix, mask filter). |
| 368 |
// 3. Will PDF viewers draw this glyph the way we want |
| 369 |
// (at the moment this means an unmodified glyph path). |
| 370 |
SkAdvancedTypefaceMetrics::FontType type = SkPDFFont::FontType(*this, metrics); |
| 371 |
// Keep the type (and original data) if the glyph is empty or the glyph has an unmodified path. |
| 372 |
// Otherwise, fall back to Type3. |
| 373 |
if (!(glyph->isEmpty() || (glyph->path() && !glyph->pathIsModified()))) { |
| 374 |
type = SkAdvancedTypefaceMetrics::kOther_Font; |
| 375 |
} |
| 376 |
|
| 377 |
bool multibyte = SkPDFFont::IsMultiByte(type); |
| 378 |
SkGlyphID subsetCode = |
| 379 |
multibyte ? 0 : first_nonzero_glyph_for_single_byte_encoding(glyph->getGlyphID()); |
| 380 |
if (SkPDFFont* font = fFontMap.find(subsetCode)) { |
| 381 |
SkASSERT(multibyte == font->multiByteGlyphs()); |
| 382 |
return font; |
| 383 |
} |
| 384 |
|
| 385 |
SkGlyphID lastGlyph = SkToU16(typeface.countGlyphs() - 1); |
| 386 |
SkASSERT(glyph->getGlyphID() <= lastGlyph); // should be caught by SkPDFDevice::internalDrawText |
| 387 |
|
| 388 |
SkGlyphID firstNonZeroGlyph; |
| 389 |
if (multibyte) { |
| 390 |
firstNonZeroGlyph = 1; |
| 391 |
} else { |
| 392 |
firstNonZeroGlyph = subsetCode; |
| 393 |
lastGlyph = SkToU16(std::min<int>((int)lastGlyph, 254 + (int)subsetCode)); |
| 394 |
} |
| 395 |
auto ref = fDoc->reserveRef(); |
| 396 |
return fFontMap.set(subsetCode, SkPDFFont(this, firstNonZeroGlyph, lastGlyph, type, ref)); |
| 397 |
} |
| 398 |
|
| 399 |
SkPDFFont::SkPDFFont(const SkPDFStrike* strike, |
| 400 |
SkGlyphID firstGlyphID, |
| 401 |
SkGlyphID lastGlyphID, |
| 402 |
SkAdvancedTypefaceMetrics::FontType fontType, |
| 403 |
SkPDFIndirectReference indirectReference) |
| 404 |
: fStrike(strike) |
| 405 |
, fGlyphUsage(firstGlyphID, lastGlyphID) |
| 406 |
, fIndirectReference(indirectReference) |
| 407 |
, fFontType(fontType) |
| 408 |
{ |
| 409 |
// Always include glyph 0 |
| 410 |
this->noteGlyphUsage(0); |
| 411 |
} |
| 412 |
|
| 413 |
void SkPDFFont::PopulateCommonFontDescriptor(SkPDFDict* descriptor, |
| 414 |
const SkAdvancedTypefaceMetrics& metrics, |
| 415 |
uint16_t emSize, |
| 416 |
int16_t defaultWidth) { |
| 417 |
descriptor->insertName("FontName", metrics.fPostScriptName); |
| 418 |
descriptor->insertInt("Flags", (size_t)(metrics.fStyle | kPdfSymbolic)); |
| 419 |
descriptor->insertScalar("Ascent", |
| 420 |
scaleFromFontUnits(metrics.fAscent, emSize)); |
| 421 |
descriptor->insertScalar("Descent", |
| 422 |
scaleFromFontUnits(metrics.fDescent, emSize)); |
| 423 |
descriptor->insertScalar("StemV", |
| 424 |
scaleFromFontUnits(metrics.fStemV, emSize)); |
| 425 |
descriptor->insertScalar("CapHeight", |
| 426 |
scaleFromFontUnits(metrics.fCapHeight, emSize)); |
| 427 |
descriptor->insertInt("ItalicAngle", metrics.fItalicAngle); |
| 428 |
descriptor->insertObject("FontBBox", |
| 429 |
SkPDFMakeArray(scaleFromFontUnits(metrics.fBBox.left(), emSize), |
| 430 |
scaleFromFontUnits(metrics.fBBox.bottom(), emSize), |
| 431 |
scaleFromFontUnits(metrics.fBBox.right(), emSize), |
| 432 |
scaleFromFontUnits(metrics.fBBox.top(), emSize))); |
| 433 |
if (defaultWidth > 0) { |
| 434 |
descriptor->insertScalar("MissingWidth", |
| 435 |
scaleFromFontUnits(defaultWidth, emSize)); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
/////////////////////////////////////////////////////////////////////////////// |
| 440 |
// Type0Font |
| 441 |
/////////////////////////////////////////////////////////////////////////////// |
| 442 |
|
| 443 |
static void emit_subset_type0(const SkPDFFont& font, SkPDFDocument* doc) { |
| 444 |
const SkTypeface& typeface = font.strike().fPath.fStrikeSpec.typeface(); |
| 445 |
const SkAdvancedTypefaceMetrics* metricsPtr = SkPDFFont::GetMetrics(typeface, doc); |
| 446 |
SkASSERT(metricsPtr); |
| 447 |
if (!metricsPtr) { |
| 448 |
return; |
| 449 |
} |
| 450 |
const SkAdvancedTypefaceMetrics& metrics = *metricsPtr; |
| 451 |
SkASSERT(can_embed(metrics)); |
| 452 |
SkAdvancedTypefaceMetrics::FontType type = font.getType(); |
| 453 |
|
| 454 |
auto descriptor = SkPDFMakeDict("FontDescriptor"); |
| 455 |
uint16_t emSize = SkToU16(SkScalarRoundToInt(font.strike().fPath.fUnitsPerEM)); |
| 456 |
SkPDFFont::PopulateCommonFontDescriptor(descriptor.get(), metrics, emSize, 0); |
| 457 |
|
| 458 |
int ttcIndex; |
| 459 |
std::unique_ptr<SkStreamAsset> fontAsset = typeface.openStream(&ttcIndex); |
| 460 |
size_t fontSize = fontAsset ? fontAsset->getLength() : 0; |
| 461 |
if (0 == fontSize) { |
| 462 |
SkDebugf("Error: (SkTypeface)(%p)::openStream() returned " |
| 463 |
"empty stream (%p) when identified as kType1CID_Font " |
| 464 |
"or kTrueType_Font.\n", &typeface, fontAsset.get()); |
| 465 |
} else if (type == SkAdvancedTypefaceMetrics::kTrueType_Font) { |
| 466 |
sk_sp<SkData> subsetFontData; |
| 467 |
if (can_subset(metrics)) { |
| 468 |
SkASSERT(font.firstGlyphID() == 1); |
| 469 |
subsetFontData = SkPDFSubsetFont(typeface, font.glyphUsage()); |
| 470 |
} |
| 471 |
std::unique_ptr<SkStreamAsset> subsetFontAsset; |
| 472 |
if (subsetFontData) { |
| 473 |
subsetFontAsset = SkMemoryStream::Make(std::move(subsetFontData)); |
| 474 |
} else { |
| 475 |
// If subsetting fails, fall back to original font data. |
| 476 |
subsetFontAsset = std::move(fontAsset); |
| 477 |
} |
| 478 |
std::unique_ptr<SkPDFDict> streamDict = SkPDFMakeDict(); |
| 479 |
streamDict->insertInt("Length1", subsetFontAsset->getLength()); |
| 480 |
descriptor->insertRef("FontFile2", |
| 481 |
SkPDFStreamOut(std::move(streamDict), std::move(subsetFontAsset), |
| 482 |
doc, SkPDFSteamCompressionEnabled::Yes)); |
| 483 |
} else if (type == SkAdvancedTypefaceMetrics::kType1CID_Font) { |
| 484 |
std::unique_ptr<SkPDFDict> streamDict = SkPDFMakeDict(); |
| 485 |
streamDict->insertName("Subtype", "CIDFontType0C"); |
| 486 |
descriptor->insertRef("FontFile3", |
| 487 |
SkPDFStreamOut(std::move(streamDict), std::move(fontAsset), |
| 488 |
doc, SkPDFSteamCompressionEnabled::Yes)); |
| 489 |
} else { |
| 490 |
SkASSERT(false); |
| 491 |
} |
| 492 |
|
| 493 |
auto newCIDFont = SkPDFMakeDict("Font"); |
| 494 |
newCIDFont->insertRef("FontDescriptor", doc->emit(*descriptor)); |
| 495 |
newCIDFont->insertName("BaseFont", metrics.fPostScriptName); |
| 496 |
|
| 497 |
switch (type) { |
| 498 |
case SkAdvancedTypefaceMetrics::kType1CID_Font: |
| 499 |
newCIDFont->insertName("Subtype", "CIDFontType0"); |
| 500 |
break; |
| 501 |
case SkAdvancedTypefaceMetrics::kTrueType_Font: |
| 502 |
newCIDFont->insertName("Subtype", "CIDFontType2"); |
| 503 |
newCIDFont->insertName("CIDToGIDMap", "Identity"); |
| 504 |
break; |
| 505 |
default: |
| 506 |
SkASSERT(false); |
| 507 |
} |
| 508 |
auto sysInfo = SkPDFMakeDict(); |
| 509 |
// These are actually ASCII strings. |
| 510 |
sysInfo->insertByteString("Registry", "Adobe"); |
| 511 |
sysInfo->insertByteString("Ordering", "Identity"); |
| 512 |
sysInfo->insertInt("Supplement", 0); |
| 513 |
newCIDFont->insertObject("CIDSystemInfo", std::move(sysInfo)); |
| 514 |
|
| 515 |
// Unfortunately, poppler enforces DW (default width) must be an integer. |
| 516 |
int32_t defaultWidth = 0; |
| 517 |
{ |
| 518 |
std::unique_ptr<SkPDFArray> widths = SkPDFMakeCIDGlyphWidthsArray( |
| 519 |
font.strike().fPath, font.glyphUsage(), &defaultWidth); |
| 520 |
if (widths && widths->size() > 0) { |
| 521 |
newCIDFont->insertObject("W", std::move(widths)); |
| 522 |
} |
| 523 |
newCIDFont->insertInt("DW", defaultWidth); |
| 524 |
} |
| 525 |
|
| 526 |
//////////////////////////////////////////////////////////////////////////// |
| 527 |
|
| 528 |
SkPDFDict fontDict("Font"); |
| 529 |
fontDict.insertName("Subtype", "Type0"); |
| 530 |
fontDict.insertName("BaseFont", metrics.fPostScriptName); |
| 531 |
fontDict.insertName("Encoding", "Identity-H"); |
| 532 |
auto descendantFonts = SkPDFMakeArray(); |
| 533 |
descendantFonts->appendRef(doc->emit(*newCIDFont)); |
| 534 |
fontDict.insertObject("DescendantFonts", std::move(descendantFonts)); |
| 535 |
|
| 536 |
const std::vector<SkUnichar>& glyphToUnicode = |
| 537 |
SkPDFFont::GetUnicodeMap(typeface, doc); |
| 538 |
SkASSERT(SkToSizeT(typeface.countGlyphs()) == glyphToUnicode.size()); |
| 539 |
std::unique_ptr<SkStreamAsset> toUnicode = |
| 540 |
SkPDFMakeToUnicodeCmap(glyphToUnicode.data(), |
| 541 |
SkPDFFont::GetUnicodeMapEx(typeface, doc), |
| 542 |
&font.glyphUsage(), |
| 543 |
font.multiByteGlyphs(), |
| 544 |
font.firstGlyphID(), |
| 545 |
font.lastGlyphID()); |
| 546 |
fontDict.insertRef("ToUnicode", SkPDFStreamOut(nullptr, std::move(toUnicode), doc)); |
| 547 |
|
| 548 |
doc->emit(fontDict, font.indirectReference()); |
| 549 |
} |
| 550 |
|
| 551 |
/////////////////////////////////////////////////////////////////////////////// |
| 552 |
// PDFType3Font |
| 553 |
/////////////////////////////////////////////////////////////////////////////// |
| 554 |
|
| 555 |
namespace { |
| 556 |
// returns [0, first, first+1, ... last-1, last] |
| 557 |
struct SingleByteGlyphIdIterator { |
| 558 |
SingleByteGlyphIdIterator(SkGlyphID first, SkGlyphID last) |
| 559 |
: fFirst(first), fLast(last) { |
| 560 |
SkASSERT(fFirst > 0); |
| 561 |
SkASSERT(fLast >= first); |
| 562 |
} |
| 563 |
struct Iter { |
| 564 |
void operator++() { |
| 565 |
fCurrent = (0 == fCurrent) ? fFirst : fCurrent + 1; |
| 566 |
} |
| 567 |
// This is an input_iterator |
| 568 |
SkGlyphID operator*() const { return (SkGlyphID)fCurrent; } |
| 569 |
bool operator!=(const Iter& rhs) const { |
| 570 |
return fCurrent != rhs.fCurrent; |
| 571 |
} |
| 572 |
Iter(SkGlyphID f, int c) : fFirst(f), fCurrent(c) {} |
| 573 |
private: |
| 574 |
const SkGlyphID fFirst; |
| 575 |
int fCurrent; // must be int to make fLast+1 to fit |
| 576 |
}; |
| 577 |
Iter begin() const { return Iter(fFirst, 0); } |
| 578 |
Iter end() const { return Iter(fFirst, (int)fLast + 1); } |
| 579 |
private: |
| 580 |
const SkGlyphID fFirst; |
| 581 |
const SkGlyphID fLast; |
| 582 |
}; |
| 583 |
} // namespace |
| 584 |
|
| 585 |
struct ImageAndOffset { |
| 586 |
sk_sp<SkImage> fImage; |
| 587 |
SkIPoint fOffset; |
| 588 |
}; |
| 589 |
static ImageAndOffset to_image(SkGlyphID gid, SkBulkGlyphMetricsAndImages* smallGlyphs) { |
| 590 |
const SkGlyph* glyph = smallGlyphs->glyph(SkPackedGlyphID{gid}); |
| 591 |
SkMask mask = glyph->mask(); |
| 592 |
if (!mask.fImage) { |
| 593 |
return {nullptr, {0, 0}}; |
| 594 |
} |
| 595 |
SkIRect bounds = mask.fBounds; |
| 596 |
SkBitmap bm; |
| 597 |
switch (mask.fFormat) { |
| 598 |
case SkMask::kBW_Format: { |
| 599 |
// Make a gray image, used to smask a rectangle. |
| 600 |
// TODO: emit as MaskImage? |
| 601 |
const SkISize size = bounds.size(); |
| 602 |
bm.allocPixels(SkImageInfo::Make(size, kGray_8_SkColorType, kUnknown_SkAlphaType)); |
| 603 |
for (int y = 0; y < bm.height(); ++y) { |
| 604 |
for (int x8 = 0; x8 < bm.width(); x8 += 8) { |
| 605 |
uint8_t v = *mask.getAddr1(x8 + bounds.x(), y + bounds.y()); |
| 606 |
int e = std::min(x8 + 8, bm.width()); |
| 607 |
for (int x = x8; x < e; ++x) { |
| 608 |
*bm.getAddr8(x, y) = (v >> (x & 0x7)) & 0x1 ? 0xFF : 0x00; |
| 609 |
} |
| 610 |
} |
| 611 |
} |
| 612 |
bm.setImmutable(); |
| 613 |
return {bm.asImage(), {bounds.x(), bounds.y()}}; |
| 614 |
} |
| 615 |
case SkMask::kA8_Format: |
| 616 |
case SkMask::k3D_Format: // just do the A8 part |
| 617 |
// Make a gray image, used to smask a rectangle. |
| 618 |
return {SkImages::RasterFromData( |
| 619 |
SkImageInfo::Make(bounds.size(), kGray_8_SkColorType, kUnknown_SkAlphaType), |
| 620 |
SkData::MakeWithCopy(mask.fImage, mask.computeImageSize()), |
| 621 |
mask.fRowBytes), |
| 622 |
{bounds.x(), bounds.y()}}; |
| 623 |
case SkMask::kARGB32_Format: |
| 624 |
// These will be drawn as images directly. |
| 625 |
return {SkImages::RasterFromData( |
| 626 |
SkImageInfo::MakeN32Premul(bounds.size()), |
| 627 |
SkData::MakeWithCopy(mask.fImage, mask.computeTotalImageSize()), |
| 628 |
mask.fRowBytes), |
| 629 |
{bounds.x(), bounds.y()}}; |
| 630 |
case SkMask::kLCD16_Format: |
| 631 |
default: |
| 632 |
SkASSERT(false); |
| 633 |
return {nullptr, {0, 0}}; |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
static SkPDFIndirectReference type3_descriptor(SkPDFDocument* doc, |
| 638 |
const SkTypeface& typeface, |
| 639 |
SkScalar xHeight) { |
| 640 |
if (SkPDFIndirectReference* ptr = doc->fType3FontDescriptors.find(typeface.uniqueID())) { |
| 641 |
return *ptr; |
| 642 |
} |
| 643 |
|
| 644 |
SkPDFDict descriptor("FontDescriptor"); |
| 645 |
int32_t fontDescriptorFlags = kPdfSymbolic; |
| 646 |
|
| 647 |
/** PDF32000_2008: FontFamily should be used for Type3 fonts in Tagged PDF documents. */ |
| 648 |
SkString familyName; |
| 649 |
typeface.getFamilyName(&familyName); |
| 650 |
if (!familyName.isEmpty()) { |
| 651 |
descriptor.insertByteString("FontFamily", familyName); |
| 652 |
} |
| 653 |
|
| 654 |
/** PDF32000_2008: FontStretch should be used for Type3 fonts in Tagged PDF documents. */ |
| 655 |
static constexpr const char* stretchNames[9] = { |
| 656 |
"UltraCondensed", |
| 657 |
"ExtraCondensed", |
| 658 |
"Condensed", |
| 659 |
"SemiCondensed", |
| 660 |
"Normal", |
| 661 |
"SemiExpanded", |
| 662 |
"Expanded", |
| 663 |
"ExtraExpanded", |
| 664 |
"UltraExpanded", |
| 665 |
}; |
| 666 |
const char* stretchName = stretchNames[typeface.fontStyle().width() - 1]; |
| 667 |
descriptor.insertName("FontStretch", stretchName); |
| 668 |
|
| 669 |
/** PDF32000_2008: FontWeight should be used for Type3 fonts in Tagged PDF documents. */ |
| 670 |
int weight = (typeface.fontStyle().weight() + 50) / 100; |
| 671 |
descriptor.insertInt("FontWeight", SkTPin(weight, 1, 9) * 100); |
| 672 |
|
| 673 |
if (const SkAdvancedTypefaceMetrics* metrics = SkPDFFont::GetMetrics(typeface, doc)) { |
| 674 |
// Type3 FontDescriptor does not require all the same fields. |
| 675 |
descriptor.insertName("FontName", metrics->fPostScriptName); |
| 676 |
descriptor.insertInt("ItalicAngle", metrics->fItalicAngle); |
| 677 |
fontDescriptorFlags |= (int32_t)metrics->fStyle; |
| 678 |
// Adobe requests CapHeight, XHeight, and StemV be added |
| 679 |
// to "greatly help our workflow downstream". |
| 680 |
if (metrics->fCapHeight != 0) { descriptor.insertInt("CapHeight", metrics->fCapHeight); } |
| 681 |
if (metrics->fStemV != 0) { descriptor.insertInt("StemV", metrics->fStemV); } |
| 682 |
if (xHeight != 0) { |
| 683 |
descriptor.insertScalar("XHeight", xHeight); |
| 684 |
} |
| 685 |
} |
| 686 |
descriptor.insertInt("Flags", fontDescriptorFlags); |
| 687 |
SkPDFIndirectReference ref = doc->emit(descriptor); |
| 688 |
doc->fType3FontDescriptors.set(typeface.uniqueID(), ref); |
| 689 |
return ref; |
| 690 |
} |
| 691 |
|
| 692 |
static void emit_subset_type3(const SkPDFFont& pdfFont, SkPDFDocument* doc) { |
| 693 |
const SkPDFStrike& pdfStrike = pdfFont.strike(); |
| 694 |
SkGlyphID firstGlyphID = pdfFont.firstGlyphID(); |
| 695 |
SkGlyphID lastGlyphID = pdfFont.lastGlyphID(); |
| 696 |
const SkPDFGlyphUse& subset = pdfFont.glyphUsage(); |
| 697 |
SkASSERT(lastGlyphID >= firstGlyphID); |
| 698 |
// Remove unused glyphs at the end of the range. |
| 699 |
// Keep the lastGlyphID >= firstGlyphID invariant true. |
| 700 |
while (lastGlyphID > firstGlyphID && !subset.has(lastGlyphID)) { |
| 701 |
--lastGlyphID; |
| 702 |
} |
| 703 |
SkScalar emSize = pdfStrike.fPath.fUnitsPerEM; |
| 704 |
sk_sp<SkStrike> strike = pdfStrike.fPath.fStrikeSpec.findOrCreateStrike(); |
| 705 |
SkASSERT(strike); |
| 706 |
SkScalar xHeight = strike->getFontMetrics().fXHeight; |
| 707 |
SkBulkGlyphMetricsAndPaths metricsAndPaths((sk_sp<SkStrike>(strike))); |
| 708 |
SkBulkGlyphMetricsAndDrawables metricsAndDrawables(std::move(strike)); |
| 709 |
|
| 710 |
SkBulkGlyphMetricsAndImages smallGlyphs(pdfFont.strike().fImage.fStrikeSpec); |
| 711 |
float bitmapScale = emSize / pdfStrike.fImage.fUnitsPerEM; |
| 712 |
|
| 713 |
SkPDFDict font("Font"); |
| 714 |
font.insertName("Subtype", "Type3"); |
| 715 |
// Flip about the x-axis and scale by 1/emSize. |
| 716 |
SkMatrix fontMatrix; |
| 717 |
fontMatrix.setScale(SkScalarInvert(emSize), -SkScalarInvert(emSize)); |
| 718 |
font.insertObject("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix)); |
| 719 |
|
| 720 |
auto charProcs = SkPDFMakeDict(); |
| 721 |
auto encoding = SkPDFMakeDict("Encoding"); |
| 722 |
|
| 723 |
auto encDiffs = SkPDFMakeArray(); |
| 724 |
// length(firstGlyphID .. lastGlyphID) == lastGlyphID - firstGlyphID + 1 |
| 725 |
// plus 1 for glyph 0; |
| 726 |
SkASSERT(firstGlyphID > 0); |
| 727 |
SkASSERT(lastGlyphID >= firstGlyphID); |
| 728 |
int glyphCount = lastGlyphID - firstGlyphID + 2; |
| 729 |
// one other entry for the index of first glyph. |
| 730 |
encDiffs->reserve(glyphCount + 1); |
| 731 |
encDiffs->appendInt(0); // index of first glyph |
| 732 |
|
| 733 |
auto widthArray = SkPDFMakeArray(); |
| 734 |
widthArray->reserve(glyphCount); |
| 735 |
|
| 736 |
SkIRect bbox = SkIRect::MakeEmpty(); |
| 737 |
|
| 738 |
std::unique_ptr<SkPDFDict> xobjects = SkPDFMakeDict(); |
| 739 |
std::unique_ptr<SkPDFDict> graphicStates = SkPDFMakeDict(); |
| 740 |
for (SkGlyphID gID : SingleByteGlyphIdIterator(firstGlyphID, lastGlyphID)) { |
| 741 |
SkString characterName; |
| 742 |
SkScalar advance = 0.0f; |
| 743 |
|
| 744 |
if (gID != 0 && !subset.has(gID)) { |
| 745 |
characterName.set("g0"); |
| 746 |
advance = 0.0f; |
| 747 |
encDiffs->appendName(std::move(characterName)); |
| 748 |
widthArray->appendScalar(advance); |
| 749 |
continue; |
| 750 |
} |
| 751 |
|
| 752 |
const SkGlyph* pathGlyph = metricsAndPaths.glyph(gID); |
| 753 |
const SkGlyph* drawableGlyph = metricsAndDrawables.glyph(gID); |
| 754 |
|
| 755 |
characterName.printf("g%X", gID); |
| 756 |
advance = pathGlyph->advanceX(); |
| 757 |
encDiffs->appendName(characterName); |
| 758 |
widthArray->appendScalar(advance); |
| 759 |
|
| 760 |
SkIRect glyphBBox = pathGlyph->iRect(); |
| 761 |
bbox.join(glyphBBox); |
| 762 |
const SkPath* path = pathGlyph->path(); |
| 763 |
SkDrawable* drawable = drawableGlyph->drawable(); |
| 764 |
SkDynamicMemoryWStream content; |
| 765 |
if (drawable && !drawable->getBounds().isEmpty()) { |
| 766 |
sk_sp<SkPDFDevice> glyphDevice = sk_make_sp<SkPDFDevice>(glyphBBox.size(), doc); |
| 767 |
SkCanvas canvas(glyphDevice); |
| 768 |
canvas.translate(-glyphBBox.fLeft, -glyphBBox.fTop); |
| 769 |
canvas.drawDrawable(drawable); |
| 770 |
SkPDFIndirectReference xobject = SkPDFMakeFormXObject( |
| 771 |
doc, glyphDevice->content(), |
| 772 |
SkPDFMakeArray(0, 0, glyphBBox.width(), glyphBBox.height()), |
| 773 |
glyphDevice->makeResourceDict(), |
| 774 |
SkMatrix::Translate(glyphBBox.fLeft, glyphBBox.fTop), nullptr); |
| 775 |
xobjects->insertRef(SkStringPrintf("Xg%X", gID), xobject); |
| 776 |
SkPDFUtils::AppendScalar(drawableGlyph->advanceX(), &content); |
| 777 |
content.writeText(" 0 d0\n1 0 0 1 0 0 cm\n/X"); |
| 778 |
content.write(characterName.c_str(), characterName.size()); |
| 779 |
content.writeText(" Do\n"); |
| 780 |
} else if (path && !path->isEmpty() && !pdfStrike.fHasMaskFilter) { |
| 781 |
setGlyphWidthAndBoundingBox(pathGlyph->advanceX(), glyphBBox, &content); |
| 782 |
SkPaint::Style style = pathGlyph->pathIsHairline() ? SkPaint::kStroke_Style |
| 783 |
: SkPaint::kFill_Style; |
| 784 |
using SkPDFUtils::EmptyPath, SkPDFUtils::EmptyVerb; |
| 785 |
if (SkPDFUtils::EmitPath(*path, style, EmptyPath::Discard, EmptyVerb::Discard, |
| 786 |
&content)) |
| 787 |
{ |
| 788 |
SkPDFUtils::PaintPath(style, path->getFillType(), &content); |
| 789 |
} |
| 790 |
} else if (auto pimg = to_image(gID, &smallGlyphs); pimg.fImage) { |
| 791 |
using SkPDFUtils::AppendScalar; |
| 792 |
if (pimg.fImage->colorType() != kGray_8_SkColorType) { |
| 793 |
AppendScalar(pathGlyph->advanceX(), &content); |
| 794 |
content.writeText(" 0 d0\n"); |
| 795 |
AppendScalar(pimg.fImage->width() * bitmapScale, &content); |
| 796 |
content.writeText(" 0 0 "); |
| 797 |
AppendScalar(-pimg.fImage->height() * bitmapScale, &content); |
| 798 |
content.writeText(" "); |
| 799 |
AppendScalar(pimg.fOffset.x() * bitmapScale, &content); |
| 800 |
content.writeText(" "); |
| 801 |
AppendScalar((pimg.fImage->height() + pimg.fOffset.y()) * bitmapScale,&content); |
| 802 |
content.writeText(" cm\n"); |
| 803 |
content.writeText("/X"); |
| 804 |
content.write(characterName.c_str(), characterName.size()); |
| 805 |
content.writeText(" Do\n"); |
| 806 |
SkPDFIndirectReference image = SkPDFSerializeImage(pimg.fImage.get(), doc, 101); |
| 807 |
xobjects->insertRef(SkStringPrintf("Xg%X", gID), image); |
| 808 |
} else { |
| 809 |
// TODO: For A1, put ImageMask on the PDF image and draw the image? |
| 810 |
// The A8 mask has been converted to a Gray image |
| 811 |
|
| 812 |
// This is a `d1` glyph (shaded with the current fill) |
| 813 |
const SkGlyph* smallGlyph = smallGlyphs.glyph(SkPackedGlyphID{gID}); |
| 814 |
SkRect smallBBox = smallGlyph->rect(); |
| 815 |
SkIRect smallIBox; |
| 816 |
SkMatrix::Scale(bitmapScale, bitmapScale).mapRect(smallBBox).roundOut(&smallIBox); |
| 817 |
bbox.join(smallIBox); |
| 818 |
setGlyphWidthAndBoundingBox(pathGlyph->advanceX(), smallIBox, &content); |
| 819 |
|
| 820 |
AppendScalar(bitmapScale, &content); |
| 821 |
content.writeText(" 0 0 "); |
| 822 |
AppendScalar(bitmapScale, &content); |
| 823 |
content.writeText(" "); |
| 824 |
AppendScalar(pimg.fOffset.x() * bitmapScale, &content); |
| 825 |
content.writeText(" "); |
| 826 |
AppendScalar(pimg.fOffset.y() * bitmapScale, &content); |
| 827 |
content.writeText(" cm\n"); |
| 828 |
|
| 829 |
// Convert Grey image to deferred jpeg image to emit as jpeg |
| 830 |
if (pdfStrike.fHasMaskFilter) { |
| 831 |
SkPDF::EncodeJpegCallback encodeJPEG = doc->metadata().jpegEncoder; |
| 832 |
SkPDF::DecodeJpegCallback decodeJPEG = doc->metadata().jpegDecoder; |
| 833 |
if (encodeJPEG && decodeJPEG) { |
| 834 |
SkImage* image = pimg.fImage.get(); |
| 835 |
SkPixmap pm; |
| 836 |
SkAssertResult(image->peekPixels(&pm)); |
| 837 |
SkDynamicMemoryWStream buffer; |
| 838 |
if (encodeJPEG(&buffer, pm, SK_PDF_MASK_QUALITY)) { |
| 839 |
std::unique_ptr<SkCodec> codec = |
| 840 |
decodeJPEG(buffer.detachAsData()); |
| 841 |
SkASSERT(codec); |
| 842 |
sk_sp<SkImage> jpegImage = SkCodecs::DeferredImage(std::move(codec)); |
| 843 |
SkASSERT(jpegImage); |
| 844 |
if (jpegImage) { |
| 845 |
pimg.fImage = jpegImage; |
| 846 |
} |
| 847 |
} |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
// Draw image into a Form XObject |
| 852 |
const SkISize imageSize = pimg.fImage->dimensions(); |
| 853 |
sk_sp<SkPDFDevice> glyphDevice = sk_sp(new SkPDFDevice(imageSize, doc)); |
| 854 |
SkCanvas canvas(glyphDevice); |
| 855 |
canvas.drawImage(pimg.fImage, 0, 0); |
| 856 |
SkPDFIndirectReference sMask = SkPDFMakeFormXObject( |
| 857 |
doc, glyphDevice->content(), |
| 858 |
SkPDFMakeArray(0, 0, pimg.fImage->width(), pimg.fImage->height()), |
| 859 |
glyphDevice->makeResourceDict(), |
| 860 |
SkMatrix(), "DeviceGray"); |
| 861 |
|
| 862 |
// Use Form XObject as SMask (luminosity) on the graphics state |
| 863 |
SkPDFIndirectReference smaskGraphicState = SkPDFGraphicState::GetSMaskGraphicState( |
| 864 |
sMask, false, |
| 865 |
SkPDFGraphicState::kLuminosity_SMaskMode, doc); |
| 866 |
SkPDFUtils::ApplyGraphicState(smaskGraphicState.fValue, &content); |
| 867 |
|
| 868 |
// Draw a rectangle the size of the glyph (masked by SMask) |
| 869 |
SkPDFUtils::AppendRectangle(SkRect::Make(pimg.fImage->bounds()), &content); |
| 870 |
SkPDFUtils::PaintPath(SkPaint::kFill_Style, SkPathFillType::kWinding, &content); |
| 871 |
|
| 872 |
// Add glyph resources to font resource dict |
| 873 |
xobjects->insertRef(SkStringPrintf("Xg%X", gID), sMask); |
| 874 |
// TODO: name must match ApplyGraphicState |
| 875 |
graphicStates->insertRef(SkStringPrintf("G%d", smaskGraphicState.fValue), |
| 876 |
smaskGraphicState); |
| 877 |
} |
| 878 |
} else { |
| 879 |
setGlyphWidthAndBoundingBox(pathGlyph->advanceX(), glyphBBox, &content); |
| 880 |
} |
| 881 |
charProcs->insertRef(std::move(characterName), |
| 882 |
SkPDFStreamOut(nullptr, content.detachAsStream(), doc)); |
| 883 |
} |
| 884 |
|
| 885 |
if (xobjects->size() || graphicStates->size()) { |
| 886 |
auto resources = SkPDFMakeDict(); |
| 887 |
if (xobjects->size()) { |
| 888 |
resources->insertObject("XObject", std::move(xobjects)); |
| 889 |
} |
| 890 |
if (graphicStates->size()) { |
| 891 |
resources->insertObject("ExtGState", std::move(graphicStates)); |
| 892 |
} |
| 893 |
font.insertObject("Resources", std::move(resources)); |
| 894 |
} |
| 895 |
|
| 896 |
encoding->insertObject("Differences", std::move(encDiffs)); |
| 897 |
font.insertInt("FirstChar", 0); |
| 898 |
font.insertInt("LastChar", lastGlyphID - firstGlyphID + 1); |
| 899 |
/* FontBBox: "A rectangle expressed in the glyph coordinate |
| 900 |
system, specifying the font bounding box. This is the smallest |
| 901 |
rectangle enclosing the shape that would result if all of the |
| 902 |
glyphs of the font were placed with their origins coincident and |
| 903 |
then filled." */ |
| 904 |
font.insertObject("FontBBox", SkPDFMakeArray(bbox.left(), |
| 905 |
bbox.bottom(), |
| 906 |
bbox.right(), |
| 907 |
bbox.top())); |
| 908 |
|
| 909 |
font.insertName("CIDToGIDMap", "Identity"); |
| 910 |
|
| 911 |
const SkTypeface& pathTypeface = pdfStrike.fPath.fStrikeSpec.typeface(); |
| 912 |
const std::vector<SkUnichar>& glyphToUnicode = SkPDFFont::GetUnicodeMap(pathTypeface, doc); |
| 913 |
SkASSERT(glyphToUnicode.size() == SkToSizeT(pathTypeface.countGlyphs())); |
| 914 |
auto toUnicodeCmap = SkPDFMakeToUnicodeCmap(glyphToUnicode.data(), |
| 915 |
SkPDFFont::GetUnicodeMapEx(pathTypeface, doc), |
| 916 |
&subset, |
| 917 |
false, |
| 918 |
firstGlyphID, |
| 919 |
lastGlyphID); |
| 920 |
font.insertRef("ToUnicode", SkPDFStreamOut(nullptr, std::move(toUnicodeCmap), doc)); |
| 921 |
font.insertRef("FontDescriptor", type3_descriptor(doc, pathTypeface, xHeight)); |
| 922 |
font.insertObject("Widths", std::move(widthArray)); |
| 923 |
font.insertObject("Encoding", std::move(encoding)); |
| 924 |
font.insertObject("CharProcs", std::move(charProcs)); |
| 925 |
|
| 926 |
doc->emit(font, pdfFont.indirectReference()); |
| 927 |
} |
| 928 |
|
| 929 |
void SkPDFFont::emitSubset(SkPDFDocument* doc) const { |
| 930 |
switch (fFontType) { |
| 931 |
case SkAdvancedTypefaceMetrics::kType1CID_Font: |
| 932 |
case SkAdvancedTypefaceMetrics::kTrueType_Font: |
| 933 |
return emit_subset_type0(*this, doc); |
| 934 |
#ifndef SK_PDF_DO_NOT_SUPPORT_TYPE_1_FONTS |
| 935 |
case SkAdvancedTypefaceMetrics::kType1_Font: |
| 936 |
return SkPDFEmitType1Font(*this, doc); |
| 937 |
#endif |
| 938 |
default: |
| 939 |
return emit_subset_type3(*this, doc); |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
//////////////////////////////////////////////////////////////////////////////// |
| 944 |
|
| 945 |
bool SkPDFFont::CanEmbedTypeface(const SkTypeface& typeface, SkPDFDocument* doc) { |
| 946 |
const SkAdvancedTypefaceMetrics* metrics = SkPDFFont::GetMetrics(typeface, doc); |
| 947 |
return metrics && can_embed(*metrics); |
| 948 |
} |
| 949 |
|