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