merged tag ooo/DEV300_m102
[LibreOffice.git] / hwpfilter / source / hwpeq.cpp
blob350a3cef0958124ec40d3b14793bdf187a2fa998
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
25 ************************************************************************/
27 //#include "stdafx.h"
29 #include <assert.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <ctype.h>
34 // DVO: always use standard headers:
35 #include <istream>
36 #include <sstream>
37 using namespace std;
39 #include "mzstring.h"
40 #include "hwpeq.h"
41 #include <sal/types.h>
42 //#define TEST
43 //#define DEBUG
46 /* @Man: hwp¼ö½ÄÀ» LaTeXÀ¸·Î ¹Ù²Ù±â */
47 #ifdef WIN32
48 # define ENDL "\r\n"
49 #else /* !WIN32 */
50 # define ENDL "\n"
51 #endif
53 #define WS " \t\r\n\v\f"
55 #define EQ_CASE 0x01 // case sensitive cmd
56 #define EQ_ENV 0x02 // equiv to latex environment
57 #define EQ_ATOP 0x04 // must revert order
59 #define IS_WS(ch) (strchr(WS, ch))
60 #define IS_BINARY(ch) (strchr("+-<=>", ch))
62 #ifdef WIN32
63 #define STRICMP stricmp
64 #else
65 #define STRICMP strcasecmp
66 #endif
68 // sub and sup scipt script status
69 enum { SCRIPT_NONE, SCRIPT_SUB, SCRIPT_SUP, SCRIPT_ALL};
71 static int eq_word(MzString& outs, istream *strm, int script = SCRIPT_NONE);
72 static bool eq_sentence(MzString& outs, istream *strm, const char *end = 0);
74 struct hwpeq {
75 const char *key; // hwp math keyword
76 const char *latex; // corresponding latex keywork
77 int nargs; // # of argument
78 unsigned char flag; // case sensitive?
81 static hwpeq eq_tbl[] = {
82 { "!=", "\\equiv ", 0, 0 },
83 { "#", "\\\\", 0, 0 },
84 { "+-", "\\pm ", 0, 0 },
85 { "-+", "\\mp ", 0, 0 },
86 { "<=", "\\leq ", 0, 0 },
87 { "==", "\\equiv ", 0, 0 },
88 { ">=", "\\geq ", 0, 0 },
89 { "Pr", NULL, 0, 0 },
90 { "^", "^", 1, 0 },
91 { "_", "_", 1, 0 },
92 { "`", "\\;", 0, 0 },
93 { "acute", NULL, 1, 0 },
94 { "aleph", NULL, 0, 0 },
95 { "alpha", NULL, 0, EQ_CASE },
96 { "amalg", NULL, 0, 0 },
97 { "and", NULL, 0, 0 },
98 { "angle", NULL, 0, 0 },
99 { "angstrom", NULL, 0, 0 },
100 { "approx", NULL, 0, 0 },
101 { "arc", NULL, 0, 0 },
102 { "arccos", NULL, 0, 0 },
103 { "arch", NULL, 0, 0 },
104 { "arcsin", NULL, 0, 0 },
105 { "arctan", NULL, 0, 0 },
106 { "arg", NULL, 0, 0 },
107 { "assert", "\\vdash", 0, 0 },
108 { "ast", NULL, 0, 0 },
109 { "asymp", NULL, 0, 0 },
110 { "atop", NULL, 1, EQ_ATOP },
111 { "backslash", NULL, 0, 0 },
112 { "bar", NULL, 1, 0 },
113 { "because", NULL, 0, 0 },
114 { "beta", NULL, 0, EQ_CASE },
115 { "big", NULL, 0, EQ_CASE },
116 { "bigcap", NULL, 0, 0 },
117 { "bigcirc", NULL, 0, 0 },
118 { "bigcup", NULL, 0, 0 },
119 { "bigg", NULL, 0, EQ_CASE },
120 { "bigodiv", NULL, 0, 0 },
121 { "bigodot", NULL, 0, 0 },
122 { "bigominus", NULL, 0, 0 },
123 { "bigoplus", NULL, 0, 0 },
124 { "bigotimes", NULL, 0, 0 },
125 { "bigsqcap", NULL, 0, 0 },
126 { "bigsqcup", NULL, 0, 0 },
127 { "biguplus", NULL, 0, 0 },
128 { "bigvee", NULL, 0, 0 },
129 { "bigwedge", NULL, 0, 0 },
130 { "binom", NULL, 2, 0 },
131 { "bmatrix", NULL, 0, EQ_ENV },
132 { "bold", NULL, 0, 0 },
133 { "bot", NULL, 0, 0 },
134 { "breve", NULL, 1, 0 },
135 { "buildrel", NULL, 0, 0 }, // LATER
136 { "bullet", NULL, 0, 0 },
137 { "cap", NULL, 0, 0 },
138 { "cases", NULL, 0, EQ_ENV },
139 { "ccol", NULL, 0, 0 }, /* ¼¼·Î·Î °¡¿îµ¥ */
140 { "cdot", NULL, 0, 0 },
141 { "cdots", NULL, 0, 0 },
142 { "check", NULL, 1, 0 },
143 { "chi", NULL, 0, EQ_CASE },
144 { "choose", NULL, 0, EQ_ATOP },
145 { "circ", NULL, 0, 0 },
146 { "col", NULL, 0, 0 }, // LATER
147 { "cong", NULL, 0, 0 },
148 { "coprod", NULL, 0, 0 },
149 { "cos", NULL, 0, 0 },
150 { "cosec", NULL, 0, 0 },
151 { "cosh", NULL, 0, 0 },
152 { "cot", NULL, 0, 0 },
153 { "coth", NULL, 0, 0 },
154 { "cpile", NULL, 0, 0 }, // LATER
155 { "csc", NULL, 0, 0 },
156 { "cup", NULL, 0, 0 },
157 { "dagger", NULL, 0, 0 },
158 { "dashv", NULL, 0, 0 },
159 { "ddagger", NULL, 0, 0 },
160 { "ddot", NULL, 1, 0 },
161 { "ddots", NULL, 0, 0 },
162 { "def", NULL, 0, 0 },
163 { "deg", NULL, 0, 0 },
164 { "del", NULL, 0, 0 },
165 { "delta", NULL, 0, EQ_CASE },
166 { "diamond", NULL, 0, 0 },
167 { "dim", NULL, 0, 0 },
168 { "div", NULL, 0, 0 },
169 { "divide", NULL, 0, 0 },
170 { "dline", NULL, 0, 0 },
171 { "dmatrix", NULL, 0, EQ_ENV },
172 { "dot", NULL, 1, 0 },
173 { "doteq", NULL, 0, 0 },
174 { "dotsaxis", NULL, 0, 0 },
175 { "dotsdiag", NULL, 0, 0 },
176 { "dotslow", "\\ldots", 0, 0 },
177 { "dotsvert", "\\vdots", 0, 0 },
178 { "downarrow", NULL, 0, EQ_CASE },
179 { "dsum", "+", 0, 0 },
180 { "dyad", NULL, 0, 0 }, // LATER
181 { "ell", NULL, 0, 0 },
182 { "emptyset", NULL, 0, 0 },
183 { "epsilon", NULL, 0, EQ_CASE },
184 { "eqalign", NULL, 0, EQ_ENV },
185 { "equiv", NULL, 0, 0 },
186 { "eta", NULL, 0, EQ_CASE },
187 { "exarrow", NULL, 0, 0 },
188 { "exist", "\\exists", 0, 0 },
189 { "exists", NULL, 0, 0 },
190 { "exp", NULL, 0, EQ_CASE },
191 { "for", NULL, 0, 0 },
192 { "forall", NULL, 0, 0 },
193 { "from", "_", 1, 0 },
194 { "gamma", NULL, 0, EQ_CASE },
195 { "gcd", NULL, 0, 0 },
196 { "ge", "\\geq", 0, 0 },
197 { "geq", NULL, 0, 0 },
198 { "ggg", NULL, 0, 0 },
199 { "grad", NULL, 0, 0 },
200 { "grave", NULL, 1, 0 },
201 { "hat", "\\widehat", 1, 0 },
202 { "hbar", NULL, 0, 0 },
203 { "hom", NULL, 0, 0 },
204 { "hookleft", NULL, 0, 0 },
205 { "hookright", NULL, 0, 0 },
206 { "identical", NULL, 0, 0 }, // LATER
207 { "if", NULL, 0, 0 },
208 { "imag", NULL, 0, 0 },
209 { "image", NULL, 0, 0 },
210 { "imath", NULL, 0, 0 },
211 { "in", NULL, 0, 0 },
212 { "inf", "\\infty", 0, 0 },
213 { "infinity", "\\infty", 0, 0 },
214 { "infty", NULL, 0, 0 },
215 { "int", NULL, 0, 0 },
216 { "integral", "\\int", 0, 0 },
217 { "inter", "\\bigcap", 0, 0 },
218 { "iota", NULL, 0, EQ_CASE },
219 { "iso", NULL, 0, 0 }, // ams
220 { "it", NULL, 0, 0 },
221 { "jmath", NULL, 0, 0 },
222 { "kappa", NULL, 0, EQ_CASE },
223 { "ker", NULL, 0, 0 },
224 { "lambda", NULL, 0, EQ_CASE },
225 { "land", NULL, 0, 0 }, // LATER
226 { "langle", NULL, 0, 0 },
227 { "larrow", "\\leftarrow", 0, EQ_CASE },
228 { "lbrace", NULL, 0, 0 },
229 { "lbrack", "[", 0, 0 },
230 { "lceil", NULL, 0, 0 },
231 { "lcol", NULL, 0, 0 }, // LATER
232 { "ldots", NULL, 0, 0 },
233 { "le", NULL, 0, 0 },
234 { "left", NULL, 0, 0 },
235 { "leftarrow", NULL, 0, EQ_CASE },
236 { "leq", NULL, 0, 0 },
237 { "lfloor", NULL, 0, 0 },
238 { "lg", NULL, 0, 0 },
239 { "lim", NULL, 0, EQ_CASE },
240 { "line", "\\vert", 0, 0 },
241 { "liter", "\\ell", 0, 0 },
242 { "lll", NULL, 0, 0 }, // ams
243 { "ln", NULL, 0, 0 },
244 { "log", NULL, 0, 0 },
245 { "lor", "\\vee", 0, 0 },
246 { "lparen", "(", 0, 0 },
247 { "lpile", NULL, 0, 0 }, // LATER
248 { "lrarrow", "\\leftrightarrow", 0, EQ_CASE },
249 { "lrharpoons", "\\leftrightharpoons",0, 0 },
250 { "mapsto", NULL, 0, 0 },
251 { "massert", "\\dashv", 0, 0 },
252 { "matrix", NULL, 0, EQ_ENV },
253 { "max", NULL, 0, 0 },
254 { "mho", NULL, 0, 0 }, // ams
255 { "min", NULL, 0, 0 },
256 { "minusplus", NULL, 0, 0 },
257 { "mit", "", 0, 0 }, // font
258 { "mod", "\\bmod", 0, 0 },
259 { "models", NULL, 0, 0 },
260 { "msangle", NULL, 0, 0 }, // LATER
261 { "mu", NULL, 0, EQ_CASE },
262 { "nabla", NULL, 0, 0 },
263 { "ne", NULL, 0, 0 },
264 { "nearrow", NULL, 0, 0 },
265 { "neg", NULL, 0, 0 },
266 { "neq", NULL, 0, 0 },
267 { "nequiv", NULL, 0, 0 },
268 { "ni", NULL, 0, 0 },
269 { "not", NULL, 0, 0 },
270 { "notin", NULL, 0, 0 },
271 { "nu", NULL, 0, EQ_CASE },
272 { "nwarrow", NULL, 0, 0 },
273 { "odiv", NULL, 0, 0 },
274 { "odot", NULL, 0, 0 },
275 { "oint", NULL, 0, 0 },
276 { "omega", NULL, 0, EQ_CASE },
277 { "omicron", NULL, 0, EQ_CASE },
278 { "ominus", NULL, 0, 0 },
279 { "oplus", NULL, 0, 0 },
280 { "or ", NULL, 0, 0 },
281 { "oslash", NULL, 0, 0 },
282 { "otimes", NULL, 0, 0 },
283 { "over", NULL, 1, EQ_ATOP },
284 { "overline", NULL, 1, 0 },
285 { "owns", "\\ni", 0, 0 },
286 { "parallel", NULL, 0, 0 },
287 { "partial", NULL, 0, 0 },
288 { "phantom", NULL, 0, 0 },
289 { "phi", NULL, 0, EQ_CASE },
290 { "pi", NULL, 0, EQ_CASE },
291 { "pile", NULL, 0, 0 }, // LATER
292 { "plusminus", "\\pm", 0, 0 },
293 { "pmatrix", NULL, 0, EQ_ENV },
294 { "prec", NULL, 0, 0 },
295 { "prep", NULL, 0, 0 },
296 { "prime", NULL, 0, 0 },
297 { "prod", NULL, 0, 0 },
298 { "propto", NULL, 0, 0 },
299 { "psi", NULL, 0, EQ_CASE },
300 { "rangle", NULL, 0, 0 },
301 { "rarrow", "\\rightarrow", 0, EQ_CASE },
302 { "rbrace", "]", 0, 0 },
303 { "rbrace", NULL, 0, 0 },
304 { "rceil", NULL, 0, 0 },
305 { "rcol", NULL, 0, 0 }, // LATER
306 { "real", "\\Re", 0, 0 },
307 { "reimage", NULL, 0, 0 },
308 { "rel", NULL, 0, 0 },
309 { "rfloor", NULL, 0, 0 },
310 { "rho", NULL, 0, EQ_CASE },
311 { "right", NULL, 0, 0 },
312 { "rightarrow", NULL, 0, EQ_CASE },
313 { "rlharpoons", NULL, 0, 0 },
314 { "rm", NULL, 0, 0 },
315 { "root", "\\sqrt", 1, 0 },
316 { "rparen", ")", 0, 0 },
317 { "rpile", NULL, 0, 0 }, // LATER
318 { "rtangle", NULL, 0, 0 },
319 { "sangle", NULL, 0, 0 },
320 { "scale", NULL, 0, 0 },
321 { "searrow", NULL, 0, 0 },
322 { "sec", NULL, 0, 0 },
323 { "sigma", NULL, 0, EQ_CASE },
324 { "sim", NULL, 0, 0 },
325 { "simeq", NULL, 0, 0 },
326 { "sin", NULL, 0, 0 },
327 { "sinh", NULL, 0, 0 },
328 { "slash", NULL, 0, 0 },
329 { "smallint", NULL, 0, 0 },
330 { "smallinter", NULL, 0, 0 },
331 { "smalloint", NULL, 0, 0 },
332 { "smallprod", NULL, 0, 0 },
333 { "smallsum", NULL, 0, 0 },
334 { "smallunion", NULL, 0, 0 },
335 { "smcoprod", NULL, 0, 0 },
336 { "sqcap", NULL, 0, 0 },
337 { "sqcup", NULL, 0, 0 },
338 { "sqrt", NULL, 1, 0 },
339 { "sqsubset", NULL, 0, 0 },
340 { "sqsubseteq", NULL, 0, 0 },
341 { "sqsupset", NULL, 0, 0 },
342 { "sqsupseteq", NULL, 0, 0 },
343 { "star", NULL, 0, 0 },
344 { "sub", "_", 0, 0 },
345 { "subset", NULL, 0, 0 },
346 { "subseteq", NULL, 0, 0 },
347 { "succ", NULL, 0, 0 },
348 { "sum", NULL, 0, 0 },
349 { "sup", "^", 0, 0 },
350 { "superset", NULL, 0, 0 },
351 { "supset", NULL, 0, 0 },
352 { "supseteq", NULL, 0, 0 },
353 { "swarrow", NULL, 0, 0 },
354 { "tan", NULL, 0, 0 },
355 { "tanh", NULL, 0, 0 },
356 { "tau", NULL, 0, EQ_CASE },
357 { "therefore", NULL, 0, 0 },
358 { "theta", NULL, 0, EQ_CASE },
359 { "tilde", "\\widetilde", 1, 0 },
360 { "times", NULL, 0, 0 },
361 { "to", "^", 1, 0 },
362 { "top", NULL, 0, 0 },
363 { "triangle", NULL, 0, 0 },
364 { "triangled", NULL, 0, 0 },
365 { "trianglel", NULL, 0, 0 },
366 { "triangler", NULL, 0, 0 },
367 { "triangleu", NULL, 0, 0 },
368 { "udarrow", "\\updownarrow",0, EQ_CASE },
369 { "under", "\\underline", 1, 0 },
370 { "underline", "\\underline", 1, 0 },
371 { "union", "\\bigcup", 0, 0 },
372 { "uparrow", NULL, 0, EQ_CASE },
373 { "uplus", NULL, 0, 0 },
374 { "upsilon", NULL, 0, EQ_CASE },
375 { "varepsilon", NULL, 0, 0 },
376 { "varphi", NULL, 0, 0 },
377 { "varpi", NULL, 0, 0 },
378 { "varrho", NULL, 0, 0 },
379 { "varsigma", NULL, 0, 0 },
380 { "vartheta", NULL, 0, 0 },
381 { "varupsilon", NULL, 0, 0 },
382 { "vdash", NULL, 0, 0 },
383 { "vdots", NULL, 0, 0 },
384 { "vec", NULL, 1, 0 },
385 { "vee", NULL, 0, 0 },
386 { "vert", NULL, 0, 0 },
387 { "wedge", NULL, 0, 0 },
388 { "wp", NULL, 0, 0 },
389 { "xi", NULL, 0, EQ_CASE },
390 { "xor", NULL, 0, 0 },
391 { "zeta", NULL, 0, EQ_CASE }
394 static hwpeq *lookup_eqn(char *str)
396 static int eqCount = sizeof(eq_tbl) / sizeof(eq_tbl[0]);
397 int m, k, l = 0, r = eqCount;
398 hwpeq *result = 0;
400 while( l < r ) {
401 m = (l + r) / 2;
402 k = strcmp(eq_tbl[m].key, str);
403 if( k == 0 ) {
404 result = eq_tbl + m;;
405 break;
407 else if( k < 0 )
408 l = m + 1;
409 else
410 r = m;
412 return result;
415 /* ùÀÚ¸¸ ´ë¹®ÀÚÀ̰ųª ÀüºÎ ´ë¹®ÀÚ¸é ¼Ò¹®ÀÚ·Î ¹Ù²Û´Ù. */
417 static char *make_keyword( char *keyword, const char *token)
419 assert(keyword);
420 char *ptr;
421 int result = true, len = strlen(token);
423 if( 255 < len )
424 strncpy(keyword, token, 255);
425 else
426 strcpy(keyword, token);
428 if( (token[0] & 0x80) || islower(token[0]) ||
429 strlen(token) < 2 )
430 return keyword;
432 int capital = isupper(keyword[1]);
433 for( ptr = keyword + 2; *ptr && result; ptr++ )
434 if( (*ptr & 0x80) ||
435 (!capital && isupper(*ptr)) ||
436 (capital && islower(*ptr)) )
437 result = false;
439 if( result ) {
440 ptr = keyword;
441 while( *ptr ) {
442 if( isupper(*ptr) )
443 *ptr = sal::static_int_cast<char>(tolower(*ptr));
444 ptr++;
447 return keyword;
450 // token reading funtion
451 struct eq_stack {
452 MzString white;
453 MzString token;
454 istream *strm;
456 eq_stack() { strm = 0; };
457 bool state(istream *s) {
458 if( strm != s) { white = 0; token = 0; }
459 return token.length() != 0;
463 static eq_stack *stk = 0;
465 void push_token(MzString &white, MzString &token, istream *strm)
467 // one time stack
468 assert(stk->state(strm) == false);
470 stk->white = white;
471 stk->token = token;
472 stk->strm = strm;
475 /* ÀÐÀº ÅäÅ«ÀÇ ±æÀ̸¦ ¹ÝȯÇÑ´Ù. */
476 /* control char, control sequence, binary sequence,
477 alphabet string, sigle character */
478 static int next_token(MzString &white, MzString &token, istream *strm)
480 register int ch = 0;
482 if( stk->state(strm) ) {
483 white = stk->white;
484 token = stk->token;
485 stk->token = 0;
486 stk->white = 0;
487 return token.length();
490 token = 0;
491 white = 0;
492 if( !strm->good() || (ch = strm->get()) == EOF )
493 return 0;
495 // read preceeding ws
496 if( IS_WS(ch) ) {
497 do white << (char) ch;
498 while( IS_WS(ch = strm->get()) );
501 if( ch == '\\' || ch & 0x80 || isalpha(ch) ) {
502 if( ch == '\\' ) {
503 token << (char) ch;
504 ch = strm->get();
506 do {
507 token << (char) ch;
508 ch = strm->get();
509 } while( ch != EOF && (ch & 0x80 || isalpha(ch)) ) ;
510 strm->putback(sal::static_int_cast<char>(ch));
511 /* sub, sub, over, atop Ư¼ö ó¸®
512 ±× ÀÌÀ¯´Â next_state()¿¡ ¿µÇâÀ» ¹ÌÄ¡±â ¶§¹®ÀÌ´Ù.
514 if( !STRICMP("sub", token) || !STRICMP("from", token) ||
515 !STRICMP("sup", token) || !STRICMP("to", token) ||
516 !STRICMP("over", token) || !STRICMP("atop", token) ||
517 !STRICMP("left", token) || !STRICMP("right", token) )
519 char buf[256];
520 make_keyword(buf, token);
521 token = buf;
523 if( !token.compare("sub") || !token.compare("from") )
524 token = "_";
525 if( !token.compare("sup") || !token.compare("to") )
526 token = "^";
528 else if( IS_BINARY(ch) ) {
529 do token << (char) ch;
530 while( IS_BINARY(ch = strm->get()) );
531 strm->putback(sal::static_int_cast<char>(ch));
533 else if( isdigit(ch) ) {
534 do token << (char) ch;
535 while( isdigit(ch = strm->get()) );
536 strm->putback(sal::static_int_cast<char>(ch));
538 else
539 token << (char) ch;
540 // cout << "WHITE : " << "|" << white.c_str() << "|" << endl ;
541 // cout << "TOKEN : " << "|" << token.c_str() << "|" << endl ;
542 return token.length();
545 static int read_white_space(MzString& outs, istream *strm)
547 int ch, result;
549 if( stk->state(strm) ) {
550 outs << stk->white;
551 stk->white = 0;
552 result = stk->token[0];
554 else {
555 while( IS_WS(ch = strm->get()) )
556 outs << (char )ch;
557 strm->putback(sal::static_int_cast<char>(ch));
558 result = ch;
560 return result;
563 /* Àμö°¡ ÇÊ¿äÇÏÁö ¾ÊÀº °æ¿ì °¢ Ç׸ñ°£ÀÇ ±¸ºÐÀº space¿Í brace
564 sqrt {ab}c = sqrt{ab} c
565 (, }´Â grouping
566 ^, _ ´Â ¾ÕµÚ·Î °áÇÕÇÑ´Ù.
568 sqrt µî°ú °°ÀÌ Àμö°¡ ÀÖ´Â Çü½Ä Á¤¸®
569 sqrt a -> sqrt{a}
570 sqrt {a} -> sqrt{a}
571 1 ÀÌ»óÀÇ Àμö°¡ ÀÖ´Â °æ¿ì Àμöµé°£ÀÇ ¿ª¹éÀº ¾ø¾Ø´Ù.
572 \frac a b -> frac{a}{b}
573 overÀÇ Çü½Ä Á¤¸®
574 a over b -> {a}over{b}
577 static int eq_word(MzString& outs, istream *strm, int status)
579 MzString token, white, state;
580 int ch, result, nargs;
581 char keyword[256];
582 hwpeq *eq;
583 #ifdef DEBUG
584 static int slevel = 0;
586 slevel++;
587 #endif
588 next_token(white, token, strm);
589 if (token.length() <= 0)
590 return 0;
591 result = token[0];
593 if( token.compare("{") == 0 ) {
594 state << white << token;
595 eq_sentence(state, strm, "}");
597 else if( token.compare("left") == 0 ) {
598 state << white << token;
599 next_token(white, token, strm);
600 state << white << token;
602 eq_sentence(state, strm, "right");
604 next_token(white, token, strm);
605 state << white << token;
607 else {
608 /* Á¤»óÀûÀÎ token */
609 int script_status = SCRIPT_NONE;
610 while( 1 ) {
611 state << white << token;
612 make_keyword(keyword, token);
613 if( token[0] == '^' )
614 script_status |= SCRIPT_SUP;
615 else if( token[0] == '_' )
616 script_status |= SCRIPT_SUB;
617 else
618 script_status = SCRIPT_NONE;
620 if( 0 != (eq = lookup_eqn(keyword)) ) {
621 nargs = eq->nargs;
622 while( nargs-- ) {
623 ch = read_white_space(state, strm);
624 if( ch != '{' ) state << '{';
625 eq_word(state, strm, script_status);
626 if( ch != '{' ) state << '}';
630 if( !next_token(white, token, strm) )
631 break;
632 // end loop and restart with this
633 if( (token[0] == '^' && status && !(status & SCRIPT_SUP)) ||
634 (token[0] == '_' && status && !(status & SCRIPT_SUB)) ||
635 strcmp("over", token) == 0 || strcmp("atop", token) == 0 ||
636 strchr("{}#&`", token[0]) ||
637 (!strchr("^_", token[0]) && white.length()) ) {
638 push_token(white, token, strm);
639 break;
643 outs << state;
644 #ifdef DEBUG
645 //if( slevel == 1 )
646 // cout << "LEVEL[" << slevel << "]" << state << "\n";
647 slevel--;
648 #endif
649 return result;
652 static bool eq_sentence(MzString& outs, istream *strm, const char *end)
654 MzString state;
655 MzString white, token;
656 bool multiline = false;
658 read_white_space(outs, strm);
659 while( eq_word(state, strm) ) {
660 if( !next_token(white, token, strm) ||
661 (end && strcmp(token.c_str(), end) == 0) )
663 state << white << token;
664 break;
666 push_token(white, token, strm);
667 if( !token.compare("atop") || !token.compare("over") )
668 outs << '{' << state << '}';
669 else {
670 if( !token.compare("#") )
671 multiline = true;
672 outs << state;
674 state = 0;
675 read_white_space(outs, strm);
677 outs << state;
678 return multiline;
681 static char eq2ltxconv(MzString& sstr, istream *strm, const char *sentinel)
683 MzString white, token;
684 char key[20];
685 int ch, result;
686 hwpeq *eq = 0;
688 while( 0 != (result = next_token(white, token, strm)) ) {
689 if( sentinel && (result == 1) && strchr(sentinel, token[0]) )
690 break;
691 make_keyword(key, token);
692 if( (eq = lookup_eqn(key)) != 0 ) {
693 if( eq->latex )
694 strcpy(key, eq->latex);
695 else {
696 key[0] = '\\';
697 strcpy(key + 1, eq->key);
699 if( (eq->flag & EQ_CASE) && isupper(token[0]) )
700 key[1] = sal::static_int_cast<char>(toupper(key[1]));
701 token = key;
704 if( token[0] == '{' ) { // grouping
705 sstr << white << token;
706 eq2ltxconv(sstr, strm, "}");
707 sstr << '}';
709 else if( eq && (eq->flag & EQ_ENV) ) {
710 next_token(white, token, strm);
711 if( token[0] != '{' ) return 0;
712 sstr << "\\begin" << "{" << eq->key << "}" << ENDL ;
713 eq2ltxconv(sstr, strm, "}");
714 if( sstr[sstr.length() - 1] != '\n' )
715 sstr << ENDL ;
716 sstr << "\\end" << "{" << eq->key << "}" << ENDL ;
718 else if( eq && (eq->flag & EQ_ATOP) ) {
719 if( sstr.length() == 0 )
720 sstr << '{';
721 else {
722 int pos = sstr.rfind('}');
723 if( 0 < pos)
724 sstr.replace(pos, ' ');
726 sstr << token;
727 while( (ch = strm->get()) != EOF && IS_WS(ch) )
728 sstr << (char)ch;
729 if( ch != '{' )
730 sstr << "{}";
731 else {
732 eq2ltxconv(sstr, strm, "}");
733 sstr << '}';
736 else
737 sstr << white << token;
739 return token[0];
742 void eq2latex(MzString& outs, char *s)
744 assert(s);
745 if( stk == 0 )
746 stk = new eq_stack;
748 MzString tstr;
750 istringstream tstrm(s);
751 bool eqnarray = eq_sentence(tstr, &tstrm);
752 #ifdef TEST
753 cout << "Step1: " << endl << tstr.c_str() << endl;
754 #endif
755 istringstream strm(tstr.c_str());
757 if( eqnarray )
758 outs << "\\begin{array}{rllll}" << ENDL;
759 eq2ltxconv(outs, &strm, 0);
760 outs << ENDL;
761 if( eqnarray )
762 outs << "\\end{array}" << ENDL;
763 delete stk;
764 stk = 0;