annotate libass/ass_font.c @ 32608:75c7654261d0

Replace hard-coded number for loop limits for array index by the define used in the array declaration.
author reimar
date Sat, 11 Dec 2010 22:26:57 +0000
parents ac6e48baa03d
children 88eebbbbd6a0
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 /*
26723
0f892cd714b2 Use standard license header.
diego
parents: 25660
diff changeset
2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
0f892cd714b2 Use standard license header.
diego
parents: 25660
diff changeset
3 *
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26723
diff changeset
4 * This file is part of libass.
26723
0f892cd714b2 Use standard license header.
diego
parents: 25660
diff changeset
5 *
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26723
diff changeset
6 * libass is free software; you can redistribute it and/or modify
26723
0f892cd714b2 Use standard license header.
diego
parents: 25660
diff changeset
7 * it under the terms of the GNU General Public License as published by
0f892cd714b2 Use standard license header.
diego
parents: 25660
diff changeset
8 * the Free Software Foundation; either version 2 of the License, or
0f892cd714b2 Use standard license header.
diego
parents: 25660
diff changeset
9 * (at your option) any later version.
0f892cd714b2 Use standard license header.
diego
parents: 25660
diff changeset
10 *
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26723
diff changeset
11 * libass is distributed in the hope that it will be useful,
26723
0f892cd714b2 Use standard license header.
diego
parents: 25660
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0f892cd714b2 Use standard license header.
diego
parents: 25660
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0f892cd714b2 Use standard license header.
diego
parents: 25660
diff changeset
14 * GNU General Public License for more details.
0f892cd714b2 Use standard license header.
diego
parents: 25660
diff changeset
15 *
0f892cd714b2 Use standard license header.
diego
parents: 25660
diff changeset
16 * You should have received a copy of the GNU General Public License along
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26723
diff changeset
17 * with libass; if not, write to the Free Software Foundation, Inc.,
26723
0f892cd714b2 Use standard license header.
diego
parents: 25660
diff changeset
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0f892cd714b2 Use standard license header.
diego
parents: 25660
diff changeset
19 */
21277
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
23328
99ac5d381aed Correct font size in libass.
eugeni
parents: 23300
diff changeset
28 #include FT_TRUETYPE_TABLES_H
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
29 #include FT_OUTLINE_H
31875
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
30 #include <strings.h>
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
31
21458
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 21351
diff changeset
32 #include "ass.h"
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 21351
diff changeset
33 #include "ass_library.h"
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
34 #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
35 #include "ass_bitmap.h"
7b7627ff1937 Move ass_font_desc_t and ass_font_t declarations to ass_font.h.
eugeni
parents: 21320
diff changeset
36 #include "ass_cache.h"
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
37 #include "ass_fontconfig.h"
23299
0ee56ec36a40 Limit ass_font_set_transform to nonrotating transformations.
eugeni
parents: 23212
diff changeset
38 #include "ass_utils.h"
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
39
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
40 #define VERTICAL_LOWER_BOUND 0x02f1
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
41
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
42 /**
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
43 * Select a good charmap, prefer Microsoft Unicode charmaps.
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
44 * Otherwise, let FreeType decide.
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
45 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
46 static void charmap_magic(ASS_Library *library, FT_Face face)
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
47 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
48 int i;
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
49 int ms_cmap = -1;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
50
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
51 // Search for a Microsoft Unicode cmap
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
52 for (i = 0; i < face->num_charmaps; ++i) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
53 FT_CharMap cmap = face->charmaps[i];
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
54 unsigned pid = cmap->platform_id;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
55 unsigned eid = cmap->encoding_id;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
56 if (pid == 3 /*microsoft */
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
57 && (eid == 1 /*unicode bmp */
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
58 || eid == 10 /*full unicode */ )) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
59 FT_Set_Charmap(face, cmap);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
60 return;
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
61 } else if (pid == 3 && ms_cmap < 0)
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
62 ms_cmap = i;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
63 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
64
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
65 // Try the first Microsoft cmap if no Microsoft Unicode cmap was found
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
66 if (ms_cmap >= 0) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
67 FT_CharMap cmap = face->charmaps[ms_cmap];
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
68 FT_Set_Charmap(face, cmap);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
69 return;
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
70 }
22210
4a958bd08920 Select the first charmap in the font, if FreeType did not autoselect any.
eugeni
parents: 21630
diff changeset
71
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
72 if (!face->charmap) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
73 if (face->num_charmaps == 0) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
74 ass_msg(library, MSGL_WARN, "Font face with no charmaps");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
75 return;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
76 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
77 ass_msg(library, MSGL_WARN,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
78 "No charmap autodetected, trying the first one");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
79 FT_Set_Charmap(face, face->charmaps[0]);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
80 return;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
81 }
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
82 }
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
83
21630
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
84 /**
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
85 * \brief find a memory font by name
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
86 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
87 static int find_font(ASS_Library *library, char *name)
21460
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
88 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
89 int i;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
90 for (i = 0; i < library->num_fontdata; ++i)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
91 if (strcasecmp(name, library->fontdata[i].name) == 0)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
92 return i;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
93 return -1;
21460
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
94 }
62bd8e0d3a0f Open embedded fonts directly from memory.
eugeni
parents: 21458
diff changeset
95
23981
705628816d98 Factor out common code from ass_font_new and ass_font_reselect.
eugeni
parents: 23980
diff changeset
96 static void face_set_size(FT_Face face, double size);
705628816d98 Factor out common code from ass_font_new and ass_font_reselect.
eugeni
parents: 23980
diff changeset
97
23982
17b5fa69243c Workaround for fonts with zero ascender/descender in horizontal header.
eugeni
parents: 23981
diff changeset
98 static void buggy_font_workaround(FT_Face face)
17b5fa69243c Workaround for fonts with zero ascender/descender in horizontal header.
eugeni
parents: 23981
diff changeset
99 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
100 // Some fonts have zero Ascender/Descender fields in 'hhea' table.
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
101 // In this case, get the information from 'os2' table or, as
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
102 // a last resort, from face.bbox.
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
103 if (face->ascender + face->descender == 0 || face->height == 0) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
104 TT_OS2 *os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
105 if (os2) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
106 face->ascender = os2->sTypoAscender;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
107 face->descender = os2->sTypoDescender;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
108 face->height = face->ascender - face->descender;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
109 } else {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
110 face->ascender = face->bbox.yMax;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
111 face->descender = face->bbox.yMin;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
112 face->height = face->ascender - face->descender;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
113 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
114 }
23982
17b5fa69243c Workaround for fonts with zero ascender/descender in horizontal header.
eugeni
parents: 23981
diff changeset
115 }
17b5fa69243c Workaround for fonts with zero ascender/descender in horizontal header.
eugeni
parents: 23981
diff changeset
116
23981
705628816d98 Factor out common code from ass_font_new and ass_font_reselect.
eugeni
parents: 23980
diff changeset
117 /**
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
118 * \brief Select a face with the given charcode and add it to ASS_Font
23981
705628816d98 Factor out common code from ass_font_new and ass_font_reselect.
eugeni
parents: 23980
diff changeset
119 * \return index of the new face in font->faces, -1 if failed
705628816d98 Factor out common code from ass_font_new and ass_font_reselect.
eugeni
parents: 23980
diff changeset
120 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
121 static int add_face(void *fc_priv, ASS_Font *font, uint32_t ch)
23981
705628816d98 Factor out common code from ass_font_new and ass_font_reselect.
eugeni
parents: 23980
diff changeset
122 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
123 char *path;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
124 int index;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
125 FT_Face face;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
126 int error;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
127 int mem_idx;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28860
diff changeset
128
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
129 if (font->n_faces == ASS_FONT_MAX_FACES)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
130 return -1;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28860
diff changeset
131
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
132 path =
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
133 fontconfig_select(font->library, fc_priv, font->desc.family,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
134 font->desc.treat_family_as_pattern,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
135 font->desc.bold, font->desc.italic, &index, ch);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
136 if (!path)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
137 return -1;
23981
705628816d98 Factor out common code from ass_font_new and ass_font_reselect.
eugeni
parents: 23980
diff changeset
138
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
139 mem_idx = find_font(font->library, path);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
140 if (mem_idx >= 0) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
141 error =
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
142 FT_New_Memory_Face(font->ftlibrary,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
143 (unsigned char *) font->library->
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
144 fontdata[mem_idx].data,
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
145 font->library->fontdata[mem_idx].size, index,
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
146 &face);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
147 if (error) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
148 ass_msg(font->library, MSGL_WARN,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
149 "Error opening memory font: '%s'", path);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
150 free(path);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
151 return -1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
152 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
153 } else {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
154 error = FT_New_Face(font->ftlibrary, path, index, &face);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
155 if (error) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
156 ass_msg(font->library, MSGL_WARN,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
157 "Error opening font: '%s', %d", path, index);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
158 free(path);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
159 return -1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
160 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
161 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
162 charmap_magic(font->library, face);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
163 buggy_font_workaround(face);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28860
diff changeset
164
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
165 font->faces[font->n_faces++] = face;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
166 face_set_size(face, font->size);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
167 free(path);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
168 return font->n_faces - 1;
23981
705628816d98 Factor out common code from ass_font_new and ass_font_reselect.
eugeni
parents: 23980
diff changeset
169 }
705628816d98 Factor out common code from ass_font_new and ass_font_reselect.
eugeni
parents: 23980
diff changeset
170
21630
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
171 /**
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
172 * \brief Create a new ASS_Font according to "desc" argument
21630
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
173 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
174 ASS_Font *ass_font_new(void *font_cache, ASS_Library *library,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
175 FT_Library ftlibrary, void *fc_priv,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
176 ASS_FontDesc *desc)
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
177 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
178 int error;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
179 ASS_Font *fontp;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
180 ASS_Font font;
21317
dcfd069efd8f Move ass_font_t allocation to ass_font.h.
eugeni
parents: 21314
diff changeset
181
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
182 fontp = ass_font_cache_find((Hashmap *) font_cache, desc);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
183 if (fontp)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
184 return fontp;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28860
diff changeset
185
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
186 font.library = library;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
187 font.ftlibrary = ftlibrary;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
188 font.n_faces = 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
189 font.desc.family = strdup(desc->family);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
190 font.desc.treat_family_as_pattern = desc->treat_family_as_pattern;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
191 font.desc.bold = desc->bold;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
192 font.desc.italic = desc->italic;
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
193 font.desc.vertical = desc->vertical;
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
194
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
195 font.scale_x = font.scale_y = 1.;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
196 font.v.x = font.v.y = 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
197 font.size = 0.;
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
198
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
199 error = add_face(fc_priv, &font, 0);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
200 if (error == -1) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
201 free(font.desc.family);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
202 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
203 } else
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
204 return ass_font_cache_add((Hashmap *) font_cache, &font);
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
205 }
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
206
21630
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
207 /**
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
208 * \brief Set font transformation matrix and shift vector
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
209 **/
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
210 void ass_font_set_transform(ASS_Font *font, double scale_x,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
211 double scale_y, FT_Vector *v)
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
212 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
213 font->scale_x = scale_x;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
214 font->scale_y = scale_y;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
215 if (v) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
216 font->v.x = v->x;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
217 font->v.y = v->y;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
218 }
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
219 }
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
220
23328
99ac5d381aed Correct font size in libass.
eugeni
parents: 23300
diff changeset
221 static void face_set_size(FT_Face face, double size)
99ac5d381aed Correct font size in libass.
eugeni
parents: 23300
diff changeset
222 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
223 TT_HoriHeader *hori = FT_Get_Sfnt_Table(face, ft_sfnt_hhea);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
224 TT_OS2 *os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
225 double mscale = 1.;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
226 FT_Size_RequestRec rq;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
227 FT_Size_Metrics *m = &face->size->metrics;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
228 // VSFilter uses metrics from TrueType OS/2 table
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
229 // The idea was borrowed from asa (http://asa.diac24.net)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
230 if (hori && os2) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
231 int hori_height = hori->Ascender - hori->Descender;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
232 int os2_height = os2->usWinAscent + os2->usWinDescent;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
233 if (hori_height && os2_height)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
234 mscale = (double) hori_height / os2_height;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
235 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
236 memset(&rq, 0, sizeof(rq));
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
237 rq.type = FT_SIZE_REQUEST_TYPE_REAL_DIM;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
238 rq.width = 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
239 rq.height = double_to_d6(size * mscale);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
240 rq.horiResolution = rq.vertResolution = 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
241 FT_Request_Size(face, &rq);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
242 m->ascender /= mscale;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
243 m->descender /= mscale;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
244 m->height /= mscale;
23328
99ac5d381aed Correct font size in libass.
eugeni
parents: 23300
diff changeset
245 }
99ac5d381aed Correct font size in libass.
eugeni
parents: 23300
diff changeset
246
21630
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
247 /**
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
248 * \brief Set font size
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
249 **/
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
250 void ass_font_set_size(ASS_Font *font, double size)
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
251 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
252 int i;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
253 if (font->size != size) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
254 font->size = size;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
255 for (i = 0; i < font->n_faces; ++i)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
256 face_set_size(font->faces[i], size);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
257 }
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
258 }
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
259
21630
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
260 /**
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
261 * \brief Get maximal font ascender and descender.
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
262 * \param ch character code
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
263 * The values are extracted from the font face that provides glyphs for the given character
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
264 **/
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
265 void ass_font_get_asc_desc(ASS_Font *font, uint32_t ch, int *asc,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
266 int *desc)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
267 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
268 int i;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
269 for (i = 0; i < font->n_faces; ++i) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
270 FT_Face face = font->faces[i];
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
271 TT_OS2 *os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
272 if (FT_Get_Char_Index(face, ch)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
273 int y_scale = face->size->metrics.y_scale;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
274 if (os2) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
275 *asc = FT_MulFix(os2->usWinAscent, y_scale);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
276 *desc = FT_MulFix(os2->usWinDescent, y_scale);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
277 } else {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
278 *asc = FT_MulFix(face->ascender, y_scale);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
279 *desc = FT_MulFix(-face->descender, y_scale);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
280 }
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
281 if (font->desc.vertical && ch >= VERTICAL_LOWER_BOUND) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
282 *asc = FT_MulFix(face->max_advance_width, y_scale);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
283 }
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
284 return;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
285 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
286 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
287
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
288 *asc = *desc = 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
289 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
290
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
291 /*
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
292 * Strike a glyph with a horizontal line; it's possible to underline it
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
293 * and/or strike through it. For the line's position and size, truetype
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
294 * tables are consulted. Obviously this relies on the data in the tables
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
295 * being accurate.
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
296 *
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
297 */
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
298 static int ass_strike_outline_glyph(FT_Face face, ASS_Font *font,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
299 FT_Glyph glyph, int under, int through)
21614
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
300 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
301 TT_OS2 *os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
302 TT_Postscript *ps = FT_Get_Sfnt_Table(face, ft_sfnt_post);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
303 FT_Outline *ol = &((FT_OutlineGlyph) glyph)->outline;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
304 int bear, advance, y_scale, i, dir;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
305
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
306 if (!under && !through)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
307 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
308
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
309 // Grow outline
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
310 i = (under ? 4 : 0) + (through ? 4 : 0);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
311 ol->points = realloc(ol->points, sizeof(FT_Vector) *
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
312 (ol->n_points + i));
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
313 ol->tags = realloc(ol->tags, ol->n_points + i);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
314 i = !!under + !!through;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
315 ol->contours = realloc(ol->contours, sizeof(short) *
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
316 (ol->n_contours + i));
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
317
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
318 // If the bearing is negative, the glyph starts left of the current
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
319 // pen position
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
320 bear = FFMIN(face->glyph->metrics.horiBearingX, 0);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
321 // We're adding half a pixel to avoid small gaps
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
322 advance = d16_to_d6(glyph->advance.x) + 32;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
323 y_scale = face->size->metrics.y_scale;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
324
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
325 // Reverse drawing direction for non-truetype fonts
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
326 dir = FT_Outline_Get_Orientation(ol);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
327
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
328 // Add points to the outline
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
329 if (under && ps) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
330 int pos, size;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
331 pos = FT_MulFix(ps->underlinePosition, y_scale * font->scale_y);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
332 size = FT_MulFix(ps->underlineThickness,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
333 y_scale * font->scale_y / 2);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
334
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
335 if (pos > 0 || size <= 0)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
336 return 1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
337
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
338 FT_Vector points[4] = {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
339 {.x = bear, .y = pos + size},
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
340 {.x = advance, .y = pos + size},
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
341 {.x = advance, .y = pos - size},
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
342 {.x = bear, .y = pos - size},
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
343 };
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28860
diff changeset
344
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
345 if (dir == FT_ORIENTATION_TRUETYPE) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
346 for (i = 0; i < 4; i++) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
347 ol->points[ol->n_points] = points[i];
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
348 ol->tags[ol->n_points++] = 1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
349 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
350 } else {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
351 for (i = 3; i >= 0; i--) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
352 ol->points[ol->n_points] = points[i];
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
353 ol->tags[ol->n_points++] = 1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
354 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
355 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
356
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
357 ol->contours[ol->n_contours++] = ol->n_points - 1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
358 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
359
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
360 if (through && os2) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
361 int pos, size;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
362 pos = FT_MulFix(os2->yStrikeoutPosition, y_scale * font->scale_y);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
363 size = FT_MulFix(os2->yStrikeoutSize, y_scale * font->scale_y / 2);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
364
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
365 if (pos < 0 || size <= 0)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
366 return 1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
367
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
368 FT_Vector points[4] = {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
369 {.x = bear, .y = pos + size},
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
370 {.x = advance, .y = pos + size},
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
371 {.x = advance, .y = pos - size},
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
372 {.x = bear, .y = pos - size},
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
373 };
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
374
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
375 if (dir == FT_ORIENTATION_TRUETYPE) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
376 for (i = 0; i < 4; i++) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
377 ol->points[ol->n_points] = points[i];
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
378 ol->tags[ol->n_points++] = 1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
379 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
380 } else {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
381 for (i = 3; i >= 0; i--) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
382 ol->points[ol->n_points] = points[i];
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
383 ol->tags[ol->n_points++] = 1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
384 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
385 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
386
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
387 ol->contours[ol->n_contours++] = ol->n_points - 1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
388 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
389
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
390 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
391 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
392
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
393 /**
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
394 * Slightly embold a glyph without touching its metrics
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
395 */
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
396 static void ass_glyph_embolden(FT_GlyphSlot slot)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
397 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
398 int str;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
399
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
400 if (slot->format != FT_GLYPH_FORMAT_OUTLINE)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
401 return;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
402
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
403 str = FT_MulFix(slot->face->units_per_EM,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
404 slot->face->size->metrics.y_scale) / 64;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
405
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
406 FT_Outline_Embolden(&slot->outline, str);
21614
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
407 }
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
408
21630
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
409 /**
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
410 * \brief Get a glyph
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
411 * \param ch character code
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
412 **/
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
413 FT_Glyph ass_font_get_glyph(void *fontconfig_priv, ASS_Font *font,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
414 uint32_t ch, ASS_Hinting hinting, int deco)
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
415 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
416 int error;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
417 int index = 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
418 int i;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
419 FT_Glyph glyph;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
420 FT_Face face = 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
421 int flags = 0;
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
422 int vertical = font->desc.vertical;
21350
5337cdeec169 Skip glyphs with char code < 0x20.
eugeni
parents: 21349
diff changeset
423
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
424 if (ch < 0x20)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
425 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
426 // Handle NBSP like a regular space when rendering the glyph
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
427 if (ch == 0xa0)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
428 ch = ' ';
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
429 if (font->n_faces == 0)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
430 return 0;
21619
b4b51eb2904f Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents: 21618
diff changeset
431
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
432 for (i = 0; i < font->n_faces; ++i) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
433 face = font->faces[i];
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
434 index = FT_Get_Char_Index(face, ch);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
435 if (index)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
436 break;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
437 }
21619
b4b51eb2904f Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents: 21618
diff changeset
438
27393
4876c89bafdd Rename font-related preprocessor directives.
diego
parents: 26738
diff changeset
439 #ifdef CONFIG_FONTCONFIG
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
440 if (index == 0) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
441 int face_idx;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
442 ass_msg(font->library, MSGL_INFO,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
443 "Glyph 0x%X not found, selecting one more "
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
444 "font for (%s, %d, %d)", ch, font->desc.family,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
445 font->desc.bold, font->desc.italic);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
446 face_idx = add_face(fontconfig_priv, font, ch);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
447 if (face_idx >= 0) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
448 face = font->faces[face_idx];
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
449 index = FT_Get_Char_Index(face, ch);
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
450 if (index == 0 && face->num_charmaps > 0) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
451 ass_msg(font->library, MSGL_WARN,
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
452 "Glyph 0x%X not found, falling back to first charmap", ch);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
453 FT_CharMap cur = face->charmap;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
454 FT_Set_Charmap(face, face->charmaps[0]);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
455 index = FT_Get_Char_Index(face, ch);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
456 FT_Set_Charmap(face, cur);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
457 }
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
458 if (index == 0) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
459 ass_msg(font->library, MSGL_ERR,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
460 "Glyph 0x%X not found in font for (%s, %d, %d)",
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
461 ch, font->desc.family, font->desc.bold,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
462 font->desc.italic);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
463 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
464 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
465 }
21351
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
466 #endif
c611dfc4cb85 If a glyph is not found in the current font, switch to another one.
eugeni
parents: 21350
diff changeset
467
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
468 flags = FT_LOAD_NO_BITMAP | FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
469 | FT_LOAD_IGNORE_TRANSFORM;
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
470 switch (hinting) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
471 case ASS_HINTING_NONE:
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
472 flags |= FT_LOAD_NO_HINTING;
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
473 break;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
474 case ASS_HINTING_LIGHT:
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
475 flags |= FT_LOAD_FORCE_AUTOHINT | FT_LOAD_TARGET_LIGHT;
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
476 break;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
477 case ASS_HINTING_NORMAL:
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
478 flags |= FT_LOAD_FORCE_AUTOHINT;
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
479 break;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
480 case ASS_HINTING_NATIVE:
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
481 break;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
482 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28860
diff changeset
483
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
484 error = FT_Load_Glyph(face, index, flags);
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
485 if (error) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
486 ass_msg(font->library, MSGL_WARN, "Error loading glyph, index %d",
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
487 index);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
488 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
489 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
490 if (!(face->style_flags & FT_STYLE_FLAG_ITALIC) &&
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
491 (font->desc.italic > 55)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
492 FT_GlyphSlot_Oblique(face->glyph);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
493 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28860
diff changeset
494
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
495 if (!(face->style_flags & FT_STYLE_FLAG_BOLD) &&
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
496 (font->desc.bold > 80)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
497 ass_glyph_embolden(face->glyph);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
498 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
499 error = FT_Get_Glyph(face->glyph, &glyph);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
500 if (error) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
501 ass_msg(font->library, MSGL_WARN, "Error loading glyph, index %d",
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
502 index);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
503 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
504 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
505
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
506 // Rotate glyph, if needed
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
507 if (vertical && ch >= VERTICAL_LOWER_BOUND) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
508 FT_Matrix m = { 0, double_to_d16(-1.0), double_to_d16(1.0), 0 };
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
509 FT_Outline_Transform(&((FT_OutlineGlyph) glyph)->outline, &m);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
510 FT_Outline_Translate(&((FT_OutlineGlyph) glyph)->outline,
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
511 face->glyph->metrics.vertAdvance,
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
512 0);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
513 glyph->advance.x = face->glyph->linearVertAdvance;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
514 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
515
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
516 // Apply scaling and shift
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
517 FT_Matrix scale = { double_to_d16(font->scale_x), 0, 0,
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
518 double_to_d16(font->scale_y) };
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
519 FT_Outline *outl = &((FT_OutlineGlyph) glyph)->outline;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
520 FT_Outline_Transform(outl, &scale);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
521 FT_Outline_Translate(outl, font->v.x, font->v.y);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
522 glyph->advance.x *= font->scale_x;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
523
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
524 ass_strike_outline_glyph(face, font, glyph, deco & DECO_UNDERLINE,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
525 deco & DECO_STRIKETHROUGH);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
526
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
527 return glyph;
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
528 }
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
529
21630
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
530 /**
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
531 * \brief Get kerning for the pair of glyphs.
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
532 **/
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
533 FT_Vector ass_font_get_kerning(ASS_Font *font, uint32_t c1, uint32_t c2)
21614
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
534 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
535 FT_Vector v = { 0, 0 };
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
536 int i;
21619
b4b51eb2904f Keep reselected fonts in an array, adding new ones to the end. Glyph
eugeni
parents: 21618
diff changeset
537
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
538 if (font->desc.vertical)
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
539 return v;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
540
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
541 for (i = 0; i < font->n_faces; ++i) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
542 FT_Face face = font->faces[i];
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
543 int i1 = FT_Get_Char_Index(face, c1);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
544 int i2 = FT_Get_Char_Index(face, c2);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
545 if (i1 && i2) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
546 if (FT_HAS_KERNING(face))
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
547 FT_Get_Kerning(face, i1, i2, FT_KERNING_DEFAULT, &v);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
548 return v;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
549 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
550 if (i1 || i2) // these glyphs are from different font faces, no kerning information
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
551 return v;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
552 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
553 return v;
21614
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
554 }
5d2ca7ca18b5 Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents: 21460
diff changeset
555
21630
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
556 /**
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
557 * \brief Deallocate ASS_Font
21630
0d8005d2fe5c Update some comments.
eugeni
parents: 21620
diff changeset
558 **/
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
559 void ass_font_free(ASS_Font *font)
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
560 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
561 int i;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
562 for (i = 0; i < font->n_faces; ++i)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
563 if (font->faces[i])
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
564 FT_Done_Face(font->faces[i]);
31875
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
565 free(font->desc.family);
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 29325
diff changeset
566 free(font);
21277
0603972f083c Move fonts-related code to a separate file.
eugeni
parents:
diff changeset
567 }
31853
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
568
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
569 /**
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
570 * \brief Calculate the cbox of a series of points
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
571 */
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
572 static void
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
573 get_contour_cbox(FT_BBox *box, FT_Vector *points, int start, int end)
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
574 {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
575 box->xMin = box->yMin = INT_MAX;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
576 box->xMax = box->yMax = INT_MIN;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
577 int i;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
578
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
579 for (i = start; i <= end; i++) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
580 box->xMin = (points[i].x < box->xMin) ? points[i].x : box->xMin;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
581 box->xMax = (points[i].x > box->xMax) ? points[i].x : box->xMax;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
582 box->yMin = (points[i].y < box->yMin) ? points[i].y : box->yMin;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
583 box->yMax = (points[i].y > box->yMax) ? points[i].y : box->yMax;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
584 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
585 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
586
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
587 /**
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
588 * \brief Determine winding direction of a contour
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
589 * \return direction; 0 = clockwise
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
590 */
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
591 static int get_contour_direction(FT_Vector *points, int start, int end)
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
592 {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
593 int i;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
594 long long sum = 0;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
595 int x = points[start].x;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
596 int y = points[start].y;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
597 for (i = start + 1; i <= end; i++) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
598 sum += x * (points[i].y - y) - y * (points[i].x - x);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
599 x = points[i].x;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
600 y = points[i].y;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
601 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
602 sum += x * (points[start].y - y) - y * (points[start].x - x);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
603 return sum > 0;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
604 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
605
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
606 /**
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
607 * \brief Fix-up stroker result for huge borders by removing inside contours
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
608 * that would reverse in size
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
609 */
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
610 void fix_freetype_stroker(FT_OutlineGlyph glyph, int border_x, int border_y)
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
611 {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
612 int nc = glyph->outline.n_contours;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
613 int begin, stop;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
614 char modified = 0;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
615 char *valid_cont = malloc(nc);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
616 int start = 0;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
617 int end = -1;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
618 FT_BBox *boxes = malloc(nc * sizeof(FT_BBox));
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
619 int i, j;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
620 int inside_direction;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
621
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
622 inside_direction = FT_Outline_Get_Orientation(&glyph->outline) ==
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
623 FT_ORIENTATION_TRUETYPE;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
624
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
625 // create a list of cboxes of the contours
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
626 for (i = 0; i < nc; i++) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
627 start = end + 1;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
628 end = glyph->outline.contours[i];
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
629 get_contour_cbox(&boxes[i], glyph->outline.points, start, end);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
630 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
631
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
632 // for each contour, check direction and whether it's "outside"
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
633 // or contained in another contour
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
634 end = -1;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
635 for (i = 0; i < nc; i++) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
636 start = end + 1;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
637 end = glyph->outline.contours[i];
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
638 int dir = get_contour_direction(glyph->outline.points, start, end);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
639 valid_cont[i] = 1;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
640 if (dir == inside_direction) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
641 for (j = 0; j < nc; j++) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
642 if (i == j)
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
643 continue;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
644 if (boxes[i].xMin >= boxes[j].xMin &&
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
645 boxes[i].xMax <= boxes[j].xMax &&
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
646 boxes[i].yMin >= boxes[j].yMin &&
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
647 boxes[i].yMax <= boxes[j].yMax)
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
648 goto check_inside;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
649 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
650 /* "inside" contour but we can't find anything it could be
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
651 * inside of - assume the font is buggy and it should be
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
652 * an "outside" contour, and reverse it */
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
653 for (j = 0; j < (end + 1 - start) / 2; j++) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
654 FT_Vector temp = glyph->outline.points[start + j];
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
655 char temp2 = glyph->outline.tags[start + j];
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
656 glyph->outline.points[start + j] = glyph->outline.points[end - j];
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
657 glyph->outline.points[end - j] = temp;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
658 glyph->outline.tags[start + j] = glyph->outline.tags[end - j];
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
659 glyph->outline.tags[end - j] = temp2;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
660 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
661 dir ^= 1;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
662 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
663 check_inside:
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
664 if (dir == inside_direction) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
665 FT_BBox box;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
666 get_contour_cbox(&box, glyph->outline.points, start, end);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
667 int width = box.xMax - box.xMin;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
668 int height = box.yMax - box.yMin;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
669 if (width < border_x * 2 || height < border_y * 2) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
670 valid_cont[i] = 0;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
671 modified = 1;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
672 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
673 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
674 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
675
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
676 // zero-out contours that can be removed; much simpler than copying
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
677 if (modified) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
678 for (i = 0; i < nc; i++) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
679 if (valid_cont[i])
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
680 continue;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
681 begin = (i == 0) ? 0 : glyph->outline.contours[i - 1] + 1;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
682 stop = glyph->outline.contours[i];
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
683 for (j = begin; j <= stop; j++) {
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
684 glyph->outline.points[j].x = 0;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
685 glyph->outline.points[j].y = 0;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
686 glyph->outline.tags[j] = 0;
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
687 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
688 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
689 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
690
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
691 free(boxes);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
692 free(valid_cont);
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
693 }
e64df5862cea Import libass 0.9.10
greg
parents: 30200
diff changeset
694