summaryrefslogtreecommitdiff
path: root/drawinglayer/source/primitive2d/textlayoutdevice.cxx
blob: c19efb39c463b7e6cd466ad70e48967eda7136f1 (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
/* -*- 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 <drawinglayer/primitive2d/textlayoutdevice.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/scoped_disposing_ptr.hxx>
#include <vcl/timer.hxx>
#include <vcl/virdev.hxx>
#include <vcl/font.hxx>
#include <vcl/metric.hxx>
#include <i18nlangtag/languagetag.hxx>
#include <drawinglayer/primitive2d/textprimitive2d.hxx>
#include <vcl/svapp.hxx>


// VDev RevDevice provider

namespace
{
    class ImpTimedRefDev;

    //the scoped_timed_RefDev owns a ImpTimeRefDev and releases it on dtor
    //or disposing of the default XComponentContext which causes the underlying
    //OutputDevice to get released

    //The ImpTimerRefDev itself, if the timeout ever gets hit, will call
    //reset on the scoped_timed_RefDev to release the ImpTimerRefDev early
    //if its unused for a few minutes
    class scoped_timed_RefDev : public comphelper::scoped_disposing_ptr<ImpTimedRefDev>
    {
    public:
        scoped_timed_RefDev() : comphelper::scoped_disposing_ptr<ImpTimedRefDev>((::com::sun::star::uno::Reference<com::sun::star::lang::XComponent>(::comphelper::getProcessComponentContext(), ::com::sun::star::uno::UNO_QUERY_THROW)))
        {
        }
    };

    class the_scoped_timed_RefDev : public rtl::Static<scoped_timed_RefDev, the_scoped_timed_RefDev> {};

    class ImpTimedRefDev : public Timer
    {
        scoped_timed_RefDev&                mrOwnerOfMe;
        VirtualDevice*                      mpVirDev;
        sal_uInt32                          mnUseCount;

    public:
        explicit ImpTimedRefDev(scoped_timed_RefDev& rOwnerofMe);
        ~ImpTimedRefDev();
        virtual void Timeout() SAL_OVERRIDE;

        VirtualDevice& acquireVirtualDevice();
        void releaseVirtualDevice();
    };

    ImpTimedRefDev::ImpTimedRefDev(scoped_timed_RefDev& rOwnerOfMe)
    :   mrOwnerOfMe(rOwnerOfMe),
        mpVirDev(0L),
        mnUseCount(0L)
    {
        SetTimeout(3L * 60L * 1000L); // three minutes
        Start();
    }

    ImpTimedRefDev::~ImpTimedRefDev()
    {
        OSL_ENSURE(0L == mnUseCount, "destruction of a still used ImpTimedRefDev (!)");
        const SolarMutexGuard aGuard;
        delete mpVirDev;
    }

    void ImpTimedRefDev::Timeout()
    {
        // for obvious reasons, do not call anything after this
        mrOwnerOfMe.reset();
    }

    VirtualDevice& ImpTimedRefDev::acquireVirtualDevice()
    {
        if(!mpVirDev)
        {
            mpVirDev = new VirtualDevice();
            mpVirDev->SetReferenceDevice( VirtualDevice::REFDEV_MODE_MSO1 );
        }

        if(!mnUseCount)
        {
            Stop();
        }

        mnUseCount++;

        return *mpVirDev;
    }

    void ImpTimedRefDev::releaseVirtualDevice()
    {
        OSL_ENSURE(mnUseCount, "mismatch call number to releaseVirtualDevice() (!)");
        mnUseCount--;

        if(!mnUseCount)
        {
            Start();
        }
    }
} // end of anonymous namespace


// access to one global ImpTimedRefDev incarnation in namespace drawinglayer::primitive

namespace drawinglayer
{
    namespace primitive2d
    {
        // static methods here
        VirtualDevice& acquireGlobalVirtualDevice()
        {
            scoped_timed_RefDev& rStdRefDevice = the_scoped_timed_RefDev::get();

            if(!rStdRefDevice)
                rStdRefDevice.reset(new ImpTimedRefDev(rStdRefDevice));

            return rStdRefDevice->acquireVirtualDevice();
        }

        void releaseGlobalVirtualDevice()
        {
            scoped_timed_RefDev& rStdRefDevice = the_scoped_timed_RefDev::get();

            OSL_ENSURE(rStdRefDevice, "releaseGlobalVirtualDevice() without prior acquireGlobalVirtualDevice() call(!)");
            rStdRefDevice->releaseVirtualDevice();
        }

        TextLayouterDevice::TextLayouterDevice()
        :   mrDevice(acquireGlobalVirtualDevice())
        {
        }

        TextLayouterDevice::~TextLayouterDevice()
        {
            releaseGlobalVirtualDevice();
        }

        void TextLayouterDevice::setFont(const Font& rFont)
        {
            mrDevice.SetFont( rFont );
        }

        void TextLayouterDevice::setFontAttribute(
            const attribute::FontAttribute& rFontAttribute,
            double fFontScaleX,
            double fFontScaleY,
            const ::com::sun::star::lang::Locale& rLocale)
        {
            setFont(getVclFontFromFontAttribute(
                rFontAttribute,
                fFontScaleX,
                fFontScaleY,
                0.0,
                rLocale));
        }

        double TextLayouterDevice::getOverlineOffset() const
        {
            const ::FontMetric& rMetric = mrDevice.GetFontMetric();
            double fRet = (rMetric.GetIntLeading() / 2.0) - rMetric.GetAscent();
            return fRet;
        }

        double TextLayouterDevice::getUnderlineOffset() const
        {
            const ::FontMetric& rMetric = mrDevice.GetFontMetric();
            double fRet = rMetric.GetDescent() / 2.0;
            return fRet;
        }

        double TextLayouterDevice::getStrikeoutOffset() const
        {
            const ::FontMetric& rMetric = mrDevice.GetFontMetric();
            double fRet = (rMetric.GetAscent() - rMetric.GetIntLeading()) / 3.0;
            return fRet;
        }

        double TextLayouterDevice::getOverlineHeight() const
        {
            const ::FontMetric& rMetric = mrDevice.GetFontMetric();
            double fRet = rMetric.GetIntLeading() / 2.5;
            return fRet;
        }

        double TextLayouterDevice::getUnderlineHeight() const
        {
            const ::FontMetric& rMetric = mrDevice.GetFontMetric();
            double fRet = rMetric.GetDescent() / 4.0;
            return fRet;
        }

        double TextLayouterDevice::getTextHeight() const
        {
            return mrDevice.GetTextHeight();
        }

        double TextLayouterDevice::getTextWidth(
            const OUString& rText,
            sal_uInt32 nIndex,
            sal_uInt32 nLength) const
        {
            return mrDevice.GetTextWidth(rText, nIndex, nLength);
        }

        bool TextLayouterDevice::getTextOutlines(
            basegfx::B2DPolyPolygonVector& rB2DPolyPolyVector,
            const OUString& rText,
            sal_uInt32 nIndex,
            sal_uInt32 nLength,
            const ::std::vector< double >& rDXArray) const
        {
            const sal_uInt32 nDXArrayCount(rDXArray.size());
            sal_uInt32 nTextLength(nLength);
            const sal_uInt32 nStringLength(rText.getLength());

            if(nTextLength + nIndex > nStringLength)
            {
                nTextLength = nStringLength - nIndex;
            }

            if(nDXArrayCount)
            {
                OSL_ENSURE(nDXArrayCount == nTextLength, "DXArray size does not correspond to text portion size (!)");
                std::vector< sal_Int32 > aIntegerDXArray(nDXArrayCount);

                for(sal_uInt32 a(0); a < nDXArrayCount; a++)
                {
                    aIntegerDXArray[a] = basegfx::fround(rDXArray[a]);
                }

                return mrDevice.GetTextOutlines(
                    rB2DPolyPolyVector,
                    rText,
                    nIndex,
                    nIndex,
                    nLength,
                    true,
                    0,
                    &(aIntegerDXArray[0]));
            }
            else
            {
                return mrDevice.GetTextOutlines(
                    rB2DPolyPolyVector,
                    rText,
                    nIndex,
                    nIndex,
                    nLength,
                    true,
                    0,
                    0);
            }
        }

        basegfx::B2DRange TextLayouterDevice::getTextBoundRect(
            const OUString& rText,
            sal_uInt32 nIndex,
            sal_uInt32 nLength) const
        {
            sal_uInt32 nTextLength(nLength);
            const sal_uInt32 nStringLength(rText.getLength());

            if(nTextLength + nIndex > nStringLength)
            {
                nTextLength = nStringLength - nIndex;
            }

            if(nTextLength)
            {
                Rectangle aRect;

                mrDevice.GetTextBoundRect(
                    aRect,
                    rText,
                    nIndex,
                    nIndex,
                    nLength);

                // #i104432#, #i102556# take empty results into account
                if(!aRect.IsEmpty())
                {
                    return basegfx::B2DRange(
                        aRect.Left(), aRect.Top(),
                        aRect.Right(), aRect.Bottom());
                }
            }

            return basegfx::B2DRange();
        }

        double TextLayouterDevice::getFontAscent() const
        {
            const ::FontMetric& rMetric = mrDevice.GetFontMetric();
            return rMetric.GetAscent();
        }

        double TextLayouterDevice::getFontDescent() const
        {
            const ::FontMetric& rMetric = mrDevice.GetFontMetric();
            return rMetric.GetDescent();
        }

        void TextLayouterDevice::addTextRectActions(
            const Rectangle& rRectangle,
            const OUString& rText,
            sal_uInt16 nStyle,
            GDIMetaFile& rGDIMetaFile) const
        {
            mrDevice.AddTextRectActions(
                rRectangle, rText, nStyle, rGDIMetaFile);
        }

        ::std::vector< double > TextLayouterDevice::getTextArray(
            const OUString& rText,
            sal_uInt32 nIndex,
            sal_uInt32 nLength) const
        {
            ::std::vector< double > aRetval;
            sal_uInt32 nTextLength(nLength);
            const sal_uInt32 nStringLength(rText.getLength());

            if(nTextLength + nIndex > nStringLength)
            {
                nTextLength = nStringLength - nIndex;
            }

            if(nTextLength)
            {
                aRetval.reserve(nTextLength);
                ::std::vector<sal_Int32> aArray(nTextLength);
                mrDevice.GetTextArray(rText, &aArray[0], nIndex, nLength);
                aRetval.assign(aArray.begin(), aArray.end());
            }

            return aRetval;
        }

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


// helper methods for vcl font handling

namespace drawinglayer
{
    namespace primitive2d
    {
        Font getVclFontFromFontAttribute(
            const attribute::FontAttribute& rFontAttribute,
            double fFontScaleX,
            double fFontScaleY,
            double fFontRotation,
            const ::com::sun::star::lang::Locale& rLocale)
        {
            // detect FontScaling
            const sal_uInt32 nHeight(basegfx::fround(fabs(fFontScaleY)));
            const sal_uInt32 nWidth(basegfx::fround(fabs(fFontScaleX)));
            const bool bFontIsScaled(nHeight != nWidth);

#ifdef WIN32
            // for WIN32 systems, start with creating an unscaled font. If FontScaling
            // is wanted, that width needs to be adapted using FontMetric again to get a
            // width of the unscaled font
            Font aRetval(
                rFontAttribute.getFamilyName(),
                rFontAttribute.getStyleName(),
                Size(0, nHeight));
#else
            // for non-WIN32 systems things are easier since these accept a Font creation
            // with initially nWidth != nHeight for FontScaling. Despite that, use zero for
            // FontWidth when no scaling is used to explicitly have that zero when e.g. the
            // Font would be recorded in a MetaFile (The MetaFile FontAction WILL record a
            // set FontWidth; import that in a WIN32 system, and trouble is there)
            Font aRetval(
                rFontAttribute.getFamilyName(),
                rFontAttribute.getStyleName(),
                Size(bFontIsScaled ? nWidth : 0, nHeight));
#endif
            // define various other FontAttribute
            aRetval.SetAlign(ALIGN_BASELINE);
            aRetval.SetCharSet(rFontAttribute.getSymbol() ? RTL_TEXTENCODING_SYMBOL : RTL_TEXTENCODING_UNICODE);
            aRetval.SetVertical(rFontAttribute.getVertical() ? sal_True : sal_False);
            aRetval.SetWeight(static_cast<FontWeight>(rFontAttribute.getWeight()));
            aRetval.SetItalic(rFontAttribute.getItalic() ? ITALIC_NORMAL : ITALIC_NONE);
            aRetval.SetOutline(rFontAttribute.getOutline());
            aRetval.SetPitch(rFontAttribute.getMonospaced() ? PITCH_FIXED : PITCH_VARIABLE);
            aRetval.SetLanguage(LanguageTag::convertToLanguageType( rLocale, false));

#ifdef WIN32
            // for WIN32 systems, correct the FontWidth if FontScaling is used
            if(bFontIsScaled && nHeight > 0)
            {
                const FontMetric aUnscaledFontMetric(Application::GetDefaultDevice()->GetFontMetric(aRetval));

                if(aUnscaledFontMetric.GetWidth() > 0)
                {
                    const double fScaleFactor((double)nWidth / (double)nHeight);
                    const sal_uInt32 nScaledWidth(basegfx::fround((double)aUnscaledFontMetric.GetWidth() * fScaleFactor));
                    aRetval.SetWidth(nScaledWidth);
                }
            }
#endif
            // handle FontRotation (if defined)
            if(!basegfx::fTools::equalZero(fFontRotation))
            {
                sal_Int16 aRotate10th((sal_Int16)(fFontRotation * (-1800.0/F_PI)));
                aRetval.SetOrientation(aRotate10th % 3600);
            }

            return aRetval;
        }

        attribute::FontAttribute getFontAttributeFromVclFont(
            basegfx::B2DVector& o_rSize,
            const Font& rFont,
            bool bRTL,
            bool bBiDiStrong)
        {
            const attribute::FontAttribute aRetval(
                rFont.GetName(),
                rFont.GetStyleName(),
                static_cast<sal_uInt16>(rFont.GetWeight()),
                RTL_TEXTENCODING_SYMBOL == rFont.GetCharSet(),
                rFont.IsVertical(),
                ITALIC_NONE != rFont.GetItalic(),
                PITCH_FIXED == rFont.GetPitch(),
                rFont.IsOutline(),
                bRTL,
                bBiDiStrong);
            // TODO: eKerning

            // set FontHeight and init to no FontScaling
            o_rSize.setY(rFont.GetSize().getHeight() > 0 ? rFont.GetSize().getHeight() : 0);
            o_rSize.setX(o_rSize.getY());

#ifdef WIN32
            // for WIN32 systems, the FontScaling at the Font is detected by
            // checking that FontWidth != 0. When FontScaling is used, WIN32
            // needs to do extra stuff to detect the correct width (since it's
            // zero and not equal the font height) and it's relationship to
            // the height
            if(rFont.GetSize().getWidth() > 0)
            {
                Font aUnscaledFont(rFont);
                aUnscaledFont.SetWidth(0);
                const FontMetric aUnscaledFontMetric(Application::GetDefaultDevice()->GetFontMetric(aUnscaledFont));

                if(aUnscaledFontMetric.GetWidth() > 0)
                {
                    const double fScaleFactor((double)rFont.GetSize().getWidth() / (double)aUnscaledFontMetric.GetWidth());
                    o_rSize.setX(fScaleFactor * o_rSize.getY());
                }
            }
#else
            // For non-WIN32 systems the detection is the same, but the value
            // is easier achieved since width == height is interpreted as no
            // scaling. Ergo, Width == 0 means width == height, and width != 0
            // means the scaling is in the direct relation of width to height
            if(rFont.GetSize().getWidth() > 0)
            {
                o_rSize.setX((double)rFont.GetSize().getWidth());
            }
#endif
            return aRetval;
        }
    } // end of namespace primitive2d
} // end of namespace drawinglayer

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */