1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <xmlemitter.hxx>
#include <genericelements.hxx>
#include <pdfiprocessor.hxx>
#include <pdfihelper.hxx>
#include "style.hxx"
#include <basegfx/polygon/b2dpolypolygontools.hxx>
#include <basegfx/range/b2drange.hxx>
#include <osl/diagnose.h>
#include <sal/log.hxx>
namespace pdfi
{
Element::~Element()
{
}
void Element::applyToChildren( ElementTreeVisitor& rVisitor )
{
for( auto it = Children.begin(); it != Children.end(); ++it )
(*it)->visitedBy( rVisitor, it );
}
void Element::setParent( std::list<std::unique_ptr<Element>>::iterator const & el, Element* pNewParent )
{
if( pNewParent )
{
pNewParent->Children.splice( pNewParent->Children.end(), (*el)->Parent->Children, el );
(*el)->Parent = pNewParent;
}
}
void Element::updateGeometryWith( const Element* pMergeFrom )
{
if( w == 0 && h == 0 )
{
x = pMergeFrom->x;
y = pMergeFrom->y;
w = pMergeFrom->w;
h = pMergeFrom->h;
}
else
{
if( pMergeFrom->x < x )
{
w += x - pMergeFrom->x;
x = pMergeFrom->x;
}
if( pMergeFrom->x+pMergeFrom->w > x+w )
w = pMergeFrom->w+pMergeFrom->x - x;
if( pMergeFrom->y < y )
{
h += y - pMergeFrom->y;
y = pMergeFrom->y;
}
if( pMergeFrom->y+pMergeFrom->h > y+h )
h = pMergeFrom->h+pMergeFrom->y - y;
}
}
#if OSL_DEBUG_LEVEL > 0
#include <typeinfo>
void Element::emitStructure( int nLevel)
{
SAL_INFO( "sdext", std::string(nLevel, ' ') << "<" << typeid( *this ).name() << " " << this << "> ("
<< std::setprecision(1) << x << "," << y << ")+(" << w << "x" << h << ")" );
for (auto const& child : Children)
child->emitStructure(nLevel+1);
SAL_INFO( "sdext", std::string(nLevel, ' ') << "</" << typeid( *this ).name() << ">" );
}
#endif
void ListElement::visitedBy( ElementTreeVisitor& visitor, const std::list< std::unique_ptr<Element> >::const_iterator& )
{
// this is only an inner node
applyToChildren(visitor);
}
void HyperlinkElement::visitedBy( ElementTreeVisitor& rVisitor,
const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt )
{
rVisitor.visit(*this,rParentIt);
}
void TextElement::visitedBy( ElementTreeVisitor& rVisitor,
const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt )
{
rVisitor.visit(*this,rParentIt);
}
void FrameElement::visitedBy( ElementTreeVisitor& rVisitor,
const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt )
{
rVisitor.visit(*this,rParentIt);
}
void ImageElement::visitedBy( ElementTreeVisitor& rVisitor,
const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt)
{
rVisitor.visit( *this, rParentIt);
}
PolyPolyElement::PolyPolyElement( Element* pParent,
sal_Int32 nGCId,
const basegfx::B2DPolyPolygon& rPolyPoly,
sal_Int8 nAction )
: DrawElement( pParent, nGCId ),
PolyPoly( rPolyPoly ),
Action( nAction )
{
}
void PolyPolyElement::updateGeometry()
{
basegfx::B2DRange aRange;
if( PolyPoly.areControlPointsUsed() )
aRange = basegfx::utils::getRange( basegfx::utils::adaptiveSubdivideByAngle( PolyPoly ) );
else
aRange = basegfx::utils::getRange( PolyPoly );
x = aRange.getMinX();
y = aRange.getMinY();
w = aRange.getWidth();
h = aRange.getHeight();
// fdo#32330 - non-closed paths will not show up filled in LibO
if( Action & (PATH_FILL | PATH_EOFILL) )
PolyPoly.setClosed(true);
}
void PolyPolyElement::visitedBy( ElementTreeVisitor& rVisitor,
const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt)
{
rVisitor.visit( *this, rParentIt);
}
#if OSL_DEBUG_LEVEL > 0
void PolyPolyElement::emitStructure( int nLevel)
{
SAL_WARN( "sdext", std::string(nLevel, ' ') << "<" << typeid( *this ).name() << " " << this << ">" );
SAL_WARN( "sdext", "path=" );
int nPoly = PolyPoly.count();
for( int i = 0; i < nPoly; i++ )
{
OUStringBuffer buff;
basegfx::B2DPolygon aPoly = PolyPoly.getB2DPolygon( i );
int nPoints = aPoly.count();
for( int n = 0; n < nPoints; n++ )
{
basegfx::B2DPoint aPoint = aPoly.getB2DPoint( n );
buff.append( " (").append(aPoint.getX()).append(",").append(aPoint.getY()).append(")");
}
SAL_WARN( "sdext", " " << buff.makeStringAndClear() );
}
for (auto const& child : Children)
child->emitStructure( nLevel+1 );
SAL_WARN( "sdext", std::string(nLevel, ' ') << "</" << typeid( *this ).name() << ">");
}
#endif
void ParagraphElement::visitedBy( ElementTreeVisitor& rVisitor,
const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt )
{
rVisitor.visit(*this,rParentIt);
}
bool ParagraphElement::isSingleLined( PDFIProcessor const & rProc ) const
{
auto it = Children.begin();
TextElement* pText = nullptr, *pLastText = nullptr;
while( it != Children.end() )
{
// a paragraph containing subparagraphs cannot be single lined
if( dynamic_cast< ParagraphElement* >(it->get()) != nullptr )
return false;
pText = dynamic_cast< TextElement* >(it->get());
if( pText )
{
const FontAttributes& rFont = rProc.getFont( pText->FontId );
if( pText->h > rFont.size*1.5 )
return false;
if( pLastText )
{
if( pText->y > pLastText->y+pLastText->h ||
pLastText->y > pText->y+pText->h )
return false;
}
else
pLastText = pText;
}
++it;
}
// a paragraph without a single text is not considered single lined
return pLastText != nullptr;
}
double ParagraphElement::getLineHeight( PDFIProcessor& rProc ) const
{
double line_h = 0;
for( auto it = Children.begin(); it != Children.end(); ++it )
{
ParagraphElement* pPara = dynamic_cast< ParagraphElement* >(it->get());
TextElement* pText = nullptr;
if( pPara )
{
double lh = pPara->getLineHeight( rProc );
if( lh > line_h )
line_h = lh;
}
else if( (pText = dynamic_cast< TextElement* >( it->get() )) != nullptr )
{
const FontAttributes& rFont = rProc.getFont( pText->FontId );
double lh = pText->h;
if( pText->h > rFont.size*1.5 )
lh = rFont.size;
if( lh > line_h )
line_h = lh;
}
}
return line_h;
}
TextElement* ParagraphElement::getFirstTextChild() const
{
TextElement* pText = nullptr;
for( auto it = Children.begin();
it != Children.end() && ! pText; ++it )
{
pText = dynamic_cast<TextElement*>(it->get());
}
return pText;
}
PageElement::~PageElement()
{
}
void PageElement::visitedBy( ElementTreeVisitor& rVisitor,
const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt )
{
rVisitor.visit(*this, rParentIt);
}
bool PageElement::resolveHyperlink( const std::list<std::unique_ptr<Element>>::iterator& link_it, std::list<std::unique_ptr<Element>>& rElements )
{
HyperlinkElement* pLink = dynamic_cast<HyperlinkElement*>(link_it->get());
if( ! pLink ) // sanity check
return false;
for( auto it = rElements.begin(); it != rElements.end(); ++it )
{
if( (*it)->x >= pLink->x && (*it)->x + (*it)->w <= pLink->x + pLink->w &&
(*it)->y >= pLink->y && (*it)->y + (*it)->h <= pLink->y + pLink->h )
{
TextElement* pText = dynamic_cast<TextElement*>(it->get());
if( pText )
{
if( pLink->Children.empty() )
{
// insert the hyperlink before the frame
rElements.splice( it, Hyperlinks.Children, link_it );
pLink->Parent = (*it)->Parent;
}
// move text element into hyperlink
auto next = it;
++next;
Element::setParent( it, pLink );
it = next;
--it;
continue;
}
// a link can contain multiple text elements or a single frame
if( ! pLink->Children.empty() )
continue;
if( dynamic_cast<ParagraphElement*>(it->get()) )
{
if( resolveHyperlink( link_it, (*it)->Children ) )
break;
continue;
}
FrameElement* pFrame = dynamic_cast<FrameElement*>(it->get());
if( pFrame )
{
// insert the hyperlink before the frame
rElements.splice( it, Hyperlinks.Children, link_it );
pLink->Parent = (*it)->Parent;
// move frame into hyperlink
Element::setParent( it, pLink );
break;
}
}
}
return ! pLink->Children.empty();
}
void PageElement::resolveHyperlinks()
{
while( ! Hyperlinks.Children.empty() )
{
if( ! resolveHyperlink( Hyperlinks.Children.begin(), Children ) )
{
Hyperlinks.Children.pop_front();
}
}
}
void PageElement::resolveFontStyles( PDFIProcessor const & rProc )
{
resolveUnderlines(rProc);
}
void PageElement::resolveUnderlines( PDFIProcessor const & rProc )
{
// FIXME: currently the algorithm used is quadratic
// this could be solved by some sorting beforehand
auto poly_it = Children.begin();
while( poly_it != Children.end() )
{
PolyPolyElement* pPoly = dynamic_cast< PolyPolyElement* >(poly_it->get());
if( ! pPoly || ! pPoly->Children.empty() )
{
++poly_it;
continue;
}
/* check for: no filling
* only two points (FIXME: handle small rectangles, too)
* y coordinates of points are equal
*/
if( pPoly->Action != PATH_STROKE )
{
++poly_it;
continue;
}
if( pPoly->PolyPoly.count() != 1 )
{
++poly_it;
continue;
}
bool bRemovePoly = false;
basegfx::B2DPolygon aPoly = pPoly->PolyPoly.getB2DPolygon(0);
if( aPoly.count() != 2 ||
aPoly.getB2DPoint(0).getY() != aPoly.getB2DPoint(1).getY() )
{
++poly_it;
continue;
}
double l_x = aPoly.getB2DPoint(0).getX();
double r_x = aPoly.getB2DPoint(1).getX();
double u_y;
if( r_x < l_x )
{
u_y = r_x; r_x = l_x; l_x = u_y;
}
u_y = aPoly.getB2DPoint(0).getY();
for( auto it = Children.begin();
it != Children.end(); ++it )
{
Element* pEle = it->get();
if( pEle->y <= u_y && pEle->y + pEle->h*1.1 >= u_y )
{
// first: is the element underlined completely ?
if( pEle->x + pEle->w*0.1 >= l_x &&
pEle->x + pEle->w*0.9 <= r_x )
{
TextElement* pText = dynamic_cast< TextElement* >(pEle);
if( pText )
{
const GraphicsContext& rTextGC = rProc.getGraphicsContext( pText->GCId );
if( ! rTextGC.isRotatedOrSkewed() )
{
bRemovePoly = true;
// retrieve ID for modified font
FontAttributes aAttr = rProc.getFont( pText->FontId );
aAttr.isUnderline = true;
pText->FontId = rProc.getFontId( aAttr );
}
}
else if( dynamic_cast< HyperlinkElement* >(pEle) )
bRemovePoly = true;
}
// second: hyperlinks may be larger than their underline
// since they are just arbitrary rectangles in the action definition
else if( dynamic_cast< HyperlinkElement* >(pEle) != nullptr &&
l_x >= pEle->x && r_x <= pEle->x+pEle->w )
{
bRemovePoly = true;
}
}
}
if( bRemovePoly )
{
auto next_it = poly_it;
++next_it;
Children.erase( poly_it );
poly_it = next_it;
}
else
++poly_it;
}
}
DocumentElement::~DocumentElement()
{
}
void DocumentElement::visitedBy( ElementTreeVisitor& rVisitor,
const std::list< std::unique_ptr<Element> >::const_iterator& rParentIt)
{
rVisitor.visit(*this, rParentIt);
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|