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