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