1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_canvas.hxx"
26 
27 #include <canvas/debug.hxx>
28 
29 #include <com/sun/star/rendering/PanoseProportion.hpp>
30 
31 #include <rtl/math.hxx>
32 #include <basegfx/numeric/ftools.hxx>
33 
34 #include <vcl/metric.hxx>
35 #include <i18npool/mslangid.hxx>
36 
37 #include "cairo_canvasfont.hxx"
38 #include "cairo_textlayout.hxx"
39 
40 using namespace ::com::sun::star;
41 
42 namespace cairocanvas
43 {
44     namespace
45     {
46         // Little helper to encapsulate locking into policy class
47         class LocalGuard
48         {
49         public:
LocalGuard()50             LocalGuard() :
51                 aGuard( Application::GetSolarMutex() )
52             {
53         	}
54 
55             /// To be compatible with CanvasBase mutex concept
LocalGuard(const::osl::Mutex &)56             LocalGuard( const ::osl::Mutex& ) :
57                 aGuard( Application::GetSolarMutex() )
58             {
59         	}
60 
61         private:
62             ::vos::OGuard aGuard;
63         };
64     }
65 
CanvasFont(const rendering::FontRequest & rFontRequest,const uno::Sequence<beans::PropertyValue> &,const geometry::Matrix2D & rFontMatrix,const SurfaceProviderRef & rDevice)66     CanvasFont::CanvasFont( const rendering::FontRequest& 					rFontRequest,
67                             const uno::Sequence< beans::PropertyValue >&	/*rExtraFontProperties*/,
68                             const geometry::Matrix2D& 						rFontMatrix,
69                             const SurfaceProviderRef&						rDevice ) :
70         CanvasFont_Base( m_aMutex ),
71         maFont( Font( rFontRequest.FontDescription.FamilyName,
72                       rFontRequest.FontDescription.StyleName,
73                       Size( 0, ::basegfx::fround(rFontRequest.CellSize) ) ) ),
74         maFontRequest( rFontRequest ),
75         mpRefDevice( rDevice )
76     {
77         maFont->SetAlign( ALIGN_BASELINE );
78         maFont->SetCharSet( (rFontRequest.FontDescription.IsSymbolFont==com::sun::star::util::TriState_YES) ? RTL_TEXTENCODING_SYMBOL : RTL_TEXTENCODING_UNICODE );
79         maFont->SetVertical( (rFontRequest.FontDescription.IsVertical==com::sun::star::util::TriState_YES) ? sal_True : sal_False );
80 
81         // TODO(F2): improve panose->vclenum conversion
82         maFont->SetWeight( static_cast<FontWeight>(rFontRequest.FontDescription.FontDescription.Weight) );
83         maFont->SetItalic( (rFontRequest.FontDescription.FontDescription.Letterform<=8) ? ITALIC_NONE : ITALIC_NORMAL );
84         maFont->SetPitch(
85                 rFontRequest.FontDescription.FontDescription.Proportion == rendering::PanoseProportion::MONO_SPACED
86                     ? PITCH_FIXED : PITCH_VARIABLE);
87 
88         maFont->SetLanguage(MsLangId::convertLocaleToLanguage(rFontRequest.Locale));
89 
90         // adjust to stretched/shrunk font
91         if( !::rtl::math::approxEqual( rFontMatrix.m00, rFontMatrix.m11) )
92         {
93             OutputDevice* pOutDev( mpRefDevice->getOutputDevice() );
94 
95             if( pOutDev )
96             {
97                 const bool bOldMapState( pOutDev->IsMapModeEnabled() );
98                 pOutDev->EnableMapMode(sal_False);
99 
100                 const Size aSize = pOutDev->GetFontMetric( *maFont ).GetSize();
101 
102                 const double fDividend( rFontMatrix.m10 + rFontMatrix.m11 );
103                 double fStretch = (rFontMatrix.m00 + rFontMatrix.m01);
104 
105                 if( !::basegfx::fTools::equalZero( fDividend) )
106                     fStretch /= fDividend;
107 
108                 const long nNewWidth = ::basegfx::fround( aSize.Width() * fStretch );
109 
110                 maFont->SetWidth( nNewWidth );
111 
112                 pOutDev->EnableMapMode(bOldMapState);
113             }
114         }
115     }
116 
disposing()117     void SAL_CALL CanvasFont::disposing()
118     {
119         LocalGuard aGuard;
120 
121         mpRefDevice.clear();
122     }
123 
createTextLayout(const rendering::StringContext & aText,sal_Int8 nDirection,sal_Int64 nRandomSeed)124     uno::Reference< rendering::XTextLayout > SAL_CALL  CanvasFont::createTextLayout( const rendering::StringContext& aText, sal_Int8 nDirection, sal_Int64 nRandomSeed ) throw (uno::RuntimeException)
125     {
126         LocalGuard aGuard;
127 
128         if( !mpRefDevice.is() )
129             return uno::Reference< rendering::XTextLayout >(); // we're disposed
130 
131         return new TextLayout( aText,
132                                nDirection,
133                                nRandomSeed,
134                                Reference( this ),
135 							   mpRefDevice );
136     }
137 
getFontRequest()138     rendering::FontRequest SAL_CALL  CanvasFont::getFontRequest(  ) throw (uno::RuntimeException)
139     {
140         LocalGuard aGuard;
141 
142         return maFontRequest;
143     }
144 
getFontMetrics()145     rendering::FontMetrics SAL_CALL  CanvasFont::getFontMetrics(  ) throw (uno::RuntimeException)
146     {
147         LocalGuard aGuard;
148 
149         // TODO(F1)
150         return rendering::FontMetrics();
151     }
152 
getAvailableSizes()153     uno::Sequence< double > SAL_CALL  CanvasFont::getAvailableSizes(  ) throw (uno::RuntimeException)
154     {
155         LocalGuard aGuard;
156 
157         // TODO(F1)
158         return uno::Sequence< double >();
159     }
160 
getExtraFontProperties()161     uno::Sequence< beans::PropertyValue > SAL_CALL  CanvasFont::getExtraFontProperties(  ) throw (uno::RuntimeException)
162     {
163         LocalGuard aGuard;
164 
165         // TODO(F1)
166         return uno::Sequence< beans::PropertyValue >();
167     }
168 
169 #define IMPLEMENTATION_NAME "CairoCanvas::CanvasFont"
170 #define SERVICE_NAME "com.sun.star.rendering.CanvasFont"
171 
getImplementationName()172     ::rtl::OUString SAL_CALL CanvasFont::getImplementationName() throw( uno::RuntimeException )
173     {
174         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
175     }
176 
supportsService(const::rtl::OUString & ServiceName)177     sal_Bool SAL_CALL CanvasFont::supportsService( const ::rtl::OUString& ServiceName ) throw( uno::RuntimeException )
178     {
179         return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ) );
180     }
181 
getSupportedServiceNames()182     uno::Sequence< ::rtl::OUString > SAL_CALL CanvasFont::getSupportedServiceNames()  throw( uno::RuntimeException )
183     {
184         uno::Sequence< ::rtl::OUString > aRet(1);
185         aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
186 
187         return aRet;
188     }
189 
getVCLFont() const190     ::Font CanvasFont::getVCLFont() const
191     {
192         return *maFont;
193     }
194 }
195