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 Translate and scale a point coordinate according to baseline
180 static inline void translate_point(ASS_Drawing *drawing, ASS_Vector *point)
182 point->x = lrint(drawing->point_scale_x * point->x);
183 point->y = lrint(drawing->point_scale_y * point->y);
185 rectangle_update(&drawing->cbox, point->x, point->y, point->x, point->y);
189 * \brief Add curve to drawing
191 static bool drawing_add_curve(ASS_Drawing *drawing, ASS_DrawingToken *token,
192 bool spline, int started)
195 for (int i = 0; i < 4; ++i) {
197 translate_point(drawing, &p[i]);
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;
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);
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);
227 * \brief Create and initialize a new drawing and return it
229 ASS_Drawing *ass_drawing_new(ASS_Library *lib)
231 ASS_Drawing *drawing = calloc(1, sizeof(*drawing));
234 rectangle_reset(&drawing->cbox);
235 drawing->library = lib;
236 drawing->scale_x = 1.;
237 drawing->scale_y = 1.;
239 if (!outline_alloc(&drawing->outline, GLYPH_INITIAL_POINTS, GLYPH_INITIAL_SEGMENTS)) {
247 * \brief Free a drawing
249 void ass_drawing_free(ASS_Drawing *drawing)
253 outline_free(&drawing->outline);
259 * \brief Copy an ASCII string to the drawing text buffer
261 void ass_drawing_set_text(ASS_Drawing *drawing, char *str, size_t len)
264 drawing->text = strndup(str, len);
268 * \brief Convert token list to outline. Calls the line and curve evaluators.
270 ASS_Outline *ass_drawing_parse(ASS_Drawing *drawing, bool raw_mode)
272 bool started = false;
273 ASS_Vector pen = {0, 0};
275 ASS_DrawingToken *tokens = drawing_tokenize(drawing->text);
276 drawing_prepare(drawing);
278 ASS_DrawingToken *token = tokens;
280 // Draw something according to current command
281 switch (token->type) {
284 translate_point(drawing, &pen);
289 translate_point(drawing, &pen);
291 if (!outline_add_segment(&drawing->outline, OUTLINE_LINE_SEGMENT))
293 if (!outline_close_contour(&drawing->outline))
300 ASS_Vector to = token->point;
301 translate_point(drawing, &to);
302 if (!started && !outline_add_point(&drawing->outline, pen, 0))
304 if (!outline_add_point(&drawing->outline, to, OUTLINE_LINE_SEGMENT))
310 case TOKEN_CUBIC_BEZIER:
311 if (token_check_values(token, 3, TOKEN_CUBIC_BEZIER) &&
313 if (!drawing_add_curve(drawing, token->prev, false, started))
323 if (token_check_values(token, 3, TOKEN_B_SPLINE) &&
325 if (!drawing_add_curve(drawing, token->prev, true, started))
338 // Close the last contour
340 if (!outline_add_segment(&drawing->outline, OUTLINE_LINE_SEGMENT))
342 if (!outline_close_contour(&drawing->outline))
346 drawing_finish(drawing, raw_mode);
347 drawing_free_tokens(tokens);
348 return &drawing->outline;
351 drawing_free_tokens(tokens);