2006-10-01 17:09:04 +00:00
|
|
|
// -*- c-basic-offset: 8; indent-tabs-mode: t -*-
|
|
|
|
// vim:ts=8:sw=8:noet:ai:
|
|
|
|
/*
|
|
|
|
Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2006-08-15 22:46:56 +00:00
|
|
|
#include <inttypes.h>
|
2006-08-15 19:25:46 +00:00
|
|
|
#include <string.h>
|
2006-08-21 22:02:49 +00:00
|
|
|
#include <stdlib.h>
|
2006-08-15 19:25:46 +00:00
|
|
|
|
2006-09-02 20:46:26 +00:00
|
|
|
#include "mp_msg.h"
|
2007-08-28 11:20:24 +00:00
|
|
|
#include "get_path.h"
|
2006-09-02 20:46:26 +00:00
|
|
|
|
2006-08-15 19:25:46 +00:00
|
|
|
#include "ass.h"
|
2006-09-03 12:18:49 +00:00
|
|
|
#include "ass_utils.h"
|
2006-07-07 18:26:51 +00:00
|
|
|
#include "ass_mp.h"
|
2006-10-28 15:07:18 +00:00
|
|
|
#include "ass_library.h"
|
2006-07-07 18:26:51 +00:00
|
|
|
|
2006-12-17 12:35:12 +00:00
|
|
|
#ifdef HAVE_FONTCONFIG
|
|
|
|
#include <fontconfig/fontconfig.h>
|
|
|
|
#endif
|
|
|
|
|
2006-07-07 18:26:51 +00:00
|
|
|
// libass-related command line options
|
2006-10-28 15:07:18 +00:00
|
|
|
ass_library_t* ass_library;
|
2006-07-07 18:26:51 +00:00
|
|
|
int ass_enabled = 0;
|
|
|
|
float ass_font_scale = 1.;
|
|
|
|
float ass_line_spacing = 0.;
|
|
|
|
int ass_top_margin = 0;
|
|
|
|
int ass_bottom_margin = 0;
|
2006-12-17 12:35:12 +00:00
|
|
|
#if defined(FC_VERSION) && (FC_VERSION >= 20402)
|
|
|
|
int extract_embedded_fonts = 1;
|
|
|
|
#else
|
2006-07-07 18:26:51 +00:00
|
|
|
int extract_embedded_fonts = 0;
|
2006-12-17 12:35:12 +00:00
|
|
|
#endif
|
2006-08-22 23:20:05 +00:00
|
|
|
char **ass_force_style_list = NULL;
|
2006-08-28 15:12:45 +00:00
|
|
|
int ass_use_margins = 0;
|
2006-09-03 12:18:49 +00:00
|
|
|
char* ass_color = NULL;
|
|
|
|
char* ass_border_color = NULL;
|
2006-09-03 17:42:31 +00:00
|
|
|
char* ass_styles_file = NULL;
|
2007-04-27 14:25:36 +00:00
|
|
|
int ass_hinting = ASS_HINTING_NATIVE + 4; // native hinting for unscaled osd
|
2006-07-07 18:26:51 +00:00
|
|
|
|
2006-10-28 10:21:30 +00:00
|
|
|
#ifdef HAVE_FONTCONFIG
|
2006-08-15 19:25:46 +00:00
|
|
|
extern int font_fontconfig;
|
2006-10-28 10:21:30 +00:00
|
|
|
#else
|
2007-12-12 10:00:34 +00:00
|
|
|
static int font_fontconfig = -1;
|
2006-10-28 10:21:30 +00:00
|
|
|
#endif
|
2006-08-15 19:25:46 +00:00
|
|
|
extern char* font_name;
|
2007-12-16 11:13:24 +00:00
|
|
|
extern char* sub_font_name;
|
2006-08-15 19:25:46 +00:00
|
|
|
extern float text_font_scale_factor;
|
|
|
|
extern int subtitle_autoscale;
|
|
|
|
|
2006-10-28 15:07:18 +00:00
|
|
|
#ifdef USE_ICONV
|
|
|
|
extern char* sub_cp;
|
|
|
|
#else
|
|
|
|
static char* sub_cp = 0;
|
|
|
|
#endif
|
|
|
|
|
2006-08-29 10:35:58 +00:00
|
|
|
extern void process_force_style(ass_track_t* track);
|
2006-08-15 19:25:46 +00:00
|
|
|
|
2006-10-28 15:07:18 +00:00
|
|
|
ass_track_t* ass_default_track(ass_library_t* library) {
|
|
|
|
ass_track_t* track = ass_new_track(library);
|
2006-08-15 19:25:46 +00:00
|
|
|
|
|
|
|
track->track_type = TRACK_TYPE_ASS;
|
|
|
|
track->Timer = 100.;
|
|
|
|
track->PlayResY = 288;
|
|
|
|
track->WrapStyle = 0;
|
|
|
|
|
2006-09-03 17:42:31 +00:00
|
|
|
if (ass_styles_file)
|
2006-10-28 15:07:18 +00:00
|
|
|
ass_read_styles(track, ass_styles_file, sub_cp);
|
2006-09-03 17:42:31 +00:00
|
|
|
|
|
|
|
if (track->n_styles == 0) {
|
2006-09-03 17:44:25 +00:00
|
|
|
ass_style_t* style;
|
|
|
|
int sid;
|
|
|
|
double fs;
|
|
|
|
uint32_t c1, c2;
|
|
|
|
|
|
|
|
sid = ass_alloc_style(track);
|
|
|
|
style = track->styles + sid;
|
|
|
|
style->Name = strdup("Default");
|
2007-12-16 11:13:24 +00:00
|
|
|
style->FontName = (font_fontconfig >= 0 && sub_font_name) ? strdup(sub_font_name) : (font_fontconfig >= 0 && font_name) ? strdup(font_name) : strdup("Sans");
|
2006-09-03 17:44:25 +00:00
|
|
|
|
2007-05-19 14:11:41 +00:00
|
|
|
fs = track->PlayResY * text_font_scale_factor / 100.;
|
2006-09-03 17:44:25 +00:00
|
|
|
// approximate autoscale coefficients
|
|
|
|
if (subtitle_autoscale == 2)
|
|
|
|
fs *= 1.3;
|
|
|
|
else if (subtitle_autoscale == 3)
|
|
|
|
fs *= 1.4;
|
|
|
|
style->FontSize = fs;
|
|
|
|
|
|
|
|
if (ass_color) c1 = strtoll(ass_color, NULL, 16);
|
|
|
|
else c1 = 0xFFFF0000;
|
|
|
|
if (ass_border_color) c2 = strtoll(ass_border_color, NULL, 16);
|
|
|
|
else c2 = 0x00000000;
|
|
|
|
|
|
|
|
style->PrimaryColour = c1;
|
|
|
|
style->SecondaryColour = c1;
|
|
|
|
style->OutlineColour = c2;
|
|
|
|
style->BackColour = 0x00000000;
|
|
|
|
style->BorderStyle = 1;
|
|
|
|
style->Alignment = 2;
|
|
|
|
style->Outline = 2;
|
2006-10-09 19:57:56 +00:00
|
|
|
style->MarginL = 10;
|
|
|
|
style->MarginR = 10;
|
|
|
|
style->MarginV = 5;
|
2006-09-03 17:44:25 +00:00
|
|
|
style->ScaleX = 1.;
|
|
|
|
style->ScaleY = 1.;
|
2006-09-03 17:42:31 +00:00
|
|
|
}
|
2006-08-15 19:25:46 +00:00
|
|
|
|
2006-09-03 17:57:41 +00:00
|
|
|
process_force_style(track);
|
2006-09-02 20:46:26 +00:00
|
|
|
return track;
|
|
|
|
}
|
2006-08-15 19:25:46 +00:00
|
|
|
|
2006-09-02 20:46:26 +00:00
|
|
|
static int check_duplicate_plaintext_event(ass_track_t* track)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
ass_event_t* evt = track->events + track->n_events - 1;
|
|
|
|
|
|
|
|
for (i = 0; i<track->n_events - 1; ++i) // ignoring last event, it is the one we are comparing with
|
|
|
|
if (track->events[i].Start == evt->Start &&
|
|
|
|
track->events[i].Duration == evt->Duration &&
|
|
|
|
strcmp(track->events[i].Text, evt->Text) == 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Convert subtitle to ass_event_t for the given track
|
|
|
|
* \param ass_track_t track
|
|
|
|
* \param sub subtitle to convert
|
|
|
|
* \return event id
|
|
|
|
* note: assumes that subtitle is _not_ fps-based; caller must manually correct
|
|
|
|
* Start and Duration in other case.
|
|
|
|
**/
|
|
|
|
int ass_process_subtitle(ass_track_t* track, subtitle* sub)
|
|
|
|
{
|
|
|
|
int eid;
|
|
|
|
ass_event_t* event;
|
|
|
|
int len = 0, j;
|
|
|
|
char* p;
|
|
|
|
char* end;
|
|
|
|
|
|
|
|
eid = ass_alloc_event(track);
|
|
|
|
event = track->events + eid;
|
|
|
|
|
|
|
|
event->Start = sub->start * 10;
|
|
|
|
event->Duration = (sub->end - sub->start) * 10;
|
|
|
|
event->Style = 0;
|
2006-08-15 19:25:46 +00:00
|
|
|
|
2006-09-03 17:44:25 +00:00
|
|
|
for (j = 0; j < sub->lines; ++j)
|
|
|
|
len += sub->text[j] ? strlen(sub->text[j]) : 0;
|
2006-08-15 19:25:46 +00:00
|
|
|
|
2006-09-03 17:44:25 +00:00
|
|
|
len += 2 * sub->lines; // '\N', including the one after the last line
|
|
|
|
len += 6; // {\anX}
|
|
|
|
len += 1; // '\0'
|
2006-08-15 19:25:46 +00:00
|
|
|
|
2006-09-03 17:44:25 +00:00
|
|
|
event->Text = malloc(len);
|
|
|
|
end = event->Text + len;
|
|
|
|
p = event->Text;
|
2006-08-15 19:25:46 +00:00
|
|
|
|
2006-09-03 17:44:25 +00:00
|
|
|
if (sub->alignment)
|
|
|
|
p += snprintf(p, end - p, "{\\an%d}", sub->alignment);
|
2006-08-15 19:25:46 +00:00
|
|
|
|
2006-09-03 17:44:25 +00:00
|
|
|
for (j = 0; j < sub->lines; ++j)
|
2006-10-29 13:52:51 +00:00
|
|
|
p += snprintf(p, end - p, "%s\\N", sub->text[j]);
|
2006-08-15 19:25:46 +00:00
|
|
|
|
2007-01-07 16:07:45 +00:00
|
|
|
if (sub->lines > 0) p-=2; // remove last "\N"
|
2006-09-03 17:44:25 +00:00
|
|
|
*p = 0;
|
2006-09-02 20:46:26 +00:00
|
|
|
|
|
|
|
if (check_duplicate_plaintext_event(track)) {
|
|
|
|
ass_free_event(track, eid);
|
|
|
|
track->n_events--;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-11-03 18:15:33 +00:00
|
|
|
mp_msg(MSGT_ASS, MSGL_V, "plaintext event at %" PRId64 ", +%" PRId64 ": %s \n",
|
2006-09-02 20:46:26 +00:00
|
|
|
(int64_t)event->Start, (int64_t)event->Duration, event->Text);
|
|
|
|
|
|
|
|
return eid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Convert subdata to ass_track
|
|
|
|
* \param subdata subtitles struct from subreader
|
|
|
|
* \param fps video framerate
|
|
|
|
* \return newly allocated ass_track, filled with subtitles from subdata
|
|
|
|
*/
|
2006-10-28 15:07:18 +00:00
|
|
|
ass_track_t* ass_read_subdata(ass_library_t* library, sub_data* subdata, double fps) {
|
2006-09-02 20:46:26 +00:00
|
|
|
ass_track_t* track;
|
|
|
|
int i;
|
|
|
|
|
2006-10-28 15:07:18 +00:00
|
|
|
track = ass_default_track(library);
|
2006-09-02 20:46:26 +00:00
|
|
|
track->name = subdata->filename ? strdup(subdata->filename) : 0;
|
|
|
|
|
|
|
|
for (i = 0; i < subdata->sub_num; ++i) {
|
|
|
|
int eid = ass_process_subtitle(track, subdata->subtitles + i);
|
|
|
|
if (eid < 0)
|
|
|
|
continue;
|
|
|
|
if (!subdata->sub_uses_time) {
|
|
|
|
track->events[eid].Start *= 100. / fps;
|
|
|
|
track->events[eid].Duration *= 100. / fps;
|
|
|
|
}
|
2006-08-15 19:25:46 +00:00
|
|
|
}
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
2007-04-27 14:25:36 +00:00
|
|
|
void ass_configure(ass_renderer_t* priv, int w, int h, int unscaled) {
|
|
|
|
int hinting;
|
2006-10-26 20:08:46 +00:00
|
|
|
ass_set_frame_size(priv, w, h);
|
|
|
|
ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
|
|
|
|
ass_set_use_margins(priv, ass_use_margins);
|
|
|
|
ass_set_font_scale(priv, ass_font_scale);
|
2007-04-27 14:25:36 +00:00
|
|
|
if (!unscaled && (ass_hinting & 4))
|
|
|
|
hinting = 0;
|
|
|
|
else
|
|
|
|
hinting = ass_hinting & 3;
|
|
|
|
ass_set_hinting(priv, hinting);
|
2007-09-18 18:34:52 +00:00
|
|
|
ass_set_line_spacing(priv, ass_line_spacing);
|
2006-11-05 18:56:31 +00:00
|
|
|
}
|
2006-10-26 20:08:46 +00:00
|
|
|
|
2006-11-05 18:56:31 +00:00
|
|
|
void ass_configure_fonts(ass_renderer_t* priv) {
|
|
|
|
char *dir, *path, *family;
|
2006-10-26 20:08:46 +00:00
|
|
|
dir = get_path("fonts");
|
2007-12-16 11:13:24 +00:00
|
|
|
if (font_fontconfig < 0 && sub_font_name) path = strdup(sub_font_name);
|
|
|
|
else if (font_fontconfig < 0 && font_name) path = strdup(font_name);
|
2006-10-26 20:08:46 +00:00
|
|
|
else path = get_path("subfont.ttf");
|
2007-12-16 11:13:24 +00:00
|
|
|
if (font_fontconfig >= 0 && sub_font_name) family = strdup(sub_font_name);
|
|
|
|
else if (font_fontconfig >= 0 && font_name) family = strdup(font_name);
|
2006-10-26 20:08:46 +00:00
|
|
|
else family = 0;
|
|
|
|
|
2006-10-28 15:07:18 +00:00
|
|
|
ass_set_fonts(priv, path, family);
|
2006-10-26 20:08:46 +00:00
|
|
|
|
|
|
|
free(dir);
|
|
|
|
free(path);
|
|
|
|
free(family);
|
|
|
|
}
|
2006-10-29 15:26:13 +00:00
|
|
|
|
2007-04-01 21:59:54 +00:00
|
|
|
ass_library_t* ass_init(void) {
|
2006-11-05 18:21:39 +00:00
|
|
|
ass_library_t* priv;
|
|
|
|
char* path = get_path("fonts");
|
|
|
|
priv = ass_library_init();
|
|
|
|
ass_set_fonts_dir(priv, path);
|
|
|
|
ass_set_extract_fonts(priv, extract_embedded_fonts);
|
|
|
|
ass_set_style_overrides(priv, ass_force_style_list);
|
|
|
|
free(path);
|
|
|
|
return priv;
|
2006-10-29 15:26:13 +00:00
|
|
|
}
|