/src/mupdf/source/fitz/text.c
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (C) 2004-2024 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 | 85.2k | { |
30 | 85.2k | fz_text *text = fz_malloc_struct(ctx, fz_text); |
31 | 85.2k | text->refs = 1; |
32 | 85.2k | return text; |
33 | 85.2k | } |
34 | | |
35 | | fz_text * |
36 | | fz_keep_text(fz_context *ctx, const fz_text *textc) |
37 | 316 | { |
38 | 316 | fz_text *text = (fz_text *)textc; /* Explicit cast away of const */ |
39 | | |
40 | 316 | return fz_keep_imp(ctx, text, &text->refs); |
41 | 316 | } |
42 | | |
43 | | void |
44 | | fz_drop_text(fz_context *ctx, const fz_text *textc) |
45 | 102k | { |
46 | 102k | fz_text *text = (fz_text *)textc; /* Explicit cast away of const */ |
47 | | |
48 | 102k | if (fz_drop_imp(ctx, text, &text->refs)) |
49 | 85.2k | { |
50 | 85.2k | fz_text_span *span = text->head; |
51 | 208k | while (span) |
52 | 123k | { |
53 | 123k | fz_text_span *next = span->next; |
54 | 123k | fz_drop_font(ctx, span->font); |
55 | 123k | fz_free(ctx, span->items); |
56 | 123k | fz_free(ctx, span); |
57 | 123k | span = next; |
58 | 123k | } |
59 | 85.2k | fz_free(ctx, text); |
60 | 85.2k | } |
61 | 102k | } |
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 | 123k | { |
66 | 123k | fz_text_span *span = fz_malloc_struct(ctx, fz_text_span); |
67 | 123k | span->font = fz_keep_font(ctx, font); |
68 | 123k | span->wmode = wmode; |
69 | 123k | span->bidi_level = bidi_level; |
70 | 123k | span->markup_dir = markup_dir; |
71 | 123k | span->language = language; |
72 | 123k | span->trm = trm; |
73 | 123k | span->trm.e = 0; |
74 | 123k | span->trm.f = 0; |
75 | 123k | return span; |
76 | 123k | } |
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 | 3.35M | { |
81 | 3.35M | if (!text->tail) |
82 | 85.0k | { |
83 | 85.0k | text->head = text->tail = fz_new_text_span(ctx, font, wmode, bidi_level, markup_dir, language, trm); |
84 | 85.0k | } |
85 | 3.27M | else if (text->tail->font != font || |
86 | 3.27M | text->tail->wmode != wmode || |
87 | 3.27M | text->tail->bidi_level != bidi_level || |
88 | 3.27M | text->tail->markup_dir != markup_dir || |
89 | 3.27M | text->tail->language != language || |
90 | 3.27M | text->tail->trm.a != trm.a || |
91 | 3.27M | text->tail->trm.b != trm.b || |
92 | 3.27M | text->tail->trm.c != trm.c || |
93 | 3.27M | text->tail->trm.d != trm.d) |
94 | 38.4k | { |
95 | 38.4k | text->tail = text->tail->next = fz_new_text_span(ctx, font, wmode, bidi_level, markup_dir, language, trm); |
96 | 38.4k | } |
97 | 3.35M | return text->tail; |
98 | 3.35M | } |
99 | | |
100 | | static void |
101 | | fz_grow_text_span(fz_context *ctx, fz_text_span *span, int n) |
102 | 3.35M | { |
103 | 3.35M | int new_cap = span->cap; |
104 | 3.35M | if (span->len + n < new_cap) |
105 | 3.09M | return; |
106 | 460k | while (span->len + n > new_cap) |
107 | 194k | new_cap = new_cap + 36; |
108 | 266k | span->items = fz_realloc_array(ctx, span->items, new_cap, fz_text_item); |
109 | 266k | span->cap = new_cap; |
110 | 266k | } |
111 | | |
112 | | void |
113 | | fz_show_glyph_aux(fz_context *ctx, fz_text *text, fz_font *font, fz_matrix trm, int gid, int ucs, int cid, int wmode, int bidi_level, fz_bidi_direction markup_dir, fz_text_language lang) |
114 | 3.35M | { |
115 | 3.35M | fz_text_span *span; |
116 | | |
117 | 3.35M | if (text->refs != 1) |
118 | 0 | fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot modify shared text objects"); |
119 | | |
120 | 3.35M | span = fz_add_text_span(ctx, text, font, wmode, bidi_level, markup_dir, lang, trm); |
121 | | |
122 | 3.35M | fz_grow_text_span(ctx, span, 1); |
123 | | |
124 | 3.35M | span->items[span->len].ucs = ucs; |
125 | 3.35M | span->items[span->len].gid = gid; |
126 | 3.35M | span->items[span->len].cid = cid; |
127 | 3.35M | span->items[span->len].x = trm.e; |
128 | 3.35M | span->items[span->len].y = trm.f; |
129 | 3.35M | span->len++; |
130 | 3.35M | } |
131 | | |
132 | | void |
133 | | 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) |
134 | 47.8k | { |
135 | 47.8k | fz_show_glyph_aux(ctx, text, font, trm, gid, ucs, ucs, wmode, bidi_level, markup_dir, lang); |
136 | 47.8k | } |
137 | | |
138 | | fz_matrix |
139 | | fz_show_string(fz_context *ctx, fz_text *text, fz_font *user_font, fz_matrix trm, const char *s, |
140 | | int wmode, int bidi_level, fz_bidi_direction markup_dir, fz_text_language language) |
141 | 146 | { |
142 | 146 | fz_font *font; |
143 | 146 | int gid, ucs; |
144 | 146 | float adv; |
145 | | |
146 | 35.5k | while (*s) |
147 | 35.3k | { |
148 | 35.3k | s += fz_chartorune(&ucs, s); |
149 | 35.3k | gid = fz_encode_character_with_fallback(ctx, user_font, ucs, 0, language, &font); |
150 | 35.3k | fz_show_glyph(ctx, text, font, trm, gid, ucs, wmode, bidi_level, markup_dir, language); |
151 | 35.3k | adv = fz_advance_glyph(ctx, font, gid, wmode); |
152 | 35.3k | if (wmode == 0) |
153 | 35.3k | trm = fz_pre_translate(trm, adv, 0); |
154 | 0 | else |
155 | 0 | trm = fz_pre_translate(trm, 0, -adv); |
156 | 35.3k | } |
157 | | |
158 | 146 | return trm; |
159 | 146 | } |
160 | | |
161 | | fz_matrix |
162 | | fz_measure_string(fz_context *ctx, fz_font *user_font, fz_matrix trm, const char *s, |
163 | | int wmode, int bidi_level, fz_bidi_direction markup_dir, fz_text_language language) |
164 | 0 | { |
165 | 0 | fz_font *font; |
166 | 0 | int gid, ucs; |
167 | 0 | float adv; |
168 | |
|
169 | 0 | while (*s) |
170 | 0 | { |
171 | 0 | s += fz_chartorune(&ucs, s); |
172 | 0 | gid = fz_encode_character_with_fallback(ctx, user_font, ucs, 0, language, &font); |
173 | 0 | adv = fz_advance_glyph(ctx, font, gid, wmode); |
174 | 0 | if (wmode == 0) |
175 | 0 | trm = fz_pre_translate(trm, adv, 0); |
176 | 0 | else |
177 | 0 | trm = fz_pre_translate(trm, 0, -adv); |
178 | 0 | } |
179 | |
|
180 | 0 | return trm; |
181 | 0 | } |
182 | | |
183 | | fz_rect |
184 | | fz_bound_text(fz_context *ctx, const fz_text *text, const fz_stroke_state *stroke, fz_matrix ctm) |
185 | 1.50k | { |
186 | 1.50k | fz_text_span *span; |
187 | 1.50k | fz_matrix tm, trm; |
188 | 1.50k | fz_rect gbox; |
189 | 1.50k | fz_rect bbox; |
190 | 1.50k | int i; |
191 | | |
192 | 1.50k | bbox = fz_empty_rect; |
193 | | |
194 | 3.08k | for (span = text->head; span; span = span->next) |
195 | 1.57k | { |
196 | 1.57k | if (span->len > 0) |
197 | 1.57k | { |
198 | 1.57k | tm = span->trm; |
199 | 150k | for (i = 0; i < span->len; i++) |
200 | 149k | { |
201 | 149k | if (span->items[i].gid >= 0) |
202 | 149k | { |
203 | 149k | tm.e = span->items[i].x; |
204 | 149k | tm.f = span->items[i].y; |
205 | 149k | trm = fz_concat(tm, ctm); |
206 | 149k | gbox = fz_bound_glyph(ctx, span->font, span->items[i].gid, trm); |
207 | 149k | bbox = fz_union_rect(bbox, gbox); |
208 | 149k | } |
209 | 149k | } |
210 | 1.57k | } |
211 | 1.57k | } |
212 | | |
213 | 1.50k | if (!fz_is_empty_rect(bbox)) |
214 | 1.11k | { |
215 | 1.11k | if (stroke) |
216 | 38 | bbox = fz_adjust_rect_for_stroke(ctx, bbox, stroke, ctm); |
217 | | |
218 | | /* Compensate for the glyph cache limited positioning precision */ |
219 | 1.11k | bbox.x0 -= 1; |
220 | 1.11k | bbox.y0 -= 1; |
221 | 1.11k | bbox.x1 += 1; |
222 | 1.11k | bbox.y1 += 1; |
223 | 1.11k | } |
224 | | |
225 | 1.50k | return bbox; |
226 | 1.50k | } |
227 | | |
228 | | fz_text_language fz_text_language_from_string(const char *str) |
229 | 57.2k | { |
230 | 57.2k | fz_text_language lang; |
231 | | |
232 | 57.2k | if (str == NULL) |
233 | 0 | return FZ_LANG_UNSET; |
234 | | |
235 | 57.2k | if (!strcmp(str, "zh-Hant") || |
236 | 57.2k | !strcmp(str, "zh-HK") || |
237 | 57.2k | !strcmp(str, "zh-MO") || |
238 | 57.2k | !strcmp(str, "zh-SG") || |
239 | 57.2k | !strcmp(str, "zh-TW")) |
240 | 0 | return FZ_LANG_zh_Hant; |
241 | 57.2k | if (!strcmp(str, "zh-Hans") || |
242 | 57.2k | !strcmp(str, "zh-CN")) |
243 | 0 | return FZ_LANG_zh_Hans; |
244 | | |
245 | | /* 1st char */ |
246 | 57.2k | if (str[0] >= 'a' && str[0] <= 'z') |
247 | 3.71k | lang = str[0] - 'a' + 1; |
248 | 53.5k | else if (str[0] >= 'A' && str[0] <= 'Z') |
249 | 69 | lang = str[0] - 'A' + 1; |
250 | 53.4k | else |
251 | 53.4k | return 0; |
252 | | |
253 | | /* 2nd char */ |
254 | 3.78k | if (str[1] >= 'a' && str[1] <= 'z') |
255 | 3.71k | lang += 27*(str[1] - 'a' + 1); |
256 | 69 | else if (str[1] >= 'A' && str[1] <= 'Z') |
257 | 69 | lang += 27*(str[1] - 'A' + 1); |
258 | 0 | else |
259 | 0 | return 0; /* There are no valid 1 char language codes */ |
260 | | |
261 | | /* 3nd char */ |
262 | 3.78k | if (str[2] >= 'a' && str[2] <= 'z') |
263 | 0 | lang += 27*27*(str[2] - 'a' + 1); |
264 | 3.78k | else if (str[2] >= 'A' && str[2] <= 'Z') |
265 | 0 | lang += 27*27*(str[2] - 'A' + 1); |
266 | | |
267 | | /* We don't support iso 639-6 4 char codes, cos the standard |
268 | | * has been withdrawn, and no one uses them. */ |
269 | 3.78k | return lang; |
270 | 3.78k | } |
271 | | |
272 | | char *fz_string_from_text_language(char str[8], fz_text_language lang) |
273 | 0 | { |
274 | 0 | int c; |
275 | | |
276 | | /* str is supposed to be at least 8 chars in size */ |
277 | 0 | if (str == NULL) |
278 | 0 | return NULL; |
279 | | |
280 | 0 | if (lang == FZ_LANG_zh_Hant) |
281 | 0 | fz_strlcpy(str, "zh-Hant", 8); |
282 | 0 | else if (lang == FZ_LANG_zh_Hans) |
283 | 0 | fz_strlcpy(str, "zh-Hans", 8); |
284 | 0 | else |
285 | 0 | { |
286 | 0 | c = lang % 27; |
287 | 0 | lang = lang / 27; |
288 | 0 | str[0] = c == 0 ? 0 : c - 1 + 'a'; |
289 | 0 | c = lang % 27; |
290 | 0 | lang = lang / 27; |
291 | 0 | str[1] = c == 0 ? 0 : c - 1 + 'a'; |
292 | 0 | c = lang % 27; |
293 | 0 | str[2] = c == 0 ? 0 : c - 1 + 'a'; |
294 | 0 | str[3] = 0; |
295 | 0 | } |
296 | |
|
297 | 0 | return str; |
298 | 0 | } |