xref: /trunk/main/canvas/source/cairo/cairo_canvasfont.cxx (revision ffd38472365e95f6a578737bc9a5eb0fac624a86)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_canvas.hxx"
24 
25 #include <canvas/debug.hxx>
26 
27 #include <com/sun/star/rendering/PanoseProportion.hpp>
28 
29 #include <rtl/math.hxx>
30 #include <basegfx/numeric/ftools.hxx>
31 
32 #include <vcl/metric.hxx>
33 #include <i18npool/mslangid.hxx>
34 
35 #include "cairo_canvasfont.hxx"
36 #include "cairo_textlayout.hxx"
37 
38 using namespace ::com::sun::star;
39 
40 namespace cairocanvas
41 {
42     namespace
43     {
44         // Little helper to encapsulate locking into policy class
45         class LocalGuard
46         {
47         public:
48             LocalGuard() :
49                 aGuard( Application::GetSolarMutex() )
50             {
51             }
52 
53             // To be compatible with CanvasBase mutex concept
54             LocalGuard( const ::osl::Mutex& ) :
55                 aGuard( Application::GetSolarMutex() )
56             {
57             }
58 
59         private:
60             ::vos::OGuard aGuard;
61         };
62     }
63 
64     CanvasFont::CanvasFont( const rendering::FontRequest&                   rFontRequest,
65                             const uno::Sequence< beans::PropertyValue >&    /*rExtraFontProperties*/,
66                             const geometry::Matrix2D&                       rFontMatrix,
67                             const SurfaceProviderRef&                       rDevice ) :
68         CanvasFont_Base( m_aMutex ),
69         maFont( Font( rFontRequest.FontDescription.FamilyName,
70                       rFontRequest.FontDescription.StyleName,
71                       Size( 0, ::basegfx::fround(rFontRequest.CellSize) ) ) ),
72         maFontRequest( rFontRequest ),
73         mpRefDevice( rDevice )
74     {
75         maFont->SetAlign( ALIGN_BASELINE );
76         maFont->SetCharSet( (rFontRequest.FontDescription.IsSymbolFont==com::sun::star::util::TriState_YES) ? RTL_TEXTENCODING_SYMBOL : RTL_TEXTENCODING_UNICODE );
77         maFont->SetVertical( (rFontRequest.FontDescription.IsVertical==com::sun::star::util::TriState_YES) ? sal_True : sal_False );
78 
79         // TODO(F2): improve panose->vclenum conversion
80         maFont->SetWeight( static_cast<FontWeight>(rFontRequest.FontDescription.FontDescription.Weight) );
81         maFont->SetItalic( (rFontRequest.FontDescription.FontDescription.Letterform<=8) ? ITALIC_NONE : ITALIC_NORMAL );
82         maFont->SetPitch(
83                 rFontRequest.FontDescription.FontDescription.Proportion == rendering::PanoseProportion::MONO_SPACED
84                     ? PITCH_FIXED : PITCH_VARIABLE);
85 
86         maFont->SetLanguage(MsLangId::convertLocaleToLanguage(rFontRequest.Locale));
87 
88         // adjust to stretched/shrunk font
89         if( !::rtl::math::approxEqual( rFontMatrix.m00, rFontMatrix.m11) )
90         {
91             OutputDevice* pOutDev( mpRefDevice->getOutputDevice() );
92 
93             if( pOutDev )
94             {
95                 const bool bOldMapState( pOutDev->IsMapModeEnabled() );
96                 pOutDev->EnableMapMode(sal_False);
97 
98                 const Size aSize = pOutDev->GetFontMetric( *maFont ).GetSize();
99 
100                 const double fDividend( rFontMatrix.m10 + rFontMatrix.m11 );
101                 double fStretch = (rFontMatrix.m00 + rFontMatrix.m01);
102 
103                 if( !::basegfx::fTools::equalZero( fDividend) )
104                     fStretch /= fDividend;
105 
106                 const long nNewWidth = ::basegfx::fround( aSize.Width() * fStretch );
107 
108                 maFont->SetWidth( nNewWidth );
109 
110                 pOutDev->EnableMapMode(bOldMapState);
111             }
112         }
113     }
114 
115     void SAL_CALL CanvasFont::disposing()
116     {
117         LocalGuard aGuard;
118 
119         mpRefDevice.clear();
120     }
121 
122     uno::Reference< rendering::XTextLayout > SAL_CALL  CanvasFont::createTextLayout( const rendering::StringContext& aText, sal_Int8 nDirection, sal_Int64 nRandomSeed ) throw (uno::RuntimeException)
123     {
124         LocalGuard aGuard;
125 
126         if( !mpRefDevice.is() )
127             return uno::Reference< rendering::XTextLayout >(); // we're disposed
128 
129         return new TextLayout( aText,
130                                nDirection,
131                                nRandomSeed,
132                                Reference( this ),
133                                mpRefDevice );
134     }
135 
136     rendering::FontRequest SAL_CALL  CanvasFont::getFontRequest(  ) throw (uno::RuntimeException)
137     {
138         LocalGuard aGuard;
139 
140         return maFontRequest;
141     }
142 
143     rendering::FontMetrics SAL_CALL  CanvasFont::getFontMetrics(  ) throw (uno::RuntimeException)
144     {
145         LocalGuard aGuard;
146 
147         // TODO(F1)
148         return rendering::FontMetrics();
149     }
150 
151     uno::Sequence< double > SAL_CALL  CanvasFont::getAvailableSizes(  ) throw (uno::RuntimeException)
152     {
153         LocalGuard aGuard;
154 
155         // TODO(F1)
156         return uno::Sequence< double >();
157     }
158 
159     uno::Sequence< beans::PropertyValue > SAL_CALL  CanvasFont::getExtraFontProperties(  ) throw (uno::RuntimeException)
160     {
161         LocalGuard aGuard;
162 
163         // TODO(F1)
164         return uno::Sequence< beans::PropertyValue >();
165     }
166 
167 #define IMPLEMENTATION_NAME "CairoCanvas::CanvasFont"
168 #define SERVICE_NAME "com.sun.star.rendering.CanvasFont"
169 
170     ::rtl::OUString SAL_CALL CanvasFont::getImplementationName() throw( uno::RuntimeException )
171     {
172         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
173     }
174 
175     sal_Bool SAL_CALL CanvasFont::supportsService( const ::rtl::OUString& ServiceName ) throw( uno::RuntimeException )
176     {
177         return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ) );
178     }
179 
180     uno::Sequence< ::rtl::OUString > SAL_CALL CanvasFont::getSupportedServiceNames()  throw( uno::RuntimeException )
181     {
182         uno::Sequence< ::rtl::OUString > aRet(1);
183         aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
184 
185         return aRet;
186     }
187 
188     ::Font CanvasFont::getVCLFont() const
189     {
190         return *maFont;
191     }
192 }
193 
194 /* vim: set noet sw=4 ts=4: */
195