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/.
10 #include <sal/log.hxx>
11 #include <tools/gen.hxx>
12 #include <vcl/lineinfo.hxx>
13 #include <visitors.hxx>
14 #include "tmpdevice.hxx"
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
,
164 , mbCaretVisible( caretVisible
)
166 SAL_WARN_IF( !position
.IsValid(), "starmath", "Cannot draw invalid position!" );
167 if( !position
.IsValid( ) )
171 mrDev
.Push( PushFlags::FONT
| PushFlags::MAPMODE
| PushFlags::LINECOLOR
| PushFlags::FILLCOLOR
| PushFlags::TEXTCOLOR
);
173 maPos
.pSelectedNode
->Accept( this );
174 //Restore device state
178 void SmCaretDrawingVisitor::Visit( SmTextNode
* pNode
)
180 long i
= maPos
.nIndex
;
182 mrDev
.SetFont( pNode
->GetFont( ) );
185 SmNode
* pLine
= SmCursor::FindTopMostNodeInLine( pNode
);
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( );
195 mrDev
.SetLineColor( COL_BLACK
);
197 if ( mbCaretVisible
) {
199 Point
p1( left
, top
);
200 Point
p2( left
, top
+ height
);
201 mrDev
.DrawLine( p1
, p2
);
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
)
213 SmNode
* pLine
= SmCursor::FindTopMostNodeInLine( pNode
);
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( );
223 mrDev
.SetLineColor( COL_BLACK
);
225 if ( mbCaretVisible
) {
227 Point
p1( left
, top
);
228 Point
p2( left
, top
+ height
);
229 mrDev
.DrawLine( p1
, p2
);
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
)
243 mpDev
->Push( PushFlags::FONT
| PushFlags::TEXTCOLOR
);
245 long i
= maPos
.nIndex
;
247 mpDev
->SetFont( pNode
->GetFont( ) );
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
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( ) );
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( ) )
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( ) )
430 long nBorderwidth
= pNode
->GetFont( ).GetBorderWidth( );
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( ) )
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( ) ) ) );
475 mrDev
.DrawRect( aTmp
);
478 void SmDrawingVisitor::DrawTextNode( SmTextNode
* pNode
)
480 if ( pNode
->IsPhantom() || pNode
->GetText().isEmpty() || pNode
->GetText()[0] == '\0' )
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( ) )
508 Point rPosition
= maPosition
;
510 for( auto pChild
: *pNode
)
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
)
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!");
540 for( auto pChild
: *static_cast<SmStructureNode
*>(pTree
) )
544 pChild
->Accept( this );
545 //If we started a selection in this line and it haven't ended, we do that now!
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
563 void SmSetSelectionVisitor::SetSelectedOnAll( SmNode
* pSubTree
, bool IsSelected
) {
564 pSubTree
->SetSelected( IsSelected
);
566 if(pSubTree
->GetNumSubNodes() == 0)
568 //Quick BFS to set all selections
569 for( auto pChild
: *static_cast<SmStructureNode
*>(pSubTree
) )
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;
590 pNode
->SetSelected( mbSelecting
);
593 if(pNode
->GetNumSubNodes() > 0)
595 for( auto pChild
: *static_cast<SmStructureNode
*>(pNode
) )
599 pChild
->Accept( this );
600 ChangedState
= ( WasSelecting
!= mbSelecting
) || 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
);
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
;
649 for( auto pChild
: *pNode
)
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
) {
670 if( maStartPos
.pSelectedNode
== pNode
)
671 i1
= maStartPos
.nIndex
;
672 if( maEndPos
.pSelectedNode
== pNode
)
673 i2
= maEndPos
.nIndex
;
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 ) {
684 } else if( mbSelecting
&& i2
!= -1 ) {
688 } else if( !mbSelecting
&& i1
!= -1 ) {
690 end
= pNode
->GetText().getLength();
692 } else if( !mbSelecting
&& i2
!= -1 ) {
694 end
= pNode
->GetText().getLength();
696 } else if( mbSelecting
) {
698 end
= pNode
->GetText().getLength();
700 pNode
->SetSelected( false );
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
) )
750 mpRightMost
= mpGraph
->Add( SmCaretPos( pChild
, 0 ) );
751 pChild
->Accept( this );
754 pRootNode
->Accept(this);
757 SmCaretPosGraphBuildingVisitor::~SmCaretPosGraphBuildingVisitor()
761 void SmCaretPosGraphBuildingVisitor::Visit( SmLineNode
* pNode
){
762 for( auto pChild
: *pNode
)
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
)
783 mpRightMost
= mpGraph
->Add( SmCaretPos( pChild
, 0 ), left
);
785 left
->SetRight(mpRightMost
);
786 pChild
->Accept( this );
787 mpRightMost
->SetRight(right
);
789 right
->SetLeft(mpRightMost
);
795 /** Build SmCaretPosGraph for SmSubSupNode
797 * The child positions in a SubSupNode, where H is the body:
810 * Graph over these, where "left" is before the SmSubSupNode and "right" is after:
825 void SmCaretPosGraphBuildingVisitor::Visit( SmSubSupNode
* pNode
)
827 SmCaretPosGraphEntry
*left
,
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??? )
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
);
851 SmNode
* pChild
= pNode
->GetSubSup( LSUP
);
853 SmCaretPosGraphEntry
*cLeft
; //Child left
854 cLeft
= mpGraph
->Add( SmCaretPos( pChild
, 0 ), left
);
857 pChild
->Accept( this );
859 mpRightMost
->SetRight( bodyLeft
);
862 pChild
= pNode
->GetSubSup( LSUB
);
864 SmCaretPosGraphEntry
*cLeft
; //Child left
865 cLeft
= mpGraph
->Add( SmCaretPos( pChild
, 0 ), left
);
868 pChild
->Accept( this );
870 mpRightMost
->SetRight( bodyLeft
);
873 pChild
= pNode
->GetSubSup( CSUP
);
875 SmCaretPosGraphEntry
*cLeft
; //Child left
876 cLeft
= mpGraph
->Add( SmCaretPos( pChild
, 0 ), left
);
879 pChild
->Accept( this );
881 mpRightMost
->SetRight( right
);
884 pChild
= pNode
->GetSubSup( CSUB
);
886 SmCaretPosGraphEntry
*cLeft
; //Child left
887 cLeft
= mpGraph
->Add( SmCaretPos( pChild
, 0 ), left
);
890 pChild
->Accept( this );
892 mpRightMost
->SetRight( right
);
895 pChild
= pNode
->GetSubSup( RSUP
);
897 SmCaretPosGraphEntry
*cLeft
; //Child left
898 cLeft
= mpGraph
->Add( SmCaretPos( pChild
, 0 ), bodyRight
);
901 pChild
->Accept( this );
903 mpRightMost
->SetRight( right
);
906 pChild
= pNode
->GetSubSup( RSUB
);
908 SmCaretPosGraphEntry
*cLeft
; //Child left
909 cLeft
= mpGraph
->Add( SmCaretPos( pChild
, 0 ), bodyRight
);
912 pChild
->Accept( this );
914 mpRightMost
->SetRight( right
);
917 //Set return parameters
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:
933 * LSUP H H RSUP BBB BB BBB B B
934 * H H B B B B B B B B
937 * LSUB H H RSUB BBB BB BBB B
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:
958 void SmCaretPosGraphBuildingVisitor::Visit( SmOperNode
* pNode
)
960 SmNode
*pOper
= pNode
->GetSubNode( 0 ),
961 *pBody
= pNode
->GetSubNode( 1 );
963 SmCaretPosGraphEntry
*left
= mpRightMost
,
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
;
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;
984 SmCaretPosGraphEntry
*childLeft
;
986 pChild
= pSubSup
->GetSubSup( LSUP
);
988 //Create position in front of pChild
989 childLeft
= mpGraph
->Add( SmCaretPos( pChild
, 0 ), left
);
991 mpRightMost
= childLeft
;
992 pChild
->Accept( this );
993 //Set right on mpRightMost from pChild
994 mpRightMost
->SetRight( bodyLeft
);
997 pChild
= pSubSup
->GetSubSup( LSUB
);
999 //Create position in front of pChild
1000 childLeft
= mpGraph
->Add( SmCaretPos( pChild
, 0 ), left
);
1002 mpRightMost
= childLeft
;
1003 pChild
->Accept( this );
1004 //Set right on mpRightMost from pChild
1005 mpRightMost
->SetRight( bodyLeft
);
1008 pChild
= pSubSup
->GetSubSup( CSUP
);
1010 //Create position in front of pChild
1011 childLeft
= mpGraph
->Add( SmCaretPos( pChild
, 0 ), left
);
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
);
1024 mpRightMost
= childLeft
;
1025 pChild
->Accept( this );
1026 //Set right on mpRightMost from pChild
1027 mpRightMost
->SetRight( bodyLeft
);
1030 pChild
= pSubSup
->GetSubSup( RSUP
);
1032 //Create position in front of pChild
1033 childLeft
= mpGraph
->Add( SmCaretPos( pChild
, 0 ), left
);
1035 mpRightMost
= childLeft
;
1036 pChild
->Accept( this );
1037 //Set right on mpRightMost from pChild
1038 mpRightMost
->SetRight( bodyLeft
);
1041 pChild
= pSubSup
->GetSubSup( RSUB
);
1043 //Create position in front of pChild
1044 childLeft
= mpGraph
->Add( SmCaretPos( pChild
, 0 ), left
);
1046 mpRightMost
= childLeft
;
1047 pChild
->Accept( this );
1048 //Set right on mpRightMost from pChild
1049 mpRightMost
->SetRight( bodyLeft
);
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 );
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:
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:
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:
1124 * Graph over these, where "left" is before the SmBinVerNode and "right" is after:
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
,
1144 assert(mpRightMost
);
1149 right
= mpGraph
->Add( SmCaretPos( pNode
, 1 ) );
1152 numLeft
= mpGraph
->Add( SmCaretPos( pNum
, 0 ), left
);
1153 left
->SetRight( numLeft
);
1156 mpRightMost
= numLeft
;
1157 pNum
->Accept( this );
1158 mpRightMost
->SetRight( right
);
1159 right
->SetLeft( mpRightMost
);
1162 denomLeft
= mpGraph
->Add( SmCaretPos( pDenom
, 0 ), left
);
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:
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
,
1198 right
= mpGraph
->Add( SmCaretPos( pNode
, 1 ) );
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
);
1209 scriptLeft
= mpGraph
->Add( SmCaretPos( pScript
, 0 ), left
);
1210 mpRightMost
= scriptLeft
;
1211 pScript
->Accept( this );
1212 mpRightMost
->SetRight( right
);
1215 mpRightMost
= right
;
1218 /** Build SmCaretPosGraph for SmBinDiagonalNode
1220 * Lines in an SmBinDiagonalNode:
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
,
1243 right
= mpGraph
->Add( SmCaretPos( pNode
, 1 ) );
1246 leftA
= mpGraph
->Add( SmCaretPos( A
, 0 ), left
);
1247 left
->SetRight( leftA
);
1250 mpRightMost
= leftA
;
1252 rightA
= mpRightMost
;
1255 leftB
= mpGraph
->Add( SmCaretPos( B
, 0 ), rightA
);
1256 rightA
->SetRight( leftB
);
1259 mpRightMost
= leftB
;
1261 mpRightMost
->SetRight( right
);
1262 right
->SetLeft( mpRightMost
);
1265 mpRightMost
= right
;
1268 //Straight forward ( I think )
1269 void SmCaretPosGraphBuildingVisitor::Visit( SmBinHorNode
* pNode
)
1271 for( auto pChild
: *pNode
)
1275 pChild
->Accept( this );
1278 void SmCaretPosGraphBuildingVisitor::Visit( SmUnHorNode
* pNode
)
1280 // Unary operator node
1281 for( auto pChild
: *pNode
)
1285 pChild
->Accept( this );
1289 void SmCaretPosGraphBuildingVisitor::Visit( SmExpressionNode
* pNode
)
1291 for( auto pChild
: *pNode
)
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
)
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
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: |"]; // 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: ∈"]; // Unicode "ELEMENT OF"
1343 * n10 -> n13 [label="2"];
1344 * n13 [label="SmMathSymbolNode: ℤ"]; // Unicode "DOUBLE-STRUCK CAPITAL Z"
1345 * n3 -> n14 [label="2"];
1346 * n14 [label="SmMathSymbolNode: }"];
1350 void SmCaretPosGraphBuildingVisitor::Visit( SmBracebodyNode
* pNode
)
1352 for( auto pChild
: *pNode
)
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
)
1372 pChild
->Accept( this );
1376 /** Build SmCaretPosGraph for SmRootNode
1378 * Lines in an SmRootNode:
1385 * A: pExtra ( optional, can be NULL ),
1388 * Graph over these, where "left" is before the SmRootNode and "right" is after:
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
1403 SmCaretPosGraphEntry
*left
,
1408 //Get left and save it
1409 assert(mpRightMost
);
1413 bodyLeft
= mpGraph
->Add( SmCaretPos( pBody
, 0 ), left
);
1414 left
->SetRight( bodyLeft
);
1417 right
= mpGraph
->Add( SmCaretPos( pNode
, 1 ) );
1420 mpRightMost
= bodyLeft
;
1421 pBody
->Accept( this );
1422 bodyRight
= mpRightMost
;
1423 bodyRight
->SetRight( right
);
1424 right
->SetLeft( bodyRight
);
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:
1477 * Graph over these, where "left" is before the SmBraceNode and "right" is after:
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
);
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:
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
1517 void SmCaretPosGraphBuildingVisitor::Visit( SmAttributNode
* pNode
)
1519 SmNode
*pAttr
= pNode
->Attribute(),
1520 *pBody
= pNode
->Body();
1524 SmCaretPosGraphEntry
*left
= mpRightMost
,
1531 bodyLeft
= mpGraph
->Add( SmCaretPos( pBody
, 0 ), left
);
1532 left
->SetRight( bodyLeft
);
1535 right
= mpGraph
->Add( SmCaretPos( pNode
, 1 ) );
1538 mpRightMost
= bodyLeft
;
1539 pBody
->Accept( this );
1540 bodyRight
= mpRightMost
;
1541 bodyRight
->SetRight( right
);
1542 right
->SetLeft( bodyRight
);
1545 attrLeft
= mpGraph
->Add( SmCaretPos( pAttr
, 0 ), left
);
1548 mpRightMost
= attrLeft
;
1549 pAttr
->Accept( this );
1550 mpRightMost
->SetRight( right
);
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
* )
1581 void SmCaretPosGraphBuildingVisitor::Visit( SmRectangleNode
* )
1585 void SmCaretPosGraphBuildingVisitor::Visit( SmPolyLineNode
* )
1592 SmNode
* SmCloningVisitor::Clone( SmNode
* pNode
)
1594 SmNode
* pCurrResult
= mpResult
;
1595 pNode
->Accept( this );
1596 SmNode
* pClone
= mpResult
;
1597 mpResult
= pCurrResult
;
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
);
1618 for (size_t i
= 0; i
< nSize
; ++i
)
1621 if( nullptr != ( pKid
= pSource
->GetSubNode( i
) ) )
1622 pKid
->Accept( this );
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
);
1643 void SmCloningVisitor::Visit( SmBraceNode
* pNode
)
1645 SmBraceNode
* pClone
= new SmBraceNode( pNode
->GetToken( ) );
1646 CloneNodeAttr( pNode
, pClone
);
1647 CloneKids( pNode
, pClone
);
1651 void SmCloningVisitor::Visit( SmBracebodyNode
* pNode
)
1653 SmBracebodyNode
* pClone
= new SmBracebodyNode( pNode
->GetToken( ) );
1654 CloneNodeAttr( pNode
, pClone
);
1655 CloneKids( pNode
, pClone
);
1659 void SmCloningVisitor::Visit( SmOperNode
* pNode
)
1661 SmOperNode
* pClone
= new SmOperNode( pNode
->GetToken( ) );
1662 CloneNodeAttr( pNode
, pClone
);
1663 CloneKids( pNode
, pClone
);
1667 void SmCloningVisitor::Visit( SmAlignNode
* pNode
)
1669 SmAlignNode
* pClone
= new SmAlignNode( pNode
->GetToken( ) );
1670 CloneNodeAttr( pNode
, pClone
);
1671 CloneKids( pNode
, pClone
);
1675 void SmCloningVisitor::Visit( SmAttributNode
* pNode
)
1677 SmAttributNode
* pClone
= new SmAttributNode( pNode
->GetToken( ) );
1678 CloneNodeAttr( pNode
, pClone
);
1679 CloneKids( pNode
, 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
);
1692 void SmCloningVisitor::Visit( SmUnHorNode
* pNode
)
1694 SmUnHorNode
* pClone
= new SmUnHorNode( pNode
->GetToken( ) );
1695 CloneNodeAttr( pNode
, pClone
);
1696 CloneKids( pNode
, pClone
);
1700 void SmCloningVisitor::Visit( SmBinHorNode
* pNode
)
1702 SmBinHorNode
* pClone
= new SmBinHorNode( pNode
->GetToken( ) );
1703 CloneNodeAttr( pNode
, pClone
);
1704 CloneKids( pNode
, pClone
);
1708 void SmCloningVisitor::Visit( SmBinVerNode
* pNode
)
1710 SmBinVerNode
* pClone
= new SmBinVerNode( pNode
->GetToken( ) );
1711 CloneNodeAttr( pNode
, pClone
);
1712 CloneKids( pNode
, 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
);
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
);
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
);
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
);
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( ) );
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
);
1797 void SmCloningVisitor::Visit( SmExpressionNode
* pNode
)
1799 SmExpressionNode
* pClone
= new SmExpressionNode( pNode
->GetToken( ) );
1800 CloneNodeAttr( pNode
, pClone
);
1801 CloneKids( pNode
, 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
);
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
);
1839 // SmSelectionDrawingVisitor
1841 SmSelectionDrawingVisitor::SmSelectionDrawingVisitor( OutputDevice
& rDevice
, SmNode
* pTree
, const Point
& rOffset
)
1843 , mbHasSelectionArea( false )
1846 SAL_WARN_IF( !pTree
, "starmath", "pTree can't be null!" );
1848 pTree
->Accept( this );
1850 //Draw selection if there's any
1851 if( !mbHasSelectionArea
) return;
1853 maSelectionArea
.Move( rOffset
.X( ), rOffset
.Y( ) );
1856 mrDev
.Push( PushFlags::LINECOLOR
| PushFlags::FILLCOLOR
);
1858 mrDev
.SetLineColor( );
1859 mrDev
.SetFillColor( COL_LIGHTGRAY
);
1862 mrDev
.DrawRect( maSelectionArea
);
1864 //Restore device state
1868 void SmSelectionDrawingVisitor::ExtendSelectionArea(const tools::Rectangle
& rArea
)
1870 if ( ! mbHasSelectionArea
) {
1871 maSelectionArea
= rArea
;
1872 mbHasSelectionArea
= true;
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)
1888 for( auto pChild
: *static_cast<SmStructureNode
*>(pNode
) )
1892 pChild
->Accept( this );
1896 void SmSelectionDrawingVisitor::Visit( SmTextNode
* pNode
)
1898 if( !pNode
->IsSelected())
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
);
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 ) );
1931 } else if( pNode
->GetToken( ).eType
== TSTACK
) {
1932 Append( "stack{ " );
1934 for( auto pChild
: *pNode
)
1945 LineToText( pChild
);
1949 } else { //Assume it's a toplevel table, containing lines
1951 for( auto pChild
: *pNode
)
1960 Append( "newline" );
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
) {
1976 LineToText( pBody
);
1978 if( pNode
->GetScaleMode( ) == SmScaleMode::Height
)
1980 pLeftBrace
->Accept( this );
1982 pBody
->Accept( this );
1984 if( pNode
->GetScaleMode( ) == SmScaleMode::Height
)
1986 pRightBrace
->Accept( this );
1990 void SmNodeToTextVisitor::Visit( SmBracebodyNode
* pNode
)
1992 for( auto pChild
: *pNode
)
1997 pChild
->Accept( this );
2001 void SmNodeToTextVisitor::Visit( SmOperNode
* pNode
)
2003 Append( pNode
->GetToken( ).aText
);
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
);
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
);
2017 Append( "lsup { " );
2018 LineToText( pChild
);
2021 pChild
= pSubSup
->GetSubSup( LSUB
);
2024 Append( "lsub { " );
2025 LineToText( pChild
);
2028 pChild
= pSubSup
->GetSubSup( RSUP
);
2032 LineToText( pChild
);
2035 pChild
= pSubSup
->GetSubSup( RSUB
);
2039 LineToText( pChild
);
2042 pChild
= pSubSup
->GetSubSup( CSUP
);
2045 if (pSubSup
->IsUseLimits())
2048 Append( "csup { " );
2049 LineToText( pChild
);
2052 pChild
= pSubSup
->GetSubSup( CSUB
);
2055 if (pSubSup
->IsUseLimits())
2056 Append( "from { " );
2058 Append( "csub { " );
2059 LineToText( pChild
);
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
)
2089 Append( "italic " );
2092 Append( "nitalic " );
2095 Append( "phantom " );
2100 switch ( pNode
->GetSizeType( ) )
2102 case FontSizeType::PLUS
:
2105 case FontSizeType::MINUS
:
2108 case FontSizeType::MULTIPLY
:
2111 case FontSizeType::DIVIDE
:
2114 case FontSizeType::ABSOLUT
:
2118 Append( ::rtl::math::doubleToUString(
2119 static_cast<double>( pNode
->GetSizeParameter( ) ),
2120 rtl_math_StringFormat_Automatic
,
2121 rtl_math_DecimalPlaces_Max
, '.', true ) );
2126 Append( "color black " );
2129 Append( "color white " );
2132 Append( "color red " );
2135 Append( "color green " );
2138 Append( "color blue " );
2141 Append( "color cyan " );
2144 Append( "color magenta " );
2147 Append( "color yellow " );
2150 Append( "font sans " );
2153 Append( "font serif " );
2156 Append( "font fixed " );
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
)
2175 pChild
->Accept( this );
2180 for( auto pChild
: *pNode
)
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();
2200 pLeft
->Accept( this );
2202 pOper
->Accept( this );
2204 pRight
->Accept( this );
2210 void SmNodeToTextVisitor::Visit( SmBinVerNode
* pNode
)
2212 SmNode
*pNum
= pNode
->GetSubNode( 0 ),
2213 *pDenom
= pNode
->GetSubNode( 2 );
2217 LineToText( pDenom
);
2221 void SmNodeToTextVisitor::Visit( SmBinDiagonalNode
* pNode
)
2223 SmNode
*pLeftOperand
= pNode
->GetSubNode( 0 ),
2224 *pRightOperand
= pNode
->GetSubNode( 1 );
2226 LineToText( pLeftOperand
);
2228 Append( "wideslash " );
2229 LineToText( pRightOperand
);
2233 void SmNodeToTextVisitor::Visit( SmSubSupNode
* pNode
)
2235 LineToText( pNode
->GetBody( ) );
2236 SmNode
*pChild
= pNode
->GetSubSup( LSUP
);
2240 LineToText( pChild
);
2242 pChild
= pNode
->GetSubSup( LSUB
);
2246 LineToText( pChild
);
2248 pChild
= pNode
->GetSubSup( RSUP
);
2252 LineToText( pChild
);
2254 pChild
= pNode
->GetSubSup( RSUB
);
2258 LineToText( pChild
);
2260 pChild
= pNode
->GetSubSup( CSUP
);
2263 if (pNode
->IsUseLimits())
2267 LineToText( pChild
);
2269 pChild
= pNode
->GetSubSup( CSUB
);
2272 if (pNode
->IsUseLimits())
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
);
2289 pSubNode
->Accept( this );
2291 if (j
!= pNode
->GetNumCols() - 1U)
2295 if (i
!= pNode
->GetNumRows() - 1U)
2301 void SmNodeToTextVisitor::Visit( SmPlaceNode
* )
2306 void SmNodeToTextVisitor::Visit( SmTextNode
* pNode
)
2308 //TODO: This method might need improvements, see SmTextNode::CreateTextFromNode
2309 if( pNode
->GetToken( ).eType
== TTEXT
)
2311 Append( pNode
->GetText( ) );
2312 if( pNode
->GetToken( ).eType
== TTEXT
)
2316 void SmNodeToTextVisitor::Visit( SmSpecialNode
* pNode
)
2318 Append( pNode
->GetToken( ).aText
);
2321 void SmNodeToTextVisitor::Visit( SmGlyphSpecialNode
* pNode
)
2323 if( pNode
->GetToken( ).eType
== TBOPER
)
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();
2340 sal_uInt16 nWide
= nNum
/ 4;
2341 sal_uInt16 nNarrow
= nNum
% 4;
2342 for (sal_uInt16 i
= 0; i
< nWide
; i
++)
2344 for (sal_uInt16 i
= 0; i
< nNarrow
; i
++)
2349 void SmNodeToTextVisitor::Visit( SmErrorNode
* )
2353 void SmNodeToTextVisitor::Visit( SmLineNode
* pNode
)
2355 for( auto pChild
: *pNode
)
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();
2372 pParent
&& pParent
->GetType() == SmNodeType::SubSup
&&
2373 pNode
->GetNumSubNodes() == 1 &&
2374 pNode
->GetSubNode(0)->GetType() == SmNodeType::SubSup
;
2377 if (bracketsNeeded
) {
2380 for( auto pChild
: *pNode
)
2384 pChild
->Accept( this );
2387 if (bracketsNeeded
) {
2392 void SmNodeToTextVisitor::Visit( SmPolyLineNode
* )
2396 void SmNodeToTextVisitor::Visit( SmRootNode
* pNode
)
2398 SmNode
*pExtra
= pNode
->GetSubNode( 0 ),
2399 *pBody
= pNode
->GetSubNode( 2 );
2402 LineToText( pExtra
);
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: */