annotate font/font_face.cc @ 63:4b9ffe15a87d

Refactor Load/LoadSys
author Thibaut GIRKA <thib@sitedethib.com>
date Sat, 06 Feb 2010 17:29:33 +0100
parents 15a18fbe6f21
children 4416cfac86ae
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
223b71206888 Initial import
thib
parents:
diff changeset
1 /*
223b71206888 Initial import
thib
parents:
diff changeset
2 * Copyright (c) 2000, 2001 Yuki Sawada
223b71206888 Initial import
thib
parents:
diff changeset
3 * All rights reserved.
223b71206888 Initial import
thib
parents:
diff changeset
4 *
223b71206888 Initial import
thib
parents:
diff changeset
5 * Redistribution and use in source and binary forms, with or without
223b71206888 Initial import
thib
parents:
diff changeset
6 * modification, are permitted provided that the following conditions
223b71206888 Initial import
thib
parents:
diff changeset
7 * are met:
223b71206888 Initial import
thib
parents:
diff changeset
8 * 1. Redistributions of source code must retain the above copyright
223b71206888 Initial import
thib
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
223b71206888 Initial import
thib
parents:
diff changeset
10 * 2. Redistributions in binary form must reproduce the above copyright
223b71206888 Initial import
thib
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer in the
223b71206888 Initial import
thib
parents:
diff changeset
12 * documentation and/or other materials provided with the distribution.
223b71206888 Initial import
thib
parents:
diff changeset
13 * 3. The name of the author may not be used to endorse or promote products
223b71206888 Initial import
thib
parents:
diff changeset
14 * derived from this software without specific prior written permission.
223b71206888 Initial import
thib
parents:
diff changeset
15 *
223b71206888 Initial import
thib
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
223b71206888 Initial import
thib
parents:
diff changeset
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
223b71206888 Initial import
thib
parents:
diff changeset
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
223b71206888 Initial import
thib
parents:
diff changeset
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
223b71206888 Initial import
thib
parents:
diff changeset
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
223b71206888 Initial import
thib
parents:
diff changeset
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
223b71206888 Initial import
thib
parents:
diff changeset
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
223b71206888 Initial import
thib
parents:
diff changeset
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
223b71206888 Initial import
thib
parents:
diff changeset
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
223b71206888 Initial import
thib
parents:
diff changeset
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
223b71206888 Initial import
thib
parents:
diff changeset
26 */
223b71206888 Initial import
thib
parents:
diff changeset
27
223b71206888 Initial import
thib
parents:
diff changeset
28 #include <stdlib.h>
223b71206888 Initial import
thib
parents:
diff changeset
29 #include <stdio.h>
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
30 #include "font.h"
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
31 #include "font_peer.h"
0
223b71206888 Initial import
thib
parents:
diff changeset
32 #include <map>
223b71206888 Initial import
thib
parents:
diff changeset
33 #include <string>
223b71206888 Initial import
thib
parents:
diff changeset
34 #include <iostream>
223b71206888 Initial import
thib
parents:
diff changeset
35
223b71206888 Initial import
thib
parents:
diff changeset
36 namespace XKFont {
223b71206888 Initial import
thib
parents:
diff changeset
37
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
38 class Cache : public std::map<unsigned int, Glyph*> {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
39 public:
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
40 Cache() {}
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
41 ~Cache() {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
42 iterator it;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
43 for (it = begin(); it != end(); it++)
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
44 delete (it->second);
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
45 }
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
46 };
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
47
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
48 Face::Face(const char *name_orig, int index, int hsize, int vsize)
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
49 {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
50 cache = new Cache;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
51
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
52 /* name: ';' 区切りで複数指定可能 */
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
53 char* name = new char[strlen(name_orig)+1];
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
54 while(*name_orig != 0) {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
55 const char* next_name = strchr(name_orig, ';');
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
56 if (next_name) {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
57 strncpy(name, name_orig, next_name - name_orig);
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
58 name[next_name-name_orig] = 0;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
59 name_orig = next_name + 1;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
60 } else {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
61 strcpy(name, name_orig);
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
62 name_orig += strlen(name_orig);
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
63 }
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
64 if (strstr(name, "fn.dat")) {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
65 peer.push_back(new PeerFn(name, index, hsize, vsize));
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
66 } else if (strstr(name, ".ttf") || strstr(name, ".ttc")) {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
67 peer.push_back(new PeerFt2(name, index, hsize, vsize));
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
68 #if USE_X11
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
69 } else {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
70 peer.push_back(new PeerX11(name, index, hsize, vsize));
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
71 #endif
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
72 }
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
73 }
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
74 delete[] name;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
75 return;
0
223b71206888 Initial import
thib
parents:
diff changeset
76 }
223b71206888 Initial import
thib
parents:
diff changeset
77
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
78 Face::~Face() {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
79 delete cache;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
80 }
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
81
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
82 Glyph* Face::GlyphLoad(unsigned int code) {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
83 if (cache->count(code))
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
84 return (*cache)[code];
0
223b71206888 Initial import
thib
parents:
diff changeset
85
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
86 Glyph* g = new Glyph;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
87 iterator it;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
88 for (it=peer.begin(); it != peer.end(); it++) {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
89 if ( (*it)->GlyphCreate(code, g)) break;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
90 }
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
91 if (it == peer.end()) {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
92 fprintf(stderr,"Cannot find glyph, code %04x\n",code);
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
93 g->bitmap_left = 0;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
94 g->bitmap_top = 0;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
95 g->bitmap.width = 0;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
96 g->bitmap.rows = 0;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
97 g->bitmap.buffer = new unsigned char[1];
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
98 g->bitmap.buffer[0] = 0;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
99 g->advance.x = 0;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
100 g->advance.y = 0;
0
223b71206888 Initial import
thib
parents:
diff changeset
101 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
102 (*cache)[code] = g;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
103 return g;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
104 }
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
105
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
106 class FontImpl {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
107 public:
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
108 std::map<int, Face*> cache;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
109 std::string fontname;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
110 int size;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
111 ~FontImpl();
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
112 };
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
113
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
114 FontImpl::~FontImpl() {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
115 std::map<int,Face*>::iterator it;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
116 for (it=cache.begin(); it!=cache.end(); it++) delete it->second;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
117 }
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
118
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
119 Font::Font(const char* name, int size) {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
120 pimpl = new FontImpl;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
121 pimpl->fontname = name;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
122 pimpl->size = size;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
123 vsize = size;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
124 };
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
125
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
126 Font::~Font() {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
127 delete pimpl;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
128 }
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
129
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
130 Face* Font::FaceLoad(double scale) {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
131 std::map<int, Face*>& cache = pimpl->cache;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
132 int size = int(scale * pimpl->size);
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
133 if (cache.find(size) != cache.end()) return cache[size];
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
134 try {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
135 Face* face = new Face(pimpl->fontname.c_str(), 0, size, size);
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
136 cache[size] = face;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
137 return face;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
138 } catch(...) {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
139 std::cerr << "Cannot create font face; font "<<pimpl->fontname<<", size "<<size<<std::endl;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
140 /* 別の大きさを探す */
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
141 int i;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
142 for (i=0; i<size; i++) {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
143 if (cache.find(size-i) != cache.end()) return cache[size-i];
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
144 if (cache.find(size+i) != cache.end()) return cache[size+i];
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
145 try {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
146 Face* face = new Face(pimpl->fontname.c_str(), 0, size-i, size-i);
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
147 cache[size-i] = face;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
148 return face;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
149 } catch(...) {};
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
150 try {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
151 Face* face = new Face(pimpl->fontname.c_str(), 0, size+i, size+i);
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
152 cache[size+i] = face;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
153 return face;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
154 } catch(...) {};
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
155 }
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
156 /* 見つからない */
15a18fbe6f21 * Known bugs added to the README
thib
parents: 7
diff changeset
157 throw;
0
223b71206888 Initial import
thib
parents:
diff changeset
158 }
223b71206888 Initial import
thib
parents:
diff changeset
159 }
223b71206888 Initial import
thib
parents:
diff changeset
160
223b71206888 Initial import
thib
parents:
diff changeset
161 } /* namespace XKFont */