-
Notifications
You must be signed in to change notification settings - Fork 3
/
bdfgrid.c
3402 lines (2987 loc) · 103 KB
/
bdfgrid.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
/*
* Copyright 2008 Department of Mathematical Sciences, New Mexico State University
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* DEPARTMENT OF MATHEMATICAL SCIENCES OR NEW MEXICO STATE UNIVERSITY BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "bdfP.h"
#ifndef MYABS
#define MYABS(n) ((n) < 0 ? -(n) : (n))
#endif
#undef MAX
#define MAX(h, i) ((h) > (i) ? (h) : (i))
#undef MIN
#define MIN(l, o) ((l) < (o) ? (l) : (o))
double _bdf_cos_tbl[360] = {
0.000000, 0.999848, 0.999391, 0.998630, 0.997564, 0.996195,
0.994522, 0.992546, 0.990268, 0.987688, 0.984808, 0.981627,
0.978148, 0.974370, 0.970296, 0.965926, 0.961262, 0.956305,
0.951057, 0.945519, 0.939693, 0.933580, 0.927184, 0.920505,
0.913545, 0.906308, 0.898794, 0.891007, 0.882948, 0.874620,
0.866025, 0.857167, 0.848048, 0.838671, 0.829038, 0.819152,
0.809017, 0.798636, 0.788011, 0.777146, 0.766044, 0.754710,
0.743145, 0.731354, 0.719340, 0.707107, 0.694658, 0.681998,
0.669131, 0.656059, 0.642788, 0.629320, 0.615661, 0.601815,
0.587785, 0.573576, 0.559193, 0.544639, 0.529919, 0.515038,
0.500000, 0.484810, 0.469472, 0.453990, 0.438371, 0.422618,
0.406737, 0.390731, 0.374607, 0.358368, 0.342020, 0.325568,
0.309017, 0.292372, 0.275637, 0.258819, 0.241922, 0.224951,
0.207912, 0.190809, 0.173648, 0.156434, 0.139173, 0.121869,
0.104528, 0.087156, 0.069756, 0.052336, 0.034899, 0.017452,
0.000000, -0.017452, -0.034899, -0.052336, -0.069756, -0.087156,
-0.104528, -0.121869, -0.139173, -0.156434, -0.173648, -0.190809,
-0.207912, -0.224951, -0.241922, -0.258819, -0.275637, -0.292372,
-0.309017, -0.325568, -0.342020, -0.358368, -0.374607, -0.390731,
-0.406737, -0.422618, -0.438371, -0.453990, -0.469472, -0.484810,
-0.500000, -0.515038, -0.529919, -0.544639, -0.559193, -0.573576,
-0.587785, -0.601815, -0.615661, -0.629320, -0.642788, -0.656059,
-0.669131, -0.681998, -0.694658, -0.707107, -0.719340, -0.731354,
-0.743145, -0.754710, -0.766044, -0.777146, -0.788011, -0.798636,
-0.809017, -0.819152, -0.829038, -0.838671, -0.848048, -0.857167,
-0.866025, -0.874620, -0.882948, -0.891007, -0.898794, -0.906308,
-0.913545, -0.920505, -0.927184, -0.933580, -0.939693, -0.945519,
-0.951057, -0.956305, -0.961262, -0.965926, -0.970296, -0.974370,
-0.978148, -0.981627, -0.984808, -0.987688, -0.990268, -0.992546,
-0.994522, -0.996195, -0.997564, -0.998630, -0.999391, -0.999848,
-1.000000, -0.999848, -0.999391, -0.998630, -0.997564, -0.996195,
-0.994522, -0.992546, -0.990268, -0.987688, -0.984808, -0.981627,
-0.978148, -0.974370, -0.970296, -0.965926, -0.961262, -0.956305,
-0.951057, -0.945519, -0.939693, -0.933580, -0.927184, -0.920505,
-0.913545, -0.906308, -0.898794, -0.891007, -0.882948, -0.874620,
-0.866025, -0.857167, -0.848048, -0.838671, -0.829038, -0.819152,
-0.809017, -0.798636, -0.788011, -0.777146, -0.766044, -0.754710,
-0.743145, -0.731354, -0.719340, -0.707107, -0.694658, -0.681998,
-0.669131, -0.656059, -0.642788, -0.629320, -0.615661, -0.601815,
-0.587785, -0.573576, -0.559193, -0.544639, -0.529919, -0.515038,
-0.500000, -0.484810, -0.469472, -0.453990, -0.438371, -0.422618,
-0.406737, -0.390731, -0.374607, -0.358368, -0.342020, -0.325568,
-0.309017, -0.292372, -0.275637, -0.258819, -0.241922, -0.224951,
-0.207912, -0.190809, -0.173648, -0.156434, -0.139173, -0.121869,
-0.104528, -0.087156, -0.069756, -0.052336, -0.034899, -0.017452,
-0.000000, 0.017452, 0.034899, 0.052336, 0.069756, 0.087156,
0.104528, 0.121869, 0.139173, 0.156434, 0.173648, 0.190809,
0.207912, 0.224951, 0.241922, 0.258819, 0.275637, 0.292372,
0.309017, 0.325568, 0.342020, 0.358368, 0.374607, 0.390731,
0.406737, 0.422618, 0.438371, 0.453990, 0.469472, 0.484810,
0.500000, 0.515038, 0.529919, 0.544639, 0.559193, 0.573576,
0.587785, 0.601815, 0.615661, 0.629320, 0.642788, 0.656059,
0.669131, 0.681998, 0.694658, 0.707107, 0.719340, 0.731354,
0.743145, 0.754710, 0.766044, 0.777146, 0.788011, 0.798636,
0.809017, 0.819152, 0.829038, 0.838671, 0.848048, 0.857167,
0.866025, 0.874620, 0.882948, 0.891007, 0.898794, 0.906308,
0.913545, 0.920505, 0.927184, 0.933580, 0.939693, 0.945519,
0.951057, 0.956305, 0.961262, 0.965926, 0.970296, 0.974370,
0.978148, 0.981627, 0.984808, 0.987688, 0.990268, 0.992546,
0.994522, 0.996195, 0.997564, 0.998630, 0.999391, 0.999848,
};
double _bdf_sin_tbl[360] = {
0.000000, 0.017452, 0.034899, 0.052336, 0.069756, 0.087156,
0.104528, 0.121869, 0.139173, 0.156434, 0.173648, 0.190809,
0.207912, 0.224951, 0.241922, 0.258819, 0.275637, 0.292372,
0.309017, 0.325568, 0.342020, 0.358368, 0.374607, 0.390731,
0.406737, 0.422618, 0.438371, 0.453990, 0.469472, 0.484810,
0.500000, 0.515038, 0.529919, 0.544639, 0.559193, 0.573576,
0.587785, 0.601815, 0.615661, 0.629320, 0.642788, 0.656059,
0.669131, 0.681998, 0.694658, 0.707107, 0.719340, 0.731354,
0.743145, 0.754710, 0.766044, 0.777146, 0.788011, 0.798636,
0.809017, 0.819152, 0.829038, 0.838671, 0.848048, 0.857167,
0.866025, 0.874620, 0.882948, 0.891007, 0.898794, 0.906308,
0.913545, 0.920505, 0.927184, 0.933580, 0.939693, 0.945519,
0.951057, 0.956305, 0.961262, 0.965926, 0.970296, 0.974370,
0.978148, 0.981627, 0.984808, 0.987688, 0.990268, 0.992546,
0.994522, 0.996195, 0.997564, 0.998630, 0.999391, 0.999848,
1.000000, 0.999848, 0.999391, 0.998630, 0.997564, 0.996195,
0.994522, 0.992546, 0.990268, 0.987688, 0.984808, 0.981627,
0.978148, 0.974370, 0.970296, 0.965926, 0.961262, 0.956305,
0.951057, 0.945519, 0.939693, 0.933580, 0.927184, 0.920505,
0.913545, 0.906308, 0.898794, 0.891007, 0.882948, 0.874620,
0.866025, 0.857167, 0.848048, 0.838671, 0.829038, 0.819152,
0.809017, 0.798636, 0.788011, 0.777146, 0.766044, 0.754710,
0.743145, 0.731354, 0.719340, 0.707107, 0.694658, 0.681998,
0.669131, 0.656059, 0.642788, 0.629320, 0.615661, 0.601815,
0.587785, 0.573576, 0.559193, 0.544639, 0.529919, 0.515038,
0.500000, 0.484810, 0.469472, 0.453990, 0.438371, 0.422618,
0.406737, 0.390731, 0.374607, 0.358368, 0.342020, 0.325568,
0.309017, 0.292372, 0.275637, 0.258819, 0.241922, 0.224951,
0.207912, 0.190809, 0.173648, 0.156434, 0.139173, 0.121869,
0.104528, 0.087156, 0.069756, 0.052336, 0.034899, 0.017452,
0.000000, -0.017452, -0.034899, -0.052336, -0.069756, -0.087156,
-0.104528, -0.121869, -0.139173, -0.156434, -0.173648, -0.190809,
-0.207912, -0.224951, -0.241922, -0.258819, -0.275637, -0.292372,
-0.309017, -0.325568, -0.342020, -0.358368, -0.374607, -0.390731,
-0.406737, -0.422618, -0.438371, -0.453990, -0.469472, -0.484810,
-0.500000, -0.515038, -0.529919, -0.544639, -0.559193, -0.573576,
-0.587785, -0.601815, -0.615661, -0.629320, -0.642788, -0.656059,
-0.669131, -0.681998, -0.694658, -0.707107, -0.719340, -0.731354,
-0.743145, -0.754710, -0.766044, -0.777146, -0.788011, -0.798636,
-0.809017, -0.819152, -0.829038, -0.838671, -0.848048, -0.857167,
-0.866025, -0.874620, -0.882948, -0.891007, -0.898794, -0.906308,
-0.913545, -0.920505, -0.927184, -0.933580, -0.939693, -0.945519,
-0.951057, -0.956305, -0.961262, -0.965926, -0.970296, -0.974370,
-0.978148, -0.981627, -0.984808, -0.987688, -0.990268, -0.992546,
-0.994522, -0.996195, -0.997564, -0.998630, -0.999391, -0.999848,
-1.000000, -0.999848, -0.999391, -0.998630, -0.997564, -0.996195,
-0.994522, -0.992546, -0.990268, -0.987688, -0.984808, -0.981627,
-0.978148, -0.974370, -0.970296, -0.965926, -0.961262, -0.956305,
-0.951057, -0.945519, -0.939693, -0.933580, -0.927184, -0.920505,
-0.913545, -0.906308, -0.898794, -0.891007, -0.882948, -0.874620,
-0.866025, -0.857167, -0.848048, -0.838671, -0.829038, -0.819152,
-0.809017, -0.798636, -0.788011, -0.777146, -0.766044, -0.754710,
-0.743145, -0.731354, -0.719340, -0.707107, -0.694658, -0.681998,
-0.669131, -0.656059, -0.642788, -0.629320, -0.615661, -0.601815,
-0.587785, -0.573576, -0.559193, -0.544639, -0.529919, -0.515038,
-0.500000, -0.484810, -0.469472, -0.453990, -0.438371, -0.422618,
-0.406737, -0.390731, -0.374607, -0.358368, -0.342020, -0.325568,
-0.309017, -0.292372, -0.275637, -0.258819, -0.241922, -0.224951,
-0.207912, -0.190809, -0.173648, -0.156434, -0.139173, -0.121869,
-0.104528, -0.087156, -0.069756, -0.052336, -0.034899, -0.017452,
};
double _bdf_tan_tbl[90] = {
0.000000, 0.017455, 0.034921, 0.052408, 0.069927, 0.087489,
0.105104, 0.122785, 0.140541, 0.158384, 0.176327, 0.194380,
0.212557, 0.230868, 0.249328, 0.267949, 0.286745, 0.305731,
0.324920, 0.344328, 0.363970, 0.383864, 0.404026, 0.424475,
0.445229, 0.466308, 0.487733, 0.509525, 0.531709, 0.554309,
0.577350, 0.600861, 0.624869, 0.649408, 0.674509, 0.700208,
0.726543, 0.753554, 0.781286, 0.809784, 0.839100, 0.869287,
0.900404, 0.932515, 0.965689, 1.000000, 1.035530, 1.072369,
1.110613, 1.150368, 1.191754, 1.234897, 1.279942, 1.327045,
1.376382, 1.428148, 1.482561, 1.539865, 1.600335, 1.664279,
1.732051, 1.804048, 1.880726, 1.962611, 2.050304, 2.144507,
2.246037, 2.355852, 2.475087, 2.605089, 2.747477, 2.904211,
3.077684, 3.270853, 3.487414, 3.732051, 4.010781, 4.331476,
4.704630, 5.144554, 5.671282, 6.313752, 7.115370, 8.144346,
9.514364, 11.430052, 14.300666, 19.081137, 28.636253, 57.289962,
};
/*
* Determine the actual ink bounds.
*/
static int
_bdf_grid_ink_bounds(bdf_glyph_grid_t *grid, short *x, short *y,
short *width, short *height)
{
short bx, by, bwd, bht, minx, maxx, miny, maxy, dx, dy;
unsigned short bpr, ink, sel, col;
unsigned char *bmap, *masks;
masks = 0;
switch (grid->bpp) {
case 1: masks = bdf_onebpp; break;
case 2: masks = bdf_twobpp; break;
case 4: masks = bdf_fourbpp; break;
case 8: masks = bdf_eightbpp; break;
}
if (grid->sel.width != 0 && grid->sel.height != 0) {
sel = 1;
bx = by = 0;
bwd = grid->sel.width;
bht = grid->sel.height;
bmap = grid->sel.bitmap;
} else {
sel = 0;
bx = grid->glyph_x;
by = grid->glyph_y;
bwd = grid->glyph_bbx.width;
bht = grid->glyph_bbx.height;
bmap = grid->bitmap;
}
maxx = maxy = 0;
minx = bx + bwd;
miny = by + bht;
bpr = ((bwd * grid->bpp) + 7) >> 3;
ink = 0;
bwd += bx;
bht += by;
for (dy = by; dy < bht; dy++) {
for (col = bx * grid->bpp, dx = bx; dx < bwd; dx++, col += grid->bpp) {
if (bmap[(dy * bpr) + (col >> 3)] & masks[(col & 7) / grid->bpp]) {
ink = 1;
minx = MIN(minx, dx);
miny = MIN(miny, dy);
maxx = MAX(maxx, dx);
maxy = MAX(maxy, dy);
}
}
}
*x = minx + ((sel) ? grid->sel.x : 0);
*y = miny + ((sel) ? grid->sel.y : 0);
if (ink == 0)
*width = *height = 0;
else {
*width = (maxx - minx) + 1;
*height = (maxy - miny) + 1;
}
return ink;
}
/**************************************************************************
*
* Glyph grid create and destroy functions.
*
**************************************************************************/
/*
* Make a glyph grid with the glyph bitmap set in the bitmap.
*/
bdf_glyph_grid_t *
bdf_make_glyph_grid(bdf_font_t *font, int code, int unencoded)
{
unsigned short si, di, col, colx, byte;
short ht, as, ds, gsize, bpr, x, y, nx, ny;
long l, r, m;
bdf_glyph_grid_t *gr;
bdf_glyph_t *gl, *glp;
bdf_property_t *p;
unsigned char *masks;
char name[24];
#if 0
if (font == 0)
return 0;
#endif
/*
* Allocate the grid and initialize it.
*/
gr = (bdf_glyph_grid_t *) malloc(sizeof(bdf_glyph_grid_t));
(void) memset((char *) gr, 0, sizeof(bdf_glyph_grid_t));
/*
* Set the encoding and the unencoded flag.
*/
gr->bpp = (font) ? font->bpp : 1;
gr->encoding = code;
gr->unencoded = unencoded;
/*
* Set the glyph grid spacing.
*/
gr->spacing = (font) ? font->spacing : BDF_CHARCELL;
/*
* Set the point size and resolutions.
*/
if (font) {
gr->point_size = font->point_size;
gr->resolution_x = font->resolution_x;
gr->resolution_y = font->resolution_y;
} else {
gr->point_size = 12;
gr->resolution_x = gr->resolution_y = 100;
}
/*
* Set the CAP_HEIGHT and X_HEIGHT if they exist in the font.
*/
if (font) {
if ((p = bdf_get_font_property(font, "CAP_HEIGHT")) != 0)
gr->cap_height = (short) p->value.int32;
if ((p = bdf_get_font_property(font, "X_HEIGHT")) != 0)
gr->x_height = (short) p->value.int32;
}
masks = 0;
switch (gr->bpp) {
case 1: masks = bdf_onebpp; break;
case 2: masks = bdf_twobpp; break;
case 4: masks = bdf_fourbpp; break;
case 8: masks = bdf_eightbpp; break;
}
/*
* Copy the font bounding box into the grid.
*/
if (font)
(void) memcpy((char *) &gr->font_bbx, (char *) &font->bbx,
sizeof(bdf_bbx_t));
else {
gr->font_bbx.height = 17;
gr->font_bbx.width = 8;
gr->font_bbx.descent = 8;
gr->font_bbx.ascent = 9;
gr->font_bbx.y_offset = -8;
}
if (font) {
if (unencoded) {
gl = font->unencoded;
r = font->unencoded_used;
} else {
gl = font->glyphs;
r = font->glyphs_used;
}
} else {
gl = 0;
r = 0;
}
/*
* Locate the specified glyph using a simple binary search.
*/
glp = 0;
if (r > 0) {
for (l = 0; r >= l; ) {
m = (l + r) >> 1;
glp = gl + m;
if (glp->encoding == code)
break;
if (glp->encoding > code)
r = m - 1;
else if (glp->encoding < code)
l = m + 1;
glp = 0;
}
}
ht = gr->font_bbx.height;
as = gr->font_bbx.ascent;
ds = gr->font_bbx.descent;
/*
* 1. Determine width and height needed from the largest of the
* width or height.
*/
gr->grid_width = gr->grid_height =
MAX(gr->font_bbx.width, gr->font_bbx.height);
/*
* 2. Make sure the grid is at least a square of the largest of the width
* or height of the glyph itself to allow room for transformations.
*/
if (glp != 0) {
/*
* Set the glyph name and other metrics.
*/
if (glp->name) {
gr->name = (char *) malloc(strlen(glp->name) + 1);
(void) memcpy(gr->name, glp->name, strlen(glp->name) + 1);
} else {
sprintf(name, "char%d", code);
gr->name = (char *) malloc(strlen(name) + 1);
(void) memcpy(gr->name, name, strlen(name) + 1);
}
gr->dwidth = glp->dwidth;
/*
* Copy the glyph bounding box into the grid.
*/
(void) memcpy((char *) &gr->glyph_bbx, (char *) &glp->bbx,
sizeof(bdf_bbx_t));
if (glp->bbx.height < glp->bbx.ascent + glp->bbx.descent)
gsize = glp->bbx.ascent + glp->bbx.descent;
else
gsize = glp->bbx.height;
/*
* Figure the maximum of the glyph width and height.
*/
gsize = MAX(gr->glyph_bbx.width, gsize);
/*
* If either the grid width or grid height is less than the
* grid size just determined, then adjust them to the new grid size.
*/
gr->grid_width = MAX(gr->grid_width, gsize);
gr->grid_height = MAX(gr->grid_height, gsize);
} else {
/*
* The glyph doesn't exist, so make up a name for it.
*/
if (unencoded)
sprintf(name, "unencoded%d", code);
else
sprintf(name, "char%d", code);
gr->name = (char *) malloc(strlen(name) + 1);
(void) memcpy(gr->name, name, strlen(name) + 1);
}
/*
* If the font has character-cell or mono spacing, make sure the grid
* device width is set to the width stored in the font.
*/
if (gr->spacing != BDF_PROPORTIONAL)
gr->dwidth = (font) ? font->monowidth : 8;
/*
* Determine the vertical origin based on the font bounding box.
*/
if (ht >= as + ds)
gr->base_y = (((gr->grid_height >> 1) - (ht >> 1)) + ht) - ds;
else
gr->base_y = ((gr->grid_height >> 1) - ((as + ds) >> 1)) + as;
/*
* The final adjust is to check to see if the glyph positioned relative to
* the baseline would cause the grid to change size. This sometimes
* happens in fonts that have incorrect metrics.
*/
if (gr->base_y + gr->glyph_bbx.descent > gr->grid_height) {
gsize = gr->base_y + gr->glyph_bbx.descent;
gr->grid_width = MAX(gsize, gr->grid_width);
gr->grid_height = MAX(gsize, gr->grid_height);
}
/*
* Determine the horizontal origin based on the font bounding box and
* centered within the grid.
*/
gr->base_x = (gr->grid_width >> 1) - (gr->font_bbx.width >> 1);
if (gr->font_bbx.x_offset < 0)
gr->base_x += MYABS(gr->font_bbx.x_offset);
/*
* Allocate double the storage needed for the grid bitmap. The extra
* storage will be used for transformations.
*/
gr->bytes = ((((gr->grid_width * gr->bpp) + 7) >> 3) *
gr->grid_height) << 1;
gr->bitmap = (unsigned char *) malloc(gr->bytes);
(void) memset((char *) gr->bitmap, 0, gr->bytes);
/*
* Initialize the top-left coordinates of the glyph to the baseline
* coordinates.
*/
gr->glyph_x = gr->base_x;
gr->glyph_y = gr->base_y;
/*
* If the glyph was not found, simply return the empty grid.
*/
if (glp == 0)
return gr;
/*
* Determine the top-left coordinates of the glyph with respect to the
* baseline coordinates.
*/
gr->glyph_x = nx = gr->base_x + gr->glyph_bbx.x_offset;
gr->glyph_y = ny = gr->base_y - gr->glyph_bbx.ascent;
/*
* Now copy the glyph bitmap to the appropriate location in the grid.
*/
bpr = ((gr->glyph_bbx.width * gr->bpp) + 7) >> 3;
gsize = ((gr->grid_width * gr->bpp) + 7) >> 3;
for (y = 0; y < gr->glyph_bbx.height; y++, ny++) {
for (colx = nx * gr->bpp, col = x = 0; x < gr->glyph_bbx.width;
x++, col += gr->bpp, colx += gr->bpp) {
si = (col & 7) / gr->bpp;
byte = glp->bitmap[(y * bpr) + (col >> 3)] & masks[si];
if (byte) {
di = (colx & 7) / gr->bpp;
if (di < si)
byte <<= (si - di) * gr->bpp;
else if (di > si)
byte >>= (di - si) * gr->bpp;
gr->bitmap[(ny * gsize) + (colx >> 3)] |= byte;
}
}
}
/*
* Always crop the glyph to the ink bounds before editing.
*/
bdf_grid_crop(gr, 0);
/*
* Copy any Unicode mappings that might be present for this glyph, even if
* it is a proportional font. It might be changed to a character cell or
* monowidth font later.
*/
gr->unicode.map_size = glp->unicode.map_size;
gr->unicode.map_used = glp->unicode.map_used;
gr->unicode.map = (unsigned char *)
malloc(sizeof(unsigned char) * gr->unicode.map_size);
(void) memcpy((char *) gr->unicode.map, (char *) glp->unicode.map,
sizeof(unsigned char) * gr->unicode.map_used);
/*
* Return the grid.
*/
return gr;
}
void
bdf_free_glyph_grid(bdf_glyph_grid_t *grid)
{
if (grid == 0)
return;
if (grid->name != 0)
free(grid->name);
if (grid->bytes > 0)
free((char *) grid->bitmap);
if (grid->sel.bytes > 0)
free((char *) grid->sel.bitmap);
if (grid->unicode.map_size > 0)
free((char *) grid->unicode.map);
free((char *) grid);
}
/**************************************************************************
*
* Glyph grid resize functions.
*
**************************************************************************/
/*
* Enlarge the grid without affecting the font or glyph metrics.
*/
int
bdf_grid_enlarge(bdf_glyph_grid_t *grid, unsigned short width,
unsigned short height)
{
unsigned short si, di, col, colx, byte;
short ht, wd, as, ds, x, y, nx, ny;
unsigned short gwd, ght, bytes, obpr, nbpr, gsize;
unsigned char *bitmap, *masks;
if (grid == 0 || (width < grid->grid_width && height < grid->grid_height))
return 0;
masks = 0;
switch (grid->bpp) {
case 1: masks = bdf_onebpp; break;
case 2: masks = bdf_twobpp; break;
case 4: masks = bdf_fourbpp; break;
case 8: masks = bdf_eightbpp; break;
}
ht = height;
as = grid->font_bbx.ascent;
ds = grid->font_bbx.descent;
gwd = MAX(width, grid->grid_width);
ght = MAX(height, grid->grid_height);
gsize = MAX(gwd, ght);
nbpr = ((gsize * grid->bpp) + 7) >> 3;
bytes = (nbpr * ght) << 1;
bitmap = (unsigned char *) malloc(bytes);
(void) memset((char *) bitmap, 0, bytes);
/*
* Determine the new baseline.
*/
if (ht >= as + ds)
grid->base_y = (((ght >> 1) - (ht >> 1)) + ht) - ds;
else
grid->base_y = ((ght >> 1) - ((as + ds) >> 1)) + as;
grid->base_x = (gwd >> 1) - (grid->font_bbx.width >> 1);
if (grid->font_bbx.x_offset < 0)
grid->base_x += MYABS(grid->font_bbx.x_offset);
nx = grid->base_x + grid->glyph_bbx.x_offset;
ny = grid->base_y - grid->glyph_bbx.ascent;
/*
* Now copy the bitmap into the new storage base on the new metrics
* values.
*/
obpr = ((grid->grid_width * grid->bpp) + 7) >> 3;
wd = grid->glyph_x + grid->glyph_bbx.width;
ht = grid->glyph_y + grid->glyph_bbx.height;
for (y = grid->glyph_y; y < ht; y++, ny++) {
col = grid->glyph_x * grid->bpp;
colx = nx * grid->bpp;
for (x = grid->glyph_x; x < wd;
x++, col += grid->bpp, colx += grid->bpp) {
si = (col & 7) / grid->bpp;
byte = grid->bitmap[(y * obpr) + (col >> 3)] & masks[si];
if (byte) {
di = (colx & 7) / grid->bpp;
if (di < si)
byte <<= (si - di) * grid->bpp;
else if (di > si)
byte >>= (di - si) * grid->bpp;
bitmap[(ny * nbpr) + (colx >> 3)] |= byte;
}
}
}
/*
* Adjust the glyph coordinates.
*/
grid->glyph_x = grid->base_x + grid->glyph_bbx.x_offset;
grid->glyph_y = grid->base_y - grid->glyph_bbx.ascent;
/*
* Get rid of the old grid bitmap and replace it with the new one.
*/
free((char *) grid->bitmap);
grid->bytes = bytes;
grid->bitmap = bitmap;
/*
* Update the new grid width and height.
*/
grid->grid_width = grid->grid_height = gsize;
/*
* Always mark the grid as being modified on a resize.
*/
grid->modified = 1;
return 1;
}
/*
* Change the font bounding box values and resize the grid bitmap if
* necessary.
*/
int
bdf_grid_resize(bdf_glyph_grid_t *grid, bdf_metrics_t *metrics)
{
int changed;
unsigned short si, di, col, colx, byte;
short ht, wd, as, ds, x, y, nx, ny;
unsigned short gwd, ght, bytes, obpr, nbpr, gsize;
unsigned char *bitmap, *masks;
changed = 0;
if (grid == 0 || metrics == 0)
return changed;
masks = 0;
switch (grid->bpp) {
case 1: masks = bdf_onebpp; break;
case 2: masks = bdf_twobpp; break;
case 4: masks = bdf_fourbpp; break;
case 8: masks = bdf_eightbpp; break;
}
/*
* Create new grid bitmaps in preparation for the various metrics changing.
*/
if (metrics->width > grid->grid_width ||
metrics->height > grid->grid_height) {
changed = 1;
ht = metrics->height;
as = metrics->ascent;
ds = metrics->descent;
gwd = MAX(metrics->width, grid->grid_width);
ght = MAX(metrics->height, grid->grid_height);
/*
* Get the larger of the two dimensions.
*/
gsize = MAX(gwd, ght);
nbpr = ((gsize * grid->bpp) + 7) >> 3;
bytes = (nbpr * gsize) << 1;
bitmap = (unsigned char *) malloc(bytes);
(void) memset((char *) bitmap, 0, bytes);
/*
* Determine the new baseline.
*/
if (ht >= as + ds)
grid->base_y = (((ght >> 1) - (ht >> 1)) + ht) - ds;
else
grid->base_y = ((ght >> 1) - ((as + ds) >> 1)) + as;
grid->base_x = (gwd >> 1) - (metrics->width >> 1);
if (metrics->x_offset < 0)
grid->base_x += MYABS(metrics->x_offset);
nx = grid->base_x + grid->glyph_bbx.x_offset;
ny = grid->base_y - grid->glyph_bbx.ascent;
/*
* Now copy the bitmap into the new storage base on the new metrics
* values.
*/
obpr = ((grid->grid_width * grid->bpp) + 7) >> 3;
wd = grid->glyph_x + grid->glyph_bbx.width;
ht = grid->glyph_y + grid->glyph_bbx.height;
for (y = grid->glyph_y; y < ht; y++, ny++) {
col = grid->glyph_x * grid->bpp;
colx = nx * grid->bpp;
for (x = grid->glyph_x; x < wd;
x++, col += grid->bpp, colx += grid->bpp) {
si = (col & 7) / grid->bpp;
byte = grid->bitmap[(y * obpr) + (col >> 3)] & masks[si];
if (byte) {
di = (colx & 7) / grid->bpp;
if (di < si)
byte <<= (si - di) * grid->bpp;
else if (di > si)
byte >>= (di - si) * grid->bpp;
bitmap[(ny * nbpr) + (colx >> 3)] |= byte;
}
}
}
/*
* Adjust the glyph coordinates.
*/
grid->glyph_x = grid->base_x + grid->glyph_bbx.x_offset;
grid->glyph_y = grid->base_y - grid->glyph_bbx.ascent;
/*
* Get rid of the old grid bitmap and replace it with the new one.
*/
free((char *) grid->bitmap);
grid->bytes = bytes;
grid->bitmap = bitmap;
/*
* Update the new grid width and height.
*/
grid->grid_width = grid->grid_height = gsize;
/*
* Copy the metrics info into the font bounding box.
*/
grid->font_bbx.width = metrics->width;
grid->font_bbx.x_offset = metrics->x_offset;
grid->font_bbx.height = metrics->height;
grid->font_bbx.ascent = metrics->ascent;
grid->font_bbx.descent = metrics->descent;
grid->font_bbx.y_offset = metrics->y_offset;
} else {
/*
* The grid does not need to resized, but the baseline must
* be recalculated and the bitmap copied again.
*/
bytes = grid->bytes >> 1;
bitmap = grid->bitmap + bytes;
(void) memset((char *) bitmap, 0, bytes);
ht = metrics->height;
as = metrics->ascent;
ds = metrics->descent;
gwd = grid->grid_width;
ght = grid->grid_height;
/*
* Determine the new baseline.
*/
if (ht >= as + ds)
grid->base_y = (((ght >> 1) - (ht >> 1)) + ht) - ds;
else
grid->base_y = ((ght >> 1) - ((as + ds) >> 1)) + as;
grid->base_x = (gwd >> 1) - (metrics->width >> 1);
if (metrics->x_offset < 0)
grid->base_x += MYABS(metrics->x_offset);
nx = grid->base_x + grid->glyph_bbx.x_offset;
ny = grid->base_y - grid->glyph_bbx.ascent;
wd = grid->glyph_x + grid->glyph_bbx.width;
ht = grid->glyph_y + grid->glyph_bbx.height;
obpr = nbpr = ((grid->grid_width * grid->bpp) + 7) >> 3;
for (y = grid->glyph_y; y < ht; y++, ny++) {
col = grid->glyph_x * grid->bpp;
colx = nx * grid->bpp;
for (x = grid->glyph_x; x < wd;
x++, col += grid->bpp, colx += grid->bpp) {
si = (col & 7) / grid->bpp;
byte = grid->bitmap[(y * obpr) + (col >> 3)] & masks[si];
if (byte) {
di = (colx & 7) / grid->bpp;
if (di < si)
byte <<= (si - di) * grid->bpp;
else if (di > si)
byte >>= (di - si) * grid->bpp;
bitmap[(ny * nbpr) + (colx >> 3)] |= byte;
}
}
}
/*
* Copy the adjusted bitmap back into the main area.
*/
(void) memcpy((char *) grid->bitmap, (char *) bitmap, bytes);
/*
* Adjust the glyph coordinates.
*/
grid->glyph_x = grid->base_x + grid->glyph_bbx.x_offset;
grid->glyph_y = grid->base_y - grid->glyph_bbx.ascent;
/*
* Copy the metrics info into the font bounding box.
*/
grid->font_bbx.width = metrics->width;
grid->font_bbx.x_offset = metrics->x_offset;
grid->font_bbx.height = metrics->height;
grid->font_bbx.ascent = metrics->ascent;
grid->font_bbx.descent = metrics->descent;
grid->font_bbx.y_offset = metrics->y_offset;
}
/*
* If the font is not proportional, make sure the device width is adjusted
* to meet the new font bounding box.
*/
if (changed && grid->spacing != BDF_PROPORTIONAL)
grid->dwidth = grid->font_bbx.width;
/*
* Always mark the grid as being modified on a resize.
*/
grid->modified = 1;
return changed;
}
int
bdf_grid_crop(bdf_glyph_grid_t *grid, int grid_modified)
{
int cropped;
short x, y, delta, maxx, minx, maxy, miny, col;
unsigned short bpr;
unsigned char *masks;
cropped = 0;
if (grid == 0)
return cropped;
masks = 0;
switch (grid->bpp) {
case 1: masks = bdf_onebpp; break;
case 2: masks = bdf_twobpp; break;
case 4: masks = bdf_fourbpp; break;
case 8: masks = bdf_eightbpp; break;
}
bpr = ((grid->grid_width * grid->bpp) + 7) >> 3;
maxx = maxy = -1;
minx = miny = grid->grid_width;
for (y = 0; y < grid->grid_height; y++) {
for (col = x = 0; x < grid->grid_width; x++, col += grid->bpp) {
if (grid->bitmap[(y * bpr) + (col >> 3)] &
masks[(col & 7) / grid->bpp]) {
minx = MIN(minx, x);
maxx = MAX(maxx, x);
miny = MIN(miny, y);
maxy = MAX(maxy, y);
}
}
}
/*
* Handle an empty bitmap as a special case.
*/
if (maxx == -1) {
/*
* If the glyph bounding box indicated something was there originally,
* then indicate that it was cropped.
*/
if (grid->glyph_bbx.width != 0 || grid->glyph_bbx.height != 0)
cropped = 1;
(void) memset((char *) &grid->glyph_bbx, 0, sizeof(bdf_bbx_t));
grid->glyph_x = grid->base_x;
grid->glyph_y = grid->base_y;
if (cropped && grid_modified)
grid->modified = 1;
return cropped;
}
/*
* Increment the max points so width and height calculations won't go
* wrong.
*/
maxx++;
maxy++;
if (minx != grid->glyph_x) {
cropped = 1;
delta = minx - grid->glyph_x;
grid->glyph_x += delta;
grid->glyph_bbx.x_offset += delta;
}
if (maxx - minx != grid->glyph_bbx.width) {
cropped = 1;
delta = (maxx - minx) - grid->glyph_bbx.width;
grid->glyph_bbx.width += delta;
}
if (miny != grid->glyph_y) {
cropped = 1;
delta = miny - grid->glyph_y;
grid->glyph_y += delta;
grid->glyph_bbx.y_offset =
grid->base_y - (grid->glyph_y + (maxy - miny));
}
if (maxy - miny != grid->glyph_bbx.height) {
cropped = 1;
delta = (maxy - miny) - grid->glyph_bbx.height;
grid->glyph_bbx.height += delta;
grid->glyph_bbx.y_offset =
grid->base_y - (grid->glyph_y + (maxy - miny));
grid->glyph_bbx.ascent =
grid->glyph_bbx.height + grid->glyph_bbx.y_offset;
grid->glyph_bbx.descent = -grid->glyph_bbx.y_offset;
}
/*
* Indicate that the grid was modified if the glyph had to be cropped.
*/
if (cropped && grid_modified)
grid->modified = 1;
return cropped;
}
/**************************************************************************
*
* Glyph grid pixel functions.
*
**************************************************************************/
int
bdf_grid_set_pixel(bdf_glyph_grid_t *grid, short x, short y, int val)
{
unsigned short si, di, dx;
int set, bpr, delta;
unsigned char *masks;
set = 0;
if (grid == 0 || x < 0 || x >= grid->grid_width ||
y < 0 || y >= grid->grid_height)
return set;
si = 0;
masks = 0;
switch (grid->bpp) {
case 1: masks = bdf_onebpp; si = 7; break;
case 2: masks = bdf_twobpp; si = 3; break;
case 4: masks = bdf_fourbpp; si = 1; break;
case 8: masks = bdf_eightbpp; si = 0; break;
}
/*
* Remove any unused bits from the value.
*/
val &= masks[si];
dx = x * grid->bpp;
di = (dx & 7) / grid->bpp;