Mercurial > mplayer.hg
annotate libass/ass_font.c @ 21319:415e2119e91c
Don't call FT_Set_Transform/FT_Set_Pixel_Sizes if values have not changed.
author | eugeni |
---|---|
date | Mon, 27 Nov 2006 17:33:59 +0000 |
parents | dcfd069efd8f |
children | 4a7e96f1eebf |
rev | line source |
---|---|
21277 | 1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*- |
2 // vim:ts=8:sw=8:noet:ai: | |
3 /* | |
4 Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com> | |
5 | |
6 This program is free software; you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation; either version 2 of the License, or | |
9 (at your option) any later version. | |
10 | |
11 This program is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with this program; if not, write to the Free Software | |
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 */ | |
20 | |
21 #include "config.h" | |
22 | |
23 #include <inttypes.h> | |
24 #include <ft2build.h> | |
25 #include FT_FREETYPE_H | |
26 #include FT_SYNTHESIS_H | |
27 #include FT_GLYPH_H | |
28 | |
29 #include "ass_font.h" | |
30 #include "ass_fontconfig.h" | |
31 #include "mputils.h" | |
32 | |
33 /** | |
34 * Select Microfost Unicode CharMap, if the font has one. | |
35 * Otherwise, let FreeType decide. | |
36 */ | |
37 static void charmap_magic(FT_Face face) | |
38 { | |
39 int i; | |
40 for (i = 0; i < face->num_charmaps; ++i) { | |
41 FT_CharMap cmap = face->charmaps[i]; | |
42 unsigned pid = cmap->platform_id; | |
43 unsigned eid = cmap->encoding_id; | |
44 if (pid == 3 /*microsoft*/ && (eid == 1 /*unicode bmp*/ || eid == 10 /*full unicode*/)) { | |
45 FT_Set_Charmap(face, cmap); | |
46 break; | |
47 } | |
48 } | |
49 } | |
50 | |
21317 | 51 ass_font_t* ass_font_new(FT_Library ftlibrary, void* fc_priv, ass_font_desc_t* desc) |
21277 | 52 { |
53 char* path; | |
54 int index; | |
55 FT_Face face; | |
56 int error; | |
21317 | 57 ass_font_t* font; |
58 | |
59 font = ass_font_cache_find(desc); | |
60 if (font) | |
61 return font; | |
21277 | 62 |
63 path = fontconfig_select(fc_priv, desc->family, desc->bold, desc->italic, &index); | |
64 | |
65 error = FT_New_Face(ftlibrary, path, index, &face); | |
66 if (error) { | |
21279 | 67 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index); |
21317 | 68 return 0; |
21277 | 69 } |
70 | |
71 charmap_magic(face); | |
72 | |
21317 | 73 font = calloc(1, sizeof(ass_font_t)); |
21277 | 74 font->path = strdup(path); |
75 font->index = index; | |
76 font->face = face; | |
77 font->desc.family = strdup(desc->family); | |
78 font->desc.bold = desc->bold; | |
79 font->desc.italic = desc->italic; | |
80 | |
81 font->m.xx = font->m.yy = (FT_Fixed)0x10000L; | |
82 font->m.xy = font->m.yy = 0; | |
83 font->v.x = font->v.y = 0; | |
21314 | 84 font->size = 0; |
21277 | 85 |
21317 | 86 ass_font_cache_add(font); |
87 | |
88 return font; | |
21277 | 89 } |
90 | |
91 void ass_font_set_transform(ass_font_t* font, FT_Matrix* m, FT_Vector* v) | |
92 { | |
21319
415e2119e91c
Don't call FT_Set_Transform/FT_Set_Pixel_Sizes if values have not changed.
eugeni
parents:
21317
diff
changeset
|
93 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
|
94 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
|
95 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
|
96 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
|
97 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
|
98 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
|
99 ) { |
21277 | 100 font->m.xx = m->xx; |
101 font->m.xy = m->xy; | |
102 font->m.yx = m->yx; | |
103 font->m.yy = m->yy; | |
104 font->v.x = v->x; | |
105 font->v.y = v->y; | |
106 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
|
107 } |
21277 | 108 } |
109 | |
110 void ass_font_set_size(ass_font_t* font, int size) | |
111 { | |
21319
415e2119e91c
Don't call FT_Set_Transform/FT_Set_Pixel_Sizes if values have not changed.
eugeni
parents:
21317
diff
changeset
|
112 if (font->size != size) { |
21277 | 113 font->size = size; |
114 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
|
115 } |
21277 | 116 } |
117 | |
118 FT_Glyph ass_font_get_glyph(void* fontconfig_priv, ass_font_t* font, uint32_t ch) | |
119 { | |
120 int error; | |
121 int index; | |
122 FT_Glyph glyph; | |
123 | |
124 index = FT_Get_Char_Index(font->face, ch); | |
125 error = FT_Load_Glyph(font->face, index, FT_LOAD_NO_BITMAP ); | |
126 if (error) { | |
127 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph); | |
128 return 0; | |
129 } | |
130 | |
131 #if (FREETYPE_MAJOR > 2) || \ | |
132 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR >= 2)) || \ | |
133 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR == 1) && (FREETYPE_PATCH >= 10)) | |
134 // FreeType >= 2.1.10 required | |
135 if (!(font->face->style_flags & FT_STYLE_FLAG_ITALIC) && | |
136 (font->desc.italic > 55)) { | |
137 FT_GlyphSlot_Oblique(font->face->glyph); | |
138 } | |
139 #endif | |
140 error = FT_Get_Glyph(font->face->glyph, &glyph); | |
141 if (error) { | |
142 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph); | |
143 return 0; | |
144 } | |
145 | |
146 return glyph; | |
147 } | |
148 | |
149 void ass_font_free(ass_font_t* font) | |
150 { | |
151 if (font->face) FT_Done_Face(font->face); | |
152 if (font->path) free(font->path); | |
153 if (font->desc.family) free(font->desc.family); | |
21317 | 154 free(font); |
21277 | 155 } |