| 1 |
|
| /* |
| 2 |
|
| * pdfmake_font_write.c - Font PDF object output |
| 3 |
|
| * |
| 4 |
|
| * Writes Font dictionary, FontDescriptor, FontFile2 (embedded TTF subset), |
| 5 |
|
| * CIDToGIDMap and ToUnicode CMap stream to PDF. |
| 6 |
|
| * |
| 7 |
|
| * Reference: PDF spec §9.6, §9.7, §9.8, §9.9 |
| 8 |
|
| * |
| 9 |
|
| * A Standard 14 font is a simple /Type1 dictionary. A TrueType font is a |
| 10 |
|
| * composite font: /Type0 with Identity-H encoding over a /CIDFontType2 |
| 11 |
|
| * descendant, because the content stream names glyphs by id and a simple |
| 12 |
|
| * /TrueType font would restrict it to a single-byte encoding. |
| 13 |
|
| * |
| 14 |
|
| * CIDs here are glyph ids in the ORIGINAL font, which is what |
| 15 |
|
| * pdfmake_font_encode_utf8 emits and what pdfmake_tounicode_generate keys |
| 16 |
|
| * on. The subsetter renumbers glyphs, so the two are bridged by a |
| 17 |
|
| * /CIDToGIDMap stream rather than /Identity. |
| 18 |
|
| */ |
| 19 |
|
| |
| 20 |
|
| #include "pdfmake_font.h" |
| 21 |
|
| #include "pdfmake_arena.h" |
| 22 |
|
| #include "pdfmake_buf.h" |
| 23 |
|
| #include "pdfmake_doc.h" |
| 24 |
|
| #include <string.h> |
| 25 |
|
| #include <stdlib.h> |
| 26 |
|
| #include <stdio.h> |
| 27 |
|
| |
| 28 |
|
| #define KEY(name) pdfmake_arena_intern_name(arena, name, (size_t)strlen(name)) |
| 29 |
|
| |
| 30 |
30 |
| static int glyph_used(const pdfmake_ttf_t *ttf, int gid) { |
| 31 |
30 |
▶ | if (!ttf->used_glyphs) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ttf->used_glyphs) return 0; |
0 |
30 |
30 |
|
| 32 |
30 |
| return (ttf->used_glyphs[gid / 8] & (1 << (gid % 8))) ? 1 : 0; |
| 33 |
|
| } |
| 34 |
|
| |
| 35 |
|
| /*============================================================================ |
| 36 |
|
| * Subset tag |
| 37 |
|
| * |
| 38 |
|
| * §9.6.4: an embedded subset's BaseFont is prefixed with six uppercase |
| 39 |
|
| * letters and a '+'. Derived from the font name and the exact set of glyphs |
| 40 |
|
| * embedded, so the same document rendered twice gets the same tag — the |
| 41 |
|
| * reproducible-output promise (t/44) covers font objects too. |
| 42 |
|
| *==========================================================================*/ |
| 43 |
|
| |
| 44 |
3 |
| static void subset_tag(const pdfmake_font_t *font, char out[8]) { |
| 45 |
3 |
| const pdfmake_ttf_t *ttf = font->ttf; |
| 46 |
3 |
| uint32_t h = 2166136261u; /* FNV-1a */ |
| 47 |
|
| const char *p; |
| 48 |
|
| int i; |
| 49 |
|
| |
| 50 |
33 |
▶ | for (p = font->base_font; p && *p; p++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (p = font->base_font; p && *p; p++) { |
33 |
0 |
33 |
| for (p = font->base_font; p && *p; p++) { |
30 |
3 |
33 |
|
| 51 |
30 |
| h ^= (uint32_t)(unsigned char)*p; |
| 52 |
30 |
| h *= 16777619u; |
| 53 |
|
| } |
| 54 |
18 |
▶ | for (i = 0; i < ttf->num_glyphs; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ttf->num_glyphs; i++) { |
15 |
3 |
18 |
|
| 55 |
15 |
▶ | if (!glyph_used(ttf, i)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!glyph_used(ttf, i)) continue; |
5 |
10 |
15 |
|
| 56 |
10 |
| h ^= (uint32_t)i; |
| 57 |
10 |
| h *= 16777619u; |
| 58 |
|
| } |
| 59 |
|
| |
| 60 |
21 |
▶ | for (i = 0; i < 6; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 6; i++) { |
18 |
3 |
21 |
|
| 61 |
18 |
| out[i] = (char)('A' + (h % 26)); |
| 62 |
18 |
| h /= 26; |
| 63 |
18 |
▶ | if (h == 0) h = 2166136261u ^ (uint32_t)i; |
Branch: 1 branch
| Branch | True | False | Total |
| if (h == 0) h = 2166136261u ^ (uint32_t)i; |
0 |
18 |
18 |
|
| 64 |
|
| } |
| 65 |
3 |
| out[6] = '+'; |
| 66 |
3 |
| out[7] = '\0'; |
| 67 |
3 |
| } |
| 68 |
|
| |
| 69 |
|
| /*============================================================================ |
| 70 |
|
| * /W widths array |
| 71 |
|
| * |
| 72 |
|
| * [ cid [w w w] cid [w] ... ] — one group per run of consecutive CIDs, which |
| 73 |
|
| * is the compact form for a subset whose glyph ids cluster. |
| 74 |
|
| *==========================================================================*/ |
| 75 |
|
| |
| 76 |
3 |
| static pdfmake_obj_t build_widths(pdfmake_arena_t *arena, |
| 77 |
|
| const pdfmake_ttf_t *ttf) { |
| 78 |
3 |
| pdfmake_obj_t arr = pdfmake_array_new(arena); |
| 79 |
|
| pdfmake_obj_t run; |
| 80 |
|
| int i; |
| 81 |
3 |
| int in_run = 0; |
| 82 |
|
| |
| 83 |
3 |
▶ | if (arr.kind != PDFMAKE_ARRAY) return arr; |
Branch: 1 branch
| Branch | True | False | Total |
| if (arr.kind != PDFMAKE_ARRAY) return arr; |
0 |
3 |
3 |
|
| 84 |
|
| |
| 85 |
3 |
| memset(&run, 0, sizeof(run)); |
| 86 |
|
| |
| 87 |
18 |
▶ | for (i = 0; i < ttf->num_glyphs; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ttf->num_glyphs; i++) { |
15 |
3 |
18 |
|
| 88 |
15 |
▶ | if (glyph_used(ttf, i)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (glyph_used(ttf, i)) { |
10 |
5 |
15 |
|
| 89 |
10 |
▶ | if (!in_run) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!in_run) { |
3 |
7 |
10 |
|
| 90 |
3 |
| pdfmake_array_push(arena, &arr, pdfmake_int(i)); |
| 91 |
3 |
| run = pdfmake_array_new(arena); |
| 92 |
3 |
▶ | if (run.kind != PDFMAKE_ARRAY) return arr; |
Branch: 1 branch
| Branch | True | False | Total |
| if (run.kind != PDFMAKE_ARRAY) return arr; |
0 |
3 |
3 |
|
| 93 |
3 |
| in_run = 1; |
| 94 |
|
| } |
| 95 |
10 |
| pdfmake_array_push(arena, &run, |
| 96 |
10 |
| pdfmake_int(pdfmake_ttf_glyph_advance(ttf, (uint16_t)i))); |
| 97 |
5 |
▶ | } else if (in_run) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (in_run) { |
2 |
3 |
5 |
|
| 98 |
2 |
| pdfmake_array_push(arena, &arr, run); |
| 99 |
2 |
| in_run = 0; |
| 100 |
|
| } |
| 101 |
|
| } |
| 102 |
3 |
▶ | if (in_run) pdfmake_array_push(arena, &arr, run); |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_run) pdfmake_array_push(arena, &arr, run); |
1 |
2 |
3 |
|
| 103 |
|
| |
| 104 |
3 |
| return arr; |
| 105 |
|
| } |
| 106 |
|
| |
| 107 |
|
| /*============================================================================ |
| 108 |
|
| * Standard 14: a simple /Type1 dictionary |
| 109 |
|
| *==========================================================================*/ |
| 110 |
|
| |
| 111 |
0 |
| static pdfmake_ref_t write_simple(pdfmake_font_t *font, pdfmake_doc_t *doc, |
| 112 |
|
| pdfmake_arena_t *arena) { |
| 113 |
0 |
| pdfmake_ref_t null_ref = {0, 0}; |
| 114 |
|
| pdfmake_obj_t dict; |
| 115 |
|
| uint32_t num; |
| 116 |
|
| |
| 117 |
0 |
| dict = pdfmake_dict_new(arena); |
| 118 |
0 |
▶ | if (dict.kind != PDFMAKE_DICT) return null_ref; |
Branch: 1 branch
| Branch | True | False | Total |
| if (dict.kind != PDFMAKE_DICT) return null_ref; |
0 |
0 |
0 |
|
| 119 |
|
| |
| 120 |
0 |
| pdfmake_dict_set(arena, &dict, KEY("Type"), pdfmake_name_cstr(arena, "Font")); |
| 121 |
0 |
| pdfmake_dict_set(arena, &dict, KEY("Subtype"), pdfmake_name_cstr(arena, "Type1")); |
| 122 |
0 |
| pdfmake_dict_set(arena, &dict, KEY("BaseFont"), |
| 123 |
0 |
▶ | pdfmake_name_cstr(arena, font->base_font ? font->base_font : "Helvetica")); |
Branch: 1 branch
| Branch | True | False | Total |
| pdfmake_name_cstr(arena, font->base_font ? font->base_font : "Helvetica")); |
0 |
0 |
0 |
|
| 124 |
|
| |
| 125 |
|
| /* Symbol and ZapfDingbats carry their own built-in encoding; naming |
| 126 |
|
| * WinAnsi over the top of one remaps every glyph. */ |
| 127 |
0 |
▶ | if (font->std14_id != PDFMAKE_STD14_SYMBOL && |
Branch: 1 branch
| Branch | True | False | Total |
| if (font->std14_id != PDFMAKE_STD14_SYMBOL && |
0 |
0 |
0 |
|
| 128 |
0 |
▶ | font->std14_id != PDFMAKE_STD14_ZAPFDINGBATS) { |
Branch: 1 branch
| Branch | True | False | Total |
| font->std14_id != PDFMAKE_STD14_ZAPFDINGBATS) { |
0 |
0 |
0 |
|
| 129 |
0 |
| pdfmake_dict_set(arena, &dict, KEY("Encoding"), |
| 130 |
|
| pdfmake_name_cstr(arena, "WinAnsiEncoding")); |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
0 |
| num = pdfmake_doc_add(doc, dict); |
| 134 |
0 |
▶ | if (num == 0) return null_ref; |
Branch: 1 branch
| Branch | True | False | Total |
| if (num == 0) return null_ref; |
0 |
0 |
0 |
|
| 135 |
|
| |
| 136 |
0 |
| font->font_ref.num = num; |
| 137 |
0 |
| font->font_ref.gen = 0; |
| 138 |
0 |
| return font->font_ref; |
| 139 |
|
| } |
| 140 |
|
| |
| 141 |
|
| /*============================================================================ |
| 142 |
|
| * TrueType: /Type0 over /CIDFontType2 |
| 143 |
|
| *==========================================================================*/ |
| 144 |
|
| |
| 145 |
3 |
| static pdfmake_ref_t write_truetype(pdfmake_font_t *font, pdfmake_doc_t *doc, |
| 146 |
|
| pdfmake_arena_t *arena) { |
| 147 |
3 |
| pdfmake_ref_t null_ref = {0, 0}; |
| 148 |
3 |
| pdfmake_ttf_t *ttf = font->ttf; |
| 149 |
|
| pdfmake_buf_t subset; |
| 150 |
|
| pdfmake_buf_t c2g; |
| 151 |
|
| pdfmake_buf_t tounicode; |
| 152 |
|
| pdfmake_obj_t stream; |
| 153 |
|
| pdfmake_obj_t descr; |
| 154 |
|
| pdfmake_obj_t cidfont; |
| 155 |
|
| pdfmake_obj_t type0; |
| 156 |
|
| pdfmake_obj_t bbox; |
| 157 |
|
| pdfmake_obj_t sysinfo; |
| 158 |
|
| pdfmake_obj_t descendants; |
| 159 |
3 |
| uint32_t fontfile_num = 0; |
| 160 |
3 |
| uint32_t c2g_num = 0; |
| 161 |
3 |
| uint32_t descr_num = 0; |
| 162 |
3 |
| uint32_t cidfont_num = 0; |
| 163 |
3 |
| uint32_t tounicode_num = 0; |
| 164 |
|
| uint32_t num; |
| 165 |
|
| char tag[8]; |
| 166 |
|
| char tagged[128]; |
| 167 |
|
| const char *base; |
| 168 |
|
| |
| 169 |
|
| /* .notdef is always embedded; a font nothing drew with still has to be a |
| 170 |
|
| * valid font rather than an empty one. */ |
| 171 |
3 |
| pdfmake_ttf_mark_glyph(ttf, 0); |
| 172 |
|
| |
| 173 |
3 |
| pdfmake_buf_init(&subset); |
| 174 |
3 |
▶ | if (pdfmake_ttf_subset(ttf, &subset) != PDFMAKE_OK || subset.len == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (pdfmake_ttf_subset(ttf, &subset) != PDFMAKE_OK || subset.len == 0) { |
3 |
0 |
3 |
| if (pdfmake_ttf_subset(ttf, &subset) != PDFMAKE_OK || subset.len == 0) { |
0 |
3 |
3 |
|
| 175 |
0 |
| pdfmake_buf_free(&subset); |
| 176 |
0 |
| return null_ref; |
| 177 |
|
| } |
| 178 |
|
| |
| 179 |
3 |
| subset_tag(font, tag); |
| 180 |
3 |
▶ | base = font->base_font ? font->base_font : "PDFMakeTTF"; |
Branch: 1 branch
| Branch | True | False | Total |
| base = font->base_font ? font->base_font : "PDFMakeTTF"; |
3 |
0 |
3 |
|
| 181 |
3 |
▶ | if (strlen(base) > sizeof(tagged) - 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strlen(base) > sizeof(tagged) - 8) { |
0 |
3 |
3 |
|
| 182 |
|
| /* A name longer than the buffer is not worth truncating into an |
| 183 |
|
| * ambiguous one; drop the tag and use the name as given. */ |
| 184 |
0 |
| snprintf(tagged, sizeof(tagged), "%s", base); |
| 185 |
|
| } else { |
| 186 |
3 |
| snprintf(tagged, sizeof(tagged), "%s%s", tag, base); |
| 187 |
|
| } |
| 188 |
|
| |
| 189 |
|
| /* ── /FontFile2: the embedded subset ────────────────── */ |
| 190 |
3 |
| stream = pdfmake_stream_new(arena); |
| 191 |
3 |
▶ | if (stream.kind != PDFMAKE_STREAM) { pdfmake_buf_free(&subset); return null_ref; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (stream.kind != PDFMAKE_STREAM) { pdfmake_buf_free(&subset); return null_ref; } |
0 |
3 |
3 |
|
| 192 |
3 |
▶ | if (!pdfmake_stream_set_data(arena, &stream, subset.data, subset.len)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pdfmake_stream_set_data(arena, &stream, subset.data, subset.len)) { |
0 |
3 |
3 |
|
| 193 |
0 |
| pdfmake_buf_free(&subset); |
| 194 |
0 |
| return null_ref; |
| 195 |
|
| } |
| 196 |
|
| { |
| 197 |
3 |
| pdfmake_dict_t *sd = pdfmake_stream_dict(&stream); |
| 198 |
|
| pdfmake_obj_t sdo; |
| 199 |
3 |
| sdo.kind = PDFMAKE_DICT; |
| 200 |
3 |
| sdo.as.dict = sd; |
| 201 |
|
| /* /Length1 is the size of the decoded font program. */ |
| 202 |
3 |
| pdfmake_dict_set(arena, &sdo, KEY("Length1"), pdfmake_int((int64_t)subset.len)); |
| 203 |
|
| } |
| 204 |
3 |
| pdfmake_stream_set_flate(arena, &stream); |
| 205 |
3 |
| fontfile_num = pdfmake_doc_add(doc, stream); |
| 206 |
3 |
| pdfmake_buf_free(&subset); |
| 207 |
3 |
▶ | if (fontfile_num == 0) return null_ref; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fontfile_num == 0) return null_ref; |
0 |
3 |
3 |
|
| 208 |
|
| |
| 209 |
|
| /* ── /CIDToGIDMap ───────────────────────────────────── */ |
| 210 |
3 |
| pdfmake_buf_init(&c2g); |
| 211 |
3 |
▶ | if (pdfmake_ttf_subset_cidtogid(ttf, &c2g) != PDFMAKE_OK || c2g.len == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (pdfmake_ttf_subset_cidtogid(ttf, &c2g) != PDFMAKE_OK || c2g.len == 0) { |
3 |
0 |
3 |
| if (pdfmake_ttf_subset_cidtogid(ttf, &c2g) != PDFMAKE_OK || c2g.len == 0) { |
0 |
3 |
3 |
|
| 212 |
0 |
| pdfmake_buf_free(&c2g); |
| 213 |
0 |
| return null_ref; |
| 214 |
|
| } |
| 215 |
3 |
| stream = pdfmake_stream_new(arena); |
| 216 |
3 |
▶ | if (stream.kind != PDFMAKE_STREAM) { pdfmake_buf_free(&c2g); return null_ref; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (stream.kind != PDFMAKE_STREAM) { pdfmake_buf_free(&c2g); return null_ref; } |
0 |
3 |
3 |
|
| 217 |
3 |
▶ | if (!pdfmake_stream_set_data(arena, &stream, c2g.data, c2g.len)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pdfmake_stream_set_data(arena, &stream, c2g.data, c2g.len)) { |
0 |
3 |
3 |
|
| 218 |
0 |
| pdfmake_buf_free(&c2g); |
| 219 |
0 |
| return null_ref; |
| 220 |
|
| } |
| 221 |
3 |
| pdfmake_stream_set_flate(arena, &stream); |
| 222 |
3 |
| c2g_num = pdfmake_doc_add(doc, stream); |
| 223 |
3 |
| pdfmake_buf_free(&c2g); |
| 224 |
3 |
▶ | if (c2g_num == 0) return null_ref; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c2g_num == 0) return null_ref; |
0 |
3 |
3 |
|
| 225 |
|
| |
| 226 |
|
| /* ── /FontDescriptor ────────────────────────────────── */ |
| 227 |
3 |
| descr = pdfmake_dict_new(arena); |
| 228 |
3 |
▶ | if (descr.kind != PDFMAKE_DICT) return null_ref; |
Branch: 1 branch
| Branch | True | False | Total |
| if (descr.kind != PDFMAKE_DICT) return null_ref; |
0 |
3 |
3 |
|
| 229 |
|
| |
| 230 |
3 |
| bbox = pdfmake_array_new(arena); |
| 231 |
3 |
| pdfmake_array_push(arena, &bbox, pdfmake_int(font->metrics.bbox[0])); |
| 232 |
3 |
| pdfmake_array_push(arena, &bbox, pdfmake_int(font->metrics.bbox[1])); |
| 233 |
3 |
| pdfmake_array_push(arena, &bbox, pdfmake_int(font->metrics.bbox[2])); |
| 234 |
3 |
| pdfmake_array_push(arena, &bbox, pdfmake_int(font->metrics.bbox[3])); |
| 235 |
|
| |
| 236 |
3 |
| pdfmake_dict_set(arena, &descr, KEY("Type"), |
| 237 |
|
| pdfmake_name_cstr(arena, "FontDescriptor")); |
| 238 |
3 |
| pdfmake_dict_set(arena, &descr, KEY("FontName"), |
| 239 |
|
| pdfmake_name_cstr(arena, tagged)); |
| 240 |
3 |
| pdfmake_dict_set(arena, &descr, KEY("Flags"), |
| 241 |
3 |
| pdfmake_int((int64_t)font->metrics.flags)); |
| 242 |
3 |
| pdfmake_dict_set(arena, &descr, KEY("FontBBox"), bbox); |
| 243 |
3 |
| pdfmake_dict_set(arena, &descr, KEY("ItalicAngle"), |
| 244 |
3 |
| pdfmake_int(font->metrics.italic_angle)); |
| 245 |
3 |
| pdfmake_dict_set(arena, &descr, KEY("Ascent"), |
| 246 |
3 |
| pdfmake_int(font->metrics.ascent)); |
| 247 |
3 |
| pdfmake_dict_set(arena, &descr, KEY("Descent"), |
| 248 |
3 |
| pdfmake_int(font->metrics.descent)); |
| 249 |
3 |
| pdfmake_dict_set(arena, &descr, KEY("CapHeight"), |
| 250 |
3 |
| pdfmake_int(font->metrics.cap_height)); |
| 251 |
3 |
| pdfmake_dict_set(arena, &descr, KEY("StemV"), |
| 252 |
3 |
| pdfmake_int(font->metrics.stem_v)); |
| 253 |
3 |
| pdfmake_dict_set(arena, &descr, KEY("FontFile2"), |
| 254 |
|
| pdfmake_ref(fontfile_num, 0)); |
| 255 |
|
| |
| 256 |
3 |
| descr_num = pdfmake_doc_add(doc, descr); |
| 257 |
3 |
▶ | if (descr_num == 0) return null_ref; |
Branch: 1 branch
| Branch | True | False | Total |
| if (descr_num == 0) return null_ref; |
0 |
3 |
3 |
|
| 258 |
|
| |
| 259 |
|
| /* ── /CIDFontType2 descendant ───────────────────────── */ |
| 260 |
3 |
| cidfont = pdfmake_dict_new(arena); |
| 261 |
3 |
▶ | if (cidfont.kind != PDFMAKE_DICT) return null_ref; |
Branch: 1 branch
| Branch | True | False | Total |
| if (cidfont.kind != PDFMAKE_DICT) return null_ref; |
0 |
3 |
3 |
|
| 262 |
|
| |
| 263 |
3 |
| sysinfo = pdfmake_dict_new(arena); |
| 264 |
3 |
| pdfmake_dict_set(arena, &sysinfo, KEY("Registry"), |
| 265 |
|
| pdfmake_str_cstr(arena, "Adobe")); |
| 266 |
3 |
| pdfmake_dict_set(arena, &sysinfo, KEY("Ordering"), |
| 267 |
|
| pdfmake_str_cstr(arena, "Identity")); |
| 268 |
3 |
| pdfmake_dict_set(arena, &sysinfo, KEY("Supplement"), pdfmake_int(0)); |
| 269 |
|
| |
| 270 |
3 |
| pdfmake_dict_set(arena, &cidfont, KEY("Type"), |
| 271 |
|
| pdfmake_name_cstr(arena, "Font")); |
| 272 |
3 |
| pdfmake_dict_set(arena, &cidfont, KEY("Subtype"), |
| 273 |
|
| pdfmake_name_cstr(arena, "CIDFontType2")); |
| 274 |
3 |
| pdfmake_dict_set(arena, &cidfont, KEY("BaseFont"), |
| 275 |
|
| pdfmake_name_cstr(arena, tagged)); |
| 276 |
3 |
| pdfmake_dict_set(arena, &cidfont, KEY("CIDSystemInfo"), sysinfo); |
| 277 |
3 |
| pdfmake_dict_set(arena, &cidfont, KEY("FontDescriptor"), |
| 278 |
|
| pdfmake_ref(descr_num, 0)); |
| 279 |
3 |
| pdfmake_dict_set(arena, &cidfont, KEY("DW"), pdfmake_int(1000)); |
| 280 |
3 |
| pdfmake_dict_set(arena, &cidfont, KEY("W"), build_widths(arena, ttf)); |
| 281 |
3 |
| pdfmake_dict_set(arena, &cidfont, KEY("CIDToGIDMap"), |
| 282 |
|
| pdfmake_ref(c2g_num, 0)); |
| 283 |
|
| |
| 284 |
3 |
| cidfont_num = pdfmake_doc_add(doc, cidfont); |
| 285 |
3 |
▶ | if (cidfont_num == 0) return null_ref; |
Branch: 1 branch
| Branch | True | False | Total |
| if (cidfont_num == 0) return null_ref; |
0 |
3 |
3 |
|
| 286 |
|
| |
| 287 |
|
| /* ── /ToUnicode ─────────────────────────────────────── */ |
| 288 |
3 |
| pdfmake_buf_init(&tounicode); |
| 289 |
3 |
▶ | if (pdfmake_tounicode_generate(font, &tounicode) == PDFMAKE_OK && |
Branch: 1 branch
| Branch | True | False | Total |
| if (pdfmake_tounicode_generate(font, &tounicode) == PDFMAKE_OK && |
3 |
0 |
3 |
|
| 290 |
3 |
▶ | tounicode.len > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| tounicode.len > 0) { |
3 |
0 |
3 |
|
| 291 |
3 |
| stream = pdfmake_stream_new(arena); |
| 292 |
6 |
| if (stream.kind == PDFMAKE_STREAM && |
| 293 |
3 |
| pdfmake_stream_set_data(arena, &stream, |
| 294 |
3 |
| tounicode.data, tounicode.len)) { |
| 295 |
3 |
| pdfmake_stream_set_flate(arena, &stream); |
| 296 |
3 |
| tounicode_num = pdfmake_doc_add(doc, stream); |
| 297 |
|
| } |
| 298 |
|
| } |
| 299 |
3 |
| pdfmake_buf_free(&tounicode); |
| 300 |
|
| |
| 301 |
|
| /* ── /Type0 ─────────────────────────────────────────── */ |
| 302 |
3 |
| type0 = pdfmake_dict_new(arena); |
| 303 |
3 |
▶ | if (type0.kind != PDFMAKE_DICT) return null_ref; |
Branch: 1 branch
| Branch | True | False | Total |
| if (type0.kind != PDFMAKE_DICT) return null_ref; |
0 |
3 |
3 |
|
| 304 |
|
| |
| 305 |
3 |
| descendants = pdfmake_array_new(arena); |
| 306 |
3 |
| pdfmake_array_push(arena, &descendants, pdfmake_ref(cidfont_num, 0)); |
| 307 |
|
| |
| 308 |
3 |
| pdfmake_dict_set(arena, &type0, KEY("Type"), |
| 309 |
|
| pdfmake_name_cstr(arena, "Font")); |
| 310 |
3 |
| pdfmake_dict_set(arena, &type0, KEY("Subtype"), |
| 311 |
|
| pdfmake_name_cstr(arena, "Type0")); |
| 312 |
3 |
| pdfmake_dict_set(arena, &type0, KEY("BaseFont"), |
| 313 |
|
| pdfmake_name_cstr(arena, tagged)); |
| 314 |
3 |
| pdfmake_dict_set(arena, &type0, KEY("Encoding"), |
| 315 |
|
| pdfmake_name_cstr(arena, "Identity-H")); |
| 316 |
3 |
| pdfmake_dict_set(arena, &type0, KEY("DescendantFonts"), descendants); |
| 317 |
3 |
▶ | if (tounicode_num) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tounicode_num) { |
3 |
0 |
3 |
|
| 318 |
3 |
| pdfmake_dict_set(arena, &type0, KEY("ToUnicode"), |
| 319 |
|
| pdfmake_ref(tounicode_num, 0)); |
| 320 |
|
| } |
| 321 |
|
| |
| 322 |
3 |
| num = pdfmake_doc_add(doc, type0); |
| 323 |
3 |
▶ | if (num == 0) return null_ref; |
Branch: 1 branch
| Branch | True | False | Total |
| if (num == 0) return null_ref; |
0 |
3 |
3 |
|
| 324 |
|
| |
| 325 |
3 |
| font->fontfile_ref.num = fontfile_num; |
| 326 |
3 |
| font->descriptor_ref.num = descr_num; |
| 327 |
3 |
| font->tounicode_ref.num = tounicode_num; |
| 328 |
3 |
| font->font_ref.num = num; |
| 329 |
3 |
| font->font_ref.gen = 0; |
| 330 |
3 |
| return font->font_ref; |
| 331 |
|
| } |
| 332 |
|
| |
| 333 |
|
| /*============================================================================ |
| 334 |
|
| * Main font writer entry point |
| 335 |
|
| *==========================================================================*/ |
| 336 |
|
| |
| 337 |
3 |
| pdfmake_ref_t pdfmake_font_write(pdfmake_font_t *font, pdfmake_doc_t *doc) { |
| 338 |
3 |
| pdfmake_ref_t null_ref = {0, 0}; |
| 339 |
|
| pdfmake_arena_t *arena; |
| 340 |
|
| |
| 341 |
3 |
▶ | if (!font || !doc) return null_ref; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!font || !doc) return null_ref; |
3 |
0 |
3 |
| if (!font || !doc) return null_ref; |
0 |
3 |
3 |
|
| 342 |
|
| |
| 343 |
|
| /* Written once per document: the caller may well ask again from each |
| 344 |
|
| * page that uses the font. */ |
| 345 |
3 |
▶ | if (font->font_ref.num != 0) return font->font_ref; |
Branch: 1 branch
| Branch | True | False | Total |
| if (font->font_ref.num != 0) return font->font_ref; |
0 |
3 |
3 |
|
| 346 |
|
| |
| 347 |
3 |
| arena = pdfmake_doc_arena(doc); |
| 348 |
3 |
▶ | if (!arena) return null_ref; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!arena) return null_ref; |
0 |
3 |
3 |
|
| 349 |
|
| |
| 350 |
3 |
▶ | if (font->type == PDFMAKE_FONT_TYPE1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (font->type == PDFMAKE_FONT_TYPE1) |
0 |
3 |
3 |
|
| 351 |
0 |
| return write_simple(font, doc, arena); |
| 352 |
3 |
▶ | if (font->type == PDFMAKE_FONT_TRUETYPE && font->ttf) |
Branch: 2 branches
| Branch | True | False | Total |
| if (font->type == PDFMAKE_FONT_TRUETYPE && font->ttf) |
3 |
0 |
3 |
| if (font->type == PDFMAKE_FONT_TRUETYPE && font->ttf) |
3 |
0 |
3 |
|
| 353 |
3 |
| return write_truetype(font, doc, arena); |
| 354 |
|
| |
| 355 |
0 |
| return null_ref; |
| 356 |
|
| } |
| 357 |
|
| |
| 358 |
|
| /*============================================================================ |
| 359 |
|
| * UTF-8 encoding |
| 360 |
|
| *==========================================================================*/ |
| 361 |
|
| |
| 362 |
4 |
| pdfmake_err_t pdfmake_font_encode_utf8(pdfmake_font_t *font, |
| 363 |
|
| const char *utf8, size_t len, |
| 364 |
|
| pdfmake_buf_t *out_bytes) { |
| 365 |
|
| const uint8_t *p; |
| 366 |
|
| const uint8_t *end; |
| 367 |
|
| |
| 368 |
4 |
▶ | if (!font || !utf8 || !out_bytes) return PDFMAKE_EINVAL; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!font || !utf8 || !out_bytes) return PDFMAKE_EINVAL; |
4 |
0 |
4 |
| if (!font || !utf8 || !out_bytes) return PDFMAKE_EINVAL; |
4 |
0 |
4 |
| if (!font || !utf8 || !out_bytes) return PDFMAKE_EINVAL; |
0 |
4 |
4 |
|
| 369 |
|
| |
| 370 |
4 |
| p = (const uint8_t *)utf8; |
| 371 |
4 |
| end = p + len; |
| 372 |
|
| |
| 373 |
17 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
13 |
4 |
17 |
|
| 374 |
|
| uint32_t cp; |
| 375 |
|
| |
| 376 |
|
| /* Decode UTF-8 */ |
| 377 |
13 |
▶ | if ((*p & 0x80) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((*p & 0x80) == 0) { |
13 |
0 |
13 |
|
| 378 |
13 |
| cp = *p++; |
| 379 |
0 |
▶ | } else if ((*p & 0xE0) == 0xC0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ((*p & 0xE0) == 0xC0) { |
0 |
0 |
0 |
|
| 380 |
0 |
▶ | if (p + 1 >= end) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p + 1 >= end) break; |
0 |
0 |
0 |
|
| 381 |
0 |
| cp = (*p++ & 0x1F) << 6; |
| 382 |
0 |
| cp |= (*p++ & 0x3F); |
| 383 |
0 |
▶ | } else if ((*p & 0xF0) == 0xE0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ((*p & 0xF0) == 0xE0) { |
0 |
0 |
0 |
|
| 384 |
0 |
▶ | if (p + 2 >= end) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p + 2 >= end) break; |
0 |
0 |
0 |
|
| 385 |
0 |
| cp = (*p++ & 0x0F) << 12; |
| 386 |
0 |
| cp |= (*p++ & 0x3F) << 6; |
| 387 |
0 |
| cp |= (*p++ & 0x3F); |
| 388 |
0 |
▶ | } else if ((*p & 0xF8) == 0xF0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ((*p & 0xF8) == 0xF0) { |
0 |
0 |
0 |
|
| 389 |
0 |
▶ | if (p + 3 >= end) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p + 3 >= end) break; |
0 |
0 |
0 |
|
| 390 |
0 |
| cp = (*p++ & 0x07) << 18; |
| 391 |
0 |
| cp |= (*p++ & 0x3F) << 12; |
| 392 |
0 |
| cp |= (*p++ & 0x3F) << 6; |
| 393 |
0 |
| cp |= (*p++ & 0x3F); |
| 394 |
|
| } else { |
| 395 |
0 |
| p++; |
| 396 |
0 |
| continue; |
| 397 |
|
| } |
| 398 |
|
| |
| 399 |
|
| /* Encode based on font type */ |
| 400 |
13 |
▶ | if (font->type == PDFMAKE_FONT_TYPE1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (font->type == PDFMAKE_FONT_TYPE1) { |
5 |
8 |
13 |
|
| 401 |
|
| /* WinAnsi encoding */ |
| 402 |
|
| uint8_t byte; |
| 403 |
5 |
▶ | if (cp >= 32 && cp <= 255) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (cp >= 32 && cp <= 255) { |
5 |
0 |
5 |
| if (cp >= 32 && cp <= 255) { |
5 |
0 |
5 |
|
| 404 |
5 |
| byte = (uint8_t)cp; |
| 405 |
0 |
▶ | } else if (cp == 0x2018) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (cp == 0x2018) { |
0 |
0 |
0 |
|
| 406 |
0 |
| byte = 0x91; |
| 407 |
0 |
▶ | } else if (cp == 0x2019) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (cp == 0x2019) { |
0 |
0 |
0 |
|
| 408 |
0 |
| byte = 0x92; |
| 409 |
0 |
▶ | } else if (cp == 0x201C) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (cp == 0x201C) { |
0 |
0 |
0 |
|
| 410 |
0 |
| byte = 0x93; |
| 411 |
0 |
▶ | } else if (cp == 0x201D) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (cp == 0x201D) { |
0 |
0 |
0 |
|
| 412 |
0 |
| byte = 0x94; |
| 413 |
|
| } else { |
| 414 |
0 |
| byte = '?'; |
| 415 |
|
| } |
| 416 |
5 |
| pdfmake_buf_append(out_bytes, &byte, 1); |
| 417 |
8 |
▶ | } else if (font->type == PDFMAKE_FONT_TRUETYPE && font->ttf) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (font->type == PDFMAKE_FONT_TRUETYPE && font->ttf) { |
8 |
0 |
8 |
| } else if (font->type == PDFMAKE_FONT_TRUETYPE && font->ttf) { |
8 |
0 |
8 |
|
| 418 |
|
| /* CID encoding - 2-byte glyph IDs */ |
| 419 |
8 |
| uint16_t gid = pdfmake_ttf_cmap_lookup(font->ttf, cp); |
| 420 |
|
| uint8_t bytes[2]; |
| 421 |
8 |
| pdfmake_ttf_mark_glyph(font->ttf, gid); |
| 422 |
8 |
| bytes[0] = (gid >> 8) & 0xFF; |
| 423 |
8 |
| bytes[1] = gid & 0xFF; |
| 424 |
8 |
| pdfmake_buf_append(out_bytes, bytes, 2); |
| 425 |
|
| } |
| 426 |
|
| } |
| 427 |
|
| |
| 428 |
4 |
| return PDFMAKE_OK; |
| 429 |
|
| } |