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