| 1 |
|
| /* |
| 2 |
|
| * pdfmake_font.c - Font management implementation |
| 3 |
|
| * |
| 4 |
|
| * Implements Standard 14 font constructor, TrueType font loading, |
| 5 |
|
| * width calculations, and PDF output. |
| 6 |
|
| */ |
| 7 |
|
| |
| 8 |
|
| #include "pdfmake_font.h" |
| 9 |
|
| #include "pdfmake_arena.h" |
| 10 |
|
| #include <string.h> |
| 11 |
|
| #include <stdlib.h> |
| 12 |
|
| #include <strings.h> |
| 13 |
|
| |
| 14 |
|
| /*============================================================================ |
| 15 |
|
| * Standard 14 font constructor |
| 16 |
|
| *==========================================================================*/ |
| 17 |
|
| |
| 18 |
59 |
| pdfmake_font_t *pdfmake_font_standard14(pdfmake_arena_t *arena, const char *base_font) { |
| 19 |
|
| int id; |
| 20 |
|
| const pdfmake_std14_data_t *data; |
| 21 |
|
| pdfmake_font_t *font; |
| 22 |
|
| |
| 23 |
59 |
▶ | if (!arena || !base_font) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!arena || !base_font) return NULL; |
59 |
0 |
59 |
| if (!arena || !base_font) return NULL; |
0 |
59 |
59 |
|
| 24 |
|
| |
| 25 |
|
| /* Look up by name */ |
| 26 |
59 |
| id = pdfmake_std14_lookup(base_font); |
| 27 |
59 |
▶ | if (id < 0) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (id < 0) return NULL; |
1 |
58 |
59 |
|
| 28 |
|
| |
| 29 |
58 |
| data = pdfmake_std14_get(id); |
| 30 |
58 |
▶ | if (!data) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!data) return NULL; |
0 |
58 |
58 |
|
| 31 |
|
| |
| 32 |
58 |
| font = pdfmake_arena_alloc(arena, sizeof(pdfmake_font_t)); |
| 33 |
58 |
▶ | if (!font) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!font) return NULL; |
0 |
58 |
58 |
|
| 34 |
58 |
| memset(font, 0, sizeof(pdfmake_font_t)); |
| 35 |
|
| |
| 36 |
58 |
| font->arena = arena; |
| 37 |
58 |
| font->type = PDFMAKE_FONT_TYPE1; |
| 38 |
58 |
| font->std14_id = id; |
| 39 |
58 |
| font->base_font = data->name; |
| 40 |
58 |
| font->metrics = data->metrics; |
| 41 |
58 |
| font->ttf = NULL; |
| 42 |
|
| |
| 43 |
58 |
| return font; |
| 44 |
|
| } |
| 45 |
|
| |
| 46 |
|
| /*============================================================================ |
| 47 |
|
| * Font destructor |
| 48 |
|
| *==========================================================================*/ |
| 49 |
|
| |
| 50 |
62 |
| void pdfmake_font_free(pdfmake_font_t *font) { |
| 51 |
|
| /* Fonts are arena-allocated, nothing to free */ |
| 52 |
|
| (void)font; |
| 53 |
62 |
| } |
| 54 |
|
| |
| 55 |
|
| /*============================================================================ |
| 56 |
|
| * Width calculations |
| 57 |
|
| *==========================================================================*/ |
| 58 |
|
| |
| 59 |
213 |
| double pdfmake_font_advance(const pdfmake_font_t *font, uint32_t codepoint, double font_size) { |
| 60 |
213 |
| int width_units = 0; |
| 61 |
|
| |
| 62 |
213 |
▶ | if (!font) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!font) return 0; |
0 |
213 |
213 |
|
| 63 |
|
| |
| 64 |
213 |
| switch (font->type) { |
| 65 |
196 |
| case PDFMAKE_FONT_TYPE1: |
| 66 |
|
| /* Standard 14 font - use std14 width table */ |
| 67 |
196 |
| width_units = pdfmake_std14_width(font->std14_id, codepoint); |
| 68 |
196 |
| break; |
| 69 |
|
| |
| 70 |
17 |
| case PDFMAKE_FONT_TRUETYPE: |
| 71 |
|
| case PDFMAKE_FONT_CID_TRUETYPE: |
| 72 |
|
| /* TrueType font - lookup in TTF */ |
| 73 |
17 |
▶ | if (font->ttf) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (font->ttf) { |
17 |
0 |
17 |
|
| 74 |
17 |
| uint16_t glyph = pdfmake_ttf_cmap_lookup(font->ttf, codepoint); |
| 75 |
17 |
| width_units = pdfmake_ttf_glyph_advance(font->ttf, glyph); |
| 76 |
|
| } |
| 77 |
17 |
| break; |
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
|
| /* Convert from 1/1000 em to PDF points */ |
| 81 |
213 |
| return (width_units * font_size) / 1000.0; |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
23 |
| double pdfmake_font_string_width(const pdfmake_font_t *font, const char *utf8, |
| 85 |
|
| size_t len, double font_size) { |
| 86 |
23 |
| double total = 0; |
| 87 |
|
| const uint8_t *p; |
| 88 |
|
| const uint8_t *end; |
| 89 |
|
| |
| 90 |
23 |
▶ | if (!font || !utf8) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!font || !utf8) return 0; |
23 |
0 |
23 |
| if (!font || !utf8) return 0; |
0 |
23 |
23 |
|
| 91 |
|
| |
| 92 |
23 |
| p = (const uint8_t *)utf8; |
| 93 |
23 |
| end = p + len; |
| 94 |
|
| |
| 95 |
217 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
194 |
23 |
217 |
|
| 96 |
|
| uint32_t cp; |
| 97 |
|
| |
| 98 |
|
| /* Decode UTF-8 */ |
| 99 |
194 |
▶ | if ((*p & 0x80) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((*p & 0x80) == 0) { |
193 |
1 |
194 |
|
| 100 |
193 |
| cp = *p++; |
| 101 |
1 |
▶ | } else if ((*p & 0xE0) == 0xC0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ((*p & 0xE0) == 0xC0) { |
1 |
0 |
1 |
|
| 102 |
1 |
▶ | if (p + 1 >= end) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p + 1 >= end) break; |
0 |
1 |
1 |
|
| 103 |
1 |
| cp = (*p++ & 0x1F) << 6; |
| 104 |
1 |
| cp |= (*p++ & 0x3F); |
| 105 |
0 |
▶ | } else if ((*p & 0xF0) == 0xE0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ((*p & 0xF0) == 0xE0) { |
0 |
0 |
0 |
|
| 106 |
0 |
▶ | if (p + 2 >= end) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p + 2 >= end) break; |
0 |
0 |
0 |
|
| 107 |
0 |
| cp = (*p++ & 0x0F) << 12; |
| 108 |
0 |
| cp |= (*p++ & 0x3F) << 6; |
| 109 |
0 |
| cp |= (*p++ & 0x3F); |
| 110 |
0 |
▶ | } else if ((*p & 0xF8) == 0xF0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ((*p & 0xF8) == 0xF0) { |
0 |
0 |
0 |
|
| 111 |
0 |
▶ | if (p + 3 >= end) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p + 3 >= end) break; |
0 |
0 |
0 |
|
| 112 |
0 |
| cp = (*p++ & 0x07) << 18; |
| 113 |
0 |
| cp |= (*p++ & 0x3F) << 12; |
| 114 |
0 |
| cp |= (*p++ & 0x3F) << 6; |
| 115 |
0 |
| cp |= (*p++ & 0x3F); |
| 116 |
|
| } else { |
| 117 |
|
| /* Invalid UTF-8, skip byte */ |
| 118 |
0 |
| p++; |
| 119 |
0 |
| continue; |
| 120 |
|
| } |
| 121 |
|
| |
| 122 |
194 |
| total += pdfmake_font_advance(font, cp, font_size); |
| 123 |
|
| } |
| 124 |
|
| |
| 125 |
23 |
| return total; |
| 126 |
|
| } |
| 127 |
|
| |
| 128 |
8 |
| const pdfmake_font_metrics_t *pdfmake_font_metrics(const pdfmake_font_t *font) { |
| 129 |
8 |
▶ | if (!font) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!font) return NULL; |
0 |
8 |
8 |
|
| 130 |
8 |
| return &font->metrics; |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
|
| /*============================================================================ |
| 134 |
|
| * TrueType font creation |
| 135 |
|
| *==========================================================================*/ |
| 136 |
|
| |
| 137 |
4 |
| pdfmake_font_t *pdfmake_font_from_ttf(pdfmake_arena_t *arena, |
| 138 |
|
| const uint8_t *ttf_bytes, size_t len) { |
| 139 |
|
| pdfmake_ttf_t *ttf; |
| 140 |
|
| pdfmake_font_t *font; |
| 141 |
|
| int upm; |
| 142 |
|
| pdfmake_font_metrics_t *m; |
| 143 |
|
| uint8_t *copy; |
| 144 |
|
| |
| 145 |
4 |
▶ | if (!arena || !ttf_bytes || len == 0) return NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!arena || !ttf_bytes || len == 0) return NULL; |
4 |
0 |
4 |
| if (!arena || !ttf_bytes || len == 0) return NULL; |
4 |
0 |
4 |
| if (!arena || !ttf_bytes || len == 0) return NULL; |
0 |
4 |
4 |
|
| 146 |
|
| |
| 147 |
|
| /* pdfmake_ttf_parse keeps the buffer rather than copying it, and every |
| 148 |
|
| * later read - cmap lookup, hmtx advance, the subsetter - goes back to |
| 149 |
|
| * it. Callers that parse a FontFile2 out of a document hold that buffer |
| 150 |
|
| * open, but a font loaded from a file or a scalar does not: both XS entry |
| 151 |
|
| * points hand over memory they release immediately afterwards. Copying |
| 152 |
|
| * into the arena ties the bytes to the font's own lifetime. |
| 153 |
|
| * |
| 154 |
|
| * Left aliased, this reads freed memory on every glyph: it returned the |
| 155 |
|
| * right answers for as long as the allocator had not reused the block, |
| 156 |
|
| * then quietly started returning glyph 0 for everything. */ |
| 157 |
4 |
| copy = pdfmake_arena_alloc(arena, len); |
| 158 |
4 |
▶ | if (!copy) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!copy) return NULL; |
0 |
4 |
4 |
|
| 159 |
4 |
| memcpy(copy, ttf_bytes, len); |
| 160 |
|
| |
| 161 |
4 |
| ttf = pdfmake_ttf_parse(arena, copy, len); |
| 162 |
4 |
▶ | if (!ttf) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ttf) return NULL; |
0 |
4 |
4 |
|
| 163 |
|
| |
| 164 |
4 |
| font = pdfmake_arena_alloc(arena, sizeof(pdfmake_font_t)); |
| 165 |
4 |
▶ | if (!font) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!font) return NULL; |
0 |
4 |
4 |
|
| 166 |
4 |
| memset(font, 0, sizeof(pdfmake_font_t)); |
| 167 |
|
| |
| 168 |
4 |
| font->arena = arena; |
| 169 |
4 |
| font->type = PDFMAKE_FONT_TRUETYPE; |
| 170 |
4 |
| font->std14_id = -1; |
| 171 |
4 |
| font->ttf = ttf; |
| 172 |
|
| |
| 173 |
|
| /* Derive metrics from TTF */ |
| 174 |
4 |
▶ | upm = ttf->units_per_em > 0 ? ttf->units_per_em : 1000; |
Branch: 1 branch
| Branch | True | False | Total |
| upm = ttf->units_per_em > 0 ? ttf->units_per_em : 1000; |
4 |
0 |
4 |
|
| 175 |
4 |
| m = &font->metrics; |
| 176 |
|
| |
| 177 |
4 |
| m->ascent = (ttf->ascender * 1000) / upm; |
| 178 |
4 |
| m->descent = (ttf->descender * 1000) / upm; |
| 179 |
|
| |
| 180 |
4 |
▶ | if (ttf->has_os2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ttf->has_os2) { |
0 |
4 |
4 |
|
| 181 |
0 |
▶ | m->cap_height = ttf->s_cap_height > 0 ? (ttf->s_cap_height * 1000) / upm : m->ascent; |
Branch: 1 branch
| Branch | True | False | Total |
| m->cap_height = ttf->s_cap_height > 0 ? (ttf->s_cap_height * 1000) / upm : m->ascent; |
0 |
0 |
0 |
|
| 182 |
0 |
▶ | m->x_height = ttf->s_x_height > 0 ? (ttf->s_x_height * 1000) / upm : m->cap_height * 7 / 10; |
Branch: 1 branch
| Branch | True | False | Total |
| m->x_height = ttf->s_x_height > 0 ? (ttf->s_x_height * 1000) / upm : m->cap_height * 7 / 10; |
0 |
0 |
0 |
|
| 183 |
|
| } else { |
| 184 |
4 |
| m->cap_height = m->ascent; |
| 185 |
4 |
| m->x_height = m->cap_height * 7 / 10; |
| 186 |
|
| } |
| 187 |
|
| |
| 188 |
4 |
| m->stem_v = 80; |
| 189 |
4 |
| m->stem_h = 80; |
| 190 |
4 |
| m->bbox[0] = (ttf->x_min * 1000) / upm; |
| 191 |
4 |
| m->bbox[1] = (ttf->y_min * 1000) / upm; |
| 192 |
4 |
| m->bbox[2] = (ttf->x_max * 1000) / upm; |
| 193 |
4 |
| m->bbox[3] = (ttf->y_max * 1000) / upm; |
| 194 |
|
| |
| 195 |
|
| /* mac_style bit 1 is italic; OS/2 fsSelection bit 0 says the same. A |
| 196 |
|
| * FontDescriptor without /ItalicAngle on a slanted face makes a reader |
| 197 |
|
| * synthesise its own slant on top of the real one. */ |
| 198 |
4 |
| m->flags = PDFMAKE_FONT_FLAG_NONSYMBOLIC; |
| 199 |
4 |
▶ | if ((ttf->mac_style & 0x02) || |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ttf->mac_style & 0x02) || |
4 |
0 |
4 |
|
| 200 |
4 |
▶ | (ttf->has_os2 && (ttf->fs_selection & 0x01))) { |
Branch: 2 branches
| Branch | True | False | Total |
| (ttf->has_os2 && (ttf->fs_selection & 0x01))) { |
0 |
4 |
4 |
| (ttf->has_os2 && (ttf->fs_selection & 0x01))) { |
0 |
0 |
0 |
|
| 201 |
0 |
| m->flags |= PDFMAKE_FONT_FLAG_ITALIC; |
| 202 |
0 |
| m->italic_angle = -12; |
| 203 |
|
| } |
| 204 |
4 |
▶ | if (ttf->has_os2 && ttf->us_weight_class >= 600) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ttf->has_os2 && ttf->us_weight_class >= 600) |
0 |
4 |
4 |
| if (ttf->has_os2 && ttf->us_weight_class >= 600) |
0 |
0 |
0 |
|
| 205 |
0 |
| m->flags |= PDFMAKE_FONT_FLAG_FORCE_BOLD; |
| 206 |
|
| /* OS/2 panose[3] == 9 is monospaced; so is a single-advance hmtx. */ |
| 207 |
4 |
▶ | if (ttf->num_h_metrics == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ttf->num_h_metrics == 1) |
0 |
4 |
4 |
|
| 208 |
0 |
| m->flags |= PDFMAKE_FONT_FLAG_FIXED_PITCH; |
| 209 |
|
| |
| 210 |
|
| /* /BaseFont. Without it a TTF cannot be written at all, and |
| 211 |
|
| * PDF::Make::Font->base_font read back undef. */ |
| 212 |
4 |
| font->base_font = pdfmake_ttf_postscript_name(ttf, arena); |
| 213 |
4 |
▶ | if (!font->base_font) font->base_font = "PDFMakeTTF"; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!font->base_font) font->base_font = "PDFMakeTTF"; |
0 |
4 |
4 |
|
| 214 |
|
| |
| 215 |
4 |
| return font; |
| 216 |
|
| } |