summaryrefslogtreecommitdiffstats
path: root/libass/ass_outline.h
blob: 414103a1db26f5432bfe1a40b5f24b45336f11f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright (C) 2016 Vabishchevich Nikolay <vabnick@gmail.com>
 *
 * This file is part of libass.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef LIBASS_OUTLINE_H
#define LIBASS_OUTLINE_H

#include <ft2build.h>
#include FT_OUTLINE_H
#include <stdbool.h>
#include <stdint.h>

#include "ass_utils.h"


typedef struct {
    int32_t x, y;
} ASS_Vector;

typedef struct {
    double x, y;
} ASS_DVector;

typedef struct {
    int32_t x_min, y_min, x_max, y_max;
} ASS_Rect;

typedef struct {
    double x_min, y_min, x_max, y_max;
} ASS_DRect;

static inline void rectangle_reset(ASS_Rect *rect)
{
    rect->x_min = rect->y_min = INT32_MAX;
    rect->x_max = rect->y_max = INT32_MIN;
}

static inline void rectangle_update(ASS_Rect *rect,
    int32_t x_min, int32_t y_min, int32_t x_max, int32_t y_max)
{
    rect->x_min = FFMIN(rect->x_min, x_min);
    rect->y_min = FFMIN(rect->y_min, y_min);
    rect->x_max = FFMAX(rect->x_max, x_max);
    rect->y_max = FFMAX(rect->y_max, y_max);
}

/*
 * Outline represented with array of points and array of segments.
 * Segment here is spline of order 1 (line), 2 (quadratic) or 3 (cubic).
 * Each segment owns number of points equal to its order in point array
 * and uses first point owned by the next segment as last point.
 * Last segment in each contour instead of the next segment point uses
 * point owned by the first segment in that contour. Correspondingly
 * total number of points is equal to the sum of spline orders of all segments.
 */

enum {
    OUTLINE_LINE_SEGMENT     = 1,  // line segment
    OUTLINE_QUADRATIC_SPLINE = 2,  // quadratic spline
    OUTLINE_CUBIC_SPLINE     = 3,  // cubic spline
    OUTLINE_COUNT_MASK       = 3,  // spline order mask
    OUTLINE_CONTOUR_END      = 4   // last segment in contour flag
};

typedef struct {
    size_t n_points, max_points;
    size_t n_segments, max_segments;
    ASS_Vector *points;
    char *segments;
} ASS_Outline;

#define OUTLINE_MIN  (-((int32_t) 1 << 28))
#define OUTLINE_MAX  (((int32_t) 1 << 28) - 1)

bool outline_alloc(ASS_Outline *outline, size_t n_points, size_t n_segments);
bool outline_convert(ASS_Outline *outline, const FT_Outline *source);
bool outline_scale_pow2(ASS_Outline *outline, const ASS_Outline *source,
                        int scale_ord_x, int scale_ord_y);
bool outline_transform_2d(ASS_Outline *outline, const ASS_Outline *source,
                          const double m[2][3]);
bool outline_transform_3d(ASS_Outline *outline, const ASS_Outline *source,
                          const double m[3][3]);
void outline_free(ASS_Outline *outline);

bool outline_add_point(ASS_Outline *outline, ASS_Vector pt, char segment);
bool outline_add_segment(ASS_Outline *outline, char segment);
bool outline_close_contour(ASS_Outline *outline);

void outline_update_cbox(const ASS_Outline *outline, ASS_Rect *cbox);

bool outline_stroke(ASS_Outline *result, ASS_Outline *result1,
                    const ASS_Outline *path, int xbord, int ybord, int eps);


#endif /* LIBASS_OUTLINE_H */