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 <vcl/metric.hxx>
28 
29 #include "cairo_canvasfont.hxx"
30 #include "cairo_textlayout.hxx"
31 
32 using namespace ::com::sun::star;
33 
34 namespace cairocanvas
35 {
36 
37  CanvasFont::CanvasFont( const rendering::FontRequest& rFontRequest,
38  const uno::Sequence< beans::PropertyValue >& /*rExtraFontProperties*/,
39  const geometry::Matrix2D& rFontMatrix,
40  const 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( 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 long nNewWidth = ::basegfx::fround( aSize.Width() * fStretch );
82 
83  maFont->SetAverageFontWidth( nNewWidth );
84 
85  pOutDev->EnableMapMode(bOldMapState);
86  }
87 
88  void SAL_CALL CanvasFont::disposing()
89  {
90  SolarMutexGuard aGuard;
91 
92  mpRefDevice.clear();
93  }
94 
95  uno::Reference< rendering::XTextLayout > SAL_CALL CanvasFont::createTextLayout( const rendering::StringContext& aText, sal_Int8 nDirection, sal_Int64 nRandomSeed )
96  {
97  SolarMutexGuard aGuard;
98 
99  if( !mpRefDevice.is() )
100  return uno::Reference< rendering::XTextLayout >(); // we're disposed
101 
102  return new TextLayout( aText,
103  nDirection,
104  nRandomSeed,
105  Reference( this ),
106  mpRefDevice );
107  }
108 
109  rendering::FontRequest SAL_CALL CanvasFont::getFontRequest( )
110  {
111  SolarMutexGuard aGuard;
112 
113  return maFontRequest;
114  }
115 
116  rendering::FontMetrics SAL_CALL CanvasFont::getFontMetrics( )
117  {
118  // TODO(F1)
119  return rendering::FontMetrics();
120  }
121 
122  uno::Sequence< double > SAL_CALL CanvasFont::getAvailableSizes( )
123  {
124  // TODO(F1)
125  return uno::Sequence< double >();
126  }
127 
128  uno::Sequence< beans::PropertyValue > SAL_CALL CanvasFont::getExtraFontProperties( )
129  {
130  // TODO(F1)
131  return uno::Sequence< beans::PropertyValue >();
132  }
133 
135  {
136  return "CairoCanvas::CanvasFont";
137  }
138 
139  sal_Bool SAL_CALL CanvasFont::supportsService( const OUString& ServiceName )
140  {
141  return cppu::supportsService( this, ServiceName );
142  }
143 
144  uno::Sequence< OUString > SAL_CALL CanvasFont::getSupportedServiceNames()
145  {
146  return { "com.sun.star.rendering.CanvasFont" };
147  }
148 
150  {
151  return *maFont;
152  }
153 }
154 
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
SurfaceProviderRef mpRefDevice
osl::Mutex m_aMutex
signed char sal_Int8
static LanguageType convertToLanguageType(const css::lang::Locale &rLocale, bool bResolveSystem=true)
vcl::Font const & getVCLFont() const
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
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)
B2IRange fround(const B2DRange &rRange)
virtual css::uno::Sequence< double > SAL_CALL getAvailableSizes() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
static bool equalZero(const double &rfVal)
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