-
Notifications
You must be signed in to change notification settings - Fork 2
/
render.c
7358 lines (7258 loc) · 359 KB
/
render.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(c) 2002-2009, John Forkosh Associates, Inc. All rights reserved.
* http://www.forkosh.com mailto: [email protected]
* --------------------------------------------------------------------------
* This file is part of mimeTeX, which is free software. You may redistribute
* and/or modify it under the terms of the GNU General Public License,
* version 3 or later, as published by the Free Software Foundation.
* MimeTeX is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY, not even the implied warranty of MERCHANTABILITY.
* See the GNU General Public License for specific details.
* By using mimeTeX, you warrant that you have read, understood and
* agreed to these terms and conditions, and that you possess the legal
* right and ability to enter into this agreement and to use mimeTeX
* in accordance with it.
* Your mimetex.zip distribution file should contain the file COPYING,
* an ascii text copy of the GNU General Public License, version 3.
* If not, point your browser to http://www.gnu.org/licenses/
* or write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*
****************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "mimetex_priv.h"
#define REVERSEGAMMA 0.5 /* for \reverse white-on-black */
/* ==========================================================================
* Function: accent_subraster ( accent, width, height, pixsz )
* Purpose: Allocate a new subraster of width x height
* (or maybe different dimensions, depending on accent),
* and draw an accent (\hat or \vec or \etc) that fills it
* --------------------------------------------------------------------------
* Arguments: accent (I) int containing either HATACCENT or VECACCENT,
* etc, indicating the type of accent desired
* width (I) int containing desired width of accent (#cols)
* height (I) int containing desired height of accent(#rows)
* pixsz (I) int containing 1 for bitmap, 8 for bytemap
* --------------------------------------------------------------------------
* Returns: ( subraster * ) ptr to newly-allocated subraster with accent,
* or NULL for any error.
* --------------------------------------------------------------------------
* Notes: o Some accents have internally-determined dimensions,
* and caller should check dimensions in returned subraster
* ======================================================================= */
/* --- entry point --- */
subraster *accent_subraster(mimetex_ctx *mctx, int accent, int width, int height, int pixsz)
{
/* --- general info --- */
raster *rp = NULL;
/* subraster returning accent */
subraster *sp = NULL;
/*free allocated raster on err*/
/* draw solid boxes */
/* line thickness */
int thickness = 1;
/* --- other working info --- */
int col0, col1, /* cols for line */
/* rows for line */
row0, row1;
subraster *accsp = NULL; /*find suitable cmex10 symbol/accent*/
/* --- info for under/overbraces, tildes, etc --- */
/*"{" for over, "}" for under, etc*/
char brace[16];
/* set true if width<0 arg passed */
int iswidthneg = 0;
/* serif for surd */
int serifwidth = 0;
/* ------------------------------------------------------------
initialization
------------------------------------------------------------ */
if (width < 0) {
/* set neg width flag */
width = (-width);
iswidthneg = 1;
}
/* ------------------------------------------------------------
outer switch() traps accents that may change caller's height,width
------------------------------------------------------------ */
switch (accent) {
default:
/* ------------------------------------------------------------
inner switch() first allocates fixed-size raster for accents that don't
------------------------------------------------------------ */
if ((rp = new_raster(mctx, width, height, pixsz)) /* allocate fixed-size raster */
!= NULL) /* and if we succeeded... */
switch (accent) { /* ...draw requested accent in it */
/* --- unrecognized request --- */
default:
/* unrecognized accent requested */
delete_raster(mctx, rp);
rp = NULL;
/* so free raster and signal error */
break;
/* --- bar request --- */
case UNDERBARACCENT:
case BARACCENT:
/*height-1;*/
thickness = 1;/* adjust thickness */
if (accent == BARACCENT) { /* bar is above expression */
/* row numbers for overbar */
row0 = row1 = max2(height - 3, 0);
line_raster(mctx, rp, row0, 0, row1, width - 1, thickness);
} /*blanks at bot*/
else { /* underbar is below expression */
/* row numbers for underbar */
row0 = row1 = min2(2, height - 1);
line_raster(mctx, rp, row0, 0, row1, width - 1, thickness);
} /*blanks at top*/
break;
/* --- dot request --- */
case DOTACCENT:
/* adjust thickness */
thickness = height - 1;
/*line_raster(mctx, rp,0,width/2,1,(width/2)+1,thickness);*//*centered dot*/
/*box*/
rule_raster(mctx, rp, 0, (width + 1 - thickness) / 2, thickness, thickness, 3);
break;
/* --- ddot request --- */
case DDOTACCENT:
/* adjust thickness */
thickness = height - 1;
/* one-third of width */
col0 = max2((width + 1) / 3 - (thickness / 2) - 1, 0);
col1 = min2((2 * width + 1) / 3 - (thickness / 2) + 1, width - thickness); /*2/3rds*/
if (col0 + thickness >= col1) { /* dots overlap */
/* try moving left dot more left */
col0 = max2(col0 - 1, 0);
col1 = min2(col1 + 1, width - thickness);
} /* and right dot right */
if (col0 + thickness >= col1) /* dots _still_ overlap */
/* so try reducing thickness */
thickness = max2(thickness - 1, 1);
/*line_raster(mctx, rp,0,col0,1,col0+1,thickness);*//*set dot at 1st third*/
/*line_raster(mctx, rp,0,col1,1,col1+1,thickness);*//*and another at 2nd*/
/*box at 1st third*/
rule_raster(mctx, rp, 0, col0, thickness, thickness, 3);
/*box at 2nd third*/
rule_raster(mctx, rp, 0, col1, thickness, thickness, 3);
break;
/* --- hat request --- */
case HATACCENT:
/*(width<=12? 2 : 3);*/
thickness = 1;/* adjust thickness */
line_raster(mctx, rp, height - 1, 0, 0, width / 2, thickness); /* / part of hat*/
/* \ part*/
line_raster(mctx, rp, 0, (width - 1) / 2, height - 1, width - 1, thickness);
break;
/* --- sqrt request --- */
case SQRTACCENT:
/* leading serif on surd */
serifwidth = SURDSERIFWIDTH(height);
/*right col of sqrt*/
col1 = SQRTWIDTH(height, (iswidthneg ? 1 : 2)) - 1;
/*col0 = (col1-serifwidth+2)/3;*/ /* midpoint col of sqrt */
/* midpoint col of sqrt */
col0 = (col1 - serifwidth + 1) / 2;
/* midpoint row of sqrt */
row0 = max2(1, ((height + 1) / 2) - 2);
/* bottom row of sqrt */
row1 = height - 1;
/*line_raster(mctx, rp,row0,0,row1,col0,thickness);*/ /*descending portion*/
line_raster(mctx, rp, row0 + serifwidth, 0, row0, serifwidth, thickness);
/* descending */
line_raster(mctx, rp, row0, serifwidth, row1, col0, thickness);
/* ascending portion */
line_raster(mctx, rp, row1, col0, 0, col1, thickness);
/*overbar of thickness 1*/
line_raster(mctx, rp, 0, col1, 0, width - 1, thickness);
break;
} /* --- end-of-inner-switch(accent) --- */
/* break from outer accent switch */
break;
/* --- underbrace, overbrace request --- */
case UNDERBRACE:
case OVERBRACE:
/* start with } brace */
if (accent == UNDERBRACE) strcpy(brace, "}");
/* start with { brace */
if (accent == OVERBRACE) strcpy(brace, "{");
if ((accsp = get_delim(mctx, brace, width, CMEX10)) /* use width for height */
!= NULL) { /* found desired brace */
/* rotate 90 degrees clockwise */
rp = rastrot(mctx, accsp->image);
delete_subraster(mctx, accsp);
} /* and free subraster "envelope" */
break;
/* --- hat request --- */
case HATACCENT:
/* start with < */
if (accent == HATACCENT) strcpy(brace, "<");
if ((accsp = get_delim(mctx, brace, width, CMEX10)) /* use width for height */
!= NULL) { /* found desired brace */
/* rotate 90 degrees clockwise */
rp = rastrot(mctx, accsp->image);
delete_subraster(mctx, accsp);
} /* and free subraster "envelope" */
break;
/* --- vec request --- */
case VECACCENT:
/* force height odd */
height = 2 * (height / 2) + 1;
if ((accsp = arrow_subraster(mctx, width, height, pixsz, 1, 0)) /*build rightarrow*/
!= NULL) { /* succeeded */
/* "extract" raster with bitmap */
rp = accsp->image;
free((void *)accsp);
} /* and free subraster "envelope" */
break;
/* --- tilde request --- */
case TILDEACCENT:
accsp = (width < 25 ? get_delim(mctx, "\\sim", -width, CMSY10) :
/*width search for tilde*/
get_delim(mctx, "~", -width, CMEX10));
if (accsp != NULL) /* found desired tilde */
if ((sp = rastack(mctx, new_subraster(mctx, 1, 1, pixsz), accsp, 1, 0, 1, 3))/*space below*/
!= NULL) { /* have tilde with space below it */
/* "extract" raster with bitmap */
rp = sp->image;
/* and free subraster "envelope" */
free((void *)sp);
mctx->leftsymdef = NULL;
} /* so \tilde{x}^2 works properly */
break;
} /* --- end-of-outer-switch(accent) --- */
/* ------------------------------------------------------------
if we constructed accent raster okay, embed it in a subraster and return it
------------------------------------------------------------ */
/* --- if all okay, allocate subraster to contain constructed raster --- */
if (rp != NULL) { /* accent raster constructed okay */
if ((sp = new_subraster(mctx, 0, 0, 0)) /* allocate subraster "envelope" */
== NULL) /* and if we fail to allocate */
/* free now-unneeded raster */
delete_raster(mctx, rp);
else
/* subraster allocated okay */
{
/* --- init subraster parameters, embedding raster in it --- */
/* constructed image */
sp->type = IMAGERASTER;
/* raster we just constructed */
sp->image = rp;
/* can't set font size here */
sp->size = (-1);
sp->baseline = 0;
} /* can't set baseline here */
} /* --- end-of-if(rp!=NULL) --- */
/* --- return subraster containing desired accent to caller --- */
/* return accent or NULL to caller */
return (sp);
} /* --- end-of-function accent_subraster() --- */
/* ==========================================================================
* Function: arrow_subraster ( width, height, pixsz, drctn, isBig )
* Purpose: Allocate a raster/subraster and draw left/right arrow in it
* --------------------------------------------------------------------------
* Arguments: width (I) int containing number of cols for arrow
* height (I) int containing number of rows for arrow
* pixsz (I) int containing 1 for bitmap, 8 for bytemap
* drctn (I) int containing +1 for right arrow,
* or -1 for left, 0 for leftright
* isBig (I) int containing 1/true for \Long arrows,
* or false for \long arrows, i.e.,
* true for ===> or false for --->.
* --------------------------------------------------------------------------
* Returns: ( subraster * ) ptr to constructed left/right arrow
* or NULL for any error.
* --------------------------------------------------------------------------
* Notes: o
* ======================================================================= */
/* --- entry point --- */
subraster *arrow_subraster(mimetex_ctx *mctx, int width, int height, int pixsz,
int drctn, int isBig)
{
subraster *arrowsp = NULL;
/* index, midrow is arrowhead apex */
int irow, midrow = height / 2;
/* arrowhead thickness and index */
int icol, thickness = (height > 15 ? 2 : 2);
/* black pixel value */
int pixval = (pixsz == 1 ? 1 : (pixsz == 8 ? 255 : (-1)));
int ipix, /* raster pixmap[] index */
npix = width * height; /* #pixels malloced in pixmap[] */
/* ------------------------------------------------------------
allocate raster/subraster and draw arrow line
------------------------------------------------------------ */
if (height < 3) {
/* set minimum height */
height = 3;
midrow = 1;
}
if ((arrowsp = new_subraster(mctx, width, height, pixsz)) /* allocate empty raster */
/* and quit if failed */
== NULL) goto end_of_job;
if (!isBig) /* single line */
/*draw line across midrow*/
rule_raster(mctx, arrowsp->image, midrow, 0, width, 1, 0);
else {
int delta = (width > 6 ? (height > 15 ? 3 : (height > 7 ? 2 : 1)) : 1);
rule_raster(mctx, arrowsp->image, midrow - delta, delta, width - 2*delta, 1, 0);
rule_raster(mctx, arrowsp->image, midrow + delta, delta, width - 2*delta, 1, 0);
}
/* ------------------------------------------------------------
construct arrowhead(s)
------------------------------------------------------------ */
for (irow = 0; irow < height; irow++) { /* for each row of arrow */
/*arrowhead offset for irow*/
int delta = abs(irow - midrow);
/* --- right arrowhead --- */
if (drctn >= 0) {
/* right arrowhead wanted */
for (icol = 0; icol < thickness; icol++) { /* for arrowhead thickness */
/* rightmost-delta-icol */
ipix = ((irow + 1) * width - 1) - delta - icol;
if (ipix >= 0) { /* bounds check */
if (pixsz == 1) /* have a bitmap */
/*turn on arrowhead bit*/
setlongbit((arrowsp->image)->pixmap, ipix);
else
/* should have a bytemap */
if (pixsz == 8) /* check pixsz for bytemap */
((arrowsp->image)->pixmap)[ipix] = pixval;
}
}/*set arrowhead byte*/
}
/* --- left arrowhead (same as right except for ipix calculation) --- */
if (drctn <= 0) {
/* left arrowhead wanted */
for (icol = 0; icol < thickness; icol++) { /* for arrowhead thickness */
/* leftmost bit+delta+icol */
ipix = irow * width + delta + icol;
if (ipix < npix) { /* bounds check */
if (pixsz == 1) /* have a bitmap */
/*turn on arrowhead bit*/
setlongbit((arrowsp->image)->pixmap, ipix);
else
/* should have a bytemap */
if (pixsz == 8) /* check pixsz for bytemap */
((arrowsp->image)->pixmap)[ipix] = pixval;
}
}/*set arrowhead byte*/
}
} /* --- end-of-for(irow) --- */
end_of_job:
/*back to caller with arrow or NULL*/
return (arrowsp);
} /* --- end-of-function arrow_subraster() --- */
/* ==========================================================================
* Function: uparrow_subraster ( width, height, pixsz, drctn, isBig )
* Purpose: Allocate a raster/subraster and draw up/down arrow in it
* --------------------------------------------------------------------------
* Arguments: width (I) int containing number of cols for arrow
* height (I) int containing number of rows for arrow
* pixsz (I) int containing 1 for bitmap, 8 for bytemap
* drctn (I) int containing +1 for up arrow,
* or -1 for down, or 0 for updown
* isBig (I) int containing 1/true for \Long arrows,
* or false for \long arrows, i.e.,
* true for ===> or false for --->.
* --------------------------------------------------------------------------
* Returns: ( subraster * ) ptr to constructed up/down arrow
* or NULL for any error.
* --------------------------------------------------------------------------
* Notes: o
* ======================================================================= */
/* --- entry point --- */
subraster *uparrow_subraster(mimetex_ctx *mctx, int width, int height, int pixsz,
int drctn, int isBig)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
/* allocate arrow subraster */
subraster *arrowsp = NULL;
/* index, midcol is arrowhead apex */
int icol, midcol = width / 2;
/* arrowhead thickness and index */
int irow, thickness = (width > 15 ? 2 : 2);
/* black pixel value */
int pixval = (pixsz == 1 ? 1 : (pixsz == 8 ? 255 : (-1)));
int ipix, /* raster pixmap[] index */
npix = width * height; /* #pixels malloced in pixmap[] */
/* ------------------------------------------------------------
allocate raster/subraster and draw arrow line
------------------------------------------------------------ */
if (width < 3) {
/* set minimum width */
width = 3;
midcol = 1;
}
if ((arrowsp = new_subraster(mctx, width, height, pixsz)) /* allocate empty raster */
/* and quit if failed */
== NULL) goto end_of_job;
if (!isBig) /* single line */
/*draw line down midcol*/
rule_raster(mctx, arrowsp->image, 0, midcol, 1, height, 0);
else {
int delta = (height > 6 ? (width > 15 ? 3 : (width > 7 ? 2 : 1)) : 1);
rule_raster(mctx, arrowsp->image, delta, midcol - delta, 1, height - 2*delta, 0);
rule_raster(mctx, arrowsp->image, delta, midcol + delta, 1, height - 2*delta, 0);
}
/* ------------------------------------------------------------
construct arrowhead(s)
------------------------------------------------------------ */
for (icol = 0; icol < width; icol++) { /* for each col of arrow */
/*arrowhead offset for icol*/
int delta = abs(icol - midcol);
/* --- up arrowhead --- */
if (drctn >= 0) /* up arrowhead wanted */
for (irow = 0; irow < thickness; irow++) { /* for arrowhead thickness */
/* leftmost+icol */
ipix = (irow + delta) * width + icol;
if (ipix < npix) { /* bounds check */
if (pixsz == 1) /* have a bitmap */
/*turn on arrowhead bit*/
setlongbit((arrowsp->image)->pixmap, ipix);
else
/* should have a bytemap */
if (pixsz == 8) /* check pixsz for bytemap */
((arrowsp->image)->pixmap)[ipix] = pixval;
}
}/*set arrowhead byte*/
/* --- down arrowhead (same as up except for ipix calculation) --- */
if (drctn <= 0) /* down arrowhead wanted */
for (irow = 0; irow < thickness; irow++) { /* for arrowhead thickness */
/* leftmost + icol */
ipix = (height - 1 - delta - irow) * width + icol;
if (ipix > 0) { /* bounds check */
if (pixsz == 1) /* have a bitmap */
/*turn on arrowhead bit*/
setlongbit((arrowsp->image)->pixmap, ipix);
else
/* should have a bytemap */
if (pixsz == 8) /* check pixsz for bytemap */
((arrowsp->image)->pixmap)[ipix] = pixval;
}
}/*set arrowhead byte*/
} /* --- end-of-for(icol) --- */
end_of_job:
/*back to caller with arrow or NULL*/
return (arrowsp);
} /* --- end-of-function uparrow_subraster() --- */
/* ==========================================================================
* Function: rule_raster ( rp, top, left, width, height, type )
* Purpose: Draw a solid or dashed line (or box) in existing raster rp,
* starting at top,left with dimensions width,height.
* --------------------------------------------------------------------------
* Arguments: rp (I) raster * to raster in which rule
* will be drawn
* top (I) int containing row at which top-left corner
* of rule starts (0 is topmost)
* left (I) int containing col at which top-left corner
* of rule starts (0 is leftmost)
* width (I) int containing number of cols for rule
* height (I) int containing number of rows for rule
* type (I) int containing 0 for solid rule,
* 1 for horizontal dashes, 2 for vertical
* 3 for solid rule with corners removed (bevel)
* 4 for strut (nothing drawn)
* --------------------------------------------------------------------------
* Returns: ( int ) 1 if rule drawn okay,
* or 0 for any error.
* --------------------------------------------------------------------------
* Notes: o Rule line is implicitly "horizontal" or "vertical" depending
* on relative width,height dimensions. It's a box if they're
* more or less comparable.
* ======================================================================= */
/* --- entry point --- */
int rule_raster(mimetex_ctx *mctx, raster *rp, int top, int left,
int width, int height, int type)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
/* indexes over rp raster */
int irow = 0, icol = 0;
int ipix = 0, /* raster pixmap[] index */
/* #pixels malloced in rp->pixmap[] */
npix = rp->width * rp->height;
/* true to abend on out-of-bounds error */
int isfatal = 0;
int hdash = 1, vdash = 2, /* type for horizontal, vertical dashes */
bevel = 99/*3*/, strut = 4; /* type for bevel (turned off), strut */
int dashlen = 3, spacelen = 2, /* #pixels for dash followed by space */
isdraw = 1; /* true when drawing dash (init for solid) */
/* ------------------------------------------------------------
Check args
------------------------------------------------------------ */
if (rp == (raster *)NULL) { /* no raster arg supplied */
if (mctx->workingbox != (subraster *)NULL) /* see if we have a mctx->workingbox */
/* use mctx->workingbox if possible */
rp = mctx->workingbox->image;
else return (0);
} /* otherwise signal error to caller */
if (type == bevel) /* remove corners of solid box */
/* too small to remove corners */
if (width < 3 || height < 3) type = 0;
/* ------------------------------------------------------------
Fill line/box
------------------------------------------------------------ */
if (width > 0) { /* zero width implies strut*/
for (irow = top; irow < top + height; irow++) { /* for each scan line */
/* draw nothing for strut */
if (type == strut) isdraw = 0;
if (type == vdash) /*set isdraw for vert dash*/
isdraw = (((irow - top) % (dashlen + spacelen)) < dashlen);
/*first pixel preceding icol*/
ipix = irow * rp->width + left - 1;
for (icol = left; icol < left + width; icol++) { /* each pixel in scan line */
if (type == bevel) { /* remove corners of box */
if ((irow == top && icol == left) /* top-left corner */
|| (irow == top && icol >= left + width - 1) /* top-right corner */
|| (irow >= top + height - 1 && icol == left) /* bottom-left corner */
|| (irow >= top + height - 1 && icol >= left + width - 1)) /* bottom-right */
isdraw = 0;
else isdraw = 1;
} /*set isdraw to skip corner*/
if (type == hdash) /*set isdraw for horiz dash*/
isdraw = (((icol - left) % (dashlen + spacelen)) < dashlen);
if (++ipix >= npix) {
/* bounds check failed */
if (isfatal)
/* abort if error is fatal */
goto end_of_job;
else
/*or just go on to next row*/
break;
} else {
/*ibit is within rp bounds*/
if (isdraw) { /*and we're drawing this bit*/
if (rp->pixsz == 1) /* have a bitmap */
/* so turn on bit in line */
setlongbit(rp->pixmap, ipix);
else
/* should have a bytemap */
if (rp->pixsz == 8) /* check pixsz for bytemap */
((unsigned char *)(rp->pixmap))[ipix] = 255;
} /* set black byte */
}
} /* --- end-of-for(icol) --- */
} /* --- end-of-for(irow) --- */
}
end_of_job:
return (isfatal ? (ipix < npix ? 1 : 0) : 1);
} /* --- end-of-function rule_raster() --- */
/* ==========================================================================
* Function: line_raster ( rp, row0, col0, row1, col1, thickness )
* Purpose: Draw a line from row0,col0 to row1,col1 of thickness
* in existing raster rp.
* --------------------------------------------------------------------------
* Arguments: rp (I) raster * to raster in which a line
* will be drawn
* row0 (I) int containing row at which
* line will start (0 is topmost)
* col0 (I) int containing col at which
* line will start (0 is leftmost)
* row1 (I) int containing row at which
* line will end (rp->height-1 is bottom-most)
* col1 (I) int containing col at which
* line will end (rp->width-1 is rightmost)
* thickness (I) int containing number of pixels/bits
* thick the line will be
* --------------------------------------------------------------------------
* Returns: ( int ) 1 if line drawn okay,
* or 0 for any error.
* --------------------------------------------------------------------------
* Notes: o if row0==row1, a horizontal line is drawn
* between col0 and col1, with row0(==row1) the top row
* and row0+(thickness-1) the bottom row
* o if col0==col1, a vertical bar is drawn
* between row0 and row1, with col0(==col1) the left col
* and col0+(thickness-1) the right col
* o if both the above, you get a square thickness x thickness
* whose top-left corner is row0,col0.
* ======================================================================= */
/* --- entry point --- */
int line_raster(mimetex_ctx *mctx, raster *rp, int row0, int col0,
int row1, int col1, int thickness)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
int irow = 0, icol = 0, /* indexes over rp raster */
locol = col0, hicol = col1, /* col limits at irow */
lorow = row0, hirow = row1; /* row limits at icol */
/* dimensions of input raster */
int width = rp->width, height = rp->height;
int ipix = 0, /* raster pixmap[] index */
npix = width * height; /* #pixels malloced in rp->pixmap[] */
/* true to abend on out-of-bounds error */
int isfatal = 0;
/*true if slope a=0,\infty*/
int isline = (row1 == row0), isbar = (col1 == col0);
double dy = row1 - row0 /* + (row1>=row0? +1.0 : -1.0) */, /* delta-x */
dx = col1 - col0 /* + (col1>=col0? +1.0 : -1.0) */, /* delta-y */
a = (isbar || isline ? 0.0 : dy / dx), /* slope = tan(theta) = dy/dx */
xcol = 0, xrow = 0; /* calculated col at irow, or row at icol */
double ar = ASPECTRATIO, /* aspect ratio width/height of one pixel */
xwidth = (isline ? 0.0 : /*#pixels per row to get sloped line thcknss*/
((double)thickness) * sqrt((dx * dx) + (dy * dy * ar * ar)) / fabs(dy * ar)),
xheight = 1.0;
/* true to draw line recursively */
int isrecurse = 1;
/* ------------------------------------------------------------
Check args
------------------------------------------------------------ */
if (rp == (raster *)NULL) { /* no raster arg supplied */
if (mctx->workingbox != (subraster *)NULL) /* see if we have a mctx->workingbox */
/* use mctx->workingbox if possible */
rp = mctx->workingbox->image;
else return (0);
} /* otherwise signal error to caller */
/* ------------------------------------------------------------
Initialization
------------------------------------------------------------ */
if (mctx->msgfp != NULL && mctx->msglevel >= 29) { /* debugging */
fprintf(mctx->msgfp, "line_raster> row,col0=%d,%d row,col1=%d,%d, thickness=%d\n"
"\t dy,dx=%3.1f,%3.1f, a=%4.3f, xwidth=%4.3f\n",
row0, col0, row1, col1, thickness, dy, dx, a, xwidth);
fflush(mctx->msgfp);
}
/* --- check for recursive line drawing --- */
if (isrecurse) { /* drawing lines recursively */
for (irow = 0; irow < thickness; irow++) { /* each line 1 pixel thick */
double xrow0 = (double)row0, xcol0 = (double)col0,
xrow1 = (double)row1, xcol1 = (double)col1;
if (isline) xrow0 = xrow1 = (double)(row0 + irow);
else if (isbar) xcol0 = xcol1 = (double)(col0 + irow);
if (xrow0 > (-0.001) && xcol0 > (-0.001) /*check line inside raster*/
&& xrow1 < ((double)(height - 1) + 0.001) && xcol1 < ((double)(width - 1) + 0.001))
line_recurse(mctx, rp, xrow0, xcol0, xrow1, xcol1, thickness);
}
return (1);
}
/* --- set params for horizontal line or vertical bar --- */
if (isline) /*interpret row as top row*/
/* set bottom row for line */
row1 = row0 + (thickness - 1);
if (0 && isbar) /*interpret col as left col*/
/* set right col for bar */
hicol = col0 + (thickness - 1);
/* ------------------------------------------------------------
draw line one row at a time
------------------------------------------------------------ */
for (irow = min2(row0, row1); irow <= max2(row0, row1); irow++) { /*each scan line*/
if (!isbar && !isline) { /* neither vert nor horiz */
/* "middle" col in irow */
xcol = col0 + ((double)(irow - row0)) / a;
/* leftmost col */
locol = max2((int)(xcol - 0.5 * (xwidth - 1.0)), 0);
hicol = min2((int)(xcol + 0.5 * (xwidth - 0.0)), max2(col0, col1));
} /*right*/
if (mctx->msgfp != NULL && mctx->msglevel >= 29) /* debugging */
fprintf(mctx->msgfp, "\t irow=%d, xcol=%4.2f, lo,hicol=%d,%d\n",
irow, xcol, locol, hicol);
/*first pix preceding icol*/
ipix = irow * rp->width + min2(locol, hicol) - 1;
for (icol = min2(locol, hicol); icol <= max2(locol, hicol); icol++) /*each pix*/
if (++ipix >= npix) /* bounds check failed */
/* abort if error is fatal */
if (isfatal) goto end_of_job;
/*or just go on to next row*/
else break;
else
/* turn on pixel in line */
if (rp->pixsz == 1) /* have a pixel bitmap */
/* so turn on bit in line */
setlongbit(rp->pixmap, ipix);
else
/* should have a bytemap */
if (rp->pixsz == 8) /* check pixsz for bytemap */
/* set black byte */
((unsigned char *)(rp->pixmap))[ipix] = 255;
} /* --- end-of-for(irow) --- */
/* ------------------------------------------------------------
now _redraw_ line one col at a time to avoid "gaps"
------------------------------------------------------------ */
if (1)
for (icol = min2(col0, col1); icol <= max2(col0, col1); icol++) { /*each scan line*/
if (!isbar && !isline) { /* neither vert nor horiz */
/* "middle" row in icol */
xrow = row0 + ((double)(icol - col0)) * a;
/* topmost row */
lorow = max2((int)(xrow - 0.5 * (xheight - 1.0)), 0);
hirow = min2((int)(xrow + 0.5 * (xheight - 0.0)), max2(row0, row1));
} /*bot*/
if (mctx->msgfp != NULL && mctx->msglevel >= 29) /* debugging */
fprintf(mctx->msgfp, "\t icol=%d, xrow=%4.2f, lo,hirow=%d,%d\n",
icol, xrow, lorow, hirow);
/*first pix preceding icol*/
ipix = irow * rp->width + min2(locol, hicol) - 1;
for (irow = min2(lorow, hirow); irow <= max2(lorow, hirow); irow++) /*each pix*/
if (irow < 0 || irow >= rp->height
|| icol < 0 || icol >= rp->width) /* bounds check */
/* abort if error is fatal */
if (isfatal) goto end_of_job;
/*or just go on to next row*/
else continue;
else
/* set pixel at irow,icol */
setpixel(rp, irow, icol, 255);
} /* --- end-of-for(irow) --- */
/* ------------------------------------------------------------
Back to caller with 1=okay, 0=failed.
------------------------------------------------------------ */
end_of_job:
return (isfatal ? (ipix < npix ? 1 : 0) : 1);
} /* --- end-of-function line_raster(mctx, ) --- */
/* ==========================================================================
* Function: line_recurse ( rp, row0, col0, row1, col1, thickness )
* Purpose: Draw a line from row0,col0 to row1,col1 of thickness
* in existing raster rp.
* --------------------------------------------------------------------------
* Arguments: rp (I) raster * to raster in which a line
* will be drawn
* row0 (I) double containing row at which
* line will start (0 is topmost)
* col0 (I) double containing col at which
* line will start (0 is leftmost)
* row1 (I) double containing row at which
* line will end (rp->height-1 is bottom-most)
* col1 (I) double containing col at which
* line will end (rp->width-1 is rightmost)
* thickness (I) int containing number of pixels/bits
* thick the line will be
* --------------------------------------------------------------------------
* Returns: ( int ) 1 if line drawn okay,
* or 0 for any error.
* --------------------------------------------------------------------------
* Notes: o Recurses, drawing left- and right-halves of line
* until a horizontal or vertical segment is found
* ======================================================================= */
/* --- entry point --- */
int line_recurse(mimetex_ctx *mctx, raster *rp, double row0, double col0,
double row1, double col1, int thickness)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
double delrow = fabs(row1 - row0), /* 0 if line horizontal */
delcol = fabs(col1 - col0), /* 0 if line vertical */
/* draw line when it goes to point */
tolerance = 0.5;
double midrow = 0.5 * (row0 + row1), /* midpoint row */
/* midpoint col */
midcol = 0.5 * (col0 + col1);
/* ------------------------------------------------------------
recurse if either delta > tolerance
------------------------------------------------------------ */
if (delrow > tolerance /* row hasn't converged */
|| delcol > tolerance) { /* col hasn't converged */
/* left half */
line_recurse(mctx, rp, row0, col0, midrow, midcol, thickness);
/* right half */
line_recurse(mctx, rp, midrow, midcol, row1, col1, thickness);
return (1);
}
/* ------------------------------------------------------------
draw converged point
------------------------------------------------------------ */
/*set pixel at midrow,midcol*/
setpixel(rp, iround(midrow), iround(midcol), 255);
return (1);
} /* --- end-of-function line_recurse() --- */
/* ==========================================================================
* Function: circle_raster ( rp, row0, col0, row1, col1,
* thickness, quads )
* Purpose: Draw quad(rant)s of an ellipse in box determined by
* diagonally opposite corner points (row0,col0) and
* (row1,col1), of thickness pixels in existing raster rp.
* --------------------------------------------------------------------------
* Arguments: rp (I) raster * to raster in which an ellipse
* will be drawn
* row0 (I) int containing 1st corner row bounding ellipse
* (0 is topmost)
* col0 (I) int containing 1st corner col bounding ellipse
* (0 is leftmost)
* row1 (I) int containing 2nd corner row bounding ellipse
* (rp->height-1 is bottom-most)
* col1 (I) int containing 2nd corner col bounding ellipse
* (rp->width-1 is rightmost)
* thickness (I) int containing number of pixels/bits
* thick the ellipse arc line will be
* quads (I) char * to null-terminated string containing
* any subset/combination of "1234" specifying
* which quadrant(s) of ellipse to draw.
* NULL ptr draws all four quadrants;
* otherwise 1=upper-right quadrant,
* 2=uper-left, 3=lower-left, 4=lower-right,
* i.e., counterclockwise from 1=positive quad.
* --------------------------------------------------------------------------
* Returns: ( int ) 1 if ellipse drawn okay,
* or 0 for any error.
* --------------------------------------------------------------------------
* Notes: o row0==row1 or col0==col1 are errors
* o using ellipse equation x^2/a^2 + y^2/b^2 = 1
* ======================================================================= */
/* --- entry point --- */
int circle_raster(mimetex_ctx *mctx, raster *rp, int row0, int col0,
int row1, int col1, int thickness, char *quads)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
/* --- lower-left and upper-right bounding points (in our coords) --- */
int lorow = min2(row0, row1), /* lower bounding row (top of box) */
locol = min2(col0, col1), /* lower bounding col (left of box)*/
hirow = max2(row0, row1), /* upper bounding row (bot of box) */
hicol = max2(col0, col1); /* upper bounding col (right of box)*/
/* --- a and b ellipse params --- */
int width = hicol - locol + 1, /* width of bounding box */
height = hirow - lorow + 1, /* height of bounding box */
islandscape = (width >= height ? 1 : 0); /*true if ellipse lying on side*/
double a = ((double)width) / 2.0, /* x=a when y=0 */
b = ((double)height) / 2.0, /* y=b when x=0 */
abmajor = (islandscape ? a : b), /* max2(a,b) */
abminor = (islandscape ? b : a), /* min2(a,b) */
abmajor2 = abmajor * abmajor, /* abmajor^2 */
abminor2 = abminor * abminor; /* abminor^2 */
/* --- other stuff --- */
int imajor = 0, nmajor = max2(width, height), /*index, #pixels on major axis*/
iminor = 0, nminor = min2(width, height); /* solved index on minor axis */
int irow, icol, /* raster indexes at circumference */
/* row,col signs, both +1 in quad 1*/
rsign = 1, csign = 1;
double midrow = ((double)(row0 + row1)) / 2.0, /* center row */
midcol = ((double)(col0 + col1)) / 2.0; /* center col */
double xy, xy2, /* major axis ellipse coord */
/* solved minor ellipse coord */
yx2, yx;
/* true if no pixels out-of-bounds */
int isokay = 1;
/* quadrants if input quads==NULL */
char *qptr = NULL, *allquads = "1234";
/* true to draw ellipse recursively*/
int isrecurse = 1;
/* ------------------------------------------------------------
pixel-by-pixel along positive major axis, quit when it goes negative
------------------------------------------------------------ */
/* draw all quads, or only user's */
if (quads == NULL) quads = allquads;
if (mctx->msgfp != NULL && mctx->msglevel >= 39) /* debugging */
fprintf(mctx->msgfp, "circle_raster> width,height;quads=%d,%d,%s\n",
width, height, quads);
/* problem with input args */
if (nmajor < 1) isokay = 0;
else {
if (isrecurse) { /* use recursive algorithm */
for (qptr = quads; *qptr != '\000'; qptr++) { /* for each character in quads */
/* set thetas based on quadrant */
double theta0 = 0.0, theta1 = 0.0;
switch (*qptr) { /* check for quadrant 1,2,3,4 */
default:
/* unrecognized, assume quadrant 1 */
case '1':
theta0 = 0.0;
theta1 = 90.0;
/* first quadrant */
break;
case '2':
theta0 = 90.0;
theta1 = 180.0;
/* second quadrant */
break;
case '3':
theta0 = 180.0;
theta1 = 270.0;
/* third quadrant */
break;
case '4':
theta0 = 270.0;
theta1 = 360.0;
break;
} /* fourth quadrant */
circle_recurse(mctx, rp, row0, col0, row1, col1, thickness, theta0, theta1);
} /* --- end-of-for(qptr) --- */
return (1);
} /* --- end-of-if(isrecurse) --- */
for (imajor = (nmajor + 1) / 2; ; imajor--) {
/* --- xy is coord along major axis, yx is "solved" along minor axis --- */
/* xy = abmajor ... 0 */
xy = ((double)imajor);
/* negative side symmetrical */
if (xy < 0.0) break;
/* "solve" ellipse equation */
yx2 = abminor2 * (1.0 - xy * xy / abmajor2);
/* take sqrt if possible */
yx = (yx2 > 0.0 ? sqrt(yx2) : 0.0);
/* nearest integer */
iminor = iround(yx);
/* --- set pixels for each requested quadrant --- */
for (qptr = quads; *qptr != '\000'; qptr++) { /* for each character in quads */
rsign = (-1);
/* init row,col in user quadrant 1 */
csign = 1;
switch (*qptr) { /* check for quadrant 1,2,3,4 */
default:
/* unrecognized, assume quadrant 1 */
break;
case '4':
rsign = 1;
/* row,col both pos in quadrant 4 */
break;
case '3':
/* row pos, col neg in quadrant 3 */
rsign = 1;
case '2':
csign = (-1);
break;
} /* row,col both neg in quadrant 2 */
irow = iround(midrow + (double)rsign * (islandscape ? yx : xy));
/* keep irow in bounds */
irow = min2(hirow, max2(lorow, irow));
icol = iround(midcol + (double)csign * (islandscape ? xy : yx));
/* keep icol in bounds */
icol = min2(hicol, max2(locol, icol));
if (mctx->msgfp != NULL && mctx->msglevel >= 49) /* debugging */
fprintf(mctx->msgfp, "\t...imajor=%d; iminor,quad,irow,icol=%d,%c,%d,%d\n",
imajor, iminor, *qptr, irow, icol);
if (irow < 0 || irow >= rp->height /* row outside raster */
|| icol < 0 || icol >= rp->width) { /* col outside raster */
/* signal out-of-bounds pixel */
isokay = 0;
continue;
} /* but still try remaining points */
/* set pixel at irow,icol */
setpixel(rp, irow, icol, 255);
} /* --- end-of-for(qptr) --- */
} /* --- end-of-for(imajor) --- */
/* ------------------------------------------------------------
now do it _again_ along minor axis to avoid "gaps"
------------------------------------------------------------ */
if (1 && iminor > 0)
for (iminor = (nminor + 1) / 2; ; iminor--) {
/* --- yx is coord along minor axis, xy is "solved" along major axis --- */
/* yx = abminor ... 0 */
yx = ((double)iminor);
/* negative side symmetrical */
if (yx < 0.0) break;
/* "solve" ellipse equation */
xy2 = abmajor2 * (1.0 - yx * yx / abminor2);
/* take sqrt if possible */
xy = (xy2 > 0.0 ? sqrt(xy2) : 0.0);
/* nearest integer */
imajor = iround(xy);
/* --- set pixels for each requested quadrant --- */
for (qptr = quads; *qptr != '\000'; qptr++) { /* for each character in quads */
rsign = (-1);
/* init row,col in user quadrant 1 */
csign = 1;
switch (*qptr) { /* check for quadrant 1,2,3,4 */
default:
/* unrecognized, assume quadrant 1 */
break;
case '4':
rsign = 1;
/* row,col both pos in quadrant 4 */
break;
case '3':
/* row pos, col neg in quadrant 3 */
rsign = 1;
case '2':
csign = (-1);
break;
} /* row,col both neg in quadrant 2 */
irow = iround(midrow + (double)rsign * (islandscape ? yx : xy));
/* keep irow in bounds */
irow = min2(hirow, max2(lorow, irow));
icol = iround(midcol + (double)csign * (islandscape ? xy : yx));
/* keep icol in bounds */
icol = min2(hicol, max2(locol, icol));
if (mctx->msgfp != NULL && mctx->msglevel >= 49) /* debugging */
fprintf(mctx->msgfp, "\t...iminor=%d; imajor,quad,irow,icol=%d,%c,%d,%d\n",
iminor, imajor, *qptr, irow, icol);
if (irow < 0 || irow >= rp->height /* row outside raster */
|| icol < 0 || icol >= rp->width) { /* col outside raster */
/* signal out-of-bounds pixel */
isokay = 0;
continue;
} /* but still try remaining points */
/* set pixel at irow,icol */