OLD | NEW |
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 Loading... |
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_FontFlag) == 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_FontFlag) == 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 Loading... |
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 Loading... |
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_FontFlag) { |
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 && |
| 914 (info->fFlags & SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag)) { |
897 NOT_IMPLEMENTED(true, true); | 915 NOT_IMPLEMENTED(true, true); |
898 return new SkPDFType3Font(info, | 916 return new SkPDFType3Font(info, |
899 typeface, | 917 typeface, |
900 glyphID); | 918 glyphID); |
901 } | 919 } |
902 if (type == SkAdvancedTypefaceMetrics::kType1CID_Font || | 920 if (type == SkAdvancedTypefaceMetrics::kType1CID_Font || |
903 type == SkAdvancedTypefaceMetrics::kTrueType_Font) { | 921 type == SkAdvancedTypefaceMetrics::kTrueType_Font) { |
904 SkASSERT(relatedFontDescriptor == NULL); | 922 SkASSERT(relatedFontDescriptor == NULL); |
905 return new SkPDFType0Font(info, typeface); | 923 return new SkPDFType0Font(info, typeface); |
906 } | 924 } |
907 if (type == SkAdvancedTypefaceMetrics::kType1_Font) { | 925 if (type == SkAdvancedTypefaceMetrics::kType1_Font) { |
908 return new SkPDFType1Font(info, | 926 return new SkPDFType1Font(info, |
909 typeface, | 927 typeface, |
910 glyphID, | 928 glyphID, |
911 relatedFontDescriptor); | 929 relatedFontDescriptor); |
912 } | 930 } |
913 | 931 |
914 SkASSERT(type == SkAdvancedTypefaceMetrics::kCFF_Font || | 932 SkASSERT(type == SkAdvancedTypefaceMetrics::kCFF_Font || |
915 type == SkAdvancedTypefaceMetrics::kOther_Font || | 933 type == SkAdvancedTypefaceMetrics::kOther_Font); |
916 type == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font); | |
917 | 934 |
918 return new SkPDFType3Font(info, typeface, glyphID); | 935 return new SkPDFType3Font(info, typeface, glyphID); |
919 } | 936 } |
920 | 937 |
921 SkAdvancedTypefaceMetrics* SkPDFFont::fontInfo() { | 938 SkAdvancedTypefaceMetrics* SkPDFFont::fontInfo() { |
922 return fFontInfo.get(); | 939 return fFontInfo.get(); |
923 } | 940 } |
924 | 941 |
925 void SkPDFFont::setFontInfo(SkAdvancedTypefaceMetrics* info) { | 942 void SkPDFFont::setFontInfo(SkAdvancedTypefaceMetrics* info) { |
926 if (info == NULL || info == fFontInfo.get()) { | 943 if (info == NULL || info == fFontInfo.get()) { |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1033 } | 1050 } |
1034 | 1051 |
1035 /////////////////////////////////////////////////////////////////////////////// | 1052 /////////////////////////////////////////////////////////////////////////////// |
1036 // class SkPDFType0Font | 1053 // class SkPDFType0Font |
1037 /////////////////////////////////////////////////////////////////////////////// | 1054 /////////////////////////////////////////////////////////////////////////////// |
1038 | 1055 |
1039 SkPDFType0Font::SkPDFType0Font(SkAdvancedTypefaceMetrics* info, | 1056 SkPDFType0Font::SkPDFType0Font(SkAdvancedTypefaceMetrics* info, |
1040 SkTypeface* typeface) | 1057 SkTypeface* typeface) |
1041 : SkPDFFont(info, typeface, NULL) { | 1058 : SkPDFFont(info, typeface, NULL) { |
1042 SkDEBUGCODE(fPopulated = false); | 1059 SkDEBUGCODE(fPopulated = false); |
| 1060 if (!canSubset()) { |
| 1061 populate(NULL); |
| 1062 } |
1043 } | 1063 } |
1044 | 1064 |
1045 SkPDFType0Font::~SkPDFType0Font() {} | 1065 SkPDFType0Font::~SkPDFType0Font() {} |
1046 | 1066 |
1047 SkPDFFont* SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) { | 1067 SkPDFFont* SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) { |
| 1068 if (!canSubset()) { |
| 1069 return NULL; |
| 1070 } |
1048 SkPDFType0Font* newSubset = new SkPDFType0Font(fontInfo(), typeface()); | 1071 SkPDFType0Font* newSubset = new SkPDFType0Font(fontInfo(), typeface()); |
1049 newSubset->populate(subset); | 1072 newSubset->populate(subset); |
1050 return newSubset; | 1073 return newSubset; |
1051 } | 1074 } |
1052 | 1075 |
1053 #ifdef SK_DEBUG | 1076 #ifdef SK_DEBUG |
1054 void SkPDFType0Font::emitObject(SkWStream* stream, SkPDFCatalog* catalog, | 1077 void SkPDFType0Font::emitObject(SkWStream* stream, SkPDFCatalog* catalog, |
1055 bool indirect) { | 1078 bool indirect) { |
1056 SkASSERT(fPopulated); | 1079 SkASSERT(fPopulated); |
1057 return INHERITED::emitObject(stream, catalog, indirect); | 1080 return INHERITED::emitObject(stream, catalog, indirect); |
(...skipping 28 matching lines...) Expand all Loading... |
1086 populate(subset); | 1109 populate(subset); |
1087 } | 1110 } |
1088 | 1111 |
1089 SkPDFCIDFont::~SkPDFCIDFont() {} | 1112 SkPDFCIDFont::~SkPDFCIDFont() {} |
1090 | 1113 |
1091 bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth, | 1114 bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth, |
1092 const SkTDArray<uint32_t>* subset) { | 1115 const SkTDArray<uint32_t>* subset) { |
1093 SkAutoTUnref<SkPDFDict> descriptor(new SkPDFDict("FontDescriptor")); | 1116 SkAutoTUnref<SkPDFDict> descriptor(new SkPDFDict("FontDescriptor")); |
1094 setFontDescriptor(descriptor.get()); | 1117 setFontDescriptor(descriptor.get()); |
1095 addResource(descriptor.get()); | 1118 addResource(descriptor.get()); |
| 1119 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref(); |
| 1120 if (!addCommonFontDescriptorEntries(defaultWidth)) { |
| 1121 return false; |
| 1122 } |
| 1123 if (!canEmbed()) { |
| 1124 return true; |
| 1125 } |
1096 | 1126 |
1097 switch (getType()) { | 1127 switch (getType()) { |
1098 case SkAdvancedTypefaceMetrics::kTrueType_Font: { | 1128 case SkAdvancedTypefaceMetrics::kTrueType_Font: { |
1099 SkASSERT(subset); | 1129 SkAutoTUnref<SkPDFStream> fontStream; |
1100 // Font subsetting | 1130 int fontSize = 0; |
1101 SkPDFStream* rawStream = NULL; | 1131 if (canSubset()) { |
1102 int fontSize = get_subset_font_stream(fontInfo()->fFontName.c_str(), | 1132 SkPDFStream* rawStream = NULL; |
| 1133 fontSize = get_subset_font_stream(fontInfo()->fFontName.c_str(), |
1103 typeface(), | 1134 typeface(), |
1104 *subset, | 1135 *subset, |
1105 &rawStream); | 1136 &rawStream); |
| 1137 fontStream.reset(rawStream); |
| 1138 } else { |
| 1139 int ttcIndex; |
| 1140 SkAutoTUnref<SkStream> fontData( |
| 1141 typeface()->openStream(&ttcIndex)); |
| 1142 fontStream.reset(new SkPDFStream(fontData.get())); |
| 1143 fontSize = fontData->getLength(); |
| 1144 } |
1106 SkASSERT(fontSize); | 1145 SkASSERT(fontSize); |
1107 SkASSERT(rawStream); | 1146 SkASSERT(fontStream.get()); |
1108 SkAutoTUnref<SkPDFStream> fontStream(rawStream); | |
1109 addResource(fontStream.get()); | 1147 addResource(fontStream.get()); |
1110 | 1148 |
1111 fontStream->insertInt("Length1", fontSize); | 1149 fontStream->insertInt("Length1", fontSize); |
1112 descriptor->insert("FontFile2", | 1150 descriptor->insert("FontFile2", |
1113 new SkPDFObjRef(fontStream.get()))->unref(); | 1151 new SkPDFObjRef(fontStream.get()))->unref(); |
1114 break; | 1152 break; |
1115 } | 1153 } |
1116 case SkAdvancedTypefaceMetrics::kCFF_Font: | 1154 case SkAdvancedTypefaceMetrics::kCFF_Font: |
1117 case SkAdvancedTypefaceMetrics::kType1CID_Font: { | 1155 case SkAdvancedTypefaceMetrics::kType1CID_Font: { |
1118 int ttcIndex; | 1156 int ttcIndex; |
1119 SkAutoTUnref<SkStream> fontData(typeface()->openStream(&ttcIndex)); | 1157 SkAutoTUnref<SkStream> fontData(typeface()->openStream(&ttcIndex)); |
1120 SkAutoTUnref<SkPDFStream> fontStream( | 1158 SkAutoTUnref<SkPDFStream> fontStream( |
1121 new SkPDFStream(fontData.get())); | 1159 new SkPDFStream(fontData.get())); |
1122 addResource(fontStream.get()); | 1160 addResource(fontStream.get()); |
1123 | 1161 |
1124 if (getType() == SkAdvancedTypefaceMetrics::kCFF_Font) { | 1162 if (getType() == SkAdvancedTypefaceMetrics::kCFF_Font) { |
1125 fontStream->insertName("Subtype", "Type1C"); | 1163 fontStream->insertName("Subtype", "Type1C"); |
1126 } else { | 1164 } else { |
1127 fontStream->insertName("Subtype", "CIDFontType0c"); | 1165 fontStream->insertName("Subtype", "CIDFontType0c"); |
1128 } | 1166 } |
1129 descriptor->insert("FontFile3", | 1167 descriptor->insert("FontFile3", |
1130 new SkPDFObjRef(fontStream.get()))->unref(); | 1168 new SkPDFObjRef(fontStream.get()))->unref(); |
1131 break; | 1169 break; |
1132 } | 1170 } |
1133 default: | 1171 default: |
1134 SkASSERT(false); | 1172 SkASSERT(false); |
1135 } | 1173 } |
1136 | 1174 return true; |
1137 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref(); | |
1138 return addCommonFontDescriptorEntries(defaultWidth); | |
1139 } | 1175 } |
1140 | 1176 |
1141 bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) { | 1177 bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) { |
1142 // Generate new font metrics with advance info for true type fonts. | 1178 // Generate new font metrics with advance info for true type fonts. |
1143 if (fontInfo()->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) { | 1179 if (fontInfo()->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) { |
1144 // Generate glyph id array. | 1180 // Generate glyph id array. |
1145 SkTDArray<uint32_t> glyphIDs; | 1181 SkTDArray<uint32_t> glyphIDs; |
1146 glyphIDs.push(0); // Always include glyph 0. | 1182 glyphIDs.push(0); // Always include glyph 0. |
1147 if (subset) { | 1183 if (subset) { |
1148 subset->exportTo(&glyphIDs); | 1184 subset->exportTo(&glyphIDs); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1244 int ttcIndex; | 1280 int ttcIndex; |
1245 size_t header SK_INIT_TO_AVOID_WARNING; | 1281 size_t header SK_INIT_TO_AVOID_WARNING; |
1246 size_t data SK_INIT_TO_AVOID_WARNING; | 1282 size_t data SK_INIT_TO_AVOID_WARNING; |
1247 size_t trailer SK_INIT_TO_AVOID_WARNING; | 1283 size_t trailer SK_INIT_TO_AVOID_WARNING; |
1248 SkAutoTUnref<SkStream> rawFontData(typeface()->openStream(&ttcIndex)); | 1284 SkAutoTUnref<SkStream> rawFontData(typeface()->openStream(&ttcIndex)); |
1249 SkStream* fontData = handleType1Stream(rawFontData.get(), &header, &data, | 1285 SkStream* fontData = handleType1Stream(rawFontData.get(), &header, &data, |
1250 &trailer); | 1286 &trailer); |
1251 if (fontData == NULL) { | 1287 if (fontData == NULL) { |
1252 return false; | 1288 return false; |
1253 } | 1289 } |
1254 SkAutoTUnref<SkPDFStream> fontStream(new SkPDFStream(fontData)); | 1290 if (canEmbed()) { |
1255 addResource(fontStream.get()); | 1291 SkAutoTUnref<SkPDFStream> fontStream(new SkPDFStream(fontData)); |
1256 fontStream->insertInt("Length1", header); | 1292 addResource(fontStream.get()); |
1257 fontStream->insertInt("Length2", data); | 1293 fontStream->insertInt("Length1", header); |
1258 fontStream->insertInt("Length3", trailer); | 1294 fontStream->insertInt("Length2", data); |
1259 descriptor->insert("FontFile", new SkPDFObjRef(fontStream.get()))->unref(); | 1295 fontStream->insertInt("Length3", trailer); |
| 1296 descriptor->insert("FontFile", |
| 1297 new SkPDFObjRef(fontStream.get()))->unref(); |
| 1298 } |
1260 | 1299 |
1261 addResource(descriptor.get()); | 1300 addResource(descriptor.get()); |
1262 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref(); | 1301 insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref(); |
1263 | 1302 |
1264 return addCommonFontDescriptorEntries(defaultWidth); | 1303 return addCommonFontDescriptorEntries(defaultWidth); |
1265 } | 1304 } |
1266 | 1305 |
1267 bool SkPDFType1Font::populate(int16_t glyphID) { | 1306 bool SkPDFType1Font::populate(int16_t glyphID) { |
1268 SkASSERT(!fontInfo()->fVerticalMetrics.get()); | 1307 SkASSERT(!fontInfo()->fVerticalMetrics.get()); |
1269 SkASSERT(fontInfo()->fGlyphWidths.get()); | 1308 SkASSERT(fontInfo()->fGlyphWidths.get()); |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1421 | 1460 |
1422 insert("FontBBox", makeFontBBox(bbox, 1000))->unref(); | 1461 insert("FontBBox", makeFontBBox(bbox, 1000))->unref(); |
1423 insertInt("FirstChar", firstGlyphID()); | 1462 insertInt("FirstChar", firstGlyphID()); |
1424 insertInt("LastChar", lastGlyphID()); | 1463 insertInt("LastChar", lastGlyphID()); |
1425 insert("Widths", widthArray.get()); | 1464 insert("Widths", widthArray.get()); |
1426 insertName("CIDToGIDMap", "Identity"); | 1465 insertName("CIDToGIDMap", "Identity"); |
1427 | 1466 |
1428 populateToUnicodeTable(NULL); | 1467 populateToUnicodeTable(NULL); |
1429 return true; | 1468 return true; |
1430 } | 1469 } |
OLD | NEW |