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

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

Issue 107863002: [PDF] Fix font embedding restrictions. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years 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 | Annotate | Revision Log
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 731 matching lines...) Expand 10 before | Expand all | Expand 10 after
742 } 742 }
743 743
744 SkTypeface* SkPDFFont::typeface() { 744 SkTypeface* SkPDFFont::typeface() {
745 return fTypeface.get(); 745 return fTypeface.get();
746 } 746 }
747 747
748 SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() { 748 SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() {
749 return fFontType; 749 return fFontType;
750 } 750 }
751 751
752 bool SkPDFFont::canEmbed() const {
753 if (!fFontInfo.get()) {
754 SkASSERT(fFontType == SkAdvancedTypefaceMetrics::kOther_Font);
755 return true;
756 }
757 return (fFontInfo->fFlags &
758 SkAdvancedTypefaceMetrics::kNotEmbeddable_Flag) == 0;
759 }
760
761 bool SkPDFFont::canSubset() const {
762 if (!fFontInfo.get()) {
763 SkASSERT(fFontType == SkAdvancedTypefaceMetrics::kOther_Font);
764 return true;
765 }
766 return (fFontInfo->fFlags &
767 SkAdvancedTypefaceMetrics::kNotSubsettable_Flag) == 0;
768 }
769
752 bool SkPDFFont::hasGlyph(uint16_t id) { 770 bool SkPDFFont::hasGlyph(uint16_t id) {
753 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0; 771 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0;
754 } 772 }
755 773
756 size_t SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs, 774 size_t SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs,
757 size_t numGlyphs) { 775 size_t numGlyphs) {
758 // A font with multibyte glyphs will support all glyph IDs in a single font. 776 // A font with multibyte glyphs will support all glyph IDs in a single font.
759 if (this->multiByteGlyphs()) { 777 if (this->multiByteGlyphs()) {
760 return numGlyphs; 778 return numGlyphs;
761 } 779 }
(...skipping 28 matching lines...) Expand all
790 SkAutoTUnref<SkAdvancedTypefaceMetrics> fontMetrics; 808 SkAutoTUnref<SkAdvancedTypefaceMetrics> fontMetrics;
791 SkPDFDict* relatedFontDescriptor = NULL; 809 SkPDFDict* relatedFontDescriptor = NULL;
792 if (relatedFontIndex >= 0) { 810 if (relatedFontIndex >= 0) {
793 SkPDFFont* relatedFont = CanonicalFonts()[relatedFontIndex].fFont; 811 SkPDFFont* relatedFont = CanonicalFonts()[relatedFontIndex].fFont;
794 fontMetrics.reset(relatedFont->fontInfo()); 812 fontMetrics.reset(relatedFont->fontInfo());
795 SkSafeRef(fontMetrics.get()); 813 SkSafeRef(fontMetrics.get());
796 relatedFontDescriptor = relatedFont->getFontDescriptor(); 814 relatedFontDescriptor = relatedFont->getFontDescriptor();
797 815
798 // This only is to catch callers who pass invalid glyph ids. 816 // This only is to catch callers who pass invalid glyph ids.
799 // If glyph id is invalid, then we will create duplicate entries 817 // If glyph id is invalid, then we will create duplicate entries
800 // for True Type fonts. 818 // for TrueType fonts.
801 SkAdvancedTypefaceMetrics::FontType fontType = 819 SkAdvancedTypefaceMetrics::FontType fontType =
802 fontMetrics.get() ? fontMetrics.get()->fType : 820 fontMetrics.get() ? fontMetrics.get()->fType :
803 SkAdvancedTypefaceMetrics::kOther_Font; 821 SkAdvancedTypefaceMetrics::kOther_Font;
804 822
805 if (fontType == SkAdvancedTypefaceMetrics::kType1CID_Font || 823 if (fontType == SkAdvancedTypefaceMetrics::kType1CID_Font ||
806 fontType == SkAdvancedTypefaceMetrics::kTrueType_Font) { 824 fontType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
807 CanonicalFonts()[relatedFontIndex].fFont->ref(); 825 CanonicalFonts()[relatedFontIndex].fFont->ref();
808 return CanonicalFonts()[relatedFontIndex].fFont; 826 return CanonicalFonts()[relatedFontIndex].fFont;
809 } 827 }
810 } else { 828 } else {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
870 } 888 }
871 889
872 SkPDFFont::SkPDFFont(SkAdvancedTypefaceMetrics* info, SkTypeface* typeface, 890 SkPDFFont::SkPDFFont(SkAdvancedTypefaceMetrics* info, SkTypeface* typeface,
873 SkPDFDict* relatedFontDescriptor) 891 SkPDFDict* relatedFontDescriptor)
874 : SkPDFDict("Font"), 892 : SkPDFDict("Font"),
875 fTypeface(ref_or_default(typeface)), 893 fTypeface(ref_or_default(typeface)),
876 fFirstGlyphID(1), 894 fFirstGlyphID(1),
877 fLastGlyphID(info ? info->fLastGlyphID : 0), 895 fLastGlyphID(info ? info->fLastGlyphID : 0),
878 fFontInfo(SkSafeRef(info)), 896 fFontInfo(SkSafeRef(info)),
879 fDescriptor(SkSafeRef(relatedFontDescriptor)) { 897 fDescriptor(SkSafeRef(relatedFontDescriptor)) {
880 if (info == NULL) { 898 if (info == NULL ||
881 fFontType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font; 899 info->fFlags & SkAdvancedTypefaceMetrics::kMultiMaster_Flag) {
882 } else if (info->fMultiMaster) {
883 fFontType = SkAdvancedTypefaceMetrics::kOther_Font; 900 fFontType = SkAdvancedTypefaceMetrics::kOther_Font;
884 } else { 901 } else {
885 fFontType = info->fType; 902 fFontType = info->fType;
886 } 903 }
887 } 904 }
888 905
889 // static 906 // static
890 SkPDFFont* SkPDFFont::Create(SkAdvancedTypefaceMetrics* info, 907 SkPDFFont* SkPDFFont::Create(SkAdvancedTypefaceMetrics* info,
891 SkTypeface* typeface, uint16_t glyphID, 908 SkTypeface* typeface, uint16_t glyphID,
892 SkPDFDict* relatedFontDescriptor) { 909 SkPDFDict* relatedFontDescriptor) {
893 SkAdvancedTypefaceMetrics::FontType type = 910 SkAdvancedTypefaceMetrics::FontType type =
894 info ? info->fType : SkAdvancedTypefaceMetrics::kNotEmbeddable_Font; 911 info ? info->fType : SkAdvancedTypefaceMetrics::kOther_Font;
895 912
896 if (info && info->fMultiMaster) { 913 if (info && (info->fFlags & SkAdvancedTypefaceMetrics::kMultiMaster_Flag)) {
897 NOT_IMPLEMENTED(true, true); 914 NOT_IMPLEMENTED(true, true);
898 return new SkPDFType3Font(info, 915 return new SkPDFType3Font(info,
899 typeface, 916 typeface,
900 glyphID); 917 glyphID);
901 } 918 }
902 if (type == SkAdvancedTypefaceMetrics::kType1CID_Font || 919 if (type == SkAdvancedTypefaceMetrics::kType1CID_Font ||
903 type == SkAdvancedTypefaceMetrics::kTrueType_Font) { 920 type == SkAdvancedTypefaceMetrics::kTrueType_Font) {
904 SkASSERT(relatedFontDescriptor == NULL); 921 SkASSERT(relatedFontDescriptor == NULL);
905 return new SkPDFType0Font(info, typeface); 922 return new SkPDFType0Font(info, typeface);
906 } 923 }
907 if (type == SkAdvancedTypefaceMetrics::kType1_Font) { 924 if (type == SkAdvancedTypefaceMetrics::kType1_Font) {
908 return new SkPDFType1Font(info, 925 return new SkPDFType1Font(info,
909 typeface, 926 typeface,
910 glyphID, 927 glyphID,
911 relatedFontDescriptor); 928 relatedFontDescriptor);
912 } 929 }
913 930
914 SkASSERT(type == SkAdvancedTypefaceMetrics::kCFF_Font || 931 SkASSERT(type == SkAdvancedTypefaceMetrics::kCFF_Font ||
915 type == SkAdvancedTypefaceMetrics::kOther_Font || 932 type == SkAdvancedTypefaceMetrics::kOther_Font);
916 type == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font);
917 933
918 return new SkPDFType3Font(info, typeface, glyphID); 934 return new SkPDFType3Font(info, typeface, glyphID);
919 } 935 }
920 936
921 SkAdvancedTypefaceMetrics* SkPDFFont::fontInfo() { 937 SkAdvancedTypefaceMetrics* SkPDFFont::fontInfo() {
922 return fFontInfo.get(); 938 return fFontInfo.get();
923 } 939 }
924 940
925 void SkPDFFont::setFontInfo(SkAdvancedTypefaceMetrics* info) { 941 void SkPDFFont::setFontInfo(SkAdvancedTypefaceMetrics* info) {
926 if (info == NULL || info == fFontInfo.get()) { 942 if (info == NULL || info == fFontInfo.get()) {
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
1033 } 1049 }
1034 1050
1035 /////////////////////////////////////////////////////////////////////////////// 1051 ///////////////////////////////////////////////////////////////////////////////
1036 // class SkPDFType0Font 1052 // class SkPDFType0Font
1037 /////////////////////////////////////////////////////////////////////////////// 1053 ///////////////////////////////////////////////////////////////////////////////
1038 1054
1039 SkPDFType0Font::SkPDFType0Font(SkAdvancedTypefaceMetrics* info, 1055 SkPDFType0Font::SkPDFType0Font(SkAdvancedTypefaceMetrics* info,
1040 SkTypeface* typeface) 1056 SkTypeface* typeface)
1041 : SkPDFFont(info, typeface, NULL) { 1057 : SkPDFFont(info, typeface, NULL) {
1042 SkDEBUGCODE(fPopulated = false); 1058 SkDEBUGCODE(fPopulated = false);
1059 if (!canSubset()) {
1060 populate(NULL);
1061 }
1043 } 1062 }
1044 1063
1045 SkPDFType0Font::~SkPDFType0Font() {} 1064 SkPDFType0Font::~SkPDFType0Font() {}
1046 1065
1047 SkPDFFont* SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) { 1066 SkPDFFont* SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) {
1067 if (!canSubset()) {
1068 return NULL;
1069 }
1048 SkPDFType0Font* newSubset = new SkPDFType0Font(fontInfo(), typeface()); 1070 SkPDFType0Font* newSubset = new SkPDFType0Font(fontInfo(), typeface());
1049 newSubset->populate(subset); 1071 newSubset->populate(subset);
1050 return newSubset; 1072 return newSubset;
1051 } 1073 }
1052 1074
1053 #ifdef SK_DEBUG 1075 #ifdef SK_DEBUG
1054 void SkPDFType0Font::emitObject(SkWStream* stream, SkPDFCatalog* catalog, 1076 void SkPDFType0Font::emitObject(SkWStream* stream, SkPDFCatalog* catalog,
1055 bool indirect) { 1077 bool indirect) {
1056 SkASSERT(fPopulated); 1078 SkASSERT(fPopulated);
1057 return INHERITED::emitObject(stream, catalog, indirect); 1079 return INHERITED::emitObject(stream, catalog, indirect);
(...skipping 28 matching lines...) Expand all
1086 populate(subset); 1108 populate(subset);
1087 } 1109 }
1088 1110
1089 SkPDFCIDFont::~SkPDFCIDFont() {} 1111 SkPDFCIDFont::~SkPDFCIDFont() {}
1090 1112
1091 bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth, 1113 bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth,
1092 const SkTDArray<uint32_t>* subset) { 1114 const SkTDArray<uint32_t>* subset) {
1093 SkAutoTUnref<SkPDFDict> descriptor(new SkPDFDict("FontDescriptor")); 1115 SkAutoTUnref<SkPDFDict> descriptor(new SkPDFDict("FontDescriptor"));
1094 setFontDescriptor(descriptor.get()); 1116 setFontDescriptor(descriptor.get());
1095 addResource(descriptor.get()); 1117 addResource(descriptor.get());
1118 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1119 if (!addCommonFontDescriptorEntries(defaultWidth)) {
1120 return false;
1121 }
1122 if (!canEmbed()) {
1123 return true;
1124 }
1096 1125
1097 switch (getType()) { 1126 switch (getType()) {
1098 case SkAdvancedTypefaceMetrics::kTrueType_Font: { 1127 case SkAdvancedTypefaceMetrics::kTrueType_Font: {
1099 SkASSERT(subset); 1128 SkAutoTUnref<SkPDFStream> fontStream;
1100 // Font subsetting 1129 int fontSize = 0;
1101 SkPDFStream* rawStream = NULL; 1130 if (canSubset()) {
1102 int fontSize = get_subset_font_stream(fontInfo()->fFontName.c_str(), 1131 SkPDFStream* rawStream = NULL;
1132 fontSize = get_subset_font_stream(fontInfo()->fFontName.c_str(),
1103 typeface(), 1133 typeface(),
1104 *subset, 1134 *subset,
1105 &rawStream); 1135 &rawStream);
1136 fontStream.reset(rawStream);
1137 } else {
1138 int ttcIndex;
1139 SkAutoTUnref<SkStream> fontData(
1140 typeface()->openStream(&ttcIndex));
1141 fontStream.reset(new SkPDFStream(fontData.get()));
1142 fontSize = fontData->getLength();
1143 }
1106 SkASSERT(fontSize); 1144 SkASSERT(fontSize);
1107 SkASSERT(rawStream); 1145 SkASSERT(fontStream.get());
1108 SkAutoTUnref<SkPDFStream> fontStream(rawStream);
1109 addResource(fontStream.get()); 1146 addResource(fontStream.get());
1110 1147
1111 fontStream->insertInt("Length1", fontSize); 1148 fontStream->insertInt("Length1", fontSize);
1112 descriptor->insert("FontFile2", 1149 descriptor->insert("FontFile2",
1113 new SkPDFObjRef(fontStream.get()))->unref(); 1150 new SkPDFObjRef(fontStream.get()))->unref();
1114 break; 1151 break;
1115 } 1152 }
1116 case SkAdvancedTypefaceMetrics::kCFF_Font: 1153 case SkAdvancedTypefaceMetrics::kCFF_Font:
1117 case SkAdvancedTypefaceMetrics::kType1CID_Font: { 1154 case SkAdvancedTypefaceMetrics::kType1CID_Font: {
1118 int ttcIndex; 1155 int ttcIndex;
1119 SkAutoTUnref<SkStream> fontData(typeface()->openStream(&ttcIndex)); 1156 SkAutoTUnref<SkStream> fontData(typeface()->openStream(&ttcIndex));
1120 SkAutoTUnref<SkPDFStream> fontStream( 1157 SkAutoTUnref<SkPDFStream> fontStream(
1121 new SkPDFStream(fontData.get())); 1158 new SkPDFStream(fontData.get()));
1122 addResource(fontStream.get()); 1159 addResource(fontStream.get());
1123 1160
1124 if (getType() == SkAdvancedTypefaceMetrics::kCFF_Font) { 1161 if (getType() == SkAdvancedTypefaceMetrics::kCFF_Font) {
1125 fontStream->insertName("Subtype", "Type1C"); 1162 fontStream->insertName("Subtype", "Type1C");
1126 } else { 1163 } else {
1127 fontStream->insertName("Subtype", "CIDFontType0c"); 1164 fontStream->insertName("Subtype", "CIDFontType0c");
1128 } 1165 }
1129 descriptor->insert("FontFile3", 1166 descriptor->insert("FontFile3",
1130 new SkPDFObjRef(fontStream.get()))->unref(); 1167 new SkPDFObjRef(fontStream.get()))->unref();
1131 break; 1168 break;
1132 } 1169 }
1133 default: 1170 default:
1134 SkASSERT(false); 1171 SkASSERT(false);
1135 } 1172 }
1136 1173 return true;
1137 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1138 return addCommonFontDescriptorEntries(defaultWidth);
1139 } 1174 }
1140 1175
1141 bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) { 1176 bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) {
1142 // Generate new font metrics with advance info for true type fonts. 1177 // Generate new font metrics with advance info for true type fonts.
1143 if (fontInfo()->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) { 1178 if (fontInfo()->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
1144 // Generate glyph id array. 1179 // Generate glyph id array.
1145 SkTDArray<uint32_t> glyphIDs; 1180 SkTDArray<uint32_t> glyphIDs;
1146 glyphIDs.push(0); // Always include glyph 0. 1181 glyphIDs.push(0); // Always include glyph 0.
1147 if (subset) { 1182 if (subset) {
1148 subset->exportTo(&glyphIDs); 1183 subset->exportTo(&glyphIDs);
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
1244 int ttcIndex; 1279 int ttcIndex;
1245 size_t header SK_INIT_TO_AVOID_WARNING; 1280 size_t header SK_INIT_TO_AVOID_WARNING;
1246 size_t data SK_INIT_TO_AVOID_WARNING; 1281 size_t data SK_INIT_TO_AVOID_WARNING;
1247 size_t trailer SK_INIT_TO_AVOID_WARNING; 1282 size_t trailer SK_INIT_TO_AVOID_WARNING;
1248 SkAutoTUnref<SkStream> rawFontData(typeface()->openStream(&ttcIndex)); 1283 SkAutoTUnref<SkStream> rawFontData(typeface()->openStream(&ttcIndex));
1249 SkStream* fontData = handleType1Stream(rawFontData.get(), &header, &data, 1284 SkStream* fontData = handleType1Stream(rawFontData.get(), &header, &data,
1250 &trailer); 1285 &trailer);
1251 if (fontData == NULL) { 1286 if (fontData == NULL) {
1252 return false; 1287 return false;
1253 } 1288 }
1254 SkAutoTUnref<SkPDFStream> fontStream(new SkPDFStream(fontData)); 1289 if (canEmbed()) {
1255 addResource(fontStream.get()); 1290 SkAutoTUnref<SkPDFStream> fontStream(new SkPDFStream(fontData));
1256 fontStream->insertInt("Length1", header); 1291 addResource(fontStream.get());
1257 fontStream->insertInt("Length2", data); 1292 fontStream->insertInt("Length1", header);
1258 fontStream->insertInt("Length3", trailer); 1293 fontStream->insertInt("Length2", data);
1259 descriptor->insert("FontFile", new SkPDFObjRef(fontStream.get()))->unref(); 1294 fontStream->insertInt("Length3", trailer);
1295 descriptor->insert("FontFile",
1296 new SkPDFObjRef(fontStream.get()))->unref();
1297 }
1260 1298
1261 addResource(descriptor.get()); 1299 addResource(descriptor.get());
1262 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref(); 1300 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
1263 1301
1264 return addCommonFontDescriptorEntries(defaultWidth); 1302 return addCommonFontDescriptorEntries(defaultWidth);
1265 } 1303 }
1266 1304
1267 bool SkPDFType1Font::populate(int16_t glyphID) { 1305 bool SkPDFType1Font::populate(int16_t glyphID) {
1268 SkASSERT(!fontInfo()->fVerticalMetrics.get()); 1306 SkASSERT(!fontInfo()->fVerticalMetrics.get());
1269 SkASSERT(fontInfo()->fGlyphWidths.get()); 1307 SkASSERT(fontInfo()->fGlyphWidths.get());
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
1421 1459
1422 insert("FontBBox", makeFontBBox(bbox, 1000))->unref(); 1460 insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
1423 insertInt("FirstChar", firstGlyphID()); 1461 insertInt("FirstChar", firstGlyphID());
1424 insertInt("LastChar", lastGlyphID()); 1462 insertInt("LastChar", lastGlyphID());
1425 insert("Widths", widthArray.get()); 1463 insert("Widths", widthArray.get());
1426 insertName("CIDToGIDMap", "Identity"); 1464 insertName("CIDToGIDMap", "Identity");
1427 1465
1428 populateToUnicodeTable(NULL); 1466 populateToUnicodeTable(NULL);
1429 return true; 1467 return true;
1430 } 1468 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698