Relicense to ISC
[libass.git] / libass / ass_render.h
blob89bffb0127ad41a7cbf9ee4d2a64aaae231bfa90
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 #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"
39 #define GLYPH_CACHE_MAX 1000
40 #define BITMAP_CACHE_MAX_SIZE 30 * 1048576
42 typedef struct {
43 double xMin;
44 double xMax;
45 double yMin;
46 double yMax;
47 } DBBox;
49 typedef struct {
50 double x;
51 double y;
52 } DVector;
54 typedef struct free_list {
55 void *object;
56 struct free_list *next;
57 } FreeList;
59 typedef struct {
60 int frame_width;
61 int frame_height;
62 double font_size_coeff; // font size multiplier
63 double line_spacing; // additional line spacing (in frame pixels)
64 int top_margin; // height of top margin. Everything except toptitles is shifted down by top_margin.
65 int bottom_margin; // height of bottom margin. (frame_height - top_margin - bottom_margin) is original video height.
66 int left_margin;
67 int right_margin;
68 int use_margins; // 0 - place all subtitles inside original frame
69 // 1 - use margins for placing toptitles and subtitles
70 double aspect; // frame aspect ratio, d_width / d_height.
71 double storage_aspect; // pixel ratio of the source image
72 ASS_Hinting hinting;
74 char *default_font;
75 char *default_family;
76 } ASS_Settings;
78 // a rendered event
79 typedef struct {
80 ASS_Image *imgs;
81 int top, height, left, width;
82 int detect_collisions;
83 int shift_direction;
84 ASS_Event *event;
85 } EventImages;
87 typedef enum {
88 EF_NONE = 0,
89 EF_KARAOKE,
90 EF_KARAOKE_KF,
91 EF_KARAOKE_KO
92 } Effect;
94 // describes a glyph
95 // GlyphInfo and TextInfo are used for text centering and word-wrapping operations
96 typedef struct {
97 unsigned symbol;
98 unsigned skip; // skip glyph when layouting text
99 FT_Glyph glyph;
100 FT_Glyph outline_glyph;
101 Bitmap *bm; // glyph bitmap
102 Bitmap *bm_o; // outline bitmap
103 Bitmap *bm_s; // shadow bitmap
104 FT_BBox bbox;
105 FT_Vector pos;
106 char linebreak; // the first (leading) glyph of some line ?
107 uint32_t c[4]; // colors
108 FT_Vector advance; // 26.6
109 Effect effect_type;
110 int effect_timing; // time duration of current karaoke word
111 // after process_karaoke_effects: distance in pixels from the glyph origin.
112 // part of the glyph to the left of it is displayed in a different color.
113 int effect_skip_timing; // delay after the end of last karaoke word
114 int asc, desc; // font max ascender and descender
115 int be; // blur edges
116 double blur; // gaussian blur
117 double shadow_x;
118 double shadow_y;
119 double frx, fry, frz; // rotation
120 double fax, fay; // text shearing
122 BitmapHashKey hash_key;
123 } GlyphInfo;
125 typedef struct {
126 double asc, desc;
127 } LineInfo;
129 typedef struct {
130 GlyphInfo *glyphs;
131 int length;
132 LineInfo *lines;
133 int n_lines;
134 double height;
135 int max_glyphs;
136 int max_lines;
137 } TextInfo;
139 // Renderer state.
140 // Values like current font face, color, screen position, clipping and so on are stored here.
141 typedef struct {
142 ASS_Event *event;
143 ASS_Style *style;
145 ASS_Font *font;
146 char *font_path;
147 double font_size;
148 int flags; // decoration flags (underline/strike-through)
150 FT_Stroker stroker;
151 int alignment; // alignment overrides go here; if zero, style value will be used
152 double frx, fry, frz;
153 double fax, fay; // text shearing
154 enum {
155 EVENT_NORMAL, // "normal" top-, sub- or mid- title
156 EVENT_POSITIONED, // happens after pos(,), margins are ignored
157 EVENT_HSCROLL, // "Banner" transition effect, text_width is unlimited
158 EVENT_VSCROLL // "Scroll up", "Scroll down" transition effects
159 } evt_type;
160 double pos_x, pos_y; // position
161 double org_x, org_y; // origin
162 char have_origin; // origin is explicitly defined; if 0, get_base_point() is used
163 double scale_x, scale_y;
164 double hspacing; // distance between letters, in pixels
165 double border_x; // outline width
166 double border_y;
167 uint32_t c[4]; // colors(Primary, Secondary, so on) in RGBA
168 int clip_x0, clip_y0, clip_x1, clip_y1;
169 char clip_mode; // 1 = iclip
170 char detect_collisions;
171 uint32_t fade; // alpha from \fad
172 char be; // blur edges
173 double blur; // gaussian blur
174 double shadow_x;
175 double shadow_y;
176 int drawing_mode; // not implemented; when != 0 text is discarded, except for style override tags
177 ASS_Drawing *drawing; // current drawing
178 ASS_Drawing *clip_drawing; // clip vector
179 int clip_drawing_mode; // 0 = regular clip, 1 = inverse clip
181 Effect effect_type;
182 int effect_timing;
183 int effect_skip_timing;
185 enum {
186 SCROLL_LR, // left-to-right
187 SCROLL_RL,
188 SCROLL_TB, // top-to-bottom
189 SCROLL_BT
190 } scroll_direction; // for EVENT_HSCROLL, EVENT_VSCROLL
191 int scroll_shift;
193 // face properties
194 char *family;
195 unsigned bold;
196 unsigned italic;
197 int treat_family_as_pattern;
198 int wrap_style;
199 } RenderContext;
201 typedef struct {
202 Hashmap *font_cache;
203 Hashmap *glyph_cache;
204 Hashmap *bitmap_cache;
205 Hashmap *composite_cache;
206 size_t glyph_max;
207 size_t bitmap_max_size;
208 } CacheStore;
210 struct ass_renderer {
211 ASS_Library *library;
212 FT_Library ftlibrary;
213 FCInstance *fontconfig_priv;
214 ASS_Settings settings;
215 int render_id;
216 ASS_SynthPriv *synth_priv;
218 ASS_Image *images_root; // rendering result is stored here
219 ASS_Image *prev_images_root;
221 EventImages *eimg; // temporary buffer for sorting rendered events
222 int eimg_size; // allocated buffer size
224 // frame-global data
225 int width, height; // screen dimensions
226 int orig_height; // frame height ( = screen height - margins )
227 int orig_width; // frame width ( = screen width - margins )
228 int orig_height_nocrop; // frame height ( = screen height - margins + cropheight)
229 int orig_width_nocrop; // frame width ( = screen width - margins + cropwidth)
230 ASS_Track *track;
231 long long time; // frame's timestamp, ms
232 double font_scale;
233 double font_scale_x; // x scale applied to all glyphs to preserve text aspect ratio
234 double border_scale;
236 RenderContext state;
237 TextInfo text_info;
238 CacheStore cache;
240 FreeList *free_head;
241 FreeList *free_tail;
244 typedef struct render_priv {
245 int top, height, left, width;
246 int render_id;
247 } RenderPriv;
249 typedef struct {
250 int x0;
251 int y0;
252 int x1;
253 int y1;
254 } Rect;
256 typedef struct {
257 int a, b; // top and height
258 int ha, hb; // left and width
259 } Segment;
261 void reset_render_context(ASS_Renderer *render_priv);
262 void ass_free_images(ASS_Image *img);
264 #endif /* LIBASS_RENDER_H */