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 program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "ass_utils.h"
30 #include "ass_library.h"
32 // libass-related command line options
33 ass_library_t
* ass_library
;
35 float ass_font_scale
= 1.;
36 float ass_line_spacing
= 0.;
37 int ass_top_margin
= 0;
38 int ass_bottom_margin
= 0;
39 int extract_embedded_fonts
= 0;
40 char **ass_force_style_list
= NULL
;
41 int ass_use_margins
= 0;
42 char* ass_color
= NULL
;
43 char* ass_border_color
= NULL
;
44 char* ass_styles_file
= NULL
;
46 #ifdef HAVE_FONTCONFIG
47 extern int font_fontconfig
;
49 static int font_fontconfig
= 0;
51 extern char* font_name
;
52 extern float text_font_scale_factor
;
53 extern int subtitle_autoscale
;
58 static char* sub_cp
= 0;
61 extern double ass_internal_font_size_coeff
;
62 extern void process_force_style(ass_track_t
* track
);
64 ass_track_t
* ass_default_track(ass_library_t
* library
) {
65 ass_track_t
* track
= ass_new_track(library
);
67 track
->track_type
= TRACK_TYPE_ASS
;
69 track
->PlayResY
= 288;
73 ass_read_styles(track
, ass_styles_file
, sub_cp
);
75 if (track
->n_styles
== 0) {
81 sid
= ass_alloc_style(track
);
82 style
= track
->styles
+ sid
;
83 style
->Name
= strdup("Default");
84 style
->FontName
= (font_fontconfig
&& font_name
) ? strdup(font_name
) : strdup("Sans");
86 fs
= track
->PlayResY
* text_font_scale_factor
/ 100. / ass_internal_font_size_coeff
;
87 // approximate autoscale coefficients
88 if (subtitle_autoscale
== 2)
90 else if (subtitle_autoscale
== 3)
94 if (ass_color
) c1
= strtoll(ass_color
, NULL
, 16);
96 if (ass_border_color
) c2
= strtoll(ass_border_color
, NULL
, 16);
99 style
->PrimaryColour
= c1
;
100 style
->SecondaryColour
= c1
;
101 style
->OutlineColour
= c2
;
102 style
->BackColour
= 0x00000000;
103 style
->BorderStyle
= 1;
104 style
->Alignment
= 2;
113 process_force_style(track
);
117 static int check_duplicate_plaintext_event(ass_track_t
* track
)
120 ass_event_t
* evt
= track
->events
+ track
->n_events
- 1;
122 for (i
= 0; i
<track
->n_events
- 1; ++i
) // ignoring last event, it is the one we are comparing with
123 if (track
->events
[i
].Start
== evt
->Start
&&
124 track
->events
[i
].Duration
== evt
->Duration
&&
125 strcmp(track
->events
[i
].Text
, evt
->Text
) == 0)
131 * \brief Convert subtitle to ass_event_t for the given track
132 * \param ass_track_t track
133 * \param sub subtitle to convert
135 * note: assumes that subtitle is _not_ fps-based; caller must manually correct
136 * Start and Duration in other case.
138 int ass_process_subtitle(ass_track_t
* track
, subtitle
* sub
)
146 eid
= ass_alloc_event(track
);
147 event
= track
->events
+ eid
;
149 event
->Start
= sub
->start
* 10;
150 event
->Duration
= (sub
->end
- sub
->start
) * 10;
153 for (j
= 0; j
< sub
->lines
; ++j
)
154 len
+= sub
->text
[j
] ? strlen(sub
->text
[j
]) : 0;
156 len
+= 2 * sub
->lines
; // '\N', including the one after the last line
160 event
->Text
= malloc(len
);
161 end
= event
->Text
+ len
;
165 p
+= snprintf(p
, end
- p
, "{\\an%d}", sub
->alignment
);
167 for (j
= 0; j
< sub
->lines
; ++j
)
168 p
+= snprintf(p
, end
- p
, "%s\\N", sub
->text
[j
]);
170 p
-=2; // remove last ' '
173 if (check_duplicate_plaintext_event(track
)) {
174 ass_free_event(track
, eid
);
179 mp_msg(MSGT_ASS
, MSGL_V
, "plaintext event at %" PRId64
", +%" PRId64
": %s \n",
180 (int64_t)event
->Start
, (int64_t)event
->Duration
, event
->Text
);
187 * \brief Convert subdata to ass_track
188 * \param subdata subtitles struct from subreader
189 * \param fps video framerate
190 * \return newly allocated ass_track, filled with subtitles from subdata
192 ass_track_t
* ass_read_subdata(ass_library_t
* library
, sub_data
* subdata
, double fps
) {
196 track
= ass_default_track(library
);
197 track
->name
= subdata
->filename
? strdup(subdata
->filename
) : 0;
199 for (i
= 0; i
< subdata
->sub_num
; ++i
) {
200 int eid
= ass_process_subtitle(track
, subdata
->subtitles
+ i
);
203 if (!subdata
->sub_uses_time
) {
204 track
->events
[eid
].Start
*= 100. / fps
;
205 track
->events
[eid
].Duration
*= 100. / fps
;
211 char *get_path(char *);
213 void ass_configure(ass_renderer_t
* priv
, int w
, int h
) {
214 ass_set_frame_size(priv
, w
, h
);
215 ass_set_margins(priv
, ass_top_margin
, ass_bottom_margin
, 0, 0);
216 ass_set_use_margins(priv
, ass_use_margins
);
217 ass_set_font_scale(priv
, ass_font_scale
);
220 void ass_configure_fonts(ass_renderer_t
* priv
) {
221 char *dir
, *path
, *family
;
222 dir
= get_path("fonts");
223 if (!font_fontconfig
&& font_name
) path
= strdup(font_name
);
224 else path
= get_path("subfont.ttf");
225 if (font_fontconfig
&& font_name
) family
= strdup(font_name
);
228 ass_set_fonts(priv
, path
, family
);
235 ass_library_t
* ass_init() {
237 char* path
= get_path("fonts");
238 priv
= ass_library_init();
239 ass_set_fonts_dir(priv
, path
);
240 ass_set_extract_fonts(priv
, extract_embedded_fonts
);
241 ass_set_style_overrides(priv
, ass_force_style_list
);