Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / inc / sallayout.hxx
blob9e82f39ece7cd3daf572856766ec710bc02e07cd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_VCL_INC_SALLAYOUT_HXX
21 #define INCLUDED_VCL_INC_SALLAYOUT_HXX
23 #include <iostream>
24 #include <list>
25 #include <vector>
27 #include <basegfx/polygon/b2dpolypolygon.hxx>
28 #include <i18nlangtag/languagetag.hxx>
29 #include <tools/gen.hxx>
30 #include <sal/log.hxx>
31 #include <vcl/dllapi.h>
32 #include <vcl/vclenum.hxx> // for typedef sal_UCS4
33 #include <vcl/devicecoordinate.hxx>
35 #ifndef _TOOLS_LANG_HXX
36 typedef unsigned short LanguageType;
37 #endif
39 #include "magic.h"
40 #include "salglyphid.hxx"
42 class SalGraphics;
43 class PhysicalFontFace;
44 enum class SalLayoutFlags;
45 namespace vcl {
46 class TextLayoutCache;
49 // used for managing runs e.g. for BiDi, glyph and script fallback
50 class VCL_PLUGIN_PUBLIC ImplLayoutRuns
52 private:
53 int mnRunIndex;
54 std::vector<int> maRuns;
56 public:
57 ImplLayoutRuns() { mnRunIndex = 0; maRuns.reserve(8); }
59 void Clear() { maRuns.clear(); }
60 bool AddPos( int nCharPos, bool bRTL );
61 bool AddRun( int nMinRunPos, int nEndRunPos, bool bRTL );
63 bool IsEmpty() const { return maRuns.empty(); }
64 void ResetPos() { mnRunIndex = 0; }
65 void NextRun() { mnRunIndex += 2; }
66 bool GetRun( int* nMinRunPos, int* nEndRunPos, bool* bRTL ) const;
67 bool GetNextPos( int* nCharPos, bool* bRTL );
68 bool PosIsInRun( int nCharPos ) const;
69 bool PosIsInAnyRun( int nCharPos ) const;
72 class ImplLayoutArgs
74 public:
75 // string related inputs
76 LanguageTag maLanguageTag;
77 SalLayoutFlags mnFlags;
78 int mnLength;
79 int mnMinCharPos;
80 int mnEndCharPos;
81 const sal_Unicode* mpStr;
83 // performance hack
84 vcl::TextLayoutCache const* m_pTextLayoutCache;
86 // positioning related inputs
87 const DeviceCoordinate* mpDXArray; // in pixel units
88 DeviceCoordinate mnLayoutWidth; // in pixel units
89 int mnOrientation; // in 0-3600 system
91 // data for bidi and glyph+script fallback
92 ImplLayoutRuns maRuns;
93 ImplLayoutRuns maFallbackRuns;
95 public:
96 ImplLayoutArgs( const sal_Unicode* pStr, int nLength,
97 int nMinCharPos, int nEndCharPos, SalLayoutFlags nFlags,
98 const LanguageTag& rLanguageTag,
99 vcl::TextLayoutCache const* pLayoutCache);
101 void SetLayoutWidth( DeviceCoordinate nWidth ) { mnLayoutWidth = nWidth; }
102 void SetDXArray( const DeviceCoordinate* pDXArray ) { mpDXArray = pDXArray; }
103 void SetOrientation( int nOrientation ) { mnOrientation = nOrientation; }
105 void ResetPos()
106 { maRuns.ResetPos(); }
107 bool GetNextPos( int* nCharPos, bool* bRTL )
108 { return maRuns.GetNextPos( nCharPos, bRTL ); }
109 bool GetNextRun( int* nMinRunPos, int* nEndRunPos, bool* bRTL );
110 bool NeedFallback( int nCharPos, bool bRTL )
111 { return maFallbackRuns.AddPos( nCharPos, bRTL ); }
112 bool NeedFallback( int nMinRunPos, int nEndRunPos, bool bRTL )
113 { return maFallbackRuns.AddRun( nMinRunPos, nEndRunPos, bRTL ); }
114 // methods used by BiDi and glyph fallback
115 bool NeedFallback() const
116 { return !maFallbackRuns.IsEmpty(); }
117 bool PrepareFallback();
119 protected:
120 void AddRun( int nMinCharPos, int nEndCharPos, bool bRTL );
123 // For nice SAL_INFO logging of ImplLayoutArgs values
124 std::ostream &operator <<(std::ostream& s, ImplLayoutArgs &rArgs);
126 // helper functions often used with ImplLayoutArgs
127 bool IsDiacritic( sal_UCS4 );
128 int GetVerticalFlags( sal_UCS4 );
130 // all positions/widths are in font units
131 // one exception: drawposition is in pixel units
133 // Unfortunately there is little documentation to help implementors of
134 // new classes derived from SalLayout ("layout engines"), and the code
135 // and data structures are far from obvious.
137 // For instance, I *think* the important virtual functions in the
138 // layout engines are called in this order:
140 // * InitFont()
141 // * LayoutText()
142 // * AdjustLayout(), any number of times (but presumably
143 // usually not at all or just once)
144 // * Optionally, DrawText()
146 // Functions that just return information like GetTexWidth() and
147 // FillDXArray() are called after LayoutText() and before DrawText().
149 // Another important questions is which parts of an ImplLayoutArgs can
150 // be changed by callers between LayoutText() and AdjustLayout()
151 // calls. It probably makes sense only if one assumes that the "string
152 // related inputs" part are not changed after LayoutText().
154 // But why use the same ImplLayoutArgs structure as parameter for both
155 // LayoutText() and AdjustLayout() in the first place? And why
156 // duplicate some of the fields in both SalLayout and ImplLayoutArgs
157 // (mnMinCharPos, mnEndCharPos, mnLayoutFlags==mnFlags,
158 // mnOrientation)? Lost in history...
160 class VCL_PLUGIN_PUBLIC SalLayout
162 public:
163 // used by upper layers
164 Point& DrawBase() { return maDrawBase; }
165 const Point& DrawBase() const { return maDrawBase; }
166 Point& DrawOffset() { return maDrawOffset; }
167 const Point& DrawOffset() const { return maDrawOffset; }
168 Point GetDrawPosition( const Point& rRelative = Point(0,0) ) const;
170 virtual bool LayoutText( ImplLayoutArgs& ) = 0; // first step of layouting
171 virtual void AdjustLayout( ImplLayoutArgs& ); // adjusting after fallback etc.
172 virtual void InitFont() const {}
173 virtual void DrawText( SalGraphics& ) const = 0;
174 virtual bool DrawTextSpecial( SalGraphics& /* rGraphics */, sal_uInt32 /* flags */ ) const { return false; }
175 #define DRAWTEXT_F_OUTLINE ((sal_uInt32)(1<<0))
177 int GetUnitsPerPixel() const { return mnUnitsPerPixel; }
178 int GetOrientation() const { return mnOrientation; }
180 // methods using string indexing
181 virtual sal_Int32 GetTextBreak(DeviceCoordinate nMaxWidth, DeviceCoordinate nCharExtra=0, int nFactor=1) const = 0;
182 virtual DeviceCoordinate FillDXArray( DeviceCoordinate* pDXArray ) const = 0;
183 virtual DeviceCoordinate GetTextWidth() const { return FillDXArray( NULL ); }
184 virtual void GetCaretPositions( int nArraySize, long* pCaretXArray ) const = 0;
185 virtual bool IsKashidaPosValid ( int /*nCharPos*/ ) const { return true; } // i60594
187 // methods using glyph indexing
188 virtual int GetNextGlyphs( int nLen, sal_GlyphId* pGlyphIdAry, Point& rPos, int&,
189 DeviceCoordinate* pGlyphAdvAry = NULL, int* pCharPosAry = NULL,
190 const PhysicalFontFace** pFallbackFonts = NULL ) const = 0;
191 virtual bool GetOutline( SalGraphics&, ::basegfx::B2DPolyPolygonVector& ) const;
192 virtual bool GetBoundRect( SalGraphics&, Rectangle& ) const;
194 static bool IsSpacingGlyph( sal_GlyphId );
196 // reference counting
197 void Release() const;
199 // used by glyph+font+script fallback
200 virtual void MoveGlyph( int nStart, long nNewXPos ) = 0;
201 virtual void DropGlyph( int nStart ) = 0;
202 virtual void Simplify( bool bIsBase ) = 0;
203 virtual void DisableGlyphInjection( bool /*bDisable*/ ) {}
205 virtual std::shared_ptr<vcl::TextLayoutCache>
206 CreateTextLayoutCache(OUString const&) const;
208 protected:
209 // used by layout engines
210 SalLayout();
211 virtual ~SalLayout();
213 // used by layout layers
214 void SetUnitsPerPixel( int n ) { mnUnitsPerPixel = n; }
215 void SetOrientation( int nOrientation ) // in 0-3600 system
216 { mnOrientation = nOrientation; }
218 static int CalcAsianKerning( sal_UCS4, bool bLeft, bool bVertical );
220 private:
221 SalLayout( const SalLayout& ) SAL_DELETED_FUNCTION;
222 SalLayout& operator=( const SalLayout& ) SAL_DELETED_FUNCTION;
224 protected:
225 int mnMinCharPos;
226 int mnEndCharPos;
227 SalLayoutFlags mnLayoutFlags;
229 int mnUnitsPerPixel;
230 int mnOrientation;
232 mutable int mnRefCount;
233 mutable Point maDrawOffset;
234 Point maDrawBase;
237 class VCL_PLUGIN_PUBLIC MultiSalLayout : public SalLayout
239 public:
240 virtual void DrawText( SalGraphics& ) const SAL_OVERRIDE;
241 virtual sal_Int32 GetTextBreak(DeviceCoordinate nMaxWidth, DeviceCoordinate nCharExtra, int nFactor) const SAL_OVERRIDE;
242 virtual DeviceCoordinate FillDXArray( DeviceCoordinate* pDXArray ) const SAL_OVERRIDE;
243 virtual void GetCaretPositions( int nArraySize, long* pCaretXArray ) const SAL_OVERRIDE;
244 virtual int GetNextGlyphs( int nLen, sal_GlyphId* pGlyphIdxAry, Point& rPos,
245 int&, DeviceCoordinate* pGlyphAdvAry, int* pCharPosAry,
246 const PhysicalFontFace** pFallbackFonts ) const SAL_OVERRIDE;
247 virtual bool GetOutline( SalGraphics&, ::basegfx::B2DPolyPolygonVector& ) const SAL_OVERRIDE;
249 // used only by OutputDevice::ImplLayout, TODO: make friend
250 explicit MultiSalLayout( SalLayout& rBaseLayout,
251 const PhysicalFontFace* pBaseFont = NULL );
252 bool AddFallback( SalLayout& rFallbackLayout,
253 ImplLayoutRuns&, const PhysicalFontFace* pFallbackFont );
254 virtual bool LayoutText( ImplLayoutArgs& ) SAL_OVERRIDE;
255 virtual void AdjustLayout( ImplLayoutArgs& ) SAL_OVERRIDE;
256 virtual void InitFont() const SAL_OVERRIDE;
258 void SetInComplete(bool bInComplete = true);
260 protected:
261 virtual ~MultiSalLayout();
263 private:
264 // dummy implementations
265 virtual void MoveGlyph( int, long ) SAL_OVERRIDE {}
266 virtual void DropGlyph( int ) SAL_OVERRIDE {}
267 virtual void Simplify( bool ) SAL_OVERRIDE {}
269 MultiSalLayout( const MultiSalLayout& ) SAL_DELETED_FUNCTION;
270 MultiSalLayout& operator=( const MultiSalLayout& ) SAL_DELETED_FUNCTION;
272 private:
273 SalLayout* mpLayouts[ MAX_FALLBACK ];
274 const PhysicalFontFace* mpFallbackFonts[ MAX_FALLBACK ];
275 ImplLayoutRuns maFallbackRuns[ MAX_FALLBACK ];
276 int mnLevel;
277 bool mbInComplete;
280 struct GlyphItem
282 int mnFlags;
283 int mnCharPos; // index in string
285 int mnOrigWidth; // original glyph width
286 int mnNewWidth; // width after adjustments
287 int mnXOffset;
289 int mnYOffset;
291 sal_GlyphId maGlyphId;
292 Point maLinearPos; // absolute position of non rotated string
294 public:
295 GlyphItem()
296 : mnFlags(0)
297 , mnCharPos(0)
298 , mnOrigWidth(0)
299 , mnNewWidth(0)
300 , mnXOffset(0)
301 , mnYOffset(0)
302 , maGlyphId(0)
305 GlyphItem( int nCharPos, sal_GlyphId aGlyphId, const Point& rLinearPos,
306 long nFlags, int nOrigWidth )
307 : mnFlags(nFlags), mnCharPos(nCharPos),
308 mnOrigWidth(nOrigWidth), mnNewWidth(nOrigWidth),
309 mnXOffset(0),
310 mnYOffset(0),
311 maGlyphId(aGlyphId), maLinearPos(rLinearPos)
314 GlyphItem( int nCharPos, sal_GlyphId aGlyphId, const Point& rLinearPos,
315 long nFlags, int nOrigWidth, int nXOffset, int nYOffset )
316 : mnFlags(nFlags), mnCharPos(nCharPos),
317 mnOrigWidth(nOrigWidth), mnNewWidth(nOrigWidth),
318 mnXOffset(nXOffset),
319 mnYOffset(nYOffset),
320 maGlyphId(aGlyphId), maLinearPos(rLinearPos)
323 enum{ FALLBACK_MASK=0xFF, IS_IN_CLUSTER=0x100, IS_RTL_GLYPH=0x200, IS_DIACRITIC=0x400 };
325 bool IsClusterStart() const { return ((mnFlags & IS_IN_CLUSTER) == 0); }
326 bool IsRTLGlyph() const { return ((mnFlags & IS_RTL_GLYPH) != 0); }
327 bool IsDiacritic() const { return ((mnFlags & IS_DIACRITIC) != 0); }
330 typedef std::list<GlyphItem> GlyphList;
331 typedef std::vector<GlyphItem> GlyphVector;
333 class VCL_PLUGIN_PUBLIC GenericSalLayout : public SalLayout
335 public:
336 // used by layout engines
337 void AppendGlyph( const GlyphItem& );
338 void Reserve(int size) { m_GlyphItems.reserve(size + 1); }
339 virtual void AdjustLayout( ImplLayoutArgs& ) SAL_OVERRIDE;
340 void ApplyDXArray( ImplLayoutArgs& );
341 void Justify( DeviceCoordinate nNewWidth );
342 void KashidaJustify( long nIndex, int nWidth );
343 void ApplyAsianKerning( const sal_Unicode*, int nLength );
344 void SortGlyphItems();
346 // used by upper layers
347 virtual DeviceCoordinate GetTextWidth() const SAL_OVERRIDE;
348 virtual Rectangle GetTextRect() const;
349 virtual DeviceCoordinate FillDXArray( DeviceCoordinate* pDXArray ) const SAL_OVERRIDE;
350 virtual sal_Int32 GetTextBreak(DeviceCoordinate nMaxWidth, DeviceCoordinate nCharExtra, int nFactor) const SAL_OVERRIDE;
351 virtual void GetCaretPositions( int nArraySize, long* pCaretXArray ) const SAL_OVERRIDE;
353 // used by display layers
354 virtual int GetNextGlyphs( int nLen, sal_GlyphId* pGlyphIdxAry, Point& rPos, int&,
355 DeviceCoordinate* pGlyphAdvAry = NULL, int* pCharPosAry = NULL,
356 const PhysicalFontFace** pFallbackFonts = NULL ) const SAL_OVERRIDE;
358 protected:
359 GenericSalLayout();
360 virtual ~GenericSalLayout();
362 // for glyph+font+script fallback
363 virtual void MoveGlyph( int nStart, long nNewXPos ) SAL_OVERRIDE;
364 virtual void DropGlyph( int nStart ) SAL_OVERRIDE;
365 virtual void Simplify( bool bIsBase ) SAL_OVERRIDE;
367 bool GetCharWidths( DeviceCoordinate* pCharWidths ) const;
369 GlyphVector m_GlyphItems;
371 private:
372 mutable Point maBasePoint;
374 GenericSalLayout( const GenericSalLayout& ) SAL_DELETED_FUNCTION;
375 GenericSalLayout& operator=( const GenericSalLayout& ) SAL_DELETED_FUNCTION;
378 #undef SalGraphics
380 #endif // INCLUDED_VCL_INC_SALLAYOUT_HXX
382 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */