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 .
29 extern std::list
<Node
*> nodelist
;
35 #define ascii(x) OUString::createFromAscii(x)
36 #define rstartEl(x,y) do { if (m_rxDocumentHandler.is()) m_rxDocumentHandler->startElement(x,y); } while(0)
37 #define rendEl(x) do { if (m_rxDocumentHandler.is()) m_rxDocumentHandler->endElement(x); } while(0)
38 #define rchars(x) do { if (m_rxDocumentHandler.is()) m_rxDocumentHandler->characters(ascii(x)); } while(0)
39 #define runistr(x) do { if (m_rxDocumentHandler.is()) m_rxDocumentHandler->characters(OUString(x)); } while(0)
40 #define reucstr(x,y) do { if (m_rxDocumentHandler.is()) m_rxDocumentHandler->characters(OUString(x,y, RTL_TEXTENCODING_EUC_KR)); } while(0)
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 extern Node
*mainParse(const char *);
52 void Formula::makeMathML(Node
*res
)
58 fprintf(stderr
,"<math:math xmlns:math=\"http://www.w3.org/1998/Math/MathML\">\n");
60 padd(ascii("xmlns:math"), ascii("CDATA"), ascii("http://www.w3.org/1998/Math/MathML"));
61 rstartEl(ascii("math:math"), rList
);
63 rstartEl(ascii("math:semantics"), rList
);
66 makeLines( tmp
->child
);
70 fprintf(stderr
,"<math:semantics/>\n");
73 fprintf(stderr
,"</math:math>\n");
75 rendEl(ascii("math:semantics"));
76 rendEl(ascii("math:math"));
80 void Formula::makeLines(Node
*res
)
86 if( tmp
->child
->id
== ID_LINES
)
87 makeLines( tmp
->child
);
89 makeLine( tmp
->child
);
92 makeLine( tmp
->next
);
95 void Formula::makeLine(Node
*res
)
99 inds
; fprintf(stderr
,"<math:mrow>\n");
101 rstartEl(ascii("math:mrow"), rList
);
104 makeExprList( res
->child
);
106 inde
; fprintf(stderr
,"</math:mrow>\n");
108 rendEl(ascii("math:mrow"));
112 void Formula::makeExprList(Node
*res
)
115 Node
*tmp
= res
->child
;
118 if( tmp
->id
== ID_EXPRLIST
){
119 Node
*next
= tmp
->next
;
120 makeExprList( tmp
) ;
128 void Formula::makeExpr(Node
*res
)
131 Node
*tmp
= res
->child
;
138 fprintf(stderr
,"<math:mrow>\n");
140 rstartEl(ascii("math:mrow"), rList
);
148 inde
; fprintf(stderr
,"</math:mrow>\n");
150 rendEl(ascii("math:mrow"));
159 case ID_FRACTIONEXPR
:
163 case ID_DECORATIONEXPR
:
193 void Formula::makeIdentifier(Node
*res
)
197 if( !tmp
->value
) return;
202 fprintf(stderr
,"<math:mi>%s</math:mi>\n",tmp
->value
);
205 rstartEl(ascii("math:mi"), rList
);
207 rendEl(ascii("math:mi"));
214 rstartEl(ascii("math:mi"), rList
);
215 reucstr(tmp
->value
, strlen(tmp
->value
));
216 rendEl(ascii("math:mi"));
223 fprintf(stderr
,"<math:mi>%s</math:mi>\n",
224 getMathMLEntity(tmp
->value
).c_str());
227 rstartEl(ascii("math:mi"), rList
);
228 runistr(getMathMLEntity(tmp
->value
).c_str());
229 rendEl(ascii("math:mi"));
235 fprintf(stderr
,"<math:mn>%s</math:mn>\n",tmp
->value
);
238 rstartEl(ascii("math:mn"), rList
);
240 rendEl(ascii("math:mn"));
247 inds
; fprintf(stderr
,"<math:mo>%s</math:mo>\n",tmp
->value
); indo
;
249 rstartEl(ascii("math:mo"), rList
);
250 runistr(getMathMLEntity(tmp
->value
).c_str());
251 rendEl(ascii("math:mo"));
257 void Formula::makePrimary(Node
*res
)
262 if( tmp
->child
->id
== ID_PRIMARYEXPR
){
263 makePrimary(tmp
->child
);
266 makeIdentifier(tmp
->child
);
270 makeIdentifier(tmp
->next
);
274 void Formula::makeSubSup(Node
*res
)
281 if( res
->id
== ID_SUBEXPR
)
282 fprintf(stderr
,"<math:msub>\n");
283 else if( res
->id
== ID_SUPEXPR
)
284 fprintf(stderr
,"<math:msup>\n");
286 fprintf(stderr
,"<math:msubsup>\n");
288 if( res
->id
== ID_SUBEXPR
)
289 rstartEl(ascii("math:msub"), rList
);
290 else if( res
->id
== ID_SUPEXPR
)
291 rstartEl(ascii("math:msup"), rList
);
293 rstartEl(ascii("math:msubsup"), rList
);
297 if( res
->id
== ID_SUBSUPEXPR
) {
299 makeBlock(tmp
->next
);
300 makeBlock(tmp
->next
->next
);
309 if( res
->id
== ID_SUBEXPR
)
310 fprintf(stderr
,"</math:msub>\n");
311 else if( res
->id
== ID_SUPEXPR
)
312 fprintf(stderr
,"</math:msup>\n");
314 fprintf(stderr
,"</math:msubsup>\n");
316 if( res
->id
== ID_SUBEXPR
)
317 rendEl(ascii("math:msub"));
318 else if( res
->id
== ID_SUPEXPR
)
319 rendEl(ascii("math:msup"));
321 rendEl(ascii("math:msubsup"));
325 void Formula::makeFraction(Node
*res
)
332 fprintf(stderr
,"<math:mfrac>\n");
334 rstartEl(ascii("math:mfrac"), rList
);
340 fprintf(stderr
,"<math:mrow>\n");
342 rstartEl(ascii("math:mrow"), rList
);
345 if( res
->id
== ID_FRACTIONEXPR
)
352 fprintf(stderr
,"</math:mrow>\n");
354 fprintf(stderr
,"<math:mrow>\n");
356 rendEl(ascii("math:mrow"));
357 rstartEl(ascii("math:mrow"), rList
);
360 if( res
->id
== ID_FRACTIONEXPR
)
361 makeBlock(tmp
->next
);
363 makeExprList(tmp
->next
);
367 fprintf(stderr
,"</math:mrow>\n");
369 fprintf(stderr
,"</math:mfrac>\n");
371 rendEl(ascii("math:mrow"));
372 rendEl(ascii("math:mfrac"));
376 void Formula::makeDecoration(Node
*res
)
379 Node
*tmp
= res
->child
;
381 if( !strncmp(tmp
->value
,"under", 5) )
386 fprintf(stderr
,"<math:mover>\n");
388 fprintf(stderr
,"<math:munder>\n");
390 /* accent´Â ¾ðÁ¦ trueÀÌ°í, ¾ðÁ¦, falseÀÎÁö ¸ð¸£°Ú´Ù. */
392 padd(ascii("accent"),ascii("CDATA"),ascii("true"));
393 rstartEl(ascii("math:mover"), rList
);
396 padd(ascii("accentunder"),ascii("CDATA"),ascii("true"));
397 rstartEl(ascii("math:munder"), rList
);
402 makeBlock(tmp
->next
);
406 fprintf(stderr
,"<math:mo>%s</math:mo>\n",
407 getMathMLEntity(tmp
->value
).c_str());
410 rstartEl(ascii("math:mo"), rList
);
411 runistr(getMathMLEntity(tmp
->value
).c_str());
412 rendEl(ascii("math:mo"));
418 fprintf(stderr
,"</math:mover>\n");
420 fprintf(stderr
,"</math:munder>\n");
423 rendEl(ascii("math:mover"));
425 rendEl(ascii("math:munder"));
429 void Formula::makeRoot(Node
*res
)
435 if( tmp
->id
== ID_SQRTEXPR
)
436 fprintf(stderr
,"<math:msqrt>\n");
438 fprintf(stderr
,"<math:mroot>\n");
440 if( tmp
->id
== ID_SQRTEXPR
)
441 rstartEl(ascii("math:msqrt"), rList
);
443 rstartEl(ascii("math:mroot"), rList
);
446 if( tmp
->id
== ID_SQRTEXPR
){
447 makeBlock(tmp
->child
);
450 makeBracket(tmp
->child
);
451 makeBlock(tmp
->child
->next
);
456 if( tmp
->id
== ID_SQRTEXPR
)
457 fprintf(stderr
,"</math:msqrt>\n");
459 fprintf(stderr
,"</math:mroot>\n");
461 if( tmp
->id
== ID_SQRTEXPR
)
462 rendEl(ascii("math:msqrt"));
464 rendEl(ascii("math:mroot"));
467 // DVO: add space to avoid warning
468 void Formula::makeArrow(Node
* /*res*/)
471 void Formula::makeAccent(Node
*res
)
473 makeDecoration( res
);
475 void Formula::makeParenth(Node
*res
)
481 fprintf(stderr
,"<math:mrow>\n");
483 if( tmp
->id
== ID_PARENTH
){
484 fprintf(stderr
,"<math:mo>(</math:mo>\n");
487 fprintf(stderr
,"<math:mo>|</math:mo>\n");
489 fprintf(stderr
,"<math:mrow>\n");
491 rstartEl(ascii("math:mrow"), rList
);
492 rstartEl(ascii("math:mo"), rList
);
493 if( tmp
->id
== ID_PARENTH
)
497 rendEl(ascii("math:mo"));
498 rstartEl(ascii("math:mrow"), rList
);
502 makeExprList(tmp
->child
);
506 fprintf(stderr
,"</math:mrow>\n");
508 if( tmp
->id
== ID_PARENTH
)
509 fprintf(stderr
,"<math:mo>)</math:mo>\n");
511 fprintf(stderr
,"<math:mo>|</math:mo>\n");
514 fprintf(stderr
,"</math:mrow>\n");
516 rendEl(ascii("math:mrow"));
517 rstartEl(ascii("math:mo"), rList
);
518 if( tmp
->id
== ID_PARENTH
)
522 rendEl(ascii("math:mo"));
523 rendEl(ascii("math:mrow"));
527 void Formula::makeFence(Node
*res
)
529 Node
*tmp
= res
->child
;
532 fprintf(stderr
,"<math:mfenced open=\"%s\" close=\"%s\">\n",
533 getMathMLEntity(tmp
->value
).c_str(),
534 getMathMLEntity(tmp
->next
->next
->value
).c_str());
536 padd(ascii("open"), ascii("CDATA"),
537 OUString(getMathMLEntity(tmp
->value
).c_str()) );
538 padd(ascii("close"), ascii("CDATA"),
539 OUString(getMathMLEntity(tmp
->next
->next
->value
).c_str()) );
540 rstartEl(ascii("math:mfenced"), rList
);
544 makeExprList(tmp
->next
);
548 fprintf(stderr
,"</math:mfenced>\n");
550 rendEl(ascii("math:mfenced"));
554 void Formula::makeBracket(Node
*res
)
559 void Formula::makeBlock(Node
*res
)
563 fprintf(stderr
,"<math:mrow>\n");
565 rstartEl(ascii("math:mrow"), rList
);
569 makeExprList(res
->child
);
573 fprintf(stderr
,"</math:mrow>\n");
575 rendEl(ascii("math:mrow"));
579 // DVO: add space to avoid warning
580 void Formula::makeBegin(Node
* /*res*/)
584 // DVO: add space to avoid warning
585 void Formula::makeEnd(Node
* /*res*/)
595 // fprintf(stderr,"\n\n[BEFORE]\n[%s]\n",eq);
598 int idx
=a
.find(sal::static_int_cast
<char>(0xff));
600 //printf("idx = [%d]\n",idx);
602 if((idx
= a
.find(sal::static_int_cast
<char>(0xff),idx
+1)) < 0)
606 char *buf
= (char *)malloc(a
.length()+1);
609 for( i
= 0, j
=0 ; i
< a
.length() ; i
++){ // rtrim and ltrim 32 10 13
614 if( a
[i
] != 32 && a
[i
] != 10 && a
[i
] != 13){
621 for( i
= j
-1 ; i
>= 0 ; i
++ ){
622 if( buf
[i
] == 32 || buf
[i
] == 10 || buf
[i
] == 13 ){
628 // fprintf(stderr,"\n\n[RESULT]\n[%s]\n",a.c_str());
629 if( strlen(buf
) > 0 )
630 res
= mainParse( a
.c_str() );
636 res
= mainParse( eq
);
643 int count
= nodelist
.size();
644 for( int i
= 0 ; i
< count
; i
++ ){
645 tmpNode
= nodelist
.front();
646 nodelist
.pop_front();
655 int len
= strlen(eq
);
656 char *buf
= (char *)malloc(len
+1);
659 for( i
= 0, j
=0 ; i
< len
; i
++){ // rtrim and ltrim 32 10 13
664 if( eq
[i
] != 32 && eq
[i
] != 10 && eq
[i
] != 13){
671 for( i
= j
-1 ; i
>= 0 ; i
++ ){
672 if( buf
[i
] == 32 || buf
[i
] == 10 || buf
[i
] == 13 ){
678 if( strlen(buf
) > 0 )
685 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */