xref: /aoo41x/main/canvas/source/vcl/canvasfont.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_canvas.hxx"
30 
31 #include <canvas/debug.hxx>
32 
33 #include <rtl/math.hxx>
34 #include <basegfx/numeric/ftools.hxx>
35 #include <i18npool/mslangid.hxx>
36 #include <vcl/metric.hxx>
37 
38 #include <com/sun/star/rendering/PanoseProportion.hpp>
39 
40 #include "canvasfont.hxx"
41 #include "textlayout.hxx"
42 
43 using namespace ::com::sun::star;
44 
45 
46 namespace vclcanvas
47 {
48     CanvasFont::CanvasFont( const rendering::FontRequest& 					rFontRequest,
49                             const uno::Sequence< beans::PropertyValue >&	,
50                             const geometry::Matrix2D& 						rFontMatrix,
51                             rendering::XGraphicDevice&                      rDevice,
52                             const OutDevProviderSharedPtr&                  rOutDevProvider ) :
53         CanvasFont_Base( m_aMutex ),
54         maFont( Font( rFontRequest.FontDescription.FamilyName,
55                       rFontRequest.FontDescription.StyleName,
56                       Size( 0, ::basegfx::fround(rFontRequest.CellSize) ) ) ),
57         maFontRequest( rFontRequest ),
58         mpRefDevice( &rDevice ),
59         mpOutDevProvider( rOutDevProvider )
60     {
61         maFont->SetAlign( ALIGN_BASELINE );
62         maFont->SetCharSet( (rFontRequest.FontDescription.IsSymbolFont==com::sun::star::util::TriState_YES) ? RTL_TEXTENCODING_SYMBOL : RTL_TEXTENCODING_UNICODE );
63         maFont->SetVertical( (rFontRequest.FontDescription.IsVertical==com::sun::star::util::TriState_YES) ? sal_True : sal_False );
64 
65         // TODO(F2): improve panose->vclenum conversion
66         maFont->SetWeight( static_cast<FontWeight>(rFontRequest.FontDescription.FontDescription.Weight) );
67         maFont->SetItalic( (rFontRequest.FontDescription.FontDescription.Letterform<=8) ? ITALIC_NONE : ITALIC_NORMAL );
68         maFont->SetPitch(
69                 rFontRequest.FontDescription.FontDescription.Proportion == rendering::PanoseProportion::MONO_SPACED
70                     ? PITCH_FIXED : PITCH_VARIABLE);
71 
72 		maFont->SetLanguage(MsLangId::convertLocaleToLanguage(rFontRequest.Locale));
73 
74         // adjust to stretched/shrinked font
75         if( !::rtl::math::approxEqual( rFontMatrix.m00, rFontMatrix.m11) )
76         {
77             OutputDevice& rOutDev( rOutDevProvider->getOutDev() );
78 
79             const bool bOldMapState( rOutDev.IsMapModeEnabled() );
80             rOutDev.EnableMapMode(sal_False);
81 
82             const Size aSize = rOutDev.GetFontMetric( *maFont ).GetSize();
83 
84             const double fDividend( rFontMatrix.m10 + rFontMatrix.m11 );
85             double fStretch = (rFontMatrix.m00 + rFontMatrix.m01);
86 
87             if( !::basegfx::fTools::equalZero( fDividend) )
88                 fStretch /= fDividend;
89 
90             const long nNewWidth = ::basegfx::fround( aSize.Width() * fStretch );
91 
92             maFont->SetWidth( nNewWidth );
93 
94             rOutDev.EnableMapMode(bOldMapState);
95         }
96     }
97 
98     void SAL_CALL CanvasFont::disposing()
99     {
100         tools::LocalGuard aGuard;
101 
102         mpOutDevProvider.reset();
103         mpRefDevice.clear();
104     }
105 
106     uno::Reference< rendering::XTextLayout > SAL_CALL  CanvasFont::createTextLayout( const rendering::StringContext& aText, sal_Int8 nDirection, sal_Int64 nRandomSeed ) throw (uno::RuntimeException)
107     {
108         tools::LocalGuard aGuard;
109 
110         if( !mpRefDevice.is() )
111             return uno::Reference< rendering::XTextLayout >(); // we're disposed
112 
113         return new TextLayout( aText,
114                                nDirection,
115                                nRandomSeed,
116                                Reference( this ),
117                                mpRefDevice,
118                                mpOutDevProvider);
119     }
120 
121     rendering::FontRequest SAL_CALL  CanvasFont::getFontRequest(  ) throw (uno::RuntimeException)
122     {
123         tools::LocalGuard aGuard;
124 
125         return maFontRequest;
126     }
127 
128     rendering::FontMetrics SAL_CALL  CanvasFont::getFontMetrics(  ) throw (uno::RuntimeException)
129     {
130         tools::LocalGuard aGuard;
131 
132         OutputDevice& rOutDev = mpOutDevProvider->getOutDev();
133         VirtualDevice aVDev( rOutDev );
134         aVDev.SetFont(getVCLFont());
135         const ::FontMetric& aMetric( aVDev.GetFontMetric() );
136 
137         return rendering::FontMetrics(
138             aMetric.GetAscent(),
139             aMetric.GetDescent(),
140             aMetric.GetIntLeading(),
141             aMetric.GetExtLeading(),
142             0,
143             aMetric.GetDescent() / 2.0,
144             aMetric.GetAscent() / 2.0);
145     }
146 
147     uno::Sequence< double > SAL_CALL  CanvasFont::getAvailableSizes(  ) throw (uno::RuntimeException)
148     {
149         tools::LocalGuard aGuard;
150 
151         // TODO(F1)
152         return uno::Sequence< double >();
153     }
154 
155     uno::Sequence< beans::PropertyValue > SAL_CALL  CanvasFont::getExtraFontProperties(  ) throw (uno::RuntimeException)
156     {
157         tools::LocalGuard aGuard;
158 
159         // TODO(F1)
160         return uno::Sequence< beans::PropertyValue >();
161     }
162 
163 #define IMPLEMENTATION_NAME "VCLCanvas::CanvasFont"
164 #define SERVICE_NAME "com.sun.star.rendering.CanvasFont"
165 
166     ::rtl::OUString SAL_CALL CanvasFont::getImplementationName() throw( uno::RuntimeException )
167     {
168         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
169     }
170 
171     sal_Bool SAL_CALL CanvasFont::supportsService( const ::rtl::OUString& ServiceName ) throw( uno::RuntimeException )
172     {
173         return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ) );
174     }
175 
176     uno::Sequence< ::rtl::OUString > SAL_CALL CanvasFont::getSupportedServiceNames()  throw( uno::RuntimeException )
177     {
178         uno::Sequence< ::rtl::OUString > aRet(1);
179         aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
180 
181         return aRet;
182     }
183 
184     ::Font CanvasFont::getVCLFont() const
185     {
186         return *maFont;
187     }
188 }
189