1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*-
2 // vim:ts=8:sw=8:noet:ai:
4 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
6 * This file is part of libass.
8 * libass is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * libass is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with libass; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include "ass_utils.h"
33 #include "ass_library.h"
35 #ifdef CONFIG_FONTCONFIG
36 #include <fontconfig/fontconfig.h>
39 // libass-related command line options
40 ass_library_t
* ass_library
;
42 float ass_font_scale
= 1.;
43 float ass_line_spacing
= 0.;
44 int ass_top_margin
= 0;
45 int ass_bottom_margin
= 0;
46 #if defined(FC_VERSION) && (FC_VERSION >= 20402)
47 int extract_embedded_fonts
= 1;
49 int extract_embedded_fonts
= 0;
51 char **ass_force_style_list
= NULL
;
52 int ass_use_margins
= 0;
53 char* ass_color
= NULL
;
54 char* ass_border_color
= NULL
;
55 char* ass_styles_file
= NULL
;
56 int ass_hinting
= ASS_HINTING_NATIVE
+ 4; // native hinting for unscaled osd
58 #ifdef CONFIG_FONTCONFIG
59 extern int font_fontconfig
;
61 static int font_fontconfig
= -1;
63 extern char* font_name
;
64 extern char* sub_font_name
;
65 extern float text_font_scale_factor
;
66 extern int subtitle_autoscale
;
71 static char* sub_cp
= 0;
74 void process_force_style(ass_track_t
* track
);
76 ass_track_t
* ass_default_track(ass_library_t
* library
) {
77 ass_track_t
* track
= ass_new_track(library
);
79 track
->track_type
= TRACK_TYPE_ASS
;
81 track
->PlayResY
= 288;
85 ass_read_styles(track
, ass_styles_file
, sub_cp
);
87 if (track
->n_styles
== 0) {
93 sid
= ass_alloc_style(track
);
94 style
= track
->styles
+ sid
;
95 style
->Name
= strdup("Default");
96 style
->FontName
= (font_fontconfig
>= 0 && sub_font_name
) ? strdup(sub_font_name
) : (font_fontconfig
>= 0 && font_name
) ? strdup(font_name
) : strdup("Sans");
98 fs
= track
->PlayResY
* text_font_scale_factor
/ 100.;
99 // approximate autoscale coefficients
100 if (subtitle_autoscale
== 2)
102 else if (subtitle_autoscale
== 3)
104 style
->FontSize
= fs
;
106 if (ass_color
) c1
= strtoll(ass_color
, NULL
, 16);
107 else c1
= 0xFFFF0000;
108 if (ass_border_color
) c2
= strtoll(ass_border_color
, NULL
, 16);
109 else c2
= 0x00000000;
111 style
->PrimaryColour
= c1
;
112 style
->SecondaryColour
= c1
;
113 style
->OutlineColour
= c2
;
114 style
->BackColour
= 0x00000000;
115 style
->BorderStyle
= 1;
116 style
->Alignment
= 2;
125 process_force_style(track
);
129 static int check_duplicate_plaintext_event(ass_track_t
* track
)
132 ass_event_t
* evt
= track
->events
+ track
->n_events
- 1;
134 for (i
= 0; i
<track
->n_events
- 1; ++i
) // ignoring last event, it is the one we are comparing with
135 if (track
->events
[i
].Start
== evt
->Start
&&
136 track
->events
[i
].Duration
== evt
->Duration
&&
137 strcmp(track
->events
[i
].Text
, evt
->Text
) == 0)
143 * \brief Convert subtitle to ass_event_t for the given track
144 * \param ass_track_t track
145 * \param sub subtitle to convert
147 * note: assumes that subtitle is _not_ fps-based; caller must manually correct
148 * Start and Duration in other case.
150 int ass_process_subtitle(ass_track_t
* track
, subtitle
* sub
)
158 eid
= ass_alloc_event(track
);
159 event
= track
->events
+ eid
;
161 event
->Start
= sub
->start
* 10;
162 event
->Duration
= (sub
->end
- sub
->start
) * 10;
165 for (j
= 0; j
< sub
->lines
; ++j
)
166 len
+= sub
->text
[j
] ? strlen(sub
->text
[j
]) : 0;
168 len
+= 2 * sub
->lines
; // '\N', including the one after the last line
172 event
->Text
= malloc(len
);
173 end
= event
->Text
+ len
;
177 p
+= snprintf(p
, end
- p
, "{\\an%d}", sub
->alignment
);
179 for (j
= 0; j
< sub
->lines
; ++j
)
180 p
+= snprintf(p
, end
- p
, "%s\\N", sub
->text
[j
]);
182 if (sub
->lines
> 0) p
-=2; // remove last "\N"
185 if (check_duplicate_plaintext_event(track
)) {
186 ass_free_event(track
, eid
);
191 mp_msg(MSGT_ASS
, MSGL_V
, "plaintext event at %" PRId64
", +%" PRId64
": %s \n",
192 (int64_t)event
->Start
, (int64_t)event
->Duration
, event
->Text
);
199 * \brief Convert subdata to ass_track
200 * \param subdata subtitles struct from subreader
201 * \param fps video framerate
202 * \return newly allocated ass_track, filled with subtitles from subdata
204 ass_track_t
* ass_read_subdata(ass_library_t
* library
, sub_data
* subdata
, double fps
) {
208 track
= ass_default_track(library
);
209 track
->name
= subdata
->filename
? strdup(subdata
->filename
) : 0;
211 for (i
= 0; i
< subdata
->sub_num
; ++i
) {
212 int eid
= ass_process_subtitle(track
, subdata
->subtitles
+ i
);
215 if (!subdata
->sub_uses_time
) {
216 track
->events
[eid
].Start
*= 100. / fps
;
217 track
->events
[eid
].Duration
*= 100. / fps
;
223 void ass_configure(ass_renderer_t
* priv
, int w
, int h
, int unscaled
) {
225 ass_set_frame_size(priv
, w
, h
);
226 ass_set_margins(priv
, ass_top_margin
, ass_bottom_margin
, 0, 0);
227 ass_set_use_margins(priv
, ass_use_margins
);
228 ass_set_font_scale(priv
, ass_font_scale
);
229 if (!unscaled
&& (ass_hinting
& 4))
232 hinting
= ass_hinting
& 3;
233 ass_set_hinting(priv
, hinting
);
234 ass_set_line_spacing(priv
, ass_line_spacing
);
237 void ass_configure_fonts(ass_renderer_t
* priv
) {
238 char *dir
, *path
, *family
;
239 dir
= get_path("fonts");
240 if (font_fontconfig
< 0 && sub_font_name
) path
= strdup(sub_font_name
);
241 else if (font_fontconfig
< 0 && font_name
) path
= strdup(font_name
);
242 else path
= get_path("subfont.ttf");
243 if (font_fontconfig
>= 0 && sub_font_name
) family
= strdup(sub_font_name
);
244 else if (font_fontconfig
>= 0 && font_name
) family
= strdup(font_name
);
247 if (font_fontconfig
>= 0)
248 ass_set_fonts(priv
, path
, family
);
250 ass_set_fonts_nofc(priv
, path
, family
);
257 ass_library_t
* ass_init(void) {
259 char* path
= get_path("fonts");
260 priv
= ass_library_init();
261 ass_set_fonts_dir(priv
, path
);
262 ass_set_extract_fonts(priv
, extract_embedded_fonts
);
263 ass_set_style_overrides(priv
, ass_force_style_list
);
268 int ass_force_reload
= 0; // flag set if global ass-related settings were changed
270 ass_image_t
* ass_mp_render_frame(ass_renderer_t
*priv
, ass_track_t
* track
, long long now
, int* detect_change
) {
271 if (ass_force_reload
) {
272 ass_set_margins(priv
, ass_top_margin
, ass_bottom_margin
, 0, 0);
273 ass_set_use_margins(priv
, ass_use_margins
);
274 ass_set_font_scale(priv
, ass_font_scale
);
275 ass_force_reload
= 0;
277 return ass_render_frame(priv
, track
, now
, detect_change
);