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