1 /*
2 
3 Boost Software License - Version 1.0 - August 17th, 2003
4 
5 Permission is hereby granted, free of charge, to any person or organization
6 obtaining a copy of the software and accompanying documentation covered by
7 this license (the "Software") to use, reproduce, display, distribute,
8 execute, and transmit the Software, and to prepare derivative works of the
9 Software, and to permit third-parties to whom the Software is furnished to
10 do so, all subject to the following:
11 
12 The copyright notices in the Software and this entire statement, including
13 the above license grant, this restriction and the following disclaimer,
14 must be included in all copies of the Software, in whole or in part, and
15 all derivative works of the Software, unless such copies or derivative
16 works are solely in the form of machine-executable object code generated by
17 a source language processor.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 
27 */
28 module derelict.allegro5.font;
29 
30 private {
31     import derelict.util.loader,
32            derelict.util.system;
33 
34     import derelict.allegro5.internal,
35            derelict.allegro5.types;
36 
37     enum libNames = genLibNames("Font");
38 }
39 
40 struct ALLEGRO_FONT {
41     void* data;
42     int height;
43     ALLEGRO_FONT_VTABLE* vtable;
44 }
45 
46 struct ALLEGRO_FONT_VTABLE {
47     extern(C) nothrow {
48         int function(const(ALLEGRO_FONT)*) font_height;
49         int function(const(ALLEGRO_FONT)*) font_ascent;
50         int function(const(ALLEGRO_FONT)*) font_descent;
51         int function(const(ALLEGRO_FONT)*,int) char_length;
52         int function(const(ALLEGRO_FONT)*,const(ALLEGRO_USTR)*) text_length;
53         int function(const(ALLEGRO_FONT)*,ALLEGRO_COLOR,int,float,float) render_char;
54         int function(const(ALLEGRO_FONT)*,ALLEGRO_COLOR,const(ALLEGRO_USTR)*,float,float) render;
55         void function(ALLEGRO_FONT*) destroy;
56         void function(const(ALLEGRO_FONT)*,const(ALLEGRO_USTR)*,int*,int*,int*,int*) get_text_dimensions;
57         int function(ALLEGRO_FONT*,int,int*) get_font_ranges;
58         bool function(const(ALLEGRO_FONT)*,int,int*,int*,int*,int*) get_glyph_dimensions;
59         int function(const(ALLEGRO_FONT)*,int,int) get_glyph_advance;
60     }
61 }
62 
63 enum {
64     ALLEGRO_NO_KERNING       = -1,
65     ALLEGRO_ALIGN_LEFT       = 0,
66     ALLEGRO_ALIGN_CENTRE     = 1,
67     ALLEGRO_ALIGN_CENTER     = 1,
68     ALLEGRO_ALIGN_RIGHT      = 2,
69     ALLEGRO_ALIGN_INTEGER    = 4,
70 }
71 
72 // Callbacks used by do_multiline functions
73 extern(C) nothrow {
74     alias DoTextCallback = bool function(int,const(char)*,int,void*);
75     alias DoUSTRCallback = bool function(int,const(ALLEGRO_USTR)*,void*);
76 }
77 
78 extern(C) @nogc nothrow {
79     alias da_al_register_font_loader = bool function(const(char)*,ALLEGRO_FONT* function(const(char)*,int,int));
80     alias da_al_load_bitmap_font = ALLEGRO_FONT* function(const(char)*);
81     alias da_al_load_bitmap_font_flags = ALLEGRO_FONT* function(const(char)*,int);
82     alias da_al_load_font = ALLEGRO_FONT* function(const(char)*,int,int);
83 
84     alias da_al_grab_font_from_bitmap = ALLEGRO_FONT* function(ALLEGRO_BITMAP*,int,const(int)*);
85     alias da_al_create_builtin_font = ALLEGRO_FONT* function();
86 
87     alias da_al_draw_ustr = void function(const(ALLEGRO_FONT)*,ALLEGRO_COLOR,float,float,int,const(ALLEGRO_USTR)*);
88     alias da_al_draw_text = void function(const(ALLEGRO_FONT)*,ALLEGRO_COLOR,float,float,int,const(char)*);
89     alias da_al_draw_justified_text = void function(const(ALLEGRO_FONT)*,ALLEGRO_COLOR,float,float,float,float,int,const(char)*);
90     alias da_al_draw_justified_ustr = void function(const(ALLEGRO_FONT)*,ALLEGRO_COLOR,float,float,float,float,int,const(ALLEGRO_USTR)*);
91     alias da_al_draw_textf = void function(const(ALLEGRO_FONT)*,ALLEGRO_COLOR,float,float,int,const(char)*,...);
92     alias da_al_draw_justified_textf = void function(const(ALLEGRO_FONT)*,ALLEGRO_COLOR,float,float,float,float,int,const(char)*,...);
93     alias da_al_get_text_width = int function(const(ALLEGRO_FONT)*,const(char)*);
94     alias da_al_get_ustr_width = int function(const(ALLEGRO_FONT)*,const(ALLEGRO_USTR)*);
95     alias da_al_get_font_line_height = int function(const(ALLEGRO_FONT)*);
96     alias da_al_get_font_ascent = int function(const(ALLEGRO_FONT)*);
97     alias da_al_get_font_descent = int function(const(ALLEGRO_FONT)*);
98     alias da_al_destroy_font = void function(ALLEGRO_FONT*);
99     alias da_al_get_ustr_dimensions = void function(const(ALLEGRO_FONT)*,const(ALLEGRO_USTR)*,int*,int*,int*,int*);
100     alias da_al_get_text_dimensions = void function(const(ALLEGRO_FONT)*,const(char)*,int*,int*,int*,int*);
101     alias da_al_init_font_addon = void function();
102     alias da_al_shutdown_font_addon = void function();
103     alias da_al_get_allegro_font_version = uint function();
104     alias da_al_get_font_ranges = int function(ALLEGRO_FONT*,int,int*);
105 
106     alias da_al_draw_glyph = void function(ALLEGRO_FONT*,ALLEGRO_COLOR,float,float,int);
107     alias da_al_get_glyph_width = int function(const(ALLEGRO_FONT)*,int);
108     alias da_al_get_glyph_dimensions = bool function(const(ALLEGRO_FONT)*,int,int*,int*,int*,int*);
109     alias da_al_get_glyph_advance = int function(const(ALLEGRO_FONT)*,int,int);
110 
111     alias da_al_draw_multiline_text = void function(const(ALLEGRO_FONT)*,ALLEGRO_COLOR,float,float,float,float,int,const(char)*);
112     alias da_al_draw_multiline_textf = void function(const(ALLEGRO_FONT)*,ALLEGRO_COLOR,float,float,float,float,int,const(char)*,...);
113     alias da_al_draw_multiline_ustr = void function(const(ALLEGRO_FONT)*,ALLEGRO_COLOR,float,float,float,float,int,const(ALLEGRO_USTR)*);
114 
115     alias da_al_do_multiline_text = void function(const(ALLEGRO_FONT)*,float,const(char)*,DoTextCallback,void*);
116     alias da_al_do_multiline_ustr = void function(const(ALLEGRO_FONT)*,float,const(ALLEGRO_USTR)*,DoUSTRCallback,void*);
117 
118     alias da_al_set_fallback_font = void function(ALLEGRO_FONT*,ALLEGRO_FONT*);
119     alias da_al_get_fallback_font = ALLEGRO_FONT* function(ALLEGRO_FONT*);
120 }
121 
122 __gshared {
123     da_al_register_font_loader al_register_font_loader;
124     da_al_load_bitmap_font al_load_bitmap_font;
125     da_al_load_bitmap_font_flags al_load_bitmap_font_flags;
126     da_al_load_font al_load_font;
127     da_al_grab_font_from_bitmap al_grab_font_from_bitmap;
128     da_al_create_builtin_font al_create_builtin_font;
129     da_al_draw_ustr al_draw_ustr;
130     da_al_draw_text al_draw_text;
131     da_al_draw_justified_text al_draw_justified_text;
132     da_al_draw_justified_ustr al_draw_justified_ustr;
133     da_al_draw_textf al_draw_textf;
134     da_al_draw_justified_textf al_draw_justified_textf;
135     da_al_get_text_width al_get_text_width;
136     da_al_get_ustr_width al_get_ustr_width;
137     da_al_get_font_line_height al_get_font_line_height;
138     da_al_get_font_ascent al_get_font_ascent;
139     da_al_get_font_descent al_get_font_descent;
140     da_al_destroy_font al_destroy_font;
141     da_al_get_ustr_dimensions al_get_ustr_dimensions;
142     da_al_get_text_dimensions al_get_text_dimensions;
143     da_al_init_font_addon al_init_font_addon;
144     da_al_shutdown_font_addon al_shutdown_font_addon;
145     da_al_get_allegro_font_version al_get_allegro_font_version;
146     da_al_get_font_ranges al_get_font_ranges;
147     da_al_draw_glyph al_draw_glyph;
148     da_al_get_glyph_width al_get_glyph_width;
149     da_al_get_glyph_dimensions al_get_glyph_dimensions;
150     da_al_get_glyph_advance al_get_glyph_advance;
151     da_al_draw_multiline_text al_draw_multiline_text;
152     da_al_draw_multiline_textf al_draw_multiline_textf;
153     da_al_draw_multiline_ustr al_draw_multiline_ustr;
154     da_al_do_multiline_text al_do_multiline_text;
155     da_al_do_multiline_ustr al_do_multiline_ustr;
156     da_al_set_fallback_font al_set_fallback_font;
157     da_al_get_fallback_font al_get_fallback_font;
158 }
159 
160 class DerelictAllegro5FontLoader : SharedLibLoader {
161     public this() {
162         super(libNames);
163     }
164 
165     protected override void loadSymbols() {
166         bindFunc(cast(void**)&al_register_font_loader, "al_register_font_loader");
167         bindFunc(cast(void**)&al_load_bitmap_font, "al_load_bitmap_font");
168         bindFunc(cast(void**)&al_load_bitmap_font_flags, "al_load_bitmap_font_flags");
169         bindFunc(cast(void**)&al_load_font, "al_load_font");
170         bindFunc(cast(void**)&al_grab_font_from_bitmap, "al_grab_font_from_bitmap");
171         bindFunc(cast(void**)&al_create_builtin_font, "al_create_builtin_font");
172         bindFunc(cast(void**)&al_draw_ustr, "al_draw_ustr");
173         bindFunc(cast(void**)&al_draw_text, "al_draw_text");
174         bindFunc(cast(void**)&al_draw_justified_text, "al_draw_justified_text");
175         bindFunc(cast(void**)&al_draw_justified_ustr, "al_draw_justified_ustr");
176         bindFunc(cast(void**)&al_draw_textf, "al_draw_textf");
177         bindFunc(cast(void**)&al_draw_justified_textf, "al_draw_justified_textf");
178         bindFunc(cast(void**)&al_get_text_width, "al_get_text_width");
179         bindFunc(cast(void**)&al_get_ustr_width, "al_get_ustr_width");
180         bindFunc(cast(void**)&al_get_font_line_height, "al_get_font_line_height");
181         bindFunc(cast(void**)&al_get_font_ascent, "al_get_font_ascent");
182         bindFunc(cast(void**)&al_get_font_descent, "al_get_font_descent");
183         bindFunc(cast(void**)&al_destroy_font, "al_destroy_font");
184         bindFunc(cast(void**)&al_get_text_dimensions, "al_get_text_dimensions");
185         bindFunc(cast(void**)&al_init_font_addon, "al_init_font_addon");
186         bindFunc(cast(void**)&al_shutdown_font_addon, "al_shutdown_font_addon");
187         bindFunc(cast(void**)&al_get_allegro_font_version, "al_get_allegro_font_version");
188 
189 
190         bindFunc(cast(void**)&al_get_font_ranges, "al_get_font_ranges");
191         bindFunc(cast(void**)&al_draw_glyph, "al_draw_glyph");
192         bindFunc(cast(void**)&al_get_glyph_width, "al_get_glyph_width");
193         bindFunc(cast(void**)&al_get_glyph_dimensions, "al_get_glyph_dimensions");
194         bindFunc(cast(void**)&al_get_glyph_advance, "al_get_glyph_advance");
195         bindFunc(cast(void**)&al_draw_multiline_text, "al_draw_multiline_text");
196         bindFunc(cast(void**)&al_draw_multiline_textf, "al_draw_multiline_textf");
197         bindFunc(cast(void**)&al_draw_multiline_ustr, "al_draw_multiline_ustr");
198         bindFunc(cast(void**)&al_do_multiline_text, "al_do_multiline_text");
199         bindFunc(cast(void**)&al_do_multiline_ustr, "al_do_multiline_ustr");
200         bindFunc(cast(void**)&al_set_fallback_font, "al_set_fallback_font");
201         bindFunc(cast(void**)&al_get_fallback_font, "al_get_fallback_font");
202     }
203 }
204 
205 __gshared DerelictAllegro5FontLoader DerelictAllegro5Font;
206 
207 shared static this() {
208     DerelictAllegro5Font = new DerelictAllegro5FontLoader;
209 }