2 * Copyright (C) 2009 Grigori Goronzy <greg@geekmind.org>
4 * This file is part of libass.
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.
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.
20 #include "ass_compat.h"
28 #include "ass_utils.h"
29 #include "ass_drawing.h"
32 #define GLYPH_INITIAL_POINTS 100
33 #define GLYPH_INITIAL_SEGMENTS 100
36 * \brief Prepare drawing for parsing. This just sets a few parameters.
38 static void drawing_prepare(ASS_Drawing *drawing)
41 drawing->point_scale_x = drawing->scale_x / (1 << (drawing->scale - 1));
42 drawing->point_scale_y = drawing->scale_y / (1 << (drawing->scale - 1));
46 * \brief Finish a drawing. This only sets the horizontal advance according
47 * to the outline's bbox at the moment.
49 static void drawing_finish(ASS_Drawing *drawing, bool raw_mode)
51 ASS_Rect bbox = drawing->cbox;
52 ASS_Outline *ol = &drawing->outline;
55 ass_msg(drawing->library, MSGL_V,
56 "Parsed drawing with %d points and %d segments",
57 ol->n_points, ol->n_segments);
62 drawing->advance.x = bbox.x_max - bbox.x_min;
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;
68 // Place it onto the baseline
69 for (size_t i = 0; i < ol->n_points; i++)
70 ol->points[i].y -= drawing->asc;
74 * \brief Check whether a number of items on the list is available
76 static int token_check_values(ASS_DrawingToken *token, int i, int type)
78 for (int j = 0; j < i; j++) {
79 if (!token || token->type != type) return 0;
87 * \brief Tokenize a drawing string into a list of ASS_DrawingToken
88 * This also expands points for closing b-splines
90 static ASS_DrawingToken *drawing_tokenize(char *str)
93 int type = -1, is_set = 0;
95 ASS_Vector point = {0, 0};
97 ASS_DrawingToken *root = NULL, *tail = NULL, *spline_start = NULL;
101 if (*p == 'c' && spline_start) {
102 // Close b-splines: add the first three points of the b-spline
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;
109 tail->type = TOKEN_B_SPLINE;
110 tail->point = spline_start->point;
111 spline_start = spline_start->next;
115 } else if (!is_set && mystrtod(&p, &val)) {
116 point.x = double_to_d6(val);
120 } else if (is_set == 1 && mystrtod(&p, &val)) {
121 point.y = double_to_d6(val);
125 } else if (*p == 'm')
128 type = TOKEN_MOVE_NC;
132 type = TOKEN_CUBIC_BEZIER;
134 type = TOKEN_CONIC_BEZIER;
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).
141 // Ignore the odd extra value, it makes no sense.
145 if (type != -1 && is_set == 2) {
147 tail->next = calloc(1, sizeof(ASS_DrawingToken));
148 tail->next->prev = tail;
151 root = tail = calloc(1, sizeof(ASS_DrawingToken));
155 if (type == TOKEN_B_SPLINE && !spline_start)
156 spline_start = tail->prev;
165 * \brief Free a list of tokens
167 static void drawing_free_tokens(ASS_DrawingToken *token)
170 ASS_DrawingToken *at = token;
177 * \brief Update drawing cbox
179 static inline void update_cbox(ASS_Drawing *drawing, ASS_Vector *point)
181 ASS_Rect *box = &drawing->cbox;
183 box->x_min = FFMIN(box->x_min, point->x);
184 box->x_max = FFMAX(box->x_max, point->x);
185 box->y_min = FFMIN(box->y_min, point->y);
186 box->y_max = FFMAX(box->y_max, point->y);
190 * \brief Translate and scale a point coordinate according to baseline
193 static inline void translate_point(ASS_Drawing *drawing, ASS_Vector *point)
195 point->x = drawing->point_scale_x * point->x;
196 point->y = drawing->point_scale_y * point->y;
198 update_cbox(drawing, point);
202 * \brief Add curve to drawing
204 static bool drawing_add_curve(ASS_Drawing *drawing, ASS_DrawingToken *token,
205 bool spline, int started)
208 for (int i = 0; i < 4; ++i) {
210 translate_point(drawing, &p[i]);
215 int x01 = (p[1].x - p[0].x) / 3;
216 int y01 = (p[1].y - p[0].y) / 3;
217 int x12 = (p[2].x - p[1].x) / 3;
218 int y12 = (p[2].y - p[1].y) / 3;
219 int x23 = (p[3].x - p[2].x) / 3;
220 int y23 = (p[3].y - p[2].y) / 3;
222 p[0].x = p[1].x + ((x12 - x01) >> 1);
223 p[0].y = p[1].y + ((y12 - y01) >> 1);
224 p[3].x = p[2].x + ((x23 - x12) >> 1);
225 p[3].y = p[2].y + ((y23 - y12) >> 1);
233 outline_add_point(&drawing->outline, p[0], 0)) &&
234 outline_add_point(&drawing->outline, p[1], 0) &&
235 outline_add_point(&drawing->outline, p[2], 0) &&
236 outline_add_point(&drawing->outline, p[3], OUTLINE_CUBIC_SPLINE);
240 * \brief Create and initialize a new drawing and return it
242 ASS_Drawing *ass_drawing_new(ASS_Library *lib)
244 ASS_Drawing *drawing = calloc(1, sizeof(*drawing));
247 drawing->cbox.x_min = drawing->cbox.y_min = INT32_MAX;
248 drawing->cbox.x_max = drawing->cbox.y_max = INT32_MIN;
249 drawing->library = lib;
250 drawing->scale_x = 1.;
251 drawing->scale_y = 1.;
253 if (!outline_alloc(&drawing->outline, GLYPH_INITIAL_POINTS, GLYPH_INITIAL_SEGMENTS)) {
261 * \brief Free a drawing
263 void ass_drawing_free(ASS_Drawing *drawing)
267 outline_free(&drawing->outline);
273 * \brief Copy an ASCII string to the drawing text buffer
275 void ass_drawing_set_text(ASS_Drawing *drawing, char *str, size_t len)
278 drawing->text = strndup(str, len);
282 * \brief Create a hashcode for the drawing
283 * XXX: To avoid collisions a better hash algorithm might be useful.
285 void ass_drawing_hash(ASS_Drawing *drawing)
289 drawing->hash = fnv_32a_str(drawing->text, FNV1_32A_INIT);
293 * \brief Convert token list to outline. Calls the line and curve evaluators.
295 ASS_Outline *ass_drawing_parse(ASS_Drawing *drawing, bool raw_mode)
297 bool started = false;
298 ASS_DrawingToken *token;
299 ASS_Vector pen = {0, 0};
301 drawing->tokens = drawing_tokenize(drawing->text);
302 drawing_prepare(drawing);
304 token = drawing->tokens;
306 // Draw something according to current command
307 switch (token->type) {
310 translate_point(drawing, &pen);
315 translate_point(drawing, &pen);
317 if (!outline_add_segment(&drawing->outline, OUTLINE_LINE_SEGMENT))
319 if (!outline_close_contour(&drawing->outline))
326 ASS_Vector to = token->point;
327 translate_point(drawing, &to);
328 if (!started && !outline_add_point(&drawing->outline, pen, 0))
330 if (!outline_add_point(&drawing->outline, to, OUTLINE_LINE_SEGMENT))
336 case TOKEN_CUBIC_BEZIER:
337 if (token_check_values(token, 3, TOKEN_CUBIC_BEZIER) &&
339 if (!drawing_add_curve(drawing, token->prev, false, started))
349 if (token_check_values(token, 3, TOKEN_B_SPLINE) &&
351 if (!drawing_add_curve(drawing, token->prev, true, started))
364 // Close the last contour
366 if (!outline_add_segment(&drawing->outline, OUTLINE_LINE_SEGMENT))
368 if (!outline_close_contour(&drawing->outline))
372 drawing_finish(drawing, raw_mode);
373 drawing_free_tokens(drawing->tokens);
374 return &drawing->outline;
377 drawing_free_tokens(drawing->tokens);