annotate font/font_face.cc @ 28:f1f22bfc4496

* Allow the building of scn2kdump by typing "make scn2kdump" in scn2k/
author thib
date Fri, 06 Mar 2009 18:30:19 +0000
parents fa8511a21d05
children 15a18fbe6f21
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>
223b71206888 Initial import
thib
parents:
diff changeset
30 #include"font.h"
223b71206888 Initial import
thib
parents:
diff changeset
31 #include"font_peer.h"
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
223b71206888 Initial import
thib
parents:
diff changeset
38 class Cache : public std::map<unsigned int, Glyph*> {
223b71206888 Initial import
thib
parents:
diff changeset
39 public:
223b71206888 Initial import
thib
parents:
diff changeset
40 Cache() {}
223b71206888 Initial import
thib
parents:
diff changeset
41 ~Cache() {
223b71206888 Initial import
thib
parents:
diff changeset
42 iterator it;
223b71206888 Initial import
thib
parents:
diff changeset
43 for (it = begin(); it != end(); it++)
223b71206888 Initial import
thib
parents:
diff changeset
44 delete (it->second);
223b71206888 Initial import
thib
parents:
diff changeset
45 }
223b71206888 Initial import
thib
parents:
diff changeset
46 };
223b71206888 Initial import
thib
parents:
diff changeset
47
223b71206888 Initial import
thib
parents:
diff changeset
48 Face::Face(const char *name_orig, int index, int hsize, int vsize)
223b71206888 Initial import
thib
parents:
diff changeset
49 {
223b71206888 Initial import
thib
parents:
diff changeset
50 cache = new Cache;
223b71206888 Initial import
thib
parents:
diff changeset
51
223b71206888 Initial import
thib
parents:
diff changeset
52 /* name: ';' 区切りで複数指定可能 */
223b71206888 Initial import
thib
parents:
diff changeset
53 char* name = new char[strlen(name_orig)+1];
223b71206888 Initial import
thib
parents:
diff changeset
54 while(*name_orig != 0) {
223b71206888 Initial import
thib
parents:
diff changeset
55 const char* next_name = strchr(name_orig, ';');
223b71206888 Initial import
thib
parents:
diff changeset
56 if (next_name) {
223b71206888 Initial import
thib
parents:
diff changeset
57 strncpy(name, name_orig, next_name - name_orig);
223b71206888 Initial import
thib
parents:
diff changeset
58 name[next_name-name_orig] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
59 name_orig = next_name + 1;
223b71206888 Initial import
thib
parents:
diff changeset
60 } else {
223b71206888 Initial import
thib
parents:
diff changeset
61 strcpy(name, name_orig);
223b71206888 Initial import
thib
parents:
diff changeset
62 name_orig += strlen(name_orig);
223b71206888 Initial import
thib
parents:
diff changeset
63 }
223b71206888 Initial import
thib
parents:
diff changeset
64 if (strstr(name, "fn.dat")) {
223b71206888 Initial import
thib
parents:
diff changeset
65 peer.push_back(new PeerFn(name, index, hsize, vsize));
223b71206888 Initial import
thib
parents:
diff changeset
66 } else if (strstr(name, ".ttf") || strstr(name, ".ttc")) {
223b71206888 Initial import
thib
parents:
diff changeset
67 peer.push_back(new PeerFt2(name, index, hsize, vsize));
223b71206888 Initial import
thib
parents:
diff changeset
68 #if USE_X11
223b71206888 Initial import
thib
parents:
diff changeset
69 } else {
223b71206888 Initial import
thib
parents:
diff changeset
70 peer.push_back(new PeerX11(name, index, hsize, vsize));
223b71206888 Initial import
thib
parents:
diff changeset
71 #endif
223b71206888 Initial import
thib
parents:
diff changeset
72 }
223b71206888 Initial import
thib
parents:
diff changeset
73 }
7
fa8511a21d05 Fixes somes memory leaks
thib
parents: 0
diff changeset
74 delete[] name;
0
223b71206888 Initial import
thib
parents:
diff changeset
75 return;
223b71206888 Initial import
thib
parents:
diff changeset
76 }
223b71206888 Initial import
thib
parents:
diff changeset
77
223b71206888 Initial import
thib
parents:
diff changeset
78 Face::~Face() {
223b71206888 Initial import
thib
parents:
diff changeset
79 delete cache;
223b71206888 Initial import
thib
parents:
diff changeset
80 }
223b71206888 Initial import
thib
parents:
diff changeset
81
223b71206888 Initial import
thib
parents:
diff changeset
82 Glyph*
223b71206888 Initial import
thib
parents:
diff changeset
83 Face::GlyphLoad(unsigned int code) {
223b71206888 Initial import
thib
parents:
diff changeset
84 if (cache->count(code))
223b71206888 Initial import
thib
parents:
diff changeset
85 return (*cache)[code];
223b71206888 Initial import
thib
parents:
diff changeset
86
223b71206888 Initial import
thib
parents:
diff changeset
87 Glyph* g = new Glyph;
223b71206888 Initial import
thib
parents:
diff changeset
88 iterator it;
223b71206888 Initial import
thib
parents:
diff changeset
89 for (it=peer.begin(); it != peer.end(); it++) {
223b71206888 Initial import
thib
parents:
diff changeset
90 if ( (*it)->GlyphCreate(code, g)) break;
223b71206888 Initial import
thib
parents:
diff changeset
91 }
223b71206888 Initial import
thib
parents:
diff changeset
92 if (it == peer.end()) {
223b71206888 Initial import
thib
parents:
diff changeset
93 fprintf(stderr,"Cannot find glyph, code %04x\n",code);
223b71206888 Initial import
thib
parents:
diff changeset
94 g->bitmap_left = 0;
223b71206888 Initial import
thib
parents:
diff changeset
95 g->bitmap_top = 0;
223b71206888 Initial import
thib
parents:
diff changeset
96 g->bitmap.width = 0;
223b71206888 Initial import
thib
parents:
diff changeset
97 g->bitmap.rows = 0;
223b71206888 Initial import
thib
parents:
diff changeset
98 g->bitmap.buffer = new unsigned char[1];
223b71206888 Initial import
thib
parents:
diff changeset
99 g->bitmap.buffer[0] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
100 g->advance.x = 0;
223b71206888 Initial import
thib
parents:
diff changeset
101 g->advance.y = 0;
223b71206888 Initial import
thib
parents:
diff changeset
102 }
223b71206888 Initial import
thib
parents:
diff changeset
103 (*cache)[code] = g;
223b71206888 Initial import
thib
parents:
diff changeset
104 return g;
223b71206888 Initial import
thib
parents:
diff changeset
105 }
223b71206888 Initial import
thib
parents:
diff changeset
106
223b71206888 Initial import
thib
parents:
diff changeset
107 class FontImpl {
223b71206888 Initial import
thib
parents:
diff changeset
108 public:
223b71206888 Initial import
thib
parents:
diff changeset
109 std::map<int, Face*> cache;
223b71206888 Initial import
thib
parents:
diff changeset
110 std::string fontname;
223b71206888 Initial import
thib
parents:
diff changeset
111 int size;
223b71206888 Initial import
thib
parents:
diff changeset
112 ~FontImpl();
223b71206888 Initial import
thib
parents:
diff changeset
113 };
223b71206888 Initial import
thib
parents:
diff changeset
114
223b71206888 Initial import
thib
parents:
diff changeset
115 FontImpl::~FontImpl() {
223b71206888 Initial import
thib
parents:
diff changeset
116 std::map<int,Face*>::iterator it;
223b71206888 Initial import
thib
parents:
diff changeset
117 for (it=cache.begin(); it!=cache.end(); it++) delete it->second;
223b71206888 Initial import
thib
parents:
diff changeset
118 }
223b71206888 Initial import
thib
parents:
diff changeset
119
223b71206888 Initial import
thib
parents:
diff changeset
120 Font::Font(const char* name, int size) {
223b71206888 Initial import
thib
parents:
diff changeset
121 pimpl = new FontImpl;
223b71206888 Initial import
thib
parents:
diff changeset
122 pimpl->fontname = name;
223b71206888 Initial import
thib
parents:
diff changeset
123 pimpl->size = size;
223b71206888 Initial import
thib
parents:
diff changeset
124 vsize = size;
223b71206888 Initial import
thib
parents:
diff changeset
125 };
223b71206888 Initial import
thib
parents:
diff changeset
126 Font::~Font() {
223b71206888 Initial import
thib
parents:
diff changeset
127 delete pimpl;
223b71206888 Initial import
thib
parents:
diff changeset
128 }
223b71206888 Initial import
thib
parents:
diff changeset
129 Face*
223b71206888 Initial import
thib
parents:
diff changeset
130 Font::FaceLoad(double scale) {
223b71206888 Initial import
thib
parents:
diff changeset
131 std::map<int, Face*>& cache = pimpl->cache;
223b71206888 Initial import
thib
parents:
diff changeset
132 int size = int(scale * pimpl->size);
223b71206888 Initial import
thib
parents:
diff changeset
133 if (cache.find(size) != cache.end()) return cache[size];
223b71206888 Initial import
thib
parents:
diff changeset
134 try {
223b71206888 Initial import
thib
parents:
diff changeset
135 Face* face = new Face(pimpl->fontname.c_str(), 0, size, size);
223b71206888 Initial import
thib
parents:
diff changeset
136 cache[size] = face;
223b71206888 Initial import
thib
parents:
diff changeset
137 return face;
223b71206888 Initial import
thib
parents:
diff changeset
138 } catch(...) {
223b71206888 Initial import
thib
parents:
diff changeset
139 std::cerr << "Cannot create font face; font "<<pimpl->fontname<<", size "<<size<<std::endl;
223b71206888 Initial import
thib
parents:
diff changeset
140 /* 別の大きさを探す */
223b71206888 Initial import
thib
parents:
diff changeset
141 int i;
223b71206888 Initial import
thib
parents:
diff changeset
142 for (i=0; i<size; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
143 if (cache.find(size-i) != cache.end()) return cache[size-i];
223b71206888 Initial import
thib
parents:
diff changeset
144 if (cache.find(size+i) != cache.end()) return cache[size+i];
223b71206888 Initial import
thib
parents:
diff changeset
145 try {
223b71206888 Initial import
thib
parents:
diff changeset
146 Face* face = new Face(pimpl->fontname.c_str(), 0, size-i, size-i);
223b71206888 Initial import
thib
parents:
diff changeset
147 cache[size-i] = face;
223b71206888 Initial import
thib
parents:
diff changeset
148 return face;
223b71206888 Initial import
thib
parents:
diff changeset
149 } catch(...) {};
223b71206888 Initial import
thib
parents:
diff changeset
150 try {
223b71206888 Initial import
thib
parents:
diff changeset
151 Face* face = new Face(pimpl->fontname.c_str(), 0, size+i, size+i);
223b71206888 Initial import
thib
parents:
diff changeset
152 cache[size+i] = face;
223b71206888 Initial import
thib
parents:
diff changeset
153 return face;
223b71206888 Initial import
thib
parents:
diff changeset
154 } catch(...) {};
223b71206888 Initial import
thib
parents:
diff changeset
155 }
223b71206888 Initial import
thib
parents:
diff changeset
156 /* 見つからない */
223b71206888 Initial import
thib
parents:
diff changeset
157 throw;
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 */