]> granicus.if.org Git - libass/blob - libass/ass_render.h
33e046f7f53235d5b97b61ce0decaa2f147db365
[libass] / libass / ass_render.h
1 /*
2  * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
3  * Copyright (C) 2009 Grigori Goronzy <greg@geekmind.org>
4  *
5  * This file is part of libass.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19
20 #ifndef LIBASS_RENDER_H
21 #define LIBASS_RENDER_H
22
23 #include <inttypes.h>
24 #include <ft2build.h>
25 #include FT_FREETYPE_H
26 #include FT_STROKER_H
27 #include FT_GLYPH_H
28 #include FT_SYNTHESIS_H
29
30 #include "ass.h"
31 #include "ass_font.h"
32 #include "ass_bitmap.h"
33 #include "ass_cache.h"
34 #include "ass_utils.h"
35 #include "ass_fontconfig.h"
36 #include "ass_library.h"
37 #include "ass_drawing.h"
38
39 #define GLYPH_CACHE_MAX 1000
40 #define BITMAP_CACHE_MAX_SIZE 30 * 1048576
41
42 #define PARSED_FADE (1<<0)
43 #define PARSED_A    (1<<1)
44
45 typedef struct {
46     double xMin;
47     double xMax;
48     double yMin;
49     double yMax;
50 } DBBox;
51
52 typedef struct {
53     double x;
54     double y;
55 } DVector;
56
57 typedef struct free_list {
58     void *object;
59     struct free_list *next;
60 } FreeList;
61
62 typedef struct {
63     int frame_width;
64     int frame_height;
65     double font_size_coeff;     // font size multiplier
66     double line_spacing;        // additional line spacing (in frame pixels)
67     int top_margin;             // height of top margin. Everything except toptitles is shifted down by top_margin.
68     int bottom_margin;          // height of bottom margin. (frame_height - top_margin - bottom_margin) is original video height.
69     int left_margin;
70     int right_margin;
71     int use_margins;            // 0 - place all subtitles inside original frame
72     // 1 - use margins for placing toptitles and subtitles
73     double aspect;              // frame aspect ratio, d_width / d_height.
74     double storage_aspect;      // pixel ratio of the source image
75     ASS_Hinting hinting;
76
77     char *default_font;
78     char *default_family;
79 } ASS_Settings;
80
81 // a rendered event
82 typedef struct {
83     ASS_Image *imgs;
84     int top, height, left, width;
85     int detect_collisions;
86     int shift_direction;
87     ASS_Event *event;
88 } EventImages;
89
90 typedef enum {
91     EF_NONE = 0,
92     EF_KARAOKE,
93     EF_KARAOKE_KF,
94     EF_KARAOKE_KO
95 } Effect;
96
97 // describes a glyph
98 // GlyphInfo and TextInfo are used for text centering and word-wrapping operations
99 typedef struct glyph_info {
100     unsigned symbol;
101     unsigned skip;              // skip glyph when layouting text
102     ASS_Font *font;
103     int face_index;
104     int glyph_index;
105     double font_size;
106     ASS_Drawing *drawing;
107     FT_Outline *outline;
108     FT_Outline *border;
109     Bitmap *bm;                 // glyph bitmap
110     Bitmap *bm_o;               // outline bitmap
111     Bitmap *bm_s;               // shadow bitmap
112     FT_BBox bbox;
113     FT_Vector pos;
114     FT_Vector offset;
115     char linebreak;             // the first (leading) glyph of some line ?
116     uint32_t c[4];              // colors
117     FT_Vector advance;          // 26.6
118     Effect effect_type;
119     int effect_timing;          // time duration of current karaoke word
120     // after process_karaoke_effects: distance in pixels from the glyph origin.
121     // part of the glyph to the left of it is displayed in a different color.
122     int effect_skip_timing;     // delay after the end of last karaoke word
123     int asc, desc;              // font max ascender and descender
124     int be;                     // blur edges
125     double blur;                // gaussian blur
126     double shadow_x;
127     double shadow_y;
128     double frx, fry, frz;       // rotation
129     double fax, fay;            // text shearing
130     double scale_x, scale_y;
131     double border_x, border_y;
132     unsigned italic;
133     unsigned bold;
134     int flags;
135
136     int bm_run_id;
137     int shape_run_id;
138
139     BitmapHashKey hash_key;
140
141     // next glyph in this cluster
142     struct glyph_info *next;
143 } GlyphInfo;
144
145 typedef struct {
146     double asc, desc;
147     int offset, len;
148 } LineInfo;
149
150 typedef struct {
151     GlyphInfo *glyphs;
152     int length;
153     LineInfo *lines;
154     int n_lines;
155     double height;
156     int max_glyphs;
157     int max_lines;
158 } TextInfo;
159
160 // Renderer state.
161 // Values like current font face, color, screen position, clipping and so on are stored here.
162 typedef struct {
163     ASS_Event *event;
164     ASS_Style *style;
165     int parsed_tags;
166
167     ASS_Font *font;
168     double font_size;
169     int flags;                  // decoration flags (underline/strike-through)
170
171     FT_Stroker stroker;
172     int alignment;              // alignment overrides go here; if zero, style value will be used
173     double frx, fry, frz;
174     double fax, fay;            // text shearing
175     enum {
176         EVENT_NORMAL,           // "normal" top-, sub- or mid- title
177         EVENT_POSITIONED,       // happens after pos(,), margins are ignored
178         EVENT_HSCROLL,          // "Banner" transition effect, text_width is unlimited
179         EVENT_VSCROLL           // "Scroll up", "Scroll down" transition effects
180     } evt_type;
181     double pos_x, pos_y;        // position
182     double org_x, org_y;        // origin
183     char have_origin;           // origin is explicitly defined; if 0, get_base_point() is used
184     double scale_x, scale_y;
185     double hspacing;            // distance between letters, in pixels
186     double border_x;            // outline width
187     double border_y;
188     uint32_t c[4];              // colors(Primary, Secondary, so on) in RGBA
189     int clip_x0, clip_y0, clip_x1, clip_y1;
190     char clip_mode;             // 1 = iclip
191     char detect_collisions;
192     uint32_t fade;              // alpha from \fad
193     char be;                    // blur edges
194     double blur;                // gaussian blur
195     double shadow_x;
196     double shadow_y;
197     int drawing_mode;           // not implemented; when != 0 text is discarded, except for style override tags
198     ASS_Drawing *drawing;       // current drawing
199     ASS_Drawing *clip_drawing;  // clip vector
200     int clip_drawing_mode;      // 0 = regular clip, 1 = inverse clip
201
202     Effect effect_type;
203     int effect_timing;
204     int effect_skip_timing;
205
206     // bitmap run id (used for final bitmap rendering)
207     int bm_run_id;
208
209     enum {
210         SCROLL_LR,              // left-to-right
211         SCROLL_RL,
212         SCROLL_TB,              // top-to-bottom
213         SCROLL_BT
214     } scroll_direction;         // for EVENT_HSCROLL, EVENT_VSCROLL
215     int scroll_shift;
216
217     // face properties
218     char *family;
219     unsigned bold;
220     unsigned italic;
221     int treat_family_as_pattern;
222     int wrap_style;
223 } RenderContext;
224
225 typedef struct {
226     Cache *font_cache;
227     Cache *outline_cache;
228     Cache *bitmap_cache;
229     Cache *composite_cache;
230     size_t glyph_max;
231     size_t bitmap_max_size;
232 } CacheStore;
233
234 struct ass_renderer {
235     ASS_Library *library;
236     FT_Library ftlibrary;
237     FCInstance *fontconfig_priv;
238     ASS_Settings settings;
239     int render_id;
240     ASS_SynthPriv *synth_priv;
241
242     ASS_Image *images_root;     // rendering result is stored here
243     ASS_Image *prev_images_root;
244
245     EventImages *eimg;          // temporary buffer for sorting rendered events
246     int eimg_size;              // allocated buffer size
247
248     // frame-global data
249     int width, height;          // screen dimensions
250     int orig_height;            // frame height ( = screen height - margins )
251     int orig_width;             // frame width ( = screen width - margins )
252     int orig_height_nocrop;     // frame height ( = screen height - margins + cropheight)
253     int orig_width_nocrop;      // frame width ( = screen width - margins + cropwidth)
254     ASS_Track *track;
255     long long time;             // frame's timestamp, ms
256     double font_scale;
257     double font_scale_x;        // x scale applied to all glyphs to preserve text aspect ratio
258     double border_scale;
259
260     RenderContext state;
261     TextInfo text_info;
262     CacheStore cache;
263
264     FreeList *free_head;
265     FreeList *free_tail;
266 };
267
268 typedef struct render_priv {
269     int top, height, left, width;
270     int render_id;
271 } RenderPriv;
272
273 typedef struct {
274     int x0;
275     int y0;
276     int x1;
277     int y1;
278 } Rect;
279
280 typedef struct {
281     int a, b;                   // top and height
282     int ha, hb;                 // left and width
283 } Segment;
284
285 void reset_render_context(ASS_Renderer *render_priv);
286 void ass_free_images(ASS_Image *img);
287
288 // XXX: this is actually in ass.c, includes should be fixed later on
289 void ass_lazy_track_init(ASS_Library *lib, ASS_Track *track);
290
291 #endif /* LIBASS_RENDER_H */