1 /*
2 * gltt graphics library
3 * Copyright (C) 1998-1999 Stephane Rehel
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include "FTBitmapFont.h"
21 #include "FTGlyph.h"
22 #include "FTGlyphBitmap.h"
23
24 /////////////////////////////////////////////////////////////////////////////
25
FTBitmapFont(FTInstance * _instance)26 FTBitmapFont::FTBitmapFont( FTInstance* _instance ): FTFont(_instance)
27 {
28 bitmaps= 0;
29 loaded= 0;
30 }
31
32 /////////////////////////////////////////////////////////////////////////////
33
~FTBitmapFont()34 FTBitmapFont::~FTBitmapFont()
35 {
36 destroy();
37 }
38
39 /////////////////////////////////////////////////////////////////////////////
40
destroy()41 void FTBitmapFont::destroy()
42 {
43 if( bitmaps != 0 )
44 {
45 for( int i= 0; i < 256; ++i )
46 delete bitmaps[i];
47
48 delete[] bitmaps;
49 bitmaps= 0;
50 }
51
52 delete[] loaded;
53 loaded= 0;
54 }
55
56 /////////////////////////////////////////////////////////////////////////////
57
create()58 GLTTboolean FTBitmapFont::create()
59 {
60 destroy();
61
62 if( ! FTFont::create() )
63 return GLTT_FALSE;
64
65 int i;
66
67 bitmaps= new FTGlyphBitmap* [ 256 ];
68 loaded= new GLTTboolean [ 256 ];
69
70 for( i= 0; i < 256; ++i )
71 {
72 bitmaps[i]= 0;
73 loaded[i]= GLTT_FALSE;
74 }
75
76 return GLTT_TRUE;
77 }
78
79 /////////////////////////////////////////////////////////////////////////////
80
loadGlyph(int ascii_code)81 GLTTboolean FTBitmapFont::loadGlyph( int ascii_code )
82 {
83 if( ascii_code < 0 || ascii_code > 255 || bitmaps == 0 || loaded == 0 )
84 return GLTT_FALSE;
85
86 if( loaded[ascii_code] )
87 return GLTT_TRUE;
88
89 loaded[ascii_code]= GLTT_TRUE;
90
91 FTGlyph* glyph= FTFont::glyphs[ascii_code];
92 if( glyph == 0 )
93 return GLTT_FALSE;
94
95 FTGlyphBitmap* gbitmap= new FTGlyphBitmap(glyph);
96 if( ! gbitmap->create() )
97 {
98 delete gbitmap;
99 return GLTT_FALSE;
100 }
101
102 bitmaps[ascii_code]= gbitmap;
103
104 return GLTT_TRUE;
105 }
106
107 /////////////////////////////////////////////////////////////////////////////
108
load(int from,int to)109 void FTBitmapFont::load( int from /* = 0 */, int to /* = 255 */ )
110 {
111 for( int i= from; i <= to; ++i )
112 loadGlyph(i);
113 }
114
115 /////////////////////////////////////////////////////////////////////////////
116
getWidth(const char * text)117 int FTBitmapFont::getWidth( const char* text )
118 {
119 if( text == 0 )
120 return 0;
121
122 int w= 0;
123 for(;;)
124 {
125 int ch= (unsigned char) *(text++);
126 if( ch == 0 )
127 break;
128
129 loadGlyph(ch);
130 if( bitmaps[ch] == 0 )
131 continue;
132
133 w+= bitmaps[ch]->getAdvance();
134 }
135
136 return w / 64;
137 }
138
139 /////////////////////////////////////////////////////////////////////////////
140
141