xref: /aoo42x/main/vcl/source/gdi/textlayout.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_vcl.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "vcl/ctrl.hxx"
32*cdf0e10cSrcweir #include "vcl/outdev.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include "outfont.hxx"
35*cdf0e10cSrcweir #include "textlayout.hxx"
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <com/sun/star/i18n/ScriptDirection.hpp>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
42*cdf0e10cSrcweir #include <rtl/strbuf.hxx>
43*cdf0e10cSrcweir #endif
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir //........................................................................
46*cdf0e10cSrcweir namespace vcl
47*cdf0e10cSrcweir {
48*cdf0e10cSrcweir //........................................................................
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
51*cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
52*cdf0e10cSrcweir     namespace ScriptDirection = ::com::sun::star::i18n::ScriptDirection;
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir 	//====================================================================
55*cdf0e10cSrcweir 	//= DefaultTextLayout
56*cdf0e10cSrcweir 	//====================================================================
57*cdf0e10cSrcweir 	//--------------------------------------------------------------------
58*cdf0e10cSrcweir     DefaultTextLayout::~DefaultTextLayout()
59*cdf0e10cSrcweir     {
60*cdf0e10cSrcweir     }
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 	//--------------------------------------------------------------------
63*cdf0e10cSrcweir     long DefaultTextLayout::GetTextWidth( const XubString& _rText, xub_StrLen _nStartIndex, xub_StrLen _nLength ) const
64*cdf0e10cSrcweir     {
65*cdf0e10cSrcweir         return m_rTargetDevice.GetTextWidth( _rText, _nStartIndex, _nLength );
66*cdf0e10cSrcweir     }
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 	//--------------------------------------------------------------------
69*cdf0e10cSrcweir     void DefaultTextLayout::DrawText( const Point& _rStartPoint, const XubString& _rText, xub_StrLen _nStartIndex,
70*cdf0e10cSrcweir         xub_StrLen _nLength, MetricVector* _pVector, String* _pDisplayText )
71*cdf0e10cSrcweir     {
72*cdf0e10cSrcweir         m_rTargetDevice.DrawText( _rStartPoint, _rText, _nStartIndex, _nLength, _pVector, _pDisplayText );
73*cdf0e10cSrcweir     }
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 	//--------------------------------------------------------------------
76*cdf0e10cSrcweir     bool DefaultTextLayout::GetCaretPositions( const XubString& _rText, sal_Int32* _pCaretXArray,
77*cdf0e10cSrcweir         xub_StrLen _nStartIndex, xub_StrLen _nLength ) const
78*cdf0e10cSrcweir     {
79*cdf0e10cSrcweir         return m_rTargetDevice.GetCaretPositions( _rText, _pCaretXArray, _nStartIndex, _nLength );
80*cdf0e10cSrcweir     }
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 	//--------------------------------------------------------------------
83*cdf0e10cSrcweir     xub_StrLen DefaultTextLayout::GetTextBreak( const XubString& _rText, long _nMaxTextWidth, xub_StrLen _nStartIndex, xub_StrLen _nLength ) const
84*cdf0e10cSrcweir     {
85*cdf0e10cSrcweir         return m_rTargetDevice.GetTextBreak( _rText, _nMaxTextWidth, _nStartIndex, _nLength );
86*cdf0e10cSrcweir     }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 	//--------------------------------------------------------------------
89*cdf0e10cSrcweir     bool DefaultTextLayout::DecomposeTextRectAction() const
90*cdf0e10cSrcweir     {
91*cdf0e10cSrcweir         return false;
92*cdf0e10cSrcweir     }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 	//====================================================================
95*cdf0e10cSrcweir 	//= ReferenceDeviceTextLayout
96*cdf0e10cSrcweir 	//====================================================================
97*cdf0e10cSrcweir     class ReferenceDeviceTextLayout : public ITextLayout
98*cdf0e10cSrcweir     {
99*cdf0e10cSrcweir     public:
100*cdf0e10cSrcweir         ReferenceDeviceTextLayout( const Control& _rControl, OutputDevice& _rTargetDevice, OutputDevice& _rReferenceDevice );
101*cdf0e10cSrcweir         virtual ~ReferenceDeviceTextLayout();
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir         // ITextLayout
104*cdf0e10cSrcweir         virtual long        GetTextWidth( const XubString& rStr, xub_StrLen nIndex, xub_StrLen nLen ) const;
105*cdf0e10cSrcweir         virtual void        DrawText( const Point& _rStartPoint, const XubString& _rText, xub_StrLen _nStartIndex, xub_StrLen _nLength, MetricVector* _pVector, String* _pDisplayText );
106*cdf0e10cSrcweir         virtual bool        GetCaretPositions( const XubString& _rText, sal_Int32* _pCaretXArray, xub_StrLen _nStartIndex, xub_StrLen _nLength ) const;
107*cdf0e10cSrcweir         virtual xub_StrLen  GetTextBreak( const XubString& _rText, long _nMaxTextWidth, xub_StrLen _nStartIndex, xub_StrLen _nLength ) const;
108*cdf0e10cSrcweir         virtual bool        DecomposeTextRectAction() const;
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir     public:
111*cdf0e10cSrcweir         // equivalents to the respective OutputDevice methods, which take the reference device into account
112*cdf0e10cSrcweir         long        GetTextArray( const XubString& _rText, sal_Int32* _pDXAry, xub_StrLen _nStartIndex, xub_StrLen _nLength ) const;
113*cdf0e10cSrcweir         Rectangle   DrawText( const Rectangle& _rRect, const XubString& _rText, sal_uInt16 _nStyle, MetricVector* _pVector, String* _pDisplayText );
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir     protected:
116*cdf0e10cSrcweir         void onBeginDrawText()
117*cdf0e10cSrcweir         {
118*cdf0e10cSrcweir             m_aCompleteTextRect.SetEmpty();
119*cdf0e10cSrcweir         }
120*cdf0e10cSrcweir         Rectangle onEndDrawText()
121*cdf0e10cSrcweir         {
122*cdf0e10cSrcweir             return m_aCompleteTextRect;
123*cdf0e10cSrcweir         }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     private:
126*cdf0e10cSrcweir         OutputDevice&   m_rTargetDevice;
127*cdf0e10cSrcweir         OutputDevice&   m_rReferenceDevice;
128*cdf0e10cSrcweir         Font            m_aUnzoomedPointFont;
129*cdf0e10cSrcweir         const Fraction  m_aZoom;
130*cdf0e10cSrcweir         const bool      m_bRTLEnabled;
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir         Rectangle       m_aCompleteTextRect;
133*cdf0e10cSrcweir     };
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir 	//====================================================================
136*cdf0e10cSrcweir 	//= ControlTextRenderer
137*cdf0e10cSrcweir 	//====================================================================
138*cdf0e10cSrcweir     ReferenceDeviceTextLayout::ReferenceDeviceTextLayout( const Control& _rControl, OutputDevice& _rTargetDevice,
139*cdf0e10cSrcweir         OutputDevice& _rReferenceDevice )
140*cdf0e10cSrcweir         :m_rTargetDevice( _rTargetDevice )
141*cdf0e10cSrcweir         ,m_rReferenceDevice( _rReferenceDevice )
142*cdf0e10cSrcweir         ,m_aUnzoomedPointFont( _rControl.GetUnzoomedControlPointFont() )
143*cdf0e10cSrcweir         ,m_aZoom( _rControl.GetZoom() )
144*cdf0e10cSrcweir         ,m_bRTLEnabled( _rControl.IsRTLEnabled() )
145*cdf0e10cSrcweir     {
146*cdf0e10cSrcweir         m_rTargetDevice.Push( PUSH_MAPMODE | PUSH_FONT | PUSH_TEXTLAYOUTMODE );
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir         MapMode aTargetMapMode( m_rTargetDevice.GetMapMode() );
149*cdf0e10cSrcweir         OSL_ENSURE( aTargetMapMode.GetOrigin() == Point(), "ReferenceDeviceTextLayout::ReferenceDeviceTextLayout: uhm, the code below won't work here ..." );
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir         // normally, controls simulate "zoom" by "zooming" the font. This is responsible for (part of) the discrepancies
152*cdf0e10cSrcweir         // between text in Writer and text in controls in Writer, though both have the same font.
153*cdf0e10cSrcweir         // So, if we have a zoom set at the control, then we do not scale the font, but instead modify the map mode
154*cdf0e10cSrcweir         // to accomodate for the zoom.
155*cdf0e10cSrcweir         aTargetMapMode.SetScaleX( m_aZoom );    // TODO: shouldn't this be "current_scale * zoom"?
156*cdf0e10cSrcweir         aTargetMapMode.SetScaleY( m_aZoom );
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir         // also, use a higher-resolution map unit than "pixels", which should save us some rounding errors when
159*cdf0e10cSrcweir         // translating coordinates between the reference device and the target device.
160*cdf0e10cSrcweir         OSL_ENSURE( aTargetMapMode.GetMapUnit() == MAP_PIXEL,
161*cdf0e10cSrcweir             "ReferenceDeviceTextLayout::ReferenceDeviceTextLayout: this class is not expected to work with such target devices!" );
162*cdf0e10cSrcweir             // we *could* adjust all the code in this class to handle this case, but at the moment, it's not necessary
163*cdf0e10cSrcweir         const MapUnit eTargetMapUnit = m_rReferenceDevice.GetMapMode().GetMapUnit();
164*cdf0e10cSrcweir         aTargetMapMode.SetMapUnit( eTargetMapUnit );
165*cdf0e10cSrcweir         OSL_ENSURE( aTargetMapMode.GetMapUnit() != MAP_PIXEL,
166*cdf0e10cSrcweir             "ReferenceDeviceTextLayout::ReferenceDeviceTextLayout: a reference device which has map mode PIXEL?!" );
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir         m_rTargetDevice.SetMapMode( aTargetMapMode );
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir         // now that the Zoom is part of the map mode, reset the target device's font to the "unzoomed" version
171*cdf0e10cSrcweir         Font aDrawFont( m_aUnzoomedPointFont );
172*cdf0e10cSrcweir         aDrawFont.SetSize( m_rTargetDevice.LogicToLogic( aDrawFont.GetSize(), MAP_POINT, eTargetMapUnit ) );
173*cdf0e10cSrcweir         _rTargetDevice.SetFont( aDrawFont );
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir         // transfer font to the reference device
176*cdf0e10cSrcweir         m_rReferenceDevice.Push( PUSH_FONT | PUSH_TEXTLAYOUTMODE );
177*cdf0e10cSrcweir         Font aRefFont( m_aUnzoomedPointFont );
178*cdf0e10cSrcweir         aRefFont.SetSize( OutputDevice::LogicToLogic(
179*cdf0e10cSrcweir             aRefFont.GetSize(), MAP_POINT, m_rReferenceDevice.GetMapMode().GetMapUnit() ) );
180*cdf0e10cSrcweir         m_rReferenceDevice.SetFont( aRefFont );
181*cdf0e10cSrcweir     }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 	//--------------------------------------------------------------------
184*cdf0e10cSrcweir     ReferenceDeviceTextLayout::~ReferenceDeviceTextLayout()
185*cdf0e10cSrcweir     {
186*cdf0e10cSrcweir         m_rReferenceDevice.Pop();
187*cdf0e10cSrcweir         m_rTargetDevice.Pop();
188*cdf0e10cSrcweir     }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 	//--------------------------------------------------------------------
191*cdf0e10cSrcweir     namespace
192*cdf0e10cSrcweir     {
193*cdf0e10cSrcweir 	    //................................................................
194*cdf0e10cSrcweir         bool lcl_normalizeLength( const XubString& _rText, const xub_StrLen _nStartIndex, xub_StrLen& _io_nLength )
195*cdf0e10cSrcweir         {
196*cdf0e10cSrcweir             xub_StrLen nTextLength = _rText.Len();
197*cdf0e10cSrcweir             if ( _nStartIndex > nTextLength )
198*cdf0e10cSrcweir                 return false;
199*cdf0e10cSrcweir             if ( _nStartIndex + _io_nLength > nTextLength )
200*cdf0e10cSrcweir                 _io_nLength = nTextLength - _nStartIndex;
201*cdf0e10cSrcweir             return true;
202*cdf0e10cSrcweir         }
203*cdf0e10cSrcweir     }
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir 	//--------------------------------------------------------------------
206*cdf0e10cSrcweir     long ReferenceDeviceTextLayout::GetTextArray( const XubString& _rText, sal_Int32* _pDXAry, xub_StrLen _nStartIndex,
207*cdf0e10cSrcweir         xub_StrLen _nLength ) const
208*cdf0e10cSrcweir     {
209*cdf0e10cSrcweir         if ( !lcl_normalizeLength( _rText, _nStartIndex, _nLength ) )
210*cdf0e10cSrcweir             return 0;
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir         // retrieve the character widths from the reference device
213*cdf0e10cSrcweir         long nTextWidth = m_rReferenceDevice.GetTextArray( _rText, _pDXAry, _nStartIndex, _nLength );
214*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
215*cdf0e10cSrcweir         if ( _pDXAry )
216*cdf0e10cSrcweir         {
217*cdf0e10cSrcweir             ::rtl::OStringBuffer aTrace;
218*cdf0e10cSrcweir             aTrace.append( "ReferenceDeviceTextLayout::GetTextArray( " );
219*cdf0e10cSrcweir             aTrace.append( ::rtl::OUStringToOString( _rText, RTL_TEXTENCODING_UTF8 ) );
220*cdf0e10cSrcweir             aTrace.append( " ): " );
221*cdf0e10cSrcweir             aTrace.append( nTextWidth );
222*cdf0e10cSrcweir             aTrace.append( " = ( " );
223*cdf0e10cSrcweir             for ( size_t i=0; i<_nLength; )
224*cdf0e10cSrcweir             {
225*cdf0e10cSrcweir                 aTrace.append( _pDXAry[i] );
226*cdf0e10cSrcweir                 if ( ++i < _nLength )
227*cdf0e10cSrcweir                     aTrace.append( ", " );
228*cdf0e10cSrcweir             }
229*cdf0e10cSrcweir             aTrace.append( ")" );
230*cdf0e10cSrcweir             OSL_TRACE( aTrace.makeStringAndClear().getStr() );
231*cdf0e10cSrcweir         }
232*cdf0e10cSrcweir #endif
233*cdf0e10cSrcweir         return nTextWidth;
234*cdf0e10cSrcweir     }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir 	//--------------------------------------------------------------------
237*cdf0e10cSrcweir     long ReferenceDeviceTextLayout::GetTextWidth( const XubString& _rText, xub_StrLen _nStartIndex, xub_StrLen _nLength ) const
238*cdf0e10cSrcweir     {
239*cdf0e10cSrcweir         return GetTextArray( _rText, NULL, _nStartIndex, _nLength );
240*cdf0e10cSrcweir     }
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir 	//--------------------------------------------------------------------
243*cdf0e10cSrcweir     void ReferenceDeviceTextLayout::DrawText( const Point& _rStartPoint, const XubString& _rText, xub_StrLen _nStartIndex, xub_StrLen _nLength, MetricVector* _pVector, String* _pDisplayText )
244*cdf0e10cSrcweir     {
245*cdf0e10cSrcweir         if ( !lcl_normalizeLength( _rText, _nStartIndex, _nLength ) )
246*cdf0e10cSrcweir             return;
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir         if ( _pVector && _pDisplayText )
249*cdf0e10cSrcweir         {
250*cdf0e10cSrcweir             MetricVector aGlyphBounds;
251*cdf0e10cSrcweir             m_rReferenceDevice.GetGlyphBoundRects( _rStartPoint, _rText, _nStartIndex, _nLength, _nStartIndex, aGlyphBounds );
252*cdf0e10cSrcweir             ::std::copy(
253*cdf0e10cSrcweir                 aGlyphBounds.begin(), aGlyphBounds.end(),
254*cdf0e10cSrcweir                 ::std::insert_iterator< MetricVector > ( *_pVector, _pVector->end() ) );
255*cdf0e10cSrcweir             _pDisplayText->Append( _rText.Copy( _nStartIndex, _nLength ) );
256*cdf0e10cSrcweir             return;
257*cdf0e10cSrcweir         }
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir         sal_Int32* pCharWidths = new sal_Int32[ _nLength ];
260*cdf0e10cSrcweir         long nTextWidth = GetTextArray( _rText, pCharWidths, _nStartIndex, _nLength );
261*cdf0e10cSrcweir         m_rTargetDevice.DrawTextArray( _rStartPoint, _rText, pCharWidths, _nStartIndex, _nLength );
262*cdf0e10cSrcweir         delete[] pCharWidths;
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir         m_aCompleteTextRect.Union( Rectangle( _rStartPoint, Size( nTextWidth, m_rTargetDevice.GetTextHeight() ) ) );
265*cdf0e10cSrcweir     }
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir 	//--------------------------------------------------------------------
268*cdf0e10cSrcweir     bool ReferenceDeviceTextLayout::GetCaretPositions( const XubString& _rText, sal_Int32* _pCaretXArray,
269*cdf0e10cSrcweir         xub_StrLen _nStartIndex, xub_StrLen _nLength ) const
270*cdf0e10cSrcweir     {
271*cdf0e10cSrcweir         if ( !lcl_normalizeLength( _rText, _nStartIndex, _nLength ) )
272*cdf0e10cSrcweir             return false;
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir         // retrieve the caret positions from the reference device
275*cdf0e10cSrcweir         if ( !m_rReferenceDevice.GetCaretPositions( _rText, _pCaretXArray, _nStartIndex, _nLength ) )
276*cdf0e10cSrcweir             return false;
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir         return true;
279*cdf0e10cSrcweir     }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir 	//--------------------------------------------------------------------
282*cdf0e10cSrcweir     xub_StrLen ReferenceDeviceTextLayout::GetTextBreak( const XubString& _rText, long _nMaxTextWidth, xub_StrLen _nStartIndex, xub_StrLen _nLength ) const
283*cdf0e10cSrcweir     {
284*cdf0e10cSrcweir         if ( !lcl_normalizeLength( _rText, _nStartIndex, _nLength ) )
285*cdf0e10cSrcweir             return 0;
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir         return m_rReferenceDevice.GetTextBreak( _rText, _nMaxTextWidth, _nStartIndex, _nLength );
288*cdf0e10cSrcweir     }
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir 	//--------------------------------------------------------------------
291*cdf0e10cSrcweir     bool ReferenceDeviceTextLayout::DecomposeTextRectAction() const
292*cdf0e10cSrcweir     {
293*cdf0e10cSrcweir         return true;
294*cdf0e10cSrcweir     }
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir 	//--------------------------------------------------------------------
297*cdf0e10cSrcweir     namespace
298*cdf0e10cSrcweir     {
299*cdf0e10cSrcweir         long zoomBy( long _value, const Fraction& _zoom )
300*cdf0e10cSrcweir         {
301*cdf0e10cSrcweir             double n = (double)_value;
302*cdf0e10cSrcweir             n *= (double)_zoom.GetNumerator();
303*cdf0e10cSrcweir             n /= (double)_zoom.GetDenominator();
304*cdf0e10cSrcweir             return (long)::rtl::math::round( n );
305*cdf0e10cSrcweir         }
306*cdf0e10cSrcweir         long unzoomBy( long _value, const Fraction& _zoom )
307*cdf0e10cSrcweir         {
308*cdf0e10cSrcweir             return zoomBy( _value, Fraction( _zoom.GetDenominator(), _zoom.GetNumerator() ) );
309*cdf0e10cSrcweir         }
310*cdf0e10cSrcweir     }
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir 	//--------------------------------------------------------------------
313*cdf0e10cSrcweir     Rectangle ReferenceDeviceTextLayout::DrawText( const Rectangle& _rRect, const XubString& _rText, sal_uInt16 _nStyle, MetricVector* _pVector, String* _pDisplayText )
314*cdf0e10cSrcweir     {
315*cdf0e10cSrcweir         if ( !_rText.Len() )
316*cdf0e10cSrcweir             return Rectangle();
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir         // determine text layout mode from the RTL-ness of the control whose text we render
319*cdf0e10cSrcweir         sal_uLong nTextLayoutMode = m_bRTLEnabled ? TEXT_LAYOUT_BIDI_RTL : TEXT_LAYOUT_BIDI_LTR;
320*cdf0e10cSrcweir         m_rReferenceDevice.SetLayoutMode( nTextLayoutMode );
321*cdf0e10cSrcweir         m_rTargetDevice.SetLayoutMode( nTextLayoutMode | TEXT_LAYOUT_TEXTORIGIN_LEFT );
322*cdf0e10cSrcweir             // TEXT_LAYOUT_TEXTORIGIN_LEFT is because when we do actually draw the text (in DrawText( Point, ... )), then
323*cdf0e10cSrcweir             // our caller gives us the left border of the draw position, regardless of script type, text layout,
324*cdf0e10cSrcweir             // and the like
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir         // in our ctor, we set the map mode of the target device from pixel to twip, but our caller doesn't know this,
327*cdf0e10cSrcweir         // but passed pixel coordinates. So, adjust the rect.
328*cdf0e10cSrcweir         Rectangle aRect( m_rTargetDevice.PixelToLogic( _rRect ) );
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir         onBeginDrawText();
331*cdf0e10cSrcweir         m_rTargetDevice.DrawText( aRect, _rText, _nStyle, _pVector, _pDisplayText, this );
332*cdf0e10cSrcweir         Rectangle aTextRect = onEndDrawText();
333*cdf0e10cSrcweir 
334*cdf0e10cSrcweir         if ( aTextRect.IsEmpty() && !aRect.IsEmpty() )
335*cdf0e10cSrcweir         {
336*cdf0e10cSrcweir             // this happens for instance if we're in a PaintToDevice call, where only a MetaFile is recorded,
337*cdf0e10cSrcweir             // but no actual painting happens, so our "DrawText( Point, ... )" is never called
338*cdf0e10cSrcweir             // In this case, calculate the rect from what OutputDevice::GetTextRect would give us. This has
339*cdf0e10cSrcweir             // the disadvantage of less accuracy, compared with the approach to calculate the rect from the
340*cdf0e10cSrcweir             // single "DrawText( Point, ... )" calls, since more intermediate arithmetics will translate
341*cdf0e10cSrcweir             // from ref- to target-units.
342*cdf0e10cSrcweir             aTextRect = m_rTargetDevice.GetTextRect( aRect, _rText, _nStyle, NULL, this );
343*cdf0e10cSrcweir         }
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir         // similar to above, the text rect now contains TWIPs (or whatever unit the ref device has), but the caller
346*cdf0e10cSrcweir         // expects pixel coordinates
347*cdf0e10cSrcweir         aTextRect = m_rTargetDevice.LogicToPixel( aTextRect );
348*cdf0e10cSrcweir 
349*cdf0e10cSrcweir         // convert the metric vector
350*cdf0e10cSrcweir         if ( _pVector )
351*cdf0e10cSrcweir         {
352*cdf0e10cSrcweir             for (   MetricVector::iterator charRect = _pVector->begin();
353*cdf0e10cSrcweir                     charRect != _pVector->end();
354*cdf0e10cSrcweir                     ++charRect
355*cdf0e10cSrcweir                 )
356*cdf0e10cSrcweir             {
357*cdf0e10cSrcweir                 *charRect = m_rTargetDevice.LogicToPixel( *charRect );
358*cdf0e10cSrcweir             }
359*cdf0e10cSrcweir         }
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir         return aTextRect;
362*cdf0e10cSrcweir     }
363*cdf0e10cSrcweir 
364*cdf0e10cSrcweir 	//====================================================================
365*cdf0e10cSrcweir 	//= ControlTextRenderer
366*cdf0e10cSrcweir 	//====================================================================
367*cdf0e10cSrcweir 	//--------------------------------------------------------------------
368*cdf0e10cSrcweir     ControlTextRenderer::ControlTextRenderer( const Control& _rControl, OutputDevice& _rTargetDevice, OutputDevice& _rReferenceDevice )
369*cdf0e10cSrcweir         :m_pImpl( new ReferenceDeviceTextLayout( _rControl, _rTargetDevice, _rReferenceDevice ) )
370*cdf0e10cSrcweir     {
371*cdf0e10cSrcweir     }
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir     //--------------------------------------------------------------------
374*cdf0e10cSrcweir     ControlTextRenderer::~ControlTextRenderer()
375*cdf0e10cSrcweir     {
376*cdf0e10cSrcweir     }
377*cdf0e10cSrcweir 
378*cdf0e10cSrcweir 	//--------------------------------------------------------------------
379*cdf0e10cSrcweir     Rectangle ControlTextRenderer::DrawText( const Rectangle& _rRect, const XubString& _rText, sal_uInt16 _nStyle,
380*cdf0e10cSrcweir         MetricVector* _pVector, String* _pDisplayText )
381*cdf0e10cSrcweir     {
382*cdf0e10cSrcweir         return m_pImpl->DrawText( _rRect, _rText, _nStyle, _pVector, _pDisplayText );
383*cdf0e10cSrcweir     }
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir //........................................................................
386*cdf0e10cSrcweir } // namespace vcl
387*cdf0e10cSrcweir //........................................................................
388