30200
|
1 /*
|
|
2 * Copyright (C) 2009 Grigori Goronzy <greg@geekmind.org>
|
|
3 *
|
|
4 * This file is part of libass.
|
|
5 *
|
|
6 * Permission to use, copy, modify, and distribute this software for any
|
|
7 * purpose with or without fee is hereby granted, provided that the above
|
|
8 * copyright notice and this permission notice appear in all copies.
|
|
9 *
|
|
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
17 */
|
|
18
|
|
19 #include <ft2build.h>
|
|
20 #include FT_GLYPH_H
|
|
21 #include FT_OUTLINE_H
|
|
22 #include FT_BBOX_H
|
|
23 #include <math.h>
|
|
24
|
|
25 #include "ass_utils.h"
|
|
26 #include "ass_font.h"
|
|
27 #include "ass_drawing.h"
|
|
28
|
|
29 #define CURVE_ACCURACY 64.0
|
|
30 #define GLYPH_INITIAL_POINTS 100
|
|
31 #define GLYPH_INITIAL_CONTOURS 5
|
|
32
|
|
33 /*
|
|
34 * \brief Get and prepare a FreeType glyph
|
|
35 */
|
|
36 static void drawing_make_glyph(ASS_Drawing *drawing, void *fontconfig_priv,
|
31875
|
37 ASS_Font *font)
|
30200
|
38 {
|
|
39 FT_OutlineGlyph glyph;
|
|
40
|
|
41 // This is hacky...
|
|
42 glyph = (FT_OutlineGlyph) ass_font_get_glyph(fontconfig_priv, font,
|
31875
|
43 (uint32_t) ' ', 0, 0);
|
30200
|
44 if (glyph) {
|
|
45 FT_Outline_Done(drawing->ftlibrary, &glyph->outline);
|
|
46 FT_Outline_New(drawing->ftlibrary, GLYPH_INITIAL_POINTS,
|
|
47 GLYPH_INITIAL_CONTOURS, &glyph->outline);
|
|
48
|
|
49 glyph->outline.n_contours = 0;
|
|
50 glyph->outline.n_points = 0;
|
|
51 glyph->root.advance.x = glyph->root.advance.y = 0;
|
|
52 }
|
|
53 drawing->glyph = glyph;
|
|
54 }
|
|
55
|
|
56 /*
|
|
57 * \brief Add a single point to a contour.
|
|
58 */
|
|
59 static inline void drawing_add_point(ASS_Drawing *drawing,
|
|
60 FT_Vector *point)
|
|
61 {
|
|
62 FT_Outline *ol = &drawing->glyph->outline;
|
|
63
|
|
64 if (ol->n_points >= drawing->max_points) {
|
|
65 drawing->max_points *= 2;
|
|
66 ol->points = realloc(ol->points, sizeof(FT_Vector) *
|
|
67 drawing->max_points);
|
|
68 ol->tags = realloc(ol->tags, drawing->max_points);
|
|
69 }
|
|
70
|
|
71 ol->points[ol->n_points].x = point->x;
|
|
72 ol->points[ol->n_points].y = point->y;
|
|
73 ol->tags[ol->n_points] = 1;
|
|
74 ol->n_points++;
|
|
75 }
|
|
76
|
|
77 /*
|
|
78 * \brief Close a contour and check glyph size overflow.
|
|
79 */
|
|
80 static inline void drawing_close_shape(ASS_Drawing *drawing)
|
|
81 {
|
|
82 FT_Outline *ol = &drawing->glyph->outline;
|
|
83
|
|
84 if (ol->n_contours >= drawing->max_contours) {
|
|
85 drawing->max_contours *= 2;
|
|
86 ol->contours = realloc(ol->contours, sizeof(short) *
|
|
87 drawing->max_contours);
|
|
88 }
|
|
89
|
|
90 if (ol->n_points) {
|
|
91 ol->contours[ol->n_contours] = ol->n_points - 1;
|
|
92 ol->n_contours++;
|
|
93 }
|
|
94 }
|
|
95
|
|
96 /*
|
|
97 * \brief Prepare drawing for parsing. This just sets a few parameters.
|
|
98 */
|
|
99 static void drawing_prepare(ASS_Drawing *drawing)
|
|
100 {
|
|
101 // Scaling parameters
|
|
102 drawing->point_scale_x = drawing->scale_x *
|
|
103 64.0 / (1 << (drawing->scale - 1));
|
|
104 drawing->point_scale_y = drawing->scale_y *
|
|
105 64.0 / (1 << (drawing->scale - 1));
|
|
106 }
|
|
107
|
|
108 /*
|
|
109 * \brief Finish a drawing. This only sets the horizontal advance according
|
|
110 * to the glyph's bbox at the moment.
|
|
111 */
|
|
112 static void drawing_finish(ASS_Drawing *drawing, int raw_mode)
|
|
113 {
|
|
114 int i, offset;
|
31853
|
115 FT_BBox bbox = drawing->cbox;
|
30200
|
116 FT_Outline *ol = &drawing->glyph->outline;
|
|
117
|
|
118 // Close the last contour
|
|
119 drawing_close_shape(drawing);
|
|
120
|
|
121 ass_msg(drawing->library, MSGL_V,
|
|
122 "Parsed drawing with %d points and %d contours", ol->n_points,
|
|
123 ol->n_contours);
|
|
124
|
|
125 if (raw_mode)
|
|
126 return;
|
|
127
|
|
128 drawing->glyph->root.advance.x = d6_to_d16(bbox.xMax - bbox.xMin);
|
|
129
|
|
130 drawing->desc = double_to_d6(-drawing->pbo * drawing->scale_y);
|
|
131 drawing->asc = bbox.yMax - bbox.yMin + drawing->desc;
|
|
132
|
|
133 // Place it onto the baseline
|
|
134 offset = (bbox.yMax - bbox.yMin) + double_to_d6(-drawing->pbo *
|
|
135 drawing->scale_y);
|
|
136 for (i = 0; i < ol->n_points; i++)
|
|
137 ol->points[i].y += offset;
|
|
138 }
|
|
139
|
|
140 /*
|
|
141 * \brief Check whether a number of items on the list is available
|
|
142 */
|
|
143 static int token_check_values(ASS_DrawingToken *token, int i, int type)
|
|
144 {
|
|
145 int j;
|
|
146 for (j = 0; j < i; j++) {
|
|
147 if (!token || token->type != type) return 0;
|
|
148 token = token->next;
|
|
149 }
|
|
150
|
|
151 return 1;
|
|
152 }
|
|
153
|
|
154 /*
|
|
155 * \brief Tokenize a drawing string into a list of ASS_DrawingToken
|
|
156 * This also expands points for closing b-splines
|
|
157 */
|
|
158 static ASS_DrawingToken *drawing_tokenize(char *str)
|
|
159 {
|
|
160 char *p = str;
|
|
161 int i, val, type = -1, is_set = 0;
|
|
162 FT_Vector point = {0, 0};
|
|
163
|
|
164 ASS_DrawingToken *root = NULL, *tail = NULL, *spline_start = NULL;
|
|
165
|
|
166 while (*p) {
|
|
167 if (*p == 'c' && spline_start) {
|
|
168 // Close b-splines: add the first three points of the b-spline
|
|
169 // back to the end
|
|
170 if (token_check_values(spline_start->next, 2, TOKEN_B_SPLINE)) {
|
|
171 for (i = 0; i < 3; i++) {
|
|
172 tail->next = calloc(1, sizeof(ASS_DrawingToken));
|
|
173 tail->next->prev = tail;
|
|
174 tail = tail->next;
|
|
175 tail->type = TOKEN_B_SPLINE;
|
|
176 tail->point = spline_start->point;
|
|
177 spline_start = spline_start->next;
|
|
178 }
|
|
179 spline_start = NULL;
|
|
180 }
|
|
181 } else if (!is_set && mystrtoi(&p, &val)) {
|
|
182 point.x = val;
|
|
183 is_set = 1;
|
|
184 p--;
|
|
185 } else if (is_set == 1 && mystrtoi(&p, &val)) {
|
|
186 point.y = val;
|
|
187 is_set = 2;
|
|
188 p--;
|
|
189 } else if (*p == 'm')
|
|
190 type = TOKEN_MOVE;
|
|
191 else if (*p == 'n')
|
|
192 type = TOKEN_MOVE_NC;
|
|
193 else if (*p == 'l')
|
|
194 type = TOKEN_LINE;
|
|
195 else if (*p == 'b')
|
|
196 type = TOKEN_CUBIC_BEZIER;
|
|
197 else if (*p == 'q')
|
|
198 type = TOKEN_CONIC_BEZIER;
|
|
199 else if (*p == 's')
|
|
200 type = TOKEN_B_SPLINE;
|
|
201 // We're simply ignoring TOKEN_EXTEND_B_SPLINE here.
|
|
202 // This is not harmful at all, since it can be ommitted with
|
|
203 // similar result (the spline is extended anyway).
|
|
204
|
|
205 if (type != -1 && is_set == 2) {
|
|
206 if (root) {
|
|
207 tail->next = calloc(1, sizeof(ASS_DrawingToken));
|
|
208 tail->next->prev = tail;
|
|
209 tail = tail->next;
|
|
210 } else
|
|
211 root = tail = calloc(1, sizeof(ASS_DrawingToken));
|
|
212 tail->type = type;
|
|
213 tail->point = point;
|
|
214 is_set = 0;
|
|
215 if (type == TOKEN_B_SPLINE && !spline_start)
|
|
216 spline_start = tail->prev;
|
|
217 }
|
|
218 p++;
|
|
219 }
|
|
220
|
|
221 return root;
|
|
222 }
|
|
223
|
|
224 /*
|
|
225 * \brief Free a list of tokens
|
|
226 */
|
|
227 static void drawing_free_tokens(ASS_DrawingToken *token)
|
|
228 {
|
|
229 while (token) {
|
|
230 ASS_DrawingToken *at = token;
|
|
231 token = token->next;
|
|
232 free(at);
|
|
233 }
|
|
234 }
|
|
235
|
|
236 /*
|
31853
|
237 * \brief Update drawing cbox
|
|
238 */
|
|
239 static inline void update_cbox(ASS_Drawing *drawing, FT_Vector *point)
|
|
240 {
|
|
241 FT_BBox *box = &drawing->cbox;
|
|
242
|
|
243 box->xMin = FFMIN(box->xMin, point->x);
|
|
244 box->xMax = FFMAX(box->xMax, point->x);
|
|
245 box->yMin = FFMIN(box->yMin, point->y);
|
|
246 box->yMax = FFMAX(box->yMax, point->y);
|
|
247 }
|
|
248
|
|
249 /*
|
30200
|
250 * \brief Translate and scale a point coordinate according to baseline
|
|
251 * offset and scale.
|
|
252 */
|
|
253 static inline void translate_point(ASS_Drawing *drawing, FT_Vector *point)
|
|
254 {
|
|
255 point->x = drawing->point_scale_x * point->x;
|
|
256 point->y = drawing->point_scale_y * -point->y;
|
31853
|
257
|
|
258 update_cbox(drawing, point);
|
30200
|
259 }
|
|
260
|
|
261 /*
|
|
262 * \brief Evaluate a curve into lines
|
|
263 * This curve evaluator is also used in VSFilter (RTS.cpp); it's a simple
|
|
264 * implementation of the De Casteljau algorithm.
|
|
265 */
|
|
266 static void drawing_evaluate_curve(ASS_Drawing *drawing,
|
|
267 ASS_DrawingToken *token, char spline,
|
|
268 int started)
|
|
269 {
|
|
270 double cx3, cx2, cx1, cx0, cy3, cy2, cy1, cy0;
|
|
271 double t, h, max_accel, max_accel1, max_accel2;
|
|
272 FT_Vector cur = {0, 0};
|
|
273
|
|
274 cur = token->point;
|
|
275 translate_point(drawing, &cur);
|
|
276 int x0 = cur.x;
|
|
277 int y0 = cur.y;
|
|
278 token = token->next;
|
|
279 cur = token->point;
|
|
280 translate_point(drawing, &cur);
|
|
281 int x1 = cur.x;
|
|
282 int y1 = cur.y;
|
|
283 token = token->next;
|
|
284 cur = token->point;
|
|
285 translate_point(drawing, &cur);
|
|
286 int x2 = cur.x;
|
|
287 int y2 = cur.y;
|
|
288 token = token->next;
|
|
289 cur = token->point;
|
|
290 translate_point(drawing, &cur);
|
|
291 int x3 = cur.x;
|
|
292 int y3 = cur.y;
|
|
293
|
|
294 if (spline) {
|
|
295 // 1 [-1 +3 -3 +1]
|
|
296 // - * [+3 -6 +3 0]
|
|
297 // 6 [-3 0 +3 0]
|
|
298 // [+1 +4 +1 0]
|
|
299
|
|
300 double div6 = 1.0/6.0;
|
|
301
|
|
302 cx3 = div6*(- x0+3*x1-3*x2+x3);
|
|
303 cx2 = div6*( 3*x0-6*x1+3*x2);
|
|
304 cx1 = div6*(-3*x0 +3*x2);
|
|
305 cx0 = div6*( x0+4*x1+1*x2);
|
|
306
|
|
307 cy3 = div6*(- y0+3*y1-3*y2+y3);
|
|
308 cy2 = div6*( 3*y0-6*y1+3*y2);
|
|
309 cy1 = div6*(-3*y0 +3*y2);
|
|
310 cy0 = div6*( y0+4*y1+1*y2);
|
|
311 } else {
|
|
312 // [-1 +3 -3 +1]
|
|
313 // [+3 -6 +3 0]
|
|
314 // [-3 +3 0 0]
|
|
315 // [+1 0 0 0]
|
|
316
|
|
317 cx3 = - x0+3*x1-3*x2+x3;
|
|
318 cx2 = 3*x0-6*x1+3*x2;
|
|
319 cx1 = -3*x0+3*x1;
|
|
320 cx0 = x0;
|
|
321
|
|
322 cy3 = - y0+3*y1-3*y2+y3;
|
|
323 cy2 = 3*y0-6*y1+3*y2;
|
|
324 cy1 = -3*y0+3*y1;
|
|
325 cy0 = y0;
|
|
326 }
|
|
327
|
|
328 max_accel1 = fabs(2 * cy2) + fabs(6 * cy3);
|
|
329 max_accel2 = fabs(2 * cx2) + fabs(6 * cx3);
|
|
330
|
|
331 max_accel = FFMAX(max_accel1, max_accel2);
|
|
332 h = 1.0;
|
|
333
|
|
334 if (max_accel > CURVE_ACCURACY)
|
|
335 h = sqrt(CURVE_ACCURACY / max_accel);
|
|
336
|
|
337 if (!started) {
|
|
338 cur.x = cx0;
|
|
339 cur.y = cy0;
|
|
340 drawing_add_point(drawing, &cur);
|
|
341 }
|
|
342
|
|
343 for (t = 0; t < 1.0; t += h) {
|
|
344 cur.x = cx0 + t * (cx1 + t * (cx2 + t * cx3));
|
|
345 cur.y = cy0 + t * (cy1 + t * (cy2 + t * cy3));
|
|
346 drawing_add_point(drawing, &cur);
|
|
347 }
|
|
348
|
|
349 cur.x = cx0 + cx1 + cx2 + cx3;
|
|
350 cur.y = cy0 + cy1 + cy2 + cy3;
|
|
351 drawing_add_point(drawing, &cur);
|
|
352 }
|
|
353
|
|
354 /*
|
|
355 * \brief Create and initialize a new drawing and return it
|
|
356 */
|
|
357 ASS_Drawing *ass_drawing_new(void *fontconfig_priv, ASS_Font *font,
|
31875
|
358 FT_Library lib)
|
30200
|
359 {
|
|
360 ASS_Drawing *drawing;
|
|
361
|
|
362 drawing = calloc(1, sizeof(*drawing));
|
|
363 drawing->text = calloc(1, DRAWING_INITIAL_SIZE);
|
|
364 drawing->size = DRAWING_INITIAL_SIZE;
|
31853
|
365 drawing->cbox.xMin = drawing->cbox.yMin = INT_MAX;
|
|
366 drawing->cbox.xMax = drawing->cbox.yMax = INT_MIN;
|
31875
|
367 drawing->fontconfig_priv = fontconfig_priv;
|
|
368 drawing->font = font;
|
30200
|
369 drawing->ftlibrary = lib;
|
32556
|
370 drawing->library = font ? font->library : NULL;
|
30200
|
371
|
|
372 drawing->scale_x = 1.;
|
|
373 drawing->scale_y = 1.;
|
|
374 drawing->max_contours = GLYPH_INITIAL_CONTOURS;
|
|
375 drawing->max_points = GLYPH_INITIAL_POINTS;
|
|
376
|
|
377 return drawing;
|
|
378 }
|
|
379
|
|
380 /*
|
|
381 * \brief Free a drawing
|
|
382 */
|
|
383 void ass_drawing_free(ASS_Drawing* drawing)
|
|
384 {
|
|
385 if (drawing) {
|
|
386 free(drawing->text);
|
|
387 }
|
|
388 free(drawing);
|
|
389 }
|
|
390
|
|
391 /*
|
|
392 * \brief Add one ASCII character to the drawing text buffer
|
|
393 */
|
|
394 void ass_drawing_add_char(ASS_Drawing* drawing, char symbol)
|
|
395 {
|
|
396 drawing->text[drawing->i++] = symbol;
|
|
397 drawing->text[drawing->i] = 0;
|
|
398
|
|
399 if (drawing->i + 1 >= drawing->size) {
|
|
400 drawing->size *= 2;
|
|
401 drawing->text = realloc(drawing->text, drawing->size);
|
|
402 }
|
|
403 }
|
|
404
|
|
405 /*
|
|
406 * \brief Create a hashcode for the drawing
|
|
407 * XXX: To avoid collisions a better hash algorithm might be useful.
|
|
408 */
|
|
409 void ass_drawing_hash(ASS_Drawing* drawing)
|
|
410 {
|
|
411 drawing->hash = fnv_32a_str(drawing->text, FNV1_32A_INIT);
|
|
412 }
|
|
413
|
|
414 /*
|
|
415 * \brief Convert token list to outline. Calls the line and curve evaluators.
|
|
416 */
|
|
417 FT_OutlineGlyph *ass_drawing_parse(ASS_Drawing *drawing, int raw_mode)
|
|
418 {
|
|
419 int started = 0;
|
|
420 ASS_DrawingToken *token;
|
|
421 FT_Vector pen = {0, 0};
|
|
422
|
31875
|
423 if (drawing->font)
|
|
424 drawing_make_glyph(drawing, drawing->fontconfig_priv, drawing->font);
|
30200
|
425 if (!drawing->glyph)
|
|
426 return NULL;
|
|
427
|
|
428 drawing->tokens = drawing_tokenize(drawing->text);
|
|
429 drawing_prepare(drawing);
|
|
430
|
|
431 token = drawing->tokens;
|
|
432 while (token) {
|
|
433 // Draw something according to current command
|
|
434 switch (token->type) {
|
|
435 case TOKEN_MOVE_NC:
|
|
436 pen = token->point;
|
|
437 translate_point(drawing, &pen);
|
|
438 token = token->next;
|
|
439 break;
|
|
440 case TOKEN_MOVE:
|
|
441 pen = token->point;
|
|
442 translate_point(drawing, &pen);
|
|
443 if (started) {
|
|
444 drawing_close_shape(drawing);
|
|
445 started = 0;
|
|
446 }
|
|
447 token = token->next;
|
|
448 break;
|
|
449 case TOKEN_LINE: {
|
|
450 FT_Vector to;
|
|
451 to = token->point;
|
|
452 translate_point(drawing, &to);
|
|
453 if (!started) drawing_add_point(drawing, &pen);
|
|
454 drawing_add_point(drawing, &to);
|
|
455 started = 1;
|
|
456 token = token->next;
|
|
457 break;
|
|
458 }
|
|
459 case TOKEN_CUBIC_BEZIER:
|
|
460 if (token_check_values(token, 3, TOKEN_CUBIC_BEZIER) &&
|
|
461 token->prev) {
|
|
462 drawing_evaluate_curve(drawing, token->prev, 0, started);
|
|
463 token = token->next;
|
|
464 token = token->next;
|
|
465 token = token->next;
|
|
466 started = 1;
|
|
467 } else
|
|
468 token = token->next;
|
|
469 break;
|
|
470 case TOKEN_B_SPLINE:
|
|
471 if (token_check_values(token, 3, TOKEN_B_SPLINE) &&
|
|
472 token->prev) {
|
|
473 drawing_evaluate_curve(drawing, token->prev, 1, started);
|
|
474 token = token->next;
|
|
475 started = 1;
|
|
476 } else
|
|
477 token = token->next;
|
|
478 break;
|
|
479 default:
|
|
480 token = token->next;
|
|
481 break;
|
|
482 }
|
|
483 }
|
|
484
|
|
485 drawing_finish(drawing, raw_mode);
|
|
486 drawing_free_tokens(drawing->tokens);
|
|
487 return &drawing->glyph;
|
|
488 }
|