Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / starmath / source / visitors.cxx
bloba950046162bf346bb8d161481cb9f0e2a4f321bd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <sal/log.hxx>
11 #include <tools/gen.hxx>
12 #include <vcl/lineinfo.hxx>
13 #include <visitors.hxx>
14 #include "tmpdevice.hxx"
15 #include <cursor.hxx>
16 #include <cassert>
18 // SmDefaultingVisitor
20 void SmDefaultingVisitor::Visit( SmTableNode* pNode )
22 DefaultVisit( pNode );
25 void SmDefaultingVisitor::Visit( SmBraceNode* pNode )
27 DefaultVisit( pNode );
30 void SmDefaultingVisitor::Visit( SmBracebodyNode* pNode )
32 DefaultVisit( pNode );
35 void SmDefaultingVisitor::Visit( SmOperNode* pNode )
37 DefaultVisit( pNode );
40 void SmDefaultingVisitor::Visit( SmAlignNode* pNode )
42 DefaultVisit( pNode );
45 void SmDefaultingVisitor::Visit( SmAttributNode* pNode )
47 DefaultVisit( pNode );
50 void SmDefaultingVisitor::Visit( SmFontNode* pNode )
52 DefaultVisit( pNode );
55 void SmDefaultingVisitor::Visit( SmUnHorNode* pNode )
57 DefaultVisit( pNode );
60 void SmDefaultingVisitor::Visit( SmBinHorNode* pNode )
62 DefaultVisit( pNode );
65 void SmDefaultingVisitor::Visit( SmBinVerNode* pNode )
67 DefaultVisit( pNode );
70 void SmDefaultingVisitor::Visit( SmBinDiagonalNode* pNode )
72 DefaultVisit( pNode );
75 void SmDefaultingVisitor::Visit( SmSubSupNode* pNode )
77 DefaultVisit( pNode );
80 void SmDefaultingVisitor::Visit( SmMatrixNode* pNode )
82 DefaultVisit( pNode );
85 void SmDefaultingVisitor::Visit( SmPlaceNode* pNode )
87 DefaultVisit( pNode );
90 void SmDefaultingVisitor::Visit( SmTextNode* pNode )
92 DefaultVisit( pNode );
95 void SmDefaultingVisitor::Visit( SmSpecialNode* pNode )
97 DefaultVisit( pNode );
100 void SmDefaultingVisitor::Visit( SmGlyphSpecialNode* pNode )
102 DefaultVisit( pNode );
105 void SmDefaultingVisitor::Visit( SmMathSymbolNode* pNode )
107 DefaultVisit( pNode );
110 void SmDefaultingVisitor::Visit( SmBlankNode* pNode )
112 DefaultVisit( pNode );
115 void SmDefaultingVisitor::Visit( SmErrorNode* pNode )
117 DefaultVisit( pNode );
120 void SmDefaultingVisitor::Visit( SmLineNode* pNode )
122 DefaultVisit( pNode );
125 void SmDefaultingVisitor::Visit( SmExpressionNode* pNode )
127 DefaultVisit( pNode );
130 void SmDefaultingVisitor::Visit( SmPolyLineNode* pNode )
132 DefaultVisit( pNode );
135 void SmDefaultingVisitor::Visit( SmRootNode* pNode )
137 DefaultVisit( pNode );
140 void SmDefaultingVisitor::Visit( SmRootSymbolNode* pNode )
142 DefaultVisit( pNode );
145 void SmDefaultingVisitor::Visit( SmRectangleNode* pNode )
147 DefaultVisit( pNode );
150 void SmDefaultingVisitor::Visit( SmVerticalBraceNode* pNode )
152 DefaultVisit( pNode );
155 // SmCaretDrawingVisitor
157 SmCaretDrawingVisitor::SmCaretDrawingVisitor( OutputDevice& rDevice,
158 SmCaretPos position,
159 Point offset,
160 bool caretVisible )
161 : mrDev( rDevice )
162 , maPos( position )
163 , maOffset( offset )
164 , mbCaretVisible( caretVisible )
166 SAL_WARN_IF( !position.IsValid(), "starmath", "Cannot draw invalid position!" );
167 if( !position.IsValid( ) )
168 return;
170 //Save device state
171 mrDev.Push( PushFlags::FONT | PushFlags::MAPMODE | PushFlags::LINECOLOR | PushFlags::FILLCOLOR | PushFlags::TEXTCOLOR );
173 maPos.pSelectedNode->Accept( this );
174 //Restore device state
175 mrDev.Pop( );
178 void SmCaretDrawingVisitor::Visit( SmTextNode* pNode )
180 long i = maPos.nIndex;
182 mrDev.SetFont( pNode->GetFont( ) );
184 //Find the line
185 SmNode* pLine = SmCursor::FindTopMostNodeInLine( pNode );
187 //Find coordinates
188 long left = pNode->GetLeft( ) + mrDev.GetTextWidth( pNode->GetText( ), 0, i ) + maOffset.X( );
189 long top = pLine->GetTop( ) + maOffset.Y( );
190 long height = pLine->GetHeight( );
191 long left_line = pLine->GetLeft( ) + maOffset.X( );
192 long right_line = pLine->GetRight( ) + maOffset.X( );
194 //Set color
195 mrDev.SetLineColor( COL_BLACK );
197 if ( mbCaretVisible ) {
198 //Draw vertical line
199 Point p1( left, top );
200 Point p2( left, top + height );
201 mrDev.DrawLine( p1, p2 );
204 //Underline the line
205 Point aLeft( left_line, top + height );
206 Point aRight( right_line, top + height );
207 mrDev.DrawLine( aLeft, aRight );
210 void SmCaretDrawingVisitor::DefaultVisit( SmNode* pNode )
212 //Find the line
213 SmNode* pLine = SmCursor::FindTopMostNodeInLine( pNode );
215 //Find coordinates
216 long left = pNode->GetLeft( ) + maOffset.X( ) + ( maPos.nIndex == 1 ? pNode->GetWidth( ) : 0 );
217 long top = pLine->GetTop( ) + maOffset.Y( );
218 long height = pLine->GetHeight( );
219 long left_line = pLine->GetLeft( ) + maOffset.X( );
220 long right_line = pLine->GetRight( ) + maOffset.X( );
222 //Set color
223 mrDev.SetLineColor( COL_BLACK );
225 if ( mbCaretVisible ) {
226 //Draw vertical line
227 Point p1( left, top );
228 Point p2( left, top + height );
229 mrDev.DrawLine( p1, p2 );
232 //Underline the line
233 Point aLeft( left_line, top + height );
234 Point aRight( right_line, top + height );
235 mrDev.DrawLine( aLeft, aRight );
238 // SmCaretPos2LineVisitor
240 void SmCaretPos2LineVisitor::Visit( SmTextNode* pNode )
242 //Save device state
243 mpDev->Push( PushFlags::FONT | PushFlags::TEXTCOLOR );
245 long i = maPos.nIndex;
247 mpDev->SetFont( pNode->GetFont( ) );
249 //Find coordinates
250 long left = pNode->GetLeft( ) + mpDev->GetTextWidth( pNode->GetText( ), 0, i );
251 long top = pNode->GetTop( );
252 long height = pNode->GetHeight( );
254 maLine = SmCaretLine( left, top, height );
256 //Restore device state
257 mpDev->Pop( );
260 void SmCaretPos2LineVisitor::DefaultVisit( SmNode* pNode )
262 //Vertical line ( code from SmCaretDrawingVisitor )
263 Point p1 = pNode->GetTopLeft( );
264 if( maPos.nIndex == 1 )
265 p1.Move( pNode->GetWidth( ), 0 );
267 maLine = SmCaretLine( p1.X( ), p1.Y( ), pNode->GetHeight( ) );
271 // SmDrawingVisitor
273 void SmDrawingVisitor::Visit( SmTableNode* pNode )
275 DrawChildren( pNode );
278 void SmDrawingVisitor::Visit( SmBraceNode* pNode )
280 DrawChildren( pNode );
283 void SmDrawingVisitor::Visit( SmBracebodyNode* pNode )
285 DrawChildren( pNode );
288 void SmDrawingVisitor::Visit( SmOperNode* pNode )
290 DrawChildren( pNode );
293 void SmDrawingVisitor::Visit( SmAlignNode* pNode )
295 DrawChildren( pNode );
298 void SmDrawingVisitor::Visit( SmAttributNode* pNode )
300 DrawChildren( pNode );
303 void SmDrawingVisitor::Visit( SmFontNode* pNode )
305 DrawChildren( pNode );
308 void SmDrawingVisitor::Visit( SmUnHorNode* pNode )
310 DrawChildren( pNode );
313 void SmDrawingVisitor::Visit( SmBinHorNode* pNode )
315 DrawChildren( pNode );
318 void SmDrawingVisitor::Visit( SmBinVerNode* pNode )
320 DrawChildren( pNode );
323 void SmDrawingVisitor::Visit( SmBinDiagonalNode* pNode )
325 DrawChildren( pNode );
328 void SmDrawingVisitor::Visit( SmSubSupNode* pNode )
330 DrawChildren( pNode );
333 void SmDrawingVisitor::Visit( SmMatrixNode* pNode )
335 DrawChildren( pNode );
338 void SmDrawingVisitor::Visit( SmPlaceNode* pNode )
340 DrawSpecialNode( pNode );
343 void SmDrawingVisitor::Visit( SmTextNode* pNode )
345 DrawTextNode( pNode );
348 void SmDrawingVisitor::Visit( SmSpecialNode* pNode )
350 DrawSpecialNode( pNode );
353 void SmDrawingVisitor::Visit( SmGlyphSpecialNode* pNode )
355 DrawSpecialNode( pNode );
358 void SmDrawingVisitor::Visit( SmMathSymbolNode* pNode )
360 DrawSpecialNode( pNode );
363 void SmDrawingVisitor::Visit( SmBlankNode* )
367 void SmDrawingVisitor::Visit( SmErrorNode* pNode )
369 DrawSpecialNode( pNode );
372 void SmDrawingVisitor::Visit( SmLineNode* pNode )
374 DrawChildren( pNode );
377 void SmDrawingVisitor::Visit( SmExpressionNode* pNode )
379 DrawChildren( pNode );
382 void SmDrawingVisitor::Visit( SmRootNode* pNode )
384 DrawChildren( pNode );
387 void SmDrawingVisitor::Visit( SmVerticalBraceNode* pNode )
389 DrawChildren( pNode );
392 void SmDrawingVisitor::Visit( SmRootSymbolNode* pNode )
394 if ( pNode->IsPhantom( ) )
395 return;
397 // draw root-sign itself
398 DrawSpecialNode( pNode );
400 SmTmpDevice aTmpDev( mrDev, true );
401 aTmpDev.SetFillColor( pNode->GetFont( ).GetColor( ) );
402 mrDev.SetLineColor( );
403 aTmpDev.SetFont( pNode->GetFont( ) );
405 // since the width is always unscaled it corresponds to the _original_
406 // _unscaled_ font height to be used, we use that to calculate the
407 // bar height. Thus it is independent of the arguments height.
408 // ( see display of sqrt QQQ versus sqrt stack{Q#Q#Q#Q} )
409 long nBarHeight = pNode->GetWidth( ) * 7L / 100L;
410 long nBarWidth = pNode->GetBodyWidth( ) + pNode->GetBorderWidth( );
411 Point aBarOffset( pNode->GetWidth( ), +pNode->GetBorderWidth( ) );
412 Point aBarPos( maPosition + aBarOffset );
414 tools::Rectangle aBar( aBarPos, Size( nBarWidth, nBarHeight ) );
415 //! avoid GROWING AND SHRINKING of drawn rectangle when constantly
416 //! increasing zoomfactor.
417 // This is done by shifting its output-position to a point that
418 // corresponds exactly to a pixel on the output device.
419 Point aDrawPos( mrDev.PixelToLogic( mrDev.LogicToPixel( aBar.TopLeft( ) ) ) );
420 aBar.SetPos( aDrawPos );
422 mrDev.DrawRect( aBar );
425 void SmDrawingVisitor::Visit( SmPolyLineNode* pNode )
427 if ( pNode->IsPhantom( ) )
428 return;
430 long nBorderwidth = pNode->GetFont( ).GetBorderWidth( );
432 LineInfo aInfo;
433 aInfo.SetWidth( pNode->GetWidth( ) - 2 * nBorderwidth );
435 Point aOffset ( Point( ) - pNode->GetPolygon( ).GetBoundRect( ).TopLeft( )
436 + Point( nBorderwidth, nBorderwidth ) ),
437 aPos ( maPosition + aOffset );
438 pNode->GetPolygon( ).Move( aPos.X( ), aPos.Y( ) ); //Works because Polygon wraps a pointer
440 SmTmpDevice aTmpDev ( mrDev, false );
441 aTmpDev.SetLineColor( pNode->GetFont( ).GetColor( ) );
443 mrDev.DrawPolyLine( pNode->GetPolygon( ), aInfo );
446 void SmDrawingVisitor::Visit( SmRectangleNode* pNode )
448 if ( pNode->IsPhantom( ) )
449 return;
451 SmTmpDevice aTmpDev ( mrDev, false );
452 aTmpDev.SetFillColor( pNode->GetFont( ).GetColor( ) );
453 mrDev.SetLineColor( );
454 aTmpDev.SetFont( pNode->GetFont( ) );
456 sal_uLong nTmpBorderWidth = pNode->GetFont( ).GetBorderWidth( );
458 // get rectangle and remove borderspace
459 tools::Rectangle aTmp ( pNode->AsRectangle( ) + maPosition - pNode->GetTopLeft( ) );
460 aTmp.AdjustLeft(nTmpBorderWidth );
461 aTmp.AdjustRight( -sal_Int32(nTmpBorderWidth) );
462 aTmp.AdjustTop(nTmpBorderWidth );
463 aTmp.AdjustBottom( -sal_Int32(nTmpBorderWidth) );
465 SAL_WARN_IF( aTmp.GetHeight() == 0 || aTmp.GetWidth() == 0,
466 "starmath", "Empty rectangle" );
468 //! avoid GROWING AND SHRINKING of drawn rectangle when constantly
469 //! increasing zoomfactor.
470 // This is done by shifting its output-position to a point that
471 // corresponds exactly to a pixel on the output device.
472 Point aPos ( mrDev.PixelToLogic( mrDev.LogicToPixel( aTmp.TopLeft( ) ) ) );
473 aTmp.SetPos( aPos );
475 mrDev.DrawRect( aTmp );
478 void SmDrawingVisitor::DrawTextNode( SmTextNode* pNode )
480 if ( pNode->IsPhantom() || pNode->GetText().isEmpty() || pNode->GetText()[0] == '\0' )
481 return;
483 SmTmpDevice aTmpDev ( mrDev, false );
484 aTmpDev.SetFont( pNode->GetFont( ) );
486 Point aPos ( maPosition );
487 aPos.AdjustY(pNode->GetBaselineOffset( ) );
488 // round to pixel coordinate
489 aPos = mrDev.PixelToLogic( mrDev.LogicToPixel( aPos ) );
491 mrDev.DrawStretchText( aPos, pNode->GetWidth( ), pNode->GetText( ) );
494 void SmDrawingVisitor::DrawSpecialNode( SmSpecialNode* pNode )
496 //! since this chars might come from any font, that we may not have
497 //! set to ALIGN_BASELINE yet, we do it now.
498 pNode->GetFont( ).SetAlignment( ALIGN_BASELINE );
500 DrawTextNode( pNode );
503 void SmDrawingVisitor::DrawChildren( SmStructureNode* pNode )
505 if ( pNode->IsPhantom( ) )
506 return;
508 Point rPosition = maPosition;
510 for( auto pChild : *pNode )
512 if(!pChild)
513 continue;
514 Point aOffset ( pChild->GetTopLeft( ) - pNode->GetTopLeft( ) );
515 maPosition = rPosition + aOffset;
516 pChild->Accept( this );
520 // SmSetSelectionVisitor
522 SmSetSelectionVisitor::SmSetSelectionVisitor( SmCaretPos startPos, SmCaretPos endPos, SmNode* pTree)
523 : maStartPos(startPos)
524 , maEndPos(endPos)
525 , mbSelecting(false)
527 //Assume that pTree is a SmTableNode
528 SAL_WARN_IF(pTree->GetType() != SmNodeType::Table, "starmath", "pTree should be a SmTableNode!");
529 //Visit root node, this is special as this node cannot be selected, but its children can!
530 if(pTree->GetType() == SmNodeType::Table){
531 //Change state if maStartPos is in front of this node
532 if( maStartPos.pSelectedNode == pTree && maStartPos.nIndex == 0 )
533 mbSelecting = !mbSelecting;
534 //Change state if maEndPos is in front of this node
535 if( maEndPos.pSelectedNode == pTree && maEndPos.nIndex == 0 )
536 mbSelecting = !mbSelecting;
537 SAL_WARN_IF(mbSelecting, "starmath", "Caret positions needed to set mbSelecting about, shouldn't be possible!");
539 //Visit lines
540 for( auto pChild : *static_cast<SmStructureNode*>(pTree) )
542 if(!pChild)
543 continue;
544 pChild->Accept( this );
545 //If we started a selection in this line and it haven't ended, we do that now!
546 if(mbSelecting) {
547 mbSelecting = false;
548 SetSelectedOnAll(pChild);
549 //Set maStartPos and maEndPos to invalid positions, this ensures that an unused
550 //start or end (because we forced end above), doesn't start a new selection.
551 maStartPos = maEndPos = SmCaretPos();
554 //Check if pTree isn't selected
555 SAL_WARN_IF(pTree->IsSelected(), "starmath", "pTree should never be selected!");
556 //Discard the selection if there's a bug (it's better than crashing)
557 if(pTree->IsSelected())
558 SetSelectedOnAll(pTree, false);
559 }else //This shouldn't happen, but I don't see any reason to die if it does
560 pTree->Accept(this);
563 void SmSetSelectionVisitor::SetSelectedOnAll( SmNode* pSubTree, bool IsSelected ) {
564 pSubTree->SetSelected( IsSelected );
566 if(pSubTree->GetNumSubNodes() == 0)
567 return;
568 //Quick BFS to set all selections
569 for( auto pChild : *static_cast<SmStructureNode*>(pSubTree) )
571 if(!pChild)
572 continue;
573 SetSelectedOnAll( pChild, IsSelected );
577 void SmSetSelectionVisitor::DefaultVisit( SmNode* pNode ) {
578 //Change state if maStartPos is in front of this node
579 if( maStartPos.pSelectedNode == pNode && maStartPos.nIndex == 0 )
580 mbSelecting = !mbSelecting;
581 //Change state if maEndPos is in front of this node
582 if( maEndPos.pSelectedNode == pNode && maEndPos.nIndex == 0 )
583 mbSelecting = !mbSelecting;
585 //Cache current state
586 bool WasSelecting = mbSelecting;
587 bool ChangedState = false;
589 //Set selected
590 pNode->SetSelected( mbSelecting );
592 //Visit children
593 if(pNode->GetNumSubNodes() > 0)
595 for( auto pChild : *static_cast<SmStructureNode*>(pNode) )
597 if(!pChild)
598 continue;
599 pChild->Accept( this );
600 ChangedState = ( WasSelecting != mbSelecting ) || ChangedState;
604 //If state changed
605 if( ChangedState )
607 //Select this node and all of its children
608 //(Make exception for SmBracebodyNode)
609 if( pNode->GetType() != SmNodeType::Bracebody ||
610 !pNode->GetParent() ||
611 pNode->GetParent()->GetType() != SmNodeType::Brace )
612 SetSelectedOnAll( pNode );
613 else
614 SetSelectedOnAll( pNode->GetParent() );
615 /* If the equation is: sqrt{2 + 4} + 5
616 * And the selection is: sqrt{2 + [4} +] 5
617 * Where [ denotes maStartPos and ] denotes maEndPos
618 * Then the sqrt node should be selected, so that the
619 * effective selection is: [sqrt{2 + 4} +] 5
620 * The same is the case if we swap maStartPos and maEndPos.
624 //Change state if maStartPos is after this node
625 if( maStartPos.pSelectedNode == pNode && maStartPos.nIndex == 1 )
627 mbSelecting = !mbSelecting;
629 //Change state if maEndPos is after of this node
630 if( maEndPos.pSelectedNode == pNode && maEndPos.nIndex == 1 )
632 mbSelecting = !mbSelecting;
636 void SmSetSelectionVisitor::VisitCompositionNode( SmStructureNode* pNode )
638 //Change state if maStartPos is in front of this node
639 if( maStartPos.pSelectedNode == pNode && maStartPos.nIndex == 0 )
640 mbSelecting = !mbSelecting;
641 //Change state if maEndPos is in front of this node
642 if( maEndPos.pSelectedNode == pNode && maEndPos.nIndex == 0 )
643 mbSelecting = !mbSelecting;
645 //Cache current state
646 bool WasSelecting = mbSelecting;
648 //Visit children
649 for( auto pChild : *pNode )
651 if(!pChild)
652 continue;
653 pChild->Accept( this );
656 //Set selected, if everything was selected
657 pNode->SetSelected( WasSelecting && mbSelecting );
659 //Change state if maStartPos is after this node
660 if( maStartPos.pSelectedNode == pNode && maStartPos.nIndex == 1 )
661 mbSelecting = !mbSelecting;
662 //Change state if maEndPos is after of this node
663 if( maEndPos.pSelectedNode == pNode && maEndPos.nIndex == 1 )
664 mbSelecting = !mbSelecting;
667 void SmSetSelectionVisitor::Visit( SmTextNode* pNode ) {
668 long i1 = -1,
669 i2 = -1;
670 if( maStartPos.pSelectedNode == pNode )
671 i1 = maStartPos.nIndex;
672 if( maEndPos.pSelectedNode == pNode )
673 i2 = maEndPos.nIndex;
675 long start, end;
676 pNode->SetSelected(true);
677 if( i1 != -1 && i2 != -1 ) {
678 start = std::min(i1, i2);
679 end = std::max(i1, i2);
680 } else if( mbSelecting && i1 != -1 ) {
681 start = 0;
682 end = i1;
683 mbSelecting = false;
684 } else if( mbSelecting && i2 != -1 ) {
685 start = 0;
686 end = i2;
687 mbSelecting = false;
688 } else if( !mbSelecting && i1 != -1 ) {
689 start = i1;
690 end = pNode->GetText().getLength();
691 mbSelecting = true;
692 } else if( !mbSelecting && i2 != -1 ) {
693 start = i2;
694 end = pNode->GetText().getLength();
695 mbSelecting = true;
696 } else if( mbSelecting ) {
697 start = 0;
698 end = pNode->GetText().getLength();
699 } else {
700 pNode->SetSelected( false );
701 start = 0;
702 end = 0;
704 pNode->SetSelected( start != end );
705 pNode->SetSelectionStart( start );
706 pNode->SetSelectionEnd( end );
709 void SmSetSelectionVisitor::Visit( SmExpressionNode* pNode ) {
710 VisitCompositionNode( pNode );
713 void SmSetSelectionVisitor::Visit( SmLineNode* pNode ) {
714 VisitCompositionNode( pNode );
717 void SmSetSelectionVisitor::Visit( SmAlignNode* pNode ) {
718 VisitCompositionNode( pNode );
721 void SmSetSelectionVisitor::Visit( SmBinHorNode* pNode ) {
722 VisitCompositionNode( pNode );
725 void SmSetSelectionVisitor::Visit( SmUnHorNode* pNode ) {
726 VisitCompositionNode( pNode );
729 void SmSetSelectionVisitor::Visit( SmFontNode* pNode ) {
730 VisitCompositionNode( pNode );
733 // SmCaretPosGraphBuildingVisitor
735 SmCaretPosGraphBuildingVisitor::SmCaretPosGraphBuildingVisitor( SmNode* pRootNode )
736 : mpRightMost(nullptr)
737 , mpGraph(new SmCaretPosGraph)
739 //pRootNode should always be a table
740 SAL_WARN_IF( pRootNode->GetType( ) != SmNodeType::Table, "starmath", "pRootNode must be a table node");
741 //Handle the special case where SmNodeType::Table is used a rootnode
742 if( pRootNode->GetType( ) == SmNodeType::Table ){
743 //Children are SmLineNodes
744 //Or so I thought... Apparently, the children can be instances of SmExpression
745 //especially if there's an error in the formula... So here we go, a simple work around.
746 for( auto pChild : *static_cast<SmStructureNode*>(pRootNode) )
748 if(!pChild)
749 continue;
750 mpRightMost = mpGraph->Add( SmCaretPos( pChild, 0 ) );
751 pChild->Accept( this );
753 }else
754 pRootNode->Accept(this);
757 SmCaretPosGraphBuildingVisitor::~SmCaretPosGraphBuildingVisitor()
761 void SmCaretPosGraphBuildingVisitor::Visit( SmLineNode* pNode ){
762 for( auto pChild : *pNode )
764 if(!pChild)
765 continue;
766 pChild->Accept( this );
770 /** Build SmCaretPosGraph for SmTableNode
771 * This method covers cases where SmTableNode is used in a binom or stack,
772 * the special case where it is used as root node for the entire formula is
773 * handled in the constructor.
775 void SmCaretPosGraphBuildingVisitor::Visit( SmTableNode* pNode ){
776 SmCaretPosGraphEntry *left = mpRightMost,
777 *right = mpGraph->Add( SmCaretPos( pNode, 1) );
778 bool bIsFirst = true;
779 for( auto pChild : *pNode )
781 if(!pChild)
782 continue;
783 mpRightMost = mpGraph->Add( SmCaretPos( pChild, 0 ), left);
784 if(bIsFirst)
785 left->SetRight(mpRightMost);
786 pChild->Accept( this );
787 mpRightMost->SetRight(right);
788 if(bIsFirst)
789 right->SetLeft(mpRightMost);
790 bIsFirst = false;
792 mpRightMost = right;
795 /** Build SmCaretPosGraph for SmSubSupNode
797 * The child positions in a SubSupNode, where H is the body:
798 * \code
799 * CSUP
801 * LSUP H H RSUP
802 * H H
803 * HHHH
804 * H H
805 * LSUB H H RSUB
807 * CSUB
808 * \endcode
810 * Graph over these, where "left" is before the SmSubSupNode and "right" is after:
811 * \dot
812 * digraph Graph{
813 * left -> H;
814 * H -> right;
815 * LSUP -> H;
816 * LSUB -> H;
817 * CSUP -> right;
818 * CSUB -> right;
819 * RSUP -> right;
820 * RSUB -> right;
821 * };
822 * \enddot
825 void SmCaretPosGraphBuildingVisitor::Visit( SmSubSupNode* pNode )
827 SmCaretPosGraphEntry *left,
828 *right,
829 *bodyLeft,
830 *bodyRight;
832 assert(mpRightMost);
833 left = mpRightMost;
835 //Create bodyLeft
836 SAL_WARN_IF( !pNode->GetBody(), "starmath", "SmSubSupNode Doesn't have a body!" );
837 bodyLeft = mpGraph->Add( SmCaretPos( pNode->GetBody( ), 0 ), left );
838 left->SetRight( bodyLeft ); //TODO: Don't make this if LSUP or LSUB are NULL ( not sure??? )
840 //Create right
841 right = mpGraph->Add( SmCaretPos( pNode, 1 ) );
843 //Visit the body, to get bodyRight
844 mpRightMost = bodyLeft;
845 pNode->GetBody( )->Accept( this );
846 bodyRight = mpRightMost;
847 bodyRight->SetRight( right );
848 right->SetLeft( bodyRight );
850 //If there's an LSUP
851 SmNode* pChild = pNode->GetSubSup( LSUP );
852 if( pChild ){
853 SmCaretPosGraphEntry *cLeft; //Child left
854 cLeft = mpGraph->Add( SmCaretPos( pChild, 0 ), left );
856 mpRightMost = cLeft;
857 pChild->Accept( this );
859 mpRightMost->SetRight( bodyLeft );
861 //If there's an LSUB
862 pChild = pNode->GetSubSup( LSUB );
863 if( pChild ){
864 SmCaretPosGraphEntry *cLeft; //Child left
865 cLeft = mpGraph->Add( SmCaretPos( pChild, 0 ), left );
867 mpRightMost = cLeft;
868 pChild->Accept( this );
870 mpRightMost->SetRight( bodyLeft );
872 //If there's a CSUP
873 pChild = pNode->GetSubSup( CSUP );
874 if( pChild ){
875 SmCaretPosGraphEntry *cLeft; //Child left
876 cLeft = mpGraph->Add( SmCaretPos( pChild, 0 ), left );
878 mpRightMost = cLeft;
879 pChild->Accept( this );
881 mpRightMost->SetRight( right );
883 //If there's a CSUB
884 pChild = pNode->GetSubSup( CSUB );
885 if( pChild ){
886 SmCaretPosGraphEntry *cLeft; //Child left
887 cLeft = mpGraph->Add( SmCaretPos( pChild, 0 ), left );
889 mpRightMost = cLeft;
890 pChild->Accept( this );
892 mpRightMost->SetRight( right );
894 //If there's an RSUP
895 pChild = pNode->GetSubSup( RSUP );
896 if( pChild ){
897 SmCaretPosGraphEntry *cLeft; //Child left
898 cLeft = mpGraph->Add( SmCaretPos( pChild, 0 ), bodyRight );
900 mpRightMost = cLeft;
901 pChild->Accept( this );
903 mpRightMost->SetRight( right );
905 //If there's an RSUB
906 pChild = pNode->GetSubSup( RSUB );
907 if( pChild ){
908 SmCaretPosGraphEntry *cLeft; //Child left
909 cLeft = mpGraph->Add( SmCaretPos( pChild, 0 ), bodyRight );
911 mpRightMost = cLeft;
912 pChild->Accept( this );
914 mpRightMost->SetRight( right );
917 //Set return parameters
918 mpRightMost = right;
921 /** Build caret position for SmOperNode
923 * If first child is an SmSubSupNode we will ignore its
924 * body, as this body is a SmMathSymbol, for SUM, INT or similar
925 * that shouldn't be subject to modification.
926 * If first child is not a SmSubSupNode, ignore it completely
927 * as it is a SmMathSymbol.
929 * The child positions in a SmOperNode, where H is symbol, e.g. int, sum or similar:
930 * \code
931 * TO
933 * LSUP H H RSUP BBB BB BBB B B
934 * H H B B B B B B B B
935 * HHHH BBB B B B B B
936 * H H B B B B B B B
937 * LSUB H H RSUB BBB BB BBB B
939 * FROM
940 * \endcode
941 * Notice, CSUP, etc. are actually grandchildren, but inorder to ignore H, these are visited
942 * from here. If they are present, that is if pOper is an instance of SmSubSupNode.
944 * Graph over these, where "left" is before the SmOperNode and "right" is after:
945 * \dot
946 * digraph Graph{
947 * left -> BODY;
948 * BODY -> right;
949 * LSUP -> BODY;
950 * LSUB -> BODY;
951 * TO -> BODY;
952 * FROM -> BODY;
953 * RSUP -> BODY;
954 * RSUB -> BODY;
955 * };
956 * \enddot
958 void SmCaretPosGraphBuildingVisitor::Visit( SmOperNode* pNode )
960 SmNode *pOper = pNode->GetSubNode( 0 ),
961 *pBody = pNode->GetSubNode( 1 );
963 SmCaretPosGraphEntry *left = mpRightMost,
964 *bodyLeft,
965 *bodyRight,
966 *right;
967 //Create body left
968 bodyLeft = mpGraph->Add( SmCaretPos( pBody, 0 ), left );
969 left->SetRight( bodyLeft );
971 //Visit body, get bodyRight
972 mpRightMost = bodyLeft;
973 pBody->Accept( this );
974 bodyRight = mpRightMost;
976 //Create right
977 right = mpGraph->Add( SmCaretPos( pNode, 1 ), bodyRight );
978 bodyRight->SetRight( right );
980 //Get subsup pNode if any
981 SmSubSupNode* pSubSup = pOper->GetType( ) == SmNodeType::SubSup ? static_cast<SmSubSupNode*>(pOper) : nullptr;
983 SmNode* pChild;
984 SmCaretPosGraphEntry *childLeft;
985 if( pSubSup ) {
986 pChild = pSubSup->GetSubSup( LSUP );
987 if( pChild ) {
988 //Create position in front of pChild
989 childLeft = mpGraph->Add( SmCaretPos( pChild, 0 ), left );
990 //Visit pChild
991 mpRightMost = childLeft;
992 pChild->Accept( this );
993 //Set right on mpRightMost from pChild
994 mpRightMost->SetRight( bodyLeft );
997 pChild = pSubSup->GetSubSup( LSUB );
998 if( pChild ) {
999 //Create position in front of pChild
1000 childLeft = mpGraph->Add( SmCaretPos( pChild, 0 ), left );
1001 //Visit pChild
1002 mpRightMost = childLeft;
1003 pChild->Accept( this );
1004 //Set right on mpRightMost from pChild
1005 mpRightMost->SetRight( bodyLeft );
1008 pChild = pSubSup->GetSubSup( CSUP );
1009 if ( pChild ) {//TO
1010 //Create position in front of pChild
1011 childLeft = mpGraph->Add( SmCaretPos( pChild, 0 ), left );
1012 //Visit pChild
1013 mpRightMost = childLeft;
1014 pChild->Accept( this );
1015 //Set right on mpRightMost from pChild
1016 mpRightMost->SetRight( bodyLeft );
1019 pChild = pSubSup->GetSubSup( CSUB );
1020 if( pChild ) { //FROM
1021 //Create position in front of pChild
1022 childLeft = mpGraph->Add( SmCaretPos( pChild, 0 ), left );
1023 //Visit pChild
1024 mpRightMost = childLeft;
1025 pChild->Accept( this );
1026 //Set right on mpRightMost from pChild
1027 mpRightMost->SetRight( bodyLeft );
1030 pChild = pSubSup->GetSubSup( RSUP );
1031 if ( pChild ) {
1032 //Create position in front of pChild
1033 childLeft = mpGraph->Add( SmCaretPos( pChild, 0 ), left );
1034 //Visit pChild
1035 mpRightMost = childLeft;
1036 pChild->Accept( this );
1037 //Set right on mpRightMost from pChild
1038 mpRightMost->SetRight( bodyLeft );
1041 pChild = pSubSup->GetSubSup( RSUB );
1042 if ( pChild ) {
1043 //Create position in front of pChild
1044 childLeft = mpGraph->Add( SmCaretPos( pChild, 0 ), left );
1045 //Visit pChild
1046 mpRightMost = childLeft;
1047 pChild->Accept( this );
1048 //Set right on mpRightMost from pChild
1049 mpRightMost->SetRight( bodyLeft );
1053 //Return right
1054 mpRightMost = right;
1057 void SmCaretPosGraphBuildingVisitor::Visit( SmMatrixNode* pNode )
1059 SmCaretPosGraphEntry *left = mpRightMost,
1060 *right = mpGraph->Add( SmCaretPos( pNode, 1 ) );
1062 for (size_t i = 0; i < pNode->GetNumRows(); ++i)
1064 SmCaretPosGraphEntry* r = left;
1065 for (size_t j = 0; j < pNode->GetNumCols(); ++j)
1067 SmNode* pSubNode = pNode->GetSubNode( i * pNode->GetNumCols( ) + j );
1069 mpRightMost = mpGraph->Add( SmCaretPos( pSubNode, 0 ), r );
1070 if( j != 0 || ( pNode->GetNumRows() - 1U ) / 2 == i )
1071 r->SetRight( mpRightMost );
1073 pSubNode->Accept( this );
1075 r = mpRightMost;
1077 mpRightMost->SetRight( right );
1078 if( ( pNode->GetNumRows() - 1U ) / 2 == i )
1079 right->SetLeft( mpRightMost );
1082 mpRightMost = right;
1085 /** Build SmCaretPosGraph for SmTextNode
1087 * Lines in an SmTextNode:
1088 * \code
1089 * A B C
1090 * \endcode
1091 * Where A B and C are characters in the text.
1093 * Graph over these, where "left" is before the SmTextNode and "right" is after:
1094 * \dot
1095 * digraph Graph{
1096 * left -> A;
1097 * A -> B
1098 * B -> right;
1099 * };
1100 * \enddot
1101 * Notice that C and right is the same position here.
1103 void SmCaretPosGraphBuildingVisitor::Visit( SmTextNode* pNode )
1105 SAL_WARN_IF( pNode->GetText().isEmpty(), "starmath", "Empty SmTextNode is bad" );
1107 int size = pNode->GetText().getLength();
1108 for( int i = 1; i <= size; i++ ){
1109 SmCaretPosGraphEntry* pRight = mpRightMost;
1110 mpRightMost = mpGraph->Add( SmCaretPos( pNode, i ), pRight );
1111 pRight->SetRight( mpRightMost );
1115 /** Build SmCaretPosGraph for SmBinVerNode
1117 * Lines in an SmBinVerNode:
1118 * \code
1120 * -----
1122 * \endcode
1124 * Graph over these, where "left" is before the SmBinVerNode and "right" is after:
1125 * \dot
1126 * digraph Graph{
1127 * left -> A;
1128 * A -> right;
1129 * B -> right;
1130 * };
1131 * \enddot
1133 void SmCaretPosGraphBuildingVisitor::Visit( SmBinVerNode* pNode )
1135 //None if these children can be NULL, see SmBinVerNode::Arrange
1136 SmNode *pNum = pNode->GetSubNode( 0 ),
1137 *pDenom = pNode->GetSubNode( 2 );
1139 SmCaretPosGraphEntry *left,
1140 *right,
1141 *numLeft,
1142 *denomLeft;
1144 assert(mpRightMost);
1145 //Set left
1146 left = mpRightMost;
1148 //Create right
1149 right = mpGraph->Add( SmCaretPos( pNode, 1 ) );
1151 //Create numLeft
1152 numLeft = mpGraph->Add( SmCaretPos( pNum, 0 ), left );
1153 left->SetRight( numLeft );
1155 //Visit pNum
1156 mpRightMost = numLeft;
1157 pNum->Accept( this );
1158 mpRightMost->SetRight( right );
1159 right->SetLeft( mpRightMost );
1161 //Create denomLeft
1162 denomLeft = mpGraph->Add( SmCaretPos( pDenom, 0 ), left );
1164 //Visit pDenom
1165 mpRightMost = denomLeft;
1166 pDenom->Accept( this );
1167 mpRightMost->SetRight( right );
1169 //Set return parameter
1170 mpRightMost = right;
1173 /** Build SmCaretPosGraph for SmVerticalBraceNode
1175 * Lines in an SmVerticalBraceNode:
1176 * \code
1177 * pScript
1178 * ________
1179 * / \
1180 * pBody
1181 * \endcode
1184 void SmCaretPosGraphBuildingVisitor::Visit( SmVerticalBraceNode* pNode )
1186 SmNode *pBody = pNode->Body(),
1187 *pScript = pNode->Script();
1188 //None of these children can be NULL
1190 SmCaretPosGraphEntry *left,
1191 *bodyLeft,
1192 *scriptLeft,
1193 *right;
1195 left = mpRightMost;
1197 //Create right
1198 right = mpGraph->Add( SmCaretPos( pNode, 1 ) );
1200 //Create bodyLeft
1201 bodyLeft = mpGraph->Add( SmCaretPos( pBody, 0 ), left );
1202 left->SetRight( bodyLeft );
1203 mpRightMost = bodyLeft;
1204 pBody->Accept( this );
1205 mpRightMost->SetRight( right );
1206 right->SetLeft( mpRightMost );
1208 //Create script
1209 scriptLeft = mpGraph->Add( SmCaretPos( pScript, 0 ), left );
1210 mpRightMost = scriptLeft;
1211 pScript->Accept( this );
1212 mpRightMost->SetRight( right );
1214 //Set return value
1215 mpRightMost = right;
1218 /** Build SmCaretPosGraph for SmBinDiagonalNode
1220 * Lines in an SmBinDiagonalNode:
1221 * \code
1222 * A /
1224 * / B
1225 * \endcode
1226 * Where A and B are lines.
1228 * Used in formulas such as "A wideslash B"
1230 void SmCaretPosGraphBuildingVisitor::Visit( SmBinDiagonalNode* pNode )
1232 SmNode *A = pNode->GetSubNode( 0 ),
1233 *B = pNode->GetSubNode( 1 );
1235 SmCaretPosGraphEntry *left,
1236 *leftA,
1237 *rightA,
1238 *leftB,
1239 *right;
1240 left = mpRightMost;
1242 //Create right
1243 right = mpGraph->Add( SmCaretPos( pNode, 1 ) );
1245 //Create left A
1246 leftA = mpGraph->Add( SmCaretPos( A, 0 ), left );
1247 left->SetRight( leftA );
1249 //Visit A
1250 mpRightMost = leftA;
1251 A->Accept( this );
1252 rightA = mpRightMost;
1254 //Create left B
1255 leftB = mpGraph->Add( SmCaretPos( B, 0 ), rightA );
1256 rightA->SetRight( leftB );
1258 //Visit B
1259 mpRightMost = leftB;
1260 B->Accept( this );
1261 mpRightMost->SetRight( right );
1262 right->SetLeft( mpRightMost );
1264 //Set return value
1265 mpRightMost = right;
1268 //Straight forward ( I think )
1269 void SmCaretPosGraphBuildingVisitor::Visit( SmBinHorNode* pNode )
1271 for( auto pChild : *pNode )
1273 if(!pChild)
1274 continue;
1275 pChild->Accept( this );
1278 void SmCaretPosGraphBuildingVisitor::Visit( SmUnHorNode* pNode )
1280 // Unary operator node
1281 for( auto pChild : *pNode )
1283 if(!pChild)
1284 continue;
1285 pChild->Accept( this );
1289 void SmCaretPosGraphBuildingVisitor::Visit( SmExpressionNode* pNode )
1291 for( auto pChild : *pNode )
1293 if(!pChild)
1294 continue;
1295 pChild->Accept( this );
1299 void SmCaretPosGraphBuildingVisitor::Visit( SmFontNode* pNode )
1301 //Has only got one child, should act as an expression if possible
1302 for( auto pChild : *pNode )
1304 if(!pChild)
1305 continue;
1306 pChild->Accept( this );
1310 /** Build SmCaretPosGraph for SmBracebodyNode
1311 * Acts as an SmExpressionNode
1313 * Below is an example of a formula tree that has multiple children for SmBracebodyNode
1314 * \dot
1315 * digraph {
1316 * labelloc = "t";
1317 * label= "Equation: \"lbrace i mline i in setZ rbrace\"";
1318 * n0 [label="SmTableNode"];
1319 * n0 -> n1 [label="0"];
1320 * n1 [label="SmLineNode"];
1321 * n1 -> n2 [label="0"];
1322 * n2 [label="SmExpressionNode"];
1323 * n2 -> n3 [label="0"];
1324 * n3 [label="SmBraceNode"];
1325 * n3 -> n4 [label="0"];
1326 * n4 [label="SmMathSymbolNode: {"];
1327 * n3 -> n5 [label="1"];
1328 * n5 [label="SmBracebodyNode"];
1329 * n5 -> n6 [label="0"];
1330 * n6 [label="SmExpressionNode"];
1331 * n6 -> n7 [label="0"];
1332 * n7 [label="SmTextNode: i"];
1333 * n5 -> n8 [label="1"];
1334 * n8 [label="SmMathSymbolNode: &#124;"]; // Unicode "VERTICAL LINE"
1335 * n5 -> n9 [label="2"];
1336 * n9 [label="SmExpressionNode"];
1337 * n9 -> n10 [label="0"];
1338 * n10 [label="SmBinHorNode"];
1339 * n10 -> n11 [label="0"];
1340 * n11 [label="SmTextNode: i"];
1341 * n10 -> n12 [label="1"];
1342 * n12 [label="SmMathSymbolNode: &#8712;"]; // Unicode "ELEMENT OF"
1343 * n10 -> n13 [label="2"];
1344 * n13 [label="SmMathSymbolNode: &#8484;"]; // Unicode "DOUBLE-STRUCK CAPITAL Z"
1345 * n3 -> n14 [label="2"];
1346 * n14 [label="SmMathSymbolNode: }"];
1348 * \enddot
1350 void SmCaretPosGraphBuildingVisitor::Visit( SmBracebodyNode* pNode )
1352 for( auto pChild : *pNode )
1354 if(!pChild)
1355 continue;
1356 SmCaretPosGraphEntry* pStart = mpGraph->Add( SmCaretPos( pChild, 0), mpRightMost );
1357 mpRightMost->SetRight( pStart );
1358 mpRightMost = pStart;
1359 pChild->Accept( this );
1363 /** Build SmCaretPosGraph for SmAlignNode
1364 * Acts as an SmExpressionNode, as it only has one child this okay
1366 void SmCaretPosGraphBuildingVisitor::Visit( SmAlignNode* pNode )
1368 for( auto pChild : *pNode )
1370 if(!pChild)
1371 continue;
1372 pChild->Accept( this );
1376 /** Build SmCaretPosGraph for SmRootNode
1378 * Lines in an SmRootNode:
1379 * \code
1380 * _________
1381 * A/
1382 * \/ B
1384 * \endcode
1385 * A: pExtra ( optional, can be NULL ),
1386 * B: pBody
1388 * Graph over these, where "left" is before the SmRootNode and "right" is after:
1389 * \dot
1390 * digraph Graph{
1391 * left -> B;
1392 * B -> right;
1393 * A -> B;
1395 * \enddot
1397 void SmCaretPosGraphBuildingVisitor::Visit( SmRootNode* pNode )
1399 SmNode *pExtra = pNode->GetSubNode( 0 ), //Argument, NULL for sqrt, and SmTextNode if cubicroot
1400 *pBody = pNode->GetSubNode( 2 ); //Body of the root
1401 assert(pBody);
1403 SmCaretPosGraphEntry *left,
1404 *right,
1405 *bodyLeft,
1406 *bodyRight;
1408 //Get left and save it
1409 assert(mpRightMost);
1410 left = mpRightMost;
1412 //Create body left
1413 bodyLeft = mpGraph->Add( SmCaretPos( pBody, 0 ), left );
1414 left->SetRight( bodyLeft );
1416 //Create right
1417 right = mpGraph->Add( SmCaretPos( pNode, 1 ) );
1419 //Visit body
1420 mpRightMost = bodyLeft;
1421 pBody->Accept( this );
1422 bodyRight = mpRightMost;
1423 bodyRight->SetRight( right );
1424 right->SetLeft( bodyRight );
1426 //Visit pExtra
1427 if( pExtra ){
1428 mpRightMost = mpGraph->Add( SmCaretPos( pExtra, 0 ), left );
1429 pExtra->Accept( this );
1430 mpRightMost->SetRight( bodyLeft );
1433 mpRightMost = right;
1437 /** Build SmCaretPosGraph for SmPlaceNode
1438 * Consider this a single character.
1440 void SmCaretPosGraphBuildingVisitor::Visit( SmPlaceNode* pNode )
1442 SmCaretPosGraphEntry* right = mpGraph->Add( SmCaretPos( pNode, 1 ), mpRightMost );
1443 mpRightMost->SetRight( right );
1444 mpRightMost = right;
1447 /** SmErrorNode is context dependent metadata, it can't be selected
1449 * @remarks There's no point in deleting, copying and/or moving an instance
1450 * of SmErrorNode as it may not exist in another context! Thus there are no
1451 * positions to select an SmErrorNode.
1453 void SmCaretPosGraphBuildingVisitor::Visit( SmErrorNode* )
1457 /** Build SmCaretPosGraph for SmBlankNode
1458 * Consider this a single character, as it is only a blank space
1460 void SmCaretPosGraphBuildingVisitor::Visit( SmBlankNode* pNode )
1462 SmCaretPosGraphEntry* right = mpGraph->Add( SmCaretPos( pNode, 1 ), mpRightMost );
1463 mpRightMost->SetRight( right );
1464 mpRightMost = right;
1467 /** Build SmCaretPosGraph for SmBraceNode
1469 * Lines in an SmBraceNode:
1470 * \code
1471 * | |
1472 * | B |
1473 * | |
1474 * \endcode
1475 * B: Body
1477 * Graph over these, where "left" is before the SmBraceNode and "right" is after:
1478 * \dot
1479 * digraph Graph{
1480 * left -> B;
1481 * B -> right;
1483 * \enddot
1485 void SmCaretPosGraphBuildingVisitor::Visit( SmBraceNode* pNode )
1487 SmNode* pBody = pNode->Body();
1489 SmCaretPosGraphEntry *left = mpRightMost,
1490 *right = mpGraph->Add( SmCaretPos( pNode, 1 ) );
1492 if( pBody->GetType() != SmNodeType::Bracebody ) {
1493 mpRightMost = mpGraph->Add( SmCaretPos( pBody, 0 ), left );
1494 left->SetRight( mpRightMost );
1495 }else
1496 mpRightMost = left;
1498 pBody->Accept( this );
1499 mpRightMost->SetRight( right );
1500 right->SetLeft( mpRightMost );
1502 mpRightMost = right;
1505 /** Build SmCaretPosGraph for SmAttributNode
1507 * Lines in an SmAttributNode:
1508 * \code
1509 * Attr
1510 * Body
1511 * \endcode
1513 * There's a body and an attribute, the construction is used for "widehat A", where "A" is the body
1514 * and "^" is the attribute ( note GetScaleMode( ) on SmAttributNode tells how the attribute should be
1515 * scaled ).
1517 void SmCaretPosGraphBuildingVisitor::Visit( SmAttributNode* pNode )
1519 SmNode *pAttr = pNode->Attribute(),
1520 *pBody = pNode->Body();
1521 assert(pAttr);
1522 assert(pBody);
1524 SmCaretPosGraphEntry *left = mpRightMost,
1525 *attrLeft,
1526 *bodyLeft,
1527 *bodyRight,
1528 *right;
1530 //Creating bodyleft
1531 bodyLeft = mpGraph->Add( SmCaretPos( pBody, 0 ), left );
1532 left->SetRight( bodyLeft );
1534 //Creating right
1535 right = mpGraph->Add( SmCaretPos( pNode, 1 ) );
1537 //Visit the body
1538 mpRightMost = bodyLeft;
1539 pBody->Accept( this );
1540 bodyRight = mpRightMost;
1541 bodyRight->SetRight( right );
1542 right->SetLeft( bodyRight );
1544 //Create attrLeft
1545 attrLeft = mpGraph->Add( SmCaretPos( pAttr, 0 ), left );
1547 //Visit attribute
1548 mpRightMost = attrLeft;
1549 pAttr->Accept( this );
1550 mpRightMost->SetRight( right );
1552 //Set return value
1553 mpRightMost = right;
1556 //Consider these single symbols
1557 void SmCaretPosGraphBuildingVisitor::Visit( SmSpecialNode* pNode )
1559 SmCaretPosGraphEntry* right = mpGraph->Add( SmCaretPos( pNode, 1 ), mpRightMost );
1560 mpRightMost->SetRight( right );
1561 mpRightMost = right;
1563 void SmCaretPosGraphBuildingVisitor::Visit( SmGlyphSpecialNode* pNode )
1565 SmCaretPosGraphEntry* right = mpGraph->Add( SmCaretPos( pNode, 1 ), mpRightMost );
1566 mpRightMost->SetRight( right );
1567 mpRightMost = right;
1569 void SmCaretPosGraphBuildingVisitor::Visit( SmMathSymbolNode* pNode )
1571 SmCaretPosGraphEntry* right = mpGraph->Add( SmCaretPos( pNode, 1 ), mpRightMost );
1572 mpRightMost->SetRight( right );
1573 mpRightMost = right;
1576 void SmCaretPosGraphBuildingVisitor::Visit( SmRootSymbolNode* )
1578 //Do nothing
1581 void SmCaretPosGraphBuildingVisitor::Visit( SmRectangleNode* )
1583 //Do nothing
1585 void SmCaretPosGraphBuildingVisitor::Visit( SmPolyLineNode* )
1587 //Do nothing
1590 // SmCloningVisitor
1592 SmNode* SmCloningVisitor::Clone( SmNode* pNode )
1594 SmNode* pCurrResult = mpResult;
1595 pNode->Accept( this );
1596 SmNode* pClone = mpResult;
1597 mpResult = pCurrResult;
1598 return pClone;
1601 void SmCloningVisitor::CloneNodeAttr( SmNode const * pSource, SmNode* pTarget )
1603 pTarget->SetScaleMode( pSource->GetScaleMode( ) );
1604 //Other attributes are set when prepare or arrange is executed
1605 //and may depend on stuff not being cloned here.
1608 void SmCloningVisitor::CloneKids( SmStructureNode* pSource, SmStructureNode* pTarget )
1610 //Cache current result
1611 SmNode* pCurrResult = mpResult;
1613 //Create array for holding clones
1614 size_t nSize = pSource->GetNumSubNodes( );
1615 SmNodeArray aNodes( nSize );
1617 //Clone children
1618 for (size_t i = 0; i < nSize; ++i)
1620 SmNode* pKid;
1621 if( nullptr != ( pKid = pSource->GetSubNode( i ) ) )
1622 pKid->Accept( this );
1623 else
1624 mpResult = nullptr;
1625 aNodes[i] = mpResult;
1628 //Set subnodes of pTarget
1629 pTarget->SetSubNodes( std::move(aNodes) );
1631 //Restore result as where prior to call
1632 mpResult = pCurrResult;
1635 void SmCloningVisitor::Visit( SmTableNode* pNode )
1637 SmTableNode* pClone = new SmTableNode( pNode->GetToken( ) );
1638 CloneNodeAttr( pNode, pClone );
1639 CloneKids( pNode, pClone );
1640 mpResult = pClone;
1643 void SmCloningVisitor::Visit( SmBraceNode* pNode )
1645 SmBraceNode* pClone = new SmBraceNode( pNode->GetToken( ) );
1646 CloneNodeAttr( pNode, pClone );
1647 CloneKids( pNode, pClone );
1648 mpResult = pClone;
1651 void SmCloningVisitor::Visit( SmBracebodyNode* pNode )
1653 SmBracebodyNode* pClone = new SmBracebodyNode( pNode->GetToken( ) );
1654 CloneNodeAttr( pNode, pClone );
1655 CloneKids( pNode, pClone );
1656 mpResult = pClone;
1659 void SmCloningVisitor::Visit( SmOperNode* pNode )
1661 SmOperNode* pClone = new SmOperNode( pNode->GetToken( ) );
1662 CloneNodeAttr( pNode, pClone );
1663 CloneKids( pNode, pClone );
1664 mpResult = pClone;
1667 void SmCloningVisitor::Visit( SmAlignNode* pNode )
1669 SmAlignNode* pClone = new SmAlignNode( pNode->GetToken( ) );
1670 CloneNodeAttr( pNode, pClone );
1671 CloneKids( pNode, pClone );
1672 mpResult = pClone;
1675 void SmCloningVisitor::Visit( SmAttributNode* pNode )
1677 SmAttributNode* pClone = new SmAttributNode( pNode->GetToken( ) );
1678 CloneNodeAttr( pNode, pClone );
1679 CloneKids( pNode, pClone );
1680 mpResult = pClone;
1683 void SmCloningVisitor::Visit( SmFontNode* pNode )
1685 SmFontNode* pClone = new SmFontNode( pNode->GetToken( ) );
1686 pClone->SetSizeParameter( pNode->GetSizeParameter( ), pNode->GetSizeType( ) );
1687 CloneNodeAttr( pNode, pClone );
1688 CloneKids( pNode, pClone );
1689 mpResult = pClone;
1692 void SmCloningVisitor::Visit( SmUnHorNode* pNode )
1694 SmUnHorNode* pClone = new SmUnHorNode( pNode->GetToken( ) );
1695 CloneNodeAttr( pNode, pClone );
1696 CloneKids( pNode, pClone );
1697 mpResult = pClone;
1700 void SmCloningVisitor::Visit( SmBinHorNode* pNode )
1702 SmBinHorNode* pClone = new SmBinHorNode( pNode->GetToken( ) );
1703 CloneNodeAttr( pNode, pClone );
1704 CloneKids( pNode, pClone );
1705 mpResult = pClone;
1708 void SmCloningVisitor::Visit( SmBinVerNode* pNode )
1710 SmBinVerNode* pClone = new SmBinVerNode( pNode->GetToken( ) );
1711 CloneNodeAttr( pNode, pClone );
1712 CloneKids( pNode, pClone );
1713 mpResult = pClone;
1716 void SmCloningVisitor::Visit( SmBinDiagonalNode* pNode )
1718 SmBinDiagonalNode *pClone = new SmBinDiagonalNode( pNode->GetToken( ) );
1719 pClone->SetAscending( pNode->IsAscending( ) );
1720 CloneNodeAttr( pNode, pClone );
1721 CloneKids( pNode, pClone );
1722 mpResult = pClone;
1725 void SmCloningVisitor::Visit( SmSubSupNode* pNode )
1727 SmSubSupNode *pClone = new SmSubSupNode( pNode->GetToken( ) );
1728 pClone->SetUseLimits( pNode->IsUseLimits( ) );
1729 CloneNodeAttr( pNode, pClone );
1730 CloneKids( pNode, pClone );
1731 mpResult = pClone;
1734 void SmCloningVisitor::Visit( SmMatrixNode* pNode )
1736 SmMatrixNode *pClone = new SmMatrixNode( pNode->GetToken( ) );
1737 pClone->SetRowCol( pNode->GetNumRows( ), pNode->GetNumCols( ) );
1738 CloneNodeAttr( pNode, pClone );
1739 CloneKids( pNode, pClone );
1740 mpResult = pClone;
1743 void SmCloningVisitor::Visit( SmPlaceNode* pNode )
1745 mpResult = new SmPlaceNode( pNode->GetToken( ) );
1746 CloneNodeAttr( pNode, mpResult );
1749 void SmCloningVisitor::Visit( SmTextNode* pNode )
1751 SmTextNode* pClone = new SmTextNode( pNode->GetToken( ), pNode->GetFontDesc( ) );
1752 pClone->ChangeText( pNode->GetText( ) );
1753 CloneNodeAttr( pNode, pClone );
1754 mpResult = pClone;
1757 void SmCloningVisitor::Visit( SmSpecialNode* pNode )
1759 mpResult = new SmSpecialNode( pNode->GetToken( ) );
1760 CloneNodeAttr( pNode, mpResult );
1763 void SmCloningVisitor::Visit( SmGlyphSpecialNode* pNode )
1765 mpResult = new SmGlyphSpecialNode( pNode->GetToken( ) );
1766 CloneNodeAttr( pNode, mpResult );
1769 void SmCloningVisitor::Visit( SmMathSymbolNode* pNode )
1771 mpResult = new SmMathSymbolNode( pNode->GetToken( ) );
1772 CloneNodeAttr( pNode, mpResult );
1775 void SmCloningVisitor::Visit( SmBlankNode* pNode )
1777 SmBlankNode* pClone = new SmBlankNode( pNode->GetToken( ) );
1778 pClone->SetBlankNum( pNode->GetBlankNum( ) );
1779 mpResult = pClone;
1780 CloneNodeAttr( pNode, mpResult );
1783 void SmCloningVisitor::Visit( SmErrorNode* pNode )
1785 mpResult = new SmErrorNode( pNode->GetToken( ) );
1786 CloneNodeAttr( pNode, mpResult );
1789 void SmCloningVisitor::Visit( SmLineNode* pNode )
1791 SmLineNode* pClone = new SmLineNode( pNode->GetToken( ) );
1792 CloneNodeAttr( pNode, pClone );
1793 CloneKids( pNode, pClone );
1794 mpResult = pClone;
1797 void SmCloningVisitor::Visit( SmExpressionNode* pNode )
1799 SmExpressionNode* pClone = new SmExpressionNode( pNode->GetToken( ) );
1800 CloneNodeAttr( pNode, pClone );
1801 CloneKids( pNode, pClone );
1802 mpResult = pClone;
1805 void SmCloningVisitor::Visit( SmPolyLineNode* pNode )
1807 mpResult = new SmPolyLineNode( pNode->GetToken( ) );
1808 CloneNodeAttr( pNode, mpResult );
1811 void SmCloningVisitor::Visit( SmRootNode* pNode )
1813 SmRootNode* pClone = new SmRootNode( pNode->GetToken( ) );
1814 CloneNodeAttr( pNode, pClone );
1815 CloneKids( pNode, pClone );
1816 mpResult = pClone;
1819 void SmCloningVisitor::Visit( SmRootSymbolNode* pNode )
1821 mpResult = new SmRootSymbolNode( pNode->GetToken( ) );
1822 CloneNodeAttr( pNode, mpResult );
1825 void SmCloningVisitor::Visit( SmRectangleNode* pNode )
1827 mpResult = new SmRectangleNode( pNode->GetToken( ) );
1828 CloneNodeAttr( pNode, mpResult );
1831 void SmCloningVisitor::Visit( SmVerticalBraceNode* pNode )
1833 SmVerticalBraceNode* pClone = new SmVerticalBraceNode( pNode->GetToken( ) );
1834 CloneNodeAttr( pNode, pClone );
1835 CloneKids( pNode, pClone );
1836 mpResult = pClone;
1839 // SmSelectionDrawingVisitor
1841 SmSelectionDrawingVisitor::SmSelectionDrawingVisitor( OutputDevice& rDevice, SmNode* pTree, const Point& rOffset )
1842 : mrDev( rDevice )
1843 , mbHasSelectionArea( false )
1845 //Visit everything
1846 SAL_WARN_IF( !pTree, "starmath", "pTree can't be null!" );
1847 if( pTree )
1848 pTree->Accept( this );
1850 //Draw selection if there's any
1851 if( !mbHasSelectionArea ) return;
1853 maSelectionArea.Move( rOffset.X( ), rOffset.Y( ) );
1855 //Save device state
1856 mrDev.Push( PushFlags::LINECOLOR | PushFlags::FILLCOLOR );
1857 //Change colors
1858 mrDev.SetLineColor( );
1859 mrDev.SetFillColor( COL_LIGHTGRAY );
1861 //Draw rectangle
1862 mrDev.DrawRect( maSelectionArea );
1864 //Restore device state
1865 mrDev.Pop( );
1868 void SmSelectionDrawingVisitor::ExtendSelectionArea(const tools::Rectangle& rArea)
1870 if ( ! mbHasSelectionArea ) {
1871 maSelectionArea = rArea;
1872 mbHasSelectionArea = true;
1873 } else
1874 maSelectionArea.Union(rArea);
1877 void SmSelectionDrawingVisitor::DefaultVisit( SmNode* pNode )
1879 if( pNode->IsSelected( ) )
1880 ExtendSelectionArea( pNode->AsRectangle( ) );
1881 VisitChildren( pNode );
1884 void SmSelectionDrawingVisitor::VisitChildren( SmNode* pNode )
1886 if(pNode->GetNumSubNodes() == 0)
1887 return;
1888 for( auto pChild : *static_cast<SmStructureNode*>(pNode) )
1890 if(!pChild)
1891 continue;
1892 pChild->Accept( this );
1896 void SmSelectionDrawingVisitor::Visit( SmTextNode* pNode )
1898 if( !pNode->IsSelected())
1899 return;
1901 mrDev.Push( PushFlags::TEXTCOLOR | PushFlags::FONT );
1903 mrDev.SetFont( pNode->GetFont( ) );
1904 Point Position = pNode->GetTopLeft( );
1905 long left = Position.getX( ) + mrDev.GetTextWidth( pNode->GetText( ), 0, pNode->GetSelectionStart( ) );
1906 long right = Position.getX( ) + mrDev.GetTextWidth( pNode->GetText( ), 0, pNode->GetSelectionEnd( ) );
1907 long top = Position.getY( );
1908 long bottom = top + pNode->GetHeight( );
1909 tools::Rectangle rect( left, top, right, bottom );
1911 ExtendSelectionArea( rect );
1913 mrDev.Pop( );
1916 // SmNodeToTextVisitor
1918 SmNodeToTextVisitor::SmNodeToTextVisitor( SmNode* pNode, OUString &rText )
1920 pNode->Accept( this );
1921 rText = maCmdText.makeStringAndClear();
1924 void SmNodeToTextVisitor::Visit( SmTableNode* pNode )
1926 if( pNode->GetToken( ).eType == TBINOM ) {
1927 Append( "{ binom" );
1928 LineToText( pNode->GetSubNode( 0 ) );
1929 LineToText( pNode->GetSubNode( 1 ) );
1930 Append("} ");
1931 } else if( pNode->GetToken( ).eType == TSTACK ) {
1932 Append( "stack{ " );
1933 bool bFirst = true;
1934 for( auto pChild : *pNode )
1936 if(!pChild)
1937 continue;
1938 if(bFirst)
1939 bFirst = false;
1940 else
1942 Separate( );
1943 Append( "# " );
1945 LineToText( pChild );
1947 Separate( );
1948 Append( "}" );
1949 } else { //Assume it's a toplevel table, containing lines
1950 bool bFirst = true;
1951 for( auto pChild : *pNode )
1953 if(!pChild)
1954 continue;
1955 if(bFirst)
1956 bFirst = false;
1957 else
1959 Separate( );
1960 Append( "newline" );
1962 Separate( );
1963 pChild->Accept( this );
1968 void SmNodeToTextVisitor::Visit( SmBraceNode* pNode )
1970 SmNode *pLeftBrace = pNode->OpeningBrace(),
1971 *pBody = pNode->Body(),
1972 *pRightBrace = pNode->ClosingBrace();
1973 //Handle special case where it's absolute function
1974 if( pNode->GetToken( ).eType == TABS ) {
1975 Append( "abs" );
1976 LineToText( pBody );
1977 } else {
1978 if( pNode->GetScaleMode( ) == SmScaleMode::Height )
1979 Append( "left " );
1980 pLeftBrace->Accept( this );
1981 Separate( );
1982 pBody->Accept( this );
1983 Separate( );
1984 if( pNode->GetScaleMode( ) == SmScaleMode::Height )
1985 Append( "right " );
1986 pRightBrace->Accept( this );
1990 void SmNodeToTextVisitor::Visit( SmBracebodyNode* pNode )
1992 for( auto pChild : *pNode )
1994 if(!pChild)
1995 continue;
1996 Separate( );
1997 pChild->Accept( this );
2001 void SmNodeToTextVisitor::Visit( SmOperNode* pNode )
2003 Append( pNode->GetToken( ).aText );
2004 Separate( );
2005 if( pNode->GetToken( ).eType == TOPER ){
2006 //There's an SmGlyphSpecialNode if eType == TOPER
2007 if( pNode->GetSubNode( 0 )->GetType( ) == SmNodeType::SubSup )
2008 Append( pNode->GetSubNode( 0 )->GetSubNode( 0 )->GetToken( ).aText );
2009 else
2010 Append( pNode->GetSubNode( 0 )->GetToken( ).aText );
2012 if( pNode->GetSubNode( 0 )->GetType( ) == SmNodeType::SubSup ) {
2013 SmSubSupNode *pSubSup = static_cast<SmSubSupNode*>( pNode->GetSubNode( 0 ) );
2014 SmNode* pChild = pSubSup->GetSubSup( LSUP );
2015 if( pChild ) {
2016 Separate( );
2017 Append( "lsup { " );
2018 LineToText( pChild );
2019 Append( "} " );
2021 pChild = pSubSup->GetSubSup( LSUB );
2022 if( pChild ) {
2023 Separate( );
2024 Append( "lsub { " );
2025 LineToText( pChild );
2026 Append( "} " );
2028 pChild = pSubSup->GetSubSup( RSUP );
2029 if( pChild ) {
2030 Separate( );
2031 Append( "^ { " );
2032 LineToText( pChild );
2033 Append( "} " );
2035 pChild = pSubSup->GetSubSup( RSUB );
2036 if( pChild ) {
2037 Separate( );
2038 Append( "_ { " );
2039 LineToText( pChild );
2040 Append( "} " );
2042 pChild = pSubSup->GetSubSup( CSUP );
2043 if( pChild ) {
2044 Separate( );
2045 if (pSubSup->IsUseLimits())
2046 Append( "to { " );
2047 else
2048 Append( "csup { " );
2049 LineToText( pChild );
2050 Append( "} " );
2052 pChild = pSubSup->GetSubSup( CSUB );
2053 if( pChild ) {
2054 Separate( );
2055 if (pSubSup->IsUseLimits())
2056 Append( "from { " );
2057 else
2058 Append( "csub { " );
2059 LineToText( pChild );
2060 Append( "} " );
2063 LineToText( pNode->GetSubNode( 1 ) );
2066 void SmNodeToTextVisitor::Visit( SmAlignNode* pNode )
2068 Append( pNode->GetToken( ).aText );
2069 LineToText( pNode->GetSubNode( 0 ) );
2072 void SmNodeToTextVisitor::Visit( SmAttributNode* pNode )
2074 Append( pNode->GetToken( ).aText );
2075 LineToText( pNode->Body() );
2078 void SmNodeToTextVisitor::Visit( SmFontNode* pNode )
2080 switch ( pNode->GetToken( ).eType )
2082 case TBOLD:
2083 Append( "bold " );
2084 break;
2085 case TNBOLD:
2086 Append( "nbold " );
2087 break;
2088 case TITALIC:
2089 Append( "italic " );
2090 break;
2091 case TNITALIC:
2092 Append( "nitalic " );
2093 break;
2094 case TPHANTOM:
2095 Append( "phantom " );
2096 break;
2097 case TSIZE:
2099 Append( "size " );
2100 switch ( pNode->GetSizeType( ) )
2102 case FontSizeType::PLUS:
2103 Append( "+" );
2104 break;
2105 case FontSizeType::MINUS:
2106 Append( "-" );
2107 break;
2108 case FontSizeType::MULTIPLY:
2109 Append( "*" );
2110 break;
2111 case FontSizeType::DIVIDE:
2112 Append( "/" );
2113 break;
2114 case FontSizeType::ABSOLUT:
2115 default:
2116 break;
2118 Append( ::rtl::math::doubleToUString(
2119 static_cast<double>( pNode->GetSizeParameter( ) ),
2120 rtl_math_StringFormat_Automatic,
2121 rtl_math_DecimalPlaces_Max, '.', true ) );
2122 Append( " " );
2124 break;
2125 case TBLACK:
2126 Append( "color black " );
2127 break;
2128 case TWHITE:
2129 Append( "color white " );
2130 break;
2131 case TRED:
2132 Append( "color red " );
2133 break;
2134 case TGREEN:
2135 Append( "color green " );
2136 break;
2137 case TBLUE:
2138 Append( "color blue " );
2139 break;
2140 case TCYAN:
2141 Append( "color cyan " );
2142 break;
2143 case TMAGENTA:
2144 Append( "color magenta " );
2145 break;
2146 case TYELLOW:
2147 Append( "color yellow " );
2148 break;
2149 case TSANS:
2150 Append( "font sans " );
2151 break;
2152 case TSERIF:
2153 Append( "font serif " );
2154 break;
2155 case TFIXED:
2156 Append( "font fixed " );
2157 break;
2158 default:
2159 break;
2161 LineToText( pNode->GetSubNode( 1 ) );
2164 void SmNodeToTextVisitor::Visit( SmUnHorNode* pNode )
2166 if(pNode->GetSubNode( 1 )->GetToken( ).eType == TFACT)
2168 // visit children in the reverse order
2169 for( auto it = pNode->rbegin(); it != pNode->rend(); ++it )
2171 auto pChild = *it;
2172 if(!pChild)
2173 continue;
2174 Separate( );
2175 pChild->Accept( this );
2178 else
2180 for( auto pChild : *pNode )
2182 if(!pChild)
2183 continue;
2184 Separate( );
2185 pChild->Accept( this );
2190 void SmNodeToTextVisitor::Visit( SmBinHorNode* pNode )
2192 const SmNode *pParent = pNode->GetParent();
2193 bool bBraceNeeded = pParent && pParent->GetType() == SmNodeType::Font;
2194 SmNode *pLeft = pNode->LeftOperand(),
2195 *pOper = pNode->Symbol(),
2196 *pRight = pNode->RightOperand();
2197 Separate( );
2198 if (bBraceNeeded)
2199 Append( "{ " );
2200 pLeft->Accept( this );
2201 Separate( );
2202 pOper->Accept( this );
2203 Separate( );
2204 pRight->Accept( this );
2205 Separate( );
2206 if (bBraceNeeded)
2207 Append( "} " );
2210 void SmNodeToTextVisitor::Visit( SmBinVerNode* pNode )
2212 SmNode *pNum = pNode->GetSubNode( 0 ),
2213 *pDenom = pNode->GetSubNode( 2 );
2214 Append( "{ " );
2215 LineToText( pNum );
2216 Append( "over" );
2217 LineToText( pDenom );
2218 Append( "} " );
2221 void SmNodeToTextVisitor::Visit( SmBinDiagonalNode* pNode )
2223 SmNode *pLeftOperand = pNode->GetSubNode( 0 ),
2224 *pRightOperand = pNode->GetSubNode( 1 );
2225 Append( "{ " );
2226 LineToText( pLeftOperand );
2227 Separate( );
2228 Append( "wideslash " );
2229 LineToText( pRightOperand );
2230 Append( "} " );
2233 void SmNodeToTextVisitor::Visit( SmSubSupNode* pNode )
2235 LineToText( pNode->GetBody( ) );
2236 SmNode *pChild = pNode->GetSubSup( LSUP );
2237 if( pChild ) {
2238 Separate( );
2239 Append( "lsup " );
2240 LineToText( pChild );
2242 pChild = pNode->GetSubSup( LSUB );
2243 if( pChild ) {
2244 Separate( );
2245 Append( "lsub " );
2246 LineToText( pChild );
2248 pChild = pNode->GetSubSup( RSUP );
2249 if( pChild ) {
2250 Separate( );
2251 Append( "^ " );
2252 LineToText( pChild );
2254 pChild = pNode->GetSubSup( RSUB );
2255 if( pChild ) {
2256 Separate( );
2257 Append( "_ " );
2258 LineToText( pChild );
2260 pChild = pNode->GetSubSup( CSUP );
2261 if( pChild ) {
2262 Separate( );
2263 if (pNode->IsUseLimits())
2264 Append( "to " );
2265 else
2266 Append( "csup " );
2267 LineToText( pChild );
2269 pChild = pNode->GetSubSup( CSUB );
2270 if( pChild ) {
2271 Separate( );
2272 if (pNode->IsUseLimits())
2273 Append( "from " );
2274 else
2275 Append( "csub " );
2276 LineToText( pChild );
2280 void SmNodeToTextVisitor::Visit( SmMatrixNode* pNode )
2282 Append( "matrix{" );
2283 for (size_t i = 0; i < pNode->GetNumRows(); ++i)
2285 for (size_t j = 0; j < pNode->GetNumCols( ); ++j)
2287 SmNode* pSubNode = pNode->GetSubNode( i * pNode->GetNumCols( ) + j );
2288 Separate( );
2289 pSubNode->Accept( this );
2290 Separate( );
2291 if (j != pNode->GetNumCols() - 1U)
2292 Append( "#" );
2294 Separate( );
2295 if (i != pNode->GetNumRows() - 1U)
2296 Append( "##" );
2298 Append( "} " );
2301 void SmNodeToTextVisitor::Visit( SmPlaceNode* )
2303 Append( "<?>" );
2306 void SmNodeToTextVisitor::Visit( SmTextNode* pNode )
2308 //TODO: This method might need improvements, see SmTextNode::CreateTextFromNode
2309 if( pNode->GetToken( ).eType == TTEXT )
2310 Append( "\"" );
2311 Append( pNode->GetText( ) );
2312 if( pNode->GetToken( ).eType == TTEXT )
2313 Append( "\"" );
2316 void SmNodeToTextVisitor::Visit( SmSpecialNode* pNode )
2318 Append( pNode->GetToken( ).aText );
2321 void SmNodeToTextVisitor::Visit( SmGlyphSpecialNode* pNode )
2323 if( pNode->GetToken( ).eType == TBOPER )
2324 Append( "boper " );
2325 else
2326 Append( "uoper " );
2327 Append( pNode->GetToken( ).aText );
2330 void SmNodeToTextVisitor::Visit( SmMathSymbolNode* pNode )
2332 Append( pNode->GetToken( ).aText );
2335 void SmNodeToTextVisitor::Visit( SmBlankNode* pNode )
2337 sal_uInt16 nNum = pNode->GetBlankNum();
2338 if (nNum <= 0)
2339 return;
2340 sal_uInt16 nWide = nNum / 4;
2341 sal_uInt16 nNarrow = nNum % 4;
2342 for (sal_uInt16 i = 0; i < nWide; i++)
2343 Append( "~" );
2344 for (sal_uInt16 i = 0; i < nNarrow; i++)
2345 Append( "`" );
2346 Append( " " );
2349 void SmNodeToTextVisitor::Visit( SmErrorNode* )
2353 void SmNodeToTextVisitor::Visit( SmLineNode* pNode )
2355 for( auto pChild : *pNode )
2357 if(!pChild)
2358 continue;
2359 Separate( );
2360 pChild->Accept( this );
2364 void SmNodeToTextVisitor::Visit( SmExpressionNode* pNode )
2366 bool bracketsNeeded = pNode->GetNumSubNodes() != 1 || pNode->GetSubNode(0)->GetType() == SmNodeType::BinHor;
2367 if (!bracketsNeeded)
2369 const SmNode *pParent = pNode->GetParent();
2370 // nested subsups
2371 bracketsNeeded =
2372 pParent && pParent->GetType() == SmNodeType::SubSup &&
2373 pNode->GetNumSubNodes() == 1 &&
2374 pNode->GetSubNode(0)->GetType() == SmNodeType::SubSup;
2377 if (bracketsNeeded) {
2378 Append( "{ " );
2380 for( auto pChild : *pNode )
2382 if(!pChild)
2383 continue;
2384 pChild->Accept( this );
2385 Separate( );
2387 if (bracketsNeeded) {
2388 Append( "} " );
2392 void SmNodeToTextVisitor::Visit( SmPolyLineNode* )
2396 void SmNodeToTextVisitor::Visit( SmRootNode* pNode )
2398 SmNode *pExtra = pNode->GetSubNode( 0 ),
2399 *pBody = pNode->GetSubNode( 2 );
2400 if( pExtra ) {
2401 Append( "nroot" );
2402 LineToText( pExtra );
2403 } else
2404 Append( "sqrt" );
2405 LineToText( pBody );
2408 void SmNodeToTextVisitor::Visit( SmRootSymbolNode* )
2412 void SmNodeToTextVisitor::Visit( SmRectangleNode* )
2416 void SmNodeToTextVisitor::Visit( SmVerticalBraceNode* pNode )
2418 SmNode *pBody = pNode->Body(),
2419 *pScript = pNode->Script();
2420 LineToText( pBody );
2421 Append( pNode->GetToken( ).aText );
2422 LineToText( pScript );
2425 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */