0
|
1 /*
|
|
2 * Copyright (c) 2004 Kazunori "jagarl" Ueno
|
|
3 * Copyright (c) 2000, 2001 Yuki Sawada
|
|
4 * All rights reserved.
|
|
5 *
|
|
6 * Redistribution and use in source and binary forms, with or without
|
|
7 * modification, are permitted provided that the following conditions
|
|
8 * are met:
|
|
9 * 1. Redistributions of source code must retain the above copyright
|
|
10 * notice, this list of conditions and the following disclaimer.
|
|
11 * 2. Redistributions in binary form must reproduce the above copyright
|
|
12 * notice, this list of conditions and the following disclaimer in the
|
|
13 * documentation and/or other materials provided with the distribution.
|
|
14 * 3. The name of the author may not be used to endorse or promote products
|
|
15 * derived from this software without specific prior written permission.
|
|
16 *
|
|
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27 */
|
|
28
|
|
29 #include <stdio.h>
|
|
30 #include <stdlib.h>
|
|
31 #include <string.h>
|
|
32 #include <stdexcept>
|
|
33 #include <string>
|
|
34
|
|
35 using namespace std;
|
|
36
|
|
37 #include "font.h"
|
|
38 #include "font_peer.h"
|
|
39 #include "codeconv.h"
|
|
40
|
|
41 namespace XKFont {
|
|
42
|
|
43 typedef struct _FontLibrary {
|
|
44 FT_Library super;
|
|
45 int ref_count;
|
|
46 char **paths;
|
|
47 int num_paths;
|
|
48 } FontLibrary;
|
|
49
|
|
50 static FontLibrary *library = NULL;
|
|
51
|
|
52 static char *default_paths[] = {
|
|
53 ".",
|
|
54 "/",
|
|
55 "/usr/X11R6/lib/X11/fonts/TrueType",
|
|
56 "/usr/local/share/fonts/TrueType",
|
|
57 "/usr//share/fonts/TrueType",
|
|
58 "/usr//share/fonts/tt",
|
|
59 NULL
|
|
60 };
|
|
61
|
|
62 static void font_library_ft2_add_path(const char *path);
|
|
63 static void font_library_ft2_remove_path(const char *path);
|
|
64 static void font_library_ft2_add_path(const char *path);
|
|
65 static char *font_library_ft2_build_path(const char *base, const char *name);
|
|
66
|
|
67 static int
|
|
68 font_library_ft2_alloc()
|
|
69 {
|
|
70 int i;
|
|
71
|
|
72 if (!library) {
|
|
73 library = (FontLibrary*) calloc(sizeof(FontLibrary), 1);
|
|
74 if (library) {
|
|
75 if (FT_Init_FreeType(&library->super))
|
|
76 goto _1;
|
|
77 fprintf(stderr, "XKFont::font_library_ft2_alloc : FreeType allocated successfully.\n");
|
|
78 for (i = 0; default_paths[i]; i++)
|
|
79 font_library_ft2_add_path(default_paths[i]);
|
|
80 }
|
|
81 }
|
|
82
|
|
83 library->ref_count++;
|
|
84 return 1;
|
|
85
|
|
86 _1:
|
|
87 free(library);
|
|
88 library = NULL;
|
|
89 fprintf(stderr, "XKFont::font_library_ft2_alloc: Couldn't allocate FreeType.\n");
|
|
90 return 0;
|
|
91 }
|
|
92
|
|
93 static void
|
|
94 font_library_ft2_free()
|
|
95 {
|
|
96 int i;
|
|
97
|
|
98 if (!library || library->ref_count <= 0)
|
|
99 return;
|
|
100 if (--library->ref_count == 0) {
|
|
101 FT_Done_FreeType(library->super);
|
|
102 for (i = 0; i < library->num_paths; i++)
|
|
103 free(library->paths[i]);
|
|
104 free(library->paths);
|
|
105 free(library);
|
|
106 library = NULL;
|
|
107 fprintf(stderr, "XKFont::font_library_ft2_free : FreeType done.\n");
|
|
108 }
|
|
109 }
|
|
110
|
|
111 static void
|
|
112 font_library_ft2_add_path(const char *path)
|
|
113 {
|
|
114 library->num_paths++;
|
|
115 if (!library->paths)
|
|
116 library->paths = (char**) malloc(sizeof(char *));
|
|
117 else
|
|
118 library->paths = (char**) realloc( (void*)library->paths,
|
|
119 library->num_paths * sizeof(char *));
|
|
120 library->paths[library->num_paths - 1] = strdup(path);
|
|
121 }
|
|
122
|
|
123 static void
|
|
124 font_library_ft2_remove_path(const char *path)
|
|
125 {
|
|
126 int i, j;
|
|
127
|
|
128 for (i = 0; i < library->num_paths; i++) {
|
|
129 if (!strcmp(path, library->paths[i])) {
|
|
130 library->num_paths--;
|
|
131 for (j = i; j < library->num_paths; j++)
|
|
132 library->paths[j] = library->paths[j + 1];
|
|
133 if (library->num_paths > 0)
|
|
134 library->paths = (char**) realloc(library->paths,
|
|
135 library->num_paths * sizeof(char *));
|
|
136 else {
|
|
137 free(library->paths);
|
|
138 library->paths = NULL;
|
|
139 }
|
|
140 }
|
|
141 }
|
|
142 }
|
|
143
|
|
144 typedef struct _FontEncoding {
|
|
145 FT_UShort platform_id;
|
|
146 FT_Encoding encoding;
|
|
147 FontCodeConverter conv_func;
|
|
148 } FontEncoding;
|
|
149
|
|
150 static FontEncoding encodings[] = {
|
|
151 { 3, (FT_Encoding)ft_encoding_unicode, codeconv_euc_to_unicode },
|
|
152 { 3, (FT_Encoding)ft_encoding_sjis, codeconv_euc_to_sjis },
|
|
153 { 1, (FT_Encoding)ft_encoding_apple_roman, codeconv_euc_to_latin1 },
|
|
154 { (FT_UShort)-1, (FT_Encoding)-1, NULL }
|
|
155 };
|
|
156
|
|
157 static char *
|
|
158 font_library_ft2_build_path(const char *base, const char *name)
|
|
159 {
|
|
160 char *path;
|
|
161 const char *strs[] = { base, "/", name, NULL };
|
|
162 int i = 0;
|
|
163
|
|
164 path = (char*) calloc(sizeof(char), strlen(base) + strlen(name) + 2);
|
|
165 if (path)
|
|
166 while (strs[i])
|
|
167 strcat(path, strs[i++]);
|
|
168
|
|
169 return path;
|
|
170 }
|
|
171
|
|
172 PeerFt2::PeerFt2(const char *name, int index, int hsize, int vsize)
|
|
173 {
|
|
174 int i,j;
|
|
175
|
|
176 font_library_ft2_alloc();
|
|
177
|
|
178 for (i = 0; i < library->num_paths; i++) {
|
|
179 char *path = font_library_ft2_build_path(library->paths[i], name);
|
|
180 if (path) {
|
|
181 if (FT_New_Face(library->super, path, index, &face))
|
|
182 face = NULL;
|
|
183 free(path);
|
|
184 }
|
|
185 if (face)
|
|
186 break;
|
|
187 }
|
|
188
|
|
189 if (!face) {
|
|
190 string err = string("XKFont::PeerFt2::PeerFt : Cannot open font(TrueType) : ")+name;
|
|
191 throw std::invalid_argument(err);
|
|
192 }
|
|
193
|
|
194 conv_func = 0;
|
|
195 for (i=0; encodings[i].conv_func != 0; i++) {
|
|
196 FT_UShort platform_id = encodings[i].platform_id;
|
|
197 FT_Encoding encoding = encodings[i].encoding;
|
|
198 for (j = 0; j < face->num_charmaps; j++) {
|
|
199 FT_CharMap cmap = face->charmaps[j];
|
|
200 if (cmap->platform_id == platform_id && cmap->encoding == encoding) {
|
|
201 if (FT_Set_Charmap(face, cmap) == 0) {
|
|
202 conv_func = encodings[i].conv_func;
|
|
203 break;
|
|
204 }
|
|
205 }
|
|
206 }
|
|
207 if (conv_func) break;
|
|
208 }
|
|
209 if (conv_func == 0) {
|
|
210 FT_Done_Face(face);
|
|
211 fprintf(stderr,"cannot find charmap\n");
|
|
212 string err = string("XKFont::PeerFt2::PeerFt : No supported code converter of font (TrueType) ")+name;
|
|
213 throw std::invalid_argument(err);
|
|
214 }
|
|
215 FT_Set_Pixel_Sizes(face, hsize, vsize);
|
|
216
|
|
217 }
|
|
218
|
|
219 PeerFt2::~PeerFt2() {
|
|
220 FT_Done_Face(face);
|
|
221 font_library_ft2_free();
|
|
222 }
|
|
223
|
|
224 bool
|
|
225 PeerFt2::GlyphCreate(unsigned int code, Glyph* glyph)
|
|
226 {
|
|
227 FT_GlyphSlot slot;
|
|
228 FT_UInt index;
|
|
229 int bmsize;
|
|
230
|
|
231 if (face == 0) return false;
|
|
232 code = conv_func(code);
|
|
233 if (code == 0) return false;
|
|
234 index = FT_Get_Char_Index(face, code);
|
|
235 if (index == 0) return false;
|
|
236
|
|
237 /* Don't consider error */
|
|
238 slot = face->glyph;
|
|
239 if (slot) {
|
|
240 // if (! FT_Load_Glyph(face, index, FT_LOAD_DEFAULT)) {
|
|
241 // BITMAP だと なぜか render してくれない……
|
|
242 // LOAD_DEFAULT でも、下に対応コードを付けたので一応は大丈夫
|
|
243 if (! FT_Load_Glyph(face, index, FT_LOAD_NO_BITMAP)) {
|
|
244 FT_Render_Glyph(slot, ft_render_mode_normal);
|
|
245 }
|
|
246 }
|
|
247
|
|
248 glyph->bitmap_left = slot->bitmap_left;
|
|
249 glyph->bitmap_top = slot->bitmap_top;
|
|
250 glyph->bitmap.width = slot->bitmap.width;
|
|
251 glyph->bitmap.rows = slot->bitmap.rows;
|
|
252
|
|
253 /*
|
|
254 glyph->metrics.ascender = private->face->size->metrics.ascender >> 6;
|
|
255 glyph->metrics.descender = private->face->size->metrics.descender >> 6;
|
|
256 */
|
|
257
|
|
258 glyph->advance.x = slot->advance.x >> 6;
|
|
259 glyph->advance.y = slot->advance.y >> 6;
|
|
260
|
|
261 bmsize = glyph->bitmap.width * glyph->bitmap.rows;
|
|
262 if (bmsize > 0) {
|
|
263 glyph->bitmap.buffer = new unsigned char[bmsize];
|
|
264 memcpy(glyph->bitmap.buffer, slot->bitmap.buffer, bmsize);
|
|
265 }
|
|
266 // なぜか Render したのに MONO なことがある……
|
|
267 /* for freetype < 2.1.3, use ``ft_pixel_mode_mono'' */
|
|
268 if (slot->bitmap.pixel_mode == ft_pixel_mode_mono) {int i,j;
|
|
269 // if (slot->bitmap.pixel_mode == FT_PIXEL_MODE_MONO) {int i,j;
|
|
270 char* d = (char*)slot->bitmap.buffer;
|
|
271 for (i=0; i<glyph->bitmap.rows; i++) {
|
|
272 int flag = *d++; int len = 8;
|
|
273 unsigned char* buf = glyph->bitmap.buffer + i*slot->bitmap.width;
|
|
274 for (j=0; j<glyph->bitmap.width; j++) {
|
|
275 if (len == 0) {
|
|
276 flag = *d++;
|
|
277 len = 8;
|
|
278 }
|
|
279 if (flag & 0x80) *buf++ = 0xff;
|
|
280 else *buf++ = 0;
|
|
281 flag <<= 1;
|
|
282 len--;
|
|
283 }
|
|
284 }
|
|
285 }
|
|
286
|
|
287 return true;
|
|
288
|
|
289 }
|
|
290
|
|
291 } /* end of namespace XKFont */
|
|
292
|