| 1 |
|
| /* |
| 2 |
|
| * pdfmake_markup_style.c - the attribute table and its coercions. |
| 3 |
|
| */ |
| 4 |
|
| |
| 5 |
|
| #include "pdfmake_markup_style.h" |
| 6 |
|
| #include <stdlib.h> |
| 7 |
|
| #include <string.h> |
| 8 |
|
| #include <stdio.h> |
| 9 |
|
| #include <ctype.h> |
| 10 |
|
| #include <stdarg.h> |
| 11 |
|
| #include <strings.h> |
| 12 |
|
| |
| 13 |
|
| /*---------------------------------------------------------------------------- |
| 14 |
|
| * The table |
| 15 |
|
| *--------------------------------------------------------------------------*/ |
| 16 |
|
| |
| 17 |
|
| typedef struct { |
| 18 |
|
| const char *name; |
| 19 |
|
| pdfmake_prop_t id; |
| 20 |
|
| pdfmake_ptype_t type; |
| 21 |
|
| int inherit; |
| 22 |
|
| } prop_ent_t; |
| 23 |
|
| |
| 24 |
|
| static const prop_ent_t PROPS[] = { |
| 25 |
|
| { "size", PDFMAKE_P_SIZE, PDFMAKE_T_LEN, 1 }, |
| 26 |
|
| { "colour", PDFMAKE_P_COLOUR, PDFMAKE_T_COLOUR, 1 }, |
| 27 |
|
| { "font", PDFMAKE_P_FONT, PDFMAKE_T_STR, 1 }, |
| 28 |
|
| { "bold", PDFMAKE_P_BOLD, PDFMAKE_T_BOOL, 1 }, |
| 29 |
|
| { "italic", PDFMAKE_P_ITALIC, PDFMAKE_T_BOOL, 1 }, |
| 30 |
|
| { "line-height", PDFMAKE_P_LINE_HEIGHT, PDFMAKE_T_LEN, 1 }, |
| 31 |
|
| { "align", PDFMAKE_P_ALIGN, PDFMAKE_T_ALIGN, 1 }, |
| 32 |
|
| |
| 33 |
|
| { "bg", PDFMAKE_P_BG, PDFMAKE_T_COLOUR, 0 }, |
| 34 |
|
| { "border", PDFMAKE_P_BORDER, PDFMAKE_T_COLOUR, 0 }, |
| 35 |
|
| { "border-width", PDFMAKE_P_BORDER_WIDTH, PDFMAKE_T_LEN, 0 }, |
| 36 |
|
| { "pad", PDFMAKE_P_PAD, PDFMAKE_T_LEN, 0 }, |
| 37 |
|
| { "weight", PDFMAKE_P_WEIGHT, PDFMAKE_T_NUM, 0 }, |
| 38 |
|
| { "width", PDFMAKE_P_WIDTH, PDFMAKE_T_LEN, 0 }, |
| 39 |
|
| { "height", PDFMAKE_P_HEIGHT, PDFMAKE_T_LEN, 0 }, |
| 40 |
|
| { "gap", PDFMAKE_P_GAP, PDFMAKE_T_LEN, 0 }, |
| 41 |
|
| { "indent", PDFMAKE_P_INDENT, PDFMAKE_T_NUM, 0 }, |
| 42 |
|
| { "spacing", PDFMAKE_P_SPACING, PDFMAKE_T_LEN, 0 }, |
| 43 |
|
| { "margin", PDFMAKE_P_MARGIN, PDFMAKE_T_LEN, 0 }, |
| 44 |
|
| { "valign", PDFMAKE_P_VALIGN, PDFMAKE_T_VALIGN, 0 }, |
| 45 |
|
| |
| 46 |
|
| { "page-size", PDFMAKE_P_PAGE_SIZE, PDFMAKE_T_STR, 0 }, |
| 47 |
|
| { "columns", PDFMAKE_P_COLUMNS, PDFMAKE_T_NUM, 0 }, |
| 48 |
|
| { "background", PDFMAKE_P_BACKGROUND, PDFMAKE_T_COLOUR, 0 }, |
| 49 |
|
| |
| 50 |
|
| { "src", PDFMAKE_P_SRC, PDFMAKE_T_STR, 0 }, |
| 51 |
|
| { "title", PDFMAKE_P_TITLE, PDFMAKE_T_STR, 0 }, |
| 52 |
|
| { "level", PDFMAKE_P_LEVEL, PDFMAKE_T_NUM, 0 }, |
| 53 |
|
| { "header-repeat",PDFMAKE_P_HEADER_REPEAT, PDFMAKE_T_BOOL, 0 }, |
| 54 |
|
| { "preformatted", PDFMAKE_P_PREFORMATTED, PDFMAKE_T_BOOL, 0 }, |
| 55 |
|
| /* Accessibility is a property of the document, declared where the |
| 56 |
|
| * document starts: <doc tagged="1"> turns the structure tree on. */ |
| 57 |
|
| { "tagged", PDFMAKE_P_TAGGED, PDFMAKE_T_BOOL, 0 } |
| 58 |
|
| }; |
| 59 |
|
| |
| 60 |
|
| #define PROP_COUNT (sizeof(PROPS) / sizeof(PROPS[0])) |
| 61 |
|
| |
| 62 |
|
| /* Per-tag allowances, as bit sets over pdfmake_prop_t. A tag's set is what it |
| 63 |
|
| * can actually act on: if the renderer cannot do anything with an attribute |
| 64 |
|
| * here, it does not belong. */ |
| 65 |
|
| #define BIT(p) (1ull << (p)) |
| 66 |
|
| |
| 67 |
|
| #define TEXT_PROPS (BIT(PDFMAKE_P_SIZE) | BIT(PDFMAKE_P_COLOUR) | \ |
| 68 |
|
| BIT(PDFMAKE_P_FONT) | BIT(PDFMAKE_P_BOLD) | \ |
| 69 |
|
| BIT(PDFMAKE_P_ITALIC) | BIT(PDFMAKE_P_LINE_HEIGHT) | \ |
| 70 |
|
| BIT(PDFMAKE_P_ALIGN)) |
| 71 |
|
| |
| 72 |
|
| #define BLOCK_PROPS (BIT(PDFMAKE_P_INDENT) | BIT(PDFMAKE_P_SPACING) | \ |
| 73 |
|
| BIT(PDFMAKE_P_MARGIN) | BIT(PDFMAKE_P_PAD) | \ |
| 74 |
|
| BIT(PDFMAKE_P_PREFORMATTED)) |
| 75 |
|
| |
| 76 |
|
| #define PAGE_PROPS (BIT(PDFMAKE_P_PAGE_SIZE) | BIT(PDFMAKE_P_MARGIN) | \ |
| 77 |
|
| BIT(PDFMAKE_P_COLUMNS) | BIT(PDFMAKE_P_BACKGROUND)) |
| 78 |
|
| |
| 79 |
|
| #define CELL_PROPS (TEXT_PROPS | BIT(PDFMAKE_P_WEIGHT) | BIT(PDFMAKE_P_PAD) | \ |
| 80 |
|
| BIT(PDFMAKE_P_BG) | BIT(PDFMAKE_P_BORDER) | \ |
| 81 |
|
| BIT(PDFMAKE_P_VALIGN) | BIT(PDFMAKE_P_WIDTH)) |
| 82 |
|
| |
| 83 |
160 |
| static uint64_t tag_allow(pdfmake_markup_tag_t tag) { |
| 84 |
160 |
| switch (tag) { |
| 85 |
26 |
| case PDFMAKE_MK_DOC: return TEXT_PROPS | PAGE_PROPS | |
| 86 |
|
| BIT(PDFMAKE_P_TAGGED); |
| 87 |
2 |
| case PDFMAKE_MK_STYLE: return 0; /* validated as declaration lists */ |
| 88 |
4 |
| case PDFMAKE_MK_PAGE: return PAGE_PROPS; |
| 89 |
2 |
| case PDFMAKE_MK_PAGEBREAK: return 0; |
| 90 |
4 |
| case PDFMAKE_MK_HEADER: |
| 91 |
4 |
| case PDFMAKE_MK_FOOTER: return TEXT_PROPS | BIT(PDFMAKE_P_PAD); |
| 92 |
|
| |
| 93 |
38 |
| case PDFMAKE_MK_H1: case PDFMAKE_MK_H2: case PDFMAKE_MK_H3: |
| 94 |
|
| case PDFMAKE_MK_H4: case PDFMAKE_MK_H5: case PDFMAKE_MK_H6: |
| 95 |
|
| case PDFMAKE_MK_P: case PDFMAKE_MK_TEXT: |
| 96 |
38 |
| return TEXT_PROPS | BLOCK_PROPS; |
| 97 |
|
| |
| 98 |
2 |
| case PDFMAKE_MK_HR: return BIT(PDFMAKE_P_COLOUR) | BIT(PDFMAKE_P_WIDTH) | |
| 99 |
|
| BIT(PDFMAKE_P_HEIGHT) | BIT(PDFMAKE_P_SPACING); |
| 100 |
10 |
| case PDFMAKE_MK_BOX: return TEXT_PROPS | BIT(PDFMAKE_P_BG) | |
| 101 |
|
| BIT(PDFMAKE_P_BORDER) | |
| 102 |
|
| BIT(PDFMAKE_P_BORDER_WIDTH) | |
| 103 |
|
| BIT(PDFMAKE_P_PAD) | BIT(PDFMAKE_P_WIDTH) | |
| 104 |
|
| BIT(PDFMAKE_P_HEIGHT); |
| 105 |
2 |
| case PDFMAKE_MK_IMG: return BIT(PDFMAKE_P_SRC) | BIT(PDFMAKE_P_WIDTH) | |
| 106 |
|
| BIT(PDFMAKE_P_HEIGHT) | BIT(PDFMAKE_P_ALIGN); |
| 107 |
|
| |
| 108 |
2 |
| case PDFMAKE_MK_ROW: return BIT(PDFMAKE_P_GAP) | BIT(PDFMAKE_P_HEIGHT) | |
| 109 |
|
| BIT(PDFMAKE_P_SPACING); |
| 110 |
50 |
| case PDFMAKE_MK_CELL: |
| 111 |
|
| case PDFMAKE_MK_TH: |
| 112 |
50 |
| case PDFMAKE_MK_TD: return CELL_PROPS; |
| 113 |
2 |
| case PDFMAKE_MK_TABLE: return TEXT_PROPS | BIT(PDFMAKE_P_GAP) | |
| 114 |
|
| BIT(PDFMAKE_P_SPACING) | |
| 115 |
|
| BIT(PDFMAKE_P_HEADER_REPEAT); |
| 116 |
2 |
| case PDFMAKE_MK_TR: return BIT(PDFMAKE_P_HEIGHT) | BIT(PDFMAKE_P_BG); |
| 117 |
|
| |
| 118 |
4 |
| case PDFMAKE_MK_BOOKMARK: return BIT(PDFMAKE_P_TITLE) | BIT(PDFMAKE_P_LEVEL); |
| 119 |
|
| |
| 120 |
10 |
| case PDFMAKE_MK_B: case PDFMAKE_MK_I: case PDFMAKE_MK_SPAN: |
| 121 |
10 |
| return TEXT_PROPS; |
| 122 |
|
| |
| 123 |
0 |
| default: return 0; |
| 124 |
|
| } |
| 125 |
|
| } |
| 126 |
|
| |
| 127 |
|
| /* Named colours: the sixteen from HTML 4 plus the greys people reach for. |
| 128 |
|
| * Deliberately short - a name that is not here is an error with a position, |
| 129 |
|
| * not a guess at what was meant. */ |
| 130 |
|
| typedef struct { const char *name; const char *hex; } colour_ent_t; |
| 131 |
|
| |
| 132 |
|
| static const colour_ent_t COLOURS[] = { |
| 133 |
|
| { "aqua", "#00ffff" }, { "black", "#000000" }, { "blue", "#0000ff" }, |
| 134 |
|
| { "fuchsia", "#ff00ff" }, { "gray", "#808080" }, { "green", "#008000" }, |
| 135 |
|
| { "grey", "#808080" }, { "lime", "#00ff00" }, { "maroon", "#800000" }, |
| 136 |
|
| { "navy", "#000080" }, { "olive", "#808000" }, { "purple", "#800080" }, |
| 137 |
|
| { "red", "#ff0000" }, { "silver", "#c0c0c0" }, { "teal", "#008080" }, |
| 138 |
|
| { "white", "#ffffff" }, { "yellow", "#ffff00" } |
| 139 |
|
| }; |
| 140 |
|
| |
| 141 |
|
| #define COLOUR_COUNT (sizeof(COLOURS) / sizeof(COLOURS[0])) |
| 142 |
|
| |
| 143 |
|
| /*---------------------------------------------------------------------------- |
| 144 |
|
| * Lookups |
| 145 |
|
| *--------------------------------------------------------------------------*/ |
| 146 |
|
| |
| 147 |
14794 |
| static const prop_ent_t *prop_ent(pdfmake_prop_t p) { |
| 148 |
|
| size_t i; |
| 149 |
212541 |
▶ | for (i = 0; i < PROP_COUNT; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < PROP_COUNT; i++) |
212541 |
0 |
212541 |
|
| 150 |
212541 |
▶ | if (PROPS[i].id == p) return &PROPS[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (PROPS[i].id == p) return &PROPS[i]; |
14794 |
197747 |
212541 |
|
| 151 |
0 |
| return NULL; |
| 152 |
|
| } |
| 153 |
|
| |
| 154 |
105 |
| pdfmake_prop_t pdfmake_style_prop(const char *name, size_t len) { |
| 155 |
|
| size_t i; |
| 156 |
105 |
▶ | if (!name) return PDFMAKE_P_NONE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!name) return PDFMAKE_P_NONE; |
0 |
105 |
105 |
|
| 157 |
|
| /* Both spellings of the same thing, once. */ |
| 158 |
105 |
▶ | if (len == 5 && memcmp(name, "color", 5) == 0) return PDFMAKE_P_COLOUR; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 5 && memcmp(name, "color", 5) == 0) return PDFMAKE_P_COLOUR; |
30 |
75 |
105 |
| if (len == 5 && memcmp(name, "color", 5) == 0) return PDFMAKE_P_COLOUR; |
2 |
28 |
30 |
|
| 159 |
1072 |
▶ | for (i = 0; i < PROP_COUNT; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < PROP_COUNT; i++) { |
1071 |
1 |
1072 |
|
| 160 |
1071 |
▶ | if (strlen(PROPS[i].name) == len && memcmp(PROPS[i].name, name, len) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (strlen(PROPS[i].name) == len && memcmp(PROPS[i].name, name, len) == 0) |
229 |
842 |
1071 |
| if (strlen(PROPS[i].name) == len && memcmp(PROPS[i].name, name, len) == 0) |
102 |
127 |
229 |
|
| 161 |
102 |
| return PROPS[i].id; |
| 162 |
|
| } |
| 163 |
1 |
| return PDFMAKE_P_NONE; |
| 164 |
|
| } |
| 165 |
|
| |
| 166 |
14410 |
| const char *pdfmake_style_prop_name(pdfmake_prop_t p) { |
| 167 |
14410 |
| const prop_ent_t *e = prop_ent(p); |
| 168 |
14410 |
▶ | return e ? e->name : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return e ? e->name : NULL; |
14410 |
0 |
14410 |
|
| 169 |
|
| } |
| 170 |
|
| |
| 171 |
311 |
| pdfmake_ptype_t pdfmake_style_type(pdfmake_prop_t p) { |
| 172 |
311 |
| const prop_ent_t *e = prop_ent(p); |
| 173 |
311 |
▶ | return e ? e->type : PDFMAKE_T_STR; |
Branch: 1 branch
| Branch | True | False | Total |
| return e ? e->type : PDFMAKE_T_STR; |
311 |
0 |
311 |
|
| 174 |
|
| } |
| 175 |
|
| |
| 176 |
73 |
| int pdfmake_style_inherits(pdfmake_prop_t p) { |
| 177 |
73 |
| const prop_ent_t *e = prop_ent(p); |
| 178 |
73 |
▶ | return e ? e->inherit : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return e ? e->inherit : 0; |
73 |
0 |
73 |
|
| 179 |
|
| } |
| 180 |
|
| |
| 181 |
104 |
| int pdfmake_style_tag_allows(pdfmake_markup_tag_t tag, pdfmake_prop_t p) { |
| 182 |
104 |
▶ | if (p <= PDFMAKE_P_NONE || p >= PDFMAKE_P_MAX) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p <= PDFMAKE_P_NONE || p >= PDFMAKE_P_MAX) return 0; |
104 |
0 |
104 |
| if (p <= PDFMAKE_P_NONE || p >= PDFMAKE_P_MAX) return 0; |
0 |
104 |
104 |
|
| 183 |
104 |
| return (tag_allow(tag) & BIT(p)) ? 1 : 0; |
| 184 |
|
| } |
| 185 |
|
| |
| 186 |
|
| /* The accepted list, for an error message. Static buffer: this is only ever |
| 187 |
|
| * used to build one message before it is copied. */ |
| 188 |
56 |
| const char *pdfmake_style_tag_allowed(pdfmake_markup_tag_t tag) { |
| 189 |
|
| static char buf[512]; |
| 190 |
|
| const char *names[PROP_COUNT]; |
| 191 |
56 |
| uint64_t allow = tag_allow(tag); |
| 192 |
56 |
| size_t i, j, count = 0, n = 0; |
| 193 |
|
| |
| 194 |
1624 |
▶ | for (i = 0; i < PROP_COUNT; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < PROP_COUNT; i++) |
1568 |
56 |
1624 |
|
| 195 |
1568 |
▶ | if (allow & BIT(PROPS[i].id)) names[count++] = PROPS[i].name; |
Branch: 1 branch
| Branch | True | False | Total |
| if (allow & BIT(PROPS[i].id)) names[count++] = PROPS[i].name; |
476 |
1092 |
1568 |
|
| 196 |
|
| |
| 197 |
|
| /* Alphabetical: someone reading "it takes: ..." in an error message is |
| 198 |
|
| * looking for one name, and the table's own grouping is no help there. */ |
| 199 |
480 |
▶ | for (i = 1; i < count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < count; i++) { |
424 |
56 |
480 |
|
| 200 |
424 |
| const char *key = names[i]; |
| 201 |
1326 |
▶ | for (j = i; j > 0 && strcmp(names[j - 1], key) > 0; j--) |
Branch: 2 branches
| Branch | True | False | Total |
| for (j = i; j > 0 && strcmp(names[j - 1], key) > 0; j--) |
1198 |
128 |
1326 |
| for (j = i; j > 0 && strcmp(names[j - 1], key) > 0; j--) |
902 |
296 |
1198 |
|
| 202 |
902 |
| names[j] = names[j - 1]; |
| 203 |
424 |
| names[j] = key; |
| 204 |
|
| } |
| 205 |
|
| |
| 206 |
56 |
| buf[0] = '\0'; |
| 207 |
532 |
▶ | for (i = 0; i < count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < count; i++) { |
476 |
56 |
532 |
|
| 208 |
476 |
| size_t l = strlen(names[i]); |
| 209 |
476 |
▶ | if (n && n + 2 < sizeof(buf)) { buf[n++] = ','; buf[n++] = ' '; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (n && n + 2 < sizeof(buf)) { buf[n++] = ','; buf[n++] = ' '; } |
424 |
52 |
476 |
| if (n && n + 2 < sizeof(buf)) { buf[n++] = ','; buf[n++] = ' '; } |
424 |
0 |
424 |
|
| 210 |
476 |
▶ | if (n + l + 1 >= sizeof(buf)) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n + l + 1 >= sizeof(buf)) break; |
0 |
476 |
476 |
|
| 211 |
476 |
| memcpy(buf + n, names[i], l); |
| 212 |
476 |
| n += l; |
| 213 |
|
| } |
| 214 |
56 |
| buf[n] = '\0'; |
| 215 |
56 |
▶ | return n ? buf : "(none)"; |
Branch: 1 branch
| Branch | True | False | Total |
| return n ? buf : "(none)"; |
52 |
4 |
56 |
|
| 216 |
|
| } |
| 217 |
|
| |
| 218 |
|
| /*---------------------------------------------------------------------------- |
| 219 |
|
| * Coercions |
| 220 |
|
| *--------------------------------------------------------------------------*/ |
| 221 |
|
| |
| 222 |
141 |
| static void trim(const char **s, size_t *len) { |
| 223 |
146 |
▶ | while (*len && isspace((unsigned char)(*s)[0])) { (*s)++; (*len)--; } |
Branch: 2 branches
| Branch | True | False | Total |
| while (*len && isspace((unsigned char)(*s)[0])) { (*s)++; (*len)--; } |
140 |
6 |
146 |
| while (*len && isspace((unsigned char)(*s)[0])) { (*s)++; (*len)--; } |
5 |
135 |
140 |
|
| 224 |
141 |
▶ | while (*len && isspace((unsigned char)(*s)[*len - 1])) (*len)--; |
Branch: 2 branches
| Branch | True | False | Total |
| while (*len && isspace((unsigned char)(*s)[*len - 1])) (*len)--; |
135 |
6 |
141 |
| while (*len && isspace((unsigned char)(*s)[*len - 1])) (*len)--; |
0 |
135 |
135 |
|
| 225 |
141 |
| } |
| 226 |
|
| |
| 227 |
23 |
| static int fail(char *err, size_t errlen, const char *fmt, ...) { |
| 228 |
|
| va_list ap; |
| 229 |
23 |
▶ | if (err && errlen) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (err && errlen) { |
23 |
0 |
23 |
| if (err && errlen) { |
23 |
0 |
23 |
|
| 230 |
23 |
| va_start(ap, fmt); |
| 231 |
23 |
| vsnprintf(err, errlen, fmt, ap); |
| 232 |
23 |
| va_end(ap); |
| 233 |
|
| } |
| 234 |
23 |
| return 0; |
| 235 |
|
| } |
| 236 |
|
| |
| 237 |
175 |
| static int hexval(char c) { |
| 238 |
175 |
▶ | if (c >= '0' && c <= '9') return c - '0'; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= '0' && c <= '9') return c - '0'; |
175 |
0 |
175 |
| if (c >= '0' && c <= '9') return c - '0'; |
62 |
113 |
175 |
|
| 239 |
113 |
▶ | if (c >= 'a' && c <= 'f') return c - 'a' + 10; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= 'a' && c <= 'f') return c - 'a' + 10; |
107 |
6 |
113 |
| if (c >= 'a' && c <= 'f') return c - 'a' + 10; |
106 |
1 |
107 |
|
| 240 |
7 |
▶ | if (c >= 'A' && c <= 'F') return c - 'A' + 10; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= 'A' && c <= 'F') return c - 'A' + 10; |
7 |
0 |
7 |
| if (c >= 'A' && c <= 'F') return c - 'A' + 10; |
6 |
1 |
7 |
|
| 241 |
1 |
| return -1; |
| 242 |
|
| } |
| 243 |
|
| |
| 244 |
39 |
| int pdfmake_style_colour(const char *v, size_t len, char out[8], |
| 245 |
|
| char *err, size_t errlen) { |
| 246 |
|
| size_t i; |
| 247 |
39 |
| trim(&v, &len); |
| 248 |
|
| |
| 249 |
39 |
▶ | if (len && v[0] == '#') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (len && v[0] == '#') { |
38 |
1 |
39 |
| if (len && v[0] == '#') { |
33 |
5 |
38 |
|
| 250 |
33 |
| const char *h = v + 1; |
| 251 |
33 |
| size_t hl = len - 1; |
| 252 |
33 |
▶ | if (hl == 3 || hl == 6) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (hl == 3 || hl == 6) { |
31 |
2 |
33 |
| if (hl == 3 || hl == 6) { |
29 |
2 |
31 |
|
| 253 |
|
| char tmp[7]; |
| 254 |
205 |
▶ | for (i = 0; i < hl; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < hl; i++) |
175 |
30 |
205 |
|
| 255 |
175 |
▶ | if (hexval(h[i]) < 0) goto bad; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hexval(h[i]) < 0) goto bad; |
1 |
174 |
175 |
|
| 256 |
30 |
▶ | if (hl == 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hl == 3) { |
2 |
28 |
30 |
|
| 257 |
8 |
▶ | for (i = 0; i < 3; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 3; i++) { |
6 |
2 |
8 |
|
| 258 |
6 |
| tmp[i * 2] = (char)tolower((unsigned char)h[i]); |
| 259 |
6 |
| tmp[i * 2 + 1] = (char)tolower((unsigned char)h[i]); |
| 260 |
|
| } |
| 261 |
|
| } else { |
| 262 |
196 |
▶ | for (i = 0; i < 6; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 6; i++) |
168 |
28 |
196 |
|
| 263 |
168 |
| tmp[i] = (char)tolower((unsigned char)h[i]); |
| 264 |
|
| } |
| 265 |
30 |
| out[0] = '#'; |
| 266 |
30 |
| memcpy(out + 1, tmp, 6); |
| 267 |
30 |
| out[7] = '\0'; |
| 268 |
30 |
| return 1; |
| 269 |
|
| } |
| 270 |
2 |
| goto bad; |
| 271 |
|
| } |
| 272 |
|
| |
| 273 |
79 |
▶ | for (i = 0; i < COLOUR_COUNT; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < COLOUR_COUNT; i++) { |
76 |
3 |
79 |
|
| 274 |
76 |
| size_t nl = strlen(COLOURS[i].name); |
| 275 |
76 |
▶ | if (nl != len) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nl != len) continue; |
67 |
9 |
76 |
|
| 276 |
|
| { |
| 277 |
|
| size_t j; |
| 278 |
22 |
▶ | for (j = 0; j < len; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < len; j++) |
19 |
3 |
22 |
|
| 279 |
19 |
▶ | if (tolower((unsigned char)v[j]) != COLOURS[i].name[j]) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tolower((unsigned char)v[j]) != COLOURS[i].name[j]) break; |
6 |
13 |
19 |
|
| 280 |
9 |
▶ | if (j == len) { memcpy(out, COLOURS[i].hex, 8); return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (j == len) { memcpy(out, COLOURS[i].hex, 8); return 1; } |
3 |
6 |
9 |
|
| 281 |
|
| } |
| 282 |
|
| } |
| 283 |
|
| |
| 284 |
3 |
| bad: |
| 285 |
6 |
| return fail(err, errlen, |
| 286 |
|
| "'%.*s' is not a colour: use #rgb, #rrggbb or a colour name", |
| 287 |
6 |
▶ | (int)(len > 40 ? 40 : len), v); |
Branch: 1 branch
| Branch | True | False | Total |
| (int)(len > 40 ? 40 : len), v); |
6 |
0 |
6 |
|
| 288 |
|
| } |
| 289 |
|
| |
| 290 |
|
| /* A number, with an optional trailing "pt" for lengths. No other unit |
| 291 |
|
| * exists, because the engine has no notion of one. */ |
| 292 |
35 |
| static int parse_num(const char *v, size_t len, int allow_pt, double *out) { |
| 293 |
|
| char buf[64]; |
| 294 |
|
| char *end; |
| 295 |
|
| double d; |
| 296 |
|
| |
| 297 |
35 |
| trim(&v, &len); |
| 298 |
35 |
▶ | if (allow_pt && len > 2 && |
Branch: 2 branches
| Branch | True | False | Total |
| if (allow_pt && len > 2 && |
29 |
6 |
35 |
| if (allow_pt && len > 2 && |
8 |
21 |
29 |
|
| 299 |
8 |
▶ | (v[len - 2] == 'p' || v[len - 2] == 'P') && |
Branch: 2 branches
| Branch | True | False | Total |
| (v[len - 2] == 'p' || v[len - 2] == 'P') && |
6 |
2 |
8 |
| (v[len - 2] == 'p' || v[len - 2] == 'P') && |
0 |
6 |
6 |
|
| 300 |
2 |
▶ | (v[len - 1] == 't' || v[len - 1] == 'T')) |
Branch: 2 branches
| Branch | True | False | Total |
| (v[len - 1] == 't' || v[len - 1] == 'T')) |
1 |
1 |
2 |
| (v[len - 1] == 't' || v[len - 1] == 'T')) |
0 |
1 |
1 |
|
| 301 |
1 |
| len -= 2; |
| 302 |
35 |
▶ | if (!len || len >= sizeof(buf)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!len || len >= sizeof(buf)) return 0; |
34 |
1 |
35 |
| if (!len || len >= sizeof(buf)) return 0; |
0 |
34 |
34 |
|
| 303 |
|
| |
| 304 |
34 |
| memcpy(buf, v, len); |
| 305 |
34 |
| buf[len] = '\0'; |
| 306 |
|
| |
| 307 |
|
| /* strtod would take "0x10", "inf" and leading whitespace; the grammar |
| 308 |
|
| * here is a plain decimal number and nothing else. */ |
| 309 |
|
| { |
| 310 |
34 |
| size_t i = 0; |
| 311 |
34 |
| int digits = 0, dot = 0; |
| 312 |
34 |
▶ | if (buf[i] == '-' || buf[i] == '+') i++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (buf[i] == '-' || buf[i] == '+') i++; |
33 |
1 |
34 |
| if (buf[i] == '-' || buf[i] == '+') i++; |
0 |
33 |
33 |
|
| 313 |
82 |
▶ | for (; buf[i]; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; buf[i]; i++) { |
55 |
27 |
82 |
|
| 314 |
55 |
▶ | if (buf[i] == '.') { if (dot) return 0; dot = 1; continue; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (buf[i] == '.') { if (dot) return 0; dot = 1; continue; } |
1 |
54 |
55 |
| if (buf[i] == '.') { if (dot) return 0; dot = 1; continue; } |
0 |
1 |
1 |
|
| 315 |
54 |
▶ | if (buf[i] < '0' || buf[i] > '9') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (buf[i] < '0' || buf[i] > '9') return 0; |
53 |
1 |
54 |
| if (buf[i] < '0' || buf[i] > '9') return 0; |
6 |
47 |
53 |
|
| 316 |
47 |
| digits++; |
| 317 |
|
| } |
| 318 |
27 |
▶ | if (!digits) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!digits) return 0; |
0 |
27 |
27 |
|
| 319 |
|
| } |
| 320 |
|
| |
| 321 |
27 |
| d = strtod(buf, &end); |
| 322 |
27 |
▶ | if (end == buf || *end) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (end == buf || *end) return 0; |
27 |
0 |
27 |
| if (end == buf || *end) return 0; |
0 |
27 |
27 |
|
| 323 |
27 |
| *out = d; |
| 324 |
27 |
| return 1; |
| 325 |
|
| } |
| 326 |
|
| |
| 327 |
29 |
| int pdfmake_style_length(const char *v, size_t len, double *out, |
| 328 |
|
| char *err, size_t errlen) { |
| 329 |
29 |
▶ | if (parse_num(v, len, 1, out)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (parse_num(v, len, 1, out)) return 1; |
21 |
8 |
29 |
|
| 330 |
8 |
▶ | return fail(err, errlen, "'%.*s' is not a number of points", |
Branch: 1 branch
| Branch | True | False | Total |
| return fail(err, errlen, "'%.*s' is not a number of points", |
8 |
0 |
8 |
|
| 331 |
|
| (int)(len > 40 ? 40 : len), v); |
| 332 |
|
| } |
| 333 |
|
| |
| 334 |
6 |
| int pdfmake_style_number(const char *v, size_t len, double *out, |
| 335 |
|
| char *err, size_t errlen) { |
| 336 |
6 |
▶ | if (parse_num(v, len, 0, out)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (parse_num(v, len, 0, out)) return 1; |
6 |
0 |
6 |
|
| 337 |
0 |
▶ | return fail(err, errlen, "'%.*s' is not a number", |
Branch: 1 branch
| Branch | True | False | Total |
| return fail(err, errlen, "'%.*s' is not a number", |
0 |
0 |
0 |
|
| 338 |
|
| (int)(len > 40 ? 40 : len), v); |
| 339 |
|
| } |
| 340 |
|
| |
| 341 |
|
| /* Booleans are written the way a template writes them, and nothing else is |
| 342 |
|
| * guessed: "maybe" is a mistake, not a false. */ |
| 343 |
17 |
| int pdfmake_style_bool(const char *v, size_t len, int *out, |
| 344 |
|
| char *err, size_t errlen) { |
| 345 |
|
| static const char *T[] = { "1", "true", "yes", "on" }; |
| 346 |
|
| static const char *F[] = { "0", "false", "no", "off" }; |
| 347 |
|
| size_t i, j; |
| 348 |
17 |
| trim(&v, &len); |
| 349 |
|
| |
| 350 |
51 |
▶ | for (i = 0; i < 4; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 4; i++) { |
47 |
4 |
51 |
|
| 351 |
121 |
▶ | for (j = 0; j < 2; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < 2; j++) { |
87 |
34 |
121 |
|
| 352 |
87 |
▶ | const char *w = j ? F[i] : T[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *w = j ? F[i] : T[i]; |
40 |
47 |
87 |
|
| 353 |
87 |
| size_t wl = strlen(w); |
| 354 |
|
| size_t k; |
| 355 |
87 |
▶ | if (wl != len) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (wl != len) continue; |
66 |
21 |
87 |
|
| 356 |
57 |
▶ | for (k = 0; k < len; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < len; k++) |
44 |
13 |
57 |
|
| 357 |
44 |
▶ | if (tolower((unsigned char)v[k]) != w[k]) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tolower((unsigned char)v[k]) != w[k]) break; |
8 |
36 |
44 |
|
| 358 |
21 |
▶ | if (k == len) { *out = j ? 0 : 1; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (k == len) { *out = j ? 0 : 1; return 1; } |
13 |
8 |
21 |
|
| 359 |
|
| } |
| 360 |
|
| } |
| 361 |
4 |
| return fail(err, errlen, |
| 362 |
|
| "'%.*s' is not a yes or no: use 1/0, true/false, yes/no, on/off", |
| 363 |
4 |
▶ | (int)(len > 40 ? 40 : len), v); |
Branch: 1 branch
| Branch | True | False | Total |
| (int)(len > 40 ? 40 : len), v); |
4 |
0 |
4 |
|
| 364 |
|
| } |
| 365 |
|
| |
| 366 |
13 |
| static int parse_align(const char *v, size_t len, double *out, |
| 367 |
|
| char *err, size_t errlen) { |
| 368 |
13 |
| trim(&v, &len); |
| 369 |
13 |
▶ | if (len == 4 && strncasecmp(v, "left", 4) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 4 && strncasecmp(v, "left", 4) == 0) |
0 |
13 |
13 |
| if (len == 4 && strncasecmp(v, "left", 4) == 0) |
0 |
0 |
0 |
|
| 370 |
0 |
| { *out = PDFMAKE_ALIGN_LEFT; return 1; } |
| 371 |
13 |
▶ | if (len == 5 && strncasecmp(v, "right", 5) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 5 && strncasecmp(v, "right", 5) == 0) |
12 |
1 |
13 |
| if (len == 5 && strncasecmp(v, "right", 5) == 0) |
12 |
0 |
12 |
|
| 372 |
12 |
| { *out = PDFMAKE_ALIGN_RIGHT; return 1; } |
| 373 |
|
| /* both spellings, one value */ |
| 374 |
1 |
▶ | if ((len == 6 && strncasecmp(v, "center", 6) == 0) || |
Branch: 2 branches
| Branch | True | False | Total |
| if ((len == 6 && strncasecmp(v, "center", 6) == 0) || |
1 |
0 |
1 |
| if ((len == 6 && strncasecmp(v, "center", 6) == 0) || |
1 |
0 |
1 |
|
| 375 |
1 |
▶ | (len == 6 && strncasecmp(v, "centre", 6) == 0)) |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 6 && strncasecmp(v, "centre", 6) == 0)) |
1 |
0 |
1 |
| (len == 6 && strncasecmp(v, "centre", 6) == 0)) |
1 |
0 |
1 |
|
| 376 |
1 |
| { *out = PDFMAKE_ALIGN_CENTER; return 1; } |
| 377 |
0 |
| return fail(err, errlen, "'%.*s' is not one of: left, center, right", |
| 378 |
0 |
▶ | (int)(len > 40 ? 40 : len), v); |
Branch: 1 branch
| Branch | True | False | Total |
| (int)(len > 40 ? 40 : len), v); |
0 |
0 |
0 |
|
| 379 |
|
| } |
| 380 |
|
| |
| 381 |
0 |
| static int parse_valign(const char *v, size_t len, double *out, |
| 382 |
|
| char *err, size_t errlen) { |
| 383 |
0 |
| trim(&v, &len); |
| 384 |
0 |
▶ | if (len == 3 && strncasecmp(v, "top", 3) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 3 && strncasecmp(v, "top", 3) == 0) |
0 |
0 |
0 |
| if (len == 3 && strncasecmp(v, "top", 3) == 0) |
0 |
0 |
0 |
|
| 385 |
0 |
| { *out = PDFMAKE_VALIGN_TOP; return 1; } |
| 386 |
0 |
▶ | if (len == 6 && strncasecmp(v, "middle", 6) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 6 && strncasecmp(v, "middle", 6) == 0) |
0 |
0 |
0 |
| if (len == 6 && strncasecmp(v, "middle", 6) == 0) |
0 |
0 |
0 |
|
| 387 |
0 |
| { *out = PDFMAKE_VALIGN_MIDDLE; return 1; } |
| 388 |
0 |
▶ | if (len == 6 && strncasecmp(v, "bottom", 6) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 6 && strncasecmp(v, "bottom", 6) == 0) |
0 |
0 |
0 |
| if (len == 6 && strncasecmp(v, "bottom", 6) == 0) |
0 |
0 |
0 |
|
| 389 |
0 |
| { *out = PDFMAKE_VALIGN_BOTTOM; return 1; } |
| 390 |
0 |
| return fail(err, errlen, "'%.*s' is not one of: top, middle, bottom", |
| 391 |
0 |
▶ | (int)(len > 40 ? 40 : len), v); |
Branch: 1 branch
| Branch | True | False | Total |
| (int)(len > 40 ? 40 : len), v); |
0 |
0 |
0 |
|
| 392 |
|
| } |
| 393 |
|
| |
| 394 |
|
| /*---------------------------------------------------------------------------- |
| 395 |
|
| * Resolving |
| 396 |
|
| *--------------------------------------------------------------------------*/ |
| 397 |
|
| |
| 398 |
1046 |
| void pdfmake_style_init(pdfmake_style_t *s) { |
| 399 |
1046 |
▶ | if (s) memset(s, 0, sizeof(*s)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s) memset(s, 0, sizeof(*s)); |
1046 |
0 |
1046 |
|
| 400 |
1046 |
| } |
| 401 |
|
| |
| 402 |
56 |
| static int coerce(pdfmake_prop_t p, const char *v, size_t vlen, |
| 403 |
|
| pdfmake_value_t *out, char *err, size_t errlen) { |
| 404 |
|
| char sub[PDFMAKE_STYLE_ERR_LEN]; |
| 405 |
56 |
| int b = 0; |
| 406 |
|
| |
| 407 |
56 |
| switch (pdfmake_style_type(p)) { |
| 408 |
20 |
| case PDFMAKE_T_LEN: |
| 409 |
20 |
▶ | if (!pdfmake_style_length(v, vlen, &out->num, sub, sizeof(sub))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pdfmake_style_length(v, vlen, &out->num, sub, sizeof(sub))) |
3 |
17 |
20 |
|
| 410 |
3 |
| return fail(err, errlen, "%s %s", pdfmake_style_prop_name(p), sub); |
| 411 |
17 |
| break; |
| 412 |
6 |
| case PDFMAKE_T_NUM: |
| 413 |
6 |
▶ | if (!pdfmake_style_number(v, vlen, &out->num, sub, sizeof(sub))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pdfmake_style_number(v, vlen, &out->num, sub, sizeof(sub))) |
0 |
6 |
6 |
|
| 414 |
0 |
| return fail(err, errlen, "%s %s", pdfmake_style_prop_name(p), sub); |
| 415 |
6 |
| break; |
| 416 |
9 |
| case PDFMAKE_T_COLOUR: |
| 417 |
9 |
▶ | if (!pdfmake_style_colour(v, vlen, out->colour, sub, sizeof(sub))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pdfmake_style_colour(v, vlen, out->colour, sub, sizeof(sub))) |
0 |
9 |
9 |
|
| 418 |
0 |
| return fail(err, errlen, "%s %s", pdfmake_style_prop_name(p), sub); |
| 419 |
9 |
| break; |
| 420 |
1 |
| case PDFMAKE_T_BOOL: |
| 421 |
1 |
▶ | if (!pdfmake_style_bool(v, vlen, &b, sub, sizeof(sub))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pdfmake_style_bool(v, vlen, &b, sub, sizeof(sub))) |
0 |
1 |
1 |
|
| 422 |
0 |
| return fail(err, errlen, "%s %s", pdfmake_style_prop_name(p), sub); |
| 423 |
1 |
| out->num = b; |
| 424 |
1 |
| break; |
| 425 |
13 |
| case PDFMAKE_T_ALIGN: |
| 426 |
13 |
▶ | if (!parse_align(v, vlen, &out->num, sub, sizeof(sub))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!parse_align(v, vlen, &out->num, sub, sizeof(sub))) |
0 |
13 |
13 |
|
| 427 |
0 |
| return fail(err, errlen, "%s %s", pdfmake_style_prop_name(p), sub); |
| 428 |
13 |
| break; |
| 429 |
0 |
| case PDFMAKE_T_VALIGN: |
| 430 |
0 |
▶ | if (!parse_valign(v, vlen, &out->num, sub, sizeof(sub))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!parse_valign(v, vlen, &out->num, sub, sizeof(sub))) |
0 |
0 |
0 |
|
| 431 |
0 |
| return fail(err, errlen, "%s %s", pdfmake_style_prop_name(p), sub); |
| 432 |
0 |
| break; |
| 433 |
7 |
| case PDFMAKE_T_STR: |
| 434 |
|
| default: |
| 435 |
7 |
| out->str = v; |
| 436 |
7 |
| out->str_len = vlen; |
| 437 |
7 |
| break; |
| 438 |
|
| } |
| 439 |
53 |
| out->set = 1; |
| 440 |
53 |
| return 1; |
| 441 |
|
| } |
| 442 |
|
| |
| 443 |
46 |
| int pdfmake_style_attrs(const pdfmake_markup_node_t *node, |
| 444 |
|
| pdfmake_style_t *out, |
| 445 |
|
| char *err, size_t errlen) { |
| 446 |
|
| const pdfmake_markup_attr_t *a; |
| 447 |
|
| const char *tag_name; |
| 448 |
|
| |
| 449 |
46 |
| pdfmake_style_init(out); |
| 450 |
46 |
▶ | if (!node) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!node) return 1; |
0 |
46 |
46 |
|
| 451 |
46 |
| tag_name = pdfmake_markup_tag_name(node->tag); |
| 452 |
|
| |
| 453 |
89 |
▶ | for (a = node->attrs; a; a = a->next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (a = node->attrs; a; a = a->next) { |
46 |
43 |
89 |
|
| 454 |
46 |
| pdfmake_prop_t p = pdfmake_style_prop(a->name, a->name_len); |
| 455 |
|
| |
| 456 |
46 |
▶ | if (p == PDFMAKE_P_NONE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (p == PDFMAKE_P_NONE) |
0 |
46 |
46 |
|
| 457 |
0 |
▶ | return fail(err, errlen, "unknown attribute '%.*s' on <%s>", |
Branch: 1 branch
| Branch | True | False | Total |
| return fail(err, errlen, "unknown attribute '%.*s' on <%s>", |
0 |
0 |
0 |
|
| 458 |
0 |
| (int)a->name_len, a->name, tag_name ? tag_name : "?"); |
| 459 |
|
| |
| 460 |
46 |
▶ | if (!pdfmake_style_tag_allows(node->tag, p)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pdfmake_style_tag_allows(node->tag, p)) |
0 |
46 |
46 |
|
| 461 |
0 |
| return fail(err, errlen, |
| 462 |
|
| "<%s> does not take '%.*s' (it takes: %s)", |
| 463 |
|
| tag_name ? tag_name : "?", |
| 464 |
0 |
▶ | (int)a->name_len, a->name, |
Branch: 1 branch
| Branch | True | False | Total |
| (int)a->name_len, a->name, |
0 |
0 |
0 |
|
| 465 |
0 |
| pdfmake_style_tag_allowed(node->tag)); |
| 466 |
|
| |
| 467 |
46 |
▶ | if (!coerce(p, a->value, a->value_len, &out->v[p], err, errlen)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!coerce(p, a->value, a->value_len, &out->v[p], err, errlen)) |
3 |
43 |
46 |
|
| 468 |
3 |
| return 0; |
| 469 |
|
| } |
| 470 |
43 |
| return 1; |
| 471 |
|
| } |
| 472 |
|
| |
| 473 |
9 |
| int pdfmake_style_declarations(const char *src, size_t len, |
| 474 |
|
| pdfmake_markup_tag_t tag, |
| 475 |
|
| pdfmake_style_t *out, |
| 476 |
|
| char *err, size_t errlen) { |
| 477 |
9 |
| const char *p = src, *end = src + len; |
| 478 |
9 |
| const char *tag_name = pdfmake_markup_tag_name(tag); |
| 479 |
|
| |
| 480 |
9 |
| pdfmake_style_init(out); |
| 481 |
9 |
▶ | if (!src) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!src) return 1; |
0 |
9 |
9 |
|
| 482 |
9 |
▶ | if (!tag_name) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tag_name) |
0 |
9 |
9 |
|
| 483 |
0 |
| return fail(err, errlen, "no such tag in <style>"); |
| 484 |
|
| |
| 485 |
22 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
15 |
7 |
22 |
|
| 486 |
15 |
| const char *decl = p, *colon = NULL, *q; |
| 487 |
|
| size_t dlen; |
| 488 |
|
| pdfmake_prop_t prop; |
| 489 |
|
| const char *name, *value; |
| 490 |
|
| size_t nlen, vlen; |
| 491 |
|
| |
| 492 |
130 |
▶ | while (p < end && *p != ';') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != ';') p++; |
122 |
8 |
130 |
| while (p < end && *p != ';') p++; |
115 |
7 |
122 |
|
| 493 |
15 |
| dlen = (size_t)(p - decl); |
| 494 |
15 |
▶ | if (p < end) p++; /* the ';' */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (p < end) p++; /* the ';' */ |
7 |
8 |
15 |
|
| 495 |
|
| |
| 496 |
|
| { /* skip an empty entry */ |
| 497 |
15 |
| const char *t = decl; |
| 498 |
15 |
| size_t tl = dlen; |
| 499 |
15 |
| trim(&t, &tl); |
| 500 |
15 |
▶ | if (!tl) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tl) continue; |
3 |
12 |
15 |
|
| 501 |
|
| } |
| 502 |
|
| |
| 503 |
74 |
▶ | for (q = decl; q < decl + dlen; q++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (q = decl; q < decl + dlen; q++) |
73 |
1 |
74 |
|
| 504 |
73 |
▶ | if (*q == ':') { colon = q; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*q == ':') { colon = q; break; } |
11 |
62 |
73 |
|
| 505 |
12 |
▶ | if (!colon) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!colon) |
1 |
11 |
12 |
|
| 506 |
2 |
▶ | return fail(err, errlen, |
Branch: 1 branch
| Branch | True | False | Total |
| return fail(err, errlen, |
1 |
0 |
1 |
|
| 507 |
|
| "<style> entry for '%s' is not name:value near '%.*s'", |
| 508 |
|
| tag_name, (int)(dlen > 40 ? 40 : dlen), decl); |
| 509 |
|
| |
| 510 |
11 |
| name = decl; |
| 511 |
11 |
| nlen = (size_t)(colon - decl); |
| 512 |
11 |
| value = colon + 1; |
| 513 |
11 |
| vlen = (size_t)((decl + dlen) - value); |
| 514 |
11 |
| trim(&name, &nlen); |
| 515 |
11 |
| trim(&value, &vlen); |
| 516 |
|
| |
| 517 |
11 |
| prop = pdfmake_style_prop(name, nlen); |
| 518 |
11 |
▶ | if (prop == PDFMAKE_P_NONE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (prop == PDFMAKE_P_NONE) |
0 |
11 |
11 |
|
| 519 |
0 |
| return fail(err, errlen, |
| 520 |
|
| "unknown property '%.*s' in <style> for '%s'", |
| 521 |
|
| (int)nlen, name, tag_name); |
| 522 |
11 |
▶ | if (!pdfmake_style_tag_allows(tag, prop)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pdfmake_style_tag_allows(tag, prop)) |
1 |
10 |
11 |
|
| 523 |
1 |
| return fail(err, errlen, |
| 524 |
|
| "'%.*s' does not apply to <%s> (it takes: %s)", |
| 525 |
|
| (int)nlen, name, tag_name, |
| 526 |
|
| pdfmake_style_tag_allowed(tag)); |
| 527 |
|
| |
| 528 |
10 |
▶ | if (!coerce(prop, value, vlen, &out->v[prop], err, errlen)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!coerce(prop, value, vlen, &out->v[prop], err, errlen)) |
0 |
10 |
10 |
|
| 529 |
0 |
| return 0; |
| 530 |
|
| } |
| 531 |
7 |
| return 1; |
| 532 |
|
| } |
| 533 |
|
| |
| 534 |
198 |
| void pdfmake_style_inherit(const pdfmake_style_t *parent, |
| 535 |
|
| const pdfmake_style_t *own, |
| 536 |
|
| pdfmake_style_t *out) { |
| 537 |
|
| int i; |
| 538 |
198 |
| pdfmake_style_init(out); |
| 539 |
|
| |
| 540 |
|
| /* Text properties pass down. Box properties - padding, background, |
| 541 |
|
| * weights - stop where they are declared: a cell's padding is the |
| 542 |
|
| * cell's, not that of every paragraph inside it. */ |
| 543 |
198 |
▶ | if (parent) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (parent) { |
198 |
0 |
198 |
|
| 544 |
5742 |
▶ | for (i = 1; i < PDFMAKE_P_MAX; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < PDFMAKE_P_MAX; i++) |
5544 |
198 |
5742 |
|
| 545 |
5544 |
▶ | if (parent->v[i].set && pdfmake_style_inherits((pdfmake_prop_t)i)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (parent->v[i].set && pdfmake_style_inherits((pdfmake_prop_t)i)) |
45 |
5499 |
5544 |
| if (parent->v[i].set && pdfmake_style_inherits((pdfmake_prop_t)i)) |
6 |
39 |
45 |
|
| 546 |
6 |
| out->v[i] = parent->v[i]; |
| 547 |
|
| } |
| 548 |
198 |
▶ | if (own) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (own) { |
198 |
0 |
198 |
|
| 549 |
5742 |
▶ | for (i = 1; i < PDFMAKE_P_MAX; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < PDFMAKE_P_MAX; i++) |
5544 |
198 |
5742 |
|
| 550 |
5544 |
▶ | if (own->v[i].set) out->v[i] = own->v[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (own->v[i].set) out->v[i] = own->v[i]; |
41 |
5503 |
5544 |
|
| 551 |
|
| } |
| 552 |
198 |
| } |