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