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 .
20 #include <sal/config.h>
22 #include <basegfx/numeric/ftools.hxx>
23 #include <com/sun/star/rendering/PanoseProportion.hpp>
24 #include <cppuhelper/supportsservice.hxx>
25 #include <i18nlangtag/languagetag.hxx>
26 #include <rtl/math.hxx>
28 #include <vcl/metric.hxx>
30 #include <canvas/canvastools.hxx>
32 #include "cairo_canvasfont.hxx"
33 #include "cairo_textlayout.hxx"
35 using namespace ::com::sun::star
;
40 CanvasFont::CanvasFont( const rendering::FontRequest
& rFontRequest
,
41 const uno::Sequence
< beans::PropertyValue
>& rExtraFontProperties
,
42 const geometry::Matrix2D
& rFontMatrix
,
43 SurfaceProviderRef rDevice
) :
44 maFont( vcl::Font( rFontRequest
.FontDescription
.FamilyName
,
45 rFontRequest
.FontDescription
.StyleName
,
46 Size( 0, ::basegfx::fround(rFontRequest
.CellSize
) ) ) ),
47 maFontRequest( rFontRequest
),
48 mpRefDevice(std::move( rDevice
)),
51 ::canvas::tools::extractExtraFontProperties(rExtraFontProperties
, mnEmphasisMark
);
53 maFont
->SetAlignment( ALIGN_BASELINE
);
54 maFont
->SetCharSet( (rFontRequest
.FontDescription
.IsSymbolFont
==css::util::TriState_YES
) ? RTL_TEXTENCODING_SYMBOL
: RTL_TEXTENCODING_UNICODE
);
55 maFont
->SetVertical( rFontRequest
.FontDescription
.IsVertical
==css::util::TriState_YES
);
57 // TODO(F2): improve panose->vclenum conversion
58 maFont
->SetWeight( static_cast<FontWeight
>(rFontRequest
.FontDescription
.FontDescription
.Weight
) );
59 maFont
->SetItalic( (rFontRequest
.FontDescription
.FontDescription
.Letterform
<=8) ? ITALIC_NONE
: ITALIC_NORMAL
);
61 rFontRequest
.FontDescription
.FontDescription
.Proportion
== rendering::PanoseProportion::MONO_SPACED
62 ? PITCH_FIXED
: PITCH_VARIABLE
);
64 maFont
->SetLanguage( LanguageTag::convertToLanguageType( rFontRequest
.Locale
, false));
66 // adjust to stretched/shrunk font
67 if( ::rtl::math::approxEqual( rFontMatrix
.m00
, rFontMatrix
.m11
) )
70 VclPtr
<OutputDevice
> pOutDev( mpRefDevice
->getOutputDevice() );
75 const bool bOldMapState( pOutDev
->IsMapModeEnabled() );
76 pOutDev
->EnableMapMode(false);
78 const Size aSize
= pOutDev
->GetFontMetric( *maFont
).GetFontSize();
80 const double fDividend( rFontMatrix
.m10
+ rFontMatrix
.m11
);
81 double fStretch
= rFontMatrix
.m00
+ rFontMatrix
.m01
;
83 if( !::basegfx::fTools::equalZero( fDividend
) )
84 fStretch
/= fDividend
;
86 const tools::Long nNewWidth
= ::basegfx::fround( aSize
.Width() * fStretch
);
88 maFont
->SetAverageFontWidth( nNewWidth
);
90 pOutDev
->EnableMapMode(bOldMapState
);
93 void CanvasFont::disposing(std::unique_lock
<std::mutex
>& rGuard
)
97 SolarMutexGuard aGuard
;
103 uno::Reference
< rendering::XTextLayout
> SAL_CALL
CanvasFont::createTextLayout( const rendering::StringContext
& aText
, sal_Int8 nDirection
, sal_Int64 nRandomSeed
)
105 SolarMutexGuard aGuard
;
107 if( !mpRefDevice
.is() )
108 return uno::Reference
< rendering::XTextLayout
>(); // we're disposed
110 return new TextLayout( aText
,
117 rendering::FontRequest SAL_CALL
CanvasFont::getFontRequest( )
119 return maFontRequest
;
122 rendering::FontMetrics SAL_CALL
CanvasFont::getFontMetrics( )
125 return rendering::FontMetrics();
128 uno::Sequence
< double > SAL_CALL
CanvasFont::getAvailableSizes( )
131 return uno::Sequence
< double >();
134 uno::Sequence
< beans::PropertyValue
> SAL_CALL
CanvasFont::getExtraFontProperties( )
137 return uno::Sequence
< beans::PropertyValue
>();
140 OUString SAL_CALL
CanvasFont::getImplementationName()
142 return u
"CairoCanvas::CanvasFont"_ustr
;
145 sal_Bool SAL_CALL
CanvasFont::supportsService( const OUString
& ServiceName
)
147 return cppu::supportsService( this, ServiceName
);
150 uno::Sequence
< OUString
> SAL_CALL
CanvasFont::getSupportedServiceNames()
152 return { u
"com.sun.star.rendering.CanvasFont"_ustr
};
155 vcl::Font
const & CanvasFont::getVCLFont() const
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */