]> granicus.if.org Git - libass/blob - libass/ass_drawing.c
drawing: remove unnecessary fields from ASS_Drawing struct
[libass] / libass / ass_drawing.c
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 "config.h"
20 #include "ass_compat.h"
21
22 #include <ft2build.h>
23 #include FT_OUTLINE_H
24 #include <math.h>
25 #include <stdbool.h>
26 #include <limits.h>
27
28 #include "ass_utils.h"
29 #include "ass_drawing.h"
30 #include "ass_font.h"
31
32 #define GLYPH_INITIAL_POINTS 100
33 #define GLYPH_INITIAL_SEGMENTS 100
34
35 /*
36  * \brief Prepare drawing for parsing.  This just sets a few parameters.
37  */
38 static void drawing_prepare(ASS_Drawing *drawing)
39 {
40     // Scaling parameters
41     drawing->point_scale_x = drawing->scale_x / (1 << (drawing->scale - 1));
42     drawing->point_scale_y = drawing->scale_y / (1 << (drawing->scale - 1));
43 }
44
45 /*
46  * \brief Finish a drawing.  This only sets the horizontal advance according
47  * to the outline's bbox at the moment.
48  */
49 static void drawing_finish(ASS_Drawing *drawing, bool raw_mode)
50 {
51     ASS_Rect bbox = drawing->cbox;
52     ASS_Outline *ol = &drawing->outline;
53
54     if (drawing->library)
55         ass_msg(drawing->library, MSGL_V,
56                 "Parsed drawing with %d points and %d segments",
57                 ol->n_points, ol->n_segments);
58
59     if (raw_mode)
60         return;
61
62     drawing->advance.x = bbox.x_max - bbox.x_min;
63
64     double pbo = drawing->pbo / (1 << (drawing->scale - 1));
65     drawing->desc = double_to_d6(pbo * drawing->scale_y);
66     drawing->asc = bbox.y_max - bbox.y_min - drawing->desc;
67
68     // Place it onto the baseline
69     for (size_t i = 0; i < ol->n_points; i++)
70         ol->points[i].y -= drawing->asc;
71 }
72
73 /*
74  * \brief Check whether a number of items on the list is available
75  */
76 static int token_check_values(ASS_DrawingToken *token, int i, int type)
77 {
78     for (int j = 0; j < i; j++) {
79         if (!token || token->type != type) return 0;
80         token = token->next;
81     }
82
83     return 1;
84 }
85
86 /*
87  * \brief Tokenize a drawing string into a list of ASS_DrawingToken
88  * This also expands points for closing b-splines
89  */
90 static ASS_DrawingToken *drawing_tokenize(char *str)
91 {
92     char *p = str;
93     int type = -1, is_set = 0;
94     double val;
95     ASS_Vector point = {0, 0};
96
97     ASS_DrawingToken *root = NULL, *tail = NULL, *spline_start = NULL;
98
99     while (p && *p) {
100         int got_coord = 0;
101         if (*p == 'c' && spline_start) {
102             // Close b-splines: add the first three points of the b-spline
103             // back to the end
104             if (token_check_values(spline_start->next, 2, TOKEN_B_SPLINE)) {
105                 for (int i = 0; i < 3; i++) {
106                     tail->next = calloc(1, sizeof(ASS_DrawingToken));
107                     tail->next->prev = tail;
108                     tail = tail->next;
109                     tail->type = TOKEN_B_SPLINE;
110                     tail->point = spline_start->point;
111                     spline_start = spline_start->next;
112                 }
113                 spline_start = NULL;
114             }
115         } else if (!is_set && mystrtod(&p, &val)) {
116             point.x = double_to_d6(val);
117             is_set = 1;
118             got_coord = 1;
119             p--;
120         } else if (is_set == 1 && mystrtod(&p, &val)) {
121             point.y = double_to_d6(val);
122             is_set = 2;
123             got_coord = 1;
124             p--;
125         } else if (*p == 'm')
126             type = TOKEN_MOVE;
127         else if (*p == 'n')
128             type = TOKEN_MOVE_NC;
129         else if (*p == 'l')
130             type = TOKEN_LINE;
131         else if (*p == 'b')
132             type = TOKEN_CUBIC_BEZIER;
133         else if (*p == 'q')
134             type = TOKEN_CONIC_BEZIER;
135         else if (*p == 's')
136             type = TOKEN_B_SPLINE;
137         // We're simply ignoring TOKEN_EXTEND_B_SPLINE here.
138         // This is not harmful at all, since it can be ommitted with
139         // similar result (the spline is extended anyway).
140
141         // Ignore the odd extra value, it makes no sense.
142         if (!got_coord)
143             is_set = 0;
144
145         if (type != -1 && is_set == 2) {
146             if (root) {
147                 tail->next = calloc(1, sizeof(ASS_DrawingToken));
148                 tail->next->prev = tail;
149                 tail = tail->next;
150             } else
151                 root = tail = calloc(1, sizeof(ASS_DrawingToken));
152             tail->type = type;
153             tail->point = point;
154             is_set = 0;
155             if (type == TOKEN_B_SPLINE && !spline_start)
156                 spline_start = tail->prev;
157         }
158         p++;
159     }
160
161     return root;
162 }
163
164 /*
165  * \brief Free a list of tokens
166  */
167 static void drawing_free_tokens(ASS_DrawingToken *token)
168 {
169     while (token) {
170         ASS_DrawingToken *at = token;
171         token = token->next;
172         free(at);
173     }
174 }
175
176 /*
177  * \brief Translate and scale a point coordinate according to baseline
178  * offset and scale.
179  */
180 static inline void translate_point(ASS_Drawing *drawing, ASS_Vector *point)
181 {
182     point->x = lrint(drawing->point_scale_x * point->x);
183     point->y = lrint(drawing->point_scale_y * point->y);
184
185     rectangle_update(&drawing->cbox, point->x, point->y, point->x, point->y);
186 }
187
188 /*
189  * \brief Add curve to drawing
190  */
191 static bool drawing_add_curve(ASS_Drawing *drawing, ASS_DrawingToken *token,
192                               bool spline, int started)
193 {
194     ASS_Vector p[4];
195     for (int i = 0; i < 4; ++i) {
196         p[i] = token->point;
197         translate_point(drawing, &p[i]);
198         token = token->next;
199     }
200
201     if (spline) {
202         int x01 = (p[1].x - p[0].x) / 3;
203         int y01 = (p[1].y - p[0].y) / 3;
204         int x12 = (p[2].x - p[1].x) / 3;
205         int y12 = (p[2].y - p[1].y) / 3;
206         int x23 = (p[3].x - p[2].x) / 3;
207         int y23 = (p[3].y - p[2].y) / 3;
208
209         p[0].x = p[1].x + ((x12 - x01) >> 1);
210         p[0].y = p[1].y + ((y12 - y01) >> 1);
211         p[3].x = p[2].x + ((x23 - x12) >> 1);
212         p[3].y = p[2].y + ((y23 - y12) >> 1);
213         p[1].x += x12;
214         p[1].y += y12;
215         p[2].x -= x12;
216         p[2].y -= y12;
217     }
218
219     return (started ||
220         outline_add_point(&drawing->outline, p[0], 0)) &&
221         outline_add_point(&drawing->outline, p[1], 0) &&
222         outline_add_point(&drawing->outline, p[2], 0) &&
223         outline_add_point(&drawing->outline, p[3], OUTLINE_CUBIC_SPLINE);
224 }
225
226 /*
227  * \brief Create and initialize a new drawing and return it
228  */
229 ASS_Drawing *ass_drawing_new(ASS_Library *lib)
230 {
231     ASS_Drawing *drawing = calloc(1, sizeof(*drawing));
232     if (!drawing)
233         return NULL;
234     rectangle_reset(&drawing->cbox);
235     drawing->library = lib;
236     drawing->scale_x = 1.;
237     drawing->scale_y = 1.;
238
239     if (!outline_alloc(&drawing->outline, GLYPH_INITIAL_POINTS, GLYPH_INITIAL_SEGMENTS)) {
240         free(drawing);
241         return NULL;
242     }
243     return drawing;
244 }
245
246 /*
247  * \brief Free a drawing
248  */
249 void ass_drawing_free(ASS_Drawing *drawing)
250 {
251     if (drawing) {
252         free(drawing->text);
253         outline_free(&drawing->outline);
254     }
255     free(drawing);
256 }
257
258 /*
259  * \brief Copy an ASCII string to the drawing text buffer
260  */
261 void ass_drawing_set_text(ASS_Drawing *drawing, char *str, size_t len)
262 {
263     free(drawing->text);
264     drawing->text = strndup(str, len);
265 }
266
267 /*
268  * \brief Convert token list to outline.  Calls the line and curve evaluators.
269  */
270 ASS_Outline *ass_drawing_parse(ASS_Drawing *drawing, bool raw_mode)
271 {
272     bool started = false;
273     ASS_Vector pen = {0, 0};
274
275     ASS_DrawingToken *tokens = drawing_tokenize(drawing->text);
276     drawing_prepare(drawing);
277
278     ASS_DrawingToken *token = tokens;
279     while (token) {
280         // Draw something according to current command
281         switch (token->type) {
282         case TOKEN_MOVE_NC:
283             pen = token->point;
284             translate_point(drawing, &pen);
285             token = token->next;
286             break;
287         case TOKEN_MOVE:
288             pen = token->point;
289             translate_point(drawing, &pen);
290             if (started) {
291                 if (!outline_add_segment(&drawing->outline, OUTLINE_LINE_SEGMENT))
292                     goto error;
293                 if (!outline_close_contour(&drawing->outline))
294                     goto error;
295                 started = false;
296             }
297             token = token->next;
298             break;
299         case TOKEN_LINE: {
300             ASS_Vector to = token->point;
301             translate_point(drawing, &to);
302             if (!started && !outline_add_point(&drawing->outline, pen, 0))
303                 goto error;
304             if (!outline_add_point(&drawing->outline, to, OUTLINE_LINE_SEGMENT))
305                 goto error;
306             started = true;
307             token = token->next;
308             break;
309         }
310         case TOKEN_CUBIC_BEZIER:
311             if (token_check_values(token, 3, TOKEN_CUBIC_BEZIER) &&
312                 token->prev) {
313                 if (!drawing_add_curve(drawing, token->prev, false, started))
314                     goto error;
315                 token = token->next;
316                 token = token->next;
317                 token = token->next;
318                 started = true;
319             } else
320                 token = token->next;
321             break;
322         case TOKEN_B_SPLINE:
323             if (token_check_values(token, 3, TOKEN_B_SPLINE) &&
324                 token->prev) {
325                 if (!drawing_add_curve(drawing, token->prev, true, started))
326                     goto error;
327                 token = token->next;
328                 started = true;
329             } else
330                 token = token->next;
331             break;
332         default:
333             token = token->next;
334             break;
335         }
336     }
337
338     // Close the last contour
339     if (started) {
340         if (!outline_add_segment(&drawing->outline, OUTLINE_LINE_SEGMENT))
341             goto error;
342         if (!outline_close_contour(&drawing->outline))
343             goto error;
344     }
345
346     drawing_finish(drawing, raw_mode);
347     drawing_free_tokens(tokens);
348     return &drawing->outline;
349
350 error:
351     drawing_free_tokens(tokens);
352     return NULL;
353 }