aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHal Canary <halcanary@google.com>2018-11-15 14:01:49 -0500
committerSkia Commit-Bot <skia-commit-bot@chromium.org>2018-11-15 19:45:35 +0000
commit42d6c59025e58343aa6a39e1dd226d033ec1b9b9 (patch)
tree463a4833d4f261d4290b6350fb73a84a48204c87
parentd32b4b8979ccc4a6e28eb5c305cdbdadc3bc2d4f (diff)
downloadskqp-42d6c59025e58343aa6a39e1dd226d033ec1b9b9.tar.gz
SkPDF: pull out SkPDFSubsetFont code to another file.
Change-Id: Ib7f6b5a4e713beb537d1e0a8f20c7a8c0e666267 Reviewed-on: https://skia-review.googlesource.com/c/171226 Auto-Submit: Hal Canary <halcanary@google.com> Commit-Queue: Ben Wagner <bungeman@google.com> Reviewed-by: Ben Wagner <bungeman@google.com>
-rw-r--r--gn/pdf.gni2
-rw-r--r--src/pdf/SkPDFFont.cpp73
-rw-r--r--src/pdf/SkPDFSubsetFont.cpp50
-rw-r--r--src/pdf/SkPDFSubsetFont.h18
4 files changed, 83 insertions, 60 deletions
diff --git a/gn/pdf.gni b/gn/pdf.gni
index 9efc65fbf5..8e5c759731 100644
--- a/gn/pdf.gni
+++ b/gn/pdf.gni
@@ -46,6 +46,8 @@ skia_pdf_sources = [
"$_src/pdf/SkPDFResourceDict.h",
"$_src/pdf/SkPDFShader.cpp",
"$_src/pdf/SkPDFShader.h",
+ "$_src/pdf/SkPDFSubsetFont.cpp",
+ "$_src/pdf/SkPDFSubsetFont.h",
"$_src/pdf/SkPDFTag.cpp",
"$_src/pdf/SkPDFTag.h",
"$_src/pdf/SkPDFTypes.cpp",
diff --git a/src/pdf/SkPDFFont.cpp b/src/pdf/SkPDFFont.cpp
index db7bdeb2c2..8d55b6bfa7 100644
--- a/src/pdf/SkPDFFont.cpp
+++ b/src/pdf/SkPDFFont.cpp
@@ -19,6 +19,7 @@
#include "SkPDFMakeCIDGlyphWidthsArray.h"
#include "SkPDFMakeToUnicodeCmap.h"
#include "SkPDFResourceDict.h"
+#include "SkPDFSubsetFont.h"
#include "SkPDFUtils.h"
#include "SkPaint.h"
#include "SkRefCnt.h"
@@ -28,10 +29,6 @@
#include "SkTypes.h"
#include "SkUTF.h"
-#ifdef SK_PDF_USE_SFNTLY
- #include "sample/chromium/font_subsetter.h"
-#endif
-
SkExclusiveStrikePtr SkPDFFont::MakeVectorCache(SkTypeface* face, int* size) {
SkPaint tmpPaint;
tmpPaint.setHinting(kNo_SkFontHinting);
@@ -329,7 +326,8 @@ void SkPDFType0Font::emitObject(SkWStream* stream) const {
}
#endif
-#ifdef SK_PDF_USE_SFNTLY
+#ifdef SK_PDF_SUBSET_SUPPORTED
+
// if possible, make no copy.
static sk_sp<SkData> stream_to_data(std::unique_ptr<SkStreamAsset> stream) {
SkASSERT(stream);
@@ -343,56 +341,7 @@ static sk_sp<SkData> stream_to_data(std::unique_ptr<SkStreamAsset> stream) {
}
return SkData::MakeFromStream(stream.get(), size);
}
-
-static sk_sp<SkPDFStream> get_subset_font_stream(
- std::unique_ptr<SkStreamAsset> fontAsset,
- const SkPDFGlyphUse& glyphUsage,
- const char* fontName,
- int ttcIndex) {
- // Generate glyph id array in format needed by sfntly.
- // TODO(halcanary): sfntly should take a more compact format.
- std::vector<unsigned> subset;
- if (!glyphUsage.has(0)) {
- subset.push_back(0); // Always include glyph 0.
- }
- glyphUsage.getSetValues([&subset](unsigned v) { subset.push_back(v); });
-
- unsigned char* subsetFont{nullptr};
- sk_sp<SkData> fontData(stream_to_data(std::move(fontAsset)));
-#if defined(SK_BUILD_FOR_GOOGLE3)
- // TODO(halcanary): update SK_BUILD_FOR_GOOGLE3 to newest version of Sfntly.
- (void)ttcIndex;
- int subsetFontSize = SfntlyWrapper::SubsetFont(fontName,
- fontData->bytes(),
- fontData->size(),
- subset.data(),
- subset.size(),
- &subsetFont);
-#else
- (void)fontName;
- int subsetFontSize = SfntlyWrapper::SubsetFont(ttcIndex,
- fontData->bytes(),
- fontData->size(),
- subset.data(),
- subset.size(),
- &subsetFont);
-#endif
- fontData.reset();
- subset = std::vector<unsigned>();
- SkASSERT(subsetFontSize > 0 || subsetFont == nullptr);
- if (subsetFontSize < 1) {
- return nullptr;
- }
- SkASSERT(subsetFont != nullptr);
- auto subsetStream = sk_make_sp<SkPDFStream>(
- SkData::MakeWithProc(
- subsetFont, subsetFontSize,
- [](const void* p, void*) { delete[] (unsigned char*)p; },
- nullptr));
- subsetStream->dict()->insertInt("Length1", subsetFontSize);
- return subsetStream;
-}
-#endif // SK_PDF_USE_SFNTLY
+#endif // SK_PDF_SUBSET_SUPPORTED
void SkPDFType0Font::getFontSubset(SkPDFCanon* canon) {
const SkAdvancedTypefaceMetrics* metricsPtr =
@@ -419,14 +368,18 @@ void SkPDFType0Font::getFontSubset(SkPDFCanon* canon) {
} else {
switch (type) {
case SkAdvancedTypefaceMetrics::kTrueType_Font: {
- #ifdef SK_PDF_USE_SFNTLY
+ #ifdef SK_PDF_SUBSET_SUPPORTED
if (!SkToBool(metrics.fFlags &
SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag)) {
SkASSERT(this->firstGlyphID() == 1);
- sk_sp<SkPDFStream> subsetStream = get_subset_font_stream(
- std::move(fontAsset), this->glyphUsage(),
+ sk_sp<SkData> subsetFontData = SkPDFSubsetFont(
+ stream_to_data(std::move(fontAsset)), this->glyphUsage(),
metrics.fFontName.c_str(), ttcIndex);
- if (subsetStream) {
+ if (subsetFontData) {
+ size_t len = subsetFontData->size();
+ sk_sp<SkPDFStream> subsetStream = sk_make_sp<SkPDFStream>(
+ std::move(subsetFontData));
+ subsetStream->dict()->insertInt("Length1", SkToInt(len));
descriptor->insertObjRef("FontFile2", std::move(subsetStream));
break;
}
@@ -436,7 +389,7 @@ void SkPDFType0Font::getFontSubset(SkPDFCanon* canon) {
SkASSERT(fontAsset->getLength() == fontSize);
if (!fontAsset || fontAsset->getLength() == 0) { break; }
}
- #endif // SK_PDF_USE_SFNTLY
+ #endif // SK_PDF_SUBSET_SUPPORTED
auto fontStream = sk_make_sp<SkPDFSharedStream>(std::move(fontAsset));
fontStream->dict()->insertInt("Length1", fontSize);
descriptor->insertObjRef("FontFile2", std::move(fontStream));
diff --git a/src/pdf/SkPDFSubsetFont.cpp b/src/pdf/SkPDFSubsetFont.cpp
new file mode 100644
index 0000000000..4fae14af1d
--- /dev/null
+++ b/src/pdf/SkPDFSubsetFont.cpp
@@ -0,0 +1,50 @@
+// Copyright 2018 Google LLC.
+// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
+
+#include "SkPDFSubsetFont.h"
+
+#if defined(SK_PDF_USE_SFNTLY)
+
+#include "sample/chromium/font_subsetter.h"
+#include <vector>
+
+sk_sp<SkData> SkPDFSubsetFont(sk_sp<SkData> fontData,
+ const SkPDFGlyphUse& glyphUsage,
+ const char* fontName,
+ int ttcIndex) {
+ // Generate glyph id array in format needed by sfntly.
+ // TODO(halcanary): sfntly should take a more compact format.
+ std::vector<unsigned> subset;
+ if (!glyphUsage.has(0)) {
+ subset.push_back(0); // Always include glyph 0.
+ }
+ glyphUsage.getSetValues([&subset](unsigned v) { subset.push_back(v); });
+
+ unsigned char* subsetFont{nullptr};
+#if defined(SK_BUILD_FOR_GOOGLE3)
+ // TODO(halcanary): update SK_BUILD_FOR_GOOGLE3 to newest version of Sfntly.
+ (void)ttcIndex;
+ int subsetFontSize = SfntlyWrapper::SubsetFont(fontName,
+ fontData->bytes(),
+ fontData->size(),
+ subset.data(),
+ subset.size(),
+ &subsetFont);
+#else
+ (void)fontName;
+ int subsetFontSize = SfntlyWrapper::SubsetFont(ttcIndex,
+ fontData->bytes(),
+ fontData->size(),
+ subset.data(),
+ subset.size(),
+ &subsetFont);
+#endif
+ SkASSERT(subsetFontSize > 0 || subsetFont == nullptr);
+ if (subsetFontSize < 1 || subsetFont == nullptr) {
+ return nullptr;
+ }
+ return SkData::MakeWithProc(subsetFont, subsetFontSize,
+ [](const void* p, void*) { delete[] (unsigned char*)p; },
+ nullptr);
+}
+#endif
diff --git a/src/pdf/SkPDFSubsetFont.h b/src/pdf/SkPDFSubsetFont.h
new file mode 100644
index 0000000000..37b27016cb
--- /dev/null
+++ b/src/pdf/SkPDFSubsetFont.h
@@ -0,0 +1,18 @@
+// Copyright 2018 Google LLC.
+// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
+#ifndef SkPDFSubsetFont_DEFINED
+#define SkPDFSubsetFont_DEFINED
+
+#include "SkData.h"
+#include "SkPDFGlyphUse.h"
+
+#ifdef SK_PDF_USE_SFNTLY
+#define SK_PDF_SUBSET_SUPPORTED
+
+sk_sp<SkData> SkPDFSubsetFont(sk_sp<SkData> fontData,
+ const SkPDFGlyphUse& glyphUsage,
+ const char* fontName,
+ int ttcIndex);
+#endif
+
+#endif // SkPDFSubsetFont_DEFINED