root/trunk/luigi/font.d

Revision 53, 52.8 kB (checked in by baxissimo, 1 year ago)

Updated for dsss usage

Line 
1 //---------------------------------------------------------------------
2 /*
3  Copyright:
4
5   luigi/font.d -- font support for 'luigi' user interface library.
6
7   Copyright (C) 2006 William V. Baxter III
8
9   This software is provided 'as-is', without any express or implied
10   warranty.  In no event will the authors be held liable for any
11   damages arising from the use of this software.
12
13   Permission is granted to anyone to use this software for any
14   purpose, including commercial applications, and to alter it and
15   redistribute it freely, subject to the following restrictions:
16
17   1. The origin of this software must not be misrepresented; you must
18      not claim that you wrote the original software. If you use this
19      software in a product, an acknowledgment in the product
20      documentation would be appreciated but is not required.
21   2. Altered source versions must be plainly marked as such, and must
22      not be misrepresented as being the original software.
23   3. This notice may not be removed or altered from any source distribution.
24
25   William Baxter wbaxter@gmail.com
26 */
27
28 // The following copyright applies to some portions of this code below.
29 /*
30  * Portions copyright (C) 2004, the OpenGLUT project contributors.
31  * OpenGLUT branched from freeglut in February, 2004.
32  *
33  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
34  * Written by Pawel W. Olszta, <olszta@sourceforge.net>
35  * Creation date: Thu Dec 16 1999
36  *
37  * Permission is hereby granted, free of charge, to any person obtaining a
38  * copy of this software and associated documentation files (the "Software"),
39  * to deal in the Software without restriction, including without limitation
40  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
41  * and/or sell copies of the Software, and to permit persons to whom the
42  * Software is furnished to do so, subject to the following conditions:
43  *
44  * The above copyright notice and this permission notice shall be included
45  * in all copies or substantial portions of the Software.
46  *
47  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
48  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
49  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
50  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
51  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
52  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
53  */
54
55
56 module luigi.font;
57
58 import luigi.opengl;
59 import luigi.base;
60
61 import std.stdio : writefln;
62
63 //--------------------------------------------------------------------------
64 /** An abstract interface for any font rendering tech that wants to be used by Luigi */
65 interface Font
66 {
67     /** Draw a character using OpenGL commands at position (0,0,0) in current
68         OpenGL coordinates.
69         Return the number of units along the baseline to advance.
70     */
71     float draw_char(dchar c);
72
73     /** Draw a string using OpenGL commands at position (0,0,0) in current
74         OpenGL coordinates.
75         Return the number of units along the baseline to advance.
76         A mixin is available to provide a default implementation of this based
77         on draw_char.
78     */
79     float draw_string(char[] str);
80
81     /** Return the bounding box of the string assuming it will be drawn at
82         location (0,0).
83         Thus the x,y positions tell you about the leading and descender.
84                       +--------------------+
85                       |       <--w-->      | ^
86                       |                    | |
87              (0,0)    |                    | h
88                +------|--------------------|-|----
89                       |(x,y)               | v
90                       +--------------------+
91     */
92     Rect string_rect(char[] str);
93
94     /** Return the basic height of the font */
95     float height();
96
97     /** Return width if font is fixed-width, or -1 if the font is proportional */
98     float width();
99
100     /** Returns the location of the origin relative to a character's bounding box
101      *  top-left corner.  Below this would be something like (ox,oy)==(-5,7).
102      *  Put another way, in order for the top left corner to appear at some (x,y)
103      *  you should call draw_string with the coordinates set to (x+ox, y+oy)
104
105                  (0,0)+------------+
106                       |            |
107                       |            |
108              (ox,oy)  |            |
109                +------|------------|----
110                       |            |
111                       +------------+
112     */                       
113     Point origin();
114
115 }
116
117
118
119
120
121 // Basic glbitmap font ported from OpenGLUT (GLUT_BITMAP_HELVETICA_12)
122 class ASCIIBitmapFont : Font
123 {
124     this() {
125         m_data = &helvetica_12_data;
126     }
127
128     float draw_char(dchar c) {
129
130         if( !m_data || ( 0 > c ) || (255 < c ) )
131             return 0;
132
133         alias m_data f;
134
135         /* Find the glyph we want to draw. */
136         GLubyte *glyph = f.characters[ c ];
137
138         glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
139
140         glPixelStorei( GL_UNPACK_SWAP_BYTES,  GL_FALSE );
141         glPixelStorei( GL_UNPACK_LSB_FIRST,   GL_FALSE );
142         glPixelStorei( GL_UNPACK_ROW_LENGTH,  0        );
143         glPixelStorei( GL_UNPACK_SKIP_ROWS,   0        );
144         glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0        );
145         glPixelStorei( GL_UNPACK_ALIGNMENT,   1        );
146        
147         _update_raster_pos();
148
149         glBitmap(
150             glyph[ 0 ], f.height,       /* The bitmap's width and height  */
151             f.xorig, f.yorig,           /* The origin in the font glyph   */
152             glyph[0], 0.0,              /* The raster advance -- inc. x,y */
153             &glyph[1]                   /* The packed bitmap data...      */
154             );
155         glPopClientAttrib( );
156         return glyph[0];
157     }
158
159     float draw_string(char[] str) {
160         float totalw = 0;
161         foreach(dchar c; str) {
162             float dx = draw_char(c);
163             totalw += dx;
164             glTranslatef(dx,0,0);
165         }
166         glTranslatef(-totalw,0,0);
167         return totalw;
168     }
169    
170     Rect string_rect(char [] str) {
171         Rect r; r.set(0,0,0,0);
172         r.h = m_data.height;
173         r.y = m_data.yorig;
174         r.x = m_data.xorig;
175         foreach(dchar c; str) {
176             if (0<=c && 255>=c) {
177                 r.w += m_data.characters[c][0];
178             }
179         }
180         return r;
181     }
182    
183     float height() {
184         return m_data.height;
185     }
186
187     /** Return if the font is proportional (vs fixed width) */
188     float width() {
189         return m_data.width;
190     }
191
192     Point origin() { return Point(-m_data.xorig,-m_data.yorig+m_data.height); }
193
194     /** Set raster pos to the current gl 0,0,0 point */
195     void _update_raster_pos()
196     {
197         // GL maintains this separately, but we just use the current 0,0 point
198         glRasterPos2f(0,0);
199     }
200
201     BitmapFontData *m_data = null;
202 }
203
204 static GLubyte Helvetica12_Character_000[33] = [cast(GLubyte)9,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
205 static GLubyte Helvetica12_Character_001[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
206 static GLubyte Helvetica12_Character_002[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
207 static GLubyte Helvetica12_Character_003[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
208 static GLubyte Helvetica12_Character_004[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
209 static GLubyte Helvetica12_Character_005[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
210 static GLubyte Helvetica12_Character_006[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
211 static GLubyte Helvetica12_Character_007[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
212 static GLubyte Helvetica12_Character_008[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
213 static GLubyte Helvetica12_Character_009[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
214 static GLubyte Helvetica12_Character_010[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
215 static GLubyte Helvetica12_Character_011[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
216 static GLubyte Helvetica12_Character_012[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
217 static GLubyte Helvetica12_Character_013[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
218 static GLubyte Helvetica12_Character_014[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
219 static GLubyte Helvetica12_Character_015[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
220 static GLubyte Helvetica12_Character_016[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
221 static GLubyte Helvetica12_Character_017[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
222 static GLubyte Helvetica12_Character_018[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
223 static GLubyte Helvetica12_Character_019[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
224 static GLubyte Helvetica12_Character_020[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
225 static GLubyte Helvetica12_Character_021[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
226 static GLubyte Helvetica12_Character_022[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
227 static GLubyte Helvetica12_Character_023[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
228 static GLubyte Helvetica12_Character_024[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
229 static GLubyte Helvetica12_Character_025[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
230 static GLubyte Helvetica12_Character_026[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
231 static GLubyte Helvetica12_Character_027[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
232 static GLubyte Helvetica12_Character_028[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
233 static GLubyte Helvetica12_Character_029[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
234 static GLubyte Helvetica12_Character_030[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
235 static GLubyte Helvetica12_Character_031[33] = [ 12,  0,  0,  0,  0,  0,  0,  0,  0, 85,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 65,  0,  0,  0, 85,  0,  0,  0,  0,  0,  0,  0];
236 static GLubyte Helvetica12_Character_032[17] = [  4,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0];
237 static GLubyte Helvetica12_Character_033[17] = [  3,  0,  0,  0,  0, 64,  0, 64, 64, 64, 64, 64, 64, 64,  0,  0,  0];
238 static GLubyte Helvetica12_Character_034[17] = [  5,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 80, 80, 80,  0,  0,  0];
239 static GLubyte Helvetica12_Character_035[17] = [  7,  0,  0,  0,  0, 80, 80, 80,252, 40,252, 40, 40,  0,  0,  0,  0];
240 static GLubyte Helvetica12_Character_036[17] = [  7,  0,  0,  0, 16, 56, 84, 84, 20, 56, 80, 84, 56, 16,  0,  0,  0];
241 static GLubyte Helvetica12_Character_037[33] = [ 11,  0,  0,  0,  0,  0,  0,  0,  0, 17,128, 10, 64, 10, 64,  9,128,  4,  0, 52,  0, 74,  0, 74,  0, 49,  0,  0,  0,  0,  0,  0,  0];
242 static GLubyte Helvetica12_Character_038[33] = [  9,  0,  0,  0,  0,  0,  0,  0,  0, 57,  0, 70,  0, 66,  0, 69,  0, 40,  0, 24,  0, 36,  0, 36,  0, 24,  0,  0,  0,  0,  0,  0,  0];
243 static GLubyte Helvetica12_Character_039[17] = [  3,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 64, 32, 96,  0,  0,  0];
244 static GLubyte Helvetica12_Character_040[17] = [  4,  0, 16, 32, 32, 64, 64, 64, 64, 64, 64, 32, 32, 16,  0,  0,  0];
245 static GLubyte Helvetica12_Character_041[17] = [  4,  0,128, 64, 64, 32, 32, 32, 32, 32, 32, 64, 64,128,  0,  0,  0];
246 static GLubyte Helvetica12_Character_042[17] = [  5,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 80, 32, 80,  0,  0,  0];
247 static GLubyte Helvetica12_Character_043[17] = [  7,  0,  0,  0,  0,  0, 16, 16,124, 16, 16,  0,  0,  0,  0,  0,  0];
248 static GLubyte Helvetica12_Character_044[33] = [  4,  0,  0, 64, 32, 32,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0];
249 static GLubyte Helvetica12_Character_045[33] = [  8,  0,  0,  0,  0,  0,  0,  0,124,  0,  0,  0,  0,  0,  0,  0,  0];
250 static GLubyte Helvetica12_Character_046[33] = [  3,  0,  0,  0,  0, 64,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0];
251 static GLubyte Helvetica12_Character_047[33] = [  4,  0,  0,  0,  0,128,128, 64, 64, 64, 32, 32, 16, 16,  0,  0,  0];
252 static GLubyte Helvetica12_Character_048[33] = [  7,  0,  0,  0,  0, 56, 68, 68, 68, 68, 68, 68, 68, 56,  0,  0,  0];
253 static GLubyte Helvetica12_Character_049[33] = [  7,  0,  0,  0,  0, 16, 16, 16, 16, 16, 16, 16,112, 16,  0,  0,  0];
254 static GLubyte Helvetica12_Character_050[33]