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