LibreOffice Module drawinglayer (master) 1
textlayoutdevice.cxx
Go to the documentation of this file.
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#include <sal/config.h>
21
22#include <algorithm>
23
24#include <com/sun/star/uno/XComponentContext.hpp>
29#include <osl/diagnose.h>
30#include <tools/gen.hxx>
31#include <vcl/canvastools.hxx>
32#include <vcl/kernarray.hxx>
33#include <vcl/timer.hxx>
34#include <vcl/virdev.hxx>
35#include <vcl/font.hxx>
36#include <vcl/metric.hxx>
38#include <vcl/svapp.hxx>
39
41{
42namespace
43{
44class ImpTimedRefDev;
45
46// VDev RevDevice provider
47
48//the scoped_timed_RefDev owns an ImpTimeRefDev and releases it on dtor
49//or disposing of the default XComponentContext which causes the underlying
50//OutputDevice to get released
51
52//The ImpTimerRefDev itself, if the timeout ever gets hit, will call
53//reset on the scoped_timed_RefDev to release the ImpTimerRefDev early
54//if it's unused for a few minutes
55class scoped_timed_RefDev : public comphelper::unique_disposing_ptr<ImpTimedRefDev>
56{
57public:
58 scoped_timed_RefDev()
59 : comphelper::unique_disposing_ptr<ImpTimedRefDev>(
60 (css::uno::Reference<css::lang::XComponent>(
61 ::comphelper::getProcessComponentContext(), css::uno::UNO_QUERY_THROW)))
62 {
63 }
64};
65
66class the_scoped_timed_RefDev : public rtl::Static<scoped_timed_RefDev, the_scoped_timed_RefDev>
67{
68};
69
70class ImpTimedRefDev : public Timer
71{
72 scoped_timed_RefDev& mrOwnerOfMe;
74 sal_uInt32 mnUseCount;
75
76public:
77 explicit ImpTimedRefDev(scoped_timed_RefDev& rOwnerofMe);
78 virtual ~ImpTimedRefDev() override;
79 virtual void Invoke() override;
80
81 VirtualDevice& acquireVirtualDevice();
82 void releaseVirtualDevice();
83};
84
85ImpTimedRefDev::ImpTimedRefDev(scoped_timed_RefDev& rOwnerOfMe)
86 : Timer("drawinglayer ImpTimedRefDev destroy mpVirDev")
87 , mrOwnerOfMe(rOwnerOfMe)
88 , mpVirDev(nullptr)
89 , mnUseCount(0)
90{
91 SetTimeout(3L * 60L * 1000L); // three minutes
92 Start();
93}
94
95ImpTimedRefDev::~ImpTimedRefDev()
96{
97 OSL_ENSURE(0 == mnUseCount, "destruction of a still used ImpTimedRefDev (!)");
98 const SolarMutexGuard aSolarGuard;
100}
101
102void ImpTimedRefDev::Invoke()
103{
104 // for obvious reasons, do not call anything after this
105 mrOwnerOfMe.reset();
106}
107
108VirtualDevice& ImpTimedRefDev::acquireVirtualDevice()
109{
110 if (!mpVirDev)
111 {
113 mpVirDev->SetReferenceDevice(VirtualDevice::RefDevMode::MSO1);
114 }
115
116 if (!mnUseCount)
117 {
118 Stop();
119 }
120
121 mnUseCount++;
122
123 return *mpVirDev;
124}
125
126void ImpTimedRefDev::releaseVirtualDevice()
127{
128 OSL_ENSURE(mnUseCount, "mismatch call number to releaseVirtualDevice() (!)");
129 mnUseCount--;
130
131 if (!mnUseCount)
132 {
133 Start();
134 }
135}
136
137VirtualDevice& acquireGlobalVirtualDevice()
138{
139 scoped_timed_RefDev& rStdRefDevice = the_scoped_timed_RefDev::get();
140
141 if (!rStdRefDevice)
142 rStdRefDevice.reset(new ImpTimedRefDev(rStdRefDevice));
143
144 return rStdRefDevice->acquireVirtualDevice();
145}
146
147void releaseGlobalVirtualDevice()
148{
149 scoped_timed_RefDev& rStdRefDevice = the_scoped_timed_RefDev::get();
150
151 OSL_ENSURE(rStdRefDevice,
152 "releaseGlobalVirtualDevice() without prior acquireGlobalVirtualDevice() call(!)");
153 rStdRefDevice->releaseVirtualDevice();
154}
155
156} // end of anonymous namespace
157
158TextLayouterDevice::TextLayouterDevice()
159 : mrDevice(acquireGlobalVirtualDevice())
160{
161}
162
163TextLayouterDevice::~TextLayouterDevice() COVERITY_NOEXCEPT_FALSE { releaseGlobalVirtualDevice(); }
164
166
168 double fFontScaleX, double fFontScaleY,
169 const css::lang::Locale& rLocale)
170{
171 setFont(getVclFontFromFontAttribute(rFontAttribute, fFontScaleX, fFontScaleY, 0.0, rLocale));
172}
173
175{
176 const ::FontMetric& rMetric = mrDevice.GetFontMetric();
177 double fRet = (rMetric.GetInternalLeading() / 2.0) - rMetric.GetAscent();
178 return fRet;
179}
180
182{
183 const ::FontMetric& rMetric = mrDevice.GetFontMetric();
184 double fRet = rMetric.GetDescent() / 2.0;
185 return fRet;
186}
187
189{
190 const ::FontMetric& rMetric = mrDevice.GetFontMetric();
191 double fRet = (rMetric.GetAscent() - rMetric.GetInternalLeading()) / 3.0;
192 return fRet;
193}
194
196{
197 const ::FontMetric& rMetric = mrDevice.GetFontMetric();
198 double fRet = rMetric.GetInternalLeading() / 2.5;
199 return fRet;
200}
201
203{
204 const ::FontMetric& rMetric = mrDevice.GetFontMetric();
205 double fRet = rMetric.GetDescent() / 4.0;
206 return fRet;
207}
208
210
211double TextLayouterDevice::getTextWidth(const OUString& rText, sal_uInt32 nIndex,
212 sal_uInt32 nLength) const
213{
214 return mrDevice.GetTextWidth(rText, nIndex, nLength);
215}
216
218 const OUString& rText, sal_uInt32 nIndex,
219 sal_uInt32 nLength, const std::vector<double>& rDXArray,
220 const std::vector<sal_Bool>& rKashidaArray) const
221{
222 const sal_uInt32 nDXArrayCount(rDXArray.size());
223 sal_uInt32 nTextLength(nLength);
224 const sal_uInt32 nStringLength(rText.getLength());
225
226 if (nTextLength + nIndex > nStringLength)
227 {
228 nTextLength = nStringLength - nIndex;
229 }
230
231 if (nDXArrayCount)
232 {
233 OSL_ENSURE(nDXArrayCount == nTextLength,
234 "DXArray size does not correspond to text portion size (!)");
235
236 KernArray aIntegerDXArray;
237 aIntegerDXArray.reserve(nDXArrayCount);
238 for (sal_uInt32 a(0); a < nDXArrayCount; a++)
239 aIntegerDXArray.push_back(basegfx::fround(rDXArray[a]));
240
241 mrDevice.GetTextOutlines(rB2DPolyPolyVector, rText, nIndex, nIndex, nLength, 0,
242 aIntegerDXArray, rKashidaArray);
243 }
244 else
245 {
246 mrDevice.GetTextOutlines(rB2DPolyPolyVector, rText, nIndex, nIndex, nLength);
247 }
248}
249
250basegfx::B2DRange TextLayouterDevice::getTextBoundRect(const OUString& rText, sal_uInt32 nIndex,
251 sal_uInt32 nLength) const
252{
253 sal_uInt32 nTextLength(nLength);
254 const sal_uInt32 nStringLength(rText.getLength());
255
256 if (nTextLength + nIndex > nStringLength)
257 {
258 nTextLength = nStringLength - nIndex;
259 }
260
261 if (nTextLength)
262 {
263 ::tools::Rectangle aRect;
264
266
267 // #i104432#, #i102556# take empty results into account
268 if (!aRect.IsEmpty())
269 {
271 }
272 }
273
274 return basegfx::B2DRange();
275}
276
278{
279 const ::FontMetric& rMetric = mrDevice.GetFontMetric();
280 return rMetric.GetAscent();
281}
282
284{
285 const ::FontMetric& rMetric = mrDevice.GetFontMetric();
286 return rMetric.GetDescent();
287}
288
289void TextLayouterDevice::addTextRectActions(const ::tools::Rectangle& rRectangle,
290 const OUString& rText, DrawTextFlags nStyle,
291 GDIMetaFile& rGDIMetaFile) const
292{
293 mrDevice.AddTextRectActions(rRectangle, rText, nStyle, rGDIMetaFile);
294}
295
296std::vector<double> TextLayouterDevice::getTextArray(const OUString& rText, sal_uInt32 nIndex,
297 sal_uInt32 nLength, bool bCaret) const
298{
299 std::vector<double> aRetval;
300 sal_uInt32 nTextLength(nLength);
301 const sal_uInt32 nStringLength(rText.getLength());
302
303 if (nTextLength + nIndex > nStringLength)
304 {
305 nTextLength = nStringLength - nIndex;
306 }
307
308 if (nTextLength)
309 {
310 KernArray aArray;
311 mrDevice.GetTextArray(rText, &aArray, nIndex, nTextLength, bCaret);
312 aRetval.reserve(aArray.size());
313 for (size_t i = 0, nEnd = aArray.size(); i < nEnd; ++i)
314 aRetval.push_back(aArray[i]);
315 }
316
317 return aRetval;
318}
319
320// helper methods for vcl font handling
321
323 double fFontScaleX, double fFontScaleY, double fFontRotation,
324 const css::lang::Locale& rLocale)
325{
326 // detect FontScaling
327 const sal_uInt32 nHeight(basegfx::fround(fabs(fFontScaleY)));
328 const sal_uInt32 nWidth(basegfx::fround(fabs(fFontScaleX)));
329 const bool bFontIsScaled(nHeight != nWidth);
330
331#ifdef _WIN32
332 // for WIN32 systems, start with creating an unscaled font. If FontScaling
333 // is wanted, that width needs to be adapted using FontMetric again to get a
334 // width of the unscaled font
335 vcl::Font aRetval(rFontAttribute.getFamilyName(), rFontAttribute.getStyleName(),
336 Size(0, nHeight));
337#else
338 // for non-WIN32 systems things are easier since these accept a Font creation
339 // with initially nWidth != nHeight for FontScaling. Despite that, use zero for
340 // FontWidth when no scaling is used to explicitly have that zero when e.g. the
341 // Font would be recorded in a MetaFile (The MetaFile FontAction WILL record a
342 // set FontWidth; import that in a WIN32 system, and trouble is there)
343 vcl::Font aRetval(rFontAttribute.getFamilyName(), rFontAttribute.getStyleName(),
344 Size(bFontIsScaled ? std::max<sal_uInt32>(nWidth, 1) : 0, nHeight));
345#endif
346 // define various other FontAttribute
348 aRetval.SetCharSet(rFontAttribute.getSymbol() ? RTL_TEXTENCODING_SYMBOL
349 : RTL_TEXTENCODING_UNICODE);
350 aRetval.SetVertical(rFontAttribute.getVertical());
351 aRetval.SetWeight(static_cast<FontWeight>(rFontAttribute.getWeight()));
352 aRetval.SetItalic(rFontAttribute.getItalic() ? ITALIC_NORMAL : ITALIC_NONE);
353 aRetval.SetOutline(rFontAttribute.getOutline());
354 aRetval.SetPitch(rFontAttribute.getMonospaced() ? PITCH_FIXED : PITCH_VARIABLE);
355 aRetval.SetLanguage(LanguageTag::convertToLanguageType(rLocale, false));
356
357#ifdef _WIN32
358 // for WIN32 systems, correct the FontWidth if FontScaling is used
359 if (bFontIsScaled && nHeight > 0)
360 {
361 const FontMetric aUnscaledFontMetric(
362 Application::GetDefaultDevice()->GetFontMetric(aRetval));
363
364 if (aUnscaledFontMetric.GetAverageFontWidth() > 0)
365 {
366 const double fScaleFactor(static_cast<double>(nWidth) / static_cast<double>(nHeight));
367 const sal_uInt32 nScaledWidth(basegfx::fround(
368 static_cast<double>(aUnscaledFontMetric.GetAverageFontWidth()) * fScaleFactor));
369 aRetval.SetAverageFontWidth(nScaledWidth);
370 }
371 }
372#endif
373 // handle FontRotation (if defined)
374 if (!basegfx::fTools::equalZero(fFontRotation))
375 {
376 int aRotate10th(-basegfx::rad2deg<10>(fFontRotation));
377 aRetval.SetOrientation(Degree10(aRotate10th % 3600));
378 }
379
380 return aRetval;
381}
382
384 const vcl::Font& rFont, bool bRTL,
385 bool bBiDiStrong)
386{
387 const attribute::FontAttribute aRetval(
388 rFont.GetFamilyName(), rFont.GetStyleName(), static_cast<sal_uInt16>(rFont.GetWeight()),
389 RTL_TEXTENCODING_SYMBOL == rFont.GetCharSet(), rFont.IsVertical(),
390 ITALIC_NONE != rFont.GetItalic(), PITCH_FIXED == rFont.GetPitch(), rFont.IsOutline(), bRTL,
391 bBiDiStrong);
392 // TODO: eKerning
393
394 // set FontHeight and init to no FontScaling
395 o_rSize.setY(std::max<tools::Long>(rFont.GetFontSize().getHeight(), 0));
396 o_rSize.setX(o_rSize.getY());
397
398#ifdef _WIN32
399 // for WIN32 systems, the FontScaling at the Font is detected by
400 // checking that FontWidth != 0. When FontScaling is used, WIN32
401 // needs to do extra stuff to detect the correct width (since it's
402 // zero and not equal the font height) and its relationship to
403 // the height
404 if (rFont.GetFontSize().getWidth() > 0)
405 {
406 vcl::Font aUnscaledFont(rFont);
407 aUnscaledFont.SetAverageFontWidth(0);
408 const FontMetric aUnscaledFontMetric(
409 Application::GetDefaultDevice()->GetFontMetric(aUnscaledFont));
410
411 if (aUnscaledFontMetric.GetAverageFontWidth() > 0)
412 {
413 const double fScaleFactor(
414 static_cast<double>(rFont.GetFontSize().getWidth())
415 / static_cast<double>(aUnscaledFontMetric.GetAverageFontWidth()));
416 o_rSize.setX(fScaleFactor * o_rSize.getY());
417 }
418 }
419#else
420 // For non-WIN32 systems the detection is the same, but the value
421 // is easier achieved since width == height is interpreted as no
422 // scaling. Ergo, Width == 0 means width == height, and width != 0
423 // means the scaling is in the direct relation of width to height
424 if (rFont.GetFontSize().getWidth() > 0)
425 {
426 o_rSize.setX(static_cast<double>(rFont.GetFontSize().getWidth()));
427 }
428#endif
429 return aRetval;
430}
431
432} // end of namespace
433
434/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
DrawTextFlags
static OutputDevice * GetDefaultDevice()
tools::Long GetDescent() const
tools::Long GetAscent() const
tools::Long GetInternalLeading() const
void reserve(size_t nCapacity)
size_t size() const
void push_back(sal_Int32 nUnit)
static LanguageType convertToLanguageType(const css::lang::Locale &rLocale, bool bResolveSystem=true)
void SetFont(const vcl::Font &rNewFont)
bool GetTextBoundRect(tools::Rectangle &rRect, const OUString &rStr, sal_Int32 nBase=0, sal_Int32 nIndex=0, sal_Int32 nLen=-1, sal_uLong nLayoutWidth=0, KernArraySpan aDXArray=KernArraySpan(), o3tl::span< const sal_Bool > pKashidaArray={}, const SalLayoutGlyphs *pGlyphs=nullptr) const
tools::Long GetTextWidth(const OUString &rStr, sal_Int32 nIndex=0, sal_Int32 nLen=-1, vcl::text::TextLayoutCache const *=nullptr, SalLayoutGlyphs const *const pLayoutCache=nullptr) const
void AddTextRectActions(const tools::Rectangle &rRect, const OUString &rOrigStr, DrawTextFlags nStyle, GDIMetaFile &rMtf)
tools::Long GetTextArray(const OUString &rStr, KernArray *pDXAry, sal_Int32 nIndex=0, sal_Int32 nLen=-1, bool bCaret=false, vcl::text::TextLayoutCache const *=nullptr, SalLayoutGlyphs const *const pLayoutCache=nullptr) const
FontMetric GetFontMetric() const
tools::Long GetTextHeight() const
bool GetTextOutlines(PolyPolyVector &, const OUString &rStr, sal_Int32 nBase=0, sal_Int32 nIndex=0, sal_Int32 nLen=-1, sal_uLong nLayoutWidth=0, KernArraySpan aDXArray=KernArraySpan(), o3tl::span< const sal_Bool > pKashidaArray={}) const
constexpr tools::Long getHeight() const
constexpr tools::Long getWidth() const
void disposeAndClear()
static VclPtr< reference_type > Create(Arg &&... arg)
void setY(TYPE fY)
TYPE getY() const
void setX(TYPE fX)
const OUString & getStyleName() const
const OUString & getFamilyName() const
data read access
void addTextRectActions(const tools::Rectangle &rRectangle, const OUString &rText, DrawTextFlags nStyle, GDIMetaFile &rGDIMetaFile) const
void setFontAttribute(const attribute::FontAttribute &rFontAttribute, double fFontScaleX, double fFontScaleY, const css::lang::Locale &rLocale)
basegfx::B2DRange getTextBoundRect(const OUString &rText, sal_uInt32 nIndex, sal_uInt32 nLength) const
double getTextWidth(const OUString &rText, sal_uInt32 nIndex, sal_uInt32 nLength) const
void getTextOutlines(basegfx::B2DPolyPolygonVector &, const OUString &rText, sal_uInt32 nIndex, sal_uInt32 nLength, const ::std::vector< double > &rDXArray, const ::std::vector< sal_Bool > &rKashidaArray) const
void setFont(const vcl::Font &rFont)
tooling methods
::std::vector< double > getTextArray(const OUString &rText, sal_uInt32 nIndex, sal_uInt32 nLength, bool bCaret=false) const
constexpr bool IsEmpty() const
void SetOrientation(Degree10 nLineOrientation)
void SetVertical(bool bVertical)
void SetOutline(bool bOutline)
void SetAverageFontWidth(tools::Long nWidth)
void SetPitch(FontPitch ePitch)
const OUString & GetStyleName() const
FontItalic GetItalic()
void SetItalic(FontItalic)
void SetWeight(FontWeight)
const OUString & GetFamilyName() const
void SetCharSet(rtl_TextEncoding)
const Size & GetFontSize() const
void SetAlignment(TextAlign)
FontPitch GetPitch()
FontWeight GetWeight()
rtl_TextEncoding GetCharSet() const
void SetLanguage(LanguageType)
bool IsVertical() const
bool IsOutline() const
tools::Long GetAverageFontWidth() const
aCursorMoveIdle Stop()
PITCH_VARIABLE
PITCH_FIXED
ITALIC_NORMAL
ITALIC_NONE
ALIGN_BASELINE
sal_Int32 nIndex
uno_Any a
bool equalZero(const T &rfVal)
::std::vector< B2DPolyPolygon > B2DPolyPolygonVector
B2IRange fround(const B2DRange &rRange)
Reference< XComponentContext > getProcessComponentContext()
vcl::Font getVclFontFromFontAttribute(const attribute::FontAttribute &rFontAttribute, double fFontScaleX, double fFontScaleY, double fFontRotation, const css::lang::Locale &rLocale)
Create a VCL-Font based on the definitions in FontAttribute and the given FontScaling.
attribute::FontAttribute getFontAttributeFromVclFont(basegfx::B2DVector &o_rSize, const vcl::Font &rFont, bool bRTL, bool bBiDiStrong)
Generate FontAttribute DataSet derived from the given VCL-Font.
Reference
int i
FontWeight
basegfx::B2DRange b2DRectangleFromRectangle(const ::tools::Rectangle &rRect)
sal_uInt32 mnUseCount
VclPtr< VirtualDevice > mpVirDev
scoped_timed_RefDev & mrOwnerOfMe
sal_Int32 nLength