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