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,
|
|
37 ASS_Font *font, ASS_Hinting hint)
|
|
38 {
|
|
39 FT_OutlineGlyph glyph;
|
|
40
|
|
41 // This is hacky...
|
|
42 glyph = (FT_OutlineGlyph) ass_font_get_glyph(fontconfig_priv, font,
|
|
43 (uint32_t) ' ', hint, 0);
|
|
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;
|
|
115 FT_BBox bbox;
|
|
116 FT_Outline *ol = &drawing->glyph->outline;
|
|
117
|
|
118 // Close the last contour
|
|
119 drawing_close_shape(drawing);
|
|
120
|
|
121 #if 0
|
|
122 // Dump points
|
|
123 for (i = 0; i < ol->n_points; i++) {
|
|
124 printf("point (%d, %d)\n", (int) ol->points[i].x,
|
|
125 (int) ol->points[i].y);
|
|
126 }
|
|
127
|
|
128 // Dump contours
|
|
129 for (i = 0; i < ol->n_contours; i++)
|
|
130 printf("contour %d\n", ol->contours[i]);
|
|
131 #endif
|
|
132
|
|
133 ass_msg(drawing->library, MSGL_V,
|
|
134 "Parsed drawing with %d points and %d contours", ol->n_points,
|
|
135 ol->n_contours);
|
|
136
|
|
137 if (raw_mode)
|
|
138 return;
|
|
139
|
|
140 FT_Outline_Get_CBox(&drawing->glyph->outline, &bbox);
|
|
141 drawing->glyph->root.advance.x = d6_to_d16(bbox.xMax - bbox.xMin);
|
|
142
|
|
143 drawing->desc = double_to_d6(-drawing->pbo * drawing->scale_y);
|
|
144 drawing->asc = bbox.yMax - bbox.yMin + drawing->desc;
|
|
145
|
|
146 // Place it onto the baseline
|
|
147 offset = (bbox.yMax - bbox.yMin) + double_to_d6(-drawing->pbo *
|
|
148 drawing->scale_y);
|
|
149 for (i = 0; i < ol->n_points; i++)
|
|
150 ol->points[i].y += offset;
|
|
151 }
|
|
152
|
|
153 /*
|
|
154 * \brief Check whether a number of items on the list is available
|
|
155 */
|
|
156 static int token_check_values(ASS_DrawingToken *token, int i, int type)
|
|
157 {
|
|
158 int j;
|
|
159 for (j = 0; j < i; j++) {
|
|
160 if (!token || token->type != type) return 0;
|
|
161 token = token->next;
|
|
162 }
|
|
163
|
|
164 return 1;
|
|
165 }
|
|
166
|
|
167 /*
|
|
168 * \brief Tokenize a drawing string into a list of ASS_DrawingToken
|
|
169 * This also expands points for closing b-splines
|
|
170 */
|
|
171 static ASS_DrawingToken *drawing_tokenize(char *str)
|
|
172 {
|
|
173 char *p = str;
|
|
174 int i, val, type = -1, is_set = 0;
|
|
175 FT_Vector point = {0, 0};
|
|
176
|
|
177 ASS_DrawingToken *root = NULL, *tail = NULL, *spline_start = NULL;
|
|
178
|
|
179 while (*p) {
|
|
180 if (*p == 'c' && spline_start) {
|
|
181 // Close b-splines: add the first three points of the b-spline
|
|
182 // back to the end
|
|
183 if (token_check_values(spline_start->next, 2, TOKEN_B_SPLINE)) {
|
|
184 for (i = 0; i < 3; i++) {
|
|
185 tail->next = calloc(1, sizeof(ASS_DrawingToken));
|
|
186 tail->next->prev = tail;
|
|
187 tail = tail->next;
|
|
188 tail->type = TOKEN_B_SPLINE;
|
|
189 tail->point = spline_start->point;
|
|
190 spline_start = spline_start->next;
|
|
191 }
|
|
192 spline_start = NULL;
|
|
193 }
|
|
194 } else if (!is_set && mystrtoi(&p, &val)) {
|
|
195 point.x = val;
|
|
196 is_set = 1;
|
|
197 p--;
|
|
198 } else if (is_set == 1 && mystrtoi(&p, &val)) {
|
|
199 point.y = val;
|
|
200 is_set = 2;
|
|
201 p--;
|
|
202 } else if (*p == 'm')
|
|
203 type = TOKEN_MOVE;
|
|
204 else if (*p == 'n')
|
|
205 type = TOKEN_MOVE_NC;
|
|
206 else if (*p == 'l')
|
|
207 type = TOKEN_LINE;
|
|
208 else if (*p == 'b')
|
|
209 type = TOKEN_CUBIC_BEZIER;
|
|
210 else if (*p == 'q')
|
|
211 type = TOKEN_CONIC_BEZIER;
|
|
212 else if (*p == 's')
|
|
213 type = TOKEN_B_SPLINE;
|
|
214 // We're simply ignoring TOKEN_EXTEND_B_SPLINE here.
|
|
215 // This is not harmful at all, since it can be ommitted with
|
|
216 // similar result (the spline is extended anyway).
|
|
217
|
|
218 if (type != -1 && is_set == 2) {
|
|
219 if (root) {
|
|
220 tail->next = calloc(1, sizeof(ASS_DrawingToken));
|
|
221 tail->next->prev = tail;
|
|
222 tail = tail->next;
|
|
223 } else
|
|
224 root = tail = calloc(1, sizeof(ASS_DrawingToken));
|
|
225 tail->type = type;
|
|
226 tail->point = point;
|
|
227 is_set = 0;
|
|
228 if (type == TOKEN_B_SPLINE && !spline_start)
|
|
229 spline_start = tail->prev;
|
|
230 }
|
|
231 p++;
|
|
232 }
|
|
233
|
|
234 #if 0
|
|
235 // Check tokens
|
|
236 ASS_DrawingToken *t = root;
|
|
237 while(t) {
|
|
238 printf("token %d point (%d, %d)\n", t->type, t->point.x, t->point.y);
|
|
239 t = t->next;
|
|
240 }
|
|
241 #endif
|
|
242
|
|
243 return root;
|
|
244 }
|
|
245
|
|
246 /*
|
|
247 * \brief Free a list of tokens
|
|
248 */
|
|
249 static void drawing_free_tokens(ASS_DrawingToken *token)
|
|
250 {
|
|
251 while (token) {
|
|
252 ASS_DrawingToken *at = token;
|
|
253 token = token->next;
|
|
254 free(at);
|
|
255 }
|
|
256 }
|
|
257
|
|
258 /*
|
|
259 * \brief Translate and scale a point coordinate according to baseline
|
|
260 * offset and scale.
|
|
261 */
|
|
262 static inline void translate_point(ASS_Drawing *drawing, FT_Vector *point)
|
|
263 {
|
|
264 point->x = drawing->point_scale_x * point->x;
|
|
265 point->y = drawing->point_scale_y * -point->y;
|
|
266 }
|
|
267
|
|
268 /*
|
|
269 * \brief Evaluate a curve into lines
|
|
270 * This curve evaluator is also used in VSFilter (RTS.cpp); it's a simple
|
|
271 * implementation of the De Casteljau algorithm.
|
|
272 */
|
|
273 static void drawing_evaluate_curve(ASS_Drawing *drawing,
|
|
274 ASS_DrawingToken *token, char spline,
|
|
275 int started)
|
|
276 {
|
|
277 double cx3, cx2, cx1, cx0, cy3, cy2, cy1, cy0;
|
|
278 double t, h, max_accel, max_accel1, max_accel2;
|
|
279 FT_Vector cur = {0, 0};
|
|
280
|
|
281 cur = token->point;
|
|
282 translate_point(drawing, &cur);
|
|
283 int x0 = cur.x;
|
|
284 int y0 = cur.y;
|
|
285 token = token->next;
|
|
286 cur = token->point;
|
|
287 translate_point(drawing, &cur);
|
|
288 int x1 = cur.x;
|
|
289 int y1 = cur.y;
|
|
290 token = token->next;
|
|
291 cur = token->point;
|
|
292 translate_point(drawing, &cur);
|
|
293 int x2 = cur.x;
|
|
294 int y2 = cur.y;
|
|
295 token = token->next;
|
|
296 cur = token->point;
|
|
297 translate_point(drawing, &cur);
|
|
298 int x3 = cur.x;
|
|
299 int y3 = cur.y;
|
|
300
|
|
301 if (spline) {
|
|
302 // 1 [-1 +3 -3 +1]
|
|
303 // - * [+3 -6 +3 0]
|
|
304 // 6 [-3 0 +3 0]
|
|
305 // [+1 +4 +1 0]
|
|
306
|
|
307 double div6 = 1.0/6.0;
|
|
308
|
|
309 cx3 = div6*(- x0+3*x1-3*x2+x3);
|
|
310 cx2 = div6*( 3*x0-6*x1+3*x2);
|
|
311 cx1 = div6*(-3*x0 +3*x2);
|
|
312 cx0 = div6*( x0+4*x1+1*x2);
|
|
313
|
|
314 cy3 = div6*(- y0+3*y1-3*y2+y3);
|
|
315 cy2 = div6*( 3*y0-6*y1+3*y2);
|
|
316 cy1 = div6*(-3*y0 +3*y2);
|
|
317 cy0 = div6*( y0+4*y1+1*y2);
|
|
318 } else {
|
|
319 // [-1 +3 -3 +1]
|
|
320 // [+3 -6 +3 0]
|
|
321 // [-3 +3 0 0]
|
|
322 // [+1 0 0 0]
|
|
323
|
|
324 cx3 = - x0+3*x1-3*x2+x3;
|
|
325 cx2 = 3*x0-6*x1+3*x2;
|
|
326 cx1 = -3*x0+3*x1;
|
|
327 cx0 = x0;
|
|
328
|
|
329 cy3 = - y0+3*y1-3*y2+y3;
|
|
330 cy2 = 3*y0-6*y1+3*y2;
|
|
331 cy1 = -3*y0+3*y1;
|
|
332 cy0 = y0;
|
|
333 }
|
|
334
|
|
335 max_accel1 = fabs(2 * cy2) + fabs(6 * cy3);
|
|
336 max_accel2 = fabs(2 * cx2) + fabs(6 * cx3);
|
|
337
|
|
338 max_accel = FFMAX(max_accel1, max_accel2);
|
|
339 h = 1.0;
|
|
340
|
|
341 if (max_accel > CURVE_ACCURACY)
|
|
342 h = sqrt(CURVE_ACCURACY / max_accel);
|
|
343
|
|
344 if (!started) {
|
|
345 cur.x = cx0;
|
|
346 cur.y = cy0;
|
|
347 drawing_add_point(drawing, &cur);
|
|
348 }
|
|
349
|
|
350 for (t = 0; t < 1.0; t += h) {
|
|
351 cur.x = cx0 + t * (cx1 + t * (cx2 + t * cx3));
|
|
352 cur.y = cy0 + t * (cy1 + t * (cy2 + t * cy3));
|
|
353 drawing_add_point(drawing, &cur);
|
|
354 }
|
|
355
|
|
356 cur.x = cx0 + cx1 + cx2 + cx3;
|
|
357 cur.y = cy0 + cy1 + cy2 + cy3;
|
|
358 drawing_add_point(drawing, &cur);
|
|
359 }
|
|
360
|
|
361 /*
|
|
362 * \brief Create and initialize a new drawing and return it
|
|
363 */
|
|
364 ASS_Drawing *ass_drawing_new(void *fontconfig_priv, ASS_Font *font,
|
|
365 ASS_Hinting hint, FT_Library lib)
|
|
366 {
|
|
367 ASS_Drawing *drawing;
|
|
368
|
|
369 drawing = calloc(1, sizeof(*drawing));
|
|
370 drawing->text = calloc(1, DRAWING_INITIAL_SIZE);
|
|
371 drawing->size = DRAWING_INITIAL_SIZE;
|
|
372
|
|
373 drawing->ftlibrary = lib;
|
|
374 if (font) {
|
|
375 drawing->library = font->library;
|
|
376 drawing_make_glyph(drawing, fontconfig_priv, font, hint);
|
|
377 }
|
|
378
|
|
379 drawing->scale_x = 1.;
|
|
380 drawing->scale_y = 1.;
|
|
381 drawing->max_contours = GLYPH_INITIAL_CONTOURS;
|
|
382 drawing->max_points = GLYPH_INITIAL_POINTS;
|
|
383
|
|
384 return drawing;
|
|
385 }
|
|
386
|
|
387 /*
|
|
388 * \brief Free a drawing
|
|
389 */
|
|
390 void ass_drawing_free(ASS_Drawing* drawing)
|
|
391 {
|
|
392 if (drawing) {
|
|
393 if (drawing->glyph)
|
|
394 FT_Done_Glyph((FT_Glyph) drawing->glyph);
|
|
395 free(drawing->text);
|
|
396 }
|
|
397 free(drawing);
|
|
398 }
|
|
399
|
|
400 /*
|
|
401 * \brief Add one ASCII character to the drawing text buffer
|
|
402 */
|
|
403 void ass_drawing_add_char(ASS_Drawing* drawing, char symbol)
|
|
404 {
|
|
405 drawing->text[drawing->i++] = symbol;
|
|
406 drawing->text[drawing->i] = 0;
|
|
407
|
|
408 if (drawing->i + 1 >= drawing->size) {
|
|
409 drawing->size *= 2;
|
|
410 drawing->text = realloc(drawing->text, drawing->size);
|
|
411 }
|
|
412 }
|
|
413
|
|
414 /*
|
|
415 * \brief Create a hashcode for the drawing
|
|
416 * XXX: To avoid collisions a better hash algorithm might be useful.
|
|
417 */
|
|
418 void ass_drawing_hash(ASS_Drawing* drawing)
|
|
419 {
|
|
420 drawing->hash = fnv_32a_str(drawing->text, FNV1_32A_INIT);
|
|
421 }
|
|
422
|
|
423 /*
|
|
424 * \brief Convert token list to outline. Calls the line and curve evaluators.
|
|
425 */
|
|
426 FT_OutlineGlyph *ass_drawing_parse(ASS_Drawing *drawing, int raw_mode)
|
|
427 {
|
|
428 int started = 0;
|
|
429 ASS_DrawingToken *token;
|
|
430 FT_Vector pen = {0, 0};
|
|
431
|
|
432 if (!drawing->glyph)
|
|
433 return NULL;
|
|
434
|
|
435 drawing->tokens = drawing_tokenize(drawing->text);
|
|
436 drawing_prepare(drawing);
|
|
437
|
|
438 token = drawing->tokens;
|
|
439 while (token) {
|
|
440 // Draw something according to current command
|
|
441 switch (token->type) {
|
|
442 case TOKEN_MOVE_NC:
|
|
443 pen = token->point;
|
|
444 translate_point(drawing, &pen);
|
|
445 token = token->next;
|
|
446 break;
|
|
447 case TOKEN_MOVE:
|
|
448 pen = token->point;
|
|
449 translate_point(drawing, &pen);
|
|
450 if (started) {
|
|
451 drawing_close_shape(drawing);
|
|
452 started = 0;
|
|
453 }
|
|
454 token = token->next;
|
|
455 break;
|
|
456 case TOKEN_LINE: {
|
|
457 FT_Vector to;
|
|
458 to = token->point;
|
|
459 translate_point(drawing, &to);
|
|
460 if (!started) drawing_add_point(drawing, &pen);
|
|
461 drawing_add_point(drawing, &to);
|
|
462 started = 1;
|
|
463 token = token->next;
|
|
464 break;
|
|
465 }
|
|
466 case TOKEN_CUBIC_BEZIER:
|
|
467 if (token_check_values(token, 3, TOKEN_CUBIC_BEZIER) &&
|
|
468 token->prev) {
|
|
469 drawing_evaluate_curve(drawing, token->prev, 0, started);
|
|
470 token = token->next;
|
|
471 token = token->next;
|
|
472 token = token->next;
|
|
473 started = 1;
|
|
474 } else
|
|
475 token = token->next;
|
|
476 break;
|
|
477 case TOKEN_B_SPLINE:
|
|
478 if (token_check_values(token, 3, TOKEN_B_SPLINE) &&
|
|
479 token->prev) {
|
|
480 drawing_evaluate_curve(drawing, token->prev, 1, started);
|
|
481 token = token->next;
|
|
482 started = 1;
|
|
483 } else
|
|
484 token = token->next;
|
|
485 break;
|
|
486 default:
|
|
487 token = token->next;
|
|
488 break;
|
|
489 }
|
|
490 }
|
|
491
|
|
492 drawing_finish(drawing, raw_mode);
|
|
493 drawing_free_tokens(drawing->tokens);
|
|
494 return &drawing->glyph;
|
|
495 }
|