-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfontft1.c
1408 lines (1079 loc) · 36.1 KB
/
fontft1.c
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "imager.h"
#include "imrender.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
/*
=head1 NAME
fontft1.c - Freetype 1.x font driver for Imager
=head1 SYNOPSIS
handle = i_tt_new(path_to_ttf);
rc = i_tt_bbox(handle, points, "foo", 3, int cords[6], utf8);
i_tt_destroy(handle);
// and much more
=head1 DESCRIPTION
fontft1.c implements font creation, rendering, bounding box functions and
more for Imager using Freetype 1.x.
In general this driver should be ignored in favour of the FT2 driver.
=head1 FUNCTION REFERENCE
Some of these functions are internal.
=over
=cut
*/
/* Truetype font support */
/* These are enabled by default when configuring Freetype 1.x
I haven't a clue how to reliably detect it at compile time.
We need a compilation probe in Makefile.PL
*/
#define FTXPOST 1
#define FTXERR18 1
#include <freetype.h>
#define TT_CHC 5
#ifdef FTXPOST
#include <ftxpost.h>
#endif
#ifdef FTXERR18
#include <ftxerr18.h>
#endif
/* some versions of FT1.x don't seem to define this - it's font defined
so it won't change */
#ifndef TT_MS_LANGID_ENGLISH_GENERAL
#define TT_MS_LANGID_ENGLISH_GENERAL 0x0409
#endif
static im_slot_t slot = -1;
/* convert a code point into an index in the glyph cache */
#define TT_HASH(x) ((x) & 0xFF)
typedef struct {
int initialized;
TT_Engine engine;
} i_tt_engine;
typedef struct i_glyph_entry_ {
TT_Glyph glyph;
unsigned long ch;
} i_tt_glyph_entry;
#define TT_NOCHAR (~0UL)
struct TT_Instancehandle_ {
TT_Instance instance;
TT_Instance_Metrics imetrics;
TT_Glyph_Metrics gmetrics[256];
i_tt_glyph_entry glyphs[256];
int smooth;
int order;
i_img_dim ptsize;
};
typedef struct TT_Instancehandle_ TT_Instancehandle;
struct TT_Fonthandle_ {
TT_Face face;
TT_Face_Properties properties;
TT_Instancehandle instanceh[TT_CHC];
TT_CharMap char_map;
#ifdef FTXPOST
int loaded_names;
TT_Error load_cond;
#endif
};
/* Defines */
#define USTRCT(x) ((x).z)
#define TT_VALID( handle ) ( ( handle ).z != NULL )
static void i_tt_push_error(TT_Error rc);
static void i_tt_uninit(void *);
/* Prototypes */
static int i_tt_get_instance( TT_Fonthandle *handle, i_img_dim points, int smooth );
static void i_tt_init_raster_map( TT_Raster_Map* bit, i_img_dim width, i_img_dim height, int smooth );
static void i_tt_done_raster_map( TT_Raster_Map *bit );
static void i_tt_clear_raster_map( TT_Raster_Map* bit );
static void i_tt_blit_or( TT_Raster_Map *dst, TT_Raster_Map *src,i_img_dim x_off, i_img_dim y_off );
static int i_tt_get_glyph( TT_Fonthandle *handle, int inst, unsigned long j );
static void
i_tt_render_glyph( TT_Glyph glyph, TT_Glyph_Metrics* gmetrics,
TT_Raster_Map *bit, TT_Raster_Map *small_bit,
i_img_dim x_off, i_img_dim y_off, int smooth );
static int
i_tt_render_all_glyphs( TT_Fonthandle *handle, int inst, TT_Raster_Map *bit,
TT_Raster_Map *small_bit, i_img_dim cords[6],
char const* txt, size_t len, int smooth, int utf8 );
static void i_tt_dump_raster_map2( i_img* im, TT_Raster_Map* bit, i_img_dim xb, i_img_dim yb, const i_color *cl, int smooth );
static void i_tt_dump_raster_map_channel( i_img* im, TT_Raster_Map* bit, i_img_dim xb, i_img_dim yb, int channel, int smooth );
static int
i_tt_rasterize( TT_Fonthandle *handle, TT_Raster_Map *bit, i_img_dim cords[6],
double points, char const* txt, size_t len, int smooth, int utf8 );
static undef_int i_tt_bbox_inst( TT_Fonthandle *handle, int inst ,const char *txt, size_t len, i_img_dim cords[6], int utf8 );
/* static globals needed */
static int LTT_dpi = 72; /* FIXME: this ought to be a part of the call interface */
static int LTT_hinted = 1; /* FIXME: this too */
/*
* FreeType interface
*/
void
i_tt_start(void) {
if (slot == -1)
slot = im_context_slot_new(i_tt_uninit);
}
/*
=item init_tt()
Initializes the freetype font rendering engine (if needed)
=cut
*/
static i_tt_engine *
i_init_tt(void) {
TT_Error error;
im_context_t ctx = im_get_context();
TT_Byte palette[] = { 0, 64, 127, 191, 255 };
i_tt_engine *result = im_context_slot_get(ctx, slot);
i_clear_error();
if (result == NULL) {
result = mymalloc(sizeof(i_tt_engine));
memset(result, 0, sizeof(*result));
im_context_slot_set(ctx, slot, result);
mm_log((1, "allocated FT1 state %p\n", result));
}
mm_log((1,"init_tt()\n"));
if (result->initialized)
return result;
error = TT_Init_FreeType( &result->engine );
if ( error ){
mm_log((1,"Initialization of freetype failed, code = 0x%x\n",
(unsigned)error));
i_tt_push_error(error);
i_push_error(0, "Could not initialize freetype 1.x");
return NULL;
}
#ifdef FTXPOST
error = TT_Init_Post_Extension( result->engine );
if (error) {
mm_log((1, "Initialization of Post extension failed = 0x%x\n",
(unsigned)error));
i_tt_push_error(error);
i_push_error(0, "Could not initialize FT 1.x POST extension");
return NULL;
}
#endif
error = TT_Set_Raster_Gray_Palette(result->engine, palette);
if (error) {
mm_log((1, "Initialization of gray levels failed = 0x%x\n",
(unsigned)error));
i_tt_push_error(error);
i_push_error(0, "Could not initialize FT 1.x POST extension");
return NULL;
}
mm_log((1, "initialized FT1 state %p\n", result));
result->initialized = 1;
return result;
}
static void
i_tt_uninit(void *p) {
i_tt_engine *tteng = p;
if (tteng->initialized) {
mm_log((1, "finalizing FT1 state %p\n", tteng));
TT_Done_FreeType(tteng->engine);
}
mm_log((1, "freeing FT1 state %p\n", tteng));
myfree(tteng);
}
/*
=item i_tt_get_instance(handle, points, smooth)
Finds a points+smooth instance or if one doesn't exist in the cache
allocates room and returns its cache entry
fontname - path to the font to load
handle - handle to the font.
points - points of the requested font
smooth - boolean (True: antialias on, False: antialias is off)
=cut
*/
static
int
i_tt_get_instance( TT_Fonthandle *handle, i_img_dim points, int smooth ) {
int i,idx;
TT_Error error;
mm_log((1,"i_tt_get_instance(handle %p, points %" i_DF ", smooth %d)\n",
handle, i_DFc(points), smooth));
if (smooth == -1) { /* Smooth doesn't matter for this search */
for(i=0;i<TT_CHC;i++) {
if (handle->instanceh[i].ptsize==points) {
mm_log((1,"i_tt_get_instance: in cache - (non selective smoothing search) returning %d\n",i));
return i;
}
}
smooth=1; /* We will be adding a font - add it as smooth then */
} else { /* Smooth doesn't matter for this search */
for(i=0;i<TT_CHC;i++) {
if (handle->instanceh[i].ptsize == points
&& handle->instanceh[i].smooth == smooth) {
mm_log((1,"i_tt_get_instance: in cache returning %d\n",i));
return i;
}
}
}
/* Found the instance in the cache - return the cache index */
for(idx=0;idx<TT_CHC;idx++) {
if (!(handle->instanceh[idx].order)) break; /* find the lru item */
}
mm_log((1,"i_tt_get_instance: lru item is %d\n",idx));
mm_log((1,"i_tt_get_instance: lru pointer %p\n",
USTRCT(handle->instanceh[idx].instance) ));
if ( USTRCT(handle->instanceh[idx].instance) ) {
mm_log((1,"i_tt_get_instance: freeing lru item from cache %d\n",idx));
/* Free cached glyphs */
for(i=0;i<256;i++)
if ( USTRCT(handle->instanceh[idx].glyphs[i].glyph) )
TT_Done_Glyph( handle->instanceh[idx].glyphs[i].glyph );
for(i=0;i<256;i++) {
handle->instanceh[idx].glyphs[i].ch = TT_NOCHAR;
USTRCT(handle->instanceh[idx].glyphs[i].glyph)=NULL;
}
/* Free instance if needed */
TT_Done_Instance( handle->instanceh[idx].instance );
}
/* create and initialize instance */
/* FIXME: probably a memory leak on fail */
(void) (( error = TT_New_Instance( handle->face, &handle->instanceh[idx].instance ) ) ||
( error = TT_Set_Instance_Resolutions( handle->instanceh[idx].instance, LTT_dpi, LTT_dpi ) ) ||
( error = TT_Set_Instance_CharSize( handle->instanceh[idx].instance, points*64 ) ) );
if ( error ) {
mm_log((1, "Could not create and initialize instance: error %x.\n",
(unsigned)error ));
return -1;
}
/* Now that the instance should the inplace we need to lower all of the
ru counts and put `this' one with the highest entry */
for(i=0;i<TT_CHC;i++) handle->instanceh[i].order--;
handle->instanceh[idx].order=TT_CHC-1;
handle->instanceh[idx].ptsize=points;
handle->instanceh[idx].smooth=smooth;
TT_Get_Instance_Metrics( handle->instanceh[idx].instance, &(handle->instanceh[idx].imetrics) );
/* Zero the memory for the glyph storage so they are not thought as
cached if they haven't been cached since this new font was loaded */
for(i=0;i<256;i++) {
handle->instanceh[idx].glyphs[i].ch = TT_NOCHAR;
USTRCT(handle->instanceh[idx].glyphs[i].glyph)=NULL;
}
return idx;
}
/*
=item i_tt_new(fontname)
Creates a new font handle object, finds a character map and initialise the
the font handle's cache
fontname - path to the font to load
=cut
*/
TT_Fonthandle*
i_tt_new(const char *fontname) {
TT_Error error;
TT_Fonthandle *handle;
unsigned short i,n;
unsigned short platform,encoding;
i_tt_engine *tteng;
if ((tteng = i_init_tt()) == NULL) {
i_push_error(0, "Could not initialize FT1 engine");
return NULL;
}
i_clear_error();
mm_log((1,"i_tt_new(fontname '%s')\n",fontname));
/* allocate memory for the structure */
handle = mymalloc( sizeof(TT_Fonthandle) ); /* checked 5Nov05 tonyc */
/* load the typeface */
error = TT_Open_Face( tteng->engine, fontname, &handle->face );
if ( error ) {
myfree(handle);
if ( error == TT_Err_Could_Not_Open_File ) {
mm_log((1, "Could not find/open %s.\n", fontname ));
}
else {
mm_log((1, "Error while opening %s, error code = 0x%x.\n",fontname,
(unsigned)error ));
}
i_tt_push_error(error);
return NULL;
}
TT_Get_Face_Properties( handle->face, &(handle->properties) );
/* First, look for a Unicode charmap */
n = handle->properties.num_CharMaps;
USTRCT( handle->char_map )=NULL; /* Invalidate character map */
for ( i = 0; i < n; i++ ) {
TT_Get_CharMap_ID( handle->face, i, &platform, &encoding );
if ( (platform == 3 && encoding == 1 )
|| (platform == 0 && encoding == 0 ) ) {
mm_log((2,"i_tt_new - found char map platform %u encoding %u\n",
platform, encoding));
TT_Get_CharMap( handle->face, i, &(handle->char_map) );
break;
}
}
if (!USTRCT(handle->char_map) && n != 0) {
/* just use the first one */
TT_Get_CharMap( handle->face, 0, &(handle->char_map));
}
/* Zero the pointsizes - and ordering */
for(i=0;i<TT_CHC;i++) {
USTRCT(handle->instanceh[i].instance)=NULL;
handle->instanceh[i].order=i;
handle->instanceh[i].ptsize=0;
handle->instanceh[i].smooth=-1;
}
#ifdef FTXPOST
handle->loaded_names = 0;
#endif
mm_log((1,"i_tt_new <- %p\n",handle));
return handle;
}
/*
* raster map management
*/
/*
=item i_tt_init_raster_map(bit, width, height, smooth)
Allocates internal memory for the bitmap as needed by the parameters (internal)
bit - bitmap to allocate into
width - width of the bitmap
height - height of the bitmap
smooth - boolean (True: antialias on, False: antialias is off)
=cut
*/
static
void
i_tt_init_raster_map( TT_Raster_Map* bit, i_img_dim width, i_img_dim height, int smooth ) {
mm_log((1,"i_tt_init_raster_map( bit %p, width %" i_DF ", height %" i_DF
", smooth %d)\n", bit, i_DFc(width), i_DFc(height), smooth));
bit->rows = height;
bit->width = ( width + 3 ) & -4;
bit->flow = TT_Flow_Down;
if ( smooth ) {
bit->cols = bit->width;
bit->size = bit->rows * bit->width;
} else {
bit->cols = ( bit->width + 7 ) / 8; /* convert to # of bytes */
bit->size = bit->rows * bit->cols; /* number of bytes in buffer */
}
/* rows can be 0 for some glyphs, for example ' ' */
if (bit->rows && bit->size / bit->rows != bit->cols) {
i_fatal(0, "Integer overflow calculating bitmap size (%d, %d)\n",
bit->width, bit->rows);
}
mm_log((1,"i_tt_init_raster_map: bit->width %d, bit->cols %d, bit->rows %d, bit->size %ld)\n", bit->width, bit->cols, bit->rows, bit->size ));
bit->bitmap = (void *) mymalloc( bit->size ); /* checked 6Nov05 tonyc */
if ( !bit->bitmap ) i_fatal(0,"Not enough memory to allocate bitmap (%d)!\n",bit->size );
}
/*
=item i_tt_clear_raster_map(bit)
Frees the bitmap data and sets pointer to NULL (internal)
bit - bitmap to free
=cut
*/
static
void
i_tt_done_raster_map( TT_Raster_Map *bit ) {
myfree( bit->bitmap );
bit->bitmap = NULL;
}
/*
=item i_tt_clear_raster_map(bit)
Clears the specified bitmap (internal)
bit - bitmap to zero
=cut
*/
static
void
i_tt_clear_raster_map( TT_Raster_Map* bit ) {
memset( bit->bitmap, 0, bit->size );
}
/*
=item i_tt_blit_or(dst, src, x_off, y_off)
function that blits one raster map into another (internal)
dst - destination bitmap
src - source bitmap
x_off - x offset into the destination bitmap
y_off - y offset into the destination bitmap
=cut
*/
static
void
i_tt_blit_or( TT_Raster_Map *dst, TT_Raster_Map *src,i_img_dim x_off, i_img_dim y_off ) {
i_img_dim x, y;
i_img_dim x1, x2, y1, y2;
unsigned char *s, *d;
x1 = x_off < 0 ? -x_off : 0;
y1 = y_off < 0 ? -y_off : 0;
x2 = (int)dst->cols - x_off;
if ( x2 > src->cols ) x2 = src->cols;
y2 = (int)dst->rows - y_off;
if ( y2 > src->rows ) y2 = src->rows;
if ( x1 >= x2 ) return;
/* do the real work now */
for ( y = y1; y < y2; ++y ) {
s = ( (unsigned char*)src->bitmap ) + y * src->cols + x1;
d = ( (unsigned char*)dst->bitmap ) + ( y + y_off ) * dst->cols + x1 + x_off;
for ( x = x1; x < x2; ++x ) {
if (*s > *d)
*d = *s;
d++;
s++;
}
}
}
/* useful for debugging */
#if 0
static void dump_raster_map(FILE *out, TT_Raster_Map *bit ) {
int x, y;
fprintf(out, "cols %d rows %d flow %d\n", bit->cols, bit->rows, bit->flow);
for (y = 0; y < bit->rows; ++y) {
fprintf(out, "%2d:", y);
for (x = 0; x < bit->cols; ++x) {
if ((x & 7) == 0 && x) putc(' ', out);
fprintf(out, "%02x", ((unsigned char *)bit->bitmap)[y*bit->cols+x]);
}
putc('\n', out);
}
}
#endif
/*
=item i_tt_get_glyph(handle, inst, j)
Function to see if a glyph exists and if so cache it (internal)
handle - pointer to font handle
inst - font instance
j - charcode of glyph
=cut
*/
static
int
i_tt_get_glyph( TT_Fonthandle *handle, int inst, unsigned long j) {
unsigned short load_flags, code;
TT_Error error;
mm_log((1, "i_tt_get_glyph(handle %p, inst %d, j %lu (%c))\n",
handle,inst,j, (int)((j >= ' ' && j <= '~') ? j : '.')));
/*mm_log((1, "handle->instanceh[inst].glyphs[j]=0x%08X\n",handle->instanceh[inst].glyphs[j] ));*/
if ( TT_VALID(handle->instanceh[inst].glyphs[TT_HASH(j)].glyph)
&& handle->instanceh[inst].glyphs[TT_HASH(j)].ch == j) {
mm_log((1,"i_tt_get_glyph: %lu in cache\n",j));
return 1;
}
if ( TT_VALID(handle->instanceh[inst].glyphs[TT_HASH(j)].glyph) ) {
/* clean up the entry */
TT_Done_Glyph( handle->instanceh[inst].glyphs[TT_HASH(j)].glyph );
USTRCT( handle->instanceh[inst].glyphs[TT_HASH(j)].glyph ) = NULL;
handle->instanceh[inst].glyphs[TT_HASH(j)].ch = TT_NOCHAR;
}
/* Ok - it wasn't cached - try to get it in */
load_flags = TTLOAD_SCALE_GLYPH;
if ( LTT_hinted ) load_flags |= TTLOAD_HINT_GLYPH;
if ( !TT_VALID(handle->char_map) ) {
code = (j < ' ' - 1) ? 0 : (j - (' ' - 1));
if ( code >= handle->properties.num_Glyphs ) code = 0;
} else code = TT_Char_Index( handle->char_map, j );
if ( (error = TT_New_Glyph( handle->face, &handle->instanceh[inst].glyphs[TT_HASH(j)].glyph)) ) {
mm_log((1, "Cannot allocate and load glyph: error %#x.\n", (unsigned)error ));
i_push_error(error, "TT_New_Glyph()");
return 0;
}
if ( (error = TT_Load_Glyph( handle->instanceh[inst].instance, handle->instanceh[inst].glyphs[TT_HASH(j)].glyph, code, load_flags)) ) {
mm_log((1, "Cannot allocate and load glyph: error %#x.\n", (unsigned)error ));
/* Don't leak */
TT_Done_Glyph( handle->instanceh[inst].glyphs[TT_HASH(j)].glyph );
USTRCT( handle->instanceh[inst].glyphs[TT_HASH(j)].glyph ) = NULL;
i_push_error(error, "TT_Load_Glyph()");
return 0;
}
/* At this point the glyph should be allocated and loaded */
handle->instanceh[inst].glyphs[TT_HASH(j)].ch = j;
/* Next get the glyph metrics */
error = TT_Get_Glyph_Metrics( handle->instanceh[inst].glyphs[TT_HASH(j)].glyph,
&handle->instanceh[inst].gmetrics[TT_HASH(j)] );
if (error) {
mm_log((1, "TT_Get_Glyph_Metrics: error %#x.\n", (unsigned)error ));
TT_Done_Glyph( handle->instanceh[inst].glyphs[TT_HASH(j)].glyph );
USTRCT( handle->instanceh[inst].glyphs[TT_HASH(j)].glyph ) = NULL;
handle->instanceh[inst].glyphs[TT_HASH(j)].ch = TT_NOCHAR;
i_push_error(error, "TT_Get_Glyph_Metrics()");
return 0;
}
return 1;
}
/*
=item i_tt_has_chars(handle, text, len, utf8, out)
Check if the given characters are defined by the font. Note that len
is the number of bytes, not the number of characters (when utf8 is
non-zero).
Returns the number of characters that were checked.
=cut
*/
size_t
i_tt_has_chars(TT_Fonthandle *handle, char const *text, size_t len, int utf8,
char *out) {
size_t count = 0;
mm_log((1, "i_tt_has_chars(handle %p, text %p, len %ld, utf8 %d)\n",
handle, text, (long)len, utf8));
while (len) {
unsigned long c;
int index;
if (utf8) {
c = i_utf8_advance(&text, &len);
if (c == ~0UL) {
i_push_error(0, "invalid UTF8 character");
return 0;
}
}
else {
c = (unsigned char)*text++;
--len;
}
if (TT_VALID(handle->char_map)) {
index = TT_Char_Index(handle->char_map, c);
}
else {
index = (c < ' ' - 1) ? 0 : (c - (' ' - 1));
if (index >= handle->properties.num_Glyphs)
index = 0;
}
*out++ = index != 0;
++count;
}
return count;
}
/*
=item i_tt_destroy(handle)
Clears the data taken by a font including all cached data such as
pixmaps and glyphs
handle - pointer to font handle
=cut
*/
void
i_tt_destroy( TT_Fonthandle *handle) {
TT_Close_Face( handle->face );
myfree( handle );
/* FIXME: Should these be freed automatically by the library?
TT_Done_Instance( instance );
void
i_tt_done_glyphs( void ) {
int i;
if ( !glyphs ) return;
for ( i = 0; i < 256; ++i ) TT_Done_Glyph( glyphs[i] );
free( glyphs );
glyphs = NULL;
}
*/
}
/*
* FreeType Rendering functions
*/
/*
=item i_tt_render_glyph(handle, gmetrics, bit, smallbit, x_off, y_off, smooth)
Renders a single glyph into the bit rastermap (internal)
handle - pointer to font handle
gmetrics - the metrics for the glyph to be rendered
bit - large bitmap that is the destination for the text
smallbit - small bitmap that is used only if smooth is true
x_off - x offset of glyph
y_off - y offset of glyph
smooth - boolean (True: antialias on, False: antialias is off)
=cut
*/
static
void
i_tt_render_glyph( TT_Glyph glyph, TT_Glyph_Metrics* gmetrics, TT_Raster_Map *bit, TT_Raster_Map *small_bit, i_img_dim x_off, i_img_dim y_off, int smooth ) {
mm_log((1,"i_tt_render_glyph(glyph %p, gmetrics %p, bit %p, small_bit %p, x_off %" i_DF ", y_off %" i_DF ", smooth %d)\n",
USTRCT(glyph), gmetrics, bit, small_bit, i_DFc(x_off),
i_DFc(y_off), smooth));
if ( !smooth ) TT_Get_Glyph_Bitmap( glyph, bit, x_off * 64, y_off * 64);
else {
TT_F26Dot6 xmin, ymin;
xmin = gmetrics->bbox.xMin & -64;
ymin = gmetrics->bbox.yMin & -64;
i_tt_clear_raster_map( small_bit );
TT_Get_Glyph_Pixmap( glyph, small_bit, -xmin, -ymin );
i_tt_blit_or( bit, small_bit, xmin/64 + x_off, -ymin/64 - y_off );
}
}
/*
=item i_tt_render_all_glyphs(handle, inst, bit, small_bit, cords, txt, len, smooth)
calls i_tt_render_glyph to render each glyph into the bit rastermap (internal)
handle - pointer to font handle
inst - font instance
bit - large bitmap that is the destination for the text
smallbit - small bitmap that is used only if smooth is true
txt - string to render
len - length of the string to render
smooth - boolean (True: antialias on, False: antialias is off)
=cut
*/
static
int
i_tt_render_all_glyphs( TT_Fonthandle *handle, int inst, TT_Raster_Map *bit,
TT_Raster_Map *small_bit, i_img_dim cords[6],
char const* txt, size_t len, int smooth, int utf8 ) {
unsigned long j;
TT_F26Dot6 x,y;
mm_log((1,"i_tt_render_all_glyphs( handle %p, inst %d, bit %p, small_bit %p, txt '%.*s', len %ld, smooth %d, utf8 %d)\n",
handle, inst, bit, small_bit, (int)len, txt, (long)len, smooth, utf8));
/*
y=-( handle->properties.horizontal->Descender * handle->instanceh[inst].imetrics.y_ppem )/(handle->properties.header->Units_Per_EM);
*/
x=-cords[0]; /* FIXME: If you font is antialiased this should be expanded by one to allow for aa expansion and the allocation too - do before passing here */
y=-cords[4];
while (len) {
if (utf8) {
j = i_utf8_advance(&txt, &len);
if (j == ~0UL) {
i_push_error(0, "invalid UTF8 character");
return 0;
}
}
else {
j = (unsigned char)*txt++;
--len;
}
if ( !i_tt_get_glyph(handle,inst,j) )
continue;
i_tt_render_glyph( handle->instanceh[inst].glyphs[TT_HASH(j)].glyph,
&handle->instanceh[inst].gmetrics[TT_HASH(j)], bit,
small_bit, x, y, smooth );
x += handle->instanceh[inst].gmetrics[TT_HASH(j)].advance / 64;
}
return 1;
}
/*
* Functions to render rasters (single channel images) onto images
*/
/*
=item i_tt_dump_raster_map2(im, bit, xb, yb, cl, smooth)
Function to dump a raster onto an image in color used by i_tt_text() (internal).
im - image to dump raster on
bit - bitmap that contains the text to be dumped to im
xb, yb - coordinates, left edge and baseline
cl - color to use for text
smooth - boolean (True: antialias on, False: antialias is off)
=cut
*/
static
void
i_tt_dump_raster_map2( i_img* im, TT_Raster_Map* bit, i_img_dim xb, i_img_dim yb, const i_color *cl, int smooth ) {
unsigned char *bmap;
i_img_dim x, y;
mm_log((1,"i_tt_dump_raster_map2(im %p, bit %p, xb %" i_DF ", yb %" i_DF ", cl %p)\n",
im, bit, i_DFc(xb), i_DFc(yb), cl));
bmap = bit->bitmap;
if ( smooth ) {
i_render r;
i_render_init(&r, im, bit->cols);
for(y=0;y<bit->rows;y++) {
i_render_color(&r, xb, yb+y, bit->cols, bmap + y*bit->cols, cl);
}
i_render_done(&r);
} else {
unsigned char *bmp = mymalloc(bit->width);
i_render r;
i_render_init(&r, im, bit->width);
for(y=0;y<bit->rows;y++) {
unsigned mask = 0x80;
unsigned char *p = bmap + y * bit->cols;
unsigned char *pout = bmp;
for(x = 0; x < bit->width; x++) {
*pout++ = (*p & mask) ? 0xFF : 0;
mask >>= 1;
if (!mask) {
mask = 0x80;
++p;
}
}
i_render_color(&r, xb, yb+y, bit->width, bmp, cl);
}
i_render_done(&r);
myfree(bmp);
}
}
/*
=item i_tt_dump_raster_map_channel(im, bit, xb, yb, channel, smooth)
Function to dump a raster onto a single channel image in color (internal)
im - image to dump raster on
bit - bitmap that contains the text to be dumped to im
xb, yb - coordinates, left edge and baseline
channel - channel to copy to
smooth - boolean (True: antialias on, False: antialias is off)
=cut
*/
static
void
i_tt_dump_raster_map_channel( i_img* im, TT_Raster_Map* bit, i_img_dim xb, i_img_dim yb, int channel, int smooth ) {
unsigned char *bmap;
i_color val;
int c;
i_img_dim x,y;
int old_mask = im->ch_mask;
im->ch_mask = 1 << channel;
mm_log((1,"i_tt_dump_raster_channel(im %p, bit %p, xb %" i_DF ", yb %" i_DF ", channel %d)\n",
im, bit, i_DFc(xb), i_DFc(yb), channel));
bmap = bit->bitmap;
if ( smooth ) {
for(y=0;y<bit->rows;y++) for(x=0;x<bit->width;x++) {
c = bmap[y*(bit->cols)+x];
val.channel[channel] = c;
i_ppix(im,x+xb,y+yb,&val);
}
} else {
for(y=0;y<bit->rows;y++) {
unsigned mask = 0x80;
unsigned char *p = bmap + y * bit->cols;
for(x=0;x<bit->width;x++) {
val.channel[channel] = (*p & mask) ? 255 : 0;
i_ppix(im,x+xb,y+yb,&val);
mask >>= 1;
if (!mask) {
++p;
mask = 0x80;
}
}
}
}
im->ch_mask = old_mask;
}
/*
=item i_tt_rasterize(handle, bit, cords, points, txt, len, smooth)
interface for generating single channel raster of text (internal)
handle - pointer to font handle
bit - the bitmap that is allocated, rendered into and NOT freed
cords - the bounding box (modified in place)
points - font size to use
txt - string to render
len - length of the string to render
smooth - boolean (True: antialias on, False: antialias is off)
=cut
*/
static
int
i_tt_rasterize( TT_Fonthandle *handle, TT_Raster_Map *bit, i_img_dim *cords, double points, char const* txt, size_t len, int smooth, int utf8 ) {
int inst;
i_img_dim width, height;
TT_Raster_Map small_bit;
/* find or install an instance */
if ( (inst=i_tt_get_instance(handle,points,smooth)) < 0) {
mm_log((1,"i_tt_rasterize: get instance failed\n"));
return 0;
}
/* calculate bounding box */
if (!i_tt_bbox_inst( handle, inst, txt, len, cords, utf8 ))
return 0;
width = cords[2]-cords[0];
height = cords[5]-cords[4];
mm_log((1,"i_tt_rasterize: width=%" i_DF ", height=%" i_DF "\n",
i_DFc(width), i_DFc(height) ));
i_tt_init_raster_map ( bit, width, height, smooth );
i_tt_clear_raster_map( bit );
if ( smooth ) i_tt_init_raster_map( &small_bit, handle->instanceh[inst].imetrics.x_ppem + 32, height, smooth );