]> granicus.if.org Git - libass/blob - libass/ass_render.h
renderer: move outline stroking immediately before rasterization
[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_GLYPH_H
27 #include FT_SYNTHESIS_H
28 #ifdef CONFIG_HARFBUZZ
29 #include <hb.h>
30 #endif
31
32 #include "ass.h"
33 #include "ass_font.h"
34 #include "ass_bitmap.h"
35 #include "ass_cache.h"
36 #include "ass_utils.h"
37 #include "ass_fontselect.h"
38 #include "ass_library.h"
39 #include "ass_drawing.h"
40 #include "ass_bitmap.h"
41 #include "ass_rasterizer.h"
42
43 #define GLYPH_CACHE_MAX 10000
44 #define MEGABYTE (1024 * 1024)
45 #define BITMAP_CACHE_MAX_SIZE (128 * MEGABYTE)
46 #define COMPOSITE_CACHE_RATIO 2
47 #define COMPOSITE_CACHE_MAX_SIZE (BITMAP_CACHE_MAX_SIZE / COMPOSITE_CACHE_RATIO)
48
49 #define PARSED_FADE (1<<0)
50 #define PARSED_A    (1<<1)
51
52 typedef struct {
53     ASS_Image result;
54     CompositeHashValue *source;
55     size_t ref_count;
56 } ASS_ImagePriv;
57
58 typedef struct {
59     int frame_width;
60     int frame_height;
61     int storage_width;          // video width before any rescaling
62     int storage_height;         // video height before any rescaling
63     double font_size_coeff;     // font size multiplier
64     double line_spacing;        // additional line spacing (in frame pixels)
65     double line_position;       // vertical position for subtitles, 0-100 (0 = no change)
66     int top_margin;             // height of top margin. Everything except toptitles is shifted down by top_margin.
67     int bottom_margin;          // height of bottom margin. (frame_height - top_margin - bottom_margin) is original video height.
68     int left_margin;
69     int right_margin;
70     int use_margins;            // 0 - place all subtitles inside original frame
71     // 1 - use margins for placing toptitles and subtitles
72     double par;                 // user defined pixel aspect ratio (0 = unset)
73     ASS_Hinting hinting;
74     ASS_ShapingLevel shaper;
75     int selective_style_overrides; // ASS_OVERRIDE_* flags
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 combined bitmap
98 typedef struct {
99     FilterDesc filter;
100     uint32_t c[4];              // colors
101     Effect effect_type;
102     int effect_timing;          // time duration of current karaoke word
103     // after process_karaoke_effects: distance in pixels from the glyph origin.
104     // part of the glyph to the left of it is displayed in a different color.
105
106     int first_pos_x;
107
108     size_t bitmap_count, max_bitmap_count;
109     BitmapRef *bitmaps;
110
111     int x, y;
112     Bitmap *bm, *bm_o, *bm_s;   // glyphs, outline, shadow bitmaps
113     CompositeHashValue *image;
114 } CombinedBitmapInfo;
115
116 typedef struct {
117     ASS_DVector scale, offset;
118 } ASS_Transform;
119
120 // describes a glyph
121 // GlyphInfo and TextInfo are used for text centering and word-wrapping operations
122 typedef struct glyph_info {
123     unsigned symbol;
124     unsigned skip;              // skip glyph when layouting text
125     ASS_Font *font;
126     int face_index;
127     int glyph_index;
128 #ifdef CONFIG_HARFBUZZ
129     hb_script_t script;
130 #else
131     int script;
132 #endif
133     double font_size;
134     char *drawing_text;
135     int drawing_scale;
136     int drawing_pbo;
137     OutlineHashValue *outline;
138     ASS_Transform transform;
139     ASS_Rect bbox;
140     ASS_Vector pos;
141     ASS_Vector offset;
142     char linebreak;             // the first (leading) glyph of some line ?
143     uint32_t c[4];              // colors
144     ASS_Vector advance;         // 26.6
145     ASS_Vector cluster_advance;
146     char effect;                // the first (leading) glyph of some effect ?
147     Effect effect_type;
148     int effect_timing;          // time duration of current karaoke word
149     // after process_karaoke_effects: distance in pixels from the glyph origin.
150     // part of the glyph to the left of it is displayed in a different color.
151     int effect_skip_timing;     // delay after the end of last karaoke word
152     int asc, desc;              // font max ascender and descender
153     int be;                     // blur edges
154     double blur;                // gaussian blur
155     double shadow_x;
156     double shadow_y;
157     double frx, fry, frz;       // rotation
158     double fax, fay;            // text shearing
159     double scale_x, scale_y;
160     // amount of scale_x,y change due to fix_glyph_scaling
161     // scale_fix = before / after
162     double scale_fix;
163     int border_style;
164     double border_x, border_y;
165     double hspacing;
166     int hspacing_scaled;        // 26.6
167     unsigned italic;
168     unsigned bold;
169     int flags;
170
171     int shape_run_id;
172
173     ASS_Vector shift;
174     ASS_Vector bitmap_advance;
175     BitmapHashValue *image, *image_o;
176
177     // next glyph in this cluster
178     struct glyph_info *next;
179 } GlyphInfo;
180
181 typedef struct {
182     double asc, desc;
183     int offset, len;
184 } LineInfo;
185
186 typedef struct {
187     GlyphInfo *glyphs;
188     int length;
189     LineInfo *lines;
190     int n_lines;
191     CombinedBitmapInfo *combined_bitmaps;
192     unsigned n_bitmaps;
193     double height;
194     int max_glyphs;
195     int max_lines;
196     unsigned max_bitmaps;
197 } TextInfo;
198
199 // Renderer state.
200 // Values like current font face, color, screen position, clipping and so on are stored here.
201 typedef struct {
202     ASS_Event *event;
203     ASS_Style *style;
204     int parsed_tags;
205
206     ASS_Font *font;
207     double font_size;
208     int flags;                  // decoration flags (underline/strike-through)
209
210     int alignment;              // alignment overrides go here; if zero, style value will be used
211     int justify;                // justify instructions
212     double frx, fry, frz;
213     double fax, fay;            // text shearing
214     enum {
215         EVENT_NORMAL,           // "normal" top-, sub- or mid- title
216         EVENT_POSITIONED,       // happens after pos(,), margins are ignored
217         EVENT_HSCROLL,          // "Banner" transition effect, text_width is unlimited
218         EVENT_VSCROLL           // "Scroll up", "Scroll down" transition effects
219     } evt_type;
220     double pos_x, pos_y;        // position
221     double org_x, org_y;        // origin
222     char have_origin;           // origin is explicitly defined; if 0, get_base_point() is used
223     double scale_x, scale_y;
224     double hspacing;            // distance between letters, in pixels
225     int border_style;
226     double border_x;            // outline width
227     double border_y;
228     uint32_t c[4];              // colors(Primary, Secondary, so on) in RGBA
229     int clip_x0, clip_y0, clip_x1, clip_y1;
230     char clip_mode;             // 1 = iclip
231     char detect_collisions;
232     int fade;                   // alpha from \fad
233     char be;                    // blur edges
234     double blur;                // gaussian blur
235     double shadow_x;
236     double shadow_y;
237     int drawing_scale;          // currently reading: regular text if 0, drawing otherwise
238     double pbo;                 // drawing baseline offset
239     char *clip_drawing_text;
240     int clip_drawing_scale;
241     int clip_drawing_mode;      // 0 = regular clip, 1 = inverse clip
242
243     Effect effect_type;
244     int effect_timing;
245     int effect_skip_timing;
246
247     enum {
248         SCROLL_LR,              // left-to-right
249         SCROLL_RL,
250         SCROLL_TB,              // top-to-bottom
251         SCROLL_BT
252     } scroll_direction;         // for EVENT_HSCROLL, EVENT_VSCROLL
253     int scroll_shift;
254
255     // face properties
256     char *family;
257     unsigned bold;
258     unsigned italic;
259     int treat_family_as_pattern;
260     int wrap_style;
261     int font_encoding;
262
263     // combination of ASS_OVERRIDE_BIT_* flags that apply right now
264     unsigned overrides;
265     // whether to apply font_scale
266     int apply_font_scale;
267     // whether this is assumed to be explicitly positioned
268     int explicit;
269
270     // used to store RenderContext.style when doing selective style overrides
271     ASS_Style override_style_temp_storage;
272 } RenderContext;
273
274 typedef struct {
275     Cache *font_cache;
276     Cache *outline_cache;
277     Cache *bitmap_cache;
278     Cache *composite_cache;
279     size_t glyph_max;
280     size_t bitmap_max_size;
281     size_t composite_max_size;
282 } CacheStore;
283
284 #include "ass_shaper.h"
285
286 struct ass_renderer {
287     ASS_Library *library;
288     FT_Library ftlibrary;
289     ASS_FontSelector *fontselect;
290     ASS_Settings settings;
291     int render_id;
292     ASS_Shaper *shaper;
293
294     ASS_Image *images_root;     // rendering result is stored here
295     ASS_Image *prev_images_root;
296
297     EventImages *eimg;          // temporary buffer for sorting rendered events
298     int eimg_size;              // allocated buffer size
299
300     // frame-global data
301     int width, height;          // screen dimensions
302     int orig_height;            // frame height ( = screen height - margins )
303     int orig_width;             // frame width ( = screen width - margins )
304     int orig_height_nocrop;     // frame height ( = screen height - margins + cropheight)
305     int orig_width_nocrop;      // frame width ( = screen width - margins + cropwidth)
306     ASS_Track *track;
307     long long time;             // frame's timestamp, ms
308     double font_scale;
309     double font_scale_x;        // x scale applied to all glyphs to preserve text aspect ratio
310     double border_scale;
311     double blur_scale;
312
313     RenderContext state;
314     TextInfo text_info;
315     CacheStore cache;
316
317     const BitmapEngine *engine;
318     RasterizerData rasterizer;
319
320     ASS_Style user_override_style;
321 };
322
323 typedef struct render_priv {
324     int top, height, left, width;
325     int render_id;
326 } RenderPriv;
327
328 typedef struct {
329     int x0;
330     int y0;
331     int x1;
332     int y1;
333 } Rect;
334
335 typedef struct {
336     int a, b;                   // top and height
337     int ha, hb;                 // left and width
338 } Segment;
339
340 void reset_render_context(ASS_Renderer *render_priv, ASS_Style *style);
341 void ass_frame_ref(ASS_Image *img);
342 void ass_frame_unref(ASS_Image *img);
343
344 // XXX: this is actually in ass.c, includes should be fixed later on
345 void ass_lazy_track_init(ASS_Library *lib, ASS_Track *track);
346
347 #endif /* LIBASS_RENDER_H */