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