OSDN Git Service

first
[psychlops/cpp.git] / psychlops / core / graphic / psychlops_g_font.cpp
1 /*
2  *  psychlops_g_font.cpp
3  *  Psychlops Standard Library (Universal)
4  *
5  *  Last Modified 2005/09/25 by Kenchi HOSOKAWA
6  *  (C) 2005 Kenchi HOSOKAWA, Kazushi MARUYA, Takao SATO
7  */
8
9 #include <Math.h>
10
11 #include <string>
12 #include <iostream>
13 #include <sstream>
14
15 #define PSYCHLOPS_WINDOW_API_PLATFORM
16 #include "../../platform/psychlops_platform_selector.h"
17
18 #include "psychlops_g_font.h"
19 #include "psychlops_g_canvas.h"
20 #include "psychlops_g_image.h"
21
22
23 #include "../../extension/standard/widgets/psychlops_widgets.h"
24
25
26
27 namespace Psychlops {
28
29         /*
30          Font::~Font() {}
31          double Font::getWidthOf(char * str) {
32          return getWidthOf(std::string(str));
33          }
34          */
35
36         Font::Font() {}
37         Font::Font(double size_, int weight_, Style style_, std::wstring family_) : size(size_), weight(weight_), style(style_) {
38                 family.push_back(family_!=L"default" ? family_ : default_font.family.at(0));
39         }
40         Font::Font(std::wstring family_, double size_, int weight_, Style style_) : size(size_), weight(weight_), style(style_) {
41                 family.push_back(family_!=L"default" ? family_ : default_font.family.at(0));
42         }
43         Font::Font(const wchar_t* family_, double size_, int weight_, Style style_) : size(size_), weight(weight_), style(style_) {
44                 family.push_back(wcsncmp(family_, L"default", 7) ? std::wstring(family_) : default_font.family.at(0));
45         }
46         Font::~Font() {
47         }
48         Font& Font::set(double size_, int weight_, Style style_, std::wstring family_)
49         {
50                 size = size_;
51                 weight = weight_;
52                 style = style_;
53                 if(family_.length()!=0) family.push_back(family_!=L"default" ? family_ : default_font.family.at(0));
54                 return *this;
55         }
56         Font& Font::set(std::wstring family_, double size_, int weight_, Style style_)
57         {
58                 size = size_;
59                 weight = weight_;
60                 style = style_;
61                 if(family_.length()!=0) family.push_back(family_!=L"default" ? family_ : default_font.family.at(0));
62                 return *this;
63         }
64         Font& Font::set(const wchar_t* family_, double size_, int weight_, Style style_)
65         {
66                 size = size_;
67                 weight = weight_;
68                 style = style_;
69                 if(family_!=L"") family.push_back(family_!=L"default" ? std::wstring(family_) : default_font.family.at(0));
70                 return *this;
71         }
72
73
74         extern std::wstring LocalEncodingtoWCHAR(const char*);
75         extern std::wstring LocalEncodingtoWCHAR(const char*, int);
76
77         Letters::Cache::Cache() : id(0), dirty(false) {}
78         Letters::Cache::Cache(APIFontProperties* id__, bool dirty__) : id(id__), dirty(dirty__) {}
79
80         Letters::Letters()
81         : str(), font(Font::default_font), width_(0), height_(0), vertical_align(BASELINE)
82         {
83         }
84         Letters::Letters(const char *init_str, const Font& init_font)
85         : font(init_font), width_(0), height_(0), vertical_align(BASELINE)
86         {
87                 str = LocalEncodingtoWCHAR(init_str);
88                 cache();
89         }
90         Letters::Letters(const char *init_str, int size_, const Font& init_font)
91         : font(init_font), width_(0), height_(0), vertical_align(BASELINE)
92         {
93                 str = LocalEncodingtoWCHAR(init_str, size_);
94                 cache();
95         }
96         Letters::Letters(const std::wstring &init_str, const Font& init_font)
97         : str(init_str), font(init_font), width_(0), height_(0), vertical_align(BASELINE)
98         {
99                 cache();
100         }
101         //      Letters::Letters(const std::wstring init_str, const Font& init_font, Canvas &cnvs) : str(init_str), font(init_font) {
102         //              cnvs.loadLetters(init_str, init_font);
103         //      }
104         Letters::~Letters() {
105                 if(!caches.empty()) {
106                         std::vector<DrawableWithCache*> tmp;
107                         for(CacheID::iterator i=caches.begin(); i!=caches.end(); i++) tmp.push_back(i->first);
108                         for(int i=0; i<tmp.size(); i++) tmp[i]->uncacheLetters(*this);
109                 }
110         }
111         Font& Letters::getFont() {
112                 return font;
113         }
114         std::wstring& Letters::getString() {
115                 return str;
116         }
117         Letters& Letters::setFont(const Font &init_font) {
118                 font = init_font;
119                 cache();
120                 return *this;
121         }
122         Letters& Letters::setString(const char *init_str) {
123                 str = LocalEncodingtoWCHAR(init_str);
124                 cache();
125                 return *this;
126         };
127         Letters& Letters::setString(const char *init_str, int size_) {
128                 str = LocalEncodingtoWCHAR(init_str, size_);
129                 cache();
130                 return *this;
131         };
132         Letters& Letters::setString(const std::wstring &init_str) {
133                 str.assign(init_str);
134                 cache();
135                 return *this;
136         };
137                 Letters& Letters::string(const char *init_str) {
138                         str = LocalEncodingtoWCHAR(init_str);
139                         cache();
140                         return *this;
141                 };
142                 Letters& Letters::string(const char *init_str, int size_) {
143                         str = LocalEncodingtoWCHAR(init_str, size_);
144                         cache();
145                         return *this;
146                 };
147                 Letters& Letters::string(const std::wstring &init_str) {
148                         str.assign(init_str);
149                         cache();
150                         return *this;
151                 };
152         Letters& Letters::operator =(const std::wstring &init_str) {
153                 return setString(init_str);
154         }
155         Letters& Letters::cache(DrawableWithCache &target) {
156                 if(0!=&target) target.cacheLetters(*this);
157                 return *this;
158         }
159         Letters& Letters::uncache(DrawableWithCache &target) {
160                 if(0!=&target) target.uncacheLetters(*this);
161                 return *this;
162         }
163         Letters& Letters::centering(const Point& p) {
164                 align = TEXT_ALIGN_CENTER;
165                 vertical_align = TEXT_ALIGN_MIDDLE;
166                 FigureDatum::centering(p);
167                 return *this;
168         }
169         Letters& Letters::centering(const Point& p, bool realign_flag) {
170                 if(realign_flag) align = TEXT_ALIGN_CENTER;
171                 vertical_align = TEXT_ALIGN_MIDDLE;
172                 FigureDatum::centering(p);
173                 return *this;
174         }
175         Letters& Letters::locate(const Point& p) {
176                 FigureDatum::setDatum(p);
177                 vertical_align = BASELINE;
178                 return *this;
179         }
180         Letters& Letters::locate(double x, double y, double z) {
181                 FigureDatum::setDatum(Point(x,y,z));
182                 vertical_align = BASELINE;
183                 return *this;
184         }
185         Letters& Letters::centering(const Figure& fig) { centering(fig.getDatum()); return *this; }
186         Letters& Letters::centering(const Drawable &target) { centering(target.getCenter()); return *this; }
187         Letters& Letters::centering(const double x, const double y, const double z) { centering(Point(x,y,z)); return *this; }
188         Letters& Letters::shift(const double x, const double y, const double z) { Point n=getDatum(); n=n+Point(x,y,z); setDatum(n); return *this; }
189
190         Letters& Letters::draw(Drawable &target) {
191                 Shape::draw_base(target);
192                 return *this;
193         }
194         Letters& Letters::draw(const Color &col, Drawable &target) {
195                 target.letters(*this, col);
196                 return *this;
197         }
198         Letters& Letters::draw(const Stroke &strk, Drawable &target) {
199                 target.letters(*this, strk.color);
200                 return *this;
201         }
202         Letters& Letters::draw(const Color &col, HorizontalAlign horiz_align, Drawable &target) {
203                 //              HorizontalAlign halign = (horiz_align==NOT_SPECIFIED ? align : horiz_align);
204                 HorizontalAlign t_align = align;
205                 align = horiz_align;
206                 target.letters(*this, col);
207                 align = t_align;
208                 return *this;
209         }
210
211
212         bool Font::initialized = false;
213         Image *Font::font_minimum = 0;
214         const size_t Font::font_minimum_proportion[ASCII_NUM] = {
215         5,5,5,5,5,5,5,5,5,15,5,5,5,5,5,5,
216         5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
217         7,5,7,7,7,7,7,5,5,5,7,7,5,5,5,5, //  !"#$%&'()*+,-./
218         7,7,7,7,7,7,7,7,7,7,5,5,5,5,5,7, // 0123456789:;<=>?
219         7,7,7,7,7,7,7,7,7,5,5,7,7,9,7,7, // @ABCDEFGHIJKLMNO
220         7,7,7,7,7,7,7,9,7,7,7,5,5,5,5,5, // PQRSTUVWXYZ[\]^_
221         5,7,7,6,7,7,6,7,7,5,5,7,5,9,7,7, // `abcdefghijklmno
222         7,7,6,7,5,7,7,9,7,7,7,5,5,5,7,5, // pqrstuvwxyz{|}~
223         /*
224          4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
225          4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
226          6,4,6,6,6,6,6,4,4,4,6,6,4,4,4,4, //  !"#$%&'()*+,-./
227          6,6,6,6,6,6,6,6,6,6,4,4,4,4,4,6, // 0123456789:;<=>?
228          6,6,6,6,6,6,6,6,6,4,4,6,6,8,6,6, // @ABCDEFGHIJKLMNO
229          6,6,6,6,6,6,6,8,6,6,6,4,4,4,4,4, // PQRSTUVWXYZ[\]^_
230          4,6,6,5,6,6,5,6,6,4,4,6,4,8,6,6, // `abcdefghijklmno
231          6,6,5,6,4,6,6,8,6,6,6,4,4,4,6,4, // pqrstuvwxyz{|}~
232          */
233         };
234         void Font::initializeFontMinimum() {
235                 const unsigned char font_minimum_GEN[ASCII_NUM][7] = {
236                         {0,0,96,96,0,0,0},{0,0,96,96,0,0,0},{0,0,96,96,0,0,0},{0,0,96,96,0,0,0},
237                         {0,0,96,96,0,0,0},{0,0,96,96,0,0,0},{0,0,96,96,0,0,0},{0,0,96,96,0,0,0},
238                         {0,0,96,96,0,0,0},{0,0, 0, 0,0,0,0},{0,0,96,96,0,0,0},{0,0,96,96,0,0,0},
239                         {0,0,96,96,0,0,0},{0,0,96,96,0,0,0},{0,0,96,96,0,0,0},{0,0,96,96,0,0,0},
240                         {0,0,96,96,0,0,0},{0,0,96,96,0,0,0},{0,0,96,96,0,0,0},{0,0,96,96,0,0,0},
241                         {0,0,96,96,0,0,0},{0,0,96,96,0,0,0},{0,0,96,96,0,0,0},{0,0,96,96,0,0,0},
242                         {0,0,96,96,0,0,0},{0,0,96,96,0,0,0},{0,0,96,96,0,0,0},{0,0,96,96,0,0,0},
243                         {0,0,96,96,0,0,0},{0,0,96,96,0,0,0},{0,0,96,96,0,0,0},{0,0,96,96,0,0,0},
244
245                         {0,0,0,0,0,0,0}, // SPC
246                         {96,96,96,0,96,96,0}, // !
247                         {108,108,0,0,0,0,0}, // "
248                         {0,108,252,108,252,108,0}, // #
249                         {16,124,208,120,20,248,16}, // $
250                         {0,204,24,48,96,204,0}, // %
251                         {120,196,120,200,220,124,0}, // &
252                         {48,96,0,0,0,0,0}, // '
253                         {48,96,96,96,96,48,0}, // (
254                         {192,96,96,96,96,192,0}, // )
255                         {0,216,112,112,216,0,0}, // *
256                         {0,0,48,252,48,0,0}, // +
257                         {0,0,0,0,0,48,96}, // ,
258                         {0,0,0,240,0,0,0}, // -
259                         {0,0,0,0,0,96,0}, // .
260                         {48,48,96,96,192,192,0}, // /
261                         {120,204,220,236,204,120,0}, // 0
262                         {48,112,48,48,48,120,0}, // 1
263                         {120,204,24,48,96,252,0}, // 2
264                         {248,12,120,12,12,248,0}, // 3
265                         {24,56,88,216,252,24,0}, // 4
266                         {252,192,248,12,12,248,0}, // 5
267                         {124,192,248,204,204,120,0}, // 6
268                         {252,12,12,24,48,96,0}, // 7
269                         {120,204,120,204,204,120,0}, // 8
270                         {120,204,204,120,24,48,0}, // 9
271                         {0,0,96,0,96,0,0}, // :
272                         {0,0,96,0,96,192,0}, // ;
273                         {48,96,192,192,96,48,0}, // <
274                         {0,0,240,0,240,0,0}, // =
275                         {192,96,48,48,96,192,0}, // >
276                         {124,204,56,48,0,48,0}, // ?
277                         {120,196,212,218,192,120,0}, // @
278                         {120,204,204,252,204,204,0}, // A
279                         {248,204,248,204,204,248,0}, // B
280                         {124,192,192,192,192,124,0}, // C
281                         {248,204,204,204,204,248,0}, // D
282                         {252,192,252,192,192,252,0}, // E
283                         {252,192,248,192,192,192,0}, // F
284                         {124,192,192,204,204,124,0}, // G
285                         {204,204,252,204,204,204,0}, // H
286                         {240,96,96,96,96,240,0}, // I
287                         {240,48,48,48,48,224,0}, // J
288                         {204,204,216,240,216,204,0}, // K
289                         {192,192,192,192,192,252,0}, // L
290                         {231,219,219,219,219,219,0}, // M
291                         {236,236,220,220,220,204,0}, // N
292                         {120,204,204,204,204,120,0}, // O
293                         {248,204,204,248,192,192,0}, // P
294                         {120,204,204,204,220,124,0}, // Q
295                         {248,204,204,248,208,204,0}, // R
296                         {124,192,120,12,12,248,0}, // S
297                         {252,48,48,48,48,48,0}, // T
298                         {204,204,204,204,204,124,0}, // U
299                         {204,204,204,204,120,48,0}, // V
300                         {219,219,219,219,219,127,0}, // W
301                         {204,104,48,48,88,204,0}, // X
302                         {204,204,120,48,48,48,0}, // Y
303                         {252,24,48,96,192,252,0}, // Z
304                         {112,96,96,96,96,112,0}, // [
305                         {192,192,96,96,48,48,0}, // ツ・
306                         {224,96,96,96,96,224,0}, // ]
307                         {96,144,0,0,0,0,0}, // ^
308                         {0,0,0,0,0,240,0}, // _
309                         {192,96,0,0,0,0,0}, // `
310                         {0,120,12,124,204,124,0}, // a
311                         {192,192,248,204,204,248,0}, // b
312                         {0,120,192,192,192,120,0}, // c
313                         {12,12,124,204,204,124,0}, // d
314                         {0,120,204,252,192,120,0}, // e
315                         {56,96,248,96,96,96,0}, // f
316                         {0,120,204,204,124,12,120}, // g
317                         {192,192,248,204,204,204,0}, // h
318                         {96,0,96,96,96,96,0}, // i
319                         {96,0,96,96,96,96,192}, // j
320                         {192,192,204,216,240,220,0}, // k
321                         {96,96,96,96,96,32,0}, // l
322                         {0,255,219,219,219,219,0}, // m
323                         {0,248,204,204,204,204,0}, // n
324                         {0,120,204,204,204,120,0}, // o
325                         {0,248,204,204,248,192,192}, // p
326                         {0,124,204,204,124,12,12}, // q
327                         {0,216,240,192,192,192,0}, // r
328                         {0,124,192,120,12,248,0}, // s
329                         {96,240,96,96,96,48,0}, // t
330                         {0,204,204,204,204,124,0}, // u
331                         {0,204,204,204,120,48,0}, // v
332                         {0,219,219,219,219,127,0}, // w
333                         {0,204,104,48,88,204,0}, // x
334                         {0,204,204,108,24,48,96}, // y
335                         {0,252,24,48,96,252,0}, // z
336                         {112,96,192,192,96,112,0}, // {
337                         {64,64,64,64,64,64,0}, // |
338                         {224,96,48,48,96,224,0}, // }
339                         {0,0,108,216,0,0,0}, // ~
340                         {0,0,0,0,0,0,0} // DEL
341                 };
342
343                 if(!initialized) {
344                         unsigned char mask = 1;
345                         font_minimum = new Image[ASCII_NUM];
346                         for(size_t i=0; i<ASCII_NUM; i++) {
347                                 font_minimum[i].set(font_minimum_proportion[i]-1, 7, Image::RGBA);
348                                 for(int y=0; y<7; y++) {
349                                         mask = 128;
350                                         for(int x=0; x<font_minimum_proportion[i]-1; x++) {
351                                                 font_minimum[i].alpha(x,y, (0!=(font_minimum_GEN[i][y] & mask) ? 1.0 : 0.0) );
352                                                 mask >>= 1;
353                                         }
354                                 }
355                         }
356                         initialized = true;
357                 }
358         }
359
360 }       /*      <- namespace Psycholops         */
361
362