1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
38 extern std::list
<Node
*> nodelist
;
44 #define ascii(x) OUString::createFromAscii(x)
45 #define rstartEl(x,y) do { if (m_rxDocumentHandler.is()) m_rxDocumentHandler->startElement(x,y); } while(0)
46 #define rendEl(x) do { if (m_rxDocumentHandler.is()) m_rxDocumentHandler->endElement(x); } while(0)
47 #define rchars(x) do { if (m_rxDocumentHandler.is()) m_rxDocumentHandler->characters(ascii(x)); } while(0)
48 #define runistr(x) do { if (m_rxDocumentHandler.is()) m_rxDocumentHandler->characters(OUString(x)); } while(0)
49 #define reucstr(x,y) do { if (m_rxDocumentHandler.is()) m_rxDocumentHandler->characters(OUString(x,y, RTL_TEXTENCODING_EUC_KR)); } while(0)
50 #define padd(x,y,z) pList->addAttribute(x,y,z)
52 static int indent
= 0;
53 #define inds indent++; for(int i = 0 ; i < indent ; i++) fprintf(stderr," ")
54 #define inde for(int i = 0 ; i < indent ; i++) fprintf(stderr," "); indent--
55 #define indo indent--;
58 extern Node
*mainParse(const char *);
61 void Formula::makeMathML(Node
*res
)
67 fprintf(stderr
,"<math:math xmlns:math=\"http://www.w3.org/1998/Math/MathML\">\n");
69 padd(ascii("xmlns:math"), ascii("CDATA"), ascii("http://www.w3.org/1998/Math/MathML"));
70 rstartEl(ascii("math:math"), rList
);
72 rstartEl(ascii("math:semantics"), rList
);
75 makeLines( tmp
->child
);
79 fprintf(stderr
,"<math:semantics/>\n");
82 fprintf(stderr
,"</math:math>\n");
84 rendEl(ascii("math:semantics"));
85 rendEl(ascii("math:math"));
89 void Formula::makeLines(Node
*res
)
95 if( tmp
->child
->id
== ID_LINES
)
96 makeLines( tmp
->child
);
98 makeLine( tmp
->child
);
101 makeLine( tmp
->next
);
104 void Formula::makeLine(Node
*res
)
108 inds
; fprintf(stderr
,"<math:mrow>\n");
110 rstartEl(ascii("math:mrow"), rList
);
113 makeExprList( res
->child
);
115 inde
; fprintf(stderr
,"</math:mrow>\n");
117 rendEl(ascii("math:mrow"));
121 void Formula::makeExprList(Node
*res
)
124 Node
*tmp
= res
->child
;
127 if( tmp
->id
== ID_EXPRLIST
){
128 Node
*next
= tmp
->next
;
129 makeExprList( tmp
) ;
137 void Formula::makeExpr(Node
*res
)
140 Node
*tmp
= res
->child
;
147 fprintf(stderr
,"<math:mrow>\n");
149 rstartEl(ascii("math:mrow"), rList
);
157 inde
; fprintf(stderr
,"</math:mrow>\n");
159 rendEl(ascii("math:mrow"));
168 case ID_FRACTIONEXPR
:
172 case ID_DECORATIONEXPR
:
202 void Formula::makeIdentifier(Node
*res
)
206 if( !tmp
->value
) return;
211 fprintf(stderr
,"<math:mi>%s</math:mi>\n",tmp
->value
);
214 rstartEl(ascii("math:mi"), rList
);
216 rendEl(ascii("math:mi"));
223 rstartEl(ascii("math:mi"), rList
);
224 reucstr(tmp
->value
, strlen(tmp
->value
));
225 rendEl(ascii("math:mi"));
232 fprintf(stderr
,"<math:mi>%s</math:mi>\n",
233 getMathMLEntity(tmp
->value
).c_str());
236 rstartEl(ascii("math:mi"), rList
);
237 runistr(getMathMLEntity(tmp
->value
).c_str());
238 rendEl(ascii("math:mi"));
244 fprintf(stderr
,"<math:mn>%s</math:mn>\n",tmp
->value
);
247 rstartEl(ascii("math:mn"), rList
);
249 rendEl(ascii("math:mn"));
256 inds
; fprintf(stderr
,"<math:mo>%s</math:mo>\n",tmp
->value
); indo
;
258 rstartEl(ascii("math:mo"), rList
);
259 runistr(getMathMLEntity(tmp
->value
).c_str());
260 rendEl(ascii("math:mo"));
266 void Formula::makePrimary(Node
*res
)
271 if( tmp
->child
->id
== ID_PRIMARYEXPR
){
272 makePrimary(tmp
->child
);
275 makeIdentifier(tmp
->child
);
279 makeIdentifier(tmp
->next
);
283 void Formula::makeSubSup(Node
*res
)
290 if( res
->id
== ID_SUBEXPR
)
291 fprintf(stderr
,"<math:msub>\n");
292 else if( res
->id
== ID_SUPEXPR
)
293 fprintf(stderr
,"<math:msup>\n");
295 fprintf(stderr
,"<math:msubsup>\n");
297 if( res
->id
== ID_SUBEXPR
)
298 rstartEl(ascii("math:msub"), rList
);
299 else if( res
->id
== ID_SUPEXPR
)
300 rstartEl(ascii("math:msup"), rList
);
302 rstartEl(ascii("math:msubsup"), rList
);
306 if( res
->id
== ID_SUBSUPEXPR
) {
308 makeBlock(tmp
->next
);
309 makeBlock(tmp
->next
->next
);
318 if( res
->id
== ID_SUBEXPR
)
319 fprintf(stderr
,"</math:msub>\n");
320 else if( res
->id
== ID_SUPEXPR
)
321 fprintf(stderr
,"</math:msup>\n");
323 fprintf(stderr
,"</math:msubsup>\n");
325 if( res
->id
== ID_SUBEXPR
)
326 rendEl(ascii("math:msub"));
327 else if( res
->id
== ID_SUPEXPR
)
328 rendEl(ascii("math:msup"));
330 rendEl(ascii("math:msubsup"));
334 void Formula::makeFraction(Node
*res
)
341 fprintf(stderr
,"<math:mfrac>\n");
343 rstartEl(ascii("math:mfrac"), rList
);
349 fprintf(stderr
,"<math:mrow>\n");
351 rstartEl(ascii("math:mrow"), rList
);
354 if( res
->id
== ID_FRACTIONEXPR
)
361 fprintf(stderr
,"</math:mrow>\n");
363 fprintf(stderr
,"<math:mrow>\n");
365 rendEl(ascii("math:mrow"));
366 rstartEl(ascii("math:mrow"), rList
);
369 if( res
->id
== ID_FRACTIONEXPR
)
370 makeBlock(tmp
->next
);
372 makeExprList(tmp
->next
);
376 fprintf(stderr
,"</math:mrow>\n");
378 fprintf(stderr
,"</math:mfrac>\n");
380 rendEl(ascii("math:mrow"));
381 rendEl(ascii("math:mfrac"));
385 void Formula::makeDecoration(Node
*res
)
388 Node
*tmp
= res
->child
;
390 if( !strncmp(tmp
->value
,"under", 5) )
395 fprintf(stderr
,"<math:mover>\n");
397 fprintf(stderr
,"<math:munder>\n");
399 /* accent´Â ¾ðÁ¦ trueÀÌ°í, ¾ðÁ¦, falseÀÎÁö ¸ð¸£°Ú´Ù. */
401 padd(ascii("accent"),ascii("CDATA"),ascii("true"));
402 rstartEl(ascii("math:mover"), rList
);
405 padd(ascii("accentunder"),ascii("CDATA"),ascii("true"));
406 rstartEl(ascii("math:munder"), rList
);
411 makeBlock(tmp
->next
);
415 fprintf(stderr
,"<math:mo>%s</math:mo>\n",
416 getMathMLEntity(tmp
->value
).c_str());
419 rstartEl(ascii("math:mo"), rList
);
420 runistr(getMathMLEntity(tmp
->value
).c_str());
421 rendEl(ascii("math:mo"));
427 fprintf(stderr
,"</math:mover>\n");
429 fprintf(stderr
,"</math:munder>\n");
432 rendEl(ascii("math:mover"));
434 rendEl(ascii("math:munder"));
438 void Formula::makeRoot(Node
*res
)
444 if( tmp
->id
== ID_SQRTEXPR
)
445 fprintf(stderr
,"<math:msqrt>\n");
447 fprintf(stderr
,"<math:mroot>\n");
449 if( tmp
->id
== ID_SQRTEXPR
)
450 rstartEl(ascii("math:msqrt"), rList
);
452 rstartEl(ascii("math:mroot"), rList
);
455 if( tmp
->id
== ID_SQRTEXPR
){
456 makeBlock(tmp
->child
);
459 makeBracket(tmp
->child
);
460 makeBlock(tmp
->child
->next
);
465 if( tmp
->id
== ID_SQRTEXPR
)
466 fprintf(stderr
,"</math:msqrt>\n");
468 fprintf(stderr
,"</math:mroot>\n");
470 if( tmp
->id
== ID_SQRTEXPR
)
471 rendEl(ascii("math:msqrt"));
473 rendEl(ascii("math:mroot"));
476 // DVO: add space to avoid warning
477 void Formula::makeArrow(Node
* /*res*/)
480 void Formula::makeAccent(Node
*res
)
482 makeDecoration( res
);
484 void Formula::makeParenth(Node
*res
)
490 fprintf(stderr
,"<math:mrow>\n");
492 if( tmp
->id
== ID_PARENTH
){
493 fprintf(stderr
,"<math:mo>(</math:mo>\n");
496 fprintf(stderr
,"<math:mo>|</math:mo>\n");
498 fprintf(stderr
,"<math:mrow>\n");
500 rstartEl(ascii("math:mrow"), rList
);
501 rstartEl(ascii("math:mo"), rList
);
502 if( tmp
->id
== ID_PARENTH
)
506 rendEl(ascii("math:mo"));
507 rstartEl(ascii("math:mrow"), rList
);
511 makeExprList(tmp
->child
);
515 fprintf(stderr
,"</math:mrow>\n");
517 if( tmp
->id
== ID_PARENTH
)
518 fprintf(stderr
,"<math:mo>)</math:mo>\n");
520 fprintf(stderr
,"<math:mo>|</math:mo>\n");
523 fprintf(stderr
,"</math:mrow>\n");
525 rendEl(ascii("math:mrow"));
526 rstartEl(ascii("math:mo"), rList
);
527 if( tmp
->id
== ID_PARENTH
)
531 rendEl(ascii("math:mo"));
532 rendEl(ascii("math:mrow"));
536 void Formula::makeFence(Node
*res
)
538 Node
*tmp
= res
->child
;
541 fprintf(stderr
,"<math:mfenced open=\"%s\" close=\"%s\">\n",
542 getMathMLEntity(tmp
->value
).c_str(),
543 getMathMLEntity(tmp
->next
->next
->value
).c_str());
545 padd(ascii("open"), ascii("CDATA"),
546 OUString(getMathMLEntity(tmp
->value
).c_str()) );
547 padd(ascii("close"), ascii("CDATA"),
548 OUString(getMathMLEntity(tmp
->next
->next
->value
).c_str()) );
549 rstartEl(ascii("math:mfenced"), rList
);
553 makeExprList(tmp
->next
);
557 fprintf(stderr
,"</math:mfenced>\n");
559 rendEl(ascii("math:mfenced"));
563 void Formula::makeBracket(Node
*res
)
568 void Formula::makeBlock(Node
*res
)
572 fprintf(stderr
,"<math:mrow>\n");
574 rstartEl(ascii("math:mrow"), rList
);
578 makeExprList(res
->child
);
582 fprintf(stderr
,"</math:mrow>\n");
584 rendEl(ascii("math:mrow"));
588 // DVO: add space to avoid warning
589 void Formula::makeBegin(Node
* /*res*/)
593 // DVO: add space to avoid warning
594 void Formula::makeEnd(Node
* /*res*/)
604 // fprintf(stderr,"\n\n[BEFORE]\n[%s]\n",eq);
607 int idx
=a
.find(sal::static_int_cast
<char>(0xff));
609 //printf("idx = [%d]\n",idx);
611 if((idx
= a
.find(sal::static_int_cast
<char>(0xff),idx
+1)) < 0)
615 char *buf
= (char *)malloc(a
.length()+1);
618 for( i
= 0, j
=0 ; i
< a
.length() ; i
++){ // rtrim and ltrim 32 10 13
623 if( a
[i
] != 32 && a
[i
] != 10 && a
[i
] != 13){
630 for( i
= j
-1 ; i
>= 0 ; i
++ ){
631 if( buf
[i
] == 32 || buf
[i
] == 10 || buf
[i
] == 13 ){
637 // fprintf(stderr,"\n\n[RESULT]\n[%s]\n",a.c_str());
638 if( strlen(buf
) > 0 )
639 res
= mainParse( a
.c_str() );
645 res
= mainParse( eq
);
652 int count
= nodelist
.size();
653 for( int i
= 0 ; i
< count
; i
++ ){
654 tmpNode
= nodelist
.front();
655 nodelist
.pop_front();
664 int len
= strlen(eq
);
665 char *buf
= (char *)malloc(len
+1);
668 for( i
= 0, j
=0 ; i
< len
; i
++){ // rtrim and ltrim 32 10 13
673 if( eq
[i
] != 32 && eq
[i
] != 10 && eq
[i
] != 13){
680 for( i
= j
-1 ; i
>= 0 ; i
++ ){
681 if( buf
[i
] == 32 || buf
[i
] == 10 || buf
[i
] == 13 ){
687 if( strlen(buf
) > 0 )
694 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */