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 0 : 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 0 : 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 0 : m_bHaveTextOnDocLevel(false)
77 : {
78 0 : FontAttributes aDefFont;
79 0 : aDefFont.familyName = "Helvetica";
80 0 : aDefFont.isBold = false;
81 0 : aDefFont.isItalic = false;
82 0 : aDefFont.size = 10*PDFI_OUTDEV_RESOLUTION/72;
83 0 : m_aIdToFont[ 0 ] = aDefFont;
84 0 : m_aFontToId[ aDefFont ] = 0;
85 :
86 0 : GraphicsContext aDefGC;
87 0 : m_aGCStack.push_back( aDefGC );
88 0 : m_aIdToGC[ 0 ] = aDefGC;
89 0 : m_aGCToId[ aDefGC ] = 0;
90 0 : }
91 :
92 0 : void PDFIProcessor::enableToplevelText()
93 : {
94 0 : m_bHaveTextOnDocLevel = true;
95 0 : }
96 :
97 0 : void PDFIProcessor::setPageNum( sal_Int32 nPages )
98 : {
99 0 : m_nPages = nPages;
100 0 : }
101 :
102 :
103 0 : void PDFIProcessor::pushState()
104 : {
105 0 : GraphicsContextStack::value_type const a(m_aGCStack.back());
106 0 : m_aGCStack.push_back(a);
107 0 : }
108 :
109 0 : void PDFIProcessor::popState()
110 : {
111 0 : m_aGCStack.pop_back();
112 0 : }
113 :
114 0 : void PDFIProcessor::setFlatness( double value )
115 : {
116 0 : getCurrentContext().Flatness = value;
117 0 : }
118 :
119 0 : void PDFIProcessor::setTransformation( const geometry::AffineMatrix2D& rMatrix )
120 : {
121 : basegfx::unotools::homMatrixFromAffineMatrix(
122 0 : getCurrentContext().Transformation,
123 0 : rMatrix );
124 0 : }
125 :
126 0 : void PDFIProcessor::setLineDash( const uno::Sequence<double>& dashes,
127 : double /*start*/ )
128 : {
129 : // TODO(F2): factor in start offset
130 0 : GraphicsContext& rContext( getCurrentContext() );
131 0 : comphelper::sequenceToContainer(rContext.DashArray,dashes);
132 0 : }
133 :
134 0 : void PDFIProcessor::setLineJoin(sal_Int8 nJoin)
135 : {
136 0 : getCurrentContext().LineJoin = nJoin;
137 0 : }
138 :
139 0 : void PDFIProcessor::setLineCap(sal_Int8 nCap)
140 : {
141 0 : getCurrentContext().LineCap = nCap;
142 0 : }
143 :
144 0 : void PDFIProcessor::setMiterLimit(double)
145 : {
146 : OSL_TRACE("PDFIProcessor::setMiterLimit(): not supported by ODF");
147 0 : }
148 :
149 0 : void PDFIProcessor::setLineWidth(double nWidth)
150 : {
151 0 : getCurrentContext().LineWidth = nWidth;
152 0 : }
153 :
154 0 : void PDFIProcessor::setFillColor( const rendering::ARGBColor& rColor )
155 : {
156 0 : getCurrentContext().FillColor = rColor;
157 0 : }
158 :
159 0 : void PDFIProcessor::setStrokeColor( const rendering::ARGBColor& rColor )
160 : {
161 0 : getCurrentContext().LineColor = rColor;
162 0 : }
163 :
164 0 : void PDFIProcessor::setBlendMode(sal_Int8)
165 : {
166 : OSL_TRACE("PDFIProcessor::setBlendMode(): not supported by ODF");
167 0 : }
168 :
169 0 : void PDFIProcessor::setFont( const FontAttributes& i_rFont )
170 : {
171 0 : FontAttributes aChangedFont( i_rFont );
172 0 : GraphicsContext& rGC=getCurrentContext();
173 : // for text render modes, please see PDF reference manual
174 0 : aChangedFont.isOutline = ( (rGC.TextRenderMode == 1) || (rGC. TextRenderMode == 2) );
175 0 : FontToIdMap::const_iterator it = m_aFontToId.find( aChangedFont );
176 0 : if( it != m_aFontToId.end() )
177 0 : rGC.FontId = it->second;
178 : else
179 : {
180 0 : m_aFontToId[ aChangedFont ] = m_nNextFontId;
181 0 : m_aIdToFont[ m_nNextFontId ] = aChangedFont;
182 0 : rGC.FontId = m_nNextFontId;
183 0 : m_nNextFontId++;
184 0 : }
185 0 : }
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 0 : void PDFIProcessor::processGlyphLine()
208 : {
209 0 : if (m_GlyphsList.empty())
210 0 : return;
211 :
212 0 : double spaceDetectBoundary = 0.0;
213 :
214 : // Try to find space glyph and its width
215 0 : for (size_t i = 0; i < m_GlyphsList.size(); i++)
216 : {
217 0 : OUString& glyph = m_GlyphsList[i].getGlyph();
218 :
219 0 : sal_Unicode ch = '\0';
220 0 : if (!glyph.isEmpty())
221 0 : ch = glyph[0];
222 :
223 0 : if ((ch == 0x20) || (ch == 0xa0))
224 : {
225 0 : double spaceWidth = m_GlyphsList[i].getWidth();
226 0 : spaceDetectBoundary = spaceWidth * 0.5;
227 0 : break;
228 : }
229 : }
230 :
231 : // If space glyph is not found, use average glyph width instead
232 0 : if (spaceDetectBoundary == 0.0)
233 : {
234 0 : double avgGlyphWidth = 0.0;
235 0 : for (size_t i = 0; i < m_GlyphsList.size(); i++)
236 0 : avgGlyphWidth += m_GlyphsList[i].getWidth();
237 0 : avgGlyphWidth /= m_GlyphsList.size();
238 0 : spaceDetectBoundary = avgGlyphWidth * 0.2;
239 : }
240 :
241 0 : FrameElement* frame = m_pElFactory->createFrameElement(
242 0 : m_GlyphsList[0].getCurElement(),
243 0 : getGCId(m_GlyphsList[0].getGC()));
244 0 : frame->ZOrder = m_nNextZOrder++;
245 0 : frame->IsForText = true;
246 0 : ParagraphElement* para = m_pElFactory->createParagraphElement(frame);
247 :
248 0 : for (size_t i = 0; i < m_GlyphsList.size(); i++)
249 : {
250 0 : bool prependSpace = false;
251 0 : TextElement* text = m_pElFactory->createTextElement(
252 : para,
253 0 : getGCId(m_GlyphsList[i].getGC()),
254 0 : m_GlyphsList[i].getGC().FontId);
255 0 : if (i == 0)
256 : {
257 0 : text->x = m_GlyphsList[0].getGC().Transformation.get(0, 2);
258 0 : text->y = m_GlyphsList[0].getGC().Transformation.get(1, 2);
259 0 : text->w = 0;
260 0 : text->h = 0;
261 0 : para->updateGeometryWith(text);
262 0 : frame->updateGeometryWith(para);
263 : }
264 : else
265 : {
266 0 : double spaceSize = m_GlyphsList[i].getPrevSpaceWidth();
267 0 : prependSpace = spaceSize > spaceDetectBoundary;
268 : }
269 0 : if (prependSpace)
270 0 : text->Text.append(" ");
271 0 : text->Text.append(m_GlyphsList[i].getGlyph());
272 : }
273 :
274 0 : m_GlyphsList.clear();
275 : }
276 :
277 0 : 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 0 : rFontMatrix.m10, rFontMatrix.m11, rRect.Y1);
284 : basegfx::B2DHomMatrix totalTextMatrix2(
285 : rFontMatrix.m00, rFontMatrix.m01, rRect.X2,
286 0 : rFontMatrix.m10, rFontMatrix.m11, rRect.Y2);
287 0 : totalTextMatrix1 *= getCurrentContext().Transformation;
288 0 : totalTextMatrix2 *= getCurrentContext().Transformation;
289 :
290 0 : basegfx::B2DHomMatrix invMatrix(totalTextMatrix1);
291 0 : basegfx::B2DHomMatrix invPrevMatrix(prevTextMatrix);
292 0 : invMatrix.invert();
293 0 : invPrevMatrix.invert();
294 0 : basegfx::B2DHomMatrix offsetMatrix1(totalTextMatrix1);
295 0 : basegfx::B2DHomMatrix offsetMatrix2(totalTextMatrix2);
296 0 : offsetMatrix1 *= invPrevMatrix;
297 0 : offsetMatrix2 *= invMatrix;
298 :
299 0 : double charWidth = offsetMatrix2.get(0, 2);
300 0 : double prevSpaceWidth = offsetMatrix1.get(0, 2) - prevCharWidth;
301 :
302 0 : if ((totalTextMatrix1.get(0, 0) != prevTextMatrix.get(0, 0)) ||
303 0 : (totalTextMatrix1.get(0, 1) != prevTextMatrix.get(0, 1)) ||
304 0 : (totalTextMatrix1.get(1, 0) != prevTextMatrix.get(1, 0)) ||
305 0 : (totalTextMatrix1.get(1, 1) != prevTextMatrix.get(1, 1)) ||
306 0 : (offsetMatrix1.get(0, 2) < 0.0) ||
307 0 : (prevSpaceWidth > prevCharWidth * 1.3) ||
308 0 : (!basegfx::fTools::equalZero(offsetMatrix1.get(1, 2), 0.0001)))
309 : {
310 0 : processGlyphLine();
311 : }
312 :
313 0 : CharGlyph aGlyph(m_pCurElement, getCurrentContext(), charWidth, prevSpaceWidth, rGlyphs);
314 0 : aGlyph.getGC().Transformation = totalTextMatrix1;
315 0 : m_GlyphsList.push_back(aGlyph);
316 :
317 0 : prevCharWidth = charWidth;
318 0 : prevTextMatrix = totalTextMatrix1;
319 0 : }
320 :
321 0 : void PDFIProcessor::endText()
322 : {
323 0 : TextElement* pText = dynamic_cast<TextElement*>(m_pCurElement);
324 0 : if( pText )
325 0 : m_pCurElement = pText->Parent;
326 0 : }
327 :
328 0 : void PDFIProcessor::setupImage(ImageId nImage)
329 : {
330 0 : const GraphicsContext& rGC( getCurrentContext() );
331 :
332 0 : basegfx::B2DHomMatrix aTrans( rGC.Transformation );
333 :
334 : // check for rotation, which is the other way around in ODF
335 0 : basegfx::B2DTuple aScale, aTranslation;
336 : double fRotate, fShearX;
337 0 : rGC.Transformation.decompose( aScale, aTranslation, fRotate, fShearX );
338 : // TODDO(F4): correcting rotation when fShearX != 0 ?
339 0 : 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 0 : bool bMirrorVertical = aScale.getY() > 0;
373 :
374 : // transform unit rect to determine view box
375 0 : basegfx::B2DRange aRect( 0, 0, 1, 1 );
376 0 : aRect.transform( aTrans );
377 :
378 : // TODO(F3): Handle clip
379 0 : const sal_Int32 nGCId = getGCId(rGC);
380 0 : FrameElement* pFrame = m_pElFactory->createFrameElement( m_pCurElement, nGCId );
381 0 : ImageElement* pImageElement = m_pElFactory->createImageElement( pFrame, nGCId, nImage );
382 0 : pFrame->x = pImageElement->x = aRect.getMinX();
383 0 : pFrame->y = pImageElement->y = aRect.getMinY();
384 0 : pFrame->w = pImageElement->w = aRect.getWidth();
385 0 : pFrame->h = pImageElement->h = aRect.getHeight();
386 0 : pFrame->ZOrder = m_nNextZOrder++;
387 :
388 0 : 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 0 : }
396 0 : }
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 0 : void PDFIProcessor::drawImage(const uno::Sequence<beans::PropertyValue>& xBitmap )
406 : {
407 0 : setupImage( m_aImages.addImage(xBitmap) );
408 0 : }
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 0 : void PDFIProcessor::strokePath( const uno::Reference< rendering::XPolyPolygon2D >& rPath )
435 : {
436 0 : basegfx::B2DPolyPolygon aPoly=basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(rPath);
437 0 : aPoly.transform(getCurrentContext().Transformation);
438 :
439 0 : PolyPolyElement* pPoly = m_pElFactory->createPolyPolyElement(
440 : m_pCurElement,
441 0 : getGCId(getCurrentContext()),
442 : aPoly,
443 0 : PATH_STROKE );
444 0 : pPoly->updateGeometry();
445 0 : pPoly->ZOrder = m_nNextZOrder++;
446 0 : }
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 0 : void PDFIProcessor::eoFillPath( const uno::Reference< rendering::XPolyPolygon2D >& rPath )
463 : {
464 0 : basegfx::B2DPolyPolygon aPoly=basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(rPath);
465 0 : aPoly.transform(getCurrentContext().Transformation);
466 :
467 0 : PolyPolyElement* pPoly = m_pElFactory->createPolyPolyElement(
468 : m_pCurElement,
469 0 : getGCId(getCurrentContext()),
470 : aPoly,
471 0 : PATH_EOFILL );
472 0 : pPoly->updateGeometry();
473 0 : pPoly->ZOrder = m_nNextZOrder++;
474 0 : }
475 :
476 0 : void PDFIProcessor::intersectClip(const uno::Reference< rendering::XPolyPolygon2D >& rPath)
477 : {
478 : // TODO(F3): interpret fill mode
479 0 : basegfx::B2DPolyPolygon aNewClip = basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(rPath);
480 0 : aNewClip.transform(getCurrentContext().Transformation);
481 0 : basegfx::B2DPolyPolygon aCurClip = getCurrentContext().Clip;
482 :
483 0 : if( aCurClip.count() ) // #i92985# adapted API from (..., false, false) to (..., true, false)
484 0 : aNewClip = basegfx::tools::clipPolyPolygonOnPolyPolygon( aCurClip, aNewClip, true, false );
485 :
486 0 : getCurrentContext().Clip = aNewClip;
487 0 : }
488 :
489 0 : void PDFIProcessor::intersectEoClip(const uno::Reference< rendering::XPolyPolygon2D >& rPath)
490 : {
491 : // TODO(F3): interpret fill mode
492 0 : basegfx::B2DPolyPolygon aNewClip = basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(rPath);
493 0 : aNewClip.transform(getCurrentContext().Transformation);
494 0 : basegfx::B2DPolyPolygon aCurClip = getCurrentContext().Clip;
495 :
496 0 : if( aCurClip.count() ) // #i92985# adapted API from (..., false, false) to (..., true, false)
497 0 : aNewClip = basegfx::tools::clipPolyPolygonOnPolyPolygon( aCurClip, aNewClip, true, false );
498 :
499 0 : getCurrentContext().Clip = aNewClip;
500 0 : }
501 :
502 0 : void PDFIProcessor::hyperLink( const geometry::RealRectangle2D& rBounds,
503 : const OUString& rURI )
504 : {
505 0 : if( !rURI.isEmpty() )
506 : {
507 0 : HyperlinkElement* pLink = m_pElFactory->createHyperlinkElement(
508 : &m_pCurPage->Hyperlinks,
509 0 : rURI );
510 0 : pLink->x = rBounds.X1;
511 0 : pLink->y = rBounds.Y1;
512 0 : pLink->w = rBounds.X2-rBounds.X1;
513 0 : pLink->h = rBounds.Y2-rBounds.Y1;
514 : }
515 0 : }
516 :
517 0 : const FontAttributes& PDFIProcessor::getFont( sal_Int32 nFontId ) const
518 : {
519 0 : IdToFontMap::const_iterator it = m_aIdToFont.find( nFontId );
520 0 : if( it == m_aIdToFont.end() )
521 0 : it = m_aIdToFont.find( 0 );
522 0 : return it->second;
523 : }
524 :
525 0 : sal_Int32 PDFIProcessor::getGCId( const GraphicsContext& rGC )
526 : {
527 0 : sal_Int32 nGCId = 0;
528 0 : GCToIdMap::const_iterator it = m_aGCToId.find( rGC );
529 0 : if( it != m_aGCToId.end() )
530 0 : nGCId = it->second;
531 : else
532 : {
533 0 : m_aGCToId[ rGC ] = m_nNextGCId;
534 0 : m_aIdToGC[ m_nNextGCId ] = rGC;
535 0 : nGCId = m_nNextGCId;
536 0 : m_nNextGCId++;
537 : }
538 :
539 0 : return nGCId;
540 : }
541 :
542 0 : const GraphicsContext& PDFIProcessor::getGraphicsContext( sal_Int32 nGCId ) const
543 : {
544 0 : IdToGCMap::const_iterator it = m_aIdToGC.find( nGCId );
545 0 : if( it == m_aIdToGC.end() )
546 0 : it = m_aIdToGC.find( 0 );
547 0 : return it->second;
548 : }
549 :
550 0 : void PDFIProcessor::endPage()
551 : {
552 0 : processGlyphLine(); // draw last line
553 0 : if( m_xStatusIndicator.is()
554 0 : && m_pCurPage
555 0 : && m_pCurPage->PageNumber == m_nPages
556 : )
557 0 : m_xStatusIndicator->end();
558 0 : }
559 :
560 0 : void PDFIProcessor::startPage( const geometry::RealSize2D& rSize )
561 : {
562 : // initial clip is to page bounds
563 0 : getCurrentContext().Clip = basegfx::B2DPolyPolygon(
564 : basegfx::tools::createPolygonFromRect(
565 0 : basegfx::B2DRange( 0, 0, rSize.Width, rSize.Height )));
566 :
567 0 : sal_Int32 nNextPageNr = m_pCurPage ? m_pCurPage->PageNumber+1 : 1;
568 0 : if( m_xStatusIndicator.is() )
569 : {
570 0 : if( nNextPageNr == 1 )
571 0 : startIndicator( OUString( " " ) );
572 0 : m_xStatusIndicator->setValue( nNextPageNr );
573 : }
574 0 : m_pCurPage = m_pElFactory->createPageElement(m_pDocument.get(), nNextPageNr);
575 0 : m_pCurElement = m_pCurPage;
576 0 : m_pCurPage->w = rSize.Width;
577 0 : m_pCurPage->h = rSize.Height;
578 0 : m_nNextZOrder = 1;
579 :
580 :
581 0 : }
582 :
583 0 : 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 0 : rVisitorFactory.createOptimizingVisitor(*this));
592 : // FIXME: localization
593 0 : startIndicator( OUString( " " ) );
594 0 : 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 0 : StyleContainer aStyles;
602 : ElementTreeVisitorSharedPtr finalizingVisitor(
603 0 : rVisitorFactory.createStyleCollectingVisitor(aStyles,*this));
604 : // FIXME: localization
605 :
606 0 : m_pDocument->visitedBy( *finalizingVisitor, std::list<Element*>::const_iterator() );
607 :
608 0 : EmitContext aContext( rEmitter, aStyles, m_aImages, *this, m_xStatusIndicator, m_xContext );
609 : ElementTreeVisitorSharedPtr aEmittingVisitor(
610 0 : rVisitorFactory.createEmittingVisitor(aContext, *this));
611 :
612 0 : PropertyMap aProps;
613 : // document prolog
614 : #define OASIS_STR "urn:oasis:names:tc:opendocument:xmlns:"
615 0 : aProps[ "xmlns:office" ] = OASIS_STR "office:1.0" ;
616 0 : aProps[ "xmlns:style" ] = OASIS_STR "style:1.0" ;
617 0 : aProps[ "xmlns:text" ] = OASIS_STR "text:1.0" ;
618 0 : aProps[ "xmlns:svg" ] = OASIS_STR "svg-compatible:1.0" ;
619 0 : aProps[ "xmlns:table" ] = OASIS_STR "table:1.0" ;
620 0 : aProps[ "xmlns:draw" ] = OASIS_STR "drawing:1.0" ;
621 0 : aProps[ "xmlns:fo" ] = OASIS_STR "xsl-fo-compatible:1.0" ;
622 0 : aProps[ "xmlns:xlink"] = "http://www.w3.org/1999/xlink";
623 0 : aProps[ "xmlns:dc"] = "http://purl.org/dc/elements/1.1/";
624 0 : aProps[ "xmlns:number"] = OASIS_STR "datastyle:1.0" ;
625 0 : aProps[ "xmlns:presentation"] = OASIS_STR "presentation:1.0" ;
626 0 : aProps[ "xmlns:math"] = "http://www.w3.org/1998/Math/MathML";
627 0 : aProps[ "xmlns:form"] = OASIS_STR "form:1.0" ;
628 0 : aProps[ "xmlns:script"] = OASIS_STR "script:1.0" ;
629 0 : aProps[ "xmlns:dom"] = "http://www.w3.org/2001/xml-events";
630 0 : aProps[ "xmlns:xforms"] = "http://www.w3.org/2002/xforms";
631 0 : aProps[ "xmlns:xsd"] = "http://www.w3.org/2001/XMLSchema";
632 0 : aProps[ "xmlns:xsi"] = "http://www.w3.org/2001/XMLSchema-instance";
633 0 : aProps[ "office:version" ] = "1.0";
634 0 : aProps[ "office:version" ] = "1.0";
635 :
636 0 : aContext.rEmitter.beginTag( "office:document", aProps );
637 :
638 : // emit style list
639 0 : aStyles.emit( aContext, *aEmittingVisitor );
640 :
641 0 : m_pDocument->visitedBy( *aEmittingVisitor, std::list<Element*>::const_iterator() );
642 0 : aContext.rEmitter.endTag( "office:document" );
643 0 : endIndicator();
644 0 : }
645 :
646 0 : void PDFIProcessor::startIndicator( const OUString& rText, sal_Int32 nElements )
647 : {
648 0 : if( nElements == -1 )
649 0 : nElements = m_nPages;
650 0 : 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 0 : }
671 :
672 0 : void PDFIProcessor::endIndicator()
673 : {
674 0 : if( m_xStatusIndicator.is() )
675 0 : m_xStatusIndicator->end();
676 0 : }
677 :
678 0 : 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 0 : double fudge_factor = 1.0;
686 0 : 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 0 : if( pLeft->y+pLeft->h*fudge_factor < pRight->y )
692 0 : return true;
693 : // if right's lower boundary is above left's upper boundary
694 : // then left is definitely not smaller
695 0 : if( pRight->y+pRight->h*fudge_factor < pLeft->y )
696 0 : 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 0 : if( pLeft->x+pLeft->w < pRight->x )
704 0 : return true;
705 : // if right's right boundary is left to left's left boundary
706 : // then left is definitely not smaller
707 0 : if( pRight->x+pRight->w < pLeft->x )
708 0 : return false;
709 :
710 : // here we have established vertical and horizontal overlap
711 : // so sort left first, top second
712 0 : if( pLeft->x < pRight->x )
713 0 : return true;
714 0 : if( pRight->x < pLeft->x )
715 0 : return false;
716 0 : if( pLeft->y < pRight->y )
717 0 : return true;
718 :
719 0 : return false;
720 : }
721 :
722 0 : void PDFIProcessor::sortElements( Element* pEle, bool bDeep )
723 : {
724 0 : if( pEle->Children.empty() )
725 0 : return;
726 :
727 0 : 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 0 : std::vector<Element*> aChildren;
743 0 : while( ! pEle->Children.empty() )
744 : {
745 0 : aChildren.push_back( pEle->Children.front() );
746 0 : pEle->Children.pop_front();
747 : }
748 0 : switch( m_eTextDirection )
749 : {
750 : case LrTb:
751 : default:
752 0 : std::stable_sort( aChildren.begin(), aChildren.end(), lr_tb_sort );
753 0 : break;
754 : }
755 0 : int nChildren = aChildren.size();
756 0 : for( int i = 0; i < nChildren; i++ )
757 0 : 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: */
|