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