LCOV - code coverage report
Current view: top level - sdext/source/pdfimport/tree - pdfiprocessor.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 314 409 76.8 %
Date: 2014-04-11 Functions: 34 44 77.3 %
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             : 
      21             : #include "pdfiprocessor.hxx"
      22             : #include "xmlemitter.hxx"
      23             : #include "pdfihelper.hxx"
      24             : #include "imagecontainer.hxx"
      25             : #include "genericelements.hxx"
      26             : #include "style.hxx"
      27             : #include "treevisiting.hxx"
      28             : 
      29             : #include <rtl/string.hxx>
      30             : #include <rtl/strbuf.hxx>
      31             : 
      32             : #include <comphelper/sequence.hxx>
      33             : #include <basegfx/polygon/b2dpolypolygontools.hxx>
      34             : #include <basegfx/polygon/b2dpolygonclipper.hxx>
      35             : #include <basegfx/polygon/b2dpolygontools.hxx>
      36             : #include <basegfx/tools/canvastools.hxx>
      37             : #include <basegfx/matrix/b2dhommatrix.hxx>
      38             : #include <basegfx/range/b2irange.hxx>
      39             : #include <basegfx/range/b2drectangle.hxx>
      40             : #include <basegfx/matrix/b2dhommatrixtools.hxx>
      41             : #include <vcl/svapp.hxx>
      42             : 
      43             : #include <com/sun/star/rendering/XVolatileBitmap.hpp>
      44             : #include <com/sun/star/geometry/RealSize2D.hpp>
      45             : #include <com/sun/star/geometry/RealPoint2D.hpp>
      46             : #include <com/sun/star/geometry/RealRectangle2D.hpp>
      47             : 
      48             : 
      49             : using namespace com::sun::star;
      50             : 
      51             : 
      52             : namespace pdfi
      53             : {
      54             : 
      55           2 :  PDFIProcessor::PDFIProcessor( const uno::Reference< task::XStatusIndicator >& xStat ,
      56             :             com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >  xContext) :
      57             : 
      58             :     m_xContext(xContext),
      59             :     prevCharWidth(0),
      60             :     m_pElFactory( new ElementFactory() ),
      61           2 :     m_pDocument( m_pElFactory->createDocumentElement() ),
      62             :     m_pCurPage(0),
      63             :     m_pCurElement(0),
      64             :     m_nNextFontId( 1 ),
      65             :     m_aIdToFont(),
      66             :     m_aFontToId(),
      67             :     m_aGCStack(),
      68             :     m_nNextGCId( 1 ),
      69             :     m_aIdToGC(),
      70             :     m_aGCToId(),
      71             :     m_aImages(),
      72             :     m_eTextDirection( LrTb ),
      73             :     m_nPages(0),
      74             :     m_nNextZOrder( 1 ),
      75             :     m_xStatusIndicator( xStat ),
      76           4 :     m_bHaveTextOnDocLevel(false)
      77             : {
      78           2 :     FontAttributes aDefFont;
      79           2 :     aDefFont.familyName = "Helvetica";
      80           2 :     aDefFont.isBold     = false;
      81           2 :     aDefFont.isItalic   = false;
      82           2 :     aDefFont.size       = 10*PDFI_OUTDEV_RESOLUTION/72;
      83           2 :     m_aIdToFont[ 0 ]    = aDefFont;
      84           2 :     m_aFontToId[ aDefFont ] = 0;
      85             : 
      86           4 :     GraphicsContext aDefGC;
      87           2 :     m_aGCStack.push_back( aDefGC );
      88           2 :     m_aIdToGC[ 0 ] = aDefGC;
      89           4 :     m_aGCToId[ aDefGC ] = 0;
      90           2 : }
      91             : 
      92           0 : void PDFIProcessor::enableToplevelText()
      93             : {
      94           0 :     m_bHaveTextOnDocLevel = true;
      95           0 : }
      96             : 
      97           2 : void PDFIProcessor::setPageNum( sal_Int32 nPages )
      98             : {
      99           2 :     m_nPages = nPages;
     100           2 : }
     101             : 
     102             : 
     103          32 : void PDFIProcessor::pushState()
     104             : {
     105          32 :     GraphicsContextStack::value_type const a(m_aGCStack.back());
     106          32 :     m_aGCStack.push_back(a);
     107          32 : }
     108             : 
     109          32 : void PDFIProcessor::popState()
     110             : {
     111          32 :     m_aGCStack.pop_back();
     112          32 : }
     113             : 
     114           2 : void PDFIProcessor::setFlatness( double value )
     115             : {
     116           2 :     getCurrentContext().Flatness = value;
     117           2 : }
     118             : 
     119           4 : void PDFIProcessor::setTransformation( const geometry::AffineMatrix2D& rMatrix )
     120             : {
     121             :     basegfx::unotools::homMatrixFromAffineMatrix(
     122           4 :         getCurrentContext().Transformation,
     123           4 :         rMatrix );
     124           4 : }
     125             : 
     126           4 : void PDFIProcessor::setLineDash( const uno::Sequence<double>& dashes,
     127             :                                  double                       /*start*/ )
     128             : {
     129             :     // TODO(F2): factor in start offset
     130           4 :     GraphicsContext& rContext( getCurrentContext() );
     131           4 :     comphelper::sequenceToContainer(rContext.DashArray,dashes);
     132           4 : }
     133             : 
     134           6 : void PDFIProcessor::setLineJoin(sal_Int8 nJoin)
     135             : {
     136           6 :     getCurrentContext().LineJoin = nJoin;
     137           6 : }
     138             : 
     139           6 : void PDFIProcessor::setLineCap(sal_Int8 nCap)
     140             : {
     141           6 :     getCurrentContext().LineCap = nCap;
     142           6 : }
     143             : 
     144           2 : void PDFIProcessor::setMiterLimit(double)
     145             : {
     146             :     OSL_TRACE("PDFIProcessor::setMiterLimit(): not supported by ODF");
     147           2 : }
     148             : 
     149           8 : void PDFIProcessor::setLineWidth(double nWidth)
     150             : {
     151           8 :     getCurrentContext().LineWidth = nWidth;
     152           8 : }
     153             : 
     154          28 : void PDFIProcessor::setFillColor( const rendering::ARGBColor& rColor )
     155             : {
     156          28 :     getCurrentContext().FillColor = rColor;
     157          28 : }
     158             : 
     159           8 : void PDFIProcessor::setStrokeColor( const rendering::ARGBColor& rColor )
     160             : {
     161           8 :     getCurrentContext().LineColor = rColor;
     162           8 : }
     163             : 
     164           0 : void PDFIProcessor::setBlendMode(sal_Int8)
     165             : {
     166             :     OSL_TRACE("PDFIProcessor::setBlendMode(): not supported by ODF");
     167           0 : }
     168             : 
     169          22 : void PDFIProcessor::setFont( const FontAttributes& i_rFont )
     170             : {
     171          22 :     FontAttributes aChangedFont( i_rFont );
     172          22 :     GraphicsContext& rGC=getCurrentContext();
     173             :     // for text render modes, please see PDF reference manual
     174          22 :     aChangedFont.isOutline = ( (rGC.TextRenderMode == 1) || (rGC. TextRenderMode == 2) );
     175          22 :     FontToIdMap::const_iterator it = m_aFontToId.find( aChangedFont );
     176          22 :     if( it != m_aFontToId.end() )
     177          14 :         rGC.FontId = it->second;
     178             :     else
     179             :     {
     180           8 :         m_aFontToId[ aChangedFont ] = m_nNextFontId;
     181           8 :         m_aIdToFont[ m_nNextFontId ] = aChangedFont;
     182           8 :         rGC.FontId = m_nNextFontId;
     183           8 :         m_nNextFontId++;
     184          22 :     }
     185          22 : }
     186             : 
     187           0 : void PDFIProcessor::setTextRenderMode( sal_Int32 i_nMode )
     188             : {
     189           0 :     GraphicsContext& rGC=getCurrentContext();
     190           0 :     rGC.TextRenderMode = i_nMode;
     191           0 :     IdToFontMap::iterator it = m_aIdToFont.find( rGC.FontId );
     192           0 :     if( it != m_aIdToFont.end() )
     193           0 :         setFont( it->second );
     194           0 : }
     195             : 
     196           0 : sal_Int32 PDFIProcessor::getFontId( const FontAttributes& rAttr ) const
     197             : {
     198           0 :     const sal_Int32 nCurFont = getCurrentContext().FontId;
     199           0 :     const_cast<PDFIProcessor*>(this)->setFont( rAttr );
     200           0 :     const sal_Int32 nFont = getCurrentContext().FontId;
     201           0 :     const_cast<PDFIProcessor*>(this)->getCurrentContext().FontId = nCurFont;
     202             : 
     203           0 :     return nFont;
     204             : }
     205             : 
     206             : // line diagnose block - start
     207          24 : void PDFIProcessor::processGlyphLine()
     208             : {
     209          24 :     if (m_GlyphsList.empty())
     210          26 :         return;
     211             : 
     212          22 :     double spaceDetectBoundary = 0.0;
     213             : 
     214             :     // Try to find space glyph and its width
     215          94 :     for (size_t i = 0; i < m_GlyphsList.size(); i++)
     216             :     {
     217          90 :         OUString& glyph = m_GlyphsList[i].getGlyph();
     218             : 
     219          90 :         sal_Unicode ch = '\0';
     220          90 :         if (!glyph.isEmpty())
     221          90 :             ch = glyph[0];
     222             : 
     223          90 :         if ((ch == 0x20) || (ch == 0xa0))
     224             :         {
     225          18 :             double spaceWidth = m_GlyphsList[i].getWidth();
     226          18 :             spaceDetectBoundary = spaceWidth * 0.5;
     227          18 :             break;
     228             :         }
     229             :     }
     230             : 
     231             :     // If space glyph is not found, use average glyph width instead
     232          22 :     if (spaceDetectBoundary == 0.0)
     233             :     {
     234           4 :         double avgGlyphWidth = 0.0;
     235           8 :         for (size_t i = 0; i < m_GlyphsList.size(); i++)
     236           4 :             avgGlyphWidth += m_GlyphsList[i].getWidth();
     237           4 :         avgGlyphWidth /= m_GlyphsList.size();
     238           4 :         spaceDetectBoundary = avgGlyphWidth * 0.2;
     239             :     }
     240             : 
     241          22 :     FrameElement* frame = m_pElFactory->createFrameElement(
     242          22 :         m_GlyphsList[0].getCurElement(),
     243          44 :         getGCId(m_GlyphsList[0].getGC()));
     244          22 :     frame->ZOrder = m_nNextZOrder++;
     245          22 :     frame->IsForText = true;
     246          22 :     ParagraphElement* para = m_pElFactory->createParagraphElement(frame);
     247             : 
     248         230 :     for (size_t i = 0; i < m_GlyphsList.size(); i++)
     249             :     {
     250         208 :         bool prependSpace = false;
     251         208 :         TextElement* text = m_pElFactory->createTextElement(
     252             :             para,
     253         208 :             getGCId(m_GlyphsList[i].getGC()),
     254         416 :             m_GlyphsList[i].getGC().FontId);
     255         208 :         if (i == 0)
     256             :         {
     257          22 :             text->x = m_GlyphsList[0].getGC().Transformation.get(0, 2);
     258          22 :             text->y = m_GlyphsList[0].getGC().Transformation.get(1, 2);
     259          22 :             text->w = 0;
     260          22 :             text->h = 0;
     261          22 :             para->updateGeometryWith(text);
     262          22 :             frame->updateGeometryWith(para);
     263             :         }
     264             :         else
     265             :         {
     266         186 :             double spaceSize = m_GlyphsList[i].getPrevSpaceWidth();
     267         186 :             prependSpace = spaceSize > spaceDetectBoundary;
     268             :         }
     269         208 :         if (prependSpace)
     270           0 :             text->Text.append(" ");
     271         208 :         text->Text.append(m_GlyphsList[i].getGlyph());
     272             :     }
     273             : 
     274          22 :     m_GlyphsList.clear();
     275             : }
     276             : 
     277         208 : void PDFIProcessor::drawGlyphs( const OUString&             rGlyphs,
     278             :                                 const geometry::RealRectangle2D& rRect,
     279             :                                 const geometry::Matrix2D&        rFontMatrix )
     280             : {
     281             :     basegfx::B2DHomMatrix totalTextMatrix1(
     282             :         rFontMatrix.m00, rFontMatrix.m01, rRect.X1,
     283         208 :         rFontMatrix.m10, rFontMatrix.m11, rRect.Y1);
     284             :     basegfx::B2DHomMatrix totalTextMatrix2(
     285             :         rFontMatrix.m00, rFontMatrix.m01, rRect.X2,
     286         416 :         rFontMatrix.m10, rFontMatrix.m11, rRect.Y2);
     287         208 :     totalTextMatrix1 *= getCurrentContext().Transformation;
     288         208 :     totalTextMatrix2 *= getCurrentContext().Transformation;
     289             : 
     290         416 :     basegfx::B2DHomMatrix invMatrix(totalTextMatrix1);
     291         416 :     basegfx::B2DHomMatrix invPrevMatrix(prevTextMatrix);
     292         208 :     invMatrix.invert();
     293         208 :     invPrevMatrix.invert();
     294         416 :     basegfx::B2DHomMatrix offsetMatrix1(totalTextMatrix1);
     295         416 :     basegfx::B2DHomMatrix offsetMatrix2(totalTextMatrix2);
     296         208 :     offsetMatrix1 *= invPrevMatrix;
     297         208 :     offsetMatrix2 *= invMatrix;
     298             : 
     299         208 :     double charWidth = offsetMatrix2.get(0, 2);
     300         208 :     double prevSpaceWidth = offsetMatrix1.get(0, 2) - prevCharWidth;
     301             : 
     302         852 :     if ((totalTextMatrix1.get(0, 0) != prevTextMatrix.get(0, 0)) ||
     303         412 :         (totalTextMatrix1.get(0, 1) != prevTextMatrix.get(0, 1)) ||
     304         412 :         (totalTextMatrix1.get(1, 0) != prevTextMatrix.get(1, 0)) ||
     305         412 :         (totalTextMatrix1.get(1, 1) != prevTextMatrix.get(1, 1)) ||
     306         400 :         (offsetMatrix1.get(0, 2) < 0.0) ||
     307        1168 :         (prevSpaceWidth > prevCharWidth * 1.3) ||
     308         952 :         (!basegfx::fTools::equalZero(offsetMatrix1.get(1, 2), 0.0001)))
     309             :     {
     310          22 :         processGlyphLine();
     311             :     }
     312             : 
     313         416 :     CharGlyph aGlyph(m_pCurElement, getCurrentContext(), charWidth, prevSpaceWidth, rGlyphs);
     314         208 :     aGlyph.getGC().Transformation = totalTextMatrix1;
     315         208 :     m_GlyphsList.push_back(aGlyph);
     316             : 
     317         208 :     prevCharWidth = charWidth;
     318         416 :     prevTextMatrix = totalTextMatrix1;
     319         208 : }
     320             : 
     321          22 : void PDFIProcessor::endText()
     322             : {
     323          22 :     TextElement* pText = dynamic_cast<TextElement*>(m_pCurElement);
     324          22 :     if( pText )
     325           0 :         m_pCurElement = pText->Parent;
     326          22 : }
     327             : 
     328           2 : void PDFIProcessor::setupImage(ImageId nImage)
     329             : {
     330           2 :     const GraphicsContext& rGC( getCurrentContext() );
     331             : 
     332           2 :     basegfx::B2DHomMatrix aTrans( rGC.Transformation );
     333             : 
     334             :     // check for rotation, which is the other way around in ODF
     335           4 :     basegfx::B2DTuple aScale, aTranslation;
     336             :     double fRotate, fShearX;
     337           2 :     rGC.Transformation.decompose( aScale, aTranslation, fRotate, fShearX );
     338             :     // TODDO(F4): correcting rotation when fShearX != 0 ?
     339           2 :     if( fRotate != 0.0 )
     340             :     {
     341             : 
     342             :         // try to create a Transformation that corrects for the wrong rotation
     343           0 :         aTrans.identity();
     344           0 :         aTrans.scale( aScale.getX(), aScale.getY() );
     345           0 :         aTrans.rotate( -fRotate );
     346             : 
     347           0 :         basegfx::B2DRange aRect( 0, 0, 1, 1 );
     348           0 :         aRect.transform( aTrans );
     349             : 
     350             :         // TODO(F3) treat translation correctly
     351             :         // the corrections below work for multiples of 90 degree
     352             :         // which is a common case (landscape/portrait/seascape)
     353             :         // we need a general solution here; however this needs to
     354             :         // work in sync with DrawXmlEmitter::fillFrameProps and WriterXmlEmitter::fillFrameProps
     355             :         // admittedly this is a lame workaround and fails for arbitrary rotation
     356           0 :         double fQuadrant = fmod( fRotate, 2.0*M_PI ) / M_PI_2;
     357           0 :         int nQuadrant = (int)fQuadrant;
     358           0 :         if( nQuadrant < 0 )
     359           0 :             nQuadrant += 4;
     360           0 :         if( nQuadrant == 1 )
     361             :         {
     362           0 :             aTranslation.setX( aTranslation.getX() + aRect.getHeight() + aRect.getWidth());
     363           0 :             aTranslation.setY( aTranslation.getY() + aRect.getHeight() );
     364             :         }
     365           0 :         if( nQuadrant == 3 )
     366           0 :             aTranslation.setX( aTranslation.getX() - aRect.getHeight() );
     367             : 
     368             :         aTrans.translate( aTranslation.getX(),
     369           0 :                           aTranslation.getY() );
     370             :     }
     371             : 
     372           2 :     bool bMirrorVertical = aScale.getY() > 0;
     373             : 
     374             :     // transform unit rect to determine view box
     375           2 :     basegfx::B2DRange aRect( 0, 0, 1, 1 );
     376           2 :     aRect.transform( aTrans );
     377             : 
     378             :     // TODO(F3): Handle clip
     379           2 :     const sal_Int32 nGCId = getGCId(rGC);
     380           2 :     FrameElement* pFrame = m_pElFactory->createFrameElement( m_pCurElement, nGCId );
     381           2 :     ImageElement* pImageElement = m_pElFactory->createImageElement( pFrame, nGCId, nImage );
     382           2 :     pFrame->x = pImageElement->x = aRect.getMinX();
     383           2 :     pFrame->y = pImageElement->y = aRect.getMinY();
     384           2 :     pFrame->w = pImageElement->w = aRect.getWidth();
     385           2 :     pFrame->h = pImageElement->h = aRect.getHeight();
     386           2 :     pFrame->ZOrder = m_nNextZOrder++;
     387             : 
     388           2 :     if( bMirrorVertical )
     389             :     {
     390           0 :         pFrame->MirrorVertical = pImageElement->MirrorVertical = true;
     391           0 :         pFrame->x        += aRect.getWidth();
     392           0 :         pImageElement->x += aRect.getWidth();
     393           0 :         pFrame->y        += aRect.getHeight();
     394           0 :         pImageElement->y += aRect.getHeight();
     395           2 :     }
     396           2 : }
     397             : 
     398           0 : void PDFIProcessor::drawMask(const uno::Sequence<beans::PropertyValue>& xBitmap,
     399             :                              bool                                       /*bInvert*/ )
     400             : {
     401             :     // TODO(F3): Handle mask and inversion
     402           0 :     setupImage( m_aImages.addImage(xBitmap) );
     403           0 : }
     404             : 
     405           2 : void PDFIProcessor::drawImage(const uno::Sequence<beans::PropertyValue>& xBitmap )
     406             : {
     407           2 :     setupImage( m_aImages.addImage(xBitmap) );
     408           2 : }
     409             : 
     410           0 : void PDFIProcessor::drawColorMaskedImage(const uno::Sequence<beans::PropertyValue>& xBitmap,
     411             :                                          const uno::Sequence<uno::Any>&             /*xMaskColors*/ )
     412             : {
     413             :     // TODO(F3): Handle mask colors
     414           0 :     setupImage( m_aImages.addImage(xBitmap) );
     415           0 : }
     416             : 
     417           0 : void PDFIProcessor::drawMaskedImage(const uno::Sequence<beans::PropertyValue>& xBitmap,
     418             :                                     const uno::Sequence<beans::PropertyValue>& /*xMask*/,
     419             :                                     bool                                       /*bInvertMask*/)
     420             : {
     421             :     // TODO(F3): Handle mask and inversion
     422           0 :     setupImage( m_aImages.addImage(xBitmap) );
     423           0 : }
     424             : 
     425           0 : void PDFIProcessor::drawAlphaMaskedImage(const uno::Sequence<beans::PropertyValue>& xBitmap,
     426             :                                          const uno::Sequence<beans::PropertyValue>& /*xMask*/)
     427             : {
     428             :     // TODO(F3): Handle mask
     429             : 
     430           0 :     setupImage( m_aImages.addImage(xBitmap) );
     431             : 
     432           0 : }
     433             : 
     434           4 : void PDFIProcessor::strokePath( const uno::Reference< rendering::XPolyPolygon2D >& rPath )
     435             : {
     436           4 :     basegfx::B2DPolyPolygon aPoly=basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(rPath);
     437           4 :     aPoly.transform(getCurrentContext().Transformation);
     438             : 
     439           4 :     PolyPolyElement* pPoly = m_pElFactory->createPolyPolyElement(
     440             :         m_pCurElement,
     441           4 :         getGCId(getCurrentContext()),
     442             :         aPoly,
     443           8 :         PATH_STROKE );
     444           4 :     pPoly->updateGeometry();
     445           4 :     pPoly->ZOrder = m_nNextZOrder++;
     446           4 : }
     447             : 
     448           0 : void PDFIProcessor::fillPath( const uno::Reference< rendering::XPolyPolygon2D >& rPath )
     449             : {
     450           0 :     basegfx::B2DPolyPolygon aPoly=basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(rPath);
     451           0 :     aPoly.transform(getCurrentContext().Transformation);
     452             : 
     453           0 :     PolyPolyElement* pPoly = m_pElFactory->createPolyPolyElement(
     454             :         m_pCurElement,
     455           0 :         getGCId(getCurrentContext()),
     456             :         aPoly,
     457           0 :         PATH_FILL );
     458           0 :     pPoly->updateGeometry();
     459           0 :     pPoly->ZOrder = m_nNextZOrder++;
     460           0 : }
     461             : 
     462           2 : void PDFIProcessor::eoFillPath( const uno::Reference< rendering::XPolyPolygon2D >& rPath )
     463             : {
     464           2 :     basegfx::B2DPolyPolygon aPoly=basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(rPath);
     465           2 :     aPoly.transform(getCurrentContext().Transformation);
     466             : 
     467           2 :     PolyPolyElement* pPoly = m_pElFactory->createPolyPolyElement(
     468             :         m_pCurElement,
     469           2 :         getGCId(getCurrentContext()),
     470             :         aPoly,
     471           4 :         PATH_EOFILL );
     472           2 :     pPoly->updateGeometry();
     473           2 :     pPoly->ZOrder = m_nNextZOrder++;
     474           2 : }
     475             : 
     476           2 : void PDFIProcessor::intersectClip(const uno::Reference< rendering::XPolyPolygon2D >& rPath)
     477             : {
     478             :     // TODO(F3): interpret fill mode
     479           2 :     basegfx::B2DPolyPolygon aNewClip = basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(rPath);
     480           2 :     aNewClip.transform(getCurrentContext().Transformation);
     481           4 :     basegfx::B2DPolyPolygon aCurClip = getCurrentContext().Clip;
     482             : 
     483           2 :     if( aCurClip.count() )  // #i92985# adapted API from (..., false, false) to (..., true, false)
     484           2 :         aNewClip = basegfx::tools::clipPolyPolygonOnPolyPolygon( aCurClip, aNewClip, true, false );
     485             : 
     486           4 :     getCurrentContext().Clip = aNewClip;
     487           2 : }
     488             : 
     489           2 : void PDFIProcessor::intersectEoClip(const uno::Reference< rendering::XPolyPolygon2D >& rPath)
     490             : {
     491             :     // TODO(F3): interpret fill mode
     492           2 :     basegfx::B2DPolyPolygon aNewClip = basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(rPath);
     493           2 :     aNewClip.transform(getCurrentContext().Transformation);
     494           4 :     basegfx::B2DPolyPolygon aCurClip = getCurrentContext().Clip;
     495             : 
     496           2 :     if( aCurClip.count() )  // #i92985# adapted API from (..., false, false) to (..., true, false)
     497           2 :         aNewClip = basegfx::tools::clipPolyPolygonOnPolyPolygon( aCurClip, aNewClip, true, false );
     498             : 
     499           4 :     getCurrentContext().Clip = aNewClip;
     500           2 : }
     501             : 
     502           2 : void PDFIProcessor::hyperLink( const geometry::RealRectangle2D& rBounds,
     503             :                                const OUString&           rURI )
     504             : {
     505           2 :     if( !rURI.isEmpty() )
     506             :     {
     507           2 :         HyperlinkElement* pLink = m_pElFactory->createHyperlinkElement(
     508             :             &m_pCurPage->Hyperlinks,
     509           2 :             rURI );
     510           2 :         pLink->x = rBounds.X1;
     511           2 :         pLink->y = rBounds.Y1;
     512           2 :         pLink->w = rBounds.X2-rBounds.X1;
     513           2 :         pLink->h = rBounds.Y2-rBounds.Y1;
     514             :     }
     515           2 : }
     516             : 
     517         115 : const FontAttributes& PDFIProcessor::getFont( sal_Int32 nFontId ) const
     518             : {
     519         115 :     IdToFontMap::const_iterator it = m_aIdToFont.find( nFontId );
     520         115 :     if( it == m_aIdToFont.end() )
     521           0 :         it = m_aIdToFont.find( 0 );
     522         115 :     return it->second;
     523             : }
     524             : 
     525         238 : sal_Int32 PDFIProcessor::getGCId( const GraphicsContext& rGC )
     526             : {
     527         238 :     sal_Int32 nGCId = 0;
     528         238 :     GCToIdMap::const_iterator it = m_aGCToId.find( rGC );
     529         238 :     if( it != m_aGCToId.end() )
     530          22 :         nGCId = it->second;
     531             :     else
     532             :     {
     533         216 :         m_aGCToId[ rGC ] = m_nNextGCId;
     534         216 :         m_aIdToGC[ m_nNextGCId ] = rGC;
     535         216 :         nGCId = m_nNextGCId;
     536         216 :         m_nNextGCId++;
     537             :     }
     538             : 
     539         238 :     return nGCId;
     540             : }
     541             : 
     542         549 : const GraphicsContext& PDFIProcessor::getGraphicsContext( sal_Int32 nGCId ) const
     543             : {
     544         549 :     IdToGCMap::const_iterator it = m_aIdToGC.find( nGCId );
     545         549 :     if( it == m_aIdToGC.end() )
     546           0 :         it = m_aIdToGC.find( 0 );
     547         549 :     return it->second;
     548             : }
     549             : 
     550           2 : void PDFIProcessor::endPage()
     551             : {
     552           2 :     processGlyphLine(); // draw last line
     553           4 :     if( m_xStatusIndicator.is()
     554           0 :         && m_pCurPage
     555           2 :         && m_pCurPage->PageNumber == m_nPages
     556             :     )
     557           0 :         m_xStatusIndicator->end();
     558           2 : }
     559             : 
     560           2 : void PDFIProcessor::startPage( const geometry::RealSize2D& rSize )
     561             : {
     562             :     // initial clip is to page bounds
     563           4 :     getCurrentContext().Clip = basegfx::B2DPolyPolygon(
     564             :         basegfx::tools::createPolygonFromRect(
     565           2 :             basegfx::B2DRange( 0, 0, rSize.Width, rSize.Height )));
     566             : 
     567           2 :     sal_Int32 nNextPageNr = m_pCurPage ? m_pCurPage->PageNumber+1 : 1;
     568           2 :     if( m_xStatusIndicator.is() )
     569             :     {
     570           0 :         if( nNextPageNr == 1 )
     571           0 :             startIndicator( OUString( " " ) );
     572           0 :         m_xStatusIndicator->setValue( nNextPageNr );
     573             :     }
     574           2 :     m_pCurPage = m_pElFactory->createPageElement(m_pDocument.get(), nNextPageNr);
     575           2 :     m_pCurElement = m_pCurPage;
     576           2 :     m_pCurPage->w = rSize.Width;
     577           2 :     m_pCurPage->h = rSize.Height;
     578           2 :     m_nNextZOrder = 1;
     579             : 
     580             : 
     581           2 : }
     582             : 
     583           2 : void PDFIProcessor::emit( XmlEmitter&               rEmitter,
     584             :                           const TreeVisitorFactory& rVisitorFactory )
     585             : {
     586             : #if OSL_DEBUG_LEVEL > 1
     587             :     m_pDocument->emitStructure( 0 );
     588             : #endif
     589             : 
     590             :     ElementTreeVisitorSharedPtr optimizingVisitor(
     591           2 :         rVisitorFactory.createOptimizingVisitor(*this));
     592             :     // FIXME: localization
     593           2 :     startIndicator( OUString( " " ) );
     594           2 :     m_pDocument->visitedBy( *optimizingVisitor, std::list<Element*>::const_iterator());
     595             : 
     596             : #if OSL_DEBUG_LEVEL > 1
     597             :     m_pDocument->emitStructure( 0 );
     598             : #endif
     599             : 
     600             :     // get styles
     601           4 :     StyleContainer aStyles;
     602             :     ElementTreeVisitorSharedPtr finalizingVisitor(
     603           4 :         rVisitorFactory.createStyleCollectingVisitor(aStyles,*this));
     604             :     // FIXME: localization
     605             : 
     606           2 :     m_pDocument->visitedBy( *finalizingVisitor, std::list<Element*>::const_iterator() );
     607             : 
     608           4 :     EmitContext aContext( rEmitter, aStyles, m_aImages, *this, m_xStatusIndicator, m_xContext );
     609             :     ElementTreeVisitorSharedPtr aEmittingVisitor(
     610           4 :         rVisitorFactory.createEmittingVisitor(aContext, *this));
     611             : 
     612           4 :     PropertyMap aProps;
     613             :     // document prolog
     614             :     #define OASIS_STR "urn:oasis:names:tc:opendocument:xmlns:"
     615           2 :     aProps[ "xmlns:office" ]      = OASIS_STR "office:1.0" ;
     616           2 :     aProps[ "xmlns:style" ]       = OASIS_STR "style:1.0" ;
     617           2 :     aProps[ "xmlns:text" ]        = OASIS_STR "text:1.0" ;
     618           2 :     aProps[ "xmlns:svg" ]         = OASIS_STR "svg-compatible:1.0" ;
     619           2 :     aProps[ "xmlns:table" ]       = OASIS_STR "table:1.0" ;
     620           2 :     aProps[ "xmlns:draw" ]        = OASIS_STR "drawing:1.0" ;
     621           2 :     aProps[ "xmlns:fo" ]          = OASIS_STR "xsl-fo-compatible:1.0" ;
     622           2 :     aProps[ "xmlns:xlink"]        = "http://www.w3.org/1999/xlink";
     623           2 :     aProps[ "xmlns:dc"]           = "http://purl.org/dc/elements/1.1/";
     624           2 :     aProps[ "xmlns:number"]       = OASIS_STR "datastyle:1.0" ;
     625           2 :     aProps[ "xmlns:presentation"] = OASIS_STR "presentation:1.0" ;
     626           2 :     aProps[ "xmlns:math"]         = "http://www.w3.org/1998/Math/MathML";
     627           2 :     aProps[ "xmlns:form"]         = OASIS_STR "form:1.0" ;
     628           2 :     aProps[ "xmlns:script"]       = OASIS_STR "script:1.0" ;
     629           2 :     aProps[ "xmlns:dom"]          = "http://www.w3.org/2001/xml-events";
     630           2 :     aProps[ "xmlns:xforms"]       = "http://www.w3.org/2002/xforms";
     631           2 :     aProps[ "xmlns:xsd"]          = "http://www.w3.org/2001/XMLSchema";
     632           2 :     aProps[ "xmlns:xsi"]          = "http://www.w3.org/2001/XMLSchema-instance";
     633           2 :     aProps[ "office:version" ]    = "1.0";
     634           2 :     aProps[ "office:version" ]    = "1.0";
     635             : 
     636           2 :     aContext.rEmitter.beginTag( "office:document", aProps );
     637             : 
     638             :     // emit style list
     639           2 :     aStyles.emit( aContext, *aEmittingVisitor );
     640             : 
     641           2 :     m_pDocument->visitedBy( *aEmittingVisitor, std::list<Element*>::const_iterator() );
     642           2 :     aContext.rEmitter.endTag( "office:document" );
     643           4 :     endIndicator();
     644           2 : }
     645             : 
     646           2 : void PDFIProcessor::startIndicator( const OUString& rText, sal_Int32 nElements )
     647             : {
     648           2 :     if( nElements == -1 )
     649           2 :         nElements = m_nPages;
     650           2 :     if( m_xStatusIndicator.is() )
     651             :     {
     652           0 :         sal_Int32 nUnicodes = rText.getLength();
     653           0 :         OUStringBuffer aStr( nUnicodes*2 );
     654           0 :         const sal_Unicode* pText = rText.getStr();
     655           0 :         for( int i = 0; i < nUnicodes; i++ )
     656             :         {
     657           0 :             if( nUnicodes-i > 1&&
     658           0 :                 pText[i]   == '%' &&
     659           0 :                 pText[i+1] == 'd'
     660             :             )
     661             :             {
     662           0 :                 aStr.append( nElements );
     663           0 :                 i++;
     664             :             }
     665             :             else
     666           0 :                 aStr.append( pText[i] );
     667             :         }
     668           0 :         m_xStatusIndicator->start( aStr.makeStringAndClear(), nElements );
     669             :     }
     670           2 : }
     671             : 
     672           2 : void PDFIProcessor::endIndicator()
     673             : {
     674           2 :     if( m_xStatusIndicator.is() )
     675           0 :         m_xStatusIndicator->end();
     676           2 : }
     677             : 
     678          90 : static bool lr_tb_sort( Element* pLeft, Element* pRight )
     679             : {
     680             :     // first: top-bottom sorting
     681             : 
     682             :     // Note: allow for 10% overlap on text lines since text lines are usually
     683             :     // of the same order as font height whereas the real paint area
     684             :     // of text is usually smaller
     685          90 :     double fudge_factor = 1.0;
     686          90 :     if( dynamic_cast< TextElement* >(pLeft) || dynamic_cast< TextElement* >(pRight) )
     687           0 :         fudge_factor = 0.9;
     688             : 
     689             :     // if left's lower boundary is above right's upper boundary
     690             :     // then left is smaller
     691          90 :     if( pLeft->y+pLeft->h*fudge_factor < pRight->y )
     692          26 :         return true;
     693             :     // if right's lower boundary is above left's upper boundary
     694             :     // then left is definitely not smaller
     695          64 :     if( pRight->y+pRight->h*fudge_factor < pLeft->y )
     696          46 :         return false;
     697             : 
     698             :     // by now we have established that left and right are inside
     699             :     // a "line", that is they have vertical overlap
     700             :     // second: left-right sorting
     701             :     // if left's right boundary is left to right's left boundary
     702             :     // then left is smaller
     703          18 :     if( pLeft->x+pLeft->w < pRight->x )
     704          10 :         return true;
     705             :     // if right's right boundary is left to left's left boundary
     706             :     // then left is definitely not smaller
     707           8 :     if( pRight->x+pRight->w < pLeft->x )
     708           2 :         return false;
     709             : 
     710             :     // here we have established vertical and horizontal overlap
     711             :     // so sort left first, top second
     712           6 :     if( pLeft->x < pRight->x )
     713           2 :         return true;
     714           4 :     if( pRight->x < pLeft->x )
     715           4 :         return false;
     716           0 :     if( pLeft->y < pRight->y )
     717           0 :         return true;
     718             : 
     719           0 :     return false;
     720             : }
     721             : 
     722           2 : void PDFIProcessor::sortElements( Element* pEle, bool bDeep )
     723             : {
     724           2 :     if( pEle->Children.empty() )
     725           2 :         return;
     726             : 
     727           2 :     if( bDeep )
     728             :     {
     729           0 :         for( std::list< Element* >::iterator it = pEle->Children.begin();
     730           0 :              it != pEle->Children.end(); ++it )
     731             :         {
     732           0 :             sortElements( *it, bDeep );
     733             :         }
     734             :     }
     735             :     // HACK: the stable sort member on std::list that takes a
     736             :     // strict weak ordering requires member templates - which we
     737             :     // do not have on all compilers. so we need to use std::stable_sort
     738             :     // here - which does need random access iterators which the
     739             :     // list iterators are not.
     740             :     // so we need to copy the Element* to an array, stable sort that and
     741             :     // copy them back.
     742           2 :     std::vector<Element*> aChildren;
     743          34 :     while( ! pEle->Children.empty() )
     744             :     {
     745          30 :         aChildren.push_back( pEle->Children.front() );
     746          30 :         pEle->Children.pop_front();
     747             :     }
     748           2 :     switch( m_eTextDirection )
     749             :     {
     750             :         case LrTb:
     751             :         default:
     752           2 :         std::stable_sort( aChildren.begin(), aChildren.end(), lr_tb_sort );
     753           2 :         break;
     754             :     }
     755           2 :     int nChildren = aChildren.size();
     756          32 :     for( int i = 0; i < nChildren; i++ )
     757          32 :         pEle->Children.push_back( aChildren[i] );
     758             : }
     759             : 
     760             : // helper method: get a mirrored string
     761           0 : OUString PDFIProcessor::mirrorString( const OUString& i_rString ) const
     762             : {
     763           0 :     const sal_Int32 nLen = i_rString.getLength();
     764           0 :     OUStringBuffer aMirror( nLen );
     765             : 
     766           0 :     sal_Int32 i = 0;
     767           0 :     while(i < nLen)
     768             :     {
     769             :         // read one code point
     770           0 :         const sal_uInt32 nCodePoint = i_rString.iterateCodePoints( &i );
     771             : 
     772             :         // and append it mirrored
     773           0 :         aMirror.appendUtf32( GetMirroredChar(nCodePoint) );
     774             :     }
     775           0 :     return aMirror.makeStringAndClear();
     776             : }
     777             : 
     778             : }
     779             : 
     780             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10