summaryrefslogtreecommitdiff
path: root/svx/source/svdraw/svdtxhdl.cxx
blob: d64dcad177da7974e0c9ae714aaf16a1c1b422b8 (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
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: svdtxhdl.cxx,v $
 * $Revision: 1.29 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org 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 version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_svx.hxx"

#include "svdtxhdl.hxx"
#include <limits.h>
#include "svditext.hxx"
#include <svx/svdmodel.hxx>
#include <svx/svdpage.hxx>
#include <svx/svdogrp.hxx>
#include <svx/svdotext.hxx>
#include <svx/svdoutl.hxx>
#include <svx/outliner.hxx>
#include <svx/outlobj.hxx>
#include <svx/svxfont.hxx>
#include <svx/xlnclit.hxx>
#include <svx/xlnwtit.hxx>
#include <svx/xflclit.hxx>
#include <vcl/metric.hxx>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>

#include <com/sun/star/i18n/ScriptType.hdl>
#include <com/sun/star/i18n/XBreakIterator.hpp>
#include <comphelper/processfactory.hxx>

#include <com/sun/star/i18n/CharacterIteratorMode.hdl>
#include "unolingu.hxx"
#include <basegfx/polygon/b2dpolygon.hxx>
#include <basegfx/polygon/b2dpolygontools.hxx>
#include <basegfx/matrix/b2dhommatrix.hxx>
#include <svtools/itemset.hxx>

using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::i18n;

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

ImpTextPortionHandler::ImpTextPortionHandler(SdrOutliner& rOutln, const SdrTextObj& rTxtObj):
    rOutliner(rOutln),
    rTextObj(rTxtObj)
{
    SdrModel* pModel = rTextObj.GetModel();
    MapMode aMap=aVDev.GetMapMode();

    aMap.SetMapUnit(pModel->GetScaleUnit());
    aMap.SetScaleX(pModel->GetScaleFraction());
    aMap.SetScaleY(pModel->GetScaleFraction());
    aVDev.SetMapMode(aMap);
}

void ImpTextPortionHandler::ConvertToPathObj(SdrObjGroup& rGroup, FASTBOOL bPoly)
{
    bToPoly=bPoly;
    pGroup=&rGroup;

    Rectangle aAnchorRect;
    Rectangle aTextRect;
    SdrFitToSizeType eFit=rTextObj.GetFitToSize();
    FASTBOOL bFitToSize=(eFit==SDRTEXTFIT_PROPORTIONAL || eFit==SDRTEXTFIT_ALLLINES);
    // Bei TakeTextRect wird u.a. auch der Text in
    // den Outliner gesteckt
    rTextObj.TakeTextRect(rOutliner,aTextRect,FALSE,&aAnchorRect);
    maTextRect=aTextRect;
    if (bFitToSize) maTextRect=aAnchorRect;
    rOutliner.SetDrawPortionHdl(LINK(this,ImpTextPortionHandler,ConvertHdl));
    rOutliner.StripPortions();
    rOutliner.SetDrawPortionHdl(Link());
    rOutliner.Clear();
    
    if(bFitToSize) 
    {
        Fraction nX(aAnchorRect.Right()-aAnchorRect.Left(),aTextRect.Right()-aTextRect.Left());
        Fraction nY(aAnchorRect.Bottom()-aAnchorRect.Top(),aTextRect.Bottom()-aTextRect.Top());

        // #95395# scale from top-right when vertical text
        if(rOutliner.IsVertical())
            pGroup->NbcResize(aAnchorRect.TopRight(),nX,nY);
        else
            pGroup->NbcResize(aAnchorRect.TopLeft(),nX,nY);
    }
    if (rTextObj.aGeo.nDrehWink!=0) { // #35825# Rotieren erst nach Resize (wg. FitToSize)
        pGroup->NbcRotate(maTextRect.TopLeft(),rTextObj.aGeo.nDrehWink,rTextObj.aGeo.nSin,rTextObj.aGeo.nCos);
    }
}

// #101498# Helper class to remember text portions in sorted manner
class ImpRecordPortion
{
public:
    Point						maPosition;
    String						maText;
    xub_StrLen					mnTextStart;
    xub_StrLen					mnTextLength;
    sal_uInt16					mnPara;
    xub_StrLen					mnIndex;

    // #102819# Here SvxFont needs to be used instead of Font
    SvxFont						maFont;

    sal_Int32*					mpDXArray;
    sal_uInt8					mnBiDiLevel;

    ImpRecordPortion(DrawPortionInfo* pInfo);
    ~ImpRecordPortion();

    sal_uInt8 GetBiDiLevel() const { return mnBiDiLevel; }
};

ImpRecordPortion::ImpRecordPortion(DrawPortionInfo* pInfo)
:	maPosition(pInfo->mrStartPos),
    maText(pInfo->mrText),
    mnTextStart((xub_StrLen)pInfo->mnTextStart),
    mnTextLength((xub_StrLen)pInfo->mnTextLen),
    mnPara(pInfo->mnPara),
    mnIndex(pInfo->mnIndex),
    maFont(pInfo->mrFont),
    mpDXArray(NULL),
    mnBiDiLevel(pInfo->GetBiDiLevel())
{
    if(pInfo->mpDXArray)
    {
        mpDXArray = new sal_Int32[pInfo->mnTextLen];

        for(sal_uInt32 a(0L); a < pInfo->mnTextLen; a++)
        {
            mpDXArray[a] = pInfo->mpDXArray[a];
        }
    }
}

ImpRecordPortion::~ImpRecordPortion()
{
    if(mpDXArray)
    {
        delete[] mpDXArray;
    }
}

IMPL_LINK(ImpTextPortionHandler,ConvertHdl,DrawPortionInfo*,pInfo)
{
    // maTextRect enthaelt den Ausgabebereich des Textobjekts
    BOOL bIsVertical(rOutliner.IsVertical());
    Point aPos2(maTextRect.TopLeft() + pInfo->mrStartPos);
    Color aColor(pInfo->mrFont.GetColor());

    if(bIsVertical)
        aPos2 = maTextRect.TopRight() + pInfo->mrStartPos;

    Point aStartPos(aPos2);
    SfxItemSet aAttrSet((SfxItemPool&)(*rTextObj.GetObjectItemPool()));
    long nHochTief(pInfo->mrFont.GetEscapement());
    FontMetric aFontMetric(aVDev.GetFontMetric());
    sal_Int32 nLineLen(0L);
    
    Font aFont( pInfo->mrFont );

    if( nHochTief) 
    { 
        // shrink the font for sub-/superscripting
        long nPercent(pInfo->mrFont.GetPropr());
        
        if(nPercent != 100) 
        {
            Size aSize(aFont.GetSize());
            aSize.Height() = (aSize.Height() * nPercent +50) / 100;
            aSize.Width() = (aSize.Width() * nPercent +50) / 100;
            aFont.SetSize(aSize);
        }

        sal_Bool bNeg(nHochTief < 0);
        
        if(bNeg) 
            nHochTief = -nHochTief;

        nHochTief = (nHochTief * pInfo->mrFont.GetSize().Height() +50) /100;

        if(bNeg) 
            nHochTief = -nHochTief;
    }

    aFont.SetOrientation( 0 );
    aVDev.SetFont( aFont );

    if(bIsVertical)
        // #83068#
        aPos2.X() += aFontMetric.GetAscent() + nHochTief;
    else
        aPos2.Y() -= aFontMetric.GetAscent() + nHochTief;

    if (pInfo->mrFont.IsOutline()) 
    {
        aAttrSet.Put(XLineColorItem(String(),aColor));
        aAttrSet.Put(XLineStyleItem(XLINE_SOLID));
        aAttrSet.Put(XLineWidthItem(0));
        aAttrSet.Put(XFillStyleItem(XFILL_NONE));
    } 
    else 
    {
        aAttrSet.Put(XFillColorItem(String(),aColor));
        aAttrSet.Put(XLineStyleItem(XLINE_NONE));
        aAttrSet.Put(XFillStyleItem(XFILL_SOLID));
    }

    // #100318# convert in a single step
    // #101499# Use GetTextOutlines and a PolyPolyVector now
    PolyPolyVector aPolyPolyVector;
    if(aVDev.GetTextOutlines(aPolyPolyVector, pInfo->mrText, pInfo->mnTextStart, pInfo->mnTextStart, pInfo->mnTextLen)
        && aPolyPolyVector.size())
    {
        for(sal_uInt32 a(0); a < aPolyPolyVector.size(); a++)
        {
            PolyPolygon aPolyPoly(aPolyPolyVector[a]);

            if(aPolyPoly.Count() > 0 && aPolyPoly[0].GetSize() > 0)
            {
                basegfx::B2DPolyPolygon aPolyPolygon(aPolyPoly.getB2DPolyPolygon());
                basegfx::B2DHomMatrix aMatrix;

                // rotate 270 degree if vertical since result is unrotated
                if(pInfo->mrFont.GetOrientation())
                {
                    double fAngle(F_PI * (pInfo->mrFont.GetOrientation() % 3600) / 1800.0);
                    aMatrix.rotate(fAngle);
                }

                // result is baseline oriented, thus move one line height, too
                if(bIsVertical)
                {
                    aMatrix.translate(-aFontMetric.GetAscent(), 0.0);
                }
                else
                {
                    aMatrix.translate(0.0, aFontMetric.GetAscent());
                }

                // move to output coordinates
                aMatrix.translate(aPos2.X(), aPos2.Y());

                // transform
                aPolyPolygon.transform(aMatrix);

                // create object
                SdrObject* pObj = rTextObj.ImpConvertMakeObj(aPolyPolygon, sal_True, !bToPoly, sal_True);
                pObj->SetMergedItemSet(aAttrSet);
                pGroup->GetSubList()->InsertObject(pObj);
            }
        }

        nLineLen = pInfo->mpDXArray[pInfo->mnTextLen - 1];
    }

    FontUnderline eUndl=pInfo->mrFont.GetUnderline();
    FontStrikeout eStrk=pInfo->mrFont.GetStrikeout();
    if (eUndl!=UNDERLINE_NONE) {
        FASTBOOL bDouble=eUndl==UNDERLINE_DOUBLE;
        long nDescent=aFontMetric.GetDescent();
        long nAscend=aFontMetric.GetAscent();
        long nDick=nDescent / (bDouble ? 5 : 3);
        long nDist=(nDescent-nDick*2)/3; // Linienabstand bei doppelt
        basegfx::B2DPolyPolygon aPolyPolygon;

        if (eUndl!=UNDERLINE_DOTTED) {
            Point aPoint(0,0);
            XPolygon aXP(Rectangle(aPoint,bIsVertical ? Point(nDick,nLineLen) : Point(nLineLen,nDick)));
            if(bIsVertical)
                aXP.Move(nAscend-nDist,0);
            aPolyPolygon.append(aXP.getB2DPolygon());
            if (bDouble) {
                if(bIsVertical)
                    aXP.Move(-(nDick+nDist),0);
                else
                    aXP.Move(0,nDick+nDist);
                aPolyPolygon.append(aXP.getB2DPolygon());
            }
        } else {
            Point aPoint(0,0);
            XPolygon aXP(Rectangle(aPoint,Point(nDick,nDick)));
            long n=0;
            while (n<=nLineLen) {
                if (n+nDick>nLineLen) { // ler letzte Dot ggf. etwas schmaler
                    aXP=XPolygon(Rectangle(
                        bIsVertical ? Point(0,n) : Point(n,0),
                        bIsVertical ? Point(nDick,nLineLen) : Point(nLineLen,nDick)));
                }
                aPolyPolygon.append(aXP.getB2DPolygon());
                if(bIsVertical)
                    aXP.Move(0,2*nDick);
                else
                    aXP.Move(2*nDick,0);
                n+=2*nDick;
            }
        }

        long y=nDescent-nDick; // y-Position der Striche zur Baseline bestimmen
        if (bDouble) y-=nDick+nDist;
        y=(y+1)/2;

        basegfx::B2DHomMatrix aMatrix;

        if(bIsVertical)
        {
            aMatrix.translate(aStartPos.X() - (y - nHochTief), aStartPos.Y());
        }
        else
        {
            aMatrix.translate(aStartPos.X(), aStartPos.Y() + y - nHochTief);
        }

        aPolyPolygon.transform(aMatrix);

        // maTextRect enthaelt den Ausgabebereich des Textobjekts
        SdrObject* pObj=rTextObj.ImpConvertMakeObj(aPolyPolygon, sal_True, !bToPoly, sal_True);
        pObj->SetMergedItemSet(aAttrSet);
        pGroup->GetSubList()->InsertObject(pObj);
    }
    if (eStrk!=STRIKEOUT_NONE) {
        FASTBOOL bDouble=eStrk==STRIKEOUT_DOUBLE;
        long nDescent=aFontMetric.GetDescent();
        //long nAscend=aFontMetric.GetAscent();
        long nDick=nDescent / (bDouble ? 5 : 3);
        long nDist=(nDescent-nDick*2)/3; // Linienabstand bei doppelt
        basegfx::B2DPolyPolygon aPolyPolygon;

        const Point aPoint(0,0);
        const Rectangle aRect(aPoint,bIsVertical ? Point(nDick,nLineLen) : Point(nLineLen,nDick));
        const basegfx::B2DRange aRectRange(aRect.Left(), aRect.Top(), aRect.Right(), aRect.Bottom());
        basegfx::B2DPolygon aPolyFromRect(basegfx::tools::createPolygonFromRect(aRectRange));
        aPolyPolygon.append(aPolyFromRect);

        if (bDouble) 
        {
            basegfx::B2DHomMatrix aMatrix;

            if(bIsVertical)
                aMatrix.translate(-(nDick+nDist), 0.0);
            else
                aMatrix.translate(0.0, nDick+nDist);

            aPolyFromRect.transform(aMatrix);
            aPolyPolygon.append(aPolyFromRect);
        }

        // y-Position der Striche zur Baseline bestimmen
        long y=-(long)(aFontMetric.GetAscent()-aFontMetric.GetIntLeading()+1)/3;
        if (!bDouble) y-=(nDick+1)/2;
        else y-=nDick+(nDist+1)/2;

        basegfx::B2DHomMatrix aMatrix;

        if(bIsVertical)
        {
            aMatrix.translate(aStartPos.X() - (y - nHochTief), aStartPos.Y());
        }
        else
        {
            aMatrix.translate(aStartPos.X(), aStartPos.Y() + y - nHochTief);
        }

        aPolyPolygon.transform(aMatrix);

        // maTextRect enthaelt den Ausgabebereich des Textobjekts
        SdrObject* pObj=rTextObj.ImpConvertMakeObj(aPolyPolygon, sal_True, !bToPoly, sal_True);
        pObj->SetMergedItemSet(aAttrSet);
        pGroup->GetSubList()->InsertObject(pObj);
    }

    return 0;
}

// eof