LibreOffice Module canvas (master) 1
cairo_canvasfont.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 <sal/config.h>
21
23#include <com/sun/star/rendering/PanoseProportion.hpp>
26#include <rtl/math.hxx>
27#include <utility>
28#include <vcl/metric.hxx>
29
30#include "cairo_canvasfont.hxx"
31#include "cairo_textlayout.hxx"
32
33using namespace ::com::sun::star;
34
35namespace cairocanvas
36{
37
38 CanvasFont::CanvasFont( const rendering::FontRequest& rFontRequest,
39 const uno::Sequence< beans::PropertyValue >& /*rExtraFontProperties*/,
40 const geometry::Matrix2D& rFontMatrix,
41 SurfaceProviderRef rDevice ) :
42 maFont( vcl::Font( rFontRequest.FontDescription.FamilyName,
43 rFontRequest.FontDescription.StyleName,
44 Size( 0, ::basegfx::fround(rFontRequest.CellSize) ) ) ),
45 maFontRequest( rFontRequest ),
46 mpRefDevice(std::move( rDevice ))
47 {
48 maFont->SetAlignment( ALIGN_BASELINE );
49 maFont->SetCharSet( (rFontRequest.FontDescription.IsSymbolFont==css::util::TriState_YES) ? RTL_TEXTENCODING_SYMBOL : RTL_TEXTENCODING_UNICODE );
50 maFont->SetVertical( rFontRequest.FontDescription.IsVertical==css::util::TriState_YES );
51
52 // TODO(F2): improve panose->vclenum conversion
53 maFont->SetWeight( static_cast<FontWeight>(rFontRequest.FontDescription.FontDescription.Weight) );
54 maFont->SetItalic( (rFontRequest.FontDescription.FontDescription.Letterform<=8) ? ITALIC_NONE : ITALIC_NORMAL );
55 maFont->SetPitch(
56 rFontRequest.FontDescription.FontDescription.Proportion == rendering::PanoseProportion::MONO_SPACED
57 ? PITCH_FIXED : PITCH_VARIABLE);
58
59 maFont->SetLanguage( LanguageTag::convertToLanguageType( rFontRequest.Locale, false));
60
61 // adjust to stretched/shrunk font
62 if( ::rtl::math::approxEqual( rFontMatrix.m00, rFontMatrix.m11) )
63 return;
64
65 VclPtr<OutputDevice> pOutDev( mpRefDevice->getOutputDevice() );
66
67 if( !pOutDev )
68 return;
69
70 const bool bOldMapState( pOutDev->IsMapModeEnabled() );
71 pOutDev->EnableMapMode(false);
72
73 const Size aSize = pOutDev->GetFontMetric( *maFont ).GetFontSize();
74
75 const double fDividend( rFontMatrix.m10 + rFontMatrix.m11 );
76 double fStretch = rFontMatrix.m00 + rFontMatrix.m01;
77
78 if( !::basegfx::fTools::equalZero( fDividend) )
79 fStretch /= fDividend;
80
81 const tools::Long nNewWidth = ::basegfx::fround( aSize.Width() * fStretch );
82
83 maFont->SetAverageFontWidth( nNewWidth );
84
85 pOutDev->EnableMapMode(bOldMapState);
86 }
87
88 void CanvasFont::disposing(std::unique_lock<std::mutex>& rGuard)
89 {
90 rGuard.unlock();
91 {
92 SolarMutexGuard aGuard;
93 mpRefDevice.clear();
94 }
95 rGuard.lock();
96 }
97
98 uno::Reference< rendering::XTextLayout > SAL_CALL CanvasFont::createTextLayout( const rendering::StringContext& aText, sal_Int8 nDirection, sal_Int64 nRandomSeed )
99 {
100 SolarMutexGuard aGuard;
101
102 if( !mpRefDevice.is() )
103 return uno::Reference< rendering::XTextLayout >(); // we're disposed
104
105 return new TextLayout( aText,
106 nDirection,
107 nRandomSeed,
108 Reference( this ),
109 mpRefDevice );
110 }
111
112 rendering::FontRequest SAL_CALL CanvasFont::getFontRequest( )
113 {
114 SolarMutexGuard aGuard;
115
116 return maFontRequest;
117 }
118
119 rendering::FontMetrics SAL_CALL CanvasFont::getFontMetrics( )
120 {
121 // TODO(F1)
122 return rendering::FontMetrics();
123 }
124
125 uno::Sequence< double > SAL_CALL CanvasFont::getAvailableSizes( )
126 {
127 // TODO(F1)
128 return uno::Sequence< double >();
129 }
130
131 uno::Sequence< beans::PropertyValue > SAL_CALL CanvasFont::getExtraFontProperties( )
132 {
133 // TODO(F1)
134 return uno::Sequence< beans::PropertyValue >();
135 }
136
137 OUString SAL_CALL CanvasFont::getImplementationName()
138 {
139 return "CairoCanvas::CanvasFont";
140 }
141
142 sal_Bool SAL_CALL CanvasFont::supportsService( const OUString& ServiceName )
143 {
144 return cppu::supportsService( this, ServiceName );
145 }
146
147 uno::Sequence< OUString > SAL_CALL CanvasFont::getSupportedServiceNames()
148 {
149 return { "com.sun.star.rendering.CanvasFont" };
150 }
151
152 vcl::Font const & CanvasFont::getVCLFont() const
153 {
154 return *maFont;
155 }
156}
157
158/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static LanguageType convertToLanguageType(const css::lang::Locale &rLocale, bool bResolveSystem=true)
constexpr tools::Long Width() const
CanvasFont(const CanvasFont &)=delete
make noncopyable
B2IRange fround(const B2DRange &rRange)
class SAL_LOPLUGIN_ANNOTATE("crosscast") SurfaceProvider typedef ::rtl::Reference< SurfaceProvider > SurfaceProviderRef
Target interface for XCachedPrimitive implementations.
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Reference
FontWeight
long Long
unsigned char sal_Bool
signed char sal_Int8