summaryrefslogtreecommitdiff
path: root/drawinglayer/source/primitive2d/textdecoratedprimitive2d.cxx
blob: f03fc284179c7869443b7158b95a675b715cb888 (plain)
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: textdecoratedprimitive2d.cxx,v $
 *
 *  $Revision: 1.4 $
 *
 *  last change: $Author: aw $ $Date: 2007-08-13 15:30:25 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

#ifndef INCLUDED_DRAWINGLAYER_PRIMITIVE_TEXTDECORATEDPRIMITIVE2D_HXX
#include <drawinglayer/primitive2d/textdecoratedprimitive2d.hxx>
#endif

#ifndef INCLUDED_DRAWINGLAYER_TEXTLAYOUTDEVICE_HXX
#include <drawinglayer/primitive2d/textlayoutdevice.hxx>
#endif

#ifndef INCLUDED_DRAWINGLAYER_PRIMITIVE2D_POLYGONPRIMITIVE2D_HXX
#include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
#endif

#ifndef INCLUDED_DRAWINGLAYER_ATTRIBUTE_STROKEATTRIBUTE_HXX
#include <drawinglayer/attribute/strokeattribute.hxx>
#endif

#ifndef INCLUDED_DRAWINGLAYER_PRIMITIVE2D_PRIMITIVETYPES2D_HXX
#include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
#endif

#include <numeric>

//////////////////////////////////////////////////////////////////////////////

namespace drawinglayer
{
    namespace primitive2d
    {
        Primitive2DSequence TextDecoratedPortionPrimitive2D::createLocalDecomposition(const geometry::ViewInformation2D& /*rViewInformation*/) const
        {
            std::vector< BasePrimitive2D* > aNewPrimitives;

            // First create a simple text primitive and ignore other attributes
            aNewPrimitives.push_back(new TextSimplePortionPrimitive2D(getTextTransform(), getText(), getDXArray(), getFontAttributes(), getLocale(), getFontColor()));

            // more to be done?
            const bool bNeedFontUnderline(getFontUnderline() != FONT_UNDERLINE_NONE);
            const bool bNeedFontStrikeout(getFontStrikeout() != FONT_STRIKEOUT_NONE);
            const bool bNeedEmphasisMarkAbove(getEmphasisMarkAbove() != FONT_EMPHASISMARK_NONE);
            const bool bNeedEmphasisMarkBelow(getEmphasisMarkBelow() != FONT_EMPHASISMARK_NONE);
            const sal_uInt32 nSpellVectorSize(maWrongSpellVector.size());

            if(bNeedFontUnderline
                || bNeedFontStrikeout
                || bNeedEmphasisMarkAbove
                || bNeedEmphasisMarkBelow
                || 0 != nSpellVectorSize)
            {
                // prepare transformations
                basegfx::B2DVector aScale, aTranslate;
                double fRotate, fShearX;
                getTextTransform().decompose(aScale, aTranslate, fRotate, fShearX);

                // unscaled transform is needed since the scale in the text transform describes the font size
                basegfx::B2DHomMatrix aUnscaledTransform;
                aUnscaledTransform.rotate( fRotate );
                aUnscaledTransform.shearX( fShearX );
                aUnscaledTransform.translate( aTranslate.getX(), aTranslate.getY() );

                // prepare TextLayouterDevice to get metrics for text decorations like
                // underline/strikeout/emphasis marks from it
                TextLayouterDevice aTextLayouter;

                {
                    // unrotated transform is needed for TextLayouterDevice setup
                    basegfx::B2DHomMatrix aUnrotatedTransform = getTextTransform();
                    aUnrotatedTransform.rotate( -fRotate );
                    aTextLayouter.setFontAttributes(getFontAttributes(), aUnrotatedTransform );
                }

                // init metrics to defaults
//              const double fLineHeight(aTextLayouter.getTextHeight());
                double fUnderlineOffset(aTextLayouter.getUnderlineOffset());
                double fUnderlineHeight(aTextLayouter.getUnderlineHeight());
                basegfx::tools::B2DLineJoin eLineJoin(basegfx::tools::B2DLINEJOIN_NONE);
                bool bDoubleLine(false);
                bool bWaveLine(false);
                double fTextWidth(0.0);

                if(getDXArray().empty())
                {
                    fTextWidth = aTextLayouter.getTextWidth( getText(), 0/*TODO*/, getText().Len()/*TODO*/ );
                }
                else
                {
                    fTextWidth = getDXArray().back() * aScale.getX();
                }

                // prepare line styles for text decoration lines
                const int* pDashDotArray(0);

                static const int aDottedArray[]     = { 1, 1, 0};               // DOTTED LINE
                static const int aDashDotArray[]    = { 1, 1, 4, 1, 0};         // DASHDOT
                static const int aDashDotDotArray[] = { 1, 1, 1, 1, 4, 1, 0};   // DASHDOTDOT
                static const int aDashedArray[]     = { 5, 2, 0};               // DASHED LINE
                static const int aLongDashArray[]   = { 7, 2, 0};               // LONGDASH

                // set Underline attribute
                switch( getFontUnderline() )
                {
                    default:
                        DBG_WARNING1( "DrawingLayer: Unknown underline attribute (%d)!", getFontUnderline() );
                        // fall through
                    case primitive2d::FONT_UNDERLINE_NONE:
                        fUnderlineHeight = 0;
                        break;
                    case primitive2d::FONT_UNDERLINE_BOLD:
                        fUnderlineHeight *= 2;
                        // fall through
                    case primitive2d::FONT_UNDERLINE_SINGLE:
                        break;
                    case primitive2d::FONT_UNDERLINE_DOUBLE:
                        bDoubleLine = true;
                        break;
                    case primitive2d::FONT_UNDERLINE_BOLDDOTTED:
                        fUnderlineHeight *= 2;
                        // fall through
                    case primitive2d::FONT_UNDERLINE_DOTTED:
                        eLineJoin = basegfx::tools::B2DLINEJOIN_ROUND;
                        pDashDotArray = aDottedArray;
                        break;
                    case primitive2d::FONT_UNDERLINE_BOLDDASH:
                        fUnderlineHeight *= 2;
                        // fall through
                    case primitive2d::FONT_UNDERLINE_DASH:
                        pDashDotArray = aDashedArray;
                        break;
                    case primitive2d::FONT_UNDERLINE_BOLDLONGDASH:
                        fUnderlineHeight *= 2;
                        // fall through
                    case primitive2d::FONT_UNDERLINE_LONGDASH:
                        pDashDotArray = aLongDashArray;
                        break;
                    case primitive2d::FONT_UNDERLINE_BOLDDASHDOT:
                        fUnderlineHeight *= 2;
                        // fall through
                    case primitive2d::FONT_UNDERLINE_DASHDOT:
                        pDashDotArray = aDashDotArray;
                        break;
                    case primitive2d::FONT_UNDERLINE_BOLDDASHDOTDOT:
                        fUnderlineHeight *= 2;
                        // fall through
                    case primitive2d::FONT_UNDERLINE_DASHDOTDOT:
                        eLineJoin = basegfx::tools::B2DLINEJOIN_ROUND;
                        pDashDotArray = aDashDotDotArray;
                        break;
                    case primitive2d::FONT_UNDERLINE_SMALLWAVE:
                        // TODO
                        bWaveLine = true;
                        break;
                    case primitive2d::FONT_UNDERLINE_BOLDWAVE:
                        fUnderlineHeight *= 2;
                        // fall through
                    case primitive2d::FONT_UNDERLINE_WAVE:
                        // TODO
                        bWaveLine = true;
                        break;
                    case primitive2d::FONT_UNDERLINE_DOUBLEWAVE:
                        bWaveLine = true;
                        bDoubleLine = true;
                        break;
                }

                if(fUnderlineHeight > 0.0)
                {
                    if(bDoubleLine)
                    {
                        fUnderlineOffset -= 0.50 * fUnderlineHeight;
                        fUnderlineHeight *= 0.64;
                    }

                    basegfx::B2DPolygon aUnderline;
                    ::basegfx::B2DPoint aPoint( 0.0, fUnderlineOffset );
                    aUnderline.append( aPoint );

                    if(!bWaveLine)
                    {
                        // straight underline
                        aUnderline.append( aPoint + ::basegfx::B2DPoint( fTextWidth, 0.0 ) );
                    }
                    else
                    {
                        // wavy underline
                        basegfx::B2DPolygon& aWavePoly = aUnderline;
                        double fWaveWidth(4.0 * fUnderlineHeight);

                        if(primitive2d::FONT_UNDERLINE_SMALLWAVE == getFontUnderline())
                        {
                            fWaveWidth *= 0.7;
                        }

                        const double fWaveHeight(0.5 * fWaveWidth);
                        const ::basegfx::B2DPoint aCtrlOffset( fWaveWidth * 0.467308, fWaveHeight );

                        for(double fPos = fWaveWidth; fPos < fTextWidth; fPos += fWaveWidth)
                        {
                            // create a symmetrical wave using one cubic bezier curve
                            // with y==0 for {x==0, x==0.5*fW or x==1.0*fW}
                            // and ymin/ymax at {x=0.25*fW or 0.75*fW}
                            const int n = aWavePoly.count();

                            aWavePoly.setNextControlPoint(  n-1, aPoint + aCtrlOffset );
                            aWavePoly.append(aPoint += ::basegfx::B2DPoint( fWaveWidth, 0.0 ) );
                            aWavePoly.setPrevControlPoint(  n-1, aPoint - aCtrlOffset );
                        }

                        // adjust stroke style
                        eLineJoin = basegfx::tools::B2DLINEJOIN_ROUND;
                        fUnderlineHeight *= 0.5;
                    }

                    const basegfx::BColor& rLineColor = getTextlineColor();
                    attribute::StrokeAttribute aStrokeAttr(rLineColor, fUnderlineHeight, eLineJoin);

                    if(pDashDotArray)
                    {
                        ::std::vector< double > aDoubleArray;

                        for( const int* p = pDashDotArray; *p; ++p )
                        {
                            aDoubleArray.push_back( *p * fUnderlineHeight);
                        }

                        const double fFullDashDotLen(::std::accumulate(aDoubleArray.begin(), aDoubleArray.end(), 0.0));
                        aStrokeAttr = attribute::StrokeAttribute(rLineColor, fUnderlineHeight, eLineJoin, aDoubleArray, fFullDashDotLen);
                    }

                    aUnderline.transform( aUnscaledTransform );
                    aNewPrimitives.push_back(new PolygonStrokePrimitive2D( aUnderline, aStrokeAttr ));

                    if( bDoubleLine )
                    {
                        // add another underline below the first underline
                        const double fLineDist((bWaveLine ? 3.0 : 2.0) * fUnderlineHeight);
                        ::basegfx::B2DVector aOffsetVector( 0.0, fLineDist );
                        basegfx::B2DHomMatrix aOffsetTransform;

                        aOffsetVector = aUnscaledTransform * aOffsetVector;
                        aOffsetTransform.translate( aOffsetVector.getX(), aOffsetVector.getY() );
                        aUnderline.transform( aOffsetTransform );
                        aNewPrimitives.push_back(new PolygonStrokePrimitive2D( aUnderline, aStrokeAttr ));
                    }
                }

                double fStrikeoutHeight(aTextLayouter.getUnderlineHeight());
                double fStrikeoutOffset(aTextLayouter.getStrikeoutOffset());
                eLineJoin = basegfx::tools::B2DLINEJOIN_NONE;
                bDoubleLine = false;
                sal_Unicode aStrikeoutChar('\0');

                // set Underline attribute
                switch( getFontStrikeout() )
                {
                    default:
                        DBG_WARNING1( "DrawingLayer: Unknown underline attribute (%d)!", getFontUnderline() );
                        // fall through
                    case primitive2d::FONT_STRIKEOUT_NONE:
                        fStrikeoutHeight = 0;
                        break;
                    case primitive2d::FONT_STRIKEOUT_SINGLE:
                        break;
                    case primitive2d::FONT_STRIKEOUT_DOUBLE:
                        bDoubleLine = true;
                        break;
                    case primitive2d::FONT_STRIKEOUT_BOLD:
                        fStrikeoutHeight *= 2;
                        break;
                    case primitive2d::FONT_STRIKEOUT_SLASH:
                        aStrikeoutChar = '/';
                        fStrikeoutHeight = 0;
                        break;
                    case primitive2d::FONT_STRIKEOUT_X:
                        aStrikeoutChar = 'X';
                        fStrikeoutHeight = 0;
                        break;
                }

                if(fStrikeoutHeight > 0.0)
                {
                    if( bDoubleLine )
                    {
                        fStrikeoutOffset -= 0.50 * fStrikeoutHeight;
                        fStrikeoutHeight *= 0.64;
                    }

                    basegfx::B2DPolygon aStrikeoutLine;
                    basegfx::B2DPoint aPoint( 0.0, -fStrikeoutOffset );
                    aStrikeoutLine.append( aPoint );

                    if( 1/*####*/ )
                    {
                        // straight underline
                        aStrikeoutLine.append( aPoint + ::basegfx::B2DPoint( fTextWidth, 0.0 ) );
                    }

                    const basegfx::BColor& rStrikeoutColor = getTextlineColor();
                    attribute::StrokeAttribute aStrokeAttr( rStrikeoutColor, fStrikeoutHeight, eLineJoin );

                    aStrikeoutLine.transform( aUnscaledTransform );
                    aNewPrimitives.push_back(new PolygonStrokePrimitive2D( aStrikeoutLine, aStrokeAttr ));

                    if( bDoubleLine )
                    {
                        // add another strikeout below the first strikeout
                        const double fLineDist(2.0 * fStrikeoutHeight);
                        ::basegfx::B2DVector aOffsetVector( 0.0, -fLineDist );
                        basegfx::B2DHomMatrix aOffsetTransform;

                        aOffsetVector = aUnscaledTransform * aOffsetVector;
                        aOffsetTransform.translate( aOffsetVector.getX(), aOffsetVector.getY() );
                        aStrikeoutLine.transform( aOffsetTransform );
                        aNewPrimitives.push_back(new PolygonStrokePrimitive2D( aStrikeoutLine, aStrokeAttr ));
                    }
                }

                if( aStrikeoutChar != '\0' )
                {
                    const String aSingleCharString( &aStrikeoutChar, 1 );
                    const double fStrikeCharWidth(aTextLayouter.getTextWidth( aSingleCharString, 0, 1 ));
                    const double fStrikeCharCount(fabs(fTextWidth / fStrikeCharWidth));
                    const sal_uInt32 nStrikeCharCount(static_cast< sal_uInt32 >(fStrikeCharCount + 0.9));
                    const double fStrikeCharWidthUnscaled(aScale.getX() == 0.0 ? fStrikeCharWidth : fStrikeCharWidth / aScale.getX());
                    const basegfx::BColor& rStrikeoutColor = getFontColor();

                    std::vector<double> aDXArray(nStrikeCharCount);
                    String aStrikeoutString;

                    for(sal_uInt32 a(0); a < nStrikeCharCount; a++)
                    {
                        aStrikeoutString += aStrikeoutChar;
                        aDXArray[a] = (a + 1) * fStrikeCharWidthUnscaled;
                    }

                    aNewPrimitives.push_back(new TextSimplePortionPrimitive2D(getTextTransform(), aStrikeoutString, aDXArray, getFontAttributes(), getLocale(), rStrikeoutColor ));
                }

                // TODO: need to take care of
                // -emphasis mark
                // -relief (embosses/engraved)
                // -shadow
                // if( getWordLineMode() )
                // if( getUnderlineAbove() )

                if(nSpellVectorSize && !getDXArray().empty())
                {
                    // TODO: take care of WrongSpellVector; create redlining (red wavelines) accordingly.
                    // For test purposes, create single lines as long as no waveline primitive is created
                    const ::std::vector< double >& rDXArray = getDXArray();
                    const sal_uInt32 nDXCount(rDXArray.size());
                    const basegfx::BColor aSpellColor(1.0, 0.0, 0.0); // red

                    for(sal_uInt32 a(0); a < nSpellVectorSize; a++)
                    {
                        const WrongSpellEntry& rCandidate = maWrongSpellVector[a];

                        if(rCandidate.getStart() < rCandidate.getEnd())
                        {
                            ::basegfx::B2DPoint aStart;
                            ::basegfx::B2DPoint aEnd;

                            if(rCandidate.getStart() > 0 && rCandidate.getStart() - 1 < nDXCount)
                            {
                                aStart.setX(rDXArray[rCandidate.getStart() - 1] * aScale.getX());
                            }

                            if(rCandidate.getEnd() > 0 && rCandidate.getEnd() - 1 < nDXCount)
                            {
                                aEnd.setX(rDXArray[rCandidate.getEnd() - 1] * aScale.getX());
                            }

                            if(aStart != aEnd)
                            {
                                   basegfx::B2DPolygon aPolygon;

                                aPolygon.append(aStart);
                                aPolygon.append(aEnd);
                                aPolygon.transform(aUnscaledTransform);

                                aNewPrimitives.push_back(new PolygonHairlinePrimitive2D(aPolygon, aSpellColor));
                            }
                        }
                    }
                }
            }

            // prepare return sequence
            Primitive2DSequence aRetval(aNewPrimitives.size());

            for(sal_uInt32 a(0); a < aNewPrimitives.size(); a++)
            {
                aRetval[a] = Primitive2DReference(aNewPrimitives[a]);
            }

            return aRetval;
        }

        TextDecoratedPortionPrimitive2D::TextDecoratedPortionPrimitive2D(

            // TextSimplePortionPrimitive2D parameters
            const basegfx::B2DHomMatrix& rNewTransform,
            const String& rText,
            const ::std::vector< double >& rDXArray,
            const FontAttributes& rFontAttributes,
            const ::com::sun::star::lang::Locale& rLocale,
            const basegfx::BColor& rFontColor,

            // local parameters
            const basegfx::BColor& rTextlineColor,
            FontUnderline eFontUnderline,
            bool bUnderlineAbove,
            FontStrikeout eFontStrikeout,
            bool bWordLineMode,
            FontEmphasisMark eFontEmphasisMark,
            bool bEmphasisMarkAbove,
            bool bEmphasisMarkBelow,
            FontRelief eFontRelief,
            bool bShadow,
            const WrongSpellVector& rWrongSpellVector)
        :   TextSimplePortionPrimitive2D(rNewTransform, rText, rDXArray, rFontAttributes, rLocale, rFontColor),
            maTextlineColor(rTextlineColor),
            meFontUnderline(eFontUnderline),
            meFontStrikeout(eFontStrikeout),
            meFontEmphasisMark(eFontEmphasisMark),
            meFontRelief(eFontRelief),
            maWrongSpellVector(rWrongSpellVector),
            mbUnderlineAbove(bUnderlineAbove),
            mbWordLineMode(bWordLineMode),
            mbEmphasisMarkAbove(bEmphasisMarkAbove),
            mbEmphasisMarkBelow(bEmphasisMarkBelow),
            mbShadow(bShadow)
        {
        }

        bool TextDecoratedPortionPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
        {
            if(TextSimplePortionPrimitive2D::operator==(rPrimitive))
            {
                const TextDecoratedPortionPrimitive2D& rCompare = (TextDecoratedPortionPrimitive2D&)rPrimitive;

                return (getTextlineColor() == rCompare.getTextlineColor()
                    && getFontUnderline() == rCompare.getFontUnderline()
                    && getFontStrikeout() == rCompare.getFontStrikeout()
                    && getFontEmphasisMark() == rCompare.getFontEmphasisMark()
                    && getFontRelief() == rCompare.getFontRelief()
                    && getWrongSpellVector() == rCompare.getWrongSpellVector()
                    && getUnderlineAbove() == rCompare.getUnderlineAbove()
                    && getWordLineMode() == rCompare.getWordLineMode()
                    && getEmphasisMarkAbove() == rCompare.getEmphasisMarkAbove()
                    && getEmphasisMarkBelow() == rCompare.getEmphasisMarkBelow()
                    && getShadow() == rCompare.getShadow());
            }

            return false;
        }

        // provide unique ID
        ImplPrimitrive2DIDBlock(TextDecoratedPortionPrimitive2D, PRIMITIVE2D_ID_TEXTDECORATEDPORTIONPRIMITIVE2D)

    } // end of namespace primitive2d
} // end of namespace drawinglayer

//////////////////////////////////////////////////////////////////////////////
// eof