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
31
32#include "cairo_canvasfont.hxx"
33#include "cairo_textlayout.hxx"
34
35using namespace ::com::sun::star;
36
37namespace cairocanvas
38{
39
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 )),
49 mnEmphasisMark(0)
50 {
51 ::canvas::tools::extractExtraFontProperties(rExtraFontProperties, mnEmphasisMark);
52
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 );
56
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 );
60 maFont->SetPitch(
61 rFontRequest.FontDescription.FontDescription.Proportion == rendering::PanoseProportion::MONO_SPACED
62 ? PITCH_FIXED : PITCH_VARIABLE);
63
64 maFont->SetLanguage( LanguageTag::convertToLanguageType( rFontRequest.Locale, false));
65
66 // adjust to stretched/shrunk font
67 if( ::rtl::math::approxEqual( rFontMatrix.m00, rFontMatrix.m11) )
68 return;
69
70 VclPtr<OutputDevice> pOutDev( mpRefDevice->getOutputDevice() );
71
72 if( !pOutDev )
73 return;
74
75 const bool bOldMapState( pOutDev->IsMapModeEnabled() );
76 pOutDev->EnableMapMode(false);
77
78 const Size aSize = pOutDev->GetFontMetric( *maFont ).GetFontSize();
79
80 const double fDividend( rFontMatrix.m10 + rFontMatrix.m11 );
81 double fStretch = rFontMatrix.m00 + rFontMatrix.m01;
82
83 if( !::basegfx::fTools::equalZero( fDividend) )
84 fStretch /= fDividend;
85
86 const tools::Long nNewWidth = ::basegfx::fround( aSize.Width() * fStretch );
87
88 maFont->SetAverageFontWidth( nNewWidth );
89
90 pOutDev->EnableMapMode(bOldMapState);
91 }
92
93 void CanvasFont::disposing(std::unique_lock<std::mutex>& rGuard)
94 {
95 rGuard.unlock();
96 {
97 SolarMutexGuard aGuard;
98 mpRefDevice.clear();
99 }
100 rGuard.lock();
101 }
102
103 uno::Reference< rendering::XTextLayout > SAL_CALL CanvasFont::createTextLayout( const rendering::StringContext& aText, sal_Int8 nDirection, sal_Int64 nRandomSeed )
104 {
105 SolarMutexGuard aGuard;
106
107 if( !mpRefDevice.is() )
108 return uno::Reference< rendering::XTextLayout >(); // we're disposed
109
110 return new TextLayout( aText,
111 nDirection,
112 nRandomSeed,
113 Reference( this ),
114 mpRefDevice );
115 }
116
117 rendering::FontRequest SAL_CALL CanvasFont::getFontRequest( )
118 {
119 SolarMutexGuard aGuard;
120
121 return maFontRequest;
122 }
123
124 rendering::FontMetrics SAL_CALL CanvasFont::getFontMetrics( )
125 {
126 // TODO(F1)
127 return rendering::FontMetrics();
128 }
129
130 uno::Sequence< double > SAL_CALL CanvasFont::getAvailableSizes( )
131 {
132 // TODO(F1)
133 return uno::Sequence< double >();
134 }
135
136 uno::Sequence< beans::PropertyValue > SAL_CALL CanvasFont::getExtraFontProperties( )
137 {
138 // TODO(F1)
139 return uno::Sequence< beans::PropertyValue >();
140 }
141
142 OUString SAL_CALL CanvasFont::getImplementationName()
143 {
144 return "CairoCanvas::CanvasFont";
145 }
146
147 sal_Bool SAL_CALL CanvasFont::supportsService( const OUString& ServiceName )
148 {
149 return cppu::supportsService( this, ServiceName );
150 }
151
152 uno::Sequence< OUString > SAL_CALL CanvasFont::getSupportedServiceNames()
153 {
154 return { "com.sun.star.rendering.CanvasFont" };
155 }
156
157 vcl::Font const & CanvasFont::getVCLFont() const
158 {
159 return *maFont;
160 }
161}
162
163/* 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.
void extractExtraFontProperties(const uno::Sequence< beans::PropertyValue > &rExtraFontProperties, sal_uInt32 &rEmphasisMark)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Reference
FontWeight
long Long
unsigned char sal_Bool
signed char sal_Int8