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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
|
#include "fitz-internal.h"
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_STROKER_H
#define MAX_BBOX_TABLE_SIZE 4096
/* 20 degrees */
#define SHEAR 0.36397f
static void fz_drop_freetype(fz_context *ctx);
static fz_font *
fz_new_font(fz_context *ctx, char *name, int use_glyph_bbox, int glyph_count)
{
fz_font *font;
int i;
font = fz_malloc_struct(ctx, fz_font);
font->refs = 1;
if (name)
fz_strlcpy(font->name, name, sizeof font->name);
else
fz_strlcpy(font->name, "(null)", sizeof font->name);
font->ft_face = NULL;
font->ft_substitute = 0;
font->ft_bold = 0;
font->ft_italic = 0;
font->ft_hint = 0;
font->ft_file = NULL;
font->ft_data = NULL;
font->ft_size = 0;
font->t3matrix = fz_identity;
font->t3resources = NULL;
font->t3procs = NULL;
font->t3lists = NULL;
font->t3widths = NULL;
font->t3flags = NULL;
font->t3doc = NULL;
font->t3run = NULL;
font->bbox.x0 = 0;
font->bbox.y0 = 0;
font->bbox.x1 = 1;
font->bbox.y1 = 1;
font->use_glyph_bbox = use_glyph_bbox;
if (use_glyph_bbox && glyph_count <= MAX_BBOX_TABLE_SIZE)
{
font->bbox_count = glyph_count;
font->bbox_table = fz_malloc_array(ctx, glyph_count, sizeof(fz_rect));
for (i = 0; i < glyph_count; i++)
font->bbox_table[i] = fz_infinite_rect;
}
else
{
if (use_glyph_bbox)
fz_warn(ctx, "not building glyph bbox table for font '%s' with %d glyphs", font->name, glyph_count);
font->bbox_count = 0;
font->bbox_table = NULL;
}
font->width_count = 0;
font->width_table = NULL;
return font;
}
fz_font *
fz_keep_font(fz_context *ctx, fz_font *font)
{
if (!font)
return NULL;
fz_lock(ctx, FZ_LOCK_ALLOC);
font->refs ++;
fz_unlock(ctx, FZ_LOCK_ALLOC);
return font;
}
void
fz_drop_font(fz_context *ctx, fz_font *font)
{
int fterr;
int i, drop;
fz_lock(ctx, FZ_LOCK_ALLOC);
drop = (font && --font->refs == 0);
fz_unlock(ctx, FZ_LOCK_ALLOC);
if (!drop)
return;
if (font->t3procs)
{
if (font->t3resources)
font->t3freeres(font->t3doc, font->t3resources);
for (i = 0; i < 256; i++)
{
if (font->t3procs[i])
fz_drop_buffer(ctx, font->t3procs[i]);
if (font->t3lists[i])
fz_free_display_list(ctx, font->t3lists[i]);
}
fz_free(ctx, font->t3procs);
fz_free(ctx, font->t3lists);
fz_free(ctx, font->t3widths);
fz_free(ctx, font->t3flags);
}
if (font->ft_face)
{
fz_lock(ctx, FZ_LOCK_FREETYPE);
fterr = FT_Done_Face((FT_Face)font->ft_face);
fz_unlock(ctx, FZ_LOCK_FREETYPE);
if (fterr)
fz_warn(ctx, "freetype finalizing face: %s", ft_error_string(fterr));
fz_drop_freetype(ctx);
}
fz_free(ctx, font->ft_file);
fz_free(ctx, font->ft_data);
fz_free(ctx, font->bbox_table);
fz_free(ctx, font->width_table);
fz_free(ctx, font);
}
void
fz_set_font_bbox(fz_context *ctx, fz_font *font, float xmin, float ymin, float xmax, float ymax)
{
font->bbox.x0 = xmin;
font->bbox.y0 = ymin;
font->bbox.x1 = xmax;
font->bbox.y1 = ymax;
}
/*
* Freetype hooks
*/
struct fz_font_context_s {
int ctx_refs;
FT_Library ftlib;
int ftlib_refs;
};
#undef __FTERRORS_H__
#define FT_ERRORDEF(e, v, s) { (e), (s) },
#define FT_ERROR_START_LIST
#define FT_ERROR_END_LIST { 0, NULL }
struct ft_error
{
int err;
char *str;
};
void fz_new_font_context(fz_context *ctx)
{
ctx->font = fz_malloc_struct(ctx, fz_font_context);
ctx->font->ctx_refs = 1;
ctx->font->ftlib = NULL;
ctx->font->ftlib_refs = 0;
}
fz_font_context *
fz_keep_font_context(fz_context *ctx)
{
if (!ctx || !ctx->font)
return NULL;
fz_lock(ctx, FZ_LOCK_ALLOC);
ctx->font->ctx_refs++;
fz_unlock(ctx, FZ_LOCK_ALLOC);
return ctx->font;
}
void fz_drop_font_context(fz_context *ctx)
{
int drop;
if (!ctx || !ctx->font)
return;
fz_lock(ctx, FZ_LOCK_ALLOC);
drop = --ctx->font->ctx_refs;
fz_unlock(ctx, FZ_LOCK_ALLOC);
if (drop == 0)
fz_free(ctx, ctx->font);
}
static const struct ft_error ft_errors[] =
{
#include FT_ERRORS_H
};
char *ft_error_string(int err)
{
const struct ft_error *e;
for (e = ft_errors; e->str; e++)
if (e->err == err)
return e->str;
return "Unknown error";
}
static void
fz_keep_freetype(fz_context *ctx)
{
int fterr;
int maj, min, pat;
fz_font_context *fct = ctx->font;
fz_lock(ctx, FZ_LOCK_FREETYPE);
if (fct->ftlib)
{
fct->ftlib_refs++;
fz_unlock(ctx, FZ_LOCK_FREETYPE);
return;
}
fterr = FT_Init_FreeType(&fct->ftlib);
if (fterr)
{
char *mess = ft_error_string(fterr);
fz_unlock(ctx, FZ_LOCK_FREETYPE);
fz_throw(ctx, "cannot init freetype: %s", mess);
}
FT_Library_Version(fct->ftlib, &maj, &min, &pat);
if (maj == 2 && min == 1 && pat < 7)
{
fterr = FT_Done_FreeType(fct->ftlib);
if (fterr)
fz_warn(ctx, "freetype finalizing: %s", ft_error_string(fterr));
fz_unlock(ctx, FZ_LOCK_FREETYPE);
fz_throw(ctx, "freetype version too old: %d.%d.%d", maj, min, pat);
}
fct->ftlib_refs++;
fz_unlock(ctx, FZ_LOCK_FREETYPE);
}
static void
fz_drop_freetype(fz_context *ctx)
{
int fterr;
fz_font_context *fct = ctx->font;
fz_lock(ctx, FZ_LOCK_FREETYPE);
if (--fct->ftlib_refs == 0)
{
fterr = FT_Done_FreeType(fct->ftlib);
if (fterr)
fz_warn(ctx, "freetype finalizing: %s", ft_error_string(fterr));
fct->ftlib = NULL;
}
fz_unlock(ctx, FZ_LOCK_FREETYPE);
}
fz_font *
fz_new_font_from_file(fz_context *ctx, char *name, char *path, int index, int use_glyph_bbox)
{
FT_Face face;
fz_font *font;
int fterr;
fz_keep_freetype(ctx);
fz_lock(ctx, FZ_LOCK_FREETYPE);
fterr = FT_New_Face(ctx->font->ftlib, path, index, &face);
fz_unlock(ctx, FZ_LOCK_FREETYPE);
if (fterr)
{
fz_drop_freetype(ctx);
fz_throw(ctx, "freetype: cannot load font: %s", ft_error_string(fterr));
}
if (!name)
name = face->family_name;
font = fz_new_font(ctx, name, use_glyph_bbox, face->num_glyphs);
font->ft_face = face;
font->bbox.x0 = (float) face->bbox.xMin / face->units_per_EM;
font->bbox.y0 = (float) face->bbox.yMin / face->units_per_EM;
font->bbox.x1 = (float) face->bbox.xMax / face->units_per_EM;
font->bbox.y1 = (float) face->bbox.yMax / face->units_per_EM;
return font;
}
fz_font *
fz_new_font_from_memory(fz_context *ctx, char *name, unsigned char *data, int len, int index, int use_glyph_bbox)
{
FT_Face face;
fz_font *font;
int fterr;
fz_keep_freetype(ctx);
fz_lock(ctx, FZ_LOCK_FREETYPE);
fterr = FT_New_Memory_Face(ctx->font->ftlib, data, len, index, &face);
fz_unlock(ctx, FZ_LOCK_FREETYPE);
if (fterr)
{
fz_drop_freetype(ctx);
fz_throw(ctx, "freetype: cannot load font: %s", ft_error_string(fterr));
}
if (!name)
name = face->family_name;
font = fz_new_font(ctx, name, use_glyph_bbox, face->num_glyphs);
font->ft_face = face;
font->bbox.x0 = (float) face->bbox.xMin / face->units_per_EM;
font->bbox.y0 = (float) face->bbox.yMin / face->units_per_EM;
font->bbox.x1 = (float) face->bbox.xMax / face->units_per_EM;
font->bbox.y1 = (float) face->bbox.yMax / face->units_per_EM;
return font;
}
static fz_matrix
fz_adjust_ft_glyph_width(fz_context *ctx, fz_font *font, int gid, fz_matrix trm)
{
/* Fudge the font matrix to stretch the glyph if we've substituted the font. */
if (font->ft_substitute && font->width_table && gid < font->width_count)
{
FT_Error fterr;
int subw;
int realw;
float scale;
fz_lock(ctx, FZ_LOCK_FREETYPE);
/* TODO: use FT_Get_Advance */
fterr = FT_Set_Char_Size(font->ft_face, 1000, 1000, 72, 72);
if (fterr)
fz_warn(ctx, "freetype setting character size: %s", ft_error_string(fterr));
fterr = FT_Load_Glyph(font->ft_face, gid,
FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP | FT_LOAD_IGNORE_TRANSFORM);
if (fterr)
fz_warn(ctx, "freetype failed to load glyph: %s", ft_error_string(fterr));
realw = ((FT_Face)font->ft_face)->glyph->metrics.horiAdvance;
fz_unlock(ctx, FZ_LOCK_FREETYPE);
subw = font->width_table[gid];
if (realw)
scale = (float) subw / realw;
else
scale = 1;
return fz_concat(fz_scale(scale, 1), trm);
}
return trm;
}
static fz_pixmap *
fz_copy_ft_bitmap(fz_context *ctx, int left, int top, FT_Bitmap *bitmap)
{
fz_pixmap *pixmap;
int y;
pixmap = fz_new_pixmap(ctx, NULL, bitmap->width, bitmap->rows);
pixmap->x = left;
pixmap->y = top - bitmap->rows;
if (bitmap->pixel_mode == FT_PIXEL_MODE_MONO)
{
for (y = 0; y < pixmap->h; y++)
{
unsigned char *out = pixmap->samples + (unsigned int)(y * pixmap->w);
unsigned char *in = bitmap->buffer + (unsigned int)((pixmap->h - y - 1) * bitmap->pitch);
unsigned char bit = 0x80;
int w = pixmap->w;
while (w--)
{
*out++ = (*in & bit) ? 255 : 0;
bit >>= 1;
if (bit == 0)
{
bit = 0x80;
in++;
}
}
}
}
else
{
for (y = 0; y < pixmap->h; y++)
{
memcpy(pixmap->samples + (unsigned int)(y * pixmap->w),
bitmap->buffer + (unsigned int)((pixmap->h - y - 1) * bitmap->pitch),
pixmap->w);
}
}
return pixmap;
}
/* The glyph cache lock is always taken when this is called. */
fz_pixmap *
fz_render_ft_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, int aa)
{
FT_Face face = font->ft_face;
FT_Matrix m;
FT_Vector v;
FT_Error fterr;
fz_pixmap *result;
float strength = fz_matrix_expansion(trm) * 0.02f;
trm = fz_adjust_ft_glyph_width(ctx, font, gid, trm);
if (font->ft_italic)
trm = fz_concat(fz_shear(SHEAR, 0), trm);
/*
Freetype mutilates complex glyphs if they are loaded
with FT_Set_Char_Size 1.0. it rounds the coordinates
before applying transformation. to get more precision in
freetype, we shift part of the scale in the matrix
into FT_Set_Char_Size instead
*/
m.xx = trm.a * 64; /* should be 65536 */
m.yx = trm.b * 64;
m.xy = trm.c * 64;
m.yy = trm.d * 64;
v.x = trm.e * 64;
v.y = trm.f * 64;
fz_lock(ctx, FZ_LOCK_FREETYPE);
fterr = FT_Set_Char_Size(face, 65536, 65536, 72, 72); /* should be 64, 64 */
if (fterr)
fz_warn(ctx, "freetype setting character size: %s", ft_error_string(fterr));
FT_Set_Transform(face, &m, &v);
if (aa == 0)
{
/* enable grid fitting for non-antialiased rendering */
float scale = fz_matrix_expansion(trm);
m.xx = trm.a * 65536 / scale;
m.xy = trm.b * 65536 / scale;
m.yx = trm.c * 65536 / scale;
m.yy = trm.d * 65536 / scale;
v.x = 0;
v.y = 0;
fterr = FT_Set_Char_Size(face, 64 * scale, 64 * scale, 72, 72);
if (fterr)
fz_warn(ctx, "freetype setting character size: %s", ft_error_string(fterr));
FT_Set_Transform(face, &m, &v);
fterr = FT_Load_Glyph(face, gid, FT_LOAD_NO_BITMAP | FT_LOAD_TARGET_MONO);
if (fterr) {
fz_warn(ctx, "freetype load hinted glyph (gid %d): %s", gid, ft_error_string(fterr));
goto retry_unhinted;
}
}
else if (font->ft_hint)
{
/*
Enable hinting, but keep the huge char size so that
it is hinted for a character. This will in effect nullify
the effect of grid fitting. This form of hinting should
only be used for DynaLab and similar tricky TrueType fonts,
so that we get the correct outline shape.
*/
fterr = FT_Load_Glyph(face, gid, FT_LOAD_NO_BITMAP);
if (fterr) {
fz_warn(ctx, "freetype load hinted glyph (gid %d): %s", gid, ft_error_string(fterr));
goto retry_unhinted;
}
}
else
{
retry_unhinted:
fterr = FT_Load_Glyph(face, gid, FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING);
if (fterr)
{
fz_warn(ctx, "freetype load glyph (gid %d): %s", gid, ft_error_string(fterr));
fz_unlock(ctx, FZ_LOCK_FREETYPE);
return NULL;
}
}
if (font->ft_bold)
{
FT_Outline_Embolden(&face->glyph->outline, strength * 64);
FT_Outline_Translate(&face->glyph->outline, -strength * 32, -strength * 32);
}
fterr = FT_Render_Glyph(face->glyph, fz_aa_level(ctx) > 0 ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
if (fterr)
{
fz_warn(ctx, "freetype render glyph (gid %d): %s", gid, ft_error_string(fterr));
fz_unlock(ctx, FZ_LOCK_FREETYPE);
return NULL;
}
result = fz_copy_ft_bitmap(ctx, face->glyph->bitmap_left, face->glyph->bitmap_top, &face->glyph->bitmap);
fz_unlock(ctx, FZ_LOCK_FREETYPE);
return result;
}
fz_pixmap *
fz_render_ft_stroked_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, fz_matrix ctm, fz_stroke_state *state)
{
FT_Face face = font->ft_face;
float expansion = fz_matrix_expansion(ctm);
int linewidth = state->linewidth * expansion * 64 / 2;
FT_Matrix m;
FT_Vector v;
FT_Error fterr;
FT_Stroker stroker;
FT_Glyph glyph;
FT_BitmapGlyph bitmap;
fz_pixmap *pixmap;
FT_Stroker_LineJoin line_join;
trm = fz_adjust_ft_glyph_width(ctx, font, gid, trm);
if (font->ft_italic)
trm = fz_concat(fz_shear(SHEAR, 0), trm);
m.xx = trm.a * 64; /* should be 65536 */
m.yx = trm.b * 64;
m.xy = trm.c * 64;
m.yy = trm.d * 64;
v.x = trm.e * 64;
v.y = trm.f * 64;
fz_lock(ctx, FZ_LOCK_FREETYPE);
fterr = FT_Set_Char_Size(face, 65536, 65536, 72, 72); /* should be 64, 64 */
if (fterr)
{
fz_warn(ctx, "FT_Set_Char_Size: %s", ft_error_string(fterr));
fz_unlock(ctx, FZ_LOCK_FREETYPE);
return NULL;
}
FT_Set_Transform(face, &m, &v);
fterr = FT_Load_Glyph(face, gid, FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING);
if (fterr)
{
fz_warn(ctx, "FT_Load_Glyph(gid %d): %s", gid, ft_error_string(fterr));
fz_unlock(ctx, FZ_LOCK_FREETYPE);
return NULL;
}
fterr = FT_Stroker_New(ctx->font->ftlib, &stroker);
if (fterr)
{
fz_warn(ctx, "FT_Stroker_New: %s", ft_error_string(fterr));
fz_unlock(ctx, FZ_LOCK_FREETYPE);
return NULL;
}
#if FREETYPE_MAJOR * 10000 + FREETYPE_MINOR * 100 + FREETYPE_PATCH > 20405
/* New freetype */
line_join =
state->linejoin == FZ_LINEJOIN_MITER ? FT_STROKER_LINEJOIN_MITER_FIXED :
state->linejoin == FZ_LINEJOIN_ROUND ? FT_STROKER_LINEJOIN_ROUND :
state->linejoin == FZ_LINEJOIN_BEVEL ? FT_STROKER_LINEJOIN_BEVEL :
FT_STROKER_LINEJOIN_MITER_VARIABLE;
#else
/* Old freetype */
line_join =
state->linejoin == FZ_LINEJOIN_MITER ? FT_STROKER_LINEJOIN_MITER :
state->linejoin == FZ_LINEJOIN_ROUND ? FT_STROKER_LINEJOIN_ROUND :
state->linejoin == FZ_LINEJOIN_BEVEL ? FT_STROKER_LINEJOIN_BEVEL :
FT_STROKER_LINEJOIN_MITER;
#endif
FT_Stroker_Set(stroker, linewidth, (FT_Stroker_LineCap)state->start_cap, line_join, state->miterlimit * 65536);
fterr = FT_Get_Glyph(face->glyph, &glyph);
if (fterr)
{
fz_warn(ctx, "FT_Get_Glyph: %s", ft_error_string(fterr));
FT_Stroker_Done(stroker);
fz_unlock(ctx, FZ_LOCK_FREETYPE);
return NULL;
}
fterr = FT_Glyph_Stroke(&glyph, stroker, 1);
if (fterr)
{
fz_warn(ctx, "FT_Glyph_Stroke: %s", ft_error_string(fterr));
FT_Done_Glyph(glyph);
FT_Stroker_Done(stroker);
fz_unlock(ctx, FZ_LOCK_FREETYPE);
return NULL;
}
FT_Stroker_Done(stroker);
fterr = FT_Glyph_To_Bitmap(&glyph, fz_aa_level(ctx) > 0 ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, 0, 1);
if (fterr)
{
fz_warn(ctx, "FT_Glyph_To_Bitmap: %s", ft_error_string(fterr));
FT_Done_Glyph(glyph);
fz_unlock(ctx, FZ_LOCK_FREETYPE);
return NULL;
}
bitmap = (FT_BitmapGlyph)glyph;
pixmap = fz_copy_ft_bitmap(ctx, bitmap->left, bitmap->top, &bitmap->bitmap);
FT_Done_Glyph(glyph);
fz_unlock(ctx, FZ_LOCK_FREETYPE);
return pixmap;
}
static fz_rect
fz_bound_ft_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm)
{
FT_Face face = font->ft_face;
FT_Error fterr;
FT_BBox cbox;
FT_Matrix m;
FT_Vector v;
fz_rect bounds;
// TODO: refactor loading into fz_load_ft_glyph
// TODO: cache results
float strength = fz_matrix_expansion(trm) * 0.02f;
trm = fz_adjust_ft_glyph_width(ctx, font, gid, trm);
if (font->ft_italic)
trm = fz_concat(fz_shear(SHEAR, 0), trm);
m.xx = trm.a * 64; /* should be 65536 */
m.yx = trm.b * 64;
m.xy = trm.c * 64;
m.yy = trm.d * 64;
v.x = trm.e * 64;
v.y = trm.f * 64;
fz_lock(ctx, FZ_LOCK_FREETYPE);
fterr = FT_Set_Char_Size(face, 65536, 65536, 72, 72); /* should be 64, 64 */
if (fterr)
fz_warn(ctx, "freetype setting character size: %s", ft_error_string(fterr));
FT_Set_Transform(face, &m, &v);
fterr = FT_Load_Glyph(face, gid, FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING);
if (fterr)
{
fz_warn(ctx, "freetype load glyph (gid %d): %s", gid, ft_error_string(fterr));
fz_unlock(ctx, FZ_LOCK_FREETYPE);
bounds.x0 = bounds.x1 = trm.e;
bounds.y0 = bounds.y1 = trm.f;
return bounds;
}
if (font->ft_bold)
{
FT_Outline_Embolden(&face->glyph->outline, strength * 64);
FT_Outline_Translate(&face->glyph->outline, -strength * 32, -strength * 32);
}
FT_Outline_Get_CBox(&face->glyph->outline, &cbox);
fz_unlock(ctx, FZ_LOCK_FREETYPE);
bounds.x0 = cbox.xMin / 64.0f;
bounds.y0 = cbox.yMin / 64.0f;
bounds.x1 = cbox.xMax / 64.0f;
bounds.y1 = cbox.yMax / 64.0f;
if (fz_is_empty_rect(bounds))
{
bounds.x0 = bounds.x1 = trm.e;
bounds.y0 = bounds.y1 = trm.f;
}
return bounds;
}
/* Turn FT_Outline into a fz_path */
struct closure {
fz_context *ctx;
fz_path *path;
float x, y;
};
static int move_to(const FT_Vector *p, void *cc)
{
fz_context *ctx = ((struct closure *)cc)->ctx;
fz_path *path = ((struct closure *)cc)->path;
float tx = ((struct closure *)cc)->x;
float ty = ((struct closure *)cc)->y;
fz_moveto(ctx, path, tx + p->x / 64.0f, ty + p->y / 64.0f);
return 0;
}
static int line_to(const FT_Vector *p, void *cc)
{
fz_context *ctx = ((struct closure *)cc)->ctx;
fz_path *path = ((struct closure *)cc)->path;
float tx = ((struct closure *)cc)->x;
float ty = ((struct closure *)cc)->y;
fz_lineto(ctx, path, tx + p->x / 64.0f, ty + p->y / 64.0f);
return 0;
}
static int conic_to(const FT_Vector *c, const FT_Vector *p, void *cc)
{
fz_context *ctx = ((struct closure *)cc)->ctx;
fz_path *path = ((struct closure *)cc)->path;
float tx = ((struct closure *)cc)->x;
float ty = ((struct closure *)cc)->y;
fz_point s, c1, c2;
float cx = tx + c->x / 64.0f, cy = ty + c->y / 64.0f;
float px = tx + p->x / 64.0f, py = ty + p->y / 64.0f;
s = fz_currentpoint(ctx, path);
c1.x = (s.x + cx * 2) / 3;
c1.y = (s.y + cy * 2) / 3;
c2.x = (px + cx * 2) / 3;
c2.y = (py + cy * 2) / 3;
fz_curveto(ctx, path, c1.x, c1.y, c2.x, c2.y, px, py);
return 0;
}
static int cubic_to(const FT_Vector *c1, const FT_Vector *c2, const FT_Vector *p, void *cc)
{
fz_context *ctx = ((struct closure *)cc)->ctx;
fz_path *path = ((struct closure *)cc)->path;
float tx = ((struct closure *)cc)->x;
float ty = ((struct closure *)cc)->y;
fz_curveto(ctx, path,
tx + c1->x/64.0f, ty + c1->y/64.0f,
tx + c2->x/64.0f, ty + c2->y/64.0f,
tx + p->x/64.0f, ty + p->y/64.0f);
return 0;
}
static const FT_Outline_Funcs outline_funcs = {
move_to, line_to, conic_to, cubic_to, 0, 0
};
fz_path *
fz_outline_ft_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm)
{
struct closure cc;
FT_Face face = font->ft_face;
FT_Matrix m;
FT_Vector v;
int fterr;
float strength = fz_matrix_expansion(trm) * 0.02f;
trm = fz_adjust_ft_glyph_width(ctx, font, gid, trm);
if (font->ft_italic)
trm = fz_concat(fz_shear(SHEAR, 0), trm);
m.xx = trm.a * 64; /* should be 65536 */
m.yx = trm.b * 64;
m.xy = trm.c * 64;
m.yy = trm.d * 64;
v.x = 0;
v.y = 0;
fz_lock(ctx, FZ_LOCK_FREETYPE);
fterr = FT_Set_Char_Size(face, 65536, 65536, 72, 72); /* should be 64, 64 */
if (fterr)
fz_warn(ctx, "freetype setting character size: %s", ft_error_string(fterr));
FT_Set_Transform(face, &m, &v);
fterr = FT_Load_Glyph(face, gid, FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING);
if (fterr)
{
fz_warn(ctx, "freetype load glyph (gid %d): %s", gid, ft_error_string(fterr));
fz_unlock(ctx, FZ_LOCK_FREETYPE);
return NULL;
}
if (font->ft_bold)
{
FT_Outline_Embolden(&face->glyph->outline, strength * 64);
FT_Outline_Translate(&face->glyph->outline, -strength * 32, -strength * 32);
}
fz_try(ctx)
{
cc.ctx = ctx;
cc.path = fz_new_path(ctx);
cc.x = trm.e;
cc.y = trm.f;
fz_moveto(ctx, cc.path, cc.x, cc.y);
FT_Outline_Decompose(&face->glyph->outline, &outline_funcs, &cc);
fz_closepath(ctx, cc.path);
}
fz_catch(ctx)
{
fz_warn(ctx, "freetype cannot decompose outline");
fz_free(ctx, cc.path);
fz_unlock(ctx, FZ_LOCK_FREETYPE);
return NULL;
}
fz_unlock(ctx, FZ_LOCK_FREETYPE);
return cc.path;
}
/*
* Type 3 fonts...
*/
fz_font *
fz_new_type3_font(fz_context *ctx, char *name, fz_matrix matrix)
{
fz_font *font;
int i;
font = fz_new_font(ctx, name, 1, 256);
font->t3procs = fz_malloc_array(ctx, 256, sizeof(fz_buffer*));
font->t3lists = fz_malloc_array(ctx, 256, sizeof(fz_display_list*));
font->t3widths = fz_malloc_array(ctx, 256, sizeof(float));
font->t3flags = fz_malloc_array(ctx, 256, sizeof(char));
font->t3matrix = matrix;
for (i = 0; i < 256; i++)
{
font->t3procs[i] = NULL;
font->t3lists[i] = NULL;
font->t3widths[i] = 0;
font->t3flags[i] = 0;
}
return font;
}
void
fz_prepare_t3_glyph(fz_context *ctx, fz_font *font, int gid)
{
fz_buffer *contents;
fz_device *dev;
contents = font->t3procs[gid];
if (!contents)
return;
font->t3lists[gid] = fz_new_display_list(ctx);
dev = fz_new_list_device(ctx, font->t3lists[gid]);
dev->flags = FZ_DEVFLAG_FILLCOLOR_UNDEFINED |
FZ_DEVFLAG_STROKECOLOR_UNDEFINED |
FZ_DEVFLAG_STARTCAP_UNDEFINED |
FZ_DEVFLAG_DASHCAP_UNDEFINED |
FZ_DEVFLAG_ENDCAP_UNDEFINED |
FZ_DEVFLAG_LINEJOIN_UNDEFINED |
FZ_DEVFLAG_MITERLIMIT_UNDEFINED |
FZ_DEVFLAG_LINEWIDTH_UNDEFINED;
font->t3run(font->t3doc, font->t3resources, contents, dev, fz_identity, NULL);
font->t3flags[gid] = dev->flags;
fz_free_device(dev);
}
static fz_rect
fz_bound_t3_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm)
{
fz_display_list *list;
fz_matrix ctm;
fz_rect bounds;
fz_bbox bbox;
fz_device *dev;
list = font->t3lists[gid];
if (!list)
return fz_transform_rect(trm, fz_empty_rect);
ctm = fz_concat(font->t3matrix, trm);
dev = fz_new_bbox_device(ctx, &bbox);
fz_run_display_list(list, dev, ctm, fz_infinite_bbox, NULL);
fz_free_device(dev);
bounds.x0 = bbox.x0;
bounds.y0 = bbox.y0;
bounds.x1 = bbox.x1;
bounds.y1 = bbox.y1;
return bounds;
}
fz_pixmap *
fz_render_t3_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, fz_colorspace *model, fz_bbox scissor)
{
fz_display_list *list;
fz_matrix ctm;
fz_bbox bbox;
fz_device *dev;
fz_pixmap *glyph;
fz_pixmap *result;
if (gid < 0 || gid > 255)
return NULL;
list = font->t3lists[gid];
if (!list)
return NULL;
if (font->t3flags[gid] & FZ_DEVFLAG_MASK)
{
if (font->t3flags[gid] & FZ_DEVFLAG_COLOR)
fz_warn(ctx, "type3 glyph claims to be both masked and colored");
model = NULL;
}
else if (font->t3flags[gid] & FZ_DEVFLAG_COLOR)
{
if (!model)
fz_warn(ctx, "colored type3 glyph wanted in masked context");
}
else
{
fz_warn(ctx, "type3 glyph doesn't specify masked or colored");
model = NULL; /* Treat as masked */
}
bbox = fz_bbox_covering_rect(fz_bound_glyph(ctx, font, gid, trm));
bbox.x0--;
bbox.y0--;
bbox.x1++;
bbox.y1++;
bbox = fz_intersect_bbox(bbox, scissor);
glyph = fz_new_pixmap_with_bbox(ctx, model ? model : fz_device_gray, bbox);
fz_clear_pixmap(ctx, glyph);
ctm = fz_concat(font->t3matrix, trm);
dev = fz_new_draw_device_type3(ctx, glyph);
fz_run_display_list(list, dev, ctm, fz_infinite_bbox, NULL);
fz_free_device(dev);
if (!model)
{
result = fz_alpha_from_gray(ctx, glyph, 0);
fz_drop_pixmap(ctx, glyph);
}
else
result = glyph;
return result;
}
void
fz_render_t3_glyph_direct(fz_context *ctx, fz_device *dev, fz_font *font, int gid, fz_matrix trm, void *gstate)
{
fz_matrix ctm;
void *contents;
if (gid < 0 || gid > 255)
return;
contents = font->t3procs[gid];
if (!contents)
return;
if (font->t3flags[gid] & FZ_DEVFLAG_MASK)
{
if (font->t3flags[gid] & FZ_DEVFLAG_COLOR)
fz_warn(ctx, "type3 glyph claims to be both masked and colored");
}
else if (font->t3flags[gid] & FZ_DEVFLAG_COLOR)
{
}
else
{
fz_warn(ctx, "type3 glyph doesn't specify masked or colored");
}
ctm = fz_concat(font->t3matrix, trm);
font->t3run(font->t3doc, font->t3resources, contents, dev, ctm, gstate);
}
#ifndef NDEBUG
void
fz_print_font(fz_context *ctx, FILE *out, fz_font *font)
{
fprintf(out, "font '%s' {\n", font->name);
if (font->ft_face)
{
fprintf(out, "\tfreetype face %p\n", font->ft_face);
if (font->ft_substitute)
fprintf(out, "\tsubstitute font\n");
}
if (font->t3procs)
{
fprintf(out, "\ttype3 matrix [%g %g %g %g]\n",
font->t3matrix.a, font->t3matrix.b,
font->t3matrix.c, font->t3matrix.d);
fprintf(out, "\ttype3 bbox [%g %g %g %g]\n",
font->bbox.x0, font->bbox.y0,
font->bbox.x1, font->bbox.y1);
}
fprintf(out, "}\n");
}
#endif
fz_rect
fz_bound_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm)
{
if (font->bbox_table && gid < font->bbox_count)
{
if (fz_is_infinite_rect(font->bbox_table[gid]))
{
if (font->ft_face)
font->bbox_table[gid] = fz_bound_ft_glyph(ctx, font, gid, fz_identity);
else if (font->t3lists)
font->bbox_table[gid] = fz_bound_t3_glyph(ctx, font, gid, fz_identity);
else
font->bbox_table[gid] = fz_empty_rect;
}
return fz_transform_rect(trm, font->bbox_table[gid]);
}
/* fall back to font bbox */
return fz_transform_rect(trm, font->bbox);
}
fz_path *
fz_outline_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix ctm)
{
if (!font->ft_face)
{
fz_warn(ctx, "cannot convert type3 glyph to path");
return NULL;
}
return fz_outline_ft_glyph(ctx, font, gid, ctm);
}
int fz_glyph_cacheable(fz_context *ctx, fz_font *font, int gid)
{
if (!font->t3procs || !font->t3flags || gid < 0 || gid >= font->bbox_count)
return 1;
return (font->t3flags[gid] & FZ_DEVFLAG_UNCACHEABLE) == 0;
}
|