LibreOffice Module toolkit (master) 1
vclxfont.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 <memory>
21
22#include <com/sun/star/awt/XDevice.hpp>
23
27
28#include <vcl/kernarray.hxx>
29#include <vcl/metric.hxx>
30#include <vcl/outdev.hxx>
31#include <vcl/svapp.hxx>
32
34{
35 mpFontMetric = nullptr;
36}
37
39{
40}
41
42void VCLXFont::Init( css::awt::XDevice& rxDev, const vcl::Font& rFont )
43{
44 mxDevice = &rxDev;
45
46 mpFontMetric.reset();
47
48 maFont = rFont;
49}
50
52{
53 if ( !mpFontMetric && mxDevice.is() )
54 {
56 if ( pOutDev )
57 {
58 vcl::Font aOldFont = pOutDev->GetFont();
59 pOutDev->SetFont( maFont );
60 mpFontMetric.reset( new FontMetric( pOutDev->GetFontMetric() ) );
61 pOutDev->SetFont( aOldFont );
62 }
63 }
64 return mpFontMetric != nullptr;
65}
66
67css::awt::FontDescriptor VCLXFont::getFontDescriptor( )
68{
69 std::unique_lock aGuard( maMutex );
70
72
73}
74
75css::awt::SimpleFontMetric VCLXFont::getFontMetric( )
76{
77 std::unique_lock aGuard( maMutex );
78
79 css::awt::SimpleFontMetric aFM;
82 return aFM;
83}
84
86{
87 std::unique_lock aGuard( maMutex );
88
89 sal_Int16 nRet = -1;
91 if ( pOutDev )
92 {
93 vcl::Font aOldFont = pOutDev->GetFont();
94 pOutDev->SetFont( maFont );
95
96 nRet = sal::static_int_cast< sal_Int16 >(
97 pOutDev->GetTextWidth( OUString(c) ));
98
99 pOutDev->SetFont( aOldFont );
100 }
101 return nRet;
102}
103
104css::uno::Sequence< sal_Int16 > VCLXFont::getCharWidths( sal_Unicode nFirst, sal_Unicode nLast )
105{
106 std::unique_lock aGuard( maMutex );
107
108 css::uno::Sequence<sal_Int16> aSeq;
110 if ( pOutDev )
111 {
112 vcl::Font aOldFont = pOutDev->GetFont();
113 pOutDev->SetFont( maFont );
114
115 sal_Int16 nCount = nLast-nFirst + 1;
116 aSeq = css::uno::Sequence<sal_Int16>( nCount );
117 for ( sal_uInt16 n = 0; n < nCount; n++ )
118 {
119 aSeq.getArray()[n] = sal::static_int_cast< sal_Int16 >(
120 pOutDev->GetTextWidth(
121 OUString(static_cast< sal_Unicode >(nFirst+n)) ));
122 }
123
124 pOutDev->SetFont( aOldFont );
125 }
126 return aSeq;
127}
128
129sal_Int32 VCLXFont::getStringWidth( const OUString& str )
130{
131 std::unique_lock aGuard( maMutex );
132
133 sal_Int32 nRet = -1;
135 if ( pOutDev )
136 {
137 vcl::Font aOldFont = pOutDev->GetFont();
138 pOutDev->SetFont( maFont );
139 nRet = pOutDev->GetTextWidth( str );
140 pOutDev->SetFont( aOldFont );
141 }
142 return nRet;
143}
144
145sal_Int32 VCLXFont::getStringWidthArray( const OUString& str, css::uno::Sequence< sal_Int32 >& rDXArray )
146{
147 std::unique_lock aGuard( maMutex );
148
149 sal_Int32 nRet = -1;
151 if ( pOutDev )
152 {
153 vcl::Font aOldFont = pOutDev->GetFont();
154 pOutDev->SetFont( maFont );
155 KernArray aDXA;
156 nRet = pOutDev->GetTextArray( str, &aDXA );
157 rDXArray.realloc(aDXA.size());
158 sal_Int32* pArray = rDXArray.getArray();
159 for (size_t i = 0, nLen = aDXA.size(); i < nLen; ++i)
160 pArray[i] = aDXA[i];
161 pOutDev->SetFont( aOldFont );
162 }
163 return nRet;
164}
165
166void VCLXFont::getKernPairs( css::uno::Sequence< sal_Unicode >& /*rnChars1*/, css::uno::Sequence< sal_Unicode >& /*rnChars2*/, css::uno::Sequence< sal_Int16 >& /*rnKerns*/ )
167{
168 // NOTE: this empty method is just used for keeping the related UNO-API stable
169}
170
171// css::awt::XFont2
172sal_Bool VCLXFont::hasGlyphs( const OUString& aText )
173{
174 std::unique_lock aGuard( maMutex );
175 SolarMutexGuard aSolarGuard;
176
178 if ( pOutDev )
179 {
180 if ( pOutDev->HasGlyphs( maFont, aText ) == -1 )
181 {
182 return true;
183 }
184 }
185
186 return false;
187}
188
189/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
size_t size() const
const vcl::Font & GetFont() const
void SetFont(const vcl::Font &rNewFont)
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
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
sal_Int32 HasGlyphs(const vcl::Font &rFont, std::u16string_view rStr, sal_Int32 nIndex=0, sal_Int32 nLen=-1) const
static css::awt::SimpleFontMetric CreateFontMetric(const FontMetric &rFontMetric)
static css::awt::FontDescriptor CreateFontDescriptor(const vcl::Font &rFont)
convert Font to css::awt::FontDescriptor
static OutputDevice * GetOutputDevice(const css::uno::Reference< css::awt::XDevice > &rxDevice)
sal_Bool SAL_CALL hasGlyphs(const OUString &aText) override
Definition: vclxfont.cxx:172
std::unique_ptr< FontMetric > mpFontMetric
Definition: vclxfont.hxx:46
css::awt::SimpleFontMetric SAL_CALL getFontMetric() override
Definition: vclxfont.cxx:75
virtual ~VCLXFont() override
Definition: vclxfont.cxx:38
void Init(css::awt::XDevice &rxDev, const vcl::Font &rFont)
Definition: vclxfont.cxx:42
vcl::Font maFont
Definition: vclxfont.hxx:44
css::awt::FontDescriptor SAL_CALL getFontDescriptor() override
Definition: vclxfont.cxx:67
sal_Int16 SAL_CALL getCharWidth(sal_Unicode c) override
Definition: vclxfont.cxx:85
VCLXFont()
Definition: vclxfont.cxx:33
sal_Int32 SAL_CALL getStringWidth(const OUString &str) override
Definition: vclxfont.cxx:129
bool ImplAssertValidFontMetric()
Definition: vclxfont.cxx:51
void SAL_CALL getKernPairs(css::uno::Sequence< sal_Unicode > &rnChars1, css::uno::Sequence< sal_Unicode > &rnChars2, css::uno::Sequence< sal_Int16 > &rnKerns) override
Definition: vclxfont.cxx:166
css::uno::Reference< css::awt::XDevice > mxDevice
Definition: vclxfont.hxx:43
std::mutex maMutex
Definition: vclxfont.hxx:42
sal_Int32 SAL_CALL getStringWidthArray(const OUString &str, css::uno::Sequence< sal_Int32 > &rDXArray) override
Definition: vclxfont.cxx:145
css::uno::Sequence< sal_Int16 > SAL_CALL getCharWidths(sal_Unicode nFirst, sal_Unicode nLast) override
Definition: vclxfont.cxx:104
int nCount
sal_Int64 n
Sequence< sal_Int8 > aSeq
int i
unsigned char sal_Bool
sal_uInt16 sal_Unicode