/src/mupdf/source/fitz/text.c
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (C) 2004-2021 Artifex Software, Inc. |
2 | | // |
3 | | // This file is part of MuPDF. |
4 | | // |
5 | | // MuPDF is free software: you can redistribute it and/or modify it under the |
6 | | // terms of the GNU Affero General Public License as published by the Free |
7 | | // Software Foundation, either version 3 of the License, or (at your option) |
8 | | // any later version. |
9 | | // |
10 | | // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY |
11 | | // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
12 | | // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more |
13 | | // details. |
14 | | // |
15 | | // You should have received a copy of the GNU Affero General Public License |
16 | | // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html> |
17 | | // |
18 | | // Alternative licensing terms are available from the licensor. |
19 | | // For commercial licensing, see <https://www.artifex.com/> or contact |
20 | | // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
21 | | // CA 94129, USA, for further information. |
22 | | |
23 | | #include "mupdf/fitz.h" |
24 | | |
25 | | #include <string.h> |
26 | | |
27 | | fz_text * |
28 | | fz_new_text(fz_context *ctx) |
29 | 236k | { |
30 | 236k | fz_text *text = fz_malloc_struct(ctx, fz_text); |
31 | 236k | text->refs = 1; |
32 | 236k | return text; |
33 | 236k | } |
34 | | |
35 | | fz_text * |
36 | | fz_keep_text(fz_context *ctx, const fz_text *textc) |
37 | 908 | { |
38 | 908 | fz_text *text = (fz_text *)textc; /* Explicit cast away of const */ |
39 | | |
40 | 908 | return fz_keep_imp(ctx, text, &text->refs); |
41 | 908 | } |
42 | | |
43 | | void |
44 | | fz_drop_text(fz_context *ctx, const fz_text *textc) |
45 | 273k | { |
46 | 273k | fz_text *text = (fz_text *)textc; /* Explicit cast away of const */ |
47 | | |
48 | 273k | if (fz_drop_imp(ctx, text, &text->refs)) |
49 | 236k | { |
50 | 236k | fz_text_span *span = text->head; |
51 | 531k | while (span) |
52 | 295k | { |
53 | 295k | fz_text_span *next = span->next; |
54 | 295k | fz_drop_font(ctx, span->font); |
55 | 295k | fz_free(ctx, span->items); |
56 | 295k | fz_free(ctx, span); |
57 | 295k | span = next; |
58 | 295k | } |
59 | 236k | fz_free(ctx, text); |
60 | 236k | } |
61 | 273k | } |
62 | | |
63 | | static fz_text_span * |
64 | | fz_new_text_span(fz_context *ctx, fz_font *font, int wmode, int bidi_level, fz_bidi_direction markup_dir, fz_text_language language, fz_matrix trm) |
65 | 295k | { |
66 | 295k | fz_text_span *span = fz_malloc_struct(ctx, fz_text_span); |
67 | 295k | span->font = fz_keep_font(ctx, font); |
68 | 295k | span->wmode = wmode; |
69 | 295k | span->bidi_level = bidi_level; |
70 | 295k | span->markup_dir = markup_dir; |
71 | 295k | span->language = language; |
72 | 295k | span->trm = trm; |
73 | 295k | span->trm.e = 0; |
74 | 295k | span->trm.f = 0; |
75 | 295k | return span; |
76 | 295k | } |
77 | | |
78 | | static fz_text_span * |
79 | | fz_add_text_span(fz_context *ctx, fz_text *text, fz_font *font, int wmode, int bidi_level, fz_bidi_direction markup_dir, fz_text_language language, fz_matrix trm) |
80 | 6.98M | { |
81 | 6.98M | if (!text->tail) |
82 | 236k | { |
83 | 236k | text->head = text->tail = fz_new_text_span(ctx, font, wmode, bidi_level, markup_dir, language, trm); |
84 | 236k | } |
85 | 6.74M | else if (text->tail->font != font || |
86 | 6.74M | text->tail->wmode != wmode || |
87 | 6.74M | text->tail->bidi_level != bidi_level || |
88 | 6.74M | text->tail->markup_dir != markup_dir || |
89 | 6.74M | text->tail->language != language || |
90 | 6.74M | text->tail->trm.a != trm.a || |
91 | 6.74M | text->tail->trm.b != trm.b || |
92 | 6.74M | text->tail->trm.c != trm.c || |
93 | 6.74M | text->tail->trm.d != trm.d) |
94 | 58.8k | { |
95 | 58.8k | text->tail = text->tail->next = fz_new_text_span(ctx, font, wmode, bidi_level, markup_dir, language, trm); |
96 | 58.8k | } |
97 | 6.98M | return text->tail; |
98 | 6.98M | } |
99 | | |
100 | | static void |
101 | | fz_grow_text_span(fz_context *ctx, fz_text_span *span, int n) |
102 | 6.98M | { |
103 | 6.98M | int new_cap = span->cap; |
104 | 6.98M | if (span->len + n < new_cap) |
105 | 6.39M | return; |
106 | 1.02M | while (span->len + n > new_cap) |
107 | 438k | new_cap = new_cap + 36; |
108 | 583k | span->items = fz_realloc_array(ctx, span->items, new_cap, fz_text_item); |
109 | 583k | span->cap = new_cap; |
110 | 583k | } |
111 | | |
112 | | void |
113 | | fz_show_glyph(fz_context *ctx, fz_text *text, fz_font *font, fz_matrix trm, int gid, int ucs, int wmode, int bidi_level, fz_bidi_direction markup_dir, fz_text_language lang) |
114 | 6.98M | { |
115 | 6.98M | fz_text_span *span; |
116 | | |
117 | 6.98M | if (text->refs != 1) |
118 | 0 | fz_throw(ctx, FZ_ERROR_GENERIC, "cannot modify shared text objects"); |
119 | | |
120 | 6.98M | span = fz_add_text_span(ctx, text, font, wmode, bidi_level, markup_dir, lang, trm); |
121 | | |
122 | 6.98M | fz_grow_text_span(ctx, span, 1); |
123 | | |
124 | 6.98M | span->items[span->len].ucs = ucs; |
125 | 6.98M | span->items[span->len].gid = gid; |
126 | 6.98M | span->items[span->len].x = trm.e; |
127 | 6.98M | span->items[span->len].y = trm.f; |
128 | 6.98M | span->len++; |
129 | 6.98M | } |
130 | | |
131 | | fz_matrix |
132 | | fz_show_string(fz_context *ctx, fz_text *text, fz_font *user_font, fz_matrix trm, const char *s, |
133 | | int wmode, int bidi_level, fz_bidi_direction markup_dir, fz_text_language language) |
134 | 0 | { |
135 | 0 | fz_font *font; |
136 | 0 | int gid, ucs; |
137 | 0 | float adv; |
138 | |
|
139 | 0 | while (*s) |
140 | 0 | { |
141 | 0 | s += fz_chartorune(&ucs, s); |
142 | 0 | gid = fz_encode_character_with_fallback(ctx, user_font, ucs, 0, language, &font); |
143 | 0 | fz_show_glyph(ctx, text, font, trm, gid, ucs, wmode, bidi_level, markup_dir, language); |
144 | 0 | adv = fz_advance_glyph(ctx, font, gid, wmode); |
145 | 0 | if (wmode == 0) |
146 | 0 | trm = fz_pre_translate(trm, adv, 0); |
147 | 0 | else |
148 | 0 | trm = fz_pre_translate(trm, 0, -adv); |
149 | 0 | } |
150 | |
|
151 | 0 | return trm; |
152 | 0 | } |
153 | | |
154 | | fz_matrix |
155 | | fz_measure_string(fz_context *ctx, fz_font *user_font, fz_matrix trm, const char *s, |
156 | | int wmode, int bidi_level, fz_bidi_direction markup_dir, fz_text_language language) |
157 | 0 | { |
158 | 0 | fz_font *font; |
159 | 0 | int gid, ucs; |
160 | 0 | float adv; |
161 | |
|
162 | 0 | while (*s) |
163 | 0 | { |
164 | 0 | s += fz_chartorune(&ucs, s); |
165 | 0 | gid = fz_encode_character_with_fallback(ctx, user_font, ucs, 0, language, &font); |
166 | 0 | adv = fz_advance_glyph(ctx, font, gid, wmode); |
167 | 0 | if (wmode == 0) |
168 | 0 | trm = fz_pre_translate(trm, adv, 0); |
169 | 0 | else |
170 | 0 | trm = fz_pre_translate(trm, 0, -adv); |
171 | 0 | } |
172 | |
|
173 | 0 | return trm; |
174 | 0 | } |
175 | | |
176 | | fz_rect |
177 | | fz_bound_text(fz_context *ctx, const fz_text *text, const fz_stroke_state *stroke, fz_matrix ctm) |
178 | 4.08k | { |
179 | 4.08k | fz_text_span *span; |
180 | 4.08k | fz_matrix tm, trm; |
181 | 4.08k | fz_rect gbox; |
182 | 4.08k | fz_rect bbox; |
183 | 4.08k | int i; |
184 | | |
185 | 4.08k | bbox = fz_empty_rect; |
186 | | |
187 | 8.47k | for (span = text->head; span; span = span->next) |
188 | 4.38k | { |
189 | 4.38k | if (span->len > 0) |
190 | 4.38k | { |
191 | 4.38k | tm = span->trm; |
192 | 377k | for (i = 0; i < span->len; i++) |
193 | 373k | { |
194 | 373k | if (span->items[i].gid >= 0) |
195 | 373k | { |
196 | 373k | tm.e = span->items[i].x; |
197 | 373k | tm.f = span->items[i].y; |
198 | 373k | trm = fz_concat(tm, ctm); |
199 | 373k | gbox = fz_bound_glyph(ctx, span->font, span->items[i].gid, trm); |
200 | 373k | bbox = fz_union_rect(bbox, gbox); |
201 | 373k | } |
202 | 373k | } |
203 | 4.38k | } |
204 | 4.38k | } |
205 | | |
206 | 4.08k | if (!fz_is_empty_rect(bbox)) |
207 | 3.36k | { |
208 | 3.36k | if (stroke) |
209 | 251 | bbox = fz_adjust_rect_for_stroke(ctx, bbox, stroke, ctm); |
210 | | |
211 | | /* Compensate for the glyph cache limited positioning precision */ |
212 | 3.36k | bbox.x0 -= 1; |
213 | 3.36k | bbox.y0 -= 1; |
214 | 3.36k | bbox.x1 += 1; |
215 | 3.36k | bbox.y1 += 1; |
216 | 3.36k | } |
217 | | |
218 | 4.08k | return bbox; |
219 | 4.08k | } |
220 | | |
221 | | fz_text_language fz_text_language_from_string(const char *str) |
222 | 123k | { |
223 | 123k | fz_text_language lang; |
224 | | |
225 | 123k | if (str == NULL) |
226 | 0 | return FZ_LANG_UNSET; |
227 | | |
228 | 123k | if (!strcmp(str, "zh-Hant") || |
229 | 123k | !strcmp(str, "zh-HK") || |
230 | 123k | !strcmp(str, "zh-MO") || |
231 | 123k | !strcmp(str, "zh-SG") || |
232 | 123k | !strcmp(str, "zh-TW")) |
233 | 0 | return FZ_LANG_zh_Hant; |
234 | 123k | if (!strcmp(str, "zh-Hans") || |
235 | 123k | !strcmp(str, "zh-CN")) |
236 | 0 | return FZ_LANG_zh_Hans; |
237 | | |
238 | | /* 1st char */ |
239 | 123k | if (str[0] >= 'a' && str[0] <= 'z') |
240 | 11.9k | lang = str[0] - 'a' + 1; |
241 | 111k | else if (str[0] >= 'A' && str[0] <= 'Z') |
242 | 36 | lang = str[0] - 'A' + 1; |
243 | 111k | else |
244 | 111k | return 0; |
245 | | |
246 | | /* 2nd char */ |
247 | 11.9k | if (str[1] >= 'a' && str[1] <= 'z') |
248 | 11.9k | lang += 27*(str[1] - 'a' + 1); |
249 | 38 | else if (str[1] >= 'A' && str[1] <= 'Z') |
250 | 36 | lang += 27*(str[1] - 'A' + 1); |
251 | 2 | else |
252 | 2 | return 0; /* There are no valid 1 char language codes */ |
253 | | |
254 | | /* 3nd char */ |
255 | 11.9k | if (str[2] >= 'a' && str[2] <= 'z') |
256 | 0 | lang += 27*27*(str[2] - 'a' + 1); |
257 | 11.9k | else if (str[2] >= 'A' && str[2] <= 'Z') |
258 | 0 | lang += 27*27*(str[2] - 'A' + 1); |
259 | | |
260 | | /* We don't support iso 639-6 4 char codes, cos the standard |
261 | | * has been withdrawn, and no one uses them. */ |
262 | 11.9k | return lang; |
263 | 11.9k | } |
264 | | |
265 | | char *fz_string_from_text_language(char str[8], fz_text_language lang) |
266 | 0 | { |
267 | 0 | int c; |
268 | | |
269 | | /* str is supposed to be at least 8 chars in size */ |
270 | 0 | if (str == NULL) |
271 | 0 | return NULL; |
272 | | |
273 | 0 | if (lang == FZ_LANG_zh_Hant) |
274 | 0 | fz_strlcpy(str, "zh-Hant", 8); |
275 | 0 | else if (lang == FZ_LANG_zh_Hans) |
276 | 0 | fz_strlcpy(str, "zh-Hans", 8); |
277 | 0 | else |
278 | 0 | { |
279 | 0 | c = lang % 27; |
280 | 0 | lang = lang / 27; |
281 | 0 | str[0] = c == 0 ? 0 : c - 1 + 'a'; |
282 | 0 | c = lang % 27; |
283 | 0 | lang = lang / 27; |
284 | 0 | str[1] = c == 0 ? 0 : c - 1 + 'a'; |
285 | 0 | c = lang % 27; |
286 | 0 | str[2] = c == 0 ? 0 : c - 1 + 'a'; |
287 | 0 | str[3] = 0; |
288 | 0 | } |
289 | |
|
290 | 0 | return str; |
291 | 0 | } |