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