| 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 "SkPDFMakeToUnicodeCmap.h" | 8 #include "SkPDFMakeToUnicodeCmap.h" |
| 9 #include "SkPDFUtils.h" | 9 #include "SkPDFUtils.h" |
| 10 #include "SkUtils.h" | 10 #include "SkUtils.h" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 SkUnichar fUnicode; | 62 SkUnichar fUnicode; |
| 63 }; | 63 }; |
| 64 | 64 |
| 65 struct BFRange { | 65 struct BFRange { |
| 66 SkGlyphID fStart; | 66 SkGlyphID fStart; |
| 67 SkGlyphID fEnd; | 67 SkGlyphID fEnd; |
| 68 SkUnichar fUnicode; | 68 SkUnichar fUnicode; |
| 69 }; | 69 }; |
| 70 } // namespace | 70 } // namespace |
| 71 | 71 |
| 72 static void write_utf16be(SkDynamicMemoryWStream* wStream, SkUnichar utf32) { |
| 73 SkGlyphID utf16[2] = {0, 0}; |
| 74 size_t len = SkUTF16_FromUnichar(utf32, utf16); |
| 75 SkASSERT(len == 1 || len == 2); |
| 76 SkPDFUtils::WriteUInt16BE(wStream, utf16[0]); |
| 77 if (len == 2) { |
| 78 SkPDFUtils::WriteUInt16BE(wStream, utf16[1]); |
| 79 } |
| 80 } |
| 81 |
| 72 static void write_glyph(SkDynamicMemoryWStream* cmap, | 82 static void write_glyph(SkDynamicMemoryWStream* cmap, |
| 73 bool multiByte, | 83 bool multiByte, |
| 74 SkGlyphID gid) { | 84 SkGlyphID gid) { |
| 75 if (multiByte) { | 85 if (multiByte) { |
| 76 SkPDFUtils::WriteUInt16BE(cmap, gid); | 86 SkPDFUtils::WriteUInt16BE(cmap, gid); |
| 77 } else { | 87 } else { |
| 78 SkPDFUtils::WriteUInt8(cmap, SkToU8(gid)); | 88 SkPDFUtils::WriteUInt8(cmap, SkToU8(gid)); |
| 79 } | 89 } |
| 80 } | 90 } |
| 81 | 91 |
| 82 static void append_bfchar_section(const SkTDArray<BFChar>& bfchar, | 92 static void append_bfchar_section(const SkTDArray<BFChar>& bfchar, |
| 83 bool multiByte, | 93 bool multiByte, |
| 84 SkDynamicMemoryWStream* cmap) { | 94 SkDynamicMemoryWStream* cmap) { |
| 85 // PDF spec defines that every bf* list can have at most 100 entries. | 95 // PDF spec defines that every bf* list can have at most 100 entries. |
| 86 for (int i = 0; i < bfchar.count(); i += 100) { | 96 for (int i = 0; i < bfchar.count(); i += 100) { |
| 87 int count = bfchar.count() - i; | 97 int count = bfchar.count() - i; |
| 88 count = SkMin32(count, 100); | 98 count = SkMin32(count, 100); |
| 89 cmap->writeDecAsText(count); | 99 cmap->writeDecAsText(count); |
| 90 cmap->writeText(" beginbfchar\n"); | 100 cmap->writeText(" beginbfchar\n"); |
| 91 for (int j = 0; j < count; ++j) { | 101 for (int j = 0; j < count; ++j) { |
| 92 cmap->writeText("<"); | 102 cmap->writeText("<"); |
| 93 write_glyph(cmap, multiByte, bfchar[i + j].fGlyphId); | 103 write_glyph(cmap, multiByte, bfchar[i + j].fGlyphId); |
| 94 cmap->writeText("> <"); | 104 cmap->writeText("> <"); |
| 95 SkPDFUtils::WriteUTF16beHex(cmap, bfchar[i + j].fUnicode); | 105 write_utf16be(cmap, bfchar[i + j].fUnicode); |
| 96 cmap->writeText(">\n"); | 106 cmap->writeText(">\n"); |
| 97 } | 107 } |
| 98 cmap->writeText("endbfchar\n"); | 108 cmap->writeText("endbfchar\n"); |
| 99 } | 109 } |
| 100 } | 110 } |
| 101 | 111 |
| 102 static void append_bfrange_section(const SkTDArray<BFRange>& bfrange, | 112 static void append_bfrange_section(const SkTDArray<BFRange>& bfrange, |
| 103 bool multiByte, | 113 bool multiByte, |
| 104 SkDynamicMemoryWStream* cmap) { | 114 SkDynamicMemoryWStream* cmap) { |
| 105 // PDF spec defines that every bf* list can have at most 100 entries. | 115 // PDF spec defines that every bf* list can have at most 100 entries. |
| 106 for (int i = 0; i < bfrange.count(); i += 100) { | 116 for (int i = 0; i < bfrange.count(); i += 100) { |
| 107 int count = bfrange.count() - i; | 117 int count = bfrange.count() - i; |
| 108 count = SkMin32(count, 100); | 118 count = SkMin32(count, 100); |
| 109 cmap->writeDecAsText(count); | 119 cmap->writeDecAsText(count); |
| 110 cmap->writeText(" beginbfrange\n"); | 120 cmap->writeText(" beginbfrange\n"); |
| 111 for (int j = 0; j < count; ++j) { | 121 for (int j = 0; j < count; ++j) { |
| 112 cmap->writeText("<"); | 122 cmap->writeText("<"); |
| 113 write_glyph(cmap, multiByte, bfrange[i + j].fStart); | 123 write_glyph(cmap, multiByte, bfrange[i + j].fStart); |
| 114 cmap->writeText("> <"); | 124 cmap->writeText("> <"); |
| 115 write_glyph(cmap, multiByte, bfrange[i + j].fEnd); | 125 write_glyph(cmap, multiByte, bfrange[i + j].fEnd); |
| 116 cmap->writeText("> <"); | 126 cmap->writeText("> <"); |
| 117 SkPDFUtils::WriteUTF16beHex(cmap, bfrange[i + j].fUnicode); | 127 write_utf16be(cmap, bfrange[i + j].fUnicode); |
| 118 cmap->writeText(">\n"); | 128 cmap->writeText(">\n"); |
| 119 } | 129 } |
| 120 cmap->writeText("endbfrange\n"); | 130 cmap->writeText("endbfrange\n"); |
| 121 } | 131 } |
| 122 } | 132 } |
| 123 | 133 |
| 124 // Generate <bfchar> and <bfrange> table according to PDF spec 1.4 and Adobe | 134 // Generate <bfchar> and <bfrange> table according to PDF spec 1.4 and Adobe |
| 125 // Technote 5014. | 135 // Technote 5014. |
| 126 // The function is not static so we can test it in unit tests. | 136 // The function is not static so we can test it in unit tests. |
| 127 // | 137 // |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 SkGlyphID firstGlyphID, | 226 SkGlyphID firstGlyphID, |
| 217 SkGlyphID lastGlyphID) { | 227 SkGlyphID lastGlyphID) { |
| 218 SkDynamicMemoryWStream cmap; | 228 SkDynamicMemoryWStream cmap; |
| 219 append_tounicode_header(&cmap, multiByteGlyphs); | 229 append_tounicode_header(&cmap, multiByteGlyphs); |
| 220 SkPDFAppendCmapSections(glyphToUnicode, subset, &cmap, multiByteGlyphs, | 230 SkPDFAppendCmapSections(glyphToUnicode, subset, &cmap, multiByteGlyphs, |
| 221 firstGlyphID, lastGlyphID); | 231 firstGlyphID, lastGlyphID); |
| 222 append_cmap_footer(&cmap); | 232 append_cmap_footer(&cmap); |
| 223 return sk_make_sp<SkPDFStream>( | 233 return sk_make_sp<SkPDFStream>( |
| 224 std::unique_ptr<SkStreamAsset>(cmap.detachAsStream())); | 234 std::unique_ptr<SkStreamAsset>(cmap.detachAsStream())); |
| 225 } | 235 } |
| OLD | NEW |