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 "style.hxx"
26 : #include "writertreevisiting.hxx"
27 : #include "genericelements.hxx"
28 :
29 : #include <basegfx/polygon/b2dpolypolygontools.hxx>
30 : #include <basegfx/range/b2drange.hxx>
31 :
32 :
33 : namespace pdfi
34 : {
35 :
36 0 : void WriterXmlEmitter::visit( HyperlinkElement& elem, const std::list< Element* >::const_iterator& )
37 : {
38 0 : if( elem.Children.empty() )
39 0 : return;
40 :
41 0 : const char* pType = dynamic_cast<DrawElement*>(elem.Children.front()) ? "draw:a" : "text:a";
42 :
43 0 : PropertyMap aProps;
44 0 : aProps[ "xlink:type" ] = "simple";
45 0 : aProps[ "xlink:href" ] = elem.URI;
46 0 : aProps[ "office:target-frame-name" ] = "_blank";
47 0 : aProps[ "xlink:show" ] = "new";
48 :
49 0 : m_rEmitContext.rEmitter.beginTag( pType, aProps );
50 0 : std::list< Element* >::iterator this_it = elem.Children.begin();
51 0 : while( this_it !=elem.Children.end() && *this_it != &elem )
52 : {
53 0 : (*this_it)->visitedBy( *this, this_it );
54 0 : ++this_it;
55 : }
56 0 : m_rEmitContext.rEmitter.endTag( pType );
57 : }
58 :
59 0 : void WriterXmlEmitter::visit( TextElement& elem, const std::list< Element* >::const_iterator& )
60 : {
61 0 : if( elem.Text.isEmpty() )
62 0 : return;
63 :
64 0 : PropertyMap aProps;
65 0 : if( elem.StyleId != -1 )
66 : {
67 0 : aProps[ OUString( "text:style-name" ) ] =
68 0 : m_rEmitContext.rStyles.getStyleName( elem.StyleId );
69 : }
70 :
71 0 : m_rEmitContext.rEmitter.beginTag( "text:span", aProps );
72 0 : m_rEmitContext.rEmitter.write( elem.Text.makeStringAndClear() );
73 0 : std::list< Element* >::iterator this_it = elem.Children.begin();
74 0 : while( this_it !=elem.Children.end() && *this_it != &elem )
75 : {
76 0 : (*this_it)->visitedBy( *this, this_it );
77 0 : ++this_it;
78 : }
79 :
80 0 : m_rEmitContext.rEmitter.endTag( "text:span" );
81 : }
82 :
83 0 : void WriterXmlEmitter::visit( ParagraphElement& elem, const std::list< Element* >::const_iterator& )
84 : {
85 0 : PropertyMap aProps;
86 0 : if( elem.StyleId != -1 )
87 : {
88 0 : aProps[ "text:style-name" ] = m_rEmitContext.rStyles.getStyleName( elem.StyleId );
89 : }
90 0 : const char* pTagType = "text:p";
91 0 : if( elem.Type == elem.Headline )
92 0 : pTagType = "text:h";
93 0 : m_rEmitContext.rEmitter.beginTag( pTagType, aProps );
94 :
95 0 : std::list< Element* >::iterator this_it = elem.Children.begin();
96 0 : while( this_it !=elem.Children.end() && *this_it != &elem )
97 : {
98 0 : (*this_it)->visitedBy( *this, this_it );
99 0 : ++this_it;
100 : }
101 :
102 0 : m_rEmitContext.rEmitter.endTag( pTagType );
103 0 : }
104 :
105 0 : void WriterXmlEmitter::fillFrameProps( DrawElement& rElem,
106 : PropertyMap& rProps,
107 : const EmitContext& rEmitContext )
108 : {
109 0 : double rel_x = rElem.x, rel_y = rElem.y;
110 :
111 : // find anchor type by recursing though parents
112 0 : Element* pAnchor = rElem.Parent;
113 0 : while( pAnchor &&
114 0 : ! dynamic_cast<ParagraphElement*>(pAnchor) &&
115 0 : ! dynamic_cast<PageElement*>(pAnchor) )
116 : {
117 0 : pAnchor = pAnchor->Parent;
118 : }
119 0 : if( pAnchor )
120 : {
121 0 : if( dynamic_cast<ParagraphElement*>(pAnchor) )
122 : {
123 0 : rProps[ "text:anchor-type" ] = rElem.isCharacter
124 0 : ? OUString("character") : OUString("paragraph");
125 : }
126 : else
127 : {
128 0 : PageElement* pPage = dynamic_cast<PageElement*>(pAnchor);
129 0 : rProps[ "text:anchor-type" ] = "page";
130 0 : rProps[ "text:anchor-page-number" ] = OUString::number(pPage->PageNumber);
131 : }
132 0 : rel_x -= pAnchor->x;
133 0 : rel_y -= pAnchor->y;
134 : }
135 :
136 0 : rProps[ "draw:z-index" ] = OUString::number( rElem.ZOrder );
137 0 : rProps[ "draw:style-name"] = rEmitContext.rStyles.getStyleName( rElem.StyleId );
138 0 : rProps[ "svg:width" ] = convertPixelToUnitString( rElem.w );
139 0 : rProps[ "svg:height" ] = convertPixelToUnitString( rElem.h );
140 :
141 : const GraphicsContext& rGC =
142 0 : rEmitContext.rProcessor.getGraphicsContext( rElem.GCId );
143 0 : if( rGC.Transformation.isIdentity() )
144 : {
145 0 : if( !rElem.isCharacter )
146 : {
147 0 : rProps[ "svg:x" ] = convertPixelToUnitString( rel_x );
148 0 : rProps[ "svg:y" ] = convertPixelToUnitString( rel_y );
149 : }
150 : }
151 : else
152 : {
153 0 : basegfx::B2DTuple aScale, aTranslation;
154 : double fRotate, fShearX;
155 :
156 0 : rGC.Transformation.decompose( aScale, aTranslation, fRotate, fShearX );
157 :
158 0 : OUStringBuffer aBuf( 256 );
159 :
160 : // TODO(F2): general transformation case missing; if implemented, note
161 : // that ODF rotation is oriented the other way
162 :
163 : // build transformation string
164 0 : if( fShearX != 0.0 )
165 : {
166 0 : aBuf.appendAscii( "skewX( " );
167 0 : aBuf.append( fShearX );
168 0 : aBuf.appendAscii( " )" );
169 : }
170 0 : if( fRotate != 0.0 )
171 : {
172 0 : if( !aBuf.isEmpty() )
173 0 : aBuf.append( ' ' );
174 0 : aBuf.appendAscii( "rotate( " );
175 0 : aBuf.append( -fRotate );
176 0 : aBuf.appendAscii( " )" );
177 :
178 : }
179 0 : if( ! rElem.isCharacter )
180 : {
181 0 : if( !aBuf.isEmpty() )
182 0 : aBuf.append( ' ' );
183 0 : aBuf.appendAscii( "translate( " );
184 0 : aBuf.append( convertPixelToUnitString( rel_x ) );
185 0 : aBuf.append( ' ' );
186 0 : aBuf.append( convertPixelToUnitString( rel_y ) );
187 0 : aBuf.appendAscii( " )" );
188 : }
189 :
190 0 : rProps[ "draw:transform" ] = aBuf.makeStringAndClear();
191 : }
192 0 : }
193 :
194 0 : void WriterXmlEmitter::visit( FrameElement& elem, const std::list< Element* >::const_iterator& )
195 : {
196 0 : if( elem.Children.empty() )
197 0 : return;
198 :
199 0 : bool bTextBox = (dynamic_cast<ParagraphElement*>(elem.Children.front()) != NULL);
200 0 : PropertyMap aFrameProps;
201 0 : fillFrameProps( elem, aFrameProps, m_rEmitContext );
202 0 : m_rEmitContext.rEmitter.beginTag( "draw:frame", aFrameProps );
203 0 : if( bTextBox )
204 0 : m_rEmitContext.rEmitter.beginTag( "draw:text-box", PropertyMap() );
205 :
206 0 : std::list< Element* >::iterator this_it = elem.Children.begin();
207 0 : while( this_it !=elem.Children.end() && *this_it != &elem )
208 : {
209 0 : (*this_it)->visitedBy( *this, this_it );
210 0 : ++this_it;
211 : }
212 :
213 0 : if( bTextBox )
214 0 : m_rEmitContext.rEmitter.endTag( "draw:text-box" );
215 0 : m_rEmitContext.rEmitter.endTag( "draw:frame" );
216 : }
217 :
218 0 : void WriterXmlEmitter::visit( PolyPolyElement& elem, const std::list< Element* >::const_iterator& )
219 : {
220 0 : elem.updateGeometry();
221 : /* note:
222 : * aw recommends using 100dth of mm in all respects since the xml import
223 : * (a) is buggy (see issue 37213)
224 : * (b) is optimized for 100dth of mm and does not scale itself then,
225 : * this does not gain us speed but makes for smaller rounding errors since
226 : * the xml importer coordinates are integer based
227 : */
228 0 : for (sal_uInt32 i = 0; i< elem.PolyPoly.count(); i++)
229 : {
230 0 : basegfx::B2DPolygon b2dPolygon;
231 0 : b2dPolygon = elem.PolyPoly.getB2DPolygon( i );
232 :
233 0 : for ( sal_uInt32 j = 0; j< b2dPolygon.count(); j++ )
234 : {
235 0 : basegfx::B2DPoint point;
236 0 : basegfx::B2DPoint nextPoint;
237 0 : point = b2dPolygon.getB2DPoint( j );
238 :
239 0 : basegfx::B2DPoint prevPoint;
240 0 : prevPoint = b2dPolygon.getPrevControlPoint( j ) ;
241 :
242 0 : point.setX( convPx2mmPrec2( point.getX() )*100.0 );
243 0 : point.setY( convPx2mmPrec2( point.getY() )*100.0 );
244 :
245 0 : if ( b2dPolygon.isPrevControlPointUsed( j ) )
246 : {
247 0 : prevPoint.setX( convPx2mmPrec2( prevPoint.getX() )*100.0 );
248 0 : prevPoint.setY( convPx2mmPrec2( prevPoint.getY() )*100.0 );
249 : }
250 :
251 0 : if ( b2dPolygon.isNextControlPointUsed( j ) )
252 : {
253 0 : nextPoint = b2dPolygon.getNextControlPoint( j ) ;
254 0 : nextPoint.setX( convPx2mmPrec2( nextPoint.getX() )*100.0 );
255 0 : nextPoint.setY( convPx2mmPrec2( nextPoint.getY() )*100.0 );
256 : }
257 :
258 0 : b2dPolygon.setB2DPoint( j, point );
259 :
260 0 : if ( b2dPolygon.isPrevControlPointUsed( j ) )
261 0 : b2dPolygon.setPrevControlPoint( j , prevPoint ) ;
262 :
263 0 : if ( b2dPolygon.isNextControlPointUsed( j ) )
264 0 : b2dPolygon.setNextControlPoint( j , nextPoint ) ;
265 0 : }
266 :
267 0 : elem.PolyPoly.setB2DPolygon( i, b2dPolygon );
268 0 : }
269 :
270 0 : PropertyMap aProps;
271 0 : fillFrameProps( elem, aProps, m_rEmitContext );
272 0 : OUStringBuffer aBuf( 64 );
273 0 : aBuf.appendAscii( "0 0 " );
274 0 : aBuf.append( convPx2mmPrec2(elem.w)*100.0 );
275 0 : aBuf.append( ' ' );
276 0 : aBuf.append( convPx2mmPrec2(elem.h)*100.0 );
277 0 : aProps[ "svg:viewBox" ] = aBuf.makeStringAndClear();
278 0 : aProps[ "svg:d" ] = basegfx::tools::exportToSvgD( elem.PolyPoly, true, true, false );
279 :
280 0 : m_rEmitContext.rEmitter.beginTag( "draw:path", aProps );
281 0 : m_rEmitContext.rEmitter.endTag( "draw:path" );
282 0 : }
283 :
284 0 : void WriterXmlEmitter::visit( ImageElement& elem, const std::list< Element* >::const_iterator& )
285 : {
286 0 : PropertyMap aImageProps;
287 0 : m_rEmitContext.rEmitter.beginTag( "draw:image", aImageProps );
288 0 : m_rEmitContext.rEmitter.beginTag( "office:binary-data", PropertyMap() );
289 0 : m_rEmitContext.rImages.writeBase64EncodedStream( elem.Image, m_rEmitContext);
290 0 : m_rEmitContext.rEmitter.endTag( "office:binary-data" );
291 0 : m_rEmitContext.rEmitter.endTag( "draw:image" );
292 0 : }
293 :
294 0 : void WriterXmlEmitter::visit( PageElement& elem, const std::list< Element* >::const_iterator& )
295 : {
296 0 : if( m_rEmitContext.xStatusIndicator.is() )
297 0 : m_rEmitContext.xStatusIndicator->setValue( elem.PageNumber );
298 :
299 0 : std::list< Element* >::iterator this_it = elem.Children.begin();
300 0 : while( this_it !=elem.Children.end() && *this_it != &elem )
301 : {
302 0 : (*this_it)->visitedBy( *this, this_it );
303 0 : ++this_it;
304 : }
305 0 : }
306 :
307 0 : void WriterXmlEmitter::visit( DocumentElement& elem, const std::list< Element* >::const_iterator&)
308 : {
309 0 : m_rEmitContext.rEmitter.beginTag( "office:body", PropertyMap() );
310 0 : m_rEmitContext.rEmitter.beginTag( "office:text", PropertyMap() );
311 :
312 0 : for( std::list< Element* >::iterator it = elem.Children.begin(); it != elem.Children.end(); ++it )
313 : {
314 0 : PageElement* pPage = dynamic_cast<PageElement*>(*it);
315 0 : if( pPage )
316 : {
317 : // emit only page anchored objects
318 : // currently these are only DrawElement types
319 0 : for( std::list< Element* >::iterator child_it = pPage->Children.begin(); child_it != pPage->Children.end(); ++child_it )
320 : {
321 0 : if( dynamic_cast<DrawElement*>(*child_it) != NULL )
322 0 : (*child_it)->visitedBy( *this, child_it );
323 : }
324 : }
325 : }
326 :
327 : // do not emit page anchored objects, they are emitted before
328 : // (must precede all pages in writer document) currently these are
329 : // only DrawElement types
330 0 : for( std::list< Element* >::iterator it = elem.Children.begin(); it != elem.Children.end(); ++it )
331 : {
332 0 : if( dynamic_cast<DrawElement*>(*it) == NULL )
333 0 : (*it)->visitedBy( *this, it );
334 : }
335 :
336 0 : m_rEmitContext.rEmitter.endTag( "office:text" );
337 0 : m_rEmitContext.rEmitter.endTag( "office:body" );
338 0 : }
339 :
340 :
341 :
342 0 : void WriterXmlOptimizer::visit( HyperlinkElement&, const std::list< Element* >::const_iterator& )
343 : {
344 0 : }
345 :
346 0 : void WriterXmlOptimizer::visit( TextElement&, const std::list< Element* >::const_iterator&)
347 : {
348 0 : }
349 :
350 0 : void WriterXmlOptimizer::visit( FrameElement& elem, const std::list< Element* >::const_iterator& )
351 : {
352 0 : elem.applyToChildren(*this);
353 0 : }
354 :
355 0 : void WriterXmlOptimizer::visit( ImageElement&, const std::list< Element* >::const_iterator& )
356 : {
357 0 : }
358 :
359 0 : void WriterXmlOptimizer::visit( PolyPolyElement& elem, const std::list< Element* >::const_iterator& )
360 : {
361 : /* note: optimize two consecutive PolyPolyElements that
362 : * have the same path but one of which is a stroke while
363 : * the other is a fill
364 : */
365 0 : if( elem.Parent )
366 : {
367 : // find following PolyPolyElement in parent's children list
368 0 : std::list< Element* >::iterator this_it = elem.Parent->Children.begin();
369 0 : while( this_it != elem.Parent->Children.end() && *this_it != &elem )
370 0 : ++this_it;
371 :
372 0 : if( this_it != elem.Parent->Children.end() )
373 : {
374 0 : std::list< Element* >::iterator next_it = this_it;
375 0 : if( ++next_it != elem.Parent->Children.end() )
376 : {
377 0 : PolyPolyElement* pNext = dynamic_cast<PolyPolyElement*>(*next_it);
378 0 : if( pNext && pNext->PolyPoly == elem.PolyPoly )
379 : {
380 : const GraphicsContext& rNextGC =
381 0 : m_rProcessor.getGraphicsContext( pNext->GCId );
382 : const GraphicsContext& rThisGC =
383 0 : m_rProcessor.getGraphicsContext( elem.GCId );
384 :
385 0 : if( rThisGC.BlendMode == rNextGC.BlendMode &&
386 0 : rThisGC.Flatness == rNextGC.Flatness &&
387 0 : rThisGC.Transformation == rNextGC.Transformation &&
388 0 : rThisGC.Clip == rNextGC.Clip &&
389 0 : pNext->Action == PATH_STROKE &&
390 0 : (elem.Action == PATH_FILL || elem.Action == PATH_EOFILL) )
391 : {
392 0 : GraphicsContext aGC = rThisGC;
393 0 : aGC.LineJoin = rNextGC.LineJoin;
394 0 : aGC.LineCap = rNextGC.LineCap;
395 0 : aGC.LineWidth = rNextGC.LineWidth;
396 0 : aGC.MiterLimit= rNextGC.MiterLimit;
397 0 : aGC.DashArray = rNextGC.DashArray;
398 0 : aGC.LineColor = rNextGC.LineColor;
399 0 : elem.GCId = m_rProcessor.getGCId( aGC );
400 :
401 0 : elem.Action |= pNext->Action;
402 :
403 0 : elem.Children.splice( elem.Children.end(), pNext->Children );
404 0 : elem.Parent->Children.erase( next_it );
405 0 : delete pNext;
406 : }
407 : }
408 : }
409 : }
410 : }
411 0 : }
412 :
413 0 : void WriterXmlOptimizer::visit( ParagraphElement& elem, const std::list< Element* >::const_iterator& rParentIt)
414 : {
415 0 : optimizeTextElements( elem );
416 :
417 0 : elem.applyToChildren(*this);
418 :
419 0 : if( elem.Parent && rParentIt != elem.Parent->Children.end() )
420 : {
421 : // find if there is a previous paragraph that might be a heading for this one
422 0 : std::list<Element*>::const_iterator prev = rParentIt;
423 0 : ParagraphElement* pPrevPara = NULL;
424 0 : while( prev != elem.Parent->Children.begin() )
425 : {
426 0 : --prev;
427 0 : pPrevPara = dynamic_cast< ParagraphElement* >(*prev);
428 0 : if( pPrevPara )
429 : {
430 : /* What constitutes a heading ? current hints are:
431 : * - one line only
432 : * - not too far away from this paragraph (two heading height max ?)
433 : * - font larger or bold
434 : * this is of course incomplete
435 : * FIXME: improve hints for heading
436 : */
437 : // check for single line
438 0 : if( pPrevPara->isSingleLined( m_rProcessor ) )
439 : {
440 0 : double head_line_height = pPrevPara->getLineHeight( m_rProcessor );
441 0 : if( pPrevPara->y + pPrevPara->h + 2*head_line_height > elem.y )
442 : {
443 : // check for larger font
444 0 : if( head_line_height > elem.getLineHeight( m_rProcessor ) )
445 : {
446 0 : pPrevPara->Type = elem.Headline;
447 : }
448 : else
449 : {
450 : // check whether text of pPrevPara is bold (at least first text element)
451 : // and this para is not bold (dito)
452 0 : TextElement* pPrevText = pPrevPara->getFirstTextChild();
453 0 : TextElement* pThisText = elem.getFirstTextChild();
454 0 : if( pPrevText && pThisText )
455 : {
456 0 : const FontAttributes& rPrevFont = m_rProcessor.getFont( pPrevText->FontId );
457 0 : const FontAttributes& rThisFont = m_rProcessor.getFont( pThisText->FontId );
458 0 : if( rPrevFont.isBold && ! rThisFont.isBold )
459 0 : pPrevPara->Type = elem.Headline;
460 : }
461 : }
462 : }
463 : }
464 0 : break;
465 : }
466 : }
467 : }
468 0 : }
469 :
470 0 : void WriterXmlOptimizer::visit( PageElement& elem, const std::list< Element* >::const_iterator& )
471 : {
472 0 : if( m_rProcessor.getStatusIndicator().is() )
473 0 : m_rProcessor.getStatusIndicator()->setValue( elem.PageNumber );
474 :
475 : // resolve hyperlinks
476 0 : elem.resolveHyperlinks();
477 :
478 0 : elem.resolveFontStyles( m_rProcessor ); // underlines and such
479 :
480 : // FIXME: until hyperlinks and font effects are adjusted for
481 : // geometrical search handle them before sorting
482 0 : m_rProcessor.sortElements( &elem );
483 :
484 : // find paragraphs in text
485 0 : ParagraphElement* pCurPara = NULL;
486 0 : std::list< Element* >::iterator page_element, next_page_element;
487 0 : next_page_element = elem.Children.begin();
488 0 : double fCurLineHeight = 0.0; // average height of text items in current para
489 0 : int nCurLineElements = 0; // number of line contributing elements in current para
490 0 : double line_left = elem.w, line_right = 0.0;
491 0 : double column_width = elem.w*0.75; // estimate text width
492 : // TODO: guess columns
493 0 : while( next_page_element != elem.Children.end() )
494 : {
495 0 : page_element = next_page_element++;
496 0 : ParagraphElement* pPagePara = dynamic_cast<ParagraphElement*>(*page_element);
497 0 : if( pPagePara )
498 : {
499 0 : pCurPara = pPagePara;
500 : // adjust line height and text items
501 0 : fCurLineHeight = 0.0;
502 0 : nCurLineElements = 0;
503 0 : for( std::list< Element* >::iterator it = pCurPara->Children.begin();
504 0 : it != pCurPara->Children.end(); ++it )
505 : {
506 0 : TextElement* pTestText = dynamic_cast<TextElement*>(*it);
507 0 : if( pTestText )
508 : {
509 0 : fCurLineHeight = (fCurLineHeight*double(nCurLineElements) + pTestText->h)/double(nCurLineElements+1);
510 0 : nCurLineElements++;
511 : }
512 : }
513 0 : continue;
514 : }
515 :
516 0 : HyperlinkElement* pLink = dynamic_cast<HyperlinkElement*>(*page_element);
517 0 : DrawElement* pDraw = dynamic_cast<DrawElement*>(*page_element);
518 0 : if( ! pDraw && pLink && ! pLink->Children.empty() )
519 0 : pDraw = dynamic_cast<DrawElement*>(pLink->Children.front() );
520 0 : if( pDraw )
521 : {
522 : // insert small drawing objects as character, else leave them page bound
523 :
524 0 : bool bInsertToParagraph = false;
525 : // first check if this is either inside the paragraph
526 0 : if( pCurPara && pDraw->y < pCurPara->y + pCurPara->h )
527 : {
528 0 : if( pDraw->h < fCurLineHeight * 1.5 )
529 : {
530 0 : bInsertToParagraph = true;
531 0 : fCurLineHeight = (fCurLineHeight*double(nCurLineElements) + pDraw->h)/double(nCurLineElements+1);
532 0 : nCurLineElements++;
533 : // mark draw element as character
534 0 : pDraw->isCharacter = true;
535 : }
536 : }
537 : // or perhaps the draw element begins a new paragraph
538 0 : else if( next_page_element != elem.Children.end() )
539 : {
540 0 : TextElement* pText = dynamic_cast<TextElement*>(*next_page_element);
541 0 : if( ! pText )
542 : {
543 0 : ParagraphElement* pPara = dynamic_cast<ParagraphElement*>(*next_page_element);
544 0 : if( pPara && ! pPara->Children.empty() )
545 0 : pText = dynamic_cast<TextElement*>(pPara->Children.front());
546 : }
547 0 : if( pText && // check there is a text
548 0 : pDraw->h < pText->h*1.5 && // and it is approx the same height
549 : // and either upper or lower edge of pDraw is inside text's vertical range
550 0 : ( ( pDraw->y >= pText->y && pDraw->y <= pText->y+pText->h ) ||
551 0 : ( pDraw->y+pDraw->h >= pText->y && pDraw->y+pDraw->h <= pText->y+pText->h )
552 : )
553 : )
554 : {
555 0 : bInsertToParagraph = true;
556 0 : fCurLineHeight = pDraw->h;
557 0 : nCurLineElements = 1;
558 0 : line_left = pDraw->x;
559 0 : line_right = pDraw->x + pDraw->w;
560 : // begin a new paragraph
561 0 : pCurPara = NULL;
562 : // mark draw element as character
563 0 : pDraw->isCharacter = true;
564 : }
565 : }
566 :
567 0 : if( ! bInsertToParagraph )
568 : {
569 0 : pCurPara = NULL;
570 0 : continue;
571 : }
572 : }
573 :
574 0 : TextElement* pText = dynamic_cast<TextElement*>(*page_element);
575 0 : if( ! pText && pLink && ! pLink->Children.empty() )
576 0 : pText = dynamic_cast<TextElement*>(pLink->Children.front());
577 0 : if( pText )
578 : {
579 : Element* pGeo = pLink ? static_cast<Element*>(pLink) :
580 0 : static_cast<Element*>(pText);
581 0 : if( pCurPara )
582 : {
583 : // there was already a text element, check for a new paragraph
584 0 : if( nCurLineElements > 0 )
585 : {
586 : // if the new text is significantly distant from the paragraph
587 : // begin a new paragraph
588 0 : if( pGeo->y > pCurPara->y+pCurPara->h + fCurLineHeight*0.5 )
589 0 : pCurPara = NULL; // insert new paragraph
590 0 : else if( pGeo->y > (pCurPara->y+pCurPara->h - fCurLineHeight*0.05) )
591 : {
592 : // new paragraph if either the last line of the paragraph
593 : // was significantly shorter than the paragraph as a whole
594 0 : if( (line_right - line_left) < pCurPara->w*0.75 )
595 0 : pCurPara = NULL;
596 : // or the last line was significantly smaller than the column width
597 0 : else if( (line_right - line_left) < column_width*0.75 )
598 0 : pCurPara = NULL;
599 : }
600 : }
601 : }
602 : // update line height/width
603 0 : if( pCurPara )
604 : {
605 0 : fCurLineHeight = (fCurLineHeight*double(nCurLineElements) + pGeo->h)/double(nCurLineElements+1);
606 0 : nCurLineElements++;
607 0 : if( pGeo->x < line_left )
608 0 : line_left = pGeo->x;
609 0 : if( pGeo->x+pGeo->w > line_right )
610 0 : line_right = pGeo->x+pGeo->w;
611 : }
612 : else
613 : {
614 0 : fCurLineHeight = pGeo->h;
615 0 : nCurLineElements = 1;
616 0 : line_left = pGeo->x;
617 0 : line_right = pGeo->x + pGeo->w;
618 : }
619 : }
620 :
621 : // move element to current paragraph
622 0 : if( ! pCurPara ) // new paragraph, insert one
623 : {
624 0 : pCurPara = m_rProcessor.getElementFactory()->createParagraphElement( NULL );
625 : // set parent
626 0 : pCurPara->Parent = &elem;
627 : //insert new paragraph before current element
628 0 : page_element = elem.Children.insert( page_element, pCurPara );
629 : // forward iterator to current element again
630 0 : ++ page_element;
631 : // update next_element which is now invalid
632 0 : next_page_element = page_element;
633 0 : ++ next_page_element;
634 : }
635 0 : Element* pCurEle = *page_element;
636 0 : pCurEle->setParent( page_element, pCurPara );
637 : OSL_ENSURE( !pText || pCurEle == pText || pCurEle == pLink, "paragraph child list in disorder" );
638 0 : if( pText || pDraw )
639 0 : pCurPara->updateGeometryWith( pCurEle );
640 : }
641 :
642 : // process children
643 0 : elem.applyToChildren(*this);
644 :
645 : // find possible header and footer
646 0 : checkHeaderAndFooter( elem );
647 0 : }
648 :
649 0 : void WriterXmlOptimizer::checkHeaderAndFooter( PageElement& rElem )
650 : {
651 : /* indicators for a header:
652 : * - single line paragrah at top of page ( inside 15% page height)
653 : * - at least linheight above the next paragr aph
654 : *
655 : * indicators for a footer likewise:
656 : * - single line paragraph at bottom of page (inside 15% page height)
657 : * - at least lineheight below the previous paragraph
658 : */
659 :
660 : // detect header
661 : // Note: the following assumes that the pages' chiuldren have been
662 : // sorted geometrically
663 0 : std::list< Element* >::iterator it = rElem.Children.begin();
664 0 : while( it != rElem.Children.end() )
665 : {
666 0 : ParagraphElement* pPara = dynamic_cast<ParagraphElement*>(*it);
667 0 : if( pPara )
668 : {
669 0 : if( pPara->y+pPara->h < rElem.h*0.15 && pPara->isSingleLined( m_rProcessor ) )
670 : {
671 0 : std::list< Element* >::iterator next_it = it;
672 0 : ParagraphElement* pNextPara = NULL;
673 0 : while( ++next_it != rElem.Children.end() && pNextPara == NULL )
674 : {
675 0 : pNextPara = dynamic_cast<ParagraphElement*>(*next_it);
676 : }
677 0 : if( pNextPara && pNextPara->y > pPara->y+pPara->h*2 )
678 : {
679 0 : rElem.HeaderElement = pPara;
680 0 : pPara->Parent = NULL;
681 0 : rElem.Children.remove( pPara );
682 : }
683 : }
684 0 : break;
685 : }
686 0 : ++it;
687 : }
688 :
689 : // detect footer
690 0 : std::list< Element* >::reverse_iterator rit = rElem.Children.rbegin();
691 0 : while( rit != rElem.Children.rend() )
692 : {
693 0 : ParagraphElement* pPara = dynamic_cast<ParagraphElement*>(*rit);
694 0 : if( pPara )
695 : {
696 0 : if( pPara->y > rElem.h*0.85 && pPara->isSingleLined( m_rProcessor ) )
697 : {
698 0 : std::list< Element* >::reverse_iterator next_it = rit;
699 0 : ParagraphElement* pNextPara = NULL;
700 0 : while( ++next_it != rElem.Children.rend() && pNextPara == NULL )
701 : {
702 0 : pNextPara = dynamic_cast<ParagraphElement*>(*next_it);
703 : }
704 0 : if( pNextPara && pNextPara->y < pPara->y-pPara->h*2 )
705 : {
706 0 : rElem.FooterElement = pPara;
707 0 : pPara->Parent = NULL;
708 0 : rElem.Children.remove( pPara );
709 : }
710 : }
711 0 : break;
712 : }
713 0 : ++rit;
714 : }
715 0 : }
716 :
717 0 : void WriterXmlOptimizer::optimizeTextElements(Element& rParent)
718 : {
719 0 : if( rParent.Children.empty() ) // this should not happen
720 : {
721 : OSL_FAIL( "empty paragraph optimized" );
722 0 : return;
723 : }
724 :
725 : // concatenate child elements with same font id
726 0 : std::list< Element* >::iterator next = rParent.Children.begin();
727 0 : std::list< Element* >::iterator it = next++;
728 0 : FrameElement* pFrame = dynamic_cast<FrameElement*>(rParent.Parent);
729 0 : bool bRotatedFrame = false;
730 0 : if( pFrame )
731 : {
732 0 : const GraphicsContext& rFrameGC = m_rProcessor.getGraphicsContext( pFrame->GCId );
733 0 : if( rFrameGC.isRotatedOrSkewed() )
734 0 : bRotatedFrame = true;
735 : }
736 0 : while( next != rParent.Children.end() )
737 : {
738 0 : bool bConcat = false;
739 0 : TextElement* pCur = dynamic_cast<TextElement*>(*it);
740 0 : if( pCur )
741 : {
742 0 : TextElement* pNext = dynamic_cast<TextElement*>(*next);
743 0 : if( pNext )
744 : {
745 0 : const GraphicsContext& rCurGC = m_rProcessor.getGraphicsContext( pCur->GCId );
746 0 : const GraphicsContext& rNextGC = m_rProcessor.getGraphicsContext( pNext->GCId );
747 :
748 : // line and space optimization; works only in strictly horizontal mode
749 :
750 0 : if( !bRotatedFrame
751 0 : && ! rCurGC.isRotatedOrSkewed()
752 0 : && ! rNextGC.isRotatedOrSkewed()
753 0 : && ! pNext->Text.isEmpty()
754 0 : && pNext->Text[0] != ' '
755 0 : && ! pCur->Text.isEmpty()
756 0 : && pCur->Text[pCur->Text.getLength() - 1] != ' '
757 : )
758 : {
759 : // check for new line in paragraph
760 0 : if( pNext->y > pCur->y+pCur->h )
761 : {
762 : // new line begins
763 : // check whether a space would should be inserted or a hyphen removed
764 0 : sal_Unicode aLastCode = pCur->Text[pCur->Text.getLength() - 1];
765 0 : if( aLastCode == '-'
766 0 : || aLastCode == 0x2010
767 0 : || (aLastCode >= 0x2012 && aLastCode <= 0x2015)
768 0 : || aLastCode == 0xff0d
769 : )
770 : {
771 : // cut a hyphen
772 0 : pCur->Text.setLength( pCur->Text.getLength()-1 );
773 : }
774 : // append a space unless there is a non breaking hyphen
775 0 : else if( aLastCode != 0x2011 )
776 : {
777 0 : pCur->Text.append( ' ' );
778 : }
779 : }
780 : else // we're continuing the same line
781 : {
782 : // check whether a space would should be inserted
783 : // check for a small horizontal offset
784 0 : if( pCur->x + pCur->w + pNext->h*0.15 < pNext->x )
785 : {
786 0 : pCur->Text.append( ' ' );
787 : }
788 : }
789 : }
790 : // concatenate consecutive text elements unless there is a
791 : // font or text color or matrix change, leave a new span in that case
792 0 : if( pCur->FontId == pNext->FontId &&
793 0 : rCurGC.FillColor.Red == rNextGC.FillColor.Red &&
794 0 : rCurGC.FillColor.Green == rNextGC.FillColor.Green &&
795 0 : rCurGC.FillColor.Blue == rNextGC.FillColor.Blue &&
796 0 : rCurGC.FillColor.Alpha == rNextGC.FillColor.Alpha &&
797 0 : rCurGC.Transformation == rNextGC.Transformation
798 : )
799 : {
800 0 : pCur->updateGeometryWith( pNext );
801 : // append text to current element
802 0 : pCur->Text.append( pNext->Text.getStr(), pNext->Text.getLength() );
803 : // append eventual children to current element
804 : // and clear children (else the children just
805 : // appended to pCur would be destroyed)
806 0 : pCur->Children.splice( pCur->Children.end(), pNext->Children );
807 : // get rid of the now useless element
808 0 : rParent.Children.erase( next );
809 0 : delete pNext;
810 0 : bConcat = true;
811 : }
812 : }
813 : }
814 0 : else if( dynamic_cast<HyperlinkElement*>(*it) )
815 0 : optimizeTextElements( **it );
816 0 : if( bConcat )
817 : {
818 0 : next = it;
819 0 : ++next;
820 : }
821 : else
822 : {
823 0 : ++it;
824 0 : ++next;
825 : }
826 : }
827 : }
828 :
829 0 : void WriterXmlOptimizer::visit( DocumentElement& elem, const std::list< Element* >::const_iterator&)
830 : {
831 0 : elem.applyToChildren(*this);
832 0 : }
833 :
834 :
835 :
836 :
837 0 : void WriterXmlFinalizer::visit( PolyPolyElement& elem, const std::list< Element* >::const_iterator& )
838 : {
839 : // xxx TODO copied from DrawElement
840 0 : const GraphicsContext& rGC = m_rProcessor.getGraphicsContext(elem.GCId );
841 0 : PropertyMap aProps;
842 0 : aProps[ "style:family" ] = "graphic";
843 :
844 0 : PropertyMap aGCProps;
845 :
846 : // TODO(F3): proper dash emulation
847 0 : if( elem.Action & PATH_STROKE )
848 : {
849 0 : aGCProps[ "draw:stroke" ] = rGC.DashArray.empty() ? OUString("solid") : OUString("dash");
850 0 : aGCProps[ "svg:stroke-color" ] = getColorString( rGC.LineColor );
851 0 : if( rGC.LineWidth != 0.0 )
852 : {
853 0 : ::basegfx::B2DVector aVec(rGC.LineWidth,0);
854 0 : aVec *= rGC.Transformation;
855 :
856 0 : aVec.setX ( convPx2mmPrec2( aVec.getX() )*100.0 );
857 0 : aVec.setY ( convPx2mmPrec2( aVec.getY() )*100.0 );
858 :
859 0 : aGCProps[ "svg:stroke-width" ] = OUString::number( aVec.getLength() );
860 : }
861 : }
862 : else
863 : {
864 0 : aGCProps[ "draw:stroke" ] = "none";
865 : }
866 :
867 : // TODO(F1): check whether stuff could be emulated by gradient/bitmap/hatch
868 0 : if( elem.Action & (PATH_FILL | PATH_EOFILL) )
869 : {
870 0 : aGCProps[ "draw:fill" ] = "solid";
871 0 : aGCProps[ "draw:fill-color" ] = getColorString( rGC.FillColor );
872 : }
873 : else
874 : {
875 0 : aGCProps[ "draw:fill" ] = "none";
876 : }
877 :
878 0 : StyleContainer::Style aStyle( "style:style", aProps );
879 0 : StyleContainer::Style aSubStyle( "style:graphic-properties", aGCProps );
880 0 : aStyle.SubStyles.push_back( &aSubStyle );
881 :
882 0 : elem.StyleId = m_rStyleContainer.getStyleId( aStyle );
883 0 : }
884 :
885 0 : void WriterXmlFinalizer::visit( HyperlinkElement&, const std::list< Element* >::const_iterator& )
886 : {
887 0 : }
888 :
889 0 : void WriterXmlFinalizer::visit( TextElement& elem, const std::list< Element* >::const_iterator& )
890 : {
891 0 : const FontAttributes& rFont = m_rProcessor.getFont( elem.FontId );
892 0 : PropertyMap aProps;
893 0 : aProps[ "style:family" ] = "text";
894 :
895 0 : PropertyMap aFontProps;
896 :
897 : // family name
898 0 : aFontProps[ "fo:font-family" ] = rFont.familyName;
899 : // bold
900 0 : if( rFont.isBold )
901 : {
902 0 : aFontProps[ "fo:font-weight" ] = "bold";
903 0 : aFontProps[ "fo:font-weight-asian" ] = "bold";
904 0 : aFontProps[ "fo:font-weight-complex" ] = "bold";
905 : }
906 : // italic
907 0 : if( rFont.isItalic )
908 : {
909 0 : aFontProps[ "fo:font-style" ] = "italic";
910 0 : aFontProps[ "fo:font-style-asian" ] = "italic";
911 0 : aFontProps[ "fo:font-style-complex" ] = "italic";
912 : }
913 : // underline
914 0 : if( rFont.isUnderline )
915 : {
916 0 : aFontProps[ "style:text-underline-style" ] = "solid";
917 0 : aFontProps[ "style:text-underline-width" ] = "auto";
918 0 : aFontProps[ "style:text-underline-color" ] = "font-color";
919 : }
920 : // outline
921 0 : if( rFont.isOutline )
922 : {
923 0 : aFontProps[ "style:text-outline" ] = "true";
924 : }
925 : // size
926 0 : OUStringBuffer aBuf( 32 );
927 0 : aBuf.append( rFont.size*72/PDFI_OUTDEV_RESOLUTION );
928 0 : aBuf.appendAscii( "pt" );
929 0 : OUString aFSize = aBuf.makeStringAndClear();
930 0 : aFontProps[ "fo:font-size" ] = aFSize;
931 0 : aFontProps[ "style:font-size-asian" ] = aFSize;
932 0 : aFontProps[ "style:font-size-complex" ] = aFSize;
933 : // color
934 0 : const GraphicsContext& rGC = m_rProcessor.getGraphicsContext( elem.GCId );
935 0 : aFontProps[ "fo:color" ] = getColorString( rFont.isOutline ? rGC.LineColor : rGC.FillColor );
936 :
937 0 : StyleContainer::Style aStyle( "style:style", aProps );
938 0 : StyleContainer::Style aSubStyle( "style:text-properties", aFontProps );
939 0 : aStyle.SubStyles.push_back( &aSubStyle );
940 0 : elem.StyleId = m_rStyleContainer.getStyleId( aStyle );
941 0 : }
942 :
943 0 : void WriterXmlFinalizer::visit( ParagraphElement& elem, const std::list< Element* >::const_iterator& rParentIt )
944 : {
945 0 : PropertyMap aParaProps;
946 :
947 0 : if( elem.Parent )
948 : {
949 : // check for center alignement
950 : // criterion: paragraph is small relative to parent and distributed around its center
951 0 : double p_x = elem.Parent->x;
952 0 : double p_w = elem.Parent->w;
953 :
954 0 : PageElement* pPage = dynamic_cast<PageElement*>(elem.Parent);
955 0 : if( pPage )
956 : {
957 0 : p_x += pPage->LeftMargin;
958 0 : p_w -= pPage->LeftMargin+pPage->RightMargin;
959 : }
960 0 : bool bIsCenter = false;
961 0 : if( elem.w < ( p_w/2) )
962 : {
963 0 : double delta = elem.w/4;
964 : // allow very small paragraphs to deviate a little more
965 : // relative to parent's center
966 0 : if( elem.w < p_w/8 )
967 0 : delta = elem.w;
968 0 : if( fabs( elem.x+elem.w/2 - ( p_x+ p_w/2) ) < delta ||
969 0 : (pPage && fabs( elem.x+elem.w/2 - (pPage->x + pPage->w/2) ) < delta) )
970 : {
971 0 : bIsCenter = true;
972 0 : aParaProps[ "fo:text-align" ] = "center";
973 : }
974 : }
975 0 : if( ! bIsCenter && elem.x > p_x + p_w/10 )
976 : {
977 : // indent
978 0 : OUStringBuffer aBuf( 32 );
979 0 : aBuf.append( convPx2mm( elem.x - p_x ) );
980 0 : aBuf.appendAscii( "mm" );
981 0 : aParaProps[ "fo:margin-left" ] = aBuf.makeStringAndClear();
982 : }
983 :
984 : // check whether to leave some space to next paragraph
985 : // find whether there is a next paragraph
986 0 : std::list< Element* >::const_iterator it = rParentIt;
987 0 : const ParagraphElement* pNextPara = NULL;
988 0 : while( ++it != elem.Parent->Children.end() && ! pNextPara )
989 0 : pNextPara = dynamic_cast< const ParagraphElement* >(*it);
990 0 : if( pNextPara )
991 : {
992 0 : if( pNextPara->y - (elem.y+elem.h) > convmm2Px( 10 ) )
993 : {
994 0 : OUStringBuffer aBuf( 32 );
995 0 : aBuf.append( convPx2mm( pNextPara->y - (elem.y+elem.h) ) );
996 0 : aBuf.appendAscii( "mm" );
997 0 : aParaProps[ "fo:margin-bottom" ] = aBuf.makeStringAndClear();
998 : }
999 : }
1000 : }
1001 :
1002 0 : if( ! aParaProps.empty() )
1003 : {
1004 0 : PropertyMap aProps;
1005 0 : aProps[ "style:family" ] = "paragraph";
1006 0 : StyleContainer::Style aStyle( "style:style", aProps );
1007 0 : StyleContainer::Style aSubStyle( "style:paragraph-properties", aParaProps );
1008 0 : aStyle.SubStyles.push_back( &aSubStyle );
1009 0 : elem.StyleId = m_rStyleContainer.getStyleId( aStyle );
1010 : }
1011 :
1012 0 : elem.applyToChildren(*this);
1013 0 : }
1014 :
1015 0 : void WriterXmlFinalizer::visit( FrameElement& elem, const std::list< Element* >::const_iterator&)
1016 : {
1017 0 : PropertyMap aProps;
1018 0 : aProps[ "style:family" ] = "graphic";
1019 :
1020 0 : PropertyMap aGCProps;
1021 :
1022 0 : aGCProps[ "draw:stroke" ] = "none";
1023 0 : aGCProps[ "draw:fill" ] = "none";
1024 :
1025 0 : StyleContainer::Style aStyle( "style:style", aProps );
1026 0 : StyleContainer::Style aSubStyle( "style:graphic-properties", aGCProps );
1027 0 : aStyle.SubStyles.push_back( &aSubStyle );
1028 :
1029 0 : elem.StyleId = m_rStyleContainer.getStyleId( aStyle );
1030 0 : elem.applyToChildren(*this);
1031 0 : }
1032 :
1033 0 : void WriterXmlFinalizer::visit( ImageElement&, const std::list< Element* >::const_iterator& )
1034 : {
1035 0 : }
1036 :
1037 0 : void WriterXmlFinalizer::setFirstOnPage( ParagraphElement& rElem,
1038 : StyleContainer& rStyles,
1039 : const OUString& rMasterPageName )
1040 : {
1041 0 : PropertyMap aProps;
1042 0 : if( rElem.StyleId != -1 )
1043 : {
1044 0 : const PropertyMap* pProps = rStyles.getProperties( rElem.StyleId );
1045 0 : if( pProps )
1046 0 : aProps = *pProps;
1047 : }
1048 :
1049 0 : aProps[ "style:family" ] = "paragraph";
1050 0 : aProps[ "style:master-page-name" ] = rMasterPageName;
1051 :
1052 0 : if( rElem.StyleId != -1 )
1053 0 : rElem.StyleId = rStyles.setProperties( rElem.StyleId, aProps );
1054 : else
1055 : {
1056 0 : StyleContainer::Style aStyle( "style:style", aProps );
1057 0 : rElem.StyleId = rStyles.getStyleId( aStyle );
1058 0 : }
1059 0 : }
1060 :
1061 0 : void WriterXmlFinalizer::visit( PageElement& elem, const std::list< Element* >::const_iterator& )
1062 : {
1063 0 : if( m_rProcessor.getStatusIndicator().is() )
1064 0 : m_rProcessor.getStatusIndicator()->setValue( elem.PageNumber );
1065 :
1066 : // transform from pixel to mm
1067 0 : double page_width = convPx2mm( elem.w ), page_height = convPx2mm( elem.h );
1068 :
1069 : // calculate page margins out of the relevant children (paragraphs)
1070 0 : elem.TopMargin = elem.h, elem.BottomMargin = 0, elem.LeftMargin = elem.w, elem.RightMargin = 0;
1071 : // first element should be a paragraphy
1072 0 : ParagraphElement* pFirstPara = NULL;
1073 0 : for( std::list< Element* >::const_iterator it = elem.Children.begin(); it != elem.Children.end(); ++it )
1074 : {
1075 0 : if( dynamic_cast<ParagraphElement*>( *it ) )
1076 : {
1077 0 : if( (*it)->x < elem.LeftMargin )
1078 0 : elem.LeftMargin = (*it)->x;
1079 0 : if( (*it)->y < elem.TopMargin )
1080 0 : elem.TopMargin = (*it)->y;
1081 0 : if( (*it)->x + (*it)->w > elem.w - elem.RightMargin )
1082 0 : elem.RightMargin = elem.w - ((*it)->x + (*it)->w);
1083 0 : if( (*it)->y + (*it)->h > elem.h - elem.BottomMargin )
1084 0 : elem.BottomMargin = elem.h - ((*it)->y + (*it)->h);
1085 0 : if( ! pFirstPara )
1086 0 : pFirstPara = dynamic_cast<ParagraphElement*>( *it );
1087 : }
1088 : }
1089 0 : if( elem.HeaderElement && elem.HeaderElement->y < elem.TopMargin )
1090 0 : elem.TopMargin = elem.HeaderElement->y;
1091 0 : if( elem.FooterElement && elem.FooterElement->y+elem.FooterElement->h > elem.h - elem.BottomMargin )
1092 0 : elem.BottomMargin = elem.h - (elem.FooterElement->y + elem.FooterElement->h);
1093 :
1094 : // transform margins to mm
1095 0 : double left_margin = convPx2mm( elem.LeftMargin );
1096 0 : double right_margin = convPx2mm( elem.RightMargin );
1097 0 : double top_margin = convPx2mm( elem.TopMargin );
1098 0 : double bottom_margin = convPx2mm( elem.BottomMargin );
1099 0 : if( ! pFirstPara )
1100 : {
1101 : // use default page margins
1102 0 : left_margin = 10;
1103 0 : right_margin = 10;
1104 0 : top_margin = 10;
1105 0 : bottom_margin = 10;
1106 : }
1107 :
1108 : // round left/top margin to nearest mm
1109 0 : left_margin = rtl_math_round( left_margin, 0, rtl_math_RoundingMode_Floor );
1110 0 : top_margin = rtl_math_round( top_margin, 0, rtl_math_RoundingMode_Floor );
1111 : // round (fuzzy) right/bottom margin to nearest cm
1112 0 : right_margin = rtl_math_round( right_margin, right_margin >= 10 ? -1 : 0, rtl_math_RoundingMode_Floor );
1113 0 : bottom_margin = rtl_math_round( bottom_margin, bottom_margin >= 10 ? -1 : 0, rtl_math_RoundingMode_Floor );
1114 :
1115 : // set reasonable default in case of way too large margins
1116 : // e.g. no paragraph case
1117 0 : if( left_margin > page_width/2.0 - 10 )
1118 0 : left_margin = 10;
1119 0 : if( right_margin > page_width/2.0 - 10 )
1120 0 : right_margin = 10;
1121 0 : if( top_margin > page_height/2.0 - 10 )
1122 0 : top_margin = 10;
1123 0 : if( bottom_margin > page_height/2.0 - 10 )
1124 0 : bottom_margin = 10;
1125 :
1126 : // catch the weird cases
1127 0 : if( left_margin < 0 )
1128 0 : left_margin = 0;
1129 0 : if( right_margin < 0 )
1130 0 : right_margin = 0;
1131 0 : if( top_margin < 0 )
1132 0 : top_margin = 0;
1133 0 : if( bottom_margin < 0 )
1134 0 : bottom_margin = 0;
1135 :
1136 : // widely differing margins are unlikely to be correct
1137 0 : if( right_margin > left_margin*1.5 )
1138 0 : right_margin = left_margin;
1139 :
1140 0 : elem.LeftMargin = convmm2Px( left_margin );
1141 0 : elem.RightMargin = convmm2Px( right_margin );
1142 0 : elem.TopMargin = convmm2Px( top_margin );
1143 0 : elem.BottomMargin = convmm2Px( bottom_margin );
1144 :
1145 : // get styles for paragraphs
1146 0 : PropertyMap aPageProps;
1147 0 : PropertyMap aPageLayoutProps;
1148 0 : aPageLayoutProps[ "fo:page-width" ] = unitMMString( page_width );
1149 0 : aPageLayoutProps[ "fo:page-height" ] = unitMMString( page_height );
1150 0 : aPageLayoutProps[ "style:print-orientation" ]
1151 0 : = elem.w < elem.h ? OUString("portrait") : OUString("landscape");
1152 0 : aPageLayoutProps[ "fo:margin-top" ] = unitMMString( top_margin );
1153 0 : aPageLayoutProps[ "fo:margin-bottom" ] = unitMMString( bottom_margin );
1154 0 : aPageLayoutProps[ "fo:margin-left" ] = unitMMString( left_margin );
1155 0 : aPageLayoutProps[ "fo:margin-right" ] = unitMMString( right_margin );
1156 0 : aPageLayoutProps[ "style:writing-mode" ]= "lr-tb";
1157 :
1158 0 : StyleContainer::Style aStyle( "style:page-layout", aPageProps);
1159 0 : StyleContainer::Style aSubStyle( "style:page-layout-properties", aPageLayoutProps);
1160 0 : aStyle.SubStyles.push_back(&aSubStyle);
1161 0 : sal_Int32 nPageStyle = m_rStyleContainer.impl_getStyleId( aStyle, false );
1162 :
1163 : // create master page
1164 0 : OUString aMasterPageLayoutName = m_rStyleContainer.getStyleName( nPageStyle );
1165 0 : aPageProps[ "style:page-layout-name" ] = aMasterPageLayoutName;
1166 0 : StyleContainer::Style aMPStyle( "style:master-page", aPageProps );
1167 0 : StyleContainer::Style aHeaderStyle( "style:header", PropertyMap() );
1168 0 : StyleContainer::Style aFooterStyle( "style:footer", PropertyMap() );
1169 0 : if( elem.HeaderElement )
1170 : {
1171 0 : elem.HeaderElement->visitedBy( *this, std::list<Element*>::iterator() );
1172 0 : aHeaderStyle.ContainedElement = elem.HeaderElement;
1173 0 : aMPStyle.SubStyles.push_back( &aHeaderStyle );
1174 : }
1175 0 : if( elem.FooterElement )
1176 : {
1177 0 : elem.FooterElement->visitedBy( *this, std::list<Element*>::iterator() );
1178 0 : aFooterStyle.ContainedElement = elem.FooterElement;
1179 0 : aMPStyle.SubStyles.push_back( &aFooterStyle );
1180 : }
1181 0 : elem.StyleId = m_rStyleContainer.impl_getStyleId( aMPStyle,false );
1182 :
1183 :
1184 0 : OUString aMasterPageName = m_rStyleContainer.getStyleName( elem.StyleId );
1185 :
1186 : // create styles for children
1187 0 : elem.applyToChildren(*this);
1188 :
1189 : // no paragraph or other elements before the first paragraph
1190 0 : if( ! pFirstPara )
1191 : {
1192 0 : pFirstPara = m_rProcessor.getElementFactory()->createParagraphElement( NULL );
1193 0 : pFirstPara->Parent = &elem;
1194 0 : elem.Children.push_front( pFirstPara );
1195 : }
1196 0 : setFirstOnPage(*pFirstPara, m_rStyleContainer, aMasterPageName);
1197 0 : }
1198 :
1199 0 : void WriterXmlFinalizer::visit( DocumentElement& elem, const std::list< Element* >::const_iterator& )
1200 : {
1201 0 : elem.applyToChildren(*this);
1202 0 : }
1203 :
1204 : }
1205 :
1206 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|