LibreOffice Module vcl (master)  1
textlayout.cxx
Go to the documentation of this file.
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 <textlayout.hxx>
24 
25 #include <osl/diagnose.h>
26 #include <tools/fract.hxx>
27 #include <sal/log.hxx>
28 
29 #if OSL_DEBUG_LEVEL > 1
30 #include <rtl/strbuf.hxx>
31 #endif
32 
33 #include <memory>
34 #include <iterator>
35 
36 namespace vcl
37 {
38 
40  {
41  }
42 
43  long DefaultTextLayout::GetTextWidth( const OUString& _rText, sal_Int32 _nStartIndex, sal_Int32 _nLength ) const
44  {
45  return m_rTargetDevice.GetTextWidth( _rText, _nStartIndex, _nLength );
46  }
47 
48  void DefaultTextLayout::DrawText( const Point& _rStartPoint, const OUString& _rText, sal_Int32 _nStartIndex,
49  sal_Int32 _nLength, MetricVector* _pVector, OUString* _pDisplayText )
50  {
51  m_rTargetDevice.DrawText( _rStartPoint, _rText, _nStartIndex, _nLength, _pVector, _pDisplayText );
52  }
53 
54  void DefaultTextLayout::GetCaretPositions( const OUString& _rText, long* _pCaretXArray,
55  sal_Int32 _nStartIndex, sal_Int32 _nLength ) const
56  {
57  m_rTargetDevice.GetCaretPositions( _rText, _pCaretXArray, _nStartIndex, _nLength );
58  }
59 
60  sal_Int32 DefaultTextLayout::GetTextBreak( const OUString& _rText, long _nMaxTextWidth, sal_Int32 _nStartIndex, sal_Int32 _nLength ) const
61  {
62  return m_rTargetDevice.GetTextBreak( _rText, _nMaxTextWidth, _nStartIndex, _nLength );
63  }
64 
66  {
67  return false;
68  }
69 
71  {
72  public:
73  ReferenceDeviceTextLayout( const Control& _rControl, OutputDevice& _rTargetDevice, OutputDevice& _rReferenceDevice );
75 
76  // ITextLayout
77  virtual long GetTextWidth( const OUString& rStr, sal_Int32 nIndex, sal_Int32 nLen ) const override;
78  virtual void DrawText( const Point& _rStartPoint, const OUString& _rText, sal_Int32 _nStartIndex, sal_Int32 _nLength, MetricVector* _pVector, OUString* _pDisplayText ) override;
79  virtual void GetCaretPositions( const OUString& _rText, long* _pCaretXArray, sal_Int32 _nStartIndex, sal_Int32 _nLength ) const override;
80  virtual sal_Int32 GetTextBreak(const OUString& _rText, long _nMaxTextWidth, sal_Int32 _nStartIndex, sal_Int32 _nLength) const override;
81  virtual bool DecomposeTextRectAction() const override;
82 
83  public:
84  // equivalents to the respective OutputDevice methods, which take the reference device into account
85  tools::Rectangle DrawText( const tools::Rectangle& _rRect, const OUString& _rText, DrawTextFlags _nStyle, MetricVector* _pVector, OUString* _pDisplayText, const Size* i_pDeviceSize );
86  tools::Rectangle GetTextRect( const tools::Rectangle& _rRect, const OUString& _rText, DrawTextFlags _nStyle, Size* o_pDeviceSize );
87 
88  private:
89  long GetTextArray( const OUString& _rText, long* _pDXAry, sal_Int32 _nStartIndex, sal_Int32 _nLength ) const;
90 
94  const bool m_bRTLEnabled;
95 
97  };
98 
100  OutputDevice& _rReferenceDevice )
101  :m_rTargetDevice( _rTargetDevice )
102  ,m_rReferenceDevice( _rReferenceDevice )
103  ,m_aUnzoomedPointFont( _rControl.GetUnzoomedControlPointFont() )
104  ,m_bRTLEnabled( _rControl.IsRTLEnabled() )
105  {
106  const Fraction& aZoom( _rControl.GetZoom() );
108 
109  MapMode aTargetMapMode( m_rTargetDevice.GetMapMode() );
110  OSL_ENSURE( aTargetMapMode.GetOrigin() == Point(), "ReferenceDeviceTextLayout::ReferenceDeviceTextLayout: uhm, the code below won't work here ..." );
111 
112  // normally, controls simulate "zoom" by "zooming" the font. This is responsible for (part of) the discrepancies
113  // between text in Writer and text in controls in Writer, though both have the same font.
114  // So, if we have a zoom set at the control, then we do not scale the font, but instead modify the map mode
115  // to accommodate for the zoom.
116  aTargetMapMode.SetScaleX( aZoom ); // TODO: shouldn't this be "current_scale * zoom"?
117  aTargetMapMode.SetScaleY( aZoom );
118 
119  // also, use a higher-resolution map unit than "pixels", which should save us some rounding errors when
120  // translating coordinates between the reference device and the target device.
121  OSL_ENSURE( aTargetMapMode.GetMapUnit() == MapUnit::MapPixel,
122  "ReferenceDeviceTextLayout::ReferenceDeviceTextLayout: this class is not expected to work with such target devices!" );
123  // we *could* adjust all the code in this class to handle this case, but at the moment, it's not necessary
124  const MapUnit eTargetMapUnit = m_rReferenceDevice.GetMapMode().GetMapUnit();
125  aTargetMapMode.SetMapUnit( eTargetMapUnit );
126  OSL_ENSURE( aTargetMapMode.GetMapUnit() != MapUnit::MapPixel,
127  "ReferenceDeviceTextLayout::ReferenceDeviceTextLayout: a reference device which has map mode PIXEL?!" );
128 
129  m_rTargetDevice.SetMapMode( aTargetMapMode );
130 
131  // now that the Zoom is part of the map mode, reset the target device's font to the "unzoomed" version
132  Font aDrawFont( m_aUnzoomedPointFont );
133  aDrawFont.SetFontSize( OutputDevice::LogicToLogic(aDrawFont.GetFontSize(), MapMode(MapUnit::MapPoint), MapMode(eTargetMapUnit)) );
134  _rTargetDevice.SetFont( aDrawFont );
135 
136  // transfer font to the reference device
138  Font aRefFont( m_aUnzoomedPointFont );
140  aRefFont.GetFontSize(), MapMode(MapUnit::MapPoint), m_rReferenceDevice.GetMapMode()) );
141  m_rReferenceDevice.SetFont( aRefFont );
142  }
143 
145  {
148  }
149 
150  namespace
151  {
152  bool lcl_normalizeLength( const OUString& _rText, const sal_Int32 _nStartIndex, sal_Int32& _io_nLength )
153  {
154  sal_Int32 nTextLength = _rText.getLength();
155  if ( _nStartIndex > nTextLength )
156  return false;
157  if ( _nStartIndex + _io_nLength > nTextLength )
158  _io_nLength = nTextLength - _nStartIndex;
159  return true;
160  }
161  }
162 
163  long ReferenceDeviceTextLayout::GetTextArray( const OUString& _rText, long* _pDXAry, sal_Int32 _nStartIndex, sal_Int32 _nLength ) const
164  {
165  if ( !lcl_normalizeLength( _rText, _nStartIndex, _nLength ) )
166  return 0;
167 
168  // retrieve the character widths from the reference device
169  long nTextWidth = m_rReferenceDevice.GetTextArray( _rText, _pDXAry, _nStartIndex, _nLength );
170 #if OSL_DEBUG_LEVEL > 1
171  if ( _pDXAry )
172  {
173  OStringBuffer aTrace;
174  aTrace.append( "ReferenceDeviceTextLayout::GetTextArray( " );
175  aTrace.append( OUStringToOString( _rText, RTL_TEXTENCODING_UTF8 ) );
176  aTrace.append( " ): " );
177  aTrace.append( nTextWidth );
178  aTrace.append( " = ( " );
179  for ( sal_Int32 i=0; i<_nLength; )
180  {
181  aTrace.append( _pDXAry[i] );
182  if ( ++i < _nLength )
183  aTrace.append( ", " );
184  }
185  aTrace.append( ")" );
186  SAL_INFO( "vcl", aTrace.makeStringAndClear() );
187  }
188 #endif
189  return nTextWidth;
190  }
191 
192  long ReferenceDeviceTextLayout::GetTextWidth( const OUString& _rText, sal_Int32 _nStartIndex, sal_Int32 _nLength ) const
193  {
194  return GetTextArray( _rText, nullptr, _nStartIndex, _nLength );
195  }
196 
197  void ReferenceDeviceTextLayout::DrawText( const Point& _rStartPoint, const OUString& _rText, sal_Int32 _nStartIndex, sal_Int32 _nLength, MetricVector* _pVector, OUString* _pDisplayText )
198  {
199  if ( !lcl_normalizeLength( _rText, _nStartIndex, _nLength ) )
200  return;
201 
202  if ( _pVector && _pDisplayText )
203  {
204  MetricVector aGlyphBounds;
205  m_rReferenceDevice.GetGlyphBoundRects( _rStartPoint, _rText, _nStartIndex, _nLength, aGlyphBounds );
206  ::std::copy(
207  aGlyphBounds.begin(), aGlyphBounds.end(),
208  ::std::insert_iterator< MetricVector > ( *_pVector, _pVector->end() ) );
209  *_pDisplayText += _rText.copy( _nStartIndex, _nLength );
210  return;
211  }
212 
213  std::unique_ptr<long[]> pCharWidths(new long[ _nLength ]);
214  long nTextWidth = GetTextArray( _rText, pCharWidths.get(), _nStartIndex, _nLength );
215  m_rTargetDevice.DrawTextArray( _rStartPoint, _rText, pCharWidths.get(), _nStartIndex, _nLength );
216  pCharWidths.reset();
217 
218  m_aCompleteTextRect.Union( tools::Rectangle( _rStartPoint, Size( nTextWidth, m_rTargetDevice.GetTextHeight() ) ) );
219  }
220 
221  void ReferenceDeviceTextLayout::GetCaretPositions( const OUString& _rText, long* _pCaretXArray,
222  sal_Int32 _nStartIndex, sal_Int32 _nLength ) const
223  {
224  if ( !lcl_normalizeLength( _rText, _nStartIndex, _nLength ) )
225  return;
226 
227  // retrieve the caret positions from the reference device
228  m_rReferenceDevice.GetCaretPositions( _rText, _pCaretXArray, _nStartIndex, _nLength );
229  }
230 
231  sal_Int32 ReferenceDeviceTextLayout::GetTextBreak( const OUString& _rText, long _nMaxTextWidth, sal_Int32 _nStartIndex, sal_Int32 _nLength ) const
232  {
233  if ( !lcl_normalizeLength( _rText, _nStartIndex, _nLength ) )
234  return 0;
235 
236  return m_rReferenceDevice.GetTextBreak( _rText, _nMaxTextWidth, _nStartIndex, _nLength );
237  }
238 
240  {
241  return true;
242  }
243 
245  MetricVector* _pVector, OUString* _pDisplayText, const Size* i_pDeviceSize )
246  {
247  if ( _rText.isEmpty() )
248  return tools::Rectangle();
249 
250  // determine text layout mode from the RTL-ness of the control whose text we render
252  m_rReferenceDevice.SetLayoutMode( nTextLayoutMode );
254 
255  // ComplexTextLayoutFlags::TextOriginLeft is because when we do actually draw the text (in DrawText( Point, ... )), then
256  // our caller gives us the left border of the draw position, regardless of script type, text layout,
257  // 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,
258  // but passed pixel coordinates. So, adjust the rect.
259  tools::Rectangle aRect( m_rTargetDevice.PixelToLogic( _rRect ) );
260  if (i_pDeviceSize)
261  {
262  //if i_pDeviceSize is passed in here, it was the original pre logic-to-pixel size of _rRect
263  SAL_WARN_IF(std::abs(_rRect.GetSize().Width() - m_rTargetDevice.LogicToPixel(*i_pDeviceSize).Width()) > 1, "vcl", "DeviceSize width was expected to match Pixel width");
264  SAL_WARN_IF(std::abs(_rRect.GetSize().Height() - m_rTargetDevice.LogicToPixel(*i_pDeviceSize).Height()) > 1, "vcl", "DeviceSize height was expected to match Pixel height");
265  aRect.SetSize(*i_pDeviceSize);
266  }
267 
269  m_rTargetDevice.DrawText( aRect, _rText, _nStyle, _pVector, _pDisplayText, this );
271 
272  if ( aTextRect.IsEmpty() && !aRect.IsEmpty() )
273  {
274  // this happens for instance if we're in a PaintToDevice call, where only a MetaFile is recorded,
275  // but no actual painting happens, so our "DrawText( Point, ... )" is never called
276  // In this case, calculate the rect from what OutputDevice::GetTextRect would give us. This has
277  // the disadvantage of less accuracy, compared with the approach to calculate the rect from the
278  // single "DrawText( Point, ... )" calls, since more intermediate arithmetic will translate
279  // from ref- to target-units.
280  aTextRect = m_rTargetDevice.GetTextRect( aRect, _rText, _nStyle, nullptr, this );
281  }
282 
283  // similar to above, the text rect now contains TWIPs (or whatever unit the ref device has), but the caller
284  // expects pixel coordinates
285  aTextRect = m_rTargetDevice.LogicToPixel( aTextRect );
286 
287  // convert the metric vector
288  if ( _pVector )
289  {
290  for ( auto& rCharRect : *_pVector )
291  {
292  rCharRect = m_rTargetDevice.LogicToPixel( rCharRect );
293  }
294  }
295 
296  return aTextRect;
297  }
298 
299  tools::Rectangle ReferenceDeviceTextLayout::GetTextRect( const tools::Rectangle& _rRect, const OUString& _rText, DrawTextFlags _nStyle, Size* o_pDeviceSize )
300  {
301  if ( _rText.isEmpty() )
302  return tools::Rectangle();
303 
304  // determine text layout mode from the RTL-ness of the control whose text we render
306  m_rReferenceDevice.SetLayoutMode( nTextLayoutMode );
308 
309  // ComplexTextLayoutFlags::TextOriginLeft is because when we do actually draw the text (in DrawText( Point, ... )), then
310  // our caller gives us the left border of the draw position, regardless of script type, text layout,
311  // 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,
312  // but passed pixel coordinates. So, adjust the rect.
313  tools::Rectangle aRect( m_rTargetDevice.PixelToLogic( _rRect ) );
314 
315  tools::Rectangle aTextRect = m_rTargetDevice.GetTextRect( aRect, _rText, _nStyle, nullptr, this );
316 
317  //if o_pDeviceSize is available, stash the pre logic-to-pixel size in it
318  if (o_pDeviceSize)
319  {
320  *o_pDeviceSize = aTextRect.GetSize();
321  }
322 
323  // similar to above, the text rect now contains TWIPs (or whatever unit the ref device has), but the caller
324  // expects pixel coordinates
325  aTextRect = m_rTargetDevice.LogicToPixel( aTextRect );
326 
327  return aTextRect;
328  }
329 
330  ControlTextRenderer::ControlTextRenderer( const Control& _rControl, OutputDevice& _rTargetDevice, OutputDevice& _rReferenceDevice )
331  :m_pImpl( new ReferenceDeviceTextLayout( _rControl, _rTargetDevice, _rReferenceDevice ) )
332  {
333  }
334 
336  {
337  }
338 
339  tools::Rectangle ControlTextRenderer::DrawText( const tools::Rectangle& _rRect, const OUString& _rText, DrawTextFlags _nStyle,
340  MetricVector* _pVector, OUString* _pDisplayText, const Size* i_pDeviceSize )
341  {
342  return m_pImpl->DrawText( _rRect, _rText, _nStyle, _pVector, _pDisplayText, i_pDeviceSize );
343  }
344 
345  tools::Rectangle ControlTextRenderer::GetTextRect( const tools::Rectangle& _rRect, const OUString& _rText, DrawTextFlags _nStyle, Size* o_pDeviceSize = nullptr )
346  {
347  return m_pImpl->GetTextRect( _rRect, _rText, _nStyle, o_pDeviceSize );
348  }
349 
350 } // namespace vcl
351 
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
void DrawText(const Point &rStartPt, const OUString &rStr, sal_Int32 nIndex=0, sal_Int32 nLen=-1, MetricVector *pVector=nullptr, OUString *pDisplayText=nullptr, const SalLayoutGlyphs *pLayoutCache=nullptr)
Definition: text.cxx:779
virtual void GetCaretPositions(const OUString &_rText, long *_pCaretXArray, sal_Int32 _nStartIndex, sal_Int32 _nLength) const override
Definition: textlayout.cxx:221
void SetFontSize(const Size &)
Definition: font/font.cxx:116
virtual bool DecomposeTextRectAction() const override
Definition: textlayout.cxx:65
std::vector< tools::Rectangle > MetricVector
Definition: outdev.hxx:139
::std::unique_ptr< ReferenceDeviceTextLayout > m_pImpl
Definition: textlayout.hxx:105
Point LogicToLogic(const Point &rPtSource, const MapMode *pMapModeSource, const MapMode *pMapModeDest) const
Definition: map.cxx:1678
long Height() const
const MapMode & GetMapMode() const
Definition: outdev.hxx:1673
void SetLayoutMode(ComplexTextLayoutFlags nTextLayoutMode)
void SetMapMode()
Definition: map.cxx:655
tools::Rectangle m_aCompleteTextRect
Definition: textlayout.cxx:96
bool IsEmpty() const
const Fraction & GetZoom() const
Definition: window2.cxx:1204
OutputDevice & m_rTargetDevice
Definition: textlayout.hxx:80
tools::Rectangle GetTextRect(const tools::Rectangle &rRect, const OUString &rStr, DrawTextFlags nStyle=DrawTextFlags::WordBreak, TextRectInfo *pInfo=nullptr, const vcl::ITextLayout *_pTextLayout=nullptr) const
Definition: text.cxx:1789
virtual bool DecomposeTextRectAction() const override
Definition: textlayout.cxx:239
virtual ~DefaultTextLayout()
Definition: textlayout.cxx:39
long GetTextArray(const OUString &_rText, long *_pDXAry, sal_Int32 _nStartIndex, sal_Int32 _nLength) const
Definition: textlayout.cxx:163
void DrawTextArray(const Point &rStartPt, const OUString &rStr, const long *pDXAry, sal_Int32 nIndex=0, sal_Int32 nLen=-1, SalLayoutFlags flags=SalLayoutFlags::NONE, const SalLayoutGlyphs *pLayoutCache=nullptr)
Definition: text.cxx:914
void SetScaleX(const Fraction &rScaleX)
Definition: mapmod.cxx:107
void GetCaretPositions(const OUString &, long *pCaretXArray, sal_Int32 nIndex, sal_Int32 nLen, const SalLayoutGlyphs *pGlyphs=nullptr) const
Definition: text.cxx:1056
ComplexTextLayoutFlags
Definition: outdevstate.hxx:66
virtual sal_Int32 GetTextBreak(const OUString &_rText, long _nMaxTextWidth, sal_Int32 _nStartIndex, sal_Int32 _nLength) const override
Definition: textlayout.cxx:60
int i
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
Definition: outdev.hxx:304
virtual long GetTextWidth(const OUString &_rText, sal_Int32 _nStartIndex, sal_Int32 _nLength) const override
Definition: textlayout.cxx:43
tools::Rectangle GetTextRect(const tools::Rectangle &_rRect, const OUString &_rText, DrawTextFlags _nStyle, Size *o_pDeviceSize)
Definition: textlayout.cxx:345
const Size & GetFontSize() const
Definition: font/font.cxx:669
MapUnit GetMapUnit() const
Definition: mapmod.cxx:163
DrawTextFlags
Definition: outdev.hxx:144
long GetTextHeight() const
Height where any character of the current font fits; in logic coordinates.
Definition: text.cxx:888
virtual void DrawText(const Point &_rStartPoint, const OUString &_rText, sal_Int32 _nStartIndex, sal_Int32 _nLength, MetricVector *_pVector, OUString *_pDisplayText) override
Definition: textlayout.cxx:197
Size GetSize() const
bool GetGlyphBoundRects(const Point &rOrigin, const OUString &rStr, int nIndex, int nLen, MetricVector &rVector)
Point PixelToLogic(const Point &rDevicePt) const
Definition: map.cxx:1185
Point LogicToPixel(const Point &rLogicPt) const
Definition: map.cxx:940
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
virtual void GetCaretPositions(const OUString &_rText, long *_pCaretXArray, sal_Int32 _nStartIndex, sal_Int32 _nLength) const override
Definition: textlayout.cxx:54
Definition: ctrl.hxx:33
virtual void DrawText(const Point &_rStartPoint, const OUString &_rText, sal_Int32 _nStartIndex, sal_Int32 _nLength, MetricVector *_pVector, OUString *_pDisplayText) override
Definition: textlayout.cxx:48
tools::Rectangle & Union(const tools::Rectangle &rRect)
#define SAL_WARN_IF(condition, area, stream)
void SetFont(const vcl::Font &rNewFont)
#define SAL_INFO(area, stream)
tools::Rectangle DrawText(const tools::Rectangle &_rRect, const OUString &_rText, DrawTextFlags _nStyle, MetricVector *_pVector, OUString *_pDisplayText, const Size *i_pDeviceSize)
Definition: textlayout.cxx:339
long GetTextWidth(const OUString &rStr, sal_Int32 nIndex=0, sal_Int32 nLen=-1, vcl::TextLayoutCache const *=nullptr, SalLayoutGlyphs const *const pLayoutCache=nullptr) const
Width of the text.
Definition: text.cxx:877
sal_Int32 GetTextBreak(const OUString &rStr, long nTextWidth, sal_Int32 nIndex, sal_Int32 nLen=-1, long nCharExtra=0, vcl::TextLayoutCache const *=nullptr, const SalLayoutGlyphs *pGlyphs=nullptr) const
Definition: text.cxx:1366
MapUnit
virtual sal_Int32 GetTextBreak(const OUString &_rText, long _nMaxTextWidth, sal_Int32 _nStartIndex, sal_Int32 _nLength) const override
Definition: textlayout.cxx:231
long GetTextArray(const OUString &rStr, long *pDXAry, sal_Int32 nIndex=0, sal_Int32 nLen=-1, vcl::TextLayoutCache const *=nullptr, SalLayoutGlyphs const *const pLayoutCache=nullptr) const
Definition: text.cxx:947
OutputDevice & m_rReferenceDevice
Definition: textlayout.cxx:92
void Push(PushFlags nFlags=PushFlags::ALL)
Definition: outdevstate.cxx:60
ControlTextRenderer(const Control &_rControl, OutputDevice &_rTargetDevice, OutputDevice &_rReferenceDevice)
Definition: textlayout.cxx:330
tools::Rectangle GetTextRect(const tools::Rectangle &_rRect, const OUString &_rText, DrawTextFlags _nStyle, Size *o_pDeviceSize)
Definition: textlayout.cxx:299
ReferenceDeviceTextLayout(const Control &_rControl, OutputDevice &_rTargetDevice, OutputDevice &_rReferenceDevice)
Definition: textlayout.cxx:99
virtual long GetTextWidth(const OUString &rStr, sal_Int32 nIndex, sal_Int32 nLen) const override
Definition: textlayout.cxx:192