drawing: scale pbo according to drawing scale
[libass.git] / libass / ass_render.h
blob45d19bbdbe2c671deaeacda31505f03ada49bea7
1 /*
2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
3 * Copyright (C) 2009 Grigori Goronzy <greg@geekmind.org>
5 * This file is part of libass.
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.
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.
20 #ifndef LIBASS_RENDER_H
21 #define LIBASS_RENDER_H
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
30 // XXX: fix the inclusion mess so we can avoid doing this here
31 typedef struct ass_shaper ASS_Shaper;
33 #include "ass.h"
34 #include "ass_font.h"
35 #include "ass_bitmap.h"
36 #include "ass_cache.h"
37 #include "ass_utils.h"
38 #include "ass_fontconfig.h"
39 #include "ass_library.h"
40 #include "ass_drawing.h"
42 #define GLYPH_CACHE_MAX 1000
43 #define BITMAP_CACHE_MAX_SIZE 30 * 1048576
45 #define PARSED_FADE (1<<0)
46 #define PARSED_A (1<<1)
48 typedef struct {
49 double xMin;
50 double xMax;
51 double yMin;
52 double yMax;
53 } DBBox;
55 typedef struct {
56 double x;
57 double y;
58 } DVector;
60 typedef struct free_list {
61 void *object;
62 struct free_list *next;
63 } FreeList;
65 typedef struct {
66 int frame_width;
67 int frame_height;
68 double font_size_coeff; // font size multiplier
69 double line_spacing; // additional line spacing (in frame pixels)
70 double line_position; // vertical position for subtitles, 0-100 (0 = no change)
71 int top_margin; // height of top margin. Everything except toptitles is shifted down by top_margin.
72 int bottom_margin; // height of bottom margin. (frame_height - top_margin - bottom_margin) is original video height.
73 int left_margin;
74 int right_margin;
75 int use_margins; // 0 - place all subtitles inside original frame
76 // 1 - use margins for placing toptitles and subtitles
77 double aspect; // frame aspect ratio, d_width / d_height.
78 double storage_aspect; // pixel ratio of the source image
79 ASS_Hinting hinting;
80 ASS_ShapingLevel shaper;
82 char *default_font;
83 char *default_family;
84 } ASS_Settings;
86 // a rendered event
87 typedef struct {
88 ASS_Image *imgs;
89 int top, height, left, width;
90 int detect_collisions;
91 int shift_direction;
92 ASS_Event *event;
93 } EventImages;
95 typedef enum {
96 EF_NONE = 0,
97 EF_KARAOKE,
98 EF_KARAOKE_KF,
99 EF_KARAOKE_KO
100 } Effect;
102 // describes a glyph
103 // GlyphInfo and TextInfo are used for text centering and word-wrapping operations
104 typedef struct glyph_info {
105 unsigned symbol;
106 unsigned skip; // skip glyph when layouting text
107 ASS_Font *font;
108 int face_index;
109 int glyph_index;
110 double font_size;
111 ASS_Drawing *drawing;
112 FT_Outline *outline;
113 FT_Outline *border;
114 Bitmap *bm; // glyph bitmap
115 Bitmap *bm_o; // outline bitmap
116 Bitmap *bm_s; // shadow bitmap
117 FT_BBox bbox;
118 FT_Vector pos;
119 FT_Vector offset;
120 char linebreak; // the first (leading) glyph of some line ?
121 uint32_t c[4]; // colors
122 FT_Vector advance; // 26.6
123 FT_Vector cluster_advance;
124 Effect effect_type;
125 int effect_timing; // time duration of current karaoke word
126 // after process_karaoke_effects: distance in pixels from the glyph origin.
127 // part of the glyph to the left of it is displayed in a different color.
128 int effect_skip_timing; // delay after the end of last karaoke word
129 int asc, desc; // font max ascender and descender
130 int be; // blur edges
131 double blur; // gaussian blur
132 double shadow_x;
133 double shadow_y;
134 double frx, fry, frz; // rotation
135 double fax, fay; // text shearing
136 double scale_x, scale_y;
137 int border_style;
138 double border_x, border_y;
139 double hspacing;
140 unsigned italic;
141 unsigned bold;
142 int flags;
144 int bm_run_id;
145 int shape_run_id;
147 BitmapHashKey hash_key;
149 // next glyph in this cluster
150 struct glyph_info *next;
151 } GlyphInfo;
153 typedef struct {
154 double asc, desc;
155 int offset, len;
156 } LineInfo;
158 typedef struct {
159 GlyphInfo *glyphs;
160 int length;
161 LineInfo *lines;
162 int n_lines;
163 double height;
164 int max_glyphs;
165 int max_lines;
166 } TextInfo;
168 // Renderer state.
169 // Values like current font face, color, screen position, clipping and so on are stored here.
170 typedef struct {
171 ASS_Event *event;
172 ASS_Style *style;
173 int parsed_tags;
175 ASS_Font *font;
176 double font_size;
177 int flags; // decoration flags (underline/strike-through)
179 FT_Stroker stroker;
180 int stroker_radius; // last stroker radius, for caching stroker objects
181 int alignment; // alignment overrides go here; if zero, style value will be used
182 double frx, fry, frz;
183 double fax, fay; // text shearing
184 enum {
185 EVENT_NORMAL, // "normal" top-, sub- or mid- title
186 EVENT_POSITIONED, // happens after pos(,), margins are ignored
187 EVENT_HSCROLL, // "Banner" transition effect, text_width is unlimited
188 EVENT_VSCROLL // "Scroll up", "Scroll down" transition effects
189 } evt_type;
190 double pos_x, pos_y; // position
191 double org_x, org_y; // origin
192 char have_origin; // origin is explicitly defined; if 0, get_base_point() is used
193 double scale_x, scale_y;
194 double hspacing; // distance between letters, in pixels
195 int border_style;
196 double border_x; // outline width
197 double border_y;
198 uint32_t c[4]; // colors(Primary, Secondary, so on) in RGBA
199 int clip_x0, clip_y0, clip_x1, clip_y1;
200 char clip_mode; // 1 = iclip
201 char detect_collisions;
202 uint32_t fade; // alpha from \fad
203 char be; // blur edges
204 double blur; // gaussian blur
205 double shadow_x;
206 double shadow_y;
207 int drawing_mode; // not implemented; when != 0 text is discarded, except for style override tags
208 ASS_Drawing *drawing; // current drawing
209 ASS_Drawing *clip_drawing; // clip vector
210 int clip_drawing_mode; // 0 = regular clip, 1 = inverse clip
212 Effect effect_type;
213 int effect_timing;
214 int effect_skip_timing;
216 // bitmap run id (used for final bitmap rendering)
217 int bm_run_id;
219 enum {
220 SCROLL_LR, // left-to-right
221 SCROLL_RL,
222 SCROLL_TB, // top-to-bottom
223 SCROLL_BT
224 } scroll_direction; // for EVENT_HSCROLL, EVENT_VSCROLL
225 int scroll_shift;
227 // face properties
228 char *family;
229 unsigned bold;
230 unsigned italic;
231 int treat_family_as_pattern;
232 int wrap_style;
233 int font_encoding;
234 } RenderContext;
236 typedef struct {
237 Cache *font_cache;
238 Cache *outline_cache;
239 Cache *bitmap_cache;
240 Cache *composite_cache;
241 size_t glyph_max;
242 size_t bitmap_max_size;
243 } CacheStore;
245 struct ass_renderer {
246 ASS_Library *library;
247 FT_Library ftlibrary;
248 FCInstance *fontconfig_priv;
249 ASS_Settings settings;
250 int render_id;
251 ASS_SynthPriv *synth_priv;
252 ASS_Shaper *shaper;
254 ASS_Image *images_root; // rendering result is stored here
255 ASS_Image *prev_images_root;
256 int cache_cleared;
258 EventImages *eimg; // temporary buffer for sorting rendered events
259 int eimg_size; // allocated buffer size
261 // frame-global data
262 int width, height; // screen dimensions
263 int orig_height; // frame height ( = screen height - margins )
264 int orig_width; // frame width ( = screen width - margins )
265 int orig_height_nocrop; // frame height ( = screen height - margins + cropheight)
266 int orig_width_nocrop; // frame width ( = screen width - margins + cropwidth)
267 ASS_Track *track;
268 long long time; // frame's timestamp, ms
269 double font_scale;
270 double font_scale_x; // x scale applied to all glyphs to preserve text aspect ratio
271 double border_scale;
273 RenderContext state;
274 TextInfo text_info;
275 CacheStore cache;
277 FreeList *free_head;
278 FreeList *free_tail;
281 typedef struct render_priv {
282 int top, height, left, width;
283 int render_id;
284 } RenderPriv;
286 typedef struct {
287 int x0;
288 int y0;
289 int x1;
290 int y1;
291 } Rect;
293 typedef struct {
294 int a, b; // top and height
295 int ha, hb; // left and width
296 } Segment;
298 void reset_render_context(ASS_Renderer *render_priv, ASS_Style *style);
299 void ass_free_images(ASS_Image *img);
301 // XXX: this is actually in ass.c, includes should be fixed later on
302 void ass_lazy_track_init(ASS_Library *lib, ASS_Track *track);
304 #endif /* LIBASS_RENDER_H */