1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
22 #include <com/sun/star/awt/XDevice.hpp>
24 #include <comphelper/sequence.hxx>
25 #include <toolkit/awt/vclxfont.hxx>
26 #include <toolkit/helper/vclunohelper.hxx>
28 #include <vcl/kernarray.hxx>
29 #include <vcl/metric.hxx>
30 #include <vcl/outdev.hxx>
31 #include <vcl/svapp.hxx>
35 mpFontMetric
= nullptr;
42 void VCLXFont::Init( css::awt::XDevice
& rxDev
, const vcl::Font
& rFont
)
51 bool VCLXFont::ImplAssertValidFontMetric()
53 if ( !mpFontMetric
&& mxDevice
.is() )
55 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
58 vcl::Font aOldFont
= pOutDev
->GetFont();
59 pOutDev
->SetFont( maFont
);
60 mpFontMetric
.reset( new FontMetric( pOutDev
->GetFontMetric() ) );
61 pOutDev
->SetFont( aOldFont
);
64 return mpFontMetric
!= nullptr;
67 css::awt::FontDescriptor
VCLXFont::getFontDescriptor( )
69 std::unique_lock
aGuard( maMutex
);
71 return VCLUnoHelper::CreateFontDescriptor( maFont
);
75 css::awt::SimpleFontMetric
VCLXFont::getFontMetric( )
77 std::unique_lock
aGuard( maMutex
);
79 css::awt::SimpleFontMetric aFM
;
80 if ( ImplAssertValidFontMetric() )
81 aFM
= VCLUnoHelper::CreateFontMetric( *mpFontMetric
);
85 sal_Int16
VCLXFont::getCharWidth( sal_Unicode c
)
87 std::unique_lock
aGuard( maMutex
);
90 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
93 vcl::Font aOldFont
= pOutDev
->GetFont();
94 pOutDev
->SetFont( maFont
);
96 nRet
= sal::static_int_cast
< sal_Int16
>(
97 pOutDev
->GetTextWidth( OUString(c
) ));
99 pOutDev
->SetFont( aOldFont
);
104 css::uno::Sequence
< sal_Int16
> VCLXFont::getCharWidths( sal_Unicode nFirst
, sal_Unicode nLast
)
106 std::unique_lock
aGuard( maMutex
);
108 css::uno::Sequence
<sal_Int16
> aSeq
;
109 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
112 vcl::Font aOldFont
= pOutDev
->GetFont();
113 pOutDev
->SetFont( maFont
);
115 sal_Int16 nCount
= nLast
-nFirst
+ 1;
116 aSeq
= css::uno::Sequence
<sal_Int16
>( nCount
);
117 for ( sal_uInt16 n
= 0; n
< nCount
; n
++ )
119 aSeq
.getArray()[n
] = sal::static_int_cast
< sal_Int16
>(
120 pOutDev
->GetTextWidth(
121 OUString(static_cast< sal_Unicode
>(nFirst
+n
)) ));
124 pOutDev
->SetFont( aOldFont
);
129 sal_Int32
VCLXFont::getStringWidth( const OUString
& str
)
131 std::unique_lock
aGuard( maMutex
);
134 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
137 vcl::Font aOldFont
= pOutDev
->GetFont();
138 pOutDev
->SetFont( maFont
);
139 nRet
= pOutDev
->GetTextWidth( str
);
140 pOutDev
->SetFont( aOldFont
);
145 sal_Int32
VCLXFont::getStringWidthArray( const OUString
& str
, css::uno::Sequence
< sal_Int32
>& rDXArray
)
147 std::unique_lock
aGuard( maMutex
);
150 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
153 vcl::Font aOldFont
= pOutDev
->GetFont();
154 pOutDev
->SetFont( maFont
);
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
)
161 pOutDev
->SetFont( aOldFont
);
166 void VCLXFont::getKernPairs( css::uno::Sequence
< sal_Unicode
>& /*rnChars1*/, css::uno::Sequence
< sal_Unicode
>& /*rnChars2*/, css::uno::Sequence
< sal_Int16
>& /*rnKerns*/ )
168 // NOTE: this empty method is just used for keeping the related UNO-API stable
172 sal_Bool
VCLXFont::hasGlyphs( const OUString
& aText
)
174 std::unique_lock
aGuard( maMutex
);
175 SolarMutexGuard aSolarGuard
;
177 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
180 if ( pOutDev
->HasGlyphs( maFont
, aText
) == -1 )
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */