LCOV - code coverage report
Current view: top level - svgio/source/svgreader - svgtextpathnode.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 0 163 0.0 %
Date: 2014-04-11 Functions: 0 15 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <svgio/svgreader/svgtextpathnode.hxx>
      21             : #include <svgio/svgreader/svgstyleattributes.hxx>
      22             : #include <svgio/svgreader/svgpathnode.hxx>
      23             : #include <svgio/svgreader/svgdocument.hxx>
      24             : #include <svgio/svgreader/svgtrefnode.hxx>
      25             : #include <basegfx/polygon/b2dpolygon.hxx>
      26             : #include <basegfx/polygon/b2dpolygontools.hxx>
      27             : #include <drawinglayer/primitive2d/textbreakuphelper.hxx>
      28             : #include <drawinglayer/primitive2d/groupprimitive2d.hxx>
      29             : #include <basegfx/curve/b2dcubicbezier.hxx>
      30             : #include <basegfx/curve/b2dbeziertools.hxx>
      31             : 
      32             : 
      33             : 
      34             : namespace svgio
      35             : {
      36             :     namespace svgreader
      37             :     {
      38             :         class pathTextBreakupHelper : public drawinglayer::primitive2d::TextBreakupHelper
      39             :         {
      40             :         private:
      41             :             const basegfx::B2DPolygon&      mrPolygon;
      42             :             const double                    mfBasegfxPathLength;
      43             :             double                          mfPosition;
      44             :             const basegfx::B2DPoint&        mrTextStart;
      45             : 
      46             :             const sal_uInt32                mnMaxIndex;
      47             :             sal_uInt32                      mnIndex;
      48             :             basegfx::B2DCubicBezier         maCurrentSegment;
      49             :             basegfx::B2DCubicBezierHelper*  mpB2DCubicBezierHelper;
      50             :             double                          mfCurrentSegmentLength;
      51             :             double                          mfSegmentStartPosition;
      52             : 
      53             :         protected:
      54             :             /// allow user callback to allow changes to the new TextTransformation. Default
      55             :             /// does nothing.
      56             :             virtual bool allowChange(sal_uInt32 nCount, basegfx::B2DHomMatrix& rNewTransform, sal_uInt32 nIndex, sal_uInt32 nLength) SAL_OVERRIDE;
      57             : 
      58             :             void freeB2DCubicBezierHelper();
      59             :             basegfx::B2DCubicBezierHelper* getB2DCubicBezierHelper();
      60             :             void advanceToPosition(double fNewPosition);
      61             : 
      62             :         public:
      63             :             pathTextBreakupHelper(
      64             :                 const drawinglayer::primitive2d::TextSimplePortionPrimitive2D& rSource,
      65             :                 const basegfx::B2DPolygon& rPolygon,
      66             :                 const double fBasegfxPathLength,
      67             :                 double fPosition,
      68             :                 const basegfx::B2DPoint& rTextStart);
      69             :             virtual ~pathTextBreakupHelper();
      70             : 
      71             :             // read access to evtl. advanced position
      72           0 :             double getPosition() const { return mfPosition; }
      73             :         };
      74             : 
      75           0 :         void pathTextBreakupHelper::freeB2DCubicBezierHelper()
      76             :         {
      77           0 :             if(mpB2DCubicBezierHelper)
      78             :             {
      79           0 :                 delete mpB2DCubicBezierHelper;
      80           0 :                 mpB2DCubicBezierHelper = 0;
      81             :             }
      82           0 :         }
      83             : 
      84           0 :         basegfx::B2DCubicBezierHelper* pathTextBreakupHelper::getB2DCubicBezierHelper()
      85             :         {
      86           0 :             if(!mpB2DCubicBezierHelper && maCurrentSegment.isBezier())
      87             :             {
      88           0 :                 mpB2DCubicBezierHelper = new basegfx::B2DCubicBezierHelper(maCurrentSegment);
      89             :             }
      90             : 
      91           0 :             return mpB2DCubicBezierHelper;
      92             :         }
      93             : 
      94           0 :         void pathTextBreakupHelper::advanceToPosition(double fNewPosition)
      95             :         {
      96           0 :             while(mfSegmentStartPosition + mfCurrentSegmentLength < fNewPosition && mnIndex < mnMaxIndex)
      97             :             {
      98           0 :                 mfSegmentStartPosition += mfCurrentSegmentLength;
      99           0 :                 mnIndex++;
     100             : 
     101           0 :                 if(mnIndex < mnMaxIndex)
     102             :                 {
     103           0 :                     freeB2DCubicBezierHelper();
     104           0 :                     mrPolygon.getBezierSegment(mnIndex % mrPolygon.count(), maCurrentSegment);
     105           0 :                     maCurrentSegment.testAndSolveTrivialBezier();
     106           0 :                     mfCurrentSegmentLength = getB2DCubicBezierHelper()
     107           0 :                         ? getB2DCubicBezierHelper()->getLength()
     108           0 :                         : maCurrentSegment.getLength();
     109             :                 }
     110             :             }
     111             : 
     112           0 :             mfPosition = fNewPosition;
     113           0 :         }
     114             : 
     115           0 :         pathTextBreakupHelper::pathTextBreakupHelper(
     116             :             const drawinglayer::primitive2d::TextSimplePortionPrimitive2D& rSource,
     117             :             const basegfx::B2DPolygon& rPolygon,
     118             :             const double fBasegfxPathLength,
     119             :             double fPosition,
     120             :             const basegfx::B2DPoint& rTextStart)
     121             :         :   drawinglayer::primitive2d::TextBreakupHelper(rSource),
     122             :             mrPolygon(rPolygon),
     123             :             mfBasegfxPathLength(fBasegfxPathLength),
     124             :             mfPosition(0.0),
     125             :             mrTextStart(rTextStart),
     126           0 :             mnMaxIndex(rPolygon.isClosed() ? rPolygon.count() : rPolygon.count() - 1),
     127             :             mnIndex(0),
     128             :             maCurrentSegment(),
     129             :             mpB2DCubicBezierHelper(0),
     130             :             mfCurrentSegmentLength(0.0),
     131           0 :             mfSegmentStartPosition(0.0)
     132             :         {
     133           0 :             mrPolygon.getBezierSegment(mnIndex % mrPolygon.count(), maCurrentSegment);
     134           0 :             mfCurrentSegmentLength = maCurrentSegment.getLength();
     135             : 
     136           0 :             advanceToPosition(fPosition);
     137           0 :         }
     138             : 
     139           0 :         pathTextBreakupHelper::~pathTextBreakupHelper()
     140             :         {
     141           0 :             freeB2DCubicBezierHelper();
     142           0 :         }
     143             : 
     144           0 :         bool pathTextBreakupHelper::allowChange(sal_uInt32 /*nCount*/, basegfx::B2DHomMatrix& rNewTransform, sal_uInt32 nIndex, sal_uInt32 nLength)
     145             :         {
     146           0 :             bool bRetval(false);
     147             : 
     148           0 :             if(mfPosition < mfBasegfxPathLength && nLength && mnIndex < mnMaxIndex)
     149             :             {
     150             :                 const double fSnippetWidth(
     151           0 :                     getTextLayouter().getTextWidth(
     152           0 :                         getSource().getText(),
     153             :                         nIndex,
     154           0 :                         nLength));
     155             : 
     156           0 :                 if(basegfx::fTools::more(fSnippetWidth, 0.0))
     157             :                 {
     158           0 :                     const OUString aText(getSource().getText());
     159           0 :                     const OUString aTrimmedChars(aText.copy(nIndex, nLength).trim());
     160           0 :                     const double fEndPos(mfPosition + fSnippetWidth);
     161             : 
     162           0 :                     if(!aTrimmedChars.isEmpty() && (mfPosition < mfBasegfxPathLength || fEndPos > 0.0))
     163             :                     {
     164           0 :                         const double fHalfSnippetWidth(fSnippetWidth * 0.5);
     165             : 
     166           0 :                         advanceToPosition(mfPosition + fHalfSnippetWidth);
     167             : 
     168             :                         // create representation for this snippet
     169           0 :                         bRetval = true;
     170             : 
     171             :                         // get target position and tangent in that pint
     172           0 :                         basegfx::B2DPoint aPosition(0.0, 0.0);
     173           0 :                         basegfx::B2DVector aTangent(0.0, 1.0);
     174             : 
     175           0 :                         if(mfPosition < 0.0)
     176             :                         {
     177             :                             // snippet center is left of first segment, but right edge is on it (SVG allows that)
     178           0 :                             aTangent = maCurrentSegment.getTangent(0.0);
     179           0 :                             aTangent.normalize();
     180           0 :                             aPosition = maCurrentSegment.getStartPoint() + (aTangent * (mfPosition - mfSegmentStartPosition));
     181             :                         }
     182           0 :                         else if(mfPosition > mfBasegfxPathLength)
     183             :                         {
     184             :                             // snippet center is right of last segment, but left edge is on it (SVG allows that)
     185           0 :                             aTangent = maCurrentSegment.getTangent(1.0);
     186           0 :                             aTangent.normalize();
     187           0 :                             aPosition = maCurrentSegment.getEndPoint() + (aTangent * (mfPosition - mfSegmentStartPosition));
     188             :                         }
     189             :                         else
     190             :                         {
     191             :                             // snippet center inside segment, interpolate
     192           0 :                             double fBezierDistance(mfPosition - mfSegmentStartPosition);
     193             : 
     194           0 :                             if(getB2DCubicBezierHelper())
     195             :                             {
     196             :                                 // use B2DCubicBezierHelper to bridge the non-linear gap between
     197             :                                 // length and bezier distances (if it's a bezier segment)
     198           0 :                                 fBezierDistance = getB2DCubicBezierHelper()->distanceToRelative(fBezierDistance);
     199             :                             }
     200             :                             else
     201             :                             {
     202             :                                 // linear relationship, make relative to segment length
     203           0 :                                 fBezierDistance = fBezierDistance / mfCurrentSegmentLength;
     204             :                             }
     205             : 
     206           0 :                             aPosition = maCurrentSegment.interpolatePoint(fBezierDistance);
     207           0 :                             aTangent = maCurrentSegment.getTangent(fBezierDistance);
     208           0 :                             aTangent.normalize();
     209             :                         }
     210             : 
     211             :                         // detect evtl. hor/ver translations (depends on text direction)
     212           0 :                         const basegfx::B2DPoint aBasePoint(rNewTransform * basegfx::B2DPoint(0.0, 0.0));
     213           0 :                         const basegfx::B2DVector aOffset(aBasePoint - mrTextStart);
     214             : 
     215           0 :                         if(!basegfx::fTools::equalZero(aOffset.getY()))
     216             :                         {
     217             :                             // ...and apply
     218           0 :                             aPosition.setY(aPosition.getY() + aOffset.getY());
     219             :                         }
     220             : 
     221             :                         // move target position from snippet center to left text start
     222           0 :                         aPosition -= fHalfSnippetWidth * aTangent;
     223             : 
     224             :                         // remove current translation
     225           0 :                         rNewTransform.translate(-aBasePoint.getX(), -aBasePoint.getY());
     226             : 
     227             :                         // rotate due to tangent
     228           0 :                         rNewTransform.rotate(atan2(aTangent.getY(), aTangent.getX()));
     229             : 
     230             :                         // add new translation
     231           0 :                         rNewTransform.translate(aPosition.getX(), aPosition.getY());
     232             :                     }
     233             : 
     234             :                     // advance to end
     235           0 :                     advanceToPosition(fEndPos);
     236             :                 }
     237             :             }
     238             : 
     239           0 :             return bRetval;
     240             :         }
     241             : 
     242             :     } // end of namespace svgreader
     243             : } // end of namespace svgio
     244             : 
     245             : 
     246             : 
     247             : namespace svgio
     248             : {
     249             :     namespace svgreader
     250             :     {
     251           0 :         SvgTextPathNode::SvgTextPathNode(
     252             :             SvgDocument& rDocument,
     253             :             SvgNode* pParent)
     254             :         :   SvgNode(SVGTokenTextPath, rDocument, pParent),
     255             :             maSvgStyleAttributes(*this),
     256             :             maXLink(),
     257             :             maStartOffset(),
     258             :             mbMethod(true),
     259           0 :             mbSpacing(false)
     260             :         {
     261           0 :         }
     262             : 
     263           0 :         SvgTextPathNode::~SvgTextPathNode()
     264             :         {
     265           0 :         }
     266             : 
     267           0 :         const SvgStyleAttributes* SvgTextPathNode::getSvgStyleAttributes() const
     268             :         {
     269           0 :             return &maSvgStyleAttributes;
     270             :         }
     271             : 
     272           0 :         void SvgTextPathNode::parseAttribute(const OUString& rTokenName, SVGToken aSVGToken, const OUString& aContent)
     273             :         {
     274             :             // call parent
     275           0 :             SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
     276             : 
     277             :             // read style attributes
     278           0 :             maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent);
     279             : 
     280             :             // parse own
     281           0 :             switch(aSVGToken)
     282             :             {
     283             :                 case SVGTokenStyle:
     284             :                 {
     285           0 :                     maSvgStyleAttributes.readStyle(aContent);
     286           0 :                     break;
     287             :                 }
     288             :                 case SVGTokenStartOffset:
     289             :                 {
     290           0 :                     SvgNumber aNum;
     291             : 
     292           0 :                     if(readSingleNumber(aContent, aNum))
     293             :                     {
     294           0 :                         if(aNum.isPositive())
     295             :                         {
     296           0 :                             setStartOffset(aNum);
     297             :                         }
     298             :                     }
     299           0 :                     break;
     300             :                 }
     301             :                 case SVGTokenMethod:
     302             :                 {
     303           0 :                     if(!aContent.isEmpty())
     304             :                     {
     305           0 :                         if(aContent.startsWith("align"))
     306             :                         {
     307           0 :                             setMethod(true);
     308             :                         }
     309           0 :                         else if(aContent.startsWith("stretch"))
     310             :                         {
     311           0 :                             setMethod(false);
     312             :                         }
     313             :                     }
     314           0 :                     break;
     315             :                 }
     316             :                 case SVGTokenSpacing:
     317             :                 {
     318           0 :                     if(!aContent.isEmpty())
     319             :                     {
     320           0 :                         if(aContent.startsWith("auto"))
     321             :                         {
     322           0 :                             setSpacing(true);
     323             :                         }
     324           0 :                         else if(aContent.startsWith("exact"))
     325             :                         {
     326           0 :                             setSpacing(false);
     327             :                         }
     328             :                     }
     329           0 :                     break;
     330             :                 }
     331             :                 case SVGTokenXlinkHref:
     332             :                 {
     333           0 :                     const sal_Int32 nLen(aContent.getLength());
     334             : 
     335           0 :                     if(nLen && '#' == aContent[0])
     336             :                     {
     337           0 :                         maXLink = aContent.copy(1);
     338             :                     }
     339           0 :                     break;
     340             :                 }
     341             :                 default:
     342             :                 {
     343           0 :                     break;
     344             :                 }
     345             :             }
     346           0 :         }
     347             : 
     348           0 :         bool SvgTextPathNode::isValid() const
     349             :         {
     350           0 :             const SvgPathNode* pSvgPathNode = dynamic_cast< const SvgPathNode* >(getDocument().findSvgNodeById(maXLink));
     351             : 
     352           0 :             if(!pSvgPathNode)
     353             :             {
     354           0 :                 return false;
     355             :             }
     356             : 
     357           0 :             const basegfx::B2DPolyPolygon* pPolyPolyPath = pSvgPathNode->getPath();
     358             : 
     359           0 :             if(!pPolyPolyPath || !pPolyPolyPath->count())
     360             :             {
     361           0 :                 return false;
     362             :             }
     363             : 
     364           0 :             const basegfx::B2DPolygon aPolygon(pPolyPolyPath->getB2DPolygon(0));
     365             : 
     366           0 :             if(!aPolygon.count())
     367             :             {
     368           0 :                 return false;
     369             :             }
     370             : 
     371           0 :             const double fBasegfxPathLength(basegfx::tools::getLength(aPolygon));
     372             : 
     373           0 :             if(basegfx::fTools::equalZero(fBasegfxPathLength))
     374             :             {
     375           0 :                 return false;
     376             :             }
     377             : 
     378           0 :             return true;
     379             :         }
     380             : 
     381           0 :         void SvgTextPathNode::decomposePathNode(
     382             :             const drawinglayer::primitive2d::Primitive2DSequence& rPathContent,
     383             :             drawinglayer::primitive2d::Primitive2DSequence& rTarget,
     384             :             const basegfx::B2DPoint& rTextStart) const
     385             :         {
     386           0 :             if(rPathContent.hasElements())
     387             :             {
     388           0 :                 const SvgPathNode* pSvgPathNode = dynamic_cast< const SvgPathNode* >(getDocument().findSvgNodeById(maXLink));
     389             : 
     390           0 :                 if(pSvgPathNode)
     391             :                 {
     392           0 :                     const basegfx::B2DPolyPolygon* pPolyPolyPath = pSvgPathNode->getPath();
     393             : 
     394           0 :                     if(pPolyPolyPath && pPolyPolyPath->count())
     395             :                     {
     396           0 :                         basegfx::B2DPolygon aPolygon(pPolyPolyPath->getB2DPolygon(0));
     397             : 
     398           0 :                         if(pSvgPathNode->getTransform())
     399             :                         {
     400           0 :                             aPolygon.transform(*pSvgPathNode->getTransform());
     401             :                         }
     402             : 
     403           0 :                         const double fBasegfxPathLength(basegfx::tools::getLength(aPolygon));
     404             : 
     405           0 :                         if(!basegfx::fTools::equalZero(fBasegfxPathLength))
     406             :                         {
     407           0 :                             double fUserToBasegfx(1.0); // multiply: user->basegfx, divide: basegfx->user
     408             : 
     409           0 :                             if(pSvgPathNode->getPathLength().isSet())
     410             :                             {
     411           0 :                                 const double fUserLength(pSvgPathNode->getPathLength().solve(*this, length));
     412             : 
     413           0 :                                 if(fUserLength > 0.0 && !basegfx::fTools::equal(fUserLength, fBasegfxPathLength))
     414             :                                 {
     415           0 :                                     fUserToBasegfx = fUserLength / fBasegfxPathLength;
     416             :                                 }
     417             :                             }
     418             : 
     419           0 :                             double fPosition(0.0);
     420             : 
     421           0 :                             if(getStartOffset().isSet())
     422             :                             {
     423           0 :                                 if(Unit_percent == getStartOffset().getUnit())
     424             :                                 {
     425             :                                     // percent are relative to path length
     426           0 :                                     fPosition = getStartOffset().getNumber() * 0.01 * fBasegfxPathLength;
     427             :                                 }
     428             :                                 else
     429             :                                 {
     430           0 :                                     fPosition = getStartOffset().solve(*this, length) * fUserToBasegfx;
     431             :                                 }
     432             :                             }
     433             : 
     434           0 :                             if(fPosition >= 0.0)
     435             :                             {
     436           0 :                                 const sal_Int32 nLength(rPathContent.getLength());
     437           0 :                                 sal_Int32 nCurrent(0);
     438             : 
     439           0 :                                 while(fPosition < fBasegfxPathLength && nCurrent < nLength)
     440             :                                 {
     441           0 :                                     const drawinglayer::primitive2d::TextSimplePortionPrimitive2D* pCandidate = 0;
     442           0 :                                     const drawinglayer::primitive2d::Primitive2DReference xReference(rPathContent[nCurrent]);
     443             : 
     444           0 :                                     if(xReference.is())
     445             :                                     {
     446           0 :                                         pCandidate = dynamic_cast< const drawinglayer::primitive2d::TextSimplePortionPrimitive2D* >(xReference.get());
     447             :                                     }
     448             : 
     449           0 :                                     if(pCandidate)
     450             :                                     {
     451             :                                         const pathTextBreakupHelper aPathTextBreakupHelper(
     452             :                                             *pCandidate,
     453             :                                             aPolygon,
     454             :                                             fBasegfxPathLength,
     455             :                                             fPosition,
     456           0 :                                             rTextStart);
     457             : 
     458             :                                         const drawinglayer::primitive2d::Primitive2DSequence aResult(
     459           0 :                                             aPathTextBreakupHelper.getResult(drawinglayer::primitive2d::BreakupUnit_character));
     460             : 
     461           0 :                                         if(aResult.hasElements())
     462             :                                         {
     463           0 :                                             drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget, aResult);
     464             :                                         }
     465             : 
     466             :                                         // advance position to consumed
     467           0 :                                         fPosition = aPathTextBreakupHelper.getPosition();
     468             :                                     }
     469             : 
     470           0 :                                     nCurrent++;
     471           0 :                                 }
     472             :                             }
     473           0 :                         }
     474             :                     }
     475             :                 }
     476             :             }
     477           0 :         }
     478             : 
     479             :     } // end of namespace svgreader
     480             : } // end of namespace svgio
     481             : 
     482             : 
     483             : // eof
     484             : 
     485             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10