1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "grammar.hxx"
30 extern std::list
<Node
*> nodelist
;
36 #define rstartEl(x,y) do { if (m_rxDocumentHandler.is()) m_rxDocumentHandler->startElement(x,y); } while(false)
37 #define rendEl(x) do { if (m_rxDocumentHandler.is()) m_rxDocumentHandler->endElement(x); } while(false)
38 #define rchars(x) do { if (m_rxDocumentHandler.is()) m_rxDocumentHandler->characters(x); } while(false)
39 #define runistr(x) do { if (m_rxDocumentHandler.is()) m_rxDocumentHandler->characters(OUString(x)); } while(false)
40 #define reucstr(x,y) do { if (m_rxDocumentHandler.is()) m_rxDocumentHandler->characters(OUString(x,y, RTL_TEXTENCODING_EUC_KR)); } while(false)
41 #define padd(x,y,z) pList->addAttribute(x,y,z)
43 static int indent
= 0;
44 #define inds indent++; for(int i = 0 ; i < indent ; i++) fprintf(stderr," ")
45 #define inde for(int i = 0 ; i < indent ; i++) fprintf(stderr," "); indent--
46 #define indo indent--;
49 void Formula::makeMathML(Node
*res
)
55 fprintf(stderr
,"<math:math xmlns:math=\"http://www.w3.org/1998/Math/MathML\">\n");
57 padd("xmlns:math", "CDATA", "http://www.w3.org/1998/Math/MathML");
58 rstartEl("math:math", rList
);
60 rstartEl("math:semantics", rList
);
63 makeLines( tmp
->child
);
67 fprintf(stderr
,"<math:semantics/>\n");
70 fprintf(stderr
,"</math:math>\n");
72 rendEl("math:semantics");
77 void Formula::makeLines(Node
*res
)
83 if( tmp
->child
->id
== ID_LINES
)
84 makeLines( tmp
->child
);
86 makeLine( tmp
->child
);
89 makeLine( tmp
->next
);
92 void Formula::makeLine(Node
*res
)
96 inds
; fprintf(stderr
,"<math:mrow>\n");
98 rstartEl("math:mrow", rList
);
101 makeExprList( res
->child
);
103 inde
; fprintf(stderr
,"</math:mrow>\n");
109 void Formula::makeExprList(Node
*res
)
112 Node
*tmp
= res
->child
;
115 if( tmp
->id
== ID_EXPRLIST
){
116 Node
*next
= tmp
->next
;
117 makeExprList( tmp
) ;
125 void Formula::makeExpr(Node
*res
)
128 Node
*tmp
= res
->child
;
135 fprintf(stderr
,"<math:mrow>\n");
137 rstartEl("math:mrow", rList
);
145 inde
; fprintf(stderr
,"</math:mrow>\n");
156 case ID_FRACTIONEXPR
:
160 case ID_DECORATIONEXPR
:
189 void Formula::makeIdentifier(Node
*res
)
193 if( !tmp
->value
) return;
198 fprintf(stderr
,"<math:mi>%s</math:mi>\n",tmp
->value
);
201 rstartEl("math:mi", rList
);
202 rchars(OUString::createFromAscii(tmp
->value
));
210 rstartEl("math:mi", rList
);
211 reucstr(tmp
->value
, strlen(tmp
->value
));
219 fprintf(stderr
,"<math:mi>%s</math:mi>\n",
220 getMathMLEntity(tmp
->value
).c_str());
223 rstartEl("math:mi", rList
);
224 runistr(getMathMLEntity(tmp
->value
).c_str());
231 fprintf(stderr
,"<math:mn>%s</math:mn>\n",tmp
->value
);
234 rstartEl("math:mn", rList
);
235 rchars(OUString::createFromAscii(tmp
->value
));
243 inds
; fprintf(stderr
,"<math:mo>%s</math:mo>\n",tmp
->value
); indo
;
245 rstartEl("math:mo", rList
);
246 runistr(getMathMLEntity(tmp
->value
).c_str());
253 void Formula::makePrimary(Node
*res
)
258 if( tmp
->child
->id
== ID_PRIMARYEXPR
){
259 makePrimary(tmp
->child
);
262 makeIdentifier(tmp
->child
);
266 makeIdentifier(tmp
->next
);
270 void Formula::makeSubSup(Node
*res
)
277 if( res
->id
== ID_SUBEXPR
)
278 fprintf(stderr
,"<math:msub>\n");
279 else if( res
->id
== ID_SUPEXPR
)
280 fprintf(stderr
,"<math:msup>\n");
282 fprintf(stderr
,"<math:msubsup>\n");
284 if( res
->id
== ID_SUBEXPR
)
285 rstartEl("math:msub", rList
);
286 else if( res
->id
== ID_SUPEXPR
)
287 rstartEl("math:msup", rList
);
289 rstartEl("math:msubsup", rList
);
293 if( res
->id
== ID_SUBSUPEXPR
) {
295 makeBlock(tmp
->next
);
296 makeBlock(tmp
->next
->next
);
305 if( res
->id
== ID_SUBEXPR
)
306 fprintf(stderr
,"</math:msub>\n");
307 else if( res
->id
== ID_SUPEXPR
)
308 fprintf(stderr
,"</math:msup>\n");
310 fprintf(stderr
,"</math:msubsup>\n");
312 if( res
->id
== ID_SUBEXPR
)
314 else if( res
->id
== ID_SUPEXPR
)
317 rendEl("math:msubsup");
321 void Formula::makeFraction(Node
*res
)
328 fprintf(stderr
,"<math:mfrac>\n");
330 rstartEl("math:mfrac", rList
);
336 fprintf(stderr
,"<math:mrow>\n");
338 rstartEl("math:mrow", rList
);
341 if( res
->id
== ID_FRACTIONEXPR
)
348 fprintf(stderr
,"</math:mrow>\n");
350 fprintf(stderr
,"<math:mrow>\n");
353 rstartEl("math:mrow", rList
);
356 if( res
->id
== ID_FRACTIONEXPR
)
357 makeBlock(tmp
->next
);
359 makeExprList(tmp
->next
);
363 fprintf(stderr
,"</math:mrow>\n");
365 fprintf(stderr
,"</math:mfrac>\n");
368 rendEl("math:mfrac");
372 void Formula::makeDecoration(Node
*res
)
375 Node
*tmp
= res
->child
;
377 if( !strncmp(tmp
->value
,"under", 5) )
382 fprintf(stderr
,"<math:mover>\n");
384 fprintf(stderr
,"<math:munder>\n");
386 /* accent는 언제 true이고, 언제, false인지 모르겠다. */
388 padd("accent","CDATA","true");
389 rstartEl("math:mover", rList
);
392 padd("accentunder","CDATA","true");
393 rstartEl("math:munder", rList
);
398 makeBlock(tmp
->next
);
402 fprintf(stderr
,"<math:mo>%s</math:mo>\n",
403 getMathMLEntity(tmp
->value
).c_str());
406 rstartEl("math:mo", rList
);
407 runistr(getMathMLEntity(tmp
->value
).c_str());
414 fprintf(stderr
,"</math:mover>\n");
416 fprintf(stderr
,"</math:munder>\n");
419 rendEl("math:mover");
421 rendEl("math:munder");
425 void Formula::makeRoot(Node
*res
)
431 if( tmp
->id
== ID_SQRTEXPR
)
432 fprintf(stderr
,"<math:msqrt>\n");
434 fprintf(stderr
,"<math:mroot>\n");
436 if( tmp
->id
== ID_SQRTEXPR
)
437 rstartEl("math:msqrt", rList
);
439 rstartEl("math:mroot", rList
);
442 if( tmp
->id
== ID_SQRTEXPR
){
443 makeBlock(tmp
->child
);
446 makeBracket(tmp
->child
);
447 makeBlock(tmp
->child
->next
);
452 if( tmp
->id
== ID_SQRTEXPR
)
453 fprintf(stderr
,"</math:msqrt>\n");
455 fprintf(stderr
,"</math:mroot>\n");
457 if( tmp
->id
== ID_SQRTEXPR
)
458 rendEl("math:msqrt");
460 rendEl("math:mroot");
463 void Formula::makeAccent(Node
*res
)
465 makeDecoration( res
);
467 void Formula::makeParenth(Node
*res
)
473 fprintf(stderr
,"<math:mrow>\n");
475 if( tmp
->id
== ID_PARENTH
){
476 fprintf(stderr
,"<math:mo>(</math:mo>\n");
479 fprintf(stderr
,"<math:mo>|</math:mo>\n");
481 fprintf(stderr
,"<math:mrow>\n");
483 rstartEl("math:mrow", rList
);
484 rstartEl("math:mo", rList
);
485 if( tmp
->id
== ID_PARENTH
)
490 rstartEl("math:mrow", rList
);
494 makeExprList(tmp
->child
);
498 fprintf(stderr
,"</math:mrow>\n");
500 if( tmp
->id
== ID_PARENTH
)
501 fprintf(stderr
,"<math:mo>)</math:mo>\n");
503 fprintf(stderr
,"<math:mo>|</math:mo>\n");
506 fprintf(stderr
,"</math:mrow>\n");
509 rstartEl("math:mo", rList
);
510 if( tmp
->id
== ID_PARENTH
)
519 void Formula::makeFence(Node
*res
)
521 Node
*tmp
= res
->child
;
524 fprintf(stderr
,"<math:mfenced open=\"%s\" close=\"%s\">\n",
525 getMathMLEntity(tmp
->value
).c_str(),
526 getMathMLEntity(tmp
->next
->next
->value
).c_str());
528 padd("open", "CDATA",
529 OUString(getMathMLEntity(tmp
->value
).c_str()) );
530 padd("close", "CDATA",
531 OUString(getMathMLEntity(tmp
->next
->next
->value
).c_str()) );
532 rstartEl("math:mfenced", rList
);
536 makeExprList(tmp
->next
);
540 fprintf(stderr
,"</math:mfenced>\n");
542 rendEl("math:mfenced");
546 void Formula::makeBracket(Node
*res
)
551 void Formula::makeBlock(Node
*res
)
555 fprintf(stderr
,"<math:mrow>\n");
557 rstartEl("math:mrow", rList
);
561 makeExprList(res
->child
);
565 fprintf(stderr
,"</math:mrow>\n");
577 // fprintf(stderr,"\n\n[BEFORE]\n[%s]\n",eq);
580 int idx
=a
.find(sal::static_int_cast
<char>(0xff));
582 //printf("idx = [%d]\n",idx);
584 if((idx
= a
.find(sal::static_int_cast
<char>(0xff),idx
+1)) < 0)
588 char *buf
= static_cast<char *>(malloc(a
.length()+1));
591 for( i
= 0, j
=0 ; i
< a
.length() ; i
++){ // rtrim and ltrim 32 10 13
596 if( a
[i
] != 32 && a
[i
] != 10 && a
[i
] != 13){
603 for( i
= j
-1 ; i
>= 0 ; i
++ ){
604 if( buf
[i
] == 32 || buf
[i
] == 10 || buf
[i
] == 13 ){
610 // fprintf(stderr,"\n\n[RESULT]\n[%s]\n",a.c_str());
612 res
= mainParse( a
.c_str() );
618 res
= mainParse( eq
);
624 int count
= nodelist
.size();
625 for( int i
= 0 ; i
< count
; i
++ ){
626 const Node
*tmpNode
= nodelist
.front();
627 nodelist
.pop_front();
636 int len
= strlen(eq
);
637 char *buf
= static_cast<char *>(malloc(len
+1));
640 for( i
= 0, j
=0 ; i
< len
; i
++){ // rtrim and ltrim 32 10 13
645 if( eq
[i
] != 32 && eq
[i
] != 10 && eq
[i
] != 13){
652 for( i
= j
-1 ; i
>= 0 ; i
++ ){
653 if( buf
[i
] == 32 || buf
[i
] == 10 || buf
[i
] == 13 ){
666 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */