-
Notifications
You must be signed in to change notification settings - Fork 2
/
tex.c
1746 lines (1719 loc) · 86.9 KB
/
tex.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"
/* ==========================================================================
* Function: strdetex ( s, mode )
* Purpose: Removes/replaces any LaTeX math chars in s
* so that s can be displayed "verbatim",
* e.g., for error messages.
* --------------------------------------------------------------------------
* Arguments: s (I) char * to null-terminated string
* whose math chars are to be removed/replaced
* mode (I) int containing 0 to _not_ use macros (i.e.,
* mimeprep won't be called afterwards),
* or containing 1 to use macros that will
* be expanded by a subsequent call to mimeprep.
* --------------------------------------------------------------------------
* Returns: ( char * ) ptr to "cleaned" copy of s
* or "" (empty string) for any error.
* --------------------------------------------------------------------------
* Notes: o The returned pointer addresses a static buffer,
* so don't call strdetex() again until you're finished
* with output from the preceding call.
* ======================================================================= */
/* --- entry point --- */
char *strdetex(char *s, int mode)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
/* copy of s with no math chars */
static char sbuff[4096];
/* replace _ with -, etc */
int strreplace();
/* ------------------------------------------------------------
Make a clean copy of s
------------------------------------------------------------ */
/* --- check input --- */
/* initialize in case of error */
*sbuff = '\000';
/* no input */
if (isempty(s)) goto end_of_job;
/* --- start with copy of s --- */
/* leave room for replacements */
strninit(sbuff, s, 2048);
/* --- make some replacements -- we *must* replace \ { } first --- */
/*change all \'s to text*/
strreplace(sbuff, "\\", "\\backslash~\\!\\!", 0);
/*change all {'s to \lbrace*/
strreplace(sbuff, "{", "\\lbrace~\\!\\!", 0);
/*change all }'s to \rbrace*/
strreplace(sbuff, "}", "\\rbrace~\\!\\!", 0);
/* --- now our further replacements may contain \directives{args} --- */
/* change all _'s to \_ */
if (mode >= 1) strreplace(sbuff, "_", "\\_", 0);
/*change them to text*/
else strreplace(sbuff, "_", "\\underline{\\qquad}", 0);
/* change all <'s to text */
if (0)strreplace(sbuff, "<", "\\textlangle ", 0);
/* change all >'s to text */
if (0)strreplace(sbuff, ">", "\\textrangle ", 0);
/* change all $'s to text */
if (0)strreplace(sbuff, "$", "\\textdollar ", 0);
/* change all $'s to \$ */
strreplace(sbuff, "$", "\\$", 0);
/* change all &'s to \& */
strreplace(sbuff, "&", "\\&", 0);
/* change all %'s to \% */
strreplace(sbuff, "%", "\\%", 0);
/* change all #'s to \# */
strreplace(sbuff, "#", "\\#", 0);
/*strreplace(sbuff,"~","\\~",0);*/ /* change all ~'s to \~ */
/* change all ^'s to \^ */
strreplace(sbuff, "^", "{\\fs{+2}\\^}", 0);
end_of_job:
/* back with clean copy of s */
return (sbuff);
} /* --- end-of-function strdetex() --- */
/* ==========================================================================
* Function: strtexchr ( char *string, char *texchr )
* Purpose: Find first texchr in string, but texchr must be followed
* by non-alpha
* --------------------------------------------------------------------------
* Arguments: string (I) char * to null-terminated string in which
* first occurrence of delim will be found
* texchr (I) char * to null-terminated string that
* will be searched for
* --------------------------------------------------------------------------
* Returns: ( char * ) ptr to first char of texchr in string
* or NULL if not found or for any error.
* --------------------------------------------------------------------------
* Notes: o texchr should contain its leading \, e.g., "\\left"
* ======================================================================= */
/* --- entry point --- */
char *strtexchr(char *string, char *texchr)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
/* ptr returned to caller*/
char delim, *ptexchr = (char *)NULL;
/* start or continue up search here*/
char *pstring = string;
/* #chars in texchr */
int texchrlen = (texchr == NULL ? 0 : strlen(texchr));
/* ------------------------------------------------------------
locate texchr in string
------------------------------------------------------------ */
if (string != (char *)NULL /* check that we got input string */
&& texchrlen > 0) { /* and a texchr to search for */
while ((ptexchr = strstr(pstring, texchr)) /* look for texchr in string */
!= (char *)NULL) /* found it */
if ((delim = ptexchr[texchrlen]) /* char immediately after texchr */
/* texchr at very end of string */
== '\000') break;
else
/* if there are chars after texchr */
if (isalpha(delim) /*texchr is prefix of longer symbol*/
|| 0) /* other tests to be determined */
/* continue search after texchr */
pstring = ptexchr + texchrlen;
else
/* passed all tests */
break;
} /*so return ptr to texchr to caller*/
/* ptr to texchar back to caller */
return (ptexchr);
} /* --- end-of-function strtexchr() --- */
/* ==========================================================================
* Function: findbraces ( char *expression, char *command )
* Purpose: If expression!=NULL, finds opening left { preceding command;
* if expression==NULL, finds closing right } after command.
* For example, to parse out {a+b\over c+d} call findbraces()
* twice.
* --------------------------------------------------------------------------
* Arguments: expression (I) NULL to find closing right } after command,
* or char * to null-terminated string to find
* left opening { preceding command.
* command (I) char * to null-terminated string whose
* first character is usually the \ of \command
* --------------------------------------------------------------------------
* Returns: ( char * ) ptr to either opening { or closing },
* or NULL for any error.
* --------------------------------------------------------------------------
* Notes: o
* ======================================================================= */
/* --- entry point --- */
char *findbraces(char *expression, char *command)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
/* true to find left opening { */
int isopen = (expression == NULL ? 0 : 1);
char *left = "{", *right = "}", /* delims bracketing {x\command y} */
*delim = (isopen ? left : right), /* delim we want, { if isopen */
*match = (isopen ? right : left), /* matching delim, } if isopen */
*brace = NULL; /* ptr to delim returned to caller */
/* pointer increment */
int inc = (isopen ? -1 : +1);
/* nesting level, for {{}\command} */
int level = 1;
/* start search here */
char *ptr = command;
/* true to set {}'s if none found */
int setbrace = 1;
/* ------------------------------------------------------------
search for left opening { before command, or right closing } after command
------------------------------------------------------------ */
while (1) { /* search for brace, or until end */
/* --- next char to check for delim --- */
/* bump ptr left or right */
ptr += inc;
/* --- check for beginning or end of expression --- */
if (isopen) { /* going left, check for beginning */
/* went before start of string */
if (ptr < expression) break;
} else {
/* went past end of string */
if (*ptr == '\000') break;
}
/* --- don't check this char if it's escaped --- */
if (!isopen || ptr > expression) /* very first char can't be escaped*/
if (isthischar(*(ptr - 1), ESCAPE)) /* escape char precedes current */
/* so don't check this char */
continue;
/* --- check for delim --- */
if (isthischar(*ptr, delim)) /* found delim */
if (--level == 0) { /* and it's not "internally" nested*/
/* set ptr to brace */
brace = ptr;
goto end_of_job;
} /* and return it to caller */
/* --- check for matching delim --- */
if (isthischar(*ptr, match)) /* found matching delim */
/* so bump nesting level */
level++;
} /* --- end-of-while(1) --- */
end_of_job:
if (brace == (char *)NULL) /* open{ or close} not found */
if (setbrace) /* want to force one at start/end? */
/* { before expressn, } after cmmnd*/
brace = ptr;
/*back to caller with delim or NULL*/
return (brace);
} /* --- end-of-function findbraces() --- */
/* ==========================================================================
* Function: texchar ( expression, chartoken )
* Purpose: scans expression, returning either its first character,
* or the next \sequence if that first char is \,
* and a pointer to the first expression char past that.
* --------------------------------------------------------------------------
* Arguments: expression (I) char * to first char of null-terminated
* string containing valid LaTeX expression
* to be scanned
* chartoken (O) char * to null-terminated string returning
* either the first (non-whitespace) character
* of expression if that char isn't \, or else
* the \ and everything following it up to
* the next non-alphabetic character (but at
* least one char following the \ even if
* it's non-alpha)
* --------------------------------------------------------------------------
* Returns: ( char * ) ptr to the first char of expression
* past returned chartoken,
* or NULL for any parsing error.
* --------------------------------------------------------------------------
* Notes: o Does *not* skip leading whitespace, but simply
* returns any whitespace character as the next character.
* ======================================================================= */
/* --- entry point --- */
char *texchar(mimetex_ctx *mctx, char *expression, char *chartoken)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
int esclen = 0, /*length of escape sequence*/
/* max len of esc sequence */
maxesclen = 128;
/* ptr into chartoken */
char *ptoken = chartoken;
/* prefix index */
int iprefix = 0;
/*e.g., \big followed by ( */
static char *prefixes[] =
{
/* "\\left", "\\right", */
"\\big", "\\Big", "\\bigg", "\\Bigg",
"\\bigl", "\\Bigl", "\\biggl", "\\Biggl",
"\\bigr", "\\Bigr", "\\biggr", "\\Biggr", NULL
};
/* may be followed by * */
static char *starred[] = {
"\\hspace", "\\!", NULL
};
/* ------------------------------------------------------------
just return the next char if it's not \
------------------------------------------------------------ */
/* --- error check for end-of-string --- */
/* init in case of error */
*ptoken = '\000';
/* nothing to scan */
if (expression == NULL) return(NULL);
/* nothing to scan */
if (*expression == '\000') return(NULL);
/* --- always returning first character (either \ or some other char) --- */
/* here's first character */
*ptoken++ = *expression++;
/* --- if first char isn't \, then just return it to caller --- */
if (!isthischar(*(expression - 1), ESCAPE)) { /* not a \, so return char */
/* add a null terminator */
*ptoken = '\000';
goto end_of_job;
} /* ptr past returned char */
if (*expression == '\000') { /* \ is very last char */
/* flush bad trailing \ */
*chartoken = '\000';
return(NULL);
} /* and signal end-of-job */
/* ------------------------------------------------------------
we have an escape sequence, so return all alpha chars following \
------------------------------------------------------------ */
/* --- accumulate chars until first non-alpha char found --- */
for (; isalpha(*expression); esclen++) { /* till first non-alpha... */
if (esclen < maxesclen - 3) /* more room in chartoken */
/*copy alpha char, bump ptr*/
*ptoken++ = *expression;
expression++;
} /* bump expression ptr */
/* --- if we have a prefix, append next texchar, e.g., \big( --- */
/* set null for compare */
*ptoken = '\000';
for (iprefix = 0; prefixes[iprefix] != NULL; iprefix++) /* run thru list */
if (strcmp(chartoken, prefixes[iprefix]) == 0) { /* have an exact match */
char nextchar[256];
/* texchar after prefix */
int nextlen = 0;
/* skip space after prefix*/
skipwhite(expression);
/* get nextchar */
expression = texchar(mctx, expression, nextchar);
if ((nextlen = strlen(nextchar)) > 0) { /* #chars in nextchar */
/* append nextchar */
strcpy(ptoken, nextchar);
/* point to null terminator*/
ptoken += strlen(nextchar);
esclen += strlen(nextchar);
} /* and bump escape length */
break;
} /* stop checking prefixes */
/* --- every \ must be followed by at least one char, e.g., \[ --- */
if (esclen < 1) /* \ followed by non-alpha */
/*copy non-alpha, bump ptrs*/
*ptoken++ = *expression++;
/* null-terminate token */
*ptoken = '\000';
/* --- check for \hspace* or other starred commands --- */
for (iprefix = 0; starred[iprefix] != NULL; iprefix++) /* run thru list */
if (strcmp(chartoken, starred[iprefix]) == 0) /* have an exact match */
if (*expression == '*') { /* follows by a * */
/* copy * and bump ptr */
*ptoken++ = *expression++;
/* null-terminate token */
*ptoken = '\000';
break;
} /* stop checking */
/* --- respect spaces in text mode, except first space after \escape --- */
if (esclen >= 1) { /*only for alpha \sequences*/
if (istextmode) /* in \rm or \it text mode */
if (isthischar(*expression, WHITEDELIM)) /* delim follows \sequence */
expression++;
} /* so flush delim */
/* --- back to caller --- */
end_of_job:
if (mctx->msgfp != NULL && mctx->msglevel >= 999) {
fprintf(mctx->msgfp, "texchar> returning token = \"%s\"\n", chartoken);
fflush(mctx->msgfp);
}
/*ptr to 1st non-alpha char*/
return (expression);
} /* --- end-of-function texchar() --- */
/* ==========================================================================
* Function: texsubexpr (expression,subexpr,maxsubsz,
* left,right,isescape,isdelim)
* Purpose: scans expression, returning everything between a balanced
* left{...right} subexpression if the first non-whitespace
* char of expression is an (escaped or unescaped) left{,
* or just the next texchar(mctx, ) otherwise,
* and a pointer to the first expression char past that.
* --------------------------------------------------------------------------
* Arguments: expression (I) char * to first char of null-terminated
* string containing valid LaTeX expression
* to be scanned
* subexpr (O) char * to null-terminated string returning
* either everything between a balanced {...}
* subexpression if the first char is {,
* or the next texchar(mctx, ) otherwise.
* maxsubsz (I) int containing max #bytes returned
* in subexpr buffer (0 means unlimited)
* left (I) char * specifying allowable left delimiters
* that begin subexpression, e.g., "{[(<"
* right (I) char * specifying matching right delimiters
* in the same order as left, e.g., "}])>"
* isescape (I) int controlling whether escaped and/or
* unescaped left,right are matched;
* see isbrace() comments below for details.
* isdelim (I) int containing true (non-zero) to return
* the leading left and trailing right delims
* (if any were found) along with subexpr,
* or containing false=0 to return subexpr
* without its delimiters
* --------------------------------------------------------------------------
* Returns: ( char * ) ptr to the first char of expression
* past returned subexpr (see Notes),
* or NULL for any parsing error.
* --------------------------------------------------------------------------
* Notes: o If subexpr is of the form left{...right},
* the outer {}'s are returned as part of subexpr
* if isdelim is true; if isdelim is false the {}'s aren't
* returned. In either case the returned pointer is
* *always* bumped past the closing right}, even if
* that closing right} isn't returned in subexpr.
* o If subexpr is not of the form left{...right},
* the returned pointer is on the character immediately
* following the last character returned in subexpr
* o \. acts as LaTeX \right. and matches any \left(
* And it also acts as a LaTeX \left. and matches any \right)
* ======================================================================= */
/* --- entry point --- */
char *texsubexpr(mimetex_ctx *mctx, char *expression, char *subexpr, int maxsubsz,
char *left, char *right, int isescape, int isdelim)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
/*next char (or \sequence) from expression*/
char *leftptr, leftdelim[256] = "(\000", /* left( found in expression */
/* and matching right) */
rightdelim[256] = ")\000";
/*original inputs*/
char *origexpression = expression, *origsubexpr = subexpr;
/* check for \left, and get it */
int gotescape = 0, /* true if leading char of expression is \ */
/* while parsing, true if preceding char \ */
prevescape = 0;
/* true matches any right with left, (...] */
int isanyright = 1;
/* true if left brace is a \. */
int isleftdot = 0;
/* current # of nested braces */
int nestlevel = 1;
int subsz = 0 /*,maxsubsz=MAXSUBXSZ*/; /*#chars in returned subexpr buffer*/
/* ------------------------------------------------------------
skip leading whitespace and just return the next char if it's not {
------------------------------------------------------------ */
/* --- skip leading whitespace and error check for end-of-string --- */
/* init in case of error */
*subexpr = '\000';
/*can't dereference null ptr*/
if (expression == NULL) return(NULL);
/* leading whitespace gone */
skipwhite(expression);
/* nothing left to scan */
if (*expression == '\000') return(NULL);
/* --- set maxsubsz --- */
/* input 0 means unlimited */
if (maxsubsz < 1) maxsubsz = MAXSUBXSZ - 2;
/* --- check for escape --- */
if (isthischar(*expression, ESCAPE)) /* expression is escaped */
/* so set flag accordingly */
gotescape = 1;
/* --- check for \left...\right --- */
if (gotescape) /* begins with \ */
if (memcmp(expression + 1, "left", 4)) /* and followed by left */
if (strchr(left, 'l') != NULL) /* caller wants \left's */
if (strtexchr(expression, "\\left") == expression) { /*expression=\left...*/
char *pright = texleft(mctx, expression, subexpr, maxsubsz, /* find ...\right*/
(isdelim ? NULL : leftdelim), rightdelim);
/* caller wants delims */
if (isdelim) strcat(subexpr, rightdelim);
/*back to caller past \right*/
return (pright);
} /* --- end-of-if(expression=="\\left") --- */
/* --- if first char isn't left{ or script, just return it to caller --- */
if (!isbrace(mctx, expression, left, isescape)) { /* not a left{ */
if (!isthischar(*expression, SCRIPTS)) /* and not a script */
/* next char to caller */
return (texchar(mctx, expression, subexpr));
else { /* --- kludge for super/subscripts to accommodate texscripts() --- */
/* signal script */
*subexpr++ = *expression;
/* null-terminate subexpr */
*subexpr = '\000';
return (expression);
}
} /* leave script in stream */
/* --- extract left and find matching right delimiter --- */
/* the left( in expression */
*leftdelim = *(expression + gotescape);
if ((gotescape && *leftdelim == '.') /* we have a left \. */
|| (gotescape && isanyright)) { /*or are matching any right*/
/* so just set flag */
isleftdot = 1;
*leftdelim = '\000';
} /* and reset leftdelim */
else
/* find matching \right */
if ((leftptr = strchr(left, *leftdelim)) != NULL) /* ptr to that left( */
/* get the matching right) */
*rightdelim = right[(int)(leftptr-left)];
else
/* can't happen -- pgm bug */
/*just signal eoj to caller*/
return (NULL);
/* ------------------------------------------------------------
accumulate chars between balanced {}'s, i.e., till nestlevel returns to 0
------------------------------------------------------------ */
/* --- first initialize by bumping past left{ or \{ --- */
/*caller wants { in subexpr*/
if (isdelim) *subexpr++ = *expression++;
/* always bump past left{ */
else expression++;
if (gotescape) { /*need to bump another char*/
/* caller wants char, too */
if (isdelim) *subexpr++ = *expression++;
else expression++;
} /* else just bump past it */
/* --- set maximum size for numerical arguments --- */
if (0) /* check turned on or off? */
if (!isescape && !isdelim) /*looking for numerical arg*/
/* set max arg size */
maxsubsz = 96;
/* --- search for matching right} --- */
while (1) { /*until balanced right} */
/* --- error check for end-of-string --- */
if (*expression == '\000') { /* premature end-of-string */
if (0 && (!isescape && !isdelim)) { /*looking for numerical arg,*/
/* so end-of-string is error*/
expression = origexpression;
subexpr = origsubexpr;
} /* so reset all ptrs */
if (isdelim) { /* generate fake right */
if (gotescape) { /* need escaped right */
/* set escape char */
*subexpr++ = '\\';
*subexpr++ = '.';
} /* and fake \right. */
else
/* escape not wanted */
*subexpr++ = *rightdelim;
} /* so fake actual right */
/* null-terminate subexpr */
*subexpr = '\000';
return (expression);
} /* back with final token */
/* --- check preceding char for escape --- */
if (isthischar(*(expression - 1), ESCAPE)) /* previous char was \ */
/* so flip escape flag */
prevescape = 1 - prevescape;
/* or turn flag off */
else prevescape = 0;
/* --- check for { and } (un/escaped as per leading left) --- */
if (gotescape == prevescape) /* escaped iff leading is */
{
/* --- check for (closing) right delim and see if we're done --- */
if (isthischar(*expression, rightdelim) /* found a right} */
|| (isleftdot && isthischar(*expression, right)) /*\left. matches all*/
|| (prevescape && isthischar(*expression, "."))) /*or found \right. */
if (--nestlevel < 1) { /*\right balances 1st \left*/
if (isdelim) /*caller wants } in subexpr*/
/* so end subexpr with } */
*subexpr++ = *expression;
else
/*check for \ before right}*/
if (prevescape) /* have unwanted \ */
/* so replace it with null */
*(subexpr - 1) = '\000';
/* null-terminate subexpr */
*subexpr = '\000';
return (expression + 1);
} /* back with char after } */
/* --- check for (another) left{ --- */
if (isthischar(*expression, leftdelim) /* found another left{ */
|| (isleftdot && isthischar(*expression, left))) /* any left{ */
nestlevel++;
} /* --- end-of-if(gotescape==prevescape) --- */
/* --- not done, so copy char to subexpr and continue with next char --- */
if (++subsz < maxsubsz - 5) /* more room in subexpr */
/* so copy char and bump ptr*/
*subexpr++ = *expression;
/* bump expression ptr */
expression++;
} /* --- end-of-while(1) --- */
} /* --- end-of-function texsubexpr() --- */
/* ==========================================================================
* Function: texleft (expression,subexpr,maxsubsz,ldelim,rdelim)
* Purpose: scans expression, starting after opening \left,
* and returning ptr after matching closing \right.
* Everything between is returned in subexpr, if given.
* Likewise, if given, ldelim returns delimiter after \left
* and rdelim returns delimiter after \right.
* If ldelim is given, the returned subexpr doesn't include it.
* If rdelim is given, the returned pointer is after that delim.
* --------------------------------------------------------------------------
* Arguments: expression (I) char * to first char of null-terminated
* string immediately following opening \left
* subexpr (O) char * to null-terminated string returning
* either everything between balanced
* \left ... \right. If leftdelim given,
* subexpr does _not_ contain that delimiter.
* maxsubsz (I) int containing max #bytes returned
* in subexpr buffer (0 means unlimited)
* ldelim (O) char * returning delimiter following
* opening \left
* rdelim (O) char * returning delimiter following
* closing \right
* --------------------------------------------------------------------------
* Returns: ( char * ) ptr to the first char of expression
* past closing \right, or past closing
* right delimiter if rdelim!=NULL,
* or NULL for any error.
* --------------------------------------------------------------------------
* Notes: o
* ======================================================================= */
/* --- entry point --- */
char *texleft(mimetex_ctx *mctx, char *expression, char *subexpr, int maxsubsz,
char *ldelim, char *rdelim)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
/* locate matching \right */
char *pright = expression;
/* tex delimiters */
static char left[16] = "\\left", right[16] = "\\right";
/* #chars between \left...\right */
int sublen = 0;
/* ------------------------------------------------------------
initialization
------------------------------------------------------------ */
/* --- init output --- */
/* init subexpr, if given */
if (subexpr != NULL) *subexpr = '\000';
/* init ldelim, if given */
if (ldelim != NULL) *ldelim = '\000';
/* init rdelim, if given */
if (rdelim != NULL) *rdelim = '\000';
/* --- check args --- */
/* no input supplied */
if (expression == NULL) goto end_of_job;
/* nothing after \left */
if (*expression == '\000') goto end_of_job;
/* --- determine left delimiter --- */
if (ldelim != NULL) { /* caller wants left delim */
/* interpret \left ( as \left( */
skipwhite(expression);
expression = texchar(mctx, expression, ldelim);
} /*delim from expression*/
/* ------------------------------------------------------------
locate \right balancing opening \left
------------------------------------------------------------ */
/* --- first \right following \left --- */
if ((pright = strtexchr(expression, right)) /* look for \right after \left */
!= NULL) { /* found it */
/* --- find matching \right by pushing past any nested \left's --- */
/* start after first \left( */
char *pleft = expression;
while (1) { /*break when matching \right found*/
/* -- locate next nested \left if there is one --- */
if ((pleft = strtexchr(pleft, left)) /* find next \left */
/*no more, so matching \right found*/
== NULL) break;
/* push ptr past \left token */
pleft += strlen(left);
/* not nested if \left after \right*/
if (pleft >= pright) break;
/* --- have nested \left, so push forward to next \right --- */
if ((pright = strtexchr(pright + strlen(right), right)) /* find next \right */
/* ran out of \right's */
== NULL) break;
} /* --- end-of-while(1) --- */
} /* --- end-of-if(pright!=NULL) --- */
/* --- set subexpression length, push pright past \right --- */
if (pright != (char *)NULL) { /* found matching \right */
/* #chars between \left...\right */
sublen = (int)(pright - expression);
pright += strlen(right);
} /* so push pright past \right */
/* ------------------------------------------------------------
get rightdelim and subexpr between \left...\right
------------------------------------------------------------ */
/* --- get delimiter following \right --- */
if (rdelim != NULL) { /* caller wants right delim */
if (pright == (char *)NULL) { /* assume \right. at end of exprssn*/
/* set default \right. */
strcpy(rdelim, ".");
/* use entire remaining expression */
sublen = strlen(expression);
pright = expression + sublen;
} /* and push pright to end-of-string*/
else { /* have explicit matching \right */
/* interpret \right ) as \right) */
skipwhite(pright);
/* pull delim from expression */
pright = texchar(mctx, pright, rdelim);
if (*rdelim == '\000') strcpy(rdelim, ".");
}
} /* or set \right. */
/* --- get subexpression between \left...\right --- */
if (sublen > 0) /* have subexpr */
if (subexpr != NULL) { /* and caller wants it */
/* max buffer size */
if (maxsubsz > 0) sublen = min2(sublen, maxsubsz - 1);
/* stuff between \left...\right */
memcpy(subexpr, expression, sublen);
subexpr[sublen] = '\000';
} /* null-terminate subexpr */
end_of_job:
if (mctx->msglevel >= 99 && mctx->msgfp != NULL) {
fprintf(mctx->msgfp, "texleft> ldelim=%s, rdelim=%s, subexpr=%.128s\n",
(ldelim == NULL ? "none" : ldelim), (rdelim == NULL ? "none" : rdelim),
(subexpr == NULL ? "none" : subexpr));
fflush(mctx->msgfp);
}
return (pright);
} /* --- end-of-function texleft --- */
/* ==========================================================================
* Function: texscripts ( expression, subscript, superscript, which )
* Purpose: scans expression, returning subscript and/or superscript
* if expression is of the form _x^y or ^{x}_{y},
* or any (valid LaTeX) permutation of the above,
* and a pointer to the first expression char past "scripts"
* --------------------------------------------------------------------------
* Arguments: expression (I) char * to first char of null-terminated
* string containing valid LaTeX expression
* to be scanned
* subscript (O) char * to null-terminated string returning
* subscript (without _), if found, or "\000"
* superscript (O) char * to null-terminated string returning
* superscript (without ^), if found, or "\000"
* which (I) int containing 1 for subscript only,
* 2 for superscript only, >=3 for either/both
* --------------------------------------------------------------------------
* Returns: ( char * ) ptr to the first char of expression
* past returned "scripts" (unchanged
* except for skipped whitespace if
* neither subscript nor superscript found),
* or NULL for any parsing error.
* --------------------------------------------------------------------------
* Notes: o an input expression like ^a^b_c will return superscript="b",
* i.e., totally ignoring all but the last "script" encountered
* ======================================================================= */
/* --- entry point --- */
char *texscripts(mimetex_ctx *mctx, char *expression, char *subscript,
char *superscript, int which)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
/* next subexpression from expression */
char *texsubexpr();
/* check that we don't eat, e.g., x_1_2 */
int gotsub = 0, gotsup = 0;
/* ------------------------------------------------------------
init "scripts"
------------------------------------------------------------ */
/*init in case no subscript*/
if (subscript != NULL) *subscript = '\000';
/*init in case no super*/
if (superscript != NULL) *superscript = '\000';
/* ------------------------------------------------------------
get subscript and/or superscript from expression
------------------------------------------------------------ */
while (expression != NULL) {
/* leading whitespace gone */
skipwhite(expression);
/* nothing left to scan */
if (*expression == '\000') return(expression);
if (isthischar(*expression, SUBSCRIPT) /* found _ */
&& (which == 1 || which > 2)) { /* and caller wants it */
if (gotsub /* found 2nd subscript */
/* or no subscript buffer */
|| subscript == NULL) break;
/* set subscript flag */
gotsub = 1;
expression = texsubexpr(mctx, expression + 1, subscript, 0, "{", "}", 0, 0);
} else /* no _, check for ^ */
if (isthischar(*expression, SUPERSCRIPT) /* found ^ */
&& which >= 2) { /* and caller wants it */
if (gotsup /* found 2nd superscript */
/* or no superscript buffer*/
|| superscript == NULL) break;
/* set superscript flag */
gotsup = 1;
expression = texsubexpr(mctx, expression + 1, superscript, 0, "{", "}", 0, 0);
} else /* neither _ nor ^ */
/*return ptr past "scripts"*/
return (expression);
} /* --- end-of-while(expression!=NULL) --- */
return (expression);
} /* --- end-of-function texscripts() --- */
/* ==========================================================================
* Function: isbrace ( expression, braces, isescape )
* Purpose: checks leading char(s) of expression for a brace,
* either escaped or unescaped depending on isescape,
* except that { and } are always matched, if they're
* in braces, regardless of isescape.
* --------------------------------------------------------------------------
* Arguments: expression (I) char * to first char of null-terminated
* string containing a valid LaTeX expression
* whose leading char(s) are checked for braces
* that begin subexpression, e.g., "{[(<"
* braces (I) char * specifying matching brace delimiters
* to be checked for, e.g., "{[(<" or "}])>"
* isescape (I) int containing 0 to match only unescaped
* braces, e.g., (...) or {...}, etc,
* or containing 1 to match only escaped
* braces, e.g., \(...\) or \[...\], etc,
* or containing 2 to match either.
* But note: if {,} are in braces
* then they're *always* matched whether
* escaped or not, regardless of isescape.
* --------------------------------------------------------------------------
* Returns: ( int ) 1 if the leading char(s) of expression
* is a brace, or 0 if not.
* --------------------------------------------------------------------------
* Notes: o
* ======================================================================= */
/* --- entry point --- */
int isbrace(mimetex_ctx *mctx, char *expression, char *braces, int isescape)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
int gotescape = 0, /* true if leading char is an escape */
/*true if first non-escape char is a brace*/
gotbrace = 0;
/* ------------------------------------------------------------
check for brace
------------------------------------------------------------ */
/* --- first check for end-of-string or \= ligature --- */
if (*expression == '\000' /* nothing to check */
/* have a \= ligature */
|| mctx->isligature) goto end_of_job;
/* --- check leading char for escape --- */
if (isthischar(*expression, ESCAPE)) { /* expression is escaped */
/* so set flag accordingly */
gotescape = 1;
expression++;
} /* and bump past escape */
/* --- check (maybe next char) for brace --- */
if (isthischar(*expression, braces)) /* expression is braced */
/* so set flag accordingly */
gotbrace = 1;
if (gotescape && *expression == '.') /* \. matches any brace */
/* set flag */
gotbrace = 1;
/* --- check for TeX brace { or } --- */
if (gotbrace && isthischar(*expression, "{}")) /*expression has TeX brace*/
/* reset escape flag */
if (isescape) isescape = 2;
/* ------------------------------------------------------------
back to caller
------------------------------------------------------------ */
end_of_job:
if (mctx->msglevel >= 999 && mctx->msgfp != NULL) {
fprintf(mctx->msgfp, "isbrace> expression=%.8s, gotbrace=%d (mctx->isligature=%d)\n",
expression, gotbrace, mctx->isligature);
fflush(mctx->msgfp);
}
if (gotbrace && /* found a brace */
(isescape == 2 || /* escape irrelevant */
gotescape == isescape) /* un/escaped as requested */
) return (1);
/* return 1,0 accordingly */
return (0);
} /* --- end-of-function isbrace() --- */
/* ==========================================================================
* Function: preamble ( expression, size, subexpr )
* Purpose: parses $-terminated preamble, if present, at beginning
* of expression, re-setting size if necessary, and
* returning any other parameters besides size in subexpr.
* --------------------------------------------------------------------------
* Arguments: expression (I) char * to first char of null-terminated
* string containing LaTeX expression possibly
* preceded by $-terminated preamble
* size (I/O) int * containing 0-4 default font size,
* and returning size modified by first
* preamble parameter (or unchanged)
* subexpr(O) char * returning any remaining preamble
* parameters past size
* --------------------------------------------------------------------------
* Returns: ( char * ) ptr to first char past preamble in expression
* or NULL for any parsing error.
* --------------------------------------------------------------------------
* Notes: o size can be any number >=0. If preceded by + or -, it's
* interpreted as an increment to input size; otherwise
* it's interpreted as the size.
* o if subexpr is passed as NULL ptr, then returned expression
* ptr will have "flushed" and preamble parameters after size
* ======================================================================= */
/* --- entry point --- */
char *preamble(mimetex_ctx *mctx, char *expression, int *size, char *subexpr)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
char pretext[512], *prep = expression, /*pream from expression, ptr into it*/
/* preamble delimiters */
*dollar, *comma;
int prelen = 0, /* preamble length */
sizevalue = 0, /* value of size parameter */
isfontsize = 0, /*true if leading mctx->fontsize present*/
/*true to increment passed size arg*/
isdelta = 0;
/* ------------------------------------------------------------
initialization
------------------------------------------------------------ */
if (subexpr != NULL) /* caller passed us an address */
/* so init assuming no preamble */
*subexpr = '\000';
/* no input */
if (expression == NULL) goto end_of_job;
/* input is an empty string */
if (*expression == '\000') goto end_of_job;
/* ------------------------------------------------------------
process preamble if present
------------------------------------------------------------ */
/*process_preamble:*/
if ((dollar = strchr(expression, '$')) /* $ signals preceding preamble */
!= NULL) { /* found embedded $ */
if ((prelen = (int)(dollar - expression)) /*#chars in expression preceding $*/
> 0) { /* must have preamble preceding $ */
if (prelen < 65) { /* too long for a prefix */
/* local copy of preamble */
memcpy(pretext, expression, prelen);
/* null-terminated */
pretext[prelen] = '\000';
if (strchr(pretext, *(ESCAPE)) == NULL /*shouldn't be an escape in preamble*/
&& strchr(pretext, '{') == NULL) { /*shouldn't be a left{ in preamble*/
/* --- skip any leading whitespace --- */
/* start at beginning of preamble */
prep = pretext;
/* skip any leading white space */
skipwhite(prep);
/* --- check for embedded , or leading +/- (either signalling size) --- */
if (isthischar(*prep, "+-")) /* have leading + or - */
/* so use size value as increment */
isdelta = 1;
/* , signals leading size param */
comma = strchr(pretext, ',');
/* --- process leading size parameter if present --- */
if (comma != NULL /* size param explicitly signalled */
|| isdelta || isdigit(*prep)) { /* or inferred implicitly */
/* --- parse size parameter and reset size accordingly --- */
/*, becomes null, terminating size*/
if (comma != NULL) *comma = '\000';
/* convert size string to integer */
sizevalue = atoi(prep);
if (size != NULL) /* caller passed address for size */
/* so reset size */
*size = (isdelta ? *size + sizevalue : sizevalue);
/* --- finally, set flag and shift size parameter out of preamble --- */
/*set flag showing font size present*/
isfontsize = 1;
/*leading size param gone*/
if (comma != NULL) strcpy(pretext, comma + 1);
} /* --- end-of-if(comma!=NULL||etc) --- */
/* --- copy any preamble params following size to caller's subexpr --- */
if (comma != NULL || !isfontsize) /*preamb contains params past size*/
if (subexpr != NULL) /* caller passed us an address */
/*so return extra params to caller*/
strcpy(subexpr, pretext);
/* --- finally, set prep to shift preamble out of expression --- */
/* set prep past $ in expression */
prep = expression + prelen + 1;
} /* --- end-of-if(strchr(pretext,*ESCAPE)==NULL) --- */
} /* --- end-of-if(prelen<65) --- */
} /* --- end-of-if(prelen>0) --- */
else { /* $ is first char of expression */
/* number of $...$ pairs removed */
int ndollars = 0;
/* start at beginning of expression*/
prep = expression;
while (*prep == '$') { /* remove all matching $...$'s */
/* index of last char in expression*/
int explen = strlen(prep) - 1;
/* no $...$'s left to remove */
if (explen < 2) break;
/* unmatched $ */
if (prep[explen] != '$') break;
/* remove trailing $ */
prep[explen] = '\000';
/* and remove matching leading $ */