1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
|
/*
+----------------------------------------------------------------------+
| PHP version 4.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Jouni Ahto <jah@mork.net> |
| |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#if 0 /* Moved currently back to gd.c */
#include "php.h"
#include "php_gd.h"
#if HAVE_LIBT1
void php_free_ps_font(int font_id)
{
T1_DeleteFont(font_id);
}
void php_free_ps_enc(char **enc)
{
T1_DeleteEncoding(enc);
}
/* {{{ proto int imagepsloadfont(string pathname)
Load a new font from specified file */
PHP_FUNCTION(imagepsloadfont)
{
zval **file;
int f_ind, l_ind;
if (ARG_COUNT(ht) != 1 || zend_get_parameters_ex(1, &file) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(file);
f_ind = T1_AddFont((*file)->value.str.val);
if (f_ind < 0) {
switch (f_ind) {
case -1:
php_error(E_WARNING, "Couldn't find the font file");
RETURN_FALSE;
break;
case -2:
case -3:
php_error(E_WARNING, "Memory allocation fault in t1lib");
RETURN_FALSE;
break;
default:
php_error(E_WARNING, "An unknown error occurred in t1lib");
RETURN_FALSE;
break;
}
}
T1_LoadFont(f_ind);
/*
l_ind = zend_list_insert(f_ind, T1_GLOBAL(le_ps_font));
RETURN_LONG(l_ind);
*/
zend_list_addref(f_ind);
RETURN_LONG(f_ind);
}
/* }}} */
/* {{{ The function in t1lib which this function uses seem to be buggy...
proto int imagepscopyfont(int font_index)
Make a copy of a font for purposes like extending or reenconding */
/*
PHP_FUNCTION(imagepscopyfont)
{
pval *fnt;
int l_ind, type;
gd_ps_font *nf_ind, *of_ind;
if (ARG_COUNT(ht) != 1 || zend_get_parameters(ht, 1, &fnt) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(fnt);
of_ind = zend_list_find(fnt->value.lval, &type);
if (type != GD_GLOBAL(le_ps_font)) {
php_error(E_WARNING, "%d is not a Type 1 font index", fnt->value.lval);
RETURN_FALSE;
}
nf_ind = emalloc(sizeof(gd_ps_font));
nf_ind->font_id = T1_CopyFont(of_ind->font_id);
if (nf_ind->font_id < 0) {
l_ind = nf_ind->font_id;
efree(nf_ind);
switch (l_ind) {
case -1:
php_error(E_WARNING, "FontID %d is not loaded in memory", l_ind);
RETURN_FALSE;
break;
case -2:
php_error(E_WARNING, "Tried to copy a logical font");
RETURN_FALSE;
break;
case -3:
php_error(E_WARNING, "Memory allocation fault in t1lib");
RETURN_FALSE;
break;
default:
php_error(E_WARNING, "An unknown error occurred in t1lib");
RETURN_FALSE;
break;
}
}
nf_ind->extend = 1;
l_ind = zend_list_insert(nf_ind, GD_GLOBAL(le_ps_font));
RETURN_LONG(l_ind);
}
*/
/* }}} */
/* {{{ proto bool imagepsfreefont(int font_index)
Free memory used by a font */
PHP_FUNCTION(imagepsfreefont)
{
zval **fnt;
int type;
if (ARG_COUNT(ht) != 1 || zend_get_parameters_ex(1, &fnt) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long_ex(fnt);
zend_list_find((*fnt)->value.lval, &type);
if (type != T1_GLOBAL(le_ps_font)) {
php_error(E_WARNING, "%d is not a Type 1 font index", (*fnt)->value.lval);
RETURN_FALSE;
}
zend_list_delete((*fnt)->value.lval);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool imagepsencodefont(int font_index, string filename)
To change a fonts character encoding vector */
PHP_FUNCTION(imagepsencodefont)
{
zval **fnt, **enc;
char **enc_vector;
int type;
int f_ind;
if (ARG_COUNT(ht) != 2 || zend_get_parameters_ex(2, &fnt, &enc) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long_ex(fnt);
convert_to_string_ex(enc);
f_ind = zend_list_find((*fnt)->value.lval, &type);
if (type != T1_GLOBAL(le_ps_font)) {
php_error(E_WARNING, "%d is not a Type 1 font index", (*fnt)->value.lval);
RETURN_FALSE;
}
if ((enc_vector = T1_LoadEncoding((*enc)->value.str.val)) == NULL) {
php_error(E_WARNING, "Couldn't load encoding vector from %s", (*enc)->value.str.val);
RETURN_FALSE;
}
T1_DeleteAllSizes(f_ind);
if (T1_ReencodeFont(f_ind, enc_vector)) {
T1_DeleteEncoding(enc_vector);
php_error(E_WARNING, "Couldn't reencode font");
RETURN_FALSE;
}
zend_list_insert(enc_vector, T1_GLOBAL(le_ps_enc));
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool imagepsextendfont(int font_index, double extend)
Extend or or condense (if extend < 1) a font */
PHP_FUNCTION(imagepsextendfont)
{
zval **fnt, **ext;
int type;
int f_ind;
if (ARG_COUNT(ht) != 2 || zend_get_parameters_ex(2, &fnt, &ext) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long_ex(fnt);
convert_to_double_ex(ext);
f_ind = zend_list_find((*fnt)->value.lval, &type);
if (type != T1_GLOBAL(le_ps_font)) {
php_error(E_WARNING, "%d is not a Type 1 font index", (*fnt)->value.lval);
RETURN_FALSE;
}
if (T1_ExtendFont(f_ind, (*ext)->value.dval) != 0) RETURN_FALSE;
/*
f_ind->extend = ext->value.dval;
*/
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool imagepsslantfont(int font_index, double slant)
Slant a font */
PHP_FUNCTION(imagepsslantfont)
{
zval **fnt, **slt;
int type;
int f_ind;
if (ARG_COUNT(ht) != 2 || zend_get_parameters_ex(2, &fnt, &slt) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long_ex(fnt);
convert_to_double_ex(slt);
f_ind = zend_list_find((*fnt)->value.lval, &type);
if (type != T1_GLOBAL(le_ps_font)) {
php_error(E_WARNING, "%d is not a Type 1 font index", (*fnt)->value.lval);
RETURN_FALSE;
}
if (T1_SlantFont(f_ind, (*slt)->value.dval) != 0) RETURN_FALSE;
RETURN_TRUE;
}
/* }}} */
/* {{{ proto array imagepstext(int image, string text, int font, int size, int xcoord, int ycoord [, int space, int tightness, double angle, int antialias])
Rasterize a string over an image */
PHP_FUNCTION(imagepstext)
{
zval **img, **str, **fnt, **sz, **fg, **bg, **sp, **px, **py, **aas, **wd, **ang;
int i, j, x, y;
int space, type;
int f_ind;
int h_lines, v_lines, c_ind;
int rd, gr, bl, fg_rd, fg_gr, fg_bl, bg_rd, bg_gr, bg_bl;
int aa[16], aa_steps;
int width, amount_kern, add_width;
double angle;
unsigned long aa_greys[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
gdImagePtr bg_img;
GLYPH *str_img;
T1_OUTLINE *char_path, *str_path;
T1_TMATRIX *transform = NULL;
switch(ARG_COUNT(ht)) {
case 8:
if (zend_get_parameters_ex(8, &img, &str, &fnt, &sz, &fg, &bg, &px, &py) == FAILURE) {
RETURN_FALSE;
}
convert_to_string_ex(str);
convert_to_long_ex(fnt);
convert_to_long_ex(sz);
convert_to_long_ex(fg);
convert_to_long_ex(bg);
convert_to_long_ex(px);
convert_to_long_ex(py);
x = (*px)->value.lval;
y = (*py)->value.lval;
space = 0;
aa_steps = 4;
width = 0;
angle = 0;
break;
case 12:
if (zend_get_parameters_ex(12, &img, &str, &fnt, &sz, &fg, &bg, &px, &py, &sp, &wd, &ang, &aas) == FAILURE) {
RETURN_FALSE;
}
convert_to_string_ex(str);
convert_to_long_ex(fnt);
convert_to_long_ex(sz);
convert_to_long_ex(sp);
convert_to_long_ex(fg);
convert_to_long_ex(bg);
convert_to_long_ex(px);
convert_to_long_ex(py);
x = (*px)->value.lval;
y = (*py)->value.lval;
convert_to_long_ex(sp);
space = (*sp)->value.lval;
convert_to_long_ex(aas);
aa_steps = (*aas)->value.lval;
convert_to_long_ex(wd);
width = (*wd)->value.lval;
convert_to_double_ex(ang);
angle = (*ang)->value.dval;
break;
default:
WRONG_PARAM_COUNT;
}
bg_img = zend_list_find((*img)->value.lval, &type);
if (!bg_img || type != GD_GLOBAL(le_gd)) {
php_error(E_WARNING, "Unable to find image pointer");
RETURN_FALSE;
}
f_ind = zend_list_find(fnt->value.lval, &type);
if (!f_ind || type != GD_GLOBAL(le_ps_font)) {
php_error(E_WARNING, "%d is not a Type 1 font index", fnt->value.lval);
RETURN_FALSE;
}
fg_rd = gdImageRed(bg_img, fg->value.lval);
fg_gr = gdImageGreen(bg_img, fg->value.lval);
fg_bl = gdImageBlue(bg_img, fg->value.lval);
bg_rd = gdImageRed(bg_img, bg->value.lval);
bg_gr = gdImageGreen(bg_img, bg->value.lval);
bg_bl = gdImageBlue(bg_img, bg->value.lval);
for (i = 0; i < aa_steps; i++) {
rd = bg_rd+(double)(fg_rd-bg_rd)/aa_steps*(i+1);
gr = bg_gr+(double)(fg_gr-bg_gr)/aa_steps*(i+1);
bl = bg_bl+(double)(fg_bl-bg_bl)/aa_steps*(i+1);
aa[i] = gdImageColorResolve(bg_img, rd, gr, bl);
}
T1_AASetBitsPerPixel(8);
switch (aa_steps) {
case 4:
T1_AASetGrayValues(0, 1, 2, 3, 4);
T1_AASetLevel(T1_AA_LOW);
break;
case 16:
T1_AAHSetGrayValues(aa_greys);
T1_AASetLevel(T1_AA_HIGH);
break;
default:
php_error(E_WARNING, "Invalid value %d as number of steps for antialiasing", aa_steps);
RETURN_FALSE;
}
if (angle) {
transform = T1_RotateMatrix(NULL, angle);
}
if (width) {
#ifdef HAVE_LIBT1_OUTLINE
str_path = T1_GetCharOutline(f_ind->font_id, str->value.str.val[0], sz->value.lval, transform);
for (i = 1; i < str->value.str.len; i++) {
amount_kern = (int) T1_GetKerning(f_ind->font_id, str->value.str.val[i-1], str->value.str.val[i]);
amount_kern += str->value.str.val[i-1] == ' ' ? space : 0;
add_width = (int) (amount_kern+width)/f_ind->extend;
char_path = T1_GetMoveOutline(f_ind->font_id, add_width, 0, 0, sz->value.lval, transform);
str_path = T1_ConcatOutlines(str_path, char_path);
char_path = T1_GetCharOutline(f_ind->font_id, str->value.str.val[i], sz->value.lval, transform);
str_path = T1_ConcatOutlines(str_path, char_path);
}
str_img = T1_AAFillOutline(str_path, 0);
#else
php_error(E_WARNING, "Setting space between characters in function ImagePSText is supported only with t1lib version 0.9 or above");
RETURN_FALSE;
#endif
} else {
str_img = T1_AASetString(f_ind->font_id, str->value.str.val, str->value.str.len,
space, T1_KERNING, sz->value.lval, transform);
}
if (T1_errno) RETURN_FALSE;
h_lines = str_img->metrics.ascent - str_img->metrics.descent;
v_lines = str_img->metrics.rightSideBearing - str_img->metrics.leftSideBearing;
for (i = 0; i < v_lines; i++) {
for (j = 0; j < h_lines; j++) {
switch (str_img->bits[j*v_lines+i]) {
case 0:
break;
default:
c_ind = aa[str_img->bits[j*v_lines+i]-1];
gdImageSetPixel(bg_img, x+str_img->metrics.leftSideBearing+i, y-str_img->metrics.ascent+j, c_ind);
}
}
}
if (array_init(return_value) == FAILURE) {
RETURN_FALSE;
}
add_next_index_long(return_value, str_img->metrics.leftSideBearing);
add_next_index_long(return_value, str_img->metrics.descent);
add_next_index_long(return_value, str_img->metrics.rightSideBearing);
add_next_index_long(return_value, str_img->metrics.ascent);
}
/* }}} */
/* {{{ proto array imagepsbbox(string text, int font, int size [, int space, int tightness, int angle])
Return the bounding box needed by a string if rasterized */
PHP_FUNCTION(imagepsbbox)
{
pval *str, *fnt, *sz, *sp, *wd, *ang;
int i, space, add_width, char_width, amount_kern, type;
int cur_x, cur_y, dx, dy;
int x1, y1, x2, y2, x3, y3, x4, y4;
gd_ps_font *f_ind;
int per_char = 0;
double angle, sin_a, cos_a;
BBox char_bbox, str_bbox = {0, 0, 0, 0};
switch(ARG_COUNT(ht)) {
case 3:
if (zend_get_parameters(ht, 3, &str, &fnt, &sz) == FAILURE) {
RETURN_FALSE;
}
convert_to_string(str);
convert_to_long(fnt);
convert_to_long(sz);
space = 0;
break;
case 6:
if (zend_get_parameters(ht, 6, &str, &fnt, &sz, &sp, &wd, &ang) == FAILURE) {
RETURN_FALSE;
}
convert_to_string(str);
convert_to_long(fnt);
convert_to_long(sz);
convert_to_long(sp);
space = sp->value.lval;
convert_to_long(wd);
add_width = wd->value.lval;
convert_to_double(ang);
angle = ang->value.dval * M_PI / 180;
sin_a = sin(angle);
cos_a = cos(angle);
per_char = add_width || angle ? 1 : 0;
break;
default:
WRONG_PARAM_COUNT;
}
f_ind = zend_list_find(fnt->value.lval, &type);
if (type != GD_GLOBAL(le_ps_font)) {
php_error(E_WARNING, "%d is not a Type 1 font index", fnt->value.lval);
RETURN_FALSE;
}
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define new_x(a, b) (int) ((a) * cos_a - (b) * sin_a)
#define new_y(a, b) (int) ((a) * sin_a + (b) * cos_a)
if (per_char) {
space += T1_GetCharWidth(f_ind->font_id, ' ');
cur_x = cur_y = 0;
for (i = 0; i < str->value.str.len; i++) {
if (str->value.str.val[i] == ' ') {
char_bbox.llx = char_bbox.lly = char_bbox.ury = 0;
char_bbox.urx = char_width = space;
} else {
char_bbox = T1_GetCharBBox(f_ind->font_id, str->value.str.val[i]);
char_width = T1_GetCharWidth(f_ind->font_id, str->value.str.val[i]);
}
amount_kern = i ? T1_GetKerning(f_ind->font_id, str->value.str.val[i-1], str->value.str.val[i]) : 0;
/* Transfer character bounding box to right place */
x1 = new_x(char_bbox.llx, char_bbox.lly) + cur_x;
y1 = new_y(char_bbox.llx, char_bbox.lly) + cur_y;
x2 = new_x(char_bbox.llx, char_bbox.ury) + cur_x;
y2 = new_y(char_bbox.llx, char_bbox.ury) + cur_y;
x3 = new_x(char_bbox.urx, char_bbox.ury) + cur_x;
y3 = new_y(char_bbox.urx, char_bbox.ury) + cur_y;
x4 = new_x(char_bbox.urx, char_bbox.lly) + cur_x;
y4 = new_y(char_bbox.urx, char_bbox.lly) + cur_y;
/* Find min & max values and compare them with current bounding box */
str_bbox.llx = min(str_bbox.llx, min(x1, min(x2, min(x3, x4))));
str_bbox.lly = min(str_bbox.lly, min(y1, min(y2, min(y3, y4))));
str_bbox.urx = max(str_bbox.urx, max(x1, max(x2, max(x3, x4))));
str_bbox.ury = max(str_bbox.ury, max(y1, max(y2, max(y3, y4))));
/* Move to the next base point */
dx = new_x(char_width + add_width + amount_kern, 0);
dy = new_y(char_width + add_width + amount_kern, 0);
cur_x += dx;
cur_y += dy;
/*
printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", x1, y1, x2, y2, x3, y3, x4, y4, char_bbox.llx, char_bbox.lly, char_bbox.urx, char_bbox.ury, char_width, amount_kern, cur_x, cur_y, dx, dy);
*/
}
} else {
str_bbox = T1_GetStringBBox(f_ind->font_id, str->value.str.val, str->value.str.len, space, T1_KERNING);
}
if (T1_errno) RETURN_FALSE;
if (array_init(return_value) == FAILURE) {
RETURN_FALSE;
}
/*
printf("%d %d %d %d\n", str_bbox.llx, str_bbox.lly, str_bbox.urx, str_bbox.ury);
*/
add_next_index_long(return_value, (int) ceil(((double) str_bbox.llx)*sz->value.lval/1000));
add_next_index_long(return_value, (int) ceil(((double) str_bbox.lly)*sz->value.lval/1000));
add_next_index_long(return_value, (int) ceil(((double) str_bbox.urx)*sz->value.lval/1000));
add_next_index_long(return_value, (int) ceil(((double) str_bbox.ury)*sz->value.lval/1000));
}
/* }}} */
#endif /* HAVE_LIBT1 */
#endif /* 0 */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
*/
|