annotate libass/ass_font.c @ 21614:5d2ca7ca18b5

Move ascender, descender, and kerning computation to ass_font.c.
author eugeni
date Sat, 16 Dec 2006 19:17:50 +0000
parents 62bd8e0d3a0f
children 62989854d340
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*-
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
2 // vim:ts=8:sw=8:noet:ai:
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
3 /*
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
4 Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
5
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
6 This program is free software; you can redistribute it and/or modify
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
7 it under the terms of the GNU General Public License as published by
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
8 the Free Software Foundation; either version 2 of the License, or
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
9 (at your option) any later version.
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
10
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
11 This program is distributed in the hope that it will be useful,
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
14 GNU General Public License for more details.
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
15
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
17 along with this program; if not, write to the Free Software
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
19 */
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
20
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
21 #include "config.h"
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
22
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
23 #include <inttypes.h>
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
24 #include <ft2build.h>
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
25 #include FT_FREETYPE_H
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
26 #include FT_SYNTHESIS_H
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
27 #include FT_GLYPH_H
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
28
21458
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 21351
diff changeset
29 #include "ass.h"
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 21351
diff changeset
30 #include "ass_library.h"
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
31 #include "ass_font.h"
21321
7b7627ff1937 Move ass_font_desc_t and ass_font_t declarations to ass_font.h.
eugeni
parents: 21320
diff changeset
32 #include "ass_bitmap.h"
7b7627ff1937 Move ass_font_desc_t and ass_font_t declarations to ass_font.h.
eugeni
parents: 21320
diff changeset
33 #include "ass_cache.h"
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
34 #include "ass_fontconfig.h"
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
35 #include "mputils.h"
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
36
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
37 /**
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
38 * Select Microfost Unicode CharMap, if the font has one.
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
39 * Otherwise, let FreeType decide.
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
40 */
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
41 static void charmap_magic(FT_Face face)
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
42 {
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
43 int i;
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
44 for (i = 0; i < face->num_charmaps; ++i) {
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
45 FT_CharMap cmap = face->charmaps[i];
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
46 unsigned pid = cmap->platform_id;
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
47 unsigned eid = cmap->encoding_id;
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
48 if (pid == 3 /*microsoft*/ && (eid == 1 /*unicode bmp*/ || eid == 10 /*full unicode*/)) {
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
49 FT_Set_Charmap(face, cmap);
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
50 break;
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
51 }
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
52 }
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
53 }
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
54
21460
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
55 static int find_font(ass_library_t* library, char* name)
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
56 {
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
57 int i;
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
58 for (i = 0; i < library->num_fontdata; ++i)
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
59 if (strcasecmp(name, library->fontdata[i].name) == 0)
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
60 return i;
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
61 return -1;
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
62 }
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
63
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
64 ass_font_t* ass_font_new(ass_library_t* library, FT_Library ftlibrary, void* fc_priv, ass_font_desc_t* desc)
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
65 {
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
66 char* path;
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
67 int index;
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
68 FT_Face face;
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
69 int error;
21317
dcfd069efd8f Move ass_font_t allocation to ass_font.h.
eugeni
parents: 21314
diff changeset
70 ass_font_t* font;
21460
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
71 int mem_idx;
21317
dcfd069efd8f Move ass_font_t allocation to ass_font.h.
eugeni
parents: 21314
diff changeset
72
dcfd069efd8f Move ass_font_t allocation to ass_font.h.
eugeni
parents: 21314
diff changeset
73 font = ass_font_cache_find(desc);
dcfd069efd8f Move ass_font_t allocation to ass_font.h.
eugeni
parents: 21314
diff changeset
74 if (font)
dcfd069efd8f Move ass_font_t allocation to ass_font.h.
eugeni
parents: 21314
diff changeset
75 return font;
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
76
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
77 path = fontconfig_select(fc_priv, desc->family, desc->bold, desc->italic, &index);
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
78
21460
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
79 mem_idx = find_font(library, path);
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
80 if (mem_idx >= 0) {
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
81 error = FT_New_Memory_Face(ftlibrary, library->fontdata[mem_idx].data,
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
82 library->fontdata[mem_idx].size, 0, &face);
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
83 if (error) {
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
84 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningMemoryFont, path);
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
85 return 0;
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
86 }
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
87 } else {
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
88 error = FT_New_Face(ftlibrary, path, index, &face);
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
89 if (error) {
21279
9040bce9f768 Remove obsolete "no_more_font_messages" hack.
eugeni
parents: 21277
diff changeset
90 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index);
21317
dcfd069efd8f Move ass_font_t allocation to ass_font.h.
eugeni
parents: 21314
diff changeset
91 return 0;
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
92 }
21460
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
93 }
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
94
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
95 charmap_magic(face);
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
96
21317
dcfd069efd8f Move ass_font_t allocation to ass_font.h.
eugeni
parents: 21314
diff changeset
97 font = calloc(1, sizeof(ass_font_t));
21349
11679d93c7d8 Add FT_Library to ass_font_t.
eugeni
parents: 21321
diff changeset
98 font->ftlibrary = ftlibrary;
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
99 font->path = strdup(path);
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
100 font->index = index;
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
101 font->face = face;
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
102 font->desc.family = strdup(desc->family);
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
103 font->desc.bold = desc->bold;
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
104 font->desc.italic = desc->italic;
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
105
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
106 font->m.xx = font->m.yy = (FT_Fixed)0x10000L;
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
107 font->m.xy = font->m.yy = 0;
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
108 font->v.x = font->v.y = 0;
21314
4b2e5a74a2eb Initialize font size with 0.
eugeni
parents: 21279
diff changeset
109 font->size = 0;
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
110
21351
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
111 #ifdef HAVE_FONTCONFIG
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
112 font->charset = FcCharSetCreate();
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
113 #endif
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
114
21317
dcfd069efd8f Move ass_font_t allocation to ass_font.h.
eugeni
parents: 21314
diff changeset
115 ass_font_cache_add(font);
dcfd069efd8f Move ass_font_t allocation to ass_font.h.
eugeni
parents: 21314
diff changeset
116
dcfd069efd8f Move ass_font_t allocation to ass_font.h.
eugeni
parents: 21314
diff changeset
117 return font;
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
118 }
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
119
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
120 void ass_font_set_transform(ass_font_t* font, FT_Matrix* m, FT_Vector* v)
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
121 {
21319
415e2119e91c Don't call FT_Set_Transform/FT_Set_Pixel_Sizes if values have not changed.
eugeni
parents: 21317
diff changeset
122 if (font->m.xx != m->xx ||
415e2119e91c Don't call FT_Set_Transform/FT_Set_Pixel_Sizes if values have not changed.
eugeni
parents: 21317
diff changeset
123 font->m.xy != m->xy ||
415e2119e91c Don't call FT_Set_Transform/FT_Set_Pixel_Sizes if values have not changed.
eugeni
parents: 21317
diff changeset
124 font->m.yx != m->yx ||
415e2119e91c Don't call FT_Set_Transform/FT_Set_Pixel_Sizes if values have not changed.
eugeni
parents: 21317
diff changeset
125 font->m.yy != m->yy ||
415e2119e91c Don't call FT_Set_Transform/FT_Set_Pixel_Sizes if values have not changed.
eugeni
parents: 21317
diff changeset
126 font->v.x != v->x ||
415e2119e91c Don't call FT_Set_Transform/FT_Set_Pixel_Sizes if values have not changed.
eugeni
parents: 21317
diff changeset
127 font->v.y != v->y
415e2119e91c Don't call FT_Set_Transform/FT_Set_Pixel_Sizes if values have not changed.
eugeni
parents: 21317
diff changeset
128 ) {
21320
4a7e96f1eebf Cosmetics: reindent.
eugeni
parents: 21319
diff changeset
129 font->m.xx = m->xx;
4a7e96f1eebf Cosmetics: reindent.
eugeni
parents: 21319
diff changeset
130 font->m.xy = m->xy;
4a7e96f1eebf Cosmetics: reindent.
eugeni
parents: 21319
diff changeset
131 font->m.yx = m->yx;
4a7e96f1eebf Cosmetics: reindent.
eugeni
parents: 21319
diff changeset
132 font->m.yy = m->yy;
4a7e96f1eebf Cosmetics: reindent.
eugeni
parents: 21319
diff changeset
133 font->v.x = v->x;
4a7e96f1eebf Cosmetics: reindent.
eugeni
parents: 21319
diff changeset
134 font->v.y = v->y;
4a7e96f1eebf Cosmetics: reindent.
eugeni
parents: 21319
diff changeset
135 FT_Set_Transform(font->face, &font->m, &font->v);
21319
415e2119e91c Don't call FT_Set_Transform/FT_Set_Pixel_Sizes if values have not changed.
eugeni
parents: 21317
diff changeset
136 }
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
137 }
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
138
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
139 void ass_font_set_size(ass_font_t* font, int size)
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
140 {
21319
415e2119e91c Don't call FT_Set_Transform/FT_Set_Pixel_Sizes if values have not changed.
eugeni
parents: 21317
diff changeset
141 if (font->size != size) {
21320
4a7e96f1eebf Cosmetics: reindent.
eugeni
parents: 21319
diff changeset
142 font->size = size;
4a7e96f1eebf Cosmetics: reindent.
eugeni
parents: 21319
diff changeset
143 FT_Set_Pixel_Sizes(font->face, 0, size);
21319
415e2119e91c Don't call FT_Set_Transform/FT_Set_Pixel_Sizes if values have not changed.
eugeni
parents: 21317
diff changeset
144 }
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
145 }
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
146
21351
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
147 #ifdef HAVE_FONTCONFIG
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
148 static void ass_font_reselect(void* fontconfig_priv, ass_font_t* font)
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
149 {
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
150 char* path;
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
151 int index;
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
152 FT_Face face;
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
153 int error;
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
154
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
155 path = fontconfig_select_with_charset(fontconfig_priv, font->desc.family, font->desc.bold,
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
156 font->desc.italic, &index, font->charset);
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
157 if (strcasecmp(path, font->path) == 0 && index == font->index) {
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
158 free(path);
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
159 return;
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
160 }
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
161
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
162 error = FT_New_Face(font->ftlibrary, path, index, &face);
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
163 if (error) {
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
164 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index);
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
165 return;
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
166 }
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
167 charmap_magic(face);
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
168
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
169 if (font->face) FT_Done_Face(font->face);
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
170 free(font->path);
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
171
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
172 font->face = face;
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
173 font->path = strdup(path);
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
174 font->index = index;
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
175
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
176 FT_Set_Transform(font->face, &font->m, &font->v);
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
177 FT_Set_Pixel_Sizes(font->face, 0, font->size);
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
178 }
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
179 #endif
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
180
21614
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
181 void ass_font_get_asc_desc(ass_font_t* font, uint32_t ch, int* asc, int* desc)
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
182 {
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
183 FT_Face face = font->face;
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
184 if (FT_Get_Char_Index(face, ch)) {
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
185 int v, v2;
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
186 v = face->size->metrics.ascender;
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
187 v2 = FT_MulFix(face->bbox.yMax, face->size->metrics.y_scale);
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
188 *asc = (v > v2 * 0.9) ? v : v2;
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
189
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
190 v = - face->size->metrics.descender;
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
191 v2 = - FT_MulFix(face->bbox.yMin, face->size->metrics.y_scale);
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
192 *desc = (v > v2 * 0.9) ? v : v2;
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
193 return;
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
194 }
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
195
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
196 *asc = *desc = 0;
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
197 }
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
198
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
199 FT_Glyph ass_font_get_glyph(void* fontconfig_priv, ass_font_t* font, uint32_t ch)
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
200 {
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
201 int error;
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
202 int index;
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
203 FT_Glyph glyph;
21350
5337cdeec169 Skip glyphs with char code < 0x20.
eugeni
parents: 21349
diff changeset
204
5337cdeec169 Skip glyphs with char code < 0x20.
eugeni
parents: 21349
diff changeset
205 if (ch < 0x20)
5337cdeec169 Skip glyphs with char code < 0x20.
eugeni
parents: 21349
diff changeset
206 return 0;
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
207
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
208 index = FT_Get_Char_Index(font->face, ch);
21351
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
209 #ifdef HAVE_FONTCONFIG
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
210 FcCharSetAddChar(font->charset, ch);
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
211 if (index == 0) {
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
212 mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_GlyphNotFoundReselectingFont,
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
213 ch, font->desc.family, font->desc.bold, font->desc.italic);
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
214 ass_font_reselect(fontconfig_priv, font);
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
215 index = FT_Get_Char_Index(font->face, ch);
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
216 if (index == 0) {
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
217 mp_msg(MSGT_ASS, MSGL_ERR, MSGTR_LIBASS_GlyphNotFound,
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
218 ch, font->desc.family, font->desc.bold, font->desc.italic);
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
219 }
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
220 }
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
221 #endif
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
222
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
223 error = FT_Load_Glyph(font->face, index, FT_LOAD_NO_BITMAP );
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
224 if (error) {
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
225 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph);
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
226 return 0;
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
227 }
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
228
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
229 #if (FREETYPE_MAJOR > 2) || \
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
230 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR >= 2)) || \
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
231 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR == 1) && (FREETYPE_PATCH >= 10))
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
232 // FreeType >= 2.1.10 required
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
233 if (!(font->face->style_flags & FT_STYLE_FLAG_ITALIC) &&
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
234 (font->desc.italic > 55)) {
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
235 FT_GlyphSlot_Oblique(font->face->glyph);
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
236 }
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
237 #endif
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
238 error = FT_Get_Glyph(font->face->glyph, &glyph);
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
239 if (error) {
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
240 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph);
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
241 return 0;
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
242 }
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
243
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
244 return glyph;
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
245 }
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
246
21614
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
247 FT_Vector ass_font_get_kerning(ass_font_t* font, uint32_t c1, uint32_t c2)
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
248 {
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
249 FT_Vector v = {0, 0};
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
250 int i1, i2;
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
251
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
252 if (!FT_HAS_KERNING(font->face))
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
253 return v;
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
254 i1 = FT_Get_Char_Index(font->face, c1);
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
255 i2 = FT_Get_Char_Index(font->face, c2);
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
256 if (i1 && i2)
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
257 FT_Get_Kerning(font->face, i1, i2, FT_KERNING_DEFAULT, &v);
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
258 return v;
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
259 }
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
260
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
261 void ass_font_free(ass_font_t* font)
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
262 {
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
263 if (font->face) FT_Done_Face(font->face);
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
264 if (font->path) free(font->path);
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
265 if (font->desc.family) free(font->desc.family);
21351
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
266 #ifdef HAVE_FONTCONFIG
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
267 if (font->charset) FcCharSetDestroy(font->charset);
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
268 #endif
21317
dcfd069efd8f Move ass_font_t allocation to ass_font.h.
eugeni
parents: 21314
diff changeset
269 free(font);
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
270 }