Remove unnecessary linking hack, compilation works fine without.
[mplayer/glamo.git] / libass / ass_mp.c
blob40da63c348e07449966fe746d2b0f8d5c09e915e
1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*-
2 // vim:ts=8:sw=8:noet:ai:
3 /*
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.
23 #include <inttypes.h>
24 #include <string.h>
25 #include <stdlib.h>
27 #include "mp_msg.h"
28 #include "get_path.h"
30 #include "ass.h"
31 #include "ass_utils.h"
32 #include "ass_mp.h"
33 #include "ass_library.h"
35 #ifdef CONFIG_FONTCONFIG
36 #include <fontconfig/fontconfig.h>
37 #endif
39 // libass-related command line options
40 ass_library_t* ass_library;
41 int ass_enabled = 0;
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;
48 #else
49 int extract_embedded_fonts = 0;
50 #endif
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;
60 #else
61 static int font_fontconfig = -1;
62 #endif
63 extern char* font_name;
64 extern char* sub_font_name;
65 extern float text_font_scale_factor;
66 extern int subtitle_autoscale;
68 #ifdef CONFIG_ICONV
69 extern char* sub_cp;
70 #else
71 static char* sub_cp = 0;
72 #endif
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;
80 track->Timer = 100.;
81 track->PlayResY = 288;
82 track->WrapStyle = 0;
84 if (ass_styles_file)
85 ass_read_styles(track, ass_styles_file, sub_cp);
87 if (track->n_styles == 0) {
88 ass_style_t* style;
89 int sid;
90 double fs;
91 uint32_t c1, c2;
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)
101 fs *= 1.3;
102 else if (subtitle_autoscale == 3)
103 fs *= 1.4;
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;
117 style->Outline = 2;
118 style->MarginL = 10;
119 style->MarginR = 10;
120 style->MarginV = 5;
121 style->ScaleX = 1.;
122 style->ScaleY = 1.;
125 process_force_style(track);
126 return track;
129 static int check_duplicate_plaintext_event(ass_track_t* track)
131 int i;
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)
138 return 1;
139 return 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
146 * \return event id
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)
152 int eid;
153 ass_event_t* event;
154 int len = 0, j;
155 char* p;
156 char* end;
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;
163 event->Style = 0;
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
169 len += 6; // {\anX}
170 len += 1; // '\0'
172 event->Text = malloc(len);
173 end = event->Text + len;
174 p = event->Text;
176 if (sub->alignment)
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"
183 *p = 0;
185 if (check_duplicate_plaintext_event(track)) {
186 ass_free_event(track, eid);
187 track->n_events--;
188 return -1;
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);
194 return eid;
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) {
205 ass_track_t* track;
206 int i;
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);
213 if (eid < 0)
214 continue;
215 if (!subdata->sub_uses_time) {
216 track->events[eid].Start *= 100. / fps;
217 track->events[eid].Duration *= 100. / fps;
220 return track;
223 void ass_configure(ass_renderer_t* priv, int w, int h, int unscaled) {
224 int hinting;
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))
230 hinting = 0;
231 else
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);
245 else family = 0;
247 if (font_fontconfig >= 0)
248 ass_set_fonts(priv, path, family);
249 else
250 ass_set_fonts_nofc(priv, path, family);
252 free(dir);
253 free(path);
254 free(family);
257 ass_library_t* ass_init(void) {
258 ass_library_t* priv;
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);
264 free(path);
265 return priv;
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);