1 |
#include "EXTERN.h" |
2 |
#include "perl.h" |
3 |
#include "XSUB.h" |
4 |
|
5 |
#include <string.h> |
6 |
|
7 |
#include <SDL.h> |
8 |
#include <SDL_opengl.h> |
9 |
|
10 |
#include <pango/pango.h> |
11 |
#include <pango/pangofc-fontmap.h> |
12 |
#include <pango/pangoft2.h> |
13 |
|
14 |
#include <sys/types.h> |
15 |
#include <sys/socket.h> |
16 |
#include <netinet/in.h> |
17 |
#include <netinet/tcp.h> |
18 |
|
19 |
static PangoContext *context; |
20 |
static PangoFontMap *fontmap; |
21 |
|
22 |
typedef struct cf_layout { |
23 |
PangoLayout *pl; |
24 |
int base_height; |
25 |
} *CFClient__Layout; |
26 |
|
27 |
static void |
28 |
substitute_func (FcPattern *pattern, gpointer data) |
29 |
{ |
30 |
//FcPatternAddBool (pattern, FC_HINTING, 1); |
31 |
//FcPatternAddBool (pattern, FC_AUTOHINT, 1); |
32 |
} |
33 |
|
34 |
static void |
35 |
layout_update (CFClient__Layout self) |
36 |
{ |
37 |
/* use a random scale factor to account for unknown descenders, 0.8 works |
38 |
* reasonably well with bitstream vera |
39 |
*/ |
40 |
PangoFontDescription *font = pango_context_get_font_description (context); |
41 |
pango_font_description_set_absolute_size (font, self->base_height * (PANGO_SCALE * 8 / 10)); |
42 |
} |
43 |
|
44 |
static void |
45 |
layout_get_pixel_size (CFClient__Layout self, int *w, int *h) |
46 |
{ |
47 |
layout_update (self); |
48 |
|
49 |
pango_layout_get_pixel_size (self->pl, w, h); |
50 |
|
51 |
*w = (*w + 3) & ~3; |
52 |
if (!*w) *w = 1; |
53 |
if (!*h) *h = 1; |
54 |
} |
55 |
|
56 |
MODULE = CFClient PACKAGE = CFClient |
57 |
|
58 |
PROTOTYPES: ENABLE |
59 |
|
60 |
BOOT: |
61 |
{ |
62 |
fontmap = pango_ft2_font_map_new (); |
63 |
pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)fontmap, substitute_func, 0, 0); |
64 |
context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap); |
65 |
} |
66 |
|
67 |
void |
68 |
lowdelay (int fd, int val = 1) |
69 |
CODE: |
70 |
setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val)); |
71 |
|
72 |
char * |
73 |
gl_version () |
74 |
CODE: |
75 |
RETVAL = (char *)glGetString (GL_VERSION); |
76 |
OUTPUT: |
77 |
RETVAL |
78 |
|
79 |
char * |
80 |
gl_extensions () |
81 |
CODE: |
82 |
RETVAL = (char *)glGetString (GL_EXTENSIONS); |
83 |
OUTPUT: |
84 |
RETVAL |
85 |
|
86 |
void |
87 |
add_font (char *file) |
88 |
CODE: |
89 |
FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */ |
90 |
|
91 |
void |
92 |
set_font (char *file) |
93 |
CODE: |
94 |
{ |
95 |
int count; |
96 |
FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count); |
97 |
PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0); |
98 |
FcPatternDestroy (pattern); |
99 |
pango_context_set_font_description (context, font); |
100 |
} |
101 |
|
102 |
MODULE = CFClient PACKAGE = CFClient::Layout |
103 |
|
104 |
CFClient::Layout |
105 |
new (SV *class, int base_height = 10) |
106 |
CODE: |
107 |
New (0, RETVAL, 1, struct cf_layout); |
108 |
RETVAL->base_height = base_height; |
109 |
RETVAL->pl = pango_layout_new (context); |
110 |
pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); |
111 |
OUTPUT: |
112 |
RETVAL |
113 |
|
114 |
void |
115 |
DESTROY (CFClient::Layout self) |
116 |
CODE: |
117 |
g_object_unref (self->pl); |
118 |
Safefree (self); |
119 |
|
120 |
void |
121 |
set_markup (CFClient::Layout self, SV *text_) |
122 |
CODE: |
123 |
{ |
124 |
STRLEN textlen; |
125 |
char *text = SvPVutf8 (text_, textlen); |
126 |
|
127 |
pango_layout_set_markup (self->pl, text, textlen); |
128 |
} |
129 |
|
130 |
void |
131 |
set_height (CFClient::Layout self, int base_height) |
132 |
CODE: |
133 |
self->base_height = base_height; |
134 |
|
135 |
void |
136 |
set_width (CFClient::Layout self, int max_width = -1) |
137 |
CODE: |
138 |
pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE); |
139 |
|
140 |
void |
141 |
size (CFClient::Layout self) |
142 |
PPCODE: |
143 |
{ |
144 |
int w, h; |
145 |
|
146 |
layout_update (self); |
147 |
layout_get_pixel_size (self, &w, &h); |
148 |
|
149 |
EXTEND (SP, 2); |
150 |
PUSHs (sv_2mortal (newSViv (w))); |
151 |
PUSHs (sv_2mortal (newSViv (h))); |
152 |
} |
153 |
|
154 |
int |
155 |
xy_to_index (CFClient::Layout self, int x, int y) |
156 |
CODE: |
157 |
{ |
158 |
int index, trailing; |
159 |
|
160 |
layout_update (self); |
161 |
pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing); |
162 |
|
163 |
RETVAL = index; |
164 |
} |
165 |
OUTPUT: |
166 |
RETVAL |
167 |
|
168 |
void |
169 |
cursor_pos (CFClient::Layout self, int index) |
170 |
PPCODE: |
171 |
{ |
172 |
PangoRectangle strong_pos; |
173 |
layout_update (self); |
174 |
pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0); |
175 |
EXTEND (SP, 3); |
176 |
PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE))); |
177 |
PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); |
178 |
PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); |
179 |
} |
180 |
|
181 |
void |
182 |
render (CFClient::Layout self) |
183 |
PPCODE: |
184 |
{ |
185 |
SV *retval; |
186 |
int w, h; |
187 |
FT_Bitmap bitmap; |
188 |
|
189 |
layout_update (self); |
190 |
layout_get_pixel_size (self, &w, &h); |
191 |
|
192 |
retval = newSV (w * h); |
193 |
SvPOK_only (retval); |
194 |
SvCUR_set (retval, w * h); |
195 |
|
196 |
bitmap.rows = h; |
197 |
bitmap.width = w; |
198 |
bitmap.pitch = w; |
199 |
bitmap.buffer = (unsigned char*)SvPVX (retval); |
200 |
bitmap.num_grays = 256; |
201 |
bitmap.pixel_mode = FT_PIXEL_MODE_GRAY; |
202 |
|
203 |
memset (bitmap.buffer, 0, w * h); |
204 |
|
205 |
pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE); |
206 |
|
207 |
EXTEND (SP, 3); |
208 |
PUSHs (sv_2mortal (newSViv (w))); |
209 |
PUSHs (sv_2mortal (newSViv (h))); |
210 |
PUSHs (sv_2mortal (retval)); |
211 |
} |
212 |
|
213 |
MODULE = CFClient PACKAGE = CFClient::Texture |
214 |
|
215 |
void |
216 |
draw_quad (SV *self, double x, double y, double w = 0, double h = 0) |
217 |
PROTOTYPE: $$$;$$ |
218 |
CODE: |
219 |
{ |
220 |
HV *hv = (HV *)SvRV (self); |
221 |
double s = SvNV (*hv_fetch (hv, "s", 1, 1)); |
222 |
double t = SvNV (*hv_fetch (hv, "t", 1, 1)); |
223 |
int name = SvIV (*hv_fetch (hv, "name", 4, 1)); |
224 |
|
225 |
if (items < 5) |
226 |
{ |
227 |
w = SvNV (*hv_fetch (hv, "w", 1, 1)); |
228 |
h = SvNV (*hv_fetch (hv, "h", 1, 1)); |
229 |
} |
230 |
|
231 |
glBindTexture (GL_TEXTURE_2D, name); |
232 |
glBegin (GL_QUADS); |
233 |
glTexCoord2d (0, 0); glVertex2d (x , y ); |
234 |
glTexCoord2d (0, t); glVertex2d (x , y + h); |
235 |
glTexCoord2d (s, t); glVertex2d (x + w, y + h); |
236 |
glTexCoord2d (s, 0); glVertex2d (x + w, y ); |
237 |
glEnd (); |
238 |
} |