xref: /trunk/main/canvas/source/vcl/canvasfont.cxx (revision 07a3d7f1)
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 <rtl/math.hxx>
30 #include <basegfx/numeric/ftools.hxx>
31 #include <i18npool/mslangid.hxx>
32 #include <vcl/metric.hxx>
33 
34 #include <com/sun/star/rendering/PanoseProportion.hpp>
35 
36 #include "canvasfont.hxx"
37 #include "textlayout.hxx"
38 
39 using namespace ::com::sun::star;
40 
41 
42 namespace vclcanvas
43 {
CanvasFont(const rendering::FontRequest & rFontRequest,const uno::Sequence<beans::PropertyValue> &,const geometry::Matrix2D & rFontMatrix,rendering::XGraphicDevice & rDevice,const OutDevProviderSharedPtr & rOutDevProvider)44     CanvasFont::CanvasFont( const rendering::FontRequest& 					rFontRequest,
45                             const uno::Sequence< beans::PropertyValue >&	,
46                             const geometry::Matrix2D& 						rFontMatrix,
47                             rendering::XGraphicDevice&                      rDevice,
48                             const OutDevProviderSharedPtr&                  rOutDevProvider ) :
49         CanvasFont_Base( m_aMutex ),
50         maFont( Font( rFontRequest.FontDescription.FamilyName,
51                       rFontRequest.FontDescription.StyleName,
52                       Size( 0, ::basegfx::fround(rFontRequest.CellSize) ) ) ),
53         maFontRequest( rFontRequest ),
54         mpRefDevice( &rDevice ),
55         mpOutDevProvider( rOutDevProvider )
56     {
57         maFont->SetAlign( ALIGN_BASELINE );
58         maFont->SetCharSet( (rFontRequest.FontDescription.IsSymbolFont==com::sun::star::util::TriState_YES) ? RTL_TEXTENCODING_SYMBOL : RTL_TEXTENCODING_UNICODE );
59         maFont->SetVertical( (rFontRequest.FontDescription.IsVertical==com::sun::star::util::TriState_YES) ? sal_True : sal_False );
60 
61         // TODO(F2): improve panose->vclenum conversion
62         maFont->SetWeight( static_cast<FontWeight>(rFontRequest.FontDescription.FontDescription.Weight) );
63         maFont->SetItalic( (rFontRequest.FontDescription.FontDescription.Letterform<=8) ? ITALIC_NONE : ITALIC_NORMAL );
64         maFont->SetPitch(
65                 rFontRequest.FontDescription.FontDescription.Proportion == rendering::PanoseProportion::MONO_SPACED
66                     ? PITCH_FIXED : PITCH_VARIABLE);
67 
68 		maFont->SetLanguage(MsLangId::convertLocaleToLanguage(rFontRequest.Locale));
69 
70         // adjust to stretched/shrunk font
71         if( !::rtl::math::approxEqual( rFontMatrix.m00, rFontMatrix.m11) )
72         {
73             OutputDevice& rOutDev( rOutDevProvider->getOutDev() );
74 
75             const bool bOldMapState( rOutDev.IsMapModeEnabled() );
76             rOutDev.EnableMapMode(sal_False);
77 
78             const Size aSize = rOutDev.GetFontMetric( *maFont ).GetSize();
79 
80             const double fDividend( rFontMatrix.m10 + rFontMatrix.m11 );
81             double fStretch = (rFontMatrix.m00 + rFontMatrix.m01);
82 
83             if( !::basegfx::fTools::equalZero( fDividend) )
84                 fStretch /= fDividend;
85 
86             const long nNewWidth = ::basegfx::fround( aSize.Width() * fStretch );
87 
88             maFont->SetWidth( nNewWidth );
89 
90             rOutDev.EnableMapMode(bOldMapState);
91         }
92     }
93 
disposing()94     void SAL_CALL CanvasFont::disposing()
95     {
96         tools::LocalGuard aGuard;
97 
98         mpOutDevProvider.reset();
99         mpRefDevice.clear();
100     }
101 
createTextLayout(const rendering::StringContext & aText,sal_Int8 nDirection,sal_Int64 nRandomSeed)102     uno::Reference< rendering::XTextLayout > SAL_CALL  CanvasFont::createTextLayout( const rendering::StringContext& aText, sal_Int8 nDirection, sal_Int64 nRandomSeed ) throw (uno::RuntimeException)
103     {
104         tools::LocalGuard aGuard;
105 
106         if( !mpRefDevice.is() )
107             return uno::Reference< rendering::XTextLayout >(); // we're disposed
108 
109         return new TextLayout( aText,
110                                nDirection,
111                                nRandomSeed,
112                                Reference( this ),
113                                mpRefDevice,
114                                mpOutDevProvider);
115     }
116 
getFontRequest()117     rendering::FontRequest SAL_CALL  CanvasFont::getFontRequest(  ) throw (uno::RuntimeException)
118     {
119         tools::LocalGuard aGuard;
120 
121         return maFontRequest;
122     }
123 
getFontMetrics()124     rendering::FontMetrics SAL_CALL  CanvasFont::getFontMetrics(  ) throw (uno::RuntimeException)
125     {
126         tools::LocalGuard aGuard;
127 
128         OutputDevice& rOutDev = mpOutDevProvider->getOutDev();
129         VirtualDevice aVDev( rOutDev );
130         aVDev.SetFont(getVCLFont());
131         const ::FontMetric& aMetric( aVDev.GetFontMetric() );
132 
133         return rendering::FontMetrics(
134             aMetric.GetAscent(),
135             aMetric.GetDescent(),
136             aMetric.GetIntLeading(),
137             aMetric.GetExtLeading(),
138             0,
139             aMetric.GetDescent() / 2.0,
140             aMetric.GetAscent() / 2.0);
141     }
142 
getAvailableSizes()143     uno::Sequence< double > SAL_CALL  CanvasFont::getAvailableSizes(  ) throw (uno::RuntimeException)
144     {
145         tools::LocalGuard aGuard;
146 
147         // TODO(F1)
148         return uno::Sequence< double >();
149     }
150 
getExtraFontProperties()151     uno::Sequence< beans::PropertyValue > SAL_CALL  CanvasFont::getExtraFontProperties(  ) throw (uno::RuntimeException)
152     {
153         tools::LocalGuard aGuard;
154 
155         // TODO(F1)
156         return uno::Sequence< beans::PropertyValue >();
157     }
158 
159 #define IMPLEMENTATION_NAME "VCLCanvas::CanvasFont"
160 #define SERVICE_NAME "com.sun.star.rendering.CanvasFont"
161 
getImplementationName()162     ::rtl::OUString SAL_CALL CanvasFont::getImplementationName() throw( uno::RuntimeException )
163     {
164         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
165     }
166 
supportsService(const::rtl::OUString & ServiceName)167     sal_Bool SAL_CALL CanvasFont::supportsService( const ::rtl::OUString& ServiceName ) throw( uno::RuntimeException )
168     {
169         return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ) );
170     }
171 
getSupportedServiceNames()172     uno::Sequence< ::rtl::OUString > SAL_CALL CanvasFont::getSupportedServiceNames()  throw( uno::RuntimeException )
173     {
174         uno::Sequence< ::rtl::OUString > aRet(1);
175         aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
176 
177         return aRet;
178     }
179 
getVCLFont() const180     ::Font CanvasFont::getVCLFont() const
181     {
182         return *maFont;
183     }
184 }
185