2006-11-26 22:49:27 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <ft2build.h>
|
|
|
|
#include FT_FREETYPE_H
|
|
|
|
#include FT_SYNTHESIS_H
|
|
|
|
#include FT_GLYPH_H
|
|
|
|
|
2006-12-03 17:59:13 +00:00
|
|
|
#include "ass.h"
|
|
|
|
#include "ass_library.h"
|
2006-11-26 22:49:27 +00:00
|
|
|
#include "ass_font.h"
|
2006-11-27 17:50:05 +00:00
|
|
|
#include "ass_bitmap.h"
|
|
|
|
#include "ass_cache.h"
|
2006-11-26 22:49:27 +00:00
|
|
|
#include "ass_fontconfig.h"
|
2007-05-14 20:24:53 +00:00
|
|
|
#include "ass_utils.h"
|
2006-11-26 22:49:27 +00:00
|
|
|
#include "mputils.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Select Microfost Unicode CharMap, if the font has one.
|
|
|
|
* Otherwise, let FreeType decide.
|
|
|
|
*/
|
|
|
|
static void charmap_magic(FT_Face face)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < face->num_charmaps; ++i) {
|
|
|
|
FT_CharMap cmap = face->charmaps[i];
|
|
|
|
unsigned pid = cmap->platform_id;
|
|
|
|
unsigned eid = cmap->encoding_id;
|
|
|
|
if (pid == 3 /*microsoft*/ && (eid == 1 /*unicode bmp*/ || eid == 10 /*full unicode*/)) {
|
|
|
|
FT_Set_Charmap(face, cmap);
|
2007-02-15 22:03:09 +00:00
|
|
|
return;
|
2006-11-26 22:49:27 +00:00
|
|
|
}
|
|
|
|
}
|
2007-02-15 22:03:09 +00:00
|
|
|
|
|
|
|
if (!face->charmap) {
|
|
|
|
if (face->num_charmaps == 0) {
|
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NoCharmaps);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NoCharmapAutodetected);
|
|
|
|
FT_Set_Charmap(face, face->charmaps[0]);
|
|
|
|
return;
|
|
|
|
}
|
2006-11-26 22:49:27 +00:00
|
|
|
}
|
|
|
|
|
2007-05-14 20:24:53 +00:00
|
|
|
static void update_transform(ass_font_t* font)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
FT_Matrix m;
|
2007-05-14 20:41:14 +00:00
|
|
|
double size_scale = font->size / (int)font->size;
|
|
|
|
m.xx = double_to_d16(font->scale_x * size_scale);
|
|
|
|
m.yy = double_to_d16(font->scale_y * size_scale);
|
2007-05-14 20:24:53 +00:00
|
|
|
m.xy = m.yx = 0;
|
|
|
|
for (i = 0; i < font->n_faces; ++i)
|
|
|
|
FT_Set_Transform(font->faces[i], &m, &font->v);
|
|
|
|
}
|
|
|
|
|
2006-12-17 12:00:55 +00:00
|
|
|
/**
|
|
|
|
* \brief find a memory font by name
|
|
|
|
*/
|
2006-12-03 18:24:11 +00:00
|
|
|
static int find_font(ass_library_t* library, char* name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < library->num_fontdata; ++i)
|
|
|
|
if (strcasecmp(name, library->fontdata[i].name) == 0)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-12-17 12:00:55 +00:00
|
|
|
/**
|
|
|
|
* \brief Create a new ass_font_t according to "desc" argument
|
|
|
|
*/
|
2006-12-03 18:24:11 +00:00
|
|
|
ass_font_t* ass_font_new(ass_library_t* library, FT_Library ftlibrary, void* fc_priv, ass_font_desc_t* desc)
|
2006-11-26 22:49:27 +00:00
|
|
|
{
|
|
|
|
char* path;
|
|
|
|
int index;
|
|
|
|
FT_Face face;
|
|
|
|
int error;
|
2007-05-03 19:26:43 +00:00
|
|
|
ass_font_t* fontp;
|
|
|
|
ass_font_t font;
|
2006-12-03 18:24:11 +00:00
|
|
|
int mem_idx;
|
2006-11-27 17:13:52 +00:00
|
|
|
|
2007-05-03 19:26:43 +00:00
|
|
|
fontp = ass_font_cache_find(desc);
|
|
|
|
if (fontp)
|
|
|
|
return fontp;
|
2006-11-26 22:49:27 +00:00
|
|
|
|
|
|
|
path = fontconfig_select(fc_priv, desc->family, desc->bold, desc->italic, &index);
|
|
|
|
|
2006-12-03 18:24:11 +00:00
|
|
|
mem_idx = find_font(library, path);
|
|
|
|
if (mem_idx >= 0) {
|
2006-12-16 19:19:27 +00:00
|
|
|
error = FT_New_Memory_Face(ftlibrary, (unsigned char*)library->fontdata[mem_idx].data,
|
2006-12-03 18:24:11 +00:00
|
|
|
library->fontdata[mem_idx].size, 0, &face);
|
|
|
|
if (error) {
|
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningMemoryFont, path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
2006-12-16 19:35:48 +00:00
|
|
|
error = FT_New_Face(ftlibrary, path, index, &face);
|
|
|
|
if (error) {
|
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-12-03 18:24:11 +00:00
|
|
|
}
|
2006-11-26 22:49:27 +00:00
|
|
|
|
|
|
|
charmap_magic(face);
|
|
|
|
|
2007-05-03 19:26:43 +00:00
|
|
|
font.ftlibrary = ftlibrary;
|
|
|
|
font.faces[0] = face;
|
|
|
|
font.n_faces = 1;
|
|
|
|
font.desc.family = strdup(desc->family);
|
|
|
|
font.desc.bold = desc->bold;
|
|
|
|
font.desc.italic = desc->italic;
|
|
|
|
|
2007-05-14 20:24:53 +00:00
|
|
|
font.scale_x = font.scale_y = 1.;
|
2007-05-03 19:26:43 +00:00
|
|
|
font.v.x = font.v.y = 0;
|
2007-05-14 20:41:14 +00:00
|
|
|
font.size = 0.;
|
2006-11-26 22:49:27 +00:00
|
|
|
|
2006-11-28 22:50:02 +00:00
|
|
|
#ifdef HAVE_FONTCONFIG
|
2007-05-03 19:26:43 +00:00
|
|
|
font.charset = FcCharSetCreate();
|
2006-11-28 22:50:02 +00:00
|
|
|
#endif
|
|
|
|
|
2007-05-03 19:26:43 +00:00
|
|
|
return ass_font_cache_add(&font);
|
2006-11-26 22:49:27 +00:00
|
|
|
}
|
|
|
|
|
2006-12-17 12:00:55 +00:00
|
|
|
/**
|
|
|
|
* \brief Set font transformation matrix and shift vector
|
|
|
|
**/
|
2007-05-14 20:24:53 +00:00
|
|
|
void ass_font_set_transform(ass_font_t* font, double scale_x, double scale_y, FT_Vector* v)
|
2006-11-26 22:49:27 +00:00
|
|
|
{
|
2007-05-14 20:24:53 +00:00
|
|
|
font->scale_x = scale_x;
|
|
|
|
font->scale_y = scale_y;
|
2006-12-16 19:24:01 +00:00
|
|
|
font->v.x = v->x;
|
|
|
|
font->v.y = v->y;
|
2007-05-14 20:24:53 +00:00
|
|
|
update_transform(font);
|
2006-11-26 22:49:27 +00:00
|
|
|
}
|
|
|
|
|
2006-12-17 12:00:55 +00:00
|
|
|
/**
|
|
|
|
* \brief Set font size
|
|
|
|
**/
|
2007-05-14 20:41:14 +00:00
|
|
|
void ass_font_set_size(ass_font_t* font, double size)
|
2006-11-26 22:49:27 +00:00
|
|
|
{
|
2006-12-16 19:34:00 +00:00
|
|
|
int i;
|
2006-11-27 17:33:59 +00:00
|
|
|
if (font->size != size) {
|
2006-11-27 17:34:47 +00:00
|
|
|
font->size = size;
|
2006-12-16 19:34:00 +00:00
|
|
|
for (i = 0; i < font->n_faces; ++i)
|
2007-05-14 20:41:14 +00:00
|
|
|
FT_Set_Pixel_Sizes(font->faces[i], 0, (int)size);
|
|
|
|
update_transform(font);
|
2006-11-27 17:33:59 +00:00
|
|
|
}
|
2006-11-26 22:49:27 +00:00
|
|
|
}
|
|
|
|
|
2006-11-28 22:50:02 +00:00
|
|
|
#ifdef HAVE_FONTCONFIG
|
2006-12-17 12:00:55 +00:00
|
|
|
/**
|
|
|
|
* \brief Select a new FT_Face with the given character
|
|
|
|
* The new face is added to the end of font->faces.
|
|
|
|
**/
|
2006-12-16 19:29:33 +00:00
|
|
|
static void ass_font_reselect(void* fontconfig_priv, ass_font_t* font, uint32_t ch)
|
2006-11-28 22:50:02 +00:00
|
|
|
{
|
|
|
|
char* path;
|
|
|
|
int index;
|
|
|
|
FT_Face face;
|
|
|
|
int error;
|
2006-12-16 19:34:00 +00:00
|
|
|
|
|
|
|
if (font->n_faces == ASS_FONT_MAX_FACES)
|
|
|
|
return;
|
2006-11-28 22:50:02 +00:00
|
|
|
|
|
|
|
path = fontconfig_select_with_charset(fontconfig_priv, font->desc.family, font->desc.bold,
|
|
|
|
font->desc.italic, &index, font->charset);
|
|
|
|
|
|
|
|
error = FT_New_Face(font->ftlibrary, path, index, &face);
|
|
|
|
if (error) {
|
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
charmap_magic(face);
|
|
|
|
|
2006-12-16 19:29:33 +00:00
|
|
|
error = FT_Get_Char_Index(face, ch);
|
|
|
|
if (error == 0) { // the new font face is not better then the old one
|
|
|
|
FT_Done_Face(face);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-12-16 19:34:00 +00:00
|
|
|
font->faces[font->n_faces++] = face;
|
2007-05-14 20:24:53 +00:00
|
|
|
update_transform(font);
|
2007-05-14 20:41:14 +00:00
|
|
|
FT_Set_Pixel_Sizes(face, 0, (int)font->size);
|
2006-11-28 22:50:02 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-12-17 12:00:55 +00:00
|
|
|
/**
|
|
|
|
* \brief Get maximal font ascender and descender.
|
|
|
|
* \param ch character code
|
|
|
|
* The values are extracted from the font face that provides glyphs for the given character
|
|
|
|
**/
|
2006-12-16 19:17:50 +00:00
|
|
|
void ass_font_get_asc_desc(ass_font_t* font, uint32_t ch, int* asc, int* desc)
|
|
|
|
{
|
2006-12-16 19:34:00 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < font->n_faces; ++i) {
|
2006-12-16 19:35:48 +00:00
|
|
|
FT_Face face = font->faces[i];
|
|
|
|
if (FT_Get_Char_Index(face, ch)) {
|
|
|
|
int v, v2;
|
|
|
|
v = face->size->metrics.ascender;
|
|
|
|
v2 = FT_MulFix(face->bbox.yMax, face->size->metrics.y_scale);
|
|
|
|
*asc = (v > v2 * 0.9) ? v : v2;
|
2006-12-16 19:17:50 +00:00
|
|
|
|
2006-12-16 19:35:48 +00:00
|
|
|
v = - face->size->metrics.descender;
|
|
|
|
v2 = - FT_MulFix(face->bbox.yMin, face->size->metrics.y_scale);
|
|
|
|
*desc = (v > v2 * 0.9) ? v : v2;
|
|
|
|
return;
|
2006-12-16 19:34:00 +00:00
|
|
|
}
|
2006-12-16 19:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*asc = *desc = 0;
|
|
|
|
}
|
|
|
|
|
2006-12-17 12:00:55 +00:00
|
|
|
/**
|
|
|
|
* \brief Get a glyph
|
|
|
|
* \param ch character code
|
|
|
|
**/
|
2007-04-27 14:25:36 +00:00
|
|
|
FT_Glyph ass_font_get_glyph(void* fontconfig_priv, ass_font_t* font, uint32_t ch, ass_hinting_t hinting)
|
2006-11-26 22:49:27 +00:00
|
|
|
{
|
|
|
|
int error;
|
2006-12-16 19:34:00 +00:00
|
|
|
int index = 0;
|
|
|
|
int i;
|
2006-11-26 22:49:27 +00:00
|
|
|
FT_Glyph glyph;
|
2006-12-16 19:34:00 +00:00
|
|
|
FT_Face face = 0;
|
2007-04-27 14:25:36 +00:00
|
|
|
int flags = 0;
|
2006-11-28 22:44:40 +00:00
|
|
|
|
|
|
|
if (ch < 0x20)
|
|
|
|
return 0;
|
2006-12-16 19:34:00 +00:00
|
|
|
if (font->n_faces == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < font->n_faces; ++i) {
|
|
|
|
face = font->faces[i];
|
|
|
|
index = FT_Get_Char_Index(face, ch);
|
|
|
|
if (index)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-11-28 22:50:02 +00:00
|
|
|
#ifdef HAVE_FONTCONFIG
|
|
|
|
FcCharSetAddChar(font->charset, ch);
|
|
|
|
if (index == 0) {
|
|
|
|
mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_GlyphNotFoundReselectingFont,
|
|
|
|
ch, font->desc.family, font->desc.bold, font->desc.italic);
|
2006-12-16 19:29:33 +00:00
|
|
|
ass_font_reselect(fontconfig_priv, font, ch);
|
2006-12-16 19:34:00 +00:00
|
|
|
face = font->faces[font->n_faces - 1];
|
|
|
|
index = FT_Get_Char_Index(face, ch);
|
2006-11-28 22:50:02 +00:00
|
|
|
if (index == 0) {
|
|
|
|
mp_msg(MSGT_ASS, MSGL_ERR, MSGTR_LIBASS_GlyphNotFound,
|
|
|
|
ch, font->desc.family, font->desc.bold, font->desc.italic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-04-27 14:25:36 +00:00
|
|
|
switch (hinting) {
|
|
|
|
case ASS_HINTING_NONE: flags = FT_LOAD_NO_HINTING; break;
|
|
|
|
case ASS_HINTING_LIGHT: flags = FT_LOAD_FORCE_AUTOHINT | FT_LOAD_TARGET_LIGHT; break;
|
|
|
|
case ASS_HINTING_NORMAL: flags = FT_LOAD_FORCE_AUTOHINT; break;
|
|
|
|
case ASS_HINTING_NATIVE: flags = 0; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = FT_Load_Glyph(face, index, FT_LOAD_NO_BITMAP | flags);
|
2006-11-26 22:49:27 +00:00
|
|
|
if (error) {
|
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if (FREETYPE_MAJOR > 2) || \
|
|
|
|
((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR >= 2)) || \
|
|
|
|
((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR == 1) && (FREETYPE_PATCH >= 10))
|
|
|
|
// FreeType >= 2.1.10 required
|
2006-12-16 19:34:00 +00:00
|
|
|
if (!(face->style_flags & FT_STYLE_FLAG_ITALIC) &&
|
2006-11-26 22:49:27 +00:00
|
|
|
(font->desc.italic > 55)) {
|
2006-12-16 19:34:00 +00:00
|
|
|
FT_GlyphSlot_Oblique(face->glyph);
|
2006-11-26 22:49:27 +00:00
|
|
|
}
|
|
|
|
#endif
|
2006-12-16 19:34:00 +00:00
|
|
|
error = FT_Get_Glyph(face->glyph, &glyph);
|
2006-11-26 22:49:27 +00:00
|
|
|
if (error) {
|
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return glyph;
|
|
|
|
}
|
|
|
|
|
2006-12-17 12:00:55 +00:00
|
|
|
/**
|
|
|
|
* \brief Get kerning for the pair of glyphs.
|
|
|
|
**/
|
2006-12-16 19:17:50 +00:00
|
|
|
FT_Vector ass_font_get_kerning(ass_font_t* font, uint32_t c1, uint32_t c2)
|
|
|
|
{
|
|
|
|
FT_Vector v = {0, 0};
|
2006-12-16 19:34:00 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < font->n_faces; ++i) {
|
|
|
|
FT_Face face = font->faces[i];
|
|
|
|
int i1 = FT_Get_Char_Index(face, c1);
|
|
|
|
int i2 = FT_Get_Char_Index(face, c2);
|
|
|
|
if (i1 && i2) {
|
|
|
|
if (FT_HAS_KERNING(face))
|
|
|
|
FT_Get_Kerning(face, i1, i2, FT_KERNING_DEFAULT, &v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
if (i1 || i2) // these glyphs are from different font faces, no kerning information
|
|
|
|
return v;
|
|
|
|
}
|
2006-12-16 19:17:50 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2006-12-17 12:00:55 +00:00
|
|
|
/**
|
|
|
|
* \brief Deallocate ass_font_t
|
|
|
|
**/
|
2006-11-26 22:49:27 +00:00
|
|
|
void ass_font_free(ass_font_t* font)
|
|
|
|
{
|
2006-12-16 19:34:00 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < font->n_faces; ++i)
|
|
|
|
if (font->faces[i]) FT_Done_Face(font->faces[i]);
|
2006-11-26 22:49:27 +00:00
|
|
|
if (font->desc.family) free(font->desc.family);
|
2006-11-28 22:50:02 +00:00
|
|
|
#ifdef HAVE_FONTCONFIG
|
|
|
|
if (font->charset) FcCharSetDestroy(font->charset);
|
|
|
|
#endif
|
2006-11-27 17:13:52 +00:00
|
|
|
free(font);
|
2006-11-26 22:49:27 +00:00
|
|
|
}
|