Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(377)

Side by Side Diff: src/pdf/SkPDFFont.cpp

Issue 334443002: [PDF] Fix font embedding restrictions. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: rebase Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/pdf/SkPDFFont.h ('k') | src/ports/SkFontHost_FreeType.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include <ctype.h> 8 #include <ctype.h>
9 9
10 #include "SkData.h" 10 #include "SkData.h"
(...skipping 743 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 } 754 }
755 755
756 SkTypeface* SkPDFFont::typeface() { 756 SkTypeface* SkPDFFont::typeface() {
757 return fTypeface.get(); 757 return fTypeface.get();
758 } 758 }
759 759
760 SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() { 760 SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() {
761 return fFontType; 761 return fFontType;
762 } 762 }
763 763
764 bool SkPDFFont::canEmbed() const {
765 if (!fFontInfo.get()) {
766 SkASSERT(fFontType == SkAdvancedTypefaceMetrics::kOther_Font);
767 return true;
768 }
769 return (fFontInfo->fFlags &
770 SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag) == 0;
771 }
772
773 bool SkPDFFont::canSubset() const {
774 if (!fFontInfo.get()) {
775 SkASSERT(fFontType == SkAdvancedTypefaceMetrics::kOther_Font);
776 return true;
777 }
778 return (fFontInfo->fFlags &
779 SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag) == 0;
780 }
781
764 bool SkPDFFont::hasGlyph(uint16_t id) { 782 bool SkPDFFont::hasGlyph(uint16_t id) {
765 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0; 783 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0;
766 } 784 }
767 785
768 int SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs, int numGlyphs) { 786 int SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs, int numGlyphs) {
769 // A font with multibyte glyphs will support all glyph IDs in a single font. 787 // A font with multibyte glyphs will support all glyph IDs in a single font.
770 if (this->multiByteGlyphs()) { 788 if (this->multiByteGlyphs()) {
771 return numGlyphs; 789 return numGlyphs;
772 } 790 }
773 791
(...skipping 27 matching lines...) Expand all
801 SkAutoTUnref<SkAdvancedTypefaceMetrics> fontMetrics; 819 SkAutoTUnref<SkAdvancedTypefaceMetrics> fontMetrics;
802 SkPDFDict* relatedFontDescriptor = NULL; 820 SkPDFDict* relatedFontDescriptor = NULL;
803 if (relatedFontIndex >= 0) { 821 if (relatedFontIndex >= 0) {
804 SkPDFFont* relatedFont = CanonicalFonts()[relatedFontIndex].fFont; 822 SkPDFFont* relatedFont = CanonicalFonts()[relatedFontIndex].fFont;
805 fontMetrics.reset(relatedFont->fontInfo()); 823 fontMetrics.reset(relatedFont->fontInfo());
806 SkSafeRef(fontMetrics.get()); 824 SkSafeRef(fontMetrics.get());
807 relatedFontDescriptor = relatedFont->getFontDescriptor(); 825 relatedFontDescriptor = relatedFont->getFontDescriptor();
808 826
809 // This only is to catch callers who pass invalid glyph ids. 827 // This only is to catch callers who pass invalid glyph ids.
810 // If glyph id is invalid, then we will create duplicate entries 828 // If glyph id is invalid, then we will create duplicate entries
811 // for True Type fonts. 829 // for TrueType fonts.
812 SkAdvancedTypefaceMetrics::FontType fontType = 830 SkAdvancedTypefaceMetrics::FontType fontType =
813 fontMetrics.get() ? fontMetrics.get()->fType : 831 fontMetrics.get() ? fontMetrics.get()->fType :
814 SkAdvancedTypefaceMetrics::kOther_Font; 832 SkAdvancedTypefaceMetrics::kOther_Font;
815 833
816 if (fontType == SkAdvancedTypefaceMetrics::kType1CID_Font || 834 if (fontType == SkAdvancedTypefaceMetrics::kType1CID_Font ||
817 fontType == SkAdvancedTypefaceMetrics::kTrueType_Font) { 835 fontType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
818 CanonicalFonts()[relatedFontIndex].fFont->ref(); 836 CanonicalFonts()[relatedFontIndex].fFont->ref();
819 return CanonicalFonts()[relatedFontIndex].fFont; 837 return CanonicalFonts()[relatedFontIndex].fFont;
820 } 838 }
821 } else { 839 } else {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
881 } 899 }
882 900
883 SkPDFFont::SkPDFFont(SkAdvancedTypefaceMetrics* info, SkTypeface* typeface, 901 SkPDFFont::SkPDFFont(SkAdvancedTypefaceMetrics* info, SkTypeface* typeface,
884 SkPDFDict* relatedFontDescriptor) 902 SkPDFDict* relatedFontDescriptor)
885 : SkPDFDict("Font"), 903 : SkPDFDict("Font"),
886 fTypeface(ref_or_default(typeface)), 904 fTypeface(ref_or_default(typeface)),
887 fFirstGlyphID(1), 905 fFirstGlyphID(1),
888 fLastGlyphID(info ? info->fLastGlyphID : 0), 906 fLastGlyphID(info ? info->fLastGlyphID : 0),
889 fFontInfo(SkSafeRef(info)), 907 fFontInfo(SkSafeRef(info)),
890 fDescriptor(SkSafeRef(relatedFontDescriptor)) { 908 fDescriptor(SkSafeRef(relatedFontDescriptor)) {
891 if (info == NULL) { 909 if (info == NULL ||
892 fFontType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font; 910 info->fFlags & SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag) {
893 } else if (info->fMultiMaster) {
894 fFontType = SkAdvancedTypefaceMetrics::kOther_Font; 911 fFontType = SkAdvancedTypefaceMetrics::kOther_Font;
895 } else { 912 } else {
896 fFontType = info->fType; 913 fFontType = info->fType;
897 } 914 }
898 } 915 }
899 916
900 // static 917 // static
901 SkPDFFont* SkPDFFont::Create(SkAdvancedTypefaceMetrics* info, 918 SkPDFFont* SkPDFFont::Create(SkAdvancedTypefaceMetrics* info,
902 SkTypeface* typeface, uint16_t glyphID, 919 SkTypeface* typeface, uint16_t glyphID,
903 SkPDFDict* relatedFontDescriptor) { 920 SkPDFDict* relatedFontDescriptor) {
904 SkAdvancedTypefaceMetrics::FontType type = 921 SkAdvancedTypefaceMetrics::FontType type =
905 info ? info->fType : SkAdvancedTypefaceMetrics::kNotEmbeddable_Font; 922 info ? info->fType : SkAdvancedTypefaceMetrics::kOther_Font;
906 923
907 if (info && info->fMultiMaster) { 924 if (info &&
925 (info->fFlags & SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag)) {
908 NOT_IMPLEMENTED(true, true); 926 NOT_IMPLEMENTED(true, true);
909 return new SkPDFType3Font(info, 927 return new SkPDFType3Font(info,
910 typeface, 928 typeface,
911 glyphID); 929 glyphID);
912 } 930 }
913 if (type == SkAdvancedTypefaceMetrics::kType1CID_Font || 931 if (type == SkAdvancedTypefaceMetrics::kType1CID_Font ||
914 type == SkAdvancedTypefaceMetrics::kTrueType_Font) { 932 type == SkAdvancedTypefaceMetrics::kTrueType_Font) {
915 SkASSERT(relatedFontDescriptor == NULL); 933 SkASSERT(relatedFontDescriptor == NULL);
916 return new SkPDFType0Font(info, typeface); 934 return new SkPDFType0Font(info, typeface);
917 } 935 }
918 if (type == SkAdvancedTypefaceMetrics::kType1_Font) { 936 if (type == SkAdvancedTypefaceMetrics::kType1_Font) {
919 return new SkPDFType1Font(info, 937 return new SkPDFType1Font(info,
920 typeface, 938 typeface,
921 glyphID, 939 glyphID,
922 relatedFontDescriptor); 940 relatedFontDescriptor);
923 } 941 }
924 942
925 SkASSERT(type == SkAdvancedTypefaceMetrics::kCFF_Font || 943 SkASSERT(type == SkAdvancedTypefaceMetrics::kCFF_Font ||
926 type == SkAdvancedTypefaceMetrics::kOther_Font || 944 type == SkAdvancedTypefaceMetrics::kOther_Font);
927 type == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font);
928 945
929 return new SkPDFType3Font(info, typeface, glyphID); 946 return new SkPDFType3Font(info, typeface, glyphID);
930 } 947 }
931 948
932 SkAdvancedTypefaceMetrics* SkPDFFont::fontInfo() { 949 SkAdvancedTypefaceMetrics* SkPDFFont::fontInfo() {
933 return fFontInfo.get(); 950 return fFontInfo.get();
934 } 951 }
935 952
936 void SkPDFFont::setFontInfo(SkAdvancedTypefaceMetrics* info) { 953 void SkPDFFont::setFontInfo(SkAdvancedTypefaceMetrics* info) {
937 if (info == NULL || info == fFontInfo.get()) { 954 if (info == NULL || info == fFontInfo.get()) {
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
1045 } 1062 }
1046 1063
1047 /////////////////////////////////////////////////////////////////////////////// 1064 ///////////////////////////////////////////////////////////////////////////////
1048 // class SkPDFType0Font 1065 // class SkPDFType0Font
1049 /////////////////////////////////////////////////////////////////////////////// 1066 ///////////////////////////////////////////////////////////////////////////////
1050 1067
1051 SkPDFType0Font::SkPDFType0Font(SkAdvancedTypefaceMetrics* info, 1068 SkPDFType0Font::SkPDFType0Font(SkAdvancedTypefaceMetrics* info,
1052 SkTypeface* typeface) 1069 SkTypeface* typeface)
1053 : SkPDFFont(info, typeface, NULL) { 1070 : SkPDFFont(info, typeface, NULL) {
1054 SkDEBUGCODE(fPopulated = false); 1071 SkDEBUGCODE(fPopulated = false);
1072 if (!canSubset()) {
1073 populate(NULL);
1074 }
1055 } 1075 }
1056 1076
1057 SkPDFType0Font::~SkPDFType0Font() {} 1077 SkPDFType0Font::~SkPDFType0Font() {}
1058 1078
1059 SkPDFFont* SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) { 1079 SkPDFFont* SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) {
1080 if (!canSubset()) {
1081 return NULL;
1082 }
1060 SkPDFType0Font* newSubset = new SkPDFType0Font(fontInfo(), typeface()); 1083 SkPDFType0Font* newSubset = new SkPDFType0Font(fontInfo(), typeface());
1061 newSubset->populate(subset); 1084 newSubset->populate(subset);
1062 return newSubset; 1085 return newSubset;
1063 } 1086 }
1064 1087
1065 #ifdef SK_DEBUG 1088 #ifdef SK_DEBUG
1066 void SkPDFType0Font::emitObject(SkWStream* stream, SkPDFCatalog* catalog, 1089 void SkPDFType0Font::emitObject(SkWStream* stream, SkPDFCatalog* catalog,
1067 bool indirect) { 1090 bool indirect) {
1068 SkASSERT(fPopulated); 1091 SkASSERT(fPopulated);
1069 return INHERITED::emitObject(stream, catalog, indirect); 1092 return INHERITED::emitObject(stream, catalog, indirect);
(...skipping 28 matching lines...) Expand all
1098 populate(subset); 1121 populate(subset);
1099 } 1122 }
1100 1123
1101 SkPDFCIDFont::~SkPDFCIDFont() {} 1124 SkPDFCIDFont::~SkPDFCIDFont() {}
1102 1125
1103 bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth, 1126 bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth,
1104 const SkTDArray<uint32_t>* subset) { 1127 const SkTDArray<uint32_t>* subset) {
1105 SkAutoTUnref<SkPDFDict> descriptor(new SkPDFDict("FontDescriptor")); 1128 SkAutoTUnref<SkPDFDict> descriptor(new SkPDFDict("FontDescriptor"));
1106 setFontDescriptor(descriptor.get()); 1129 setFontDescriptor(descriptor.get());
1107 addResource(descriptor.get()); 1130 addResource(descriptor.get());
1131 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1132 if (!addCommonFontDescriptorEntries(defaultWidth)) {
1133 return false;
1134 }
1135 if (!canEmbed()) {
1136 return true;
1137 }
1108 1138
1109 switch (getType()) { 1139 switch (getType()) {
1110 case SkAdvancedTypefaceMetrics::kTrueType_Font: { 1140 case SkAdvancedTypefaceMetrics::kTrueType_Font: {
1111 SkASSERT(subset); 1141 SkAutoTUnref<SkPDFStream> fontStream;
1112 // Font subsetting 1142 size_t fontSize = 0;
1113 SkPDFStream* rawStream = NULL; 1143 if (canSubset()) {
1114 size_t fontSize = get_subset_font_stream(fontInfo()->fFontName.c_str (), 1144 SkPDFStream* rawStream = NULL;
1115 typeface(), 1145 fontSize = get_subset_font_stream(fontInfo()->fFontName.c_str(),
1116 *subset, 1146 typeface(),
1117 &rawStream); 1147 *subset,
1148 &rawStream);
1149 fontStream.reset(rawStream);
1150 } else {
1151 int ttcIndex;
1152 SkAutoTUnref<SkStream> fontData(
1153 typeface()->openStream(&ttcIndex));
1154 fontStream.reset(new SkPDFStream(fontData.get()));
1155 fontSize = fontData->getLength();
1156 }
1118 SkASSERT(fontSize); 1157 SkASSERT(fontSize);
1119 SkASSERT(rawStream); 1158 SkASSERT(fontStream.get());
1120 SkAutoTUnref<SkPDFStream> fontStream(rawStream);
1121 addResource(fontStream.get()); 1159 addResource(fontStream.get());
1122 1160
1123 fontStream->insertInt("Length1", fontSize); 1161 fontStream->insertInt("Length1", fontSize);
1124 descriptor->insert("FontFile2", 1162 descriptor->insert("FontFile2",
1125 new SkPDFObjRef(fontStream.get()))->unref(); 1163 new SkPDFObjRef(fontStream.get()))->unref();
1126 break; 1164 break;
1127 } 1165 }
1128 case SkAdvancedTypefaceMetrics::kCFF_Font: 1166 case SkAdvancedTypefaceMetrics::kCFF_Font:
1129 case SkAdvancedTypefaceMetrics::kType1CID_Font: { 1167 case SkAdvancedTypefaceMetrics::kType1CID_Font: {
1130 int ttcIndex; 1168 int ttcIndex;
1131 SkAutoTUnref<SkStream> fontData(typeface()->openStream(&ttcIndex)); 1169 SkAutoTUnref<SkStream> fontData(typeface()->openStream(&ttcIndex));
1132 SkAutoTUnref<SkPDFStream> fontStream( 1170 SkAutoTUnref<SkPDFStream> fontStream(
1133 new SkPDFStream(fontData.get())); 1171 new SkPDFStream(fontData.get()));
1134 addResource(fontStream.get()); 1172 addResource(fontStream.get());
1135 1173
1136 if (getType() == SkAdvancedTypefaceMetrics::kCFF_Font) { 1174 if (getType() == SkAdvancedTypefaceMetrics::kCFF_Font) {
1137 fontStream->insertName("Subtype", "Type1C"); 1175 fontStream->insertName("Subtype", "Type1C");
1138 } else { 1176 } else {
1139 fontStream->insertName("Subtype", "CIDFontType0c"); 1177 fontStream->insertName("Subtype", "CIDFontType0c");
1140 } 1178 }
1141 descriptor->insert("FontFile3", 1179 descriptor->insert("FontFile3",
1142 new SkPDFObjRef(fontStream.get()))->unref(); 1180 new SkPDFObjRef(fontStream.get()))->unref();
1143 break; 1181 break;
1144 } 1182 }
1145 default: 1183 default:
1146 SkASSERT(false); 1184 SkASSERT(false);
1147 } 1185 }
1148 1186 return true;
1149 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1150 return addCommonFontDescriptorEntries(defaultWidth);
1151 } 1187 }
1152 1188
1153 bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) { 1189 bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) {
1154 // Generate new font metrics with advance info for true type fonts. 1190 // Generate new font metrics with advance info for true type fonts.
1155 if (fontInfo()->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) { 1191 if (fontInfo()->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
1156 // Generate glyph id array. 1192 // Generate glyph id array.
1157 SkTDArray<uint32_t> glyphIDs; 1193 SkTDArray<uint32_t> glyphIDs;
1158 if (subset) { 1194 if (subset) {
1159 // Always include glyph 0. 1195 // Always include glyph 0.
1160 if (!subset->has(0)) { 1196 if (!subset->has(0)) {
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
1259 int ttcIndex; 1295 int ttcIndex;
1260 size_t header SK_INIT_TO_AVOID_WARNING; 1296 size_t header SK_INIT_TO_AVOID_WARNING;
1261 size_t data SK_INIT_TO_AVOID_WARNING; 1297 size_t data SK_INIT_TO_AVOID_WARNING;
1262 size_t trailer SK_INIT_TO_AVOID_WARNING; 1298 size_t trailer SK_INIT_TO_AVOID_WARNING;
1263 SkAutoTUnref<SkStream> rawFontData(typeface()->openStream(&ttcIndex)); 1299 SkAutoTUnref<SkStream> rawFontData(typeface()->openStream(&ttcIndex));
1264 SkStream* fontData = handleType1Stream(rawFontData.get(), &header, &data, 1300 SkStream* fontData = handleType1Stream(rawFontData.get(), &header, &data,
1265 &trailer); 1301 &trailer);
1266 if (fontData == NULL) { 1302 if (fontData == NULL) {
1267 return false; 1303 return false;
1268 } 1304 }
1269 SkAutoTUnref<SkPDFStream> fontStream(new SkPDFStream(fontData)); 1305 if (canEmbed()) {
1270 addResource(fontStream.get()); 1306 SkAutoTUnref<SkPDFStream> fontStream(new SkPDFStream(fontData));
1271 fontStream->insertInt("Length1", header); 1307 addResource(fontStream.get());
1272 fontStream->insertInt("Length2", data); 1308 fontStream->insertInt("Length1", header);
1273 fontStream->insertInt("Length3", trailer); 1309 fontStream->insertInt("Length2", data);
1274 descriptor->insert("FontFile", new SkPDFObjRef(fontStream.get()))->unref(); 1310 fontStream->insertInt("Length3", trailer);
1311 descriptor->insert("FontFile",
1312 new SkPDFObjRef(fontStream.get()))->unref();
1313 }
1275 1314
1276 addResource(descriptor.get()); 1315 addResource(descriptor.get());
1277 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref(); 1316 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1278 1317
1279 return addCommonFontDescriptorEntries(defaultWidth); 1318 return addCommonFontDescriptorEntries(defaultWidth);
1280 } 1319 }
1281 1320
1282 bool SkPDFType1Font::populate(int16_t glyphID) { 1321 bool SkPDFType1Font::populate(int16_t glyphID) {
1283 SkASSERT(!fontInfo()->fVerticalMetrics.get()); 1322 SkASSERT(!fontInfo()->fVerticalMetrics.get());
1284 SkASSERT(fontInfo()->fGlyphWidths.get()); 1323 SkASSERT(fontInfo()->fGlyphWidths.get());
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
1436 1475
1437 insert("FontBBox", makeFontBBox(bbox, 1000))->unref(); 1476 insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
1438 insertInt("FirstChar", 1); 1477 insertInt("FirstChar", 1);
1439 insertInt("LastChar", lastGlyphID() - firstGlyphID() + 1); 1478 insertInt("LastChar", lastGlyphID() - firstGlyphID() + 1);
1440 insert("Widths", widthArray.get()); 1479 insert("Widths", widthArray.get());
1441 insertName("CIDToGIDMap", "Identity"); 1480 insertName("CIDToGIDMap", "Identity");
1442 1481
1443 populateToUnicodeTable(NULL); 1482 populateToUnicodeTable(NULL);
1444 return true; 1483 return true;
1445 } 1484 }
OLDNEW
« no previous file with comments | « src/pdf/SkPDFFont.h ('k') | src/ports/SkFontHost_FreeType.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698