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

Generated by: LCOV version 1.11