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>
33 VCLXFont::VCLXFont(css::awt::XDevice
& rxDev
, const vcl::Font
& rFont
)
43 bool VCLXFont::ImplAssertValidFontMetric()
45 if ( !mpFontMetric
&& mxDevice
.is() )
47 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
50 vcl::Font aOldFont
= pOutDev
->GetFont();
51 pOutDev
->SetFont( maFont
);
52 mpFontMetric
.reset( new FontMetric( pOutDev
->GetFontMetric() ) );
53 pOutDev
->SetFont( aOldFont
);
56 return mpFontMetric
!= nullptr;
59 css::awt::FontDescriptor
VCLXFont::getFontDescriptor( )
61 std::unique_lock
aGuard( maMutex
);
63 return VCLUnoHelper::CreateFontDescriptor( maFont
);
67 css::awt::SimpleFontMetric
VCLXFont::getFontMetric( )
69 std::unique_lock
aGuard( maMutex
);
71 css::awt::SimpleFontMetric aFM
;
72 if ( ImplAssertValidFontMetric() )
73 aFM
= VCLUnoHelper::CreateFontMetric( *mpFontMetric
);
77 sal_Int16
VCLXFont::getCharWidth( sal_Unicode c
)
79 std::unique_lock
aGuard( maMutex
);
82 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
85 vcl::Font aOldFont
= pOutDev
->GetFont();
86 pOutDev
->SetFont( maFont
);
88 nRet
= sal::static_int_cast
< sal_Int16
>(
89 pOutDev
->GetTextWidth( OUString(c
) ));
91 pOutDev
->SetFont( aOldFont
);
96 css::uno::Sequence
< sal_Int16
> VCLXFont::getCharWidths( sal_Unicode nFirst
, sal_Unicode nLast
)
98 std::unique_lock
aGuard( maMutex
);
100 css::uno::Sequence
<sal_Int16
> aSeq
;
101 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
104 vcl::Font aOldFont
= pOutDev
->GetFont();
105 pOutDev
->SetFont( maFont
);
107 sal_Int16 nCount
= nLast
-nFirst
+ 1;
108 aSeq
= css::uno::Sequence
<sal_Int16
>( nCount
);
109 for ( sal_uInt16 n
= 0; n
< nCount
; n
++ )
111 aSeq
.getArray()[n
] = sal::static_int_cast
< sal_Int16
>(
112 pOutDev
->GetTextWidth(
113 OUString(static_cast< sal_Unicode
>(nFirst
+n
)) ));
116 pOutDev
->SetFont( aOldFont
);
121 sal_Int32
VCLXFont::getStringWidth( const OUString
& str
)
123 std::unique_lock
aGuard( maMutex
);
126 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
129 vcl::Font aOldFont
= pOutDev
->GetFont();
130 pOutDev
->SetFont( maFont
);
131 nRet
= pOutDev
->GetTextWidth( str
);
132 pOutDev
->SetFont( aOldFont
);
137 sal_Int32
VCLXFont::getStringWidthArray( const OUString
& str
, css::uno::Sequence
< sal_Int32
>& rDXArray
)
139 std::unique_lock
aGuard( maMutex
);
142 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
145 vcl::Font aOldFont
= pOutDev
->GetFont();
146 pOutDev
->SetFont( maFont
);
148 nRet
= basegfx::fround(pOutDev
->GetTextArray(str
, &aDXA
).nWidth
);
149 rDXArray
.realloc(aDXA
.size());
150 sal_Int32
* pArray
= rDXArray
.getArray();
151 for (size_t i
= 0, nLen
= aDXA
.size(); i
< nLen
; ++i
)
153 pOutDev
->SetFont( aOldFont
);
158 void VCLXFont::getKernPairs( css::uno::Sequence
< sal_Unicode
>& /*rnChars1*/, css::uno::Sequence
< sal_Unicode
>& /*rnChars2*/, css::uno::Sequence
< sal_Int16
>& /*rnKerns*/ )
160 // NOTE: this empty method is just used for keeping the related UNO-API stable
164 sal_Bool
VCLXFont::hasGlyphs( const OUString
& aText
)
166 std::unique_lock
aGuard( maMutex
);
167 SolarMutexGuard aSolarGuard
;
169 OutputDevice
* pOutDev
= VCLUnoHelper::GetOutputDevice( mxDevice
);
172 if ( pOutDev
->HasGlyphs( maFont
, aText
) == -1 )
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */