[pdf] Give up on embedding CFF

Several attempts have been made to embed CFF data in the generated PDFs
instead of emitting Type3 as this generally leads to a better user
experience when supported. However, OpenType in PDF is still not
universally supported and bare CFF in PDF requires writing out the
internal CID values which Skia does not know.

Bug: b/367544743
Change-Id: Ied59ca6799824a2396368dc8c74c2b93e526fb7a
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/903036
Reviewed-by: Florin Malita <fmalita@google.com>
Commit-Queue: Ben Wagner <bungeman@google.com>
diff --git a/src/pdf/SkPDFFont.cpp b/src/pdf/SkPDFFont.cpp
index 69f84d1..38d04c0 100644
--- a/src/pdf/SkPDFFont.cpp
+++ b/src/pdf/SkPDFFont.cpp
@@ -17,7 +17,6 @@
 #include "include/core/SkFontMetrics.h"
 #include "include/core/SkFontStyle.h"
 #include "include/core/SkFontTypes.h"
-#include "include/core/SkFourByteTag.h"
 #include "include/core/SkImage.h"
 #include "include/core/SkImageInfo.h"
 #include "include/core/SkMaskFilter.h"
@@ -326,6 +325,12 @@
         // https://skia-review.googlesource.com/c/skia/+/543485
         SkToBool(metrics.fFlags & SkAdvancedTypefaceMetrics::kAltDataFormat_FontFlag) ||
         SkToBool(metrics.fFlags & SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag) ||
+        // Something like 45eeeddb00741493 and 7c86e7641b348ca7b0 to output OpenType should work,
+        // but requires PDF 1.6 which is still not supported by all printers. One could fix this by
+        // using bare CFF like 31a170226c22244cbd00497b67f6ae181f0f3e76 which is only PDF 1.3,
+        // but this only works when the CFF CIDs == CFF index == GlyphID as PDF bare CFF prefers
+        // CFF CIDs instead of GlyphIDs and Skia doesn't know the CIDs.
+        metrics.fType == SkAdvancedTypefaceMetrics::kCFF_Font ||
         pdfStrike.fHasMaskFilter)
     {
         // force Type3 fallback.
@@ -447,27 +452,12 @@
         SkDebugf("Error: (SkTypeface)(%p)::openStream() returned "
                  "empty stream (%p) when identified as kType1CID_Font "
                  "or kTrueType_Font.\n", &typeface, fontAsset.get());
-    } else if (type == SkAdvancedTypefaceMetrics::kTrueType_Font ||
-               type == SkAdvancedTypefaceMetrics::kCFF_Font)
-    {
-        // Avoid use of FontFile3 OpenType (OpenType with CFF) which is PDF 1.6 (2004).
-        // Instead use FontFile3 CIDFontType0C (bare CFF) which is PDF 1.3 (2000).
-        // See b/352098914
+    } else if (type == SkAdvancedTypefaceMetrics::kTrueType_Font) {
         sk_sp<SkData> subsetFontData;
         if (can_subset(metrics)) {
             SkASSERT(font.firstGlyphID() == 1);
-            // If the face has CFF the subsetter will always return just the CFF.
             subsetFontData = SkPDFSubsetFont(typeface, font.glyphUsage());
         }
-        if (!subsetFontData) {
-            // If the data cannot be subset, still ensure bare CFF.
-            constexpr SkFontTableTag CFFTag = SkSetFourByteTag('C', 'F', 'F', ' ');
-            size_t cffTableSize = typeface.getTableSize(CFFTag);
-            if (cffTableSize) {
-                subsetFontData = SkData::MakeUninitialized(cffTableSize);
-                typeface.getTableData(CFFTag, 0, cffTableSize, subsetFontData->writable_data());
-            }
-        }
         std::unique_ptr<SkStreamAsset> subsetFontAsset;
         if (subsetFontData) {
             subsetFontAsset = SkMemoryStream::Make(std::move(subsetFontData));
@@ -477,14 +467,7 @@
         }
         std::unique_ptr<SkPDFDict> streamDict = SkPDFMakeDict();
         streamDict->insertInt("Length1", subsetFontAsset->getLength());
-        const char* fontFileKey;
-        if (type == SkAdvancedTypefaceMetrics::kTrueType_Font) {
-            fontFileKey = "FontFile2";
-        } else {
-            streamDict->insertName("Subtype", "CIDFontType0C");
-            fontFileKey = "FontFile3";
-        }
-        descriptor->insertRef(fontFileKey,
+        descriptor->insertRef("FontFile2",
                               SkPDFStreamOut(std::move(streamDict), std::move(subsetFontAsset),
                                              doc, SkPDFSteamCompressionEnabled::Yes));
     } else if (type == SkAdvancedTypefaceMetrics::kType1CID_Font) {
@@ -505,10 +488,6 @@
         case SkAdvancedTypefaceMetrics::kType1CID_Font:
             newCIDFont->insertName("Subtype", "CIDFontType0");
             break;
-        case SkAdvancedTypefaceMetrics::kCFF_Font:
-            newCIDFont->insertName("Subtype", "CIDFontType0");
-            newCIDFont->insertName("CIDToGIDMap", "Identity");
-            break;
         case SkAdvancedTypefaceMetrics::kTrueType_Font:
             newCIDFont->insertName("Subtype", "CIDFontType2");
             newCIDFont->insertName("CIDToGIDMap", "Identity");
@@ -928,7 +907,6 @@
     switch (fFontType) {
         case SkAdvancedTypefaceMetrics::kType1CID_Font:
         case SkAdvancedTypefaceMetrics::kTrueType_Font:
-        case SkAdvancedTypefaceMetrics::kCFF_Font:
             return emit_subset_type0(*this, doc);
 #ifndef SK_PDF_DO_NOT_SUPPORT_TYPE_1_FONTS
         case SkAdvancedTypefaceMetrics::kType1_Font:
diff --git a/src/pdf/SkPDFSubsetFont.cpp b/src/pdf/SkPDFSubsetFont.cpp
index 5ab82fb..307c430 100644
--- a/src/pdf/SkPDFSubsetFont.cpp
+++ b/src/pdf/SkPDFSubsetFont.cpp
@@ -59,12 +59,6 @@
                                 blob.release());
 }
 
-sk_sp<SkData> extract_cff_data(const hb_face_t* face) {
-    // hb_face_reference_table usually returns hb_blob_get_empty instead of nullptr.
-    HBBlob cff(hb_face_reference_table(face, HB_TAG('C','F','F',' ')));
-    return to_data(std::move(cff));
-}
-
 HBFace make_subset(hb_subset_input_t* input, hb_face_t* face, bool retainZeroGlyph) {
     // TODO: When possible, check if a font is 'tricky' with FT_IS_TRICKY.
     // If it isn't known if a font is 'tricky', retain the hints.
@@ -102,13 +96,7 @@
 
     HBFace subset = make_subset(input.get(), face.get(), glyphUsage.has(0));
     if (!subset) {
-        // Even if subsetting fails, extract CFF if available
-        return extract_cff_data(face.get());
-    }
-
-    // Extract subset CFF if available
-    if (sk_sp<SkData> cffData = extract_cff_data(subset.get())) {
-        return cffData;
+        return nullptr;
     }
 
     HBBlob result(hb_face_reference_blob(subset.get()));
diff --git a/src/pdf/SkPDFSubsetFont.h b/src/pdf/SkPDFSubsetFont.h
index 3ee239d..6d4e8e9 100644
--- a/src/pdf/SkPDFSubsetFont.h
+++ b/src/pdf/SkPDFSubsetFont.h
@@ -10,11 +10,8 @@
 class SkTypeface;
 
 /** Subset the typeface's data to only include the glyphs used.
- *
  *  The glyph ids will remain the same.
  *
- *  If the font data contains a CFF table, only the (possibly subset) CFF table will be returned.
- *
  *  @return The subset font data, or nullptr if it cannot be subset.
  */
 sk_sp<SkData> SkPDFSubsetFont(const SkTypeface& typeface, const SkPDFGlyphUse& glyphUsage);