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");
97 style
->treat_fontname_as_pattern
= 1;
99 fs
= track
->PlayResY
* text_font_scale_factor
/ 100.;
100 // approximate autoscale coefficients
101 if (subtitle_autoscale
== 2)
103 else if (subtitle_autoscale
== 3)
105 style
->FontSize
= fs
;
107 if (ass_color
) c1
= strtoll(ass_color
, NULL
, 16);
108 else c1
= 0xFFFF0000;
109 if (ass_border_color
) c2
= strtoll(ass_border_color
, NULL
, 16);
110 else c2
= 0x00000000;
112 style
->PrimaryColour
= c1
;
113 style
->SecondaryColour
= c1
;
114 style
->OutlineColour
= c2
;
115 style
->BackColour
= 0x00000000;
116 style
->BorderStyle
= 1;
117 style
->Alignment
= 2;
126 process_force_style(track
);
130 static int check_duplicate_plaintext_event(ass_track_t
* track
)
133 ass_event_t
* evt
= track
->events
+ track
->n_events
- 1;
135 for (i
= 0; i
<track
->n_events
- 1; ++i
) // ignoring last event, it is the one we are comparing with
136 if (track
->events
[i
].Start
== evt
->Start
&&
137 track
->events
[i
].Duration
== evt
->Duration
&&
138 strcmp(track
->events
[i
].Text
, evt
->Text
) == 0)
144 * \brief Convert subtitle to ass_event_t for the given track
145 * \param ass_track_t track
146 * \param sub subtitle to convert
148 * note: assumes that subtitle is _not_ fps-based; caller must manually correct
149 * Start and Duration in other case.
151 int ass_process_subtitle(ass_track_t
* track
, subtitle
* sub
)
159 eid
= ass_alloc_event(track
);
160 event
= track
->events
+ eid
;
162 event
->Start
= sub
->start
* 10;
163 event
->Duration
= (sub
->end
- sub
->start
) * 10;
166 for (j
= 0; j
< sub
->lines
; ++j
)
167 len
+= sub
->text
[j
] ? strlen(sub
->text
[j
]) : 0;
169 len
+= 2 * sub
->lines
; // '\N', including the one after the last line
173 event
->Text
= malloc(len
);
174 end
= event
->Text
+ len
;
178 p
+= snprintf(p
, end
- p
, "{\\an%d}", sub
->alignment
);
180 for (j
= 0; j
< sub
->lines
; ++j
)
181 p
+= snprintf(p
, end
- p
, "%s\\N", sub
->text
[j
]);
183 if (sub
->lines
> 0) p
-=2; // remove last "\N"
186 if (check_duplicate_plaintext_event(track
)) {
187 ass_free_event(track
, eid
);
192 mp_msg(MSGT_ASS
, MSGL_V
, "plaintext event at %" PRId64
", +%" PRId64
": %s \n",
193 (int64_t)event
->Start
, (int64_t)event
->Duration
, event
->Text
);
200 * \brief Convert subdata to ass_track
201 * \param subdata subtitles struct from subreader
202 * \param fps video framerate
203 * \return newly allocated ass_track, filled with subtitles from subdata
205 ass_track_t
* ass_read_subdata(ass_library_t
* library
, sub_data
* subdata
, double fps
) {
209 track
= ass_default_track(library
);
210 track
->name
= subdata
->filename
? strdup(subdata
->filename
) : 0;
212 for (i
= 0; i
< subdata
->sub_num
; ++i
) {
213 int eid
= ass_process_subtitle(track
, subdata
->subtitles
+ i
);
216 if (!subdata
->sub_uses_time
) {
217 track
->events
[eid
].Start
*= 100. / fps
;
218 track
->events
[eid
].Duration
*= 100. / fps
;
224 void ass_configure(ass_renderer_t
* priv
, int w
, int h
, int unscaled
) {
226 ass_set_frame_size(priv
, w
, h
);
227 ass_set_margins(priv
, ass_top_margin
, ass_bottom_margin
, 0, 0);
228 ass_set_use_margins(priv
, ass_use_margins
);
229 ass_set_font_scale(priv
, ass_font_scale
);
230 if (!unscaled
&& (ass_hinting
& 4))
233 hinting
= ass_hinting
& 3;
234 ass_set_hinting(priv
, hinting
);
235 ass_set_line_spacing(priv
, ass_line_spacing
);
238 void ass_configure_fonts(ass_renderer_t
* priv
) {
239 char *dir
, *path
, *family
;
240 dir
= get_path("fonts");
241 if (font_fontconfig
< 0 && sub_font_name
) path
= strdup(sub_font_name
);
242 else if (font_fontconfig
< 0 && font_name
) path
= strdup(font_name
);
243 else path
= get_path("subfont.ttf");
244 if (font_fontconfig
>= 0 && sub_font_name
) family
= strdup(sub_font_name
);
245 else if (font_fontconfig
>= 0 && font_name
) family
= strdup(font_name
);
248 if (font_fontconfig
>= 0)
249 ass_set_fonts(priv
, path
, family
);
251 ass_set_fonts_nofc(priv
, path
, family
);
258 ass_library_t
* ass_init(void) {
260 char* path
= get_path("fonts");
261 priv
= ass_library_init();
262 ass_set_fonts_dir(priv
, path
);
263 ass_set_extract_fonts(priv
, extract_embedded_fonts
);
264 ass_set_style_overrides(priv
, ass_force_style_list
);
269 int ass_force_reload
= 0; // flag set if global ass-related settings were changed
271 ass_image_t
* ass_mp_render_frame(ass_renderer_t
*priv
, ass_track_t
* track
, long long now
, int* detect_change
) {
272 if (ass_force_reload
) {
273 ass_set_margins(priv
, ass_top_margin
, ass_bottom_margin
, 0, 0);
274 ass_set_use_margins(priv
, ass_use_margins
);
275 ass_set_font_scale(priv
, ass_font_scale
);
276 ass_force_reload
= 0;
278 return ass_render_frame(priv
, track
, now
, detect_change
);