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 .
21 #include <canvas/debug.hxx>
23 #include <rtl/math.hxx>
24 #include <basegfx/numeric/ftools.hxx>
25 #include <i18nlangtag/languagetag.hxx>
26 #include <vcl/metric.hxx>
28 #include <com/sun/star/rendering/PanoseProportion.hpp>
29 #include <cppuhelper/supportsservice.hxx>
31 #include "canvasfont.hxx"
32 #include "textlayout.hxx"
34 using namespace ::com::sun::star
;
39 CanvasFont::CanvasFont( const rendering::FontRequest
& rFontRequest
,
40 const uno::Sequence
< beans::PropertyValue
>& ,
41 const geometry::Matrix2D
& rFontMatrix
,
42 rendering::XGraphicDevice
& rDevice
,
43 const OutDevProviderSharedPtr
& rOutDevProvider
) :
44 CanvasFont_Base( m_aMutex
),
45 maFont( vcl::Font( rFontRequest
.FontDescription
.FamilyName
,
46 rFontRequest
.FontDescription
.StyleName
,
47 Size( 0, ::basegfx::fround(rFontRequest
.CellSize
) ) ) ),
48 maFontRequest( rFontRequest
),
49 mpRefDevice( &rDevice
),
50 mpOutDevProvider( rOutDevProvider
)
52 maFont
->SetAlign( ALIGN_BASELINE
);
53 maFont
->SetCharSet( (rFontRequest
.FontDescription
.IsSymbolFont
==com::sun::star::util::TriState_YES
) ? RTL_TEXTENCODING_SYMBOL
: RTL_TEXTENCODING_UNICODE
);
54 maFont
->SetVertical( rFontRequest
.FontDescription
.IsVertical
==com::sun::star::util::TriState_YES
);
56 // TODO(F2): improve panose->vclenum conversion
57 maFont
->SetWeight( static_cast<FontWeight
>(rFontRequest
.FontDescription
.FontDescription
.Weight
) );
58 maFont
->SetItalic( (rFontRequest
.FontDescription
.FontDescription
.Letterform
<=8) ? ITALIC_NONE
: ITALIC_NORMAL
);
60 rFontRequest
.FontDescription
.FontDescription
.Proportion
== rendering::PanoseProportion::MONO_SPACED
61 ? PITCH_FIXED
: PITCH_VARIABLE
);
63 maFont
->SetLanguage( LanguageTag::convertToLanguageType( rFontRequest
.Locale
, false));
65 // adjust to stretched/shrunk font
66 if( !::rtl::math::approxEqual( rFontMatrix
.m00
, rFontMatrix
.m11
) )
68 OutputDevice
& rOutDev( rOutDevProvider
->getOutDev() );
70 const bool bOldMapState( rOutDev
.IsMapModeEnabled() );
71 rOutDev
.EnableMapMode(false);
73 const Size aSize
= rOutDev
.GetFontMetric( *maFont
).GetSize();
75 const double fDividend( rFontMatrix
.m10
+ rFontMatrix
.m11
);
76 double fStretch
= (rFontMatrix
.m00
+ rFontMatrix
.m01
);
78 if( !::basegfx::fTools::equalZero( fDividend
) )
79 fStretch
/= fDividend
;
81 const long nNewWidth
= ::basegfx::fround( aSize
.Width() * fStretch
);
83 maFont
->SetWidth( nNewWidth
);
85 rOutDev
.EnableMapMode(bOldMapState
);
89 void SAL_CALL
CanvasFont::disposing()
91 SolarMutexGuard aGuard
;
93 mpOutDevProvider
.reset();
97 uno::Reference
< rendering::XTextLayout
> SAL_CALL
CanvasFont::createTextLayout( const rendering::StringContext
& aText
, sal_Int8 nDirection
, sal_Int64 nRandomSeed
) throw (uno::RuntimeException
, std::exception
)
99 SolarMutexGuard aGuard
;
101 if( !mpRefDevice
.is() )
102 return uno::Reference
< rendering::XTextLayout
>(); // we're disposed
104 return new TextLayout( aText
,
112 rendering::FontRequest SAL_CALL
CanvasFont::getFontRequest( ) throw (uno::RuntimeException
, std::exception
)
114 SolarMutexGuard aGuard
;
116 return maFontRequest
;
119 rendering::FontMetrics SAL_CALL
CanvasFont::getFontMetrics( ) throw (uno::RuntimeException
, std::exception
)
121 SolarMutexGuard aGuard
;
123 OutputDevice
& rOutDev
= mpOutDevProvider
->getOutDev();
124 ScopedVclPtrInstance
< VirtualDevice
> pVDev( rOutDev
);
125 pVDev
->SetFont(getVCLFont());
126 const ::FontMetric
& aMetric( pVDev
->GetFontMetric() );
128 return rendering::FontMetrics(
130 aMetric
.GetDescent(),
131 aMetric
.GetIntLeading(),
132 aMetric
.GetExtLeading(),
134 aMetric
.GetDescent() / 2.0,
135 aMetric
.GetAscent() / 2.0);
138 uno::Sequence
< double > SAL_CALL
CanvasFont::getAvailableSizes( ) throw (uno::RuntimeException
, std::exception
)
140 SolarMutexGuard aGuard
;
143 return uno::Sequence
< double >();
146 uno::Sequence
< beans::PropertyValue
> SAL_CALL
CanvasFont::getExtraFontProperties( ) throw (uno::RuntimeException
, std::exception
)
148 SolarMutexGuard aGuard
;
151 return uno::Sequence
< beans::PropertyValue
>();
154 OUString SAL_CALL
CanvasFont::getImplementationName() throw( uno::RuntimeException
, std::exception
)
156 return OUString( "VCLCanvas::CanvasFont" );
159 sal_Bool SAL_CALL
CanvasFont::supportsService( const OUString
& ServiceName
) throw( uno::RuntimeException
, std::exception
)
161 return cppu::supportsService( this, ServiceName
);
164 uno::Sequence
< OUString
> SAL_CALL
CanvasFont::getSupportedServiceNames() throw( uno::RuntimeException
, std::exception
)
166 uno::Sequence
< OUString
> aRet(1);
167 aRet
[0] = "com.sun.star.rendering.CanvasFont";
172 vcl::Font
CanvasFont::getVCLFont() const
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */