Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / vcl / inc / sallayout.hxx
blobd4a960d634322394f65782e7c5cf141bf600e600
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 <vcl/dllapi.h>
31 #include <vcl/vclenum.hxx> // for typedef sal_UCS4
32 #include <vcl/devicecoordinate.hxx>
34 #include "salglyphid.hxx"
36 #define MAX_FALLBACK 16
39 class SalGraphics;
40 class PhysicalFontFace;
41 struct GlyphItem;
42 enum class SalLayoutFlags;
43 namespace vcl {
44 class TextLayoutCache;
47 // used for managing runs e.g. for BiDi, glyph and script fallback
48 class VCL_PLUGIN_PUBLIC ImplLayoutRuns
50 private:
51 int mnRunIndex;
52 std::vector<int> maRuns;
54 public:
55 ImplLayoutRuns() { mnRunIndex = 0; maRuns.reserve(8); }
57 void Clear() { maRuns.clear(); }
58 void AddPos( int nCharPos, bool bRTL );
59 void AddRun( int nMinRunPos, int nEndRunPos, bool bRTL );
61 bool IsEmpty() const { return maRuns.empty(); }
62 void ResetPos() { mnRunIndex = 0; }
63 void NextRun() { mnRunIndex += 2; }
64 bool GetRun( int* nMinRunPos, int* nEndRunPos, bool* bRTL ) const;
65 bool GetNextPos( int* nCharPos, bool* bRTL );
66 bool PosIsInRun( int nCharPos ) const;
67 bool PosIsInAnyRun( int nCharPos ) const;
70 class ImplLayoutArgs
72 public:
73 // string related inputs
74 LanguageTag maLanguageTag;
75 SalLayoutFlags mnFlags;
76 const OUString& mrStr;
77 int mnMinCharPos;
78 int mnEndCharPos;
80 // performance hack
81 vcl::TextLayoutCache const* m_pTextLayoutCache;
83 // positioning related inputs
84 const DeviceCoordinate* mpDXArray; // in pixel units
85 DeviceCoordinate mnLayoutWidth; // in pixel units
86 int mnOrientation; // in 0-3600 system
88 // data for bidi and glyph+script fallback
89 ImplLayoutRuns maRuns;
90 ImplLayoutRuns maFallbackRuns;
92 public:
93 ImplLayoutArgs( const OUString& rStr,
94 int nMinCharPos, int nEndCharPos, SalLayoutFlags nFlags,
95 const LanguageTag& rLanguageTag,
96 vcl::TextLayoutCache const* pLayoutCache);
98 void SetLayoutWidth( DeviceCoordinate nWidth ) { mnLayoutWidth = nWidth; }
99 void SetDXArray( const DeviceCoordinate* pDXArray ) { mpDXArray = pDXArray; }
100 void SetOrientation( int nOrientation ) { mnOrientation = nOrientation; }
102 void ResetPos()
103 { maRuns.ResetPos(); }
104 bool GetNextPos( int* nCharPos, bool* bRTL )
105 { return maRuns.GetNextPos( nCharPos, bRTL ); }
106 bool GetNextRun( int* nMinRunPos, int* nEndRunPos, bool* bRTL );
107 void NeedFallback( int nMinRunPos, int nEndRunPos, bool bRTL )
108 { maFallbackRuns.AddRun( nMinRunPos, nEndRunPos, bRTL ); }
109 // methods used by BiDi and glyph fallback
110 bool NeedFallback() const
111 { return !maFallbackRuns.IsEmpty(); }
112 bool PrepareFallback();
114 protected:
115 void AddRun( int nMinCharPos, int nEndCharPos, bool bRTL );
118 // For nice SAL_INFO logging of ImplLayoutArgs values
119 std::ostream &operator <<(std::ostream& s, ImplLayoutArgs &rArgs);
121 // all positions/widths are in font units
122 // one exception: drawposition is in pixel units
124 // Unfortunately there is little documentation to help implementors of
125 // new classes derived from SalLayout ("layout engines"), and the code
126 // and data structures are far from obvious.
128 // For instance, I *think* the important virtual functions in the
129 // layout engines are called in this order:
131 // * InitFont()
132 // * LayoutText()
133 // * AdjustLayout(), any number of times (but presumably
134 // usually not at all or just once)
135 // * Optionally, DrawText()
137 // Functions that just return information like GetTexWidth() and
138 // FillDXArray() are called after LayoutText() and before DrawText().
140 // Another important questions is which parts of an ImplLayoutArgs can
141 // be changed by callers between LayoutText() and AdjustLayout()
142 // calls. It probably makes sense only if one assumes that the "string
143 // related inputs" part are not changed after LayoutText().
145 // But why use the same ImplLayoutArgs structure as parameter for both
146 // LayoutText() and AdjustLayout() in the first place? And why
147 // duplicate some of the fields in both SalLayout and ImplLayoutArgs
148 // (mnMinCharPos, mnEndCharPos, mnLayoutFlags==mnFlags,
149 // mnOrientation)? Lost in history...
151 class VCL_PLUGIN_PUBLIC SalLayout
153 public:
154 // used by upper layers
155 Point& DrawBase() { return maDrawBase; }
156 const Point& DrawBase() const { return maDrawBase; }
157 Point& DrawOffset() { return maDrawOffset; }
158 const Point& DrawOffset() const { return maDrawOffset; }
159 Point GetDrawPosition( const Point& rRelative = Point(0,0) ) const;
161 virtual bool LayoutText( ImplLayoutArgs& ) = 0; // first step of layouting
162 virtual void AdjustLayout( ImplLayoutArgs& ); // adjusting after fallback etc.
163 virtual void InitFont() const {}
164 virtual void DrawText( SalGraphics& ) const = 0;
166 int GetUnitsPerPixel() const { return mnUnitsPerPixel; }
167 int GetOrientation() const { return mnOrientation; }
169 // methods using string indexing
170 virtual sal_Int32 GetTextBreak(DeviceCoordinate nMaxWidth, DeviceCoordinate nCharExtra, int nFactor) const = 0;
171 virtual DeviceCoordinate FillDXArray( DeviceCoordinate* pDXArray ) const = 0;
172 virtual DeviceCoordinate GetTextWidth() const { return FillDXArray( nullptr ); }
173 virtual void GetCaretPositions( int nArraySize, long* pCaretXArray ) const = 0;
174 virtual bool IsKashidaPosValid ( int /*nCharPos*/ ) const { return true; } // i60594
176 // methods using glyph indexing
177 virtual int GetNextGlyphs(int nLen, const GlyphItem** pGlyphs, Point& rPos, int&,
178 const PhysicalFontFace** pFallbackFonts = nullptr) const = 0;
179 virtual bool GetOutline( SalGraphics&, basegfx::B2DPolyPolygonVector& ) const;
180 virtual bool GetBoundRect( SalGraphics&, tools::Rectangle& ) const;
182 // reference counting
183 void Release() const;
185 // used by glyph+font+script fallback
186 virtual void MoveGlyph( int nStart, long nNewXPos ) = 0;
187 virtual void DropGlyph( int nStart ) = 0;
188 virtual void Simplify( bool bIsBase ) = 0;
190 virtual std::shared_ptr<vcl::TextLayoutCache>
191 CreateTextLayoutCache(OUString const&) const;
193 protected:
194 // used by layout engines
195 SalLayout();
196 virtual ~SalLayout();
198 static int CalcAsianKerning( sal_UCS4, bool bLeft, bool bVertical );
200 private:
201 SalLayout( const SalLayout& ) = delete;
202 SalLayout& operator=( const SalLayout& ) = delete;
204 protected:
205 int mnMinCharPos;
206 int mnEndCharPos;
207 SalLayoutFlags mnLayoutFlags;
209 int mnUnitsPerPixel;
210 int mnOrientation;
212 mutable int mnRefCount;
213 mutable Point maDrawOffset;
214 Point maDrawBase;
217 class VCL_PLUGIN_PUBLIC MultiSalLayout : public SalLayout
219 public:
220 virtual void DrawText( SalGraphics& ) const override;
221 virtual sal_Int32 GetTextBreak(DeviceCoordinate nMaxWidth, DeviceCoordinate nCharExtra, int nFactor) const override;
222 virtual DeviceCoordinate FillDXArray( DeviceCoordinate* pDXArray ) const override;
223 virtual void GetCaretPositions( int nArraySize, long* pCaretXArray ) const override;
224 virtual int GetNextGlyphs(int nLen, const GlyphItem** pGlyphs, Point& rPos, int&,
225 const PhysicalFontFace** pFallbackFonts = nullptr) const override;
226 virtual bool GetOutline( SalGraphics&, basegfx::B2DPolyPolygonVector& ) const override;
227 virtual bool IsKashidaPosValid(int nCharPos) const override;
229 // used only by OutputDevice::ImplLayout, TODO: make friend
230 explicit MultiSalLayout( SalLayout& rBaseLayout );
231 void AddFallback( SalLayout& rFallbackLayout,
232 ImplLayoutRuns&, const PhysicalFontFace* pFallbackFont );
233 virtual bool LayoutText( ImplLayoutArgs& ) override;
234 virtual void AdjustLayout( ImplLayoutArgs& ) override;
235 virtual void InitFont() const override;
237 void SetIncomplete(bool bIncomplete);
239 protected:
240 virtual ~MultiSalLayout() override;
242 private:
243 // dummy implementations
244 virtual void MoveGlyph( int, long ) override {}
245 virtual void DropGlyph( int ) override {}
246 virtual void Simplify( bool ) override {}
248 MultiSalLayout( const MultiSalLayout& ) = delete;
249 MultiSalLayout& operator=( const MultiSalLayout& ) = delete;
251 private:
252 SalLayout* mpLayouts[ MAX_FALLBACK ];
253 const PhysicalFontFace* mpFallbackFonts[ MAX_FALLBACK ];
254 ImplLayoutRuns maFallbackRuns[ MAX_FALLBACK ];
255 int mnLevel;
256 bool mbIncomplete;
259 struct GlyphItem
261 int mnFlags;
262 int mnCharPos; // index in string
264 int mnOrigWidth; // original glyph width
265 int mnNewWidth; // width after adjustments
266 int mnXOffset;
268 sal_GlyphId maGlyphId;
269 Point maLinearPos; // absolute position of non rotated string
271 int mnFallbackLevel;
273 public:
274 GlyphItem( int nCharPos, sal_GlyphId aGlyphId, const Point& rLinearPos,
275 long nFlags, int nOrigWidth )
276 : mnFlags(nFlags)
277 , mnCharPos(nCharPos)
278 , mnOrigWidth(nOrigWidth)
279 , mnNewWidth(nOrigWidth)
280 , mnXOffset(0)
281 , maGlyphId(aGlyphId)
282 , maLinearPos(rLinearPos)
283 , mnFallbackLevel(0)
286 GlyphItem( int nCharPos, sal_GlyphId aGlyphId, const Point& rLinearPos,
287 long nFlags, int nOrigWidth, int nXOffset )
288 : mnFlags(nFlags)
289 , mnCharPos(nCharPos)
290 , mnOrigWidth(nOrigWidth)
291 , mnNewWidth(nOrigWidth)
292 , mnXOffset(nXOffset)
293 , maGlyphId(aGlyphId)
294 , maLinearPos(rLinearPos)
295 , mnFallbackLevel(0)
298 enum {
299 IS_IN_CLUSTER = 0x001,
300 IS_RTL_GLYPH = 0x002,
301 IS_DIACRITIC = 0x004,
302 IS_VERTICAL = 0x008,
303 IS_SPACING = 0x010,
304 ALLOW_KASHIDA = 0x020
307 bool IsClusterStart() const { return ((mnFlags & IS_IN_CLUSTER) == 0); }
308 bool IsRTLGlyph() const { return ((mnFlags & IS_RTL_GLYPH) != 0); }
309 bool IsDiacritic() const { return ((mnFlags & IS_DIACRITIC) != 0); }
310 bool IsVertical() const { return ((mnFlags & IS_VERTICAL) != 0); }
311 bool IsSpacing() const { return ((mnFlags & IS_SPACING) != 0); }
312 bool AllowKashida() const { return ((mnFlags & ALLOW_KASHIDA) != 0); }
315 class VCL_PLUGIN_PUBLIC GenericSalLayout : public SalLayout
317 public:
318 // used by layout engines
319 void AppendGlyph( const GlyphItem& );
320 void Reserve(int size) { m_GlyphItems.reserve(size + 1); }
321 virtual void ApplyDXArray(ImplLayoutArgs&) = 0;
322 void Justify(DeviceCoordinate nNewWidth);
323 void ApplyAsianKerning(const OUString& rStr);
325 // used by upper layers
326 virtual DeviceCoordinate GetTextWidth() const override;
327 virtual DeviceCoordinate FillDXArray( DeviceCoordinate* pDXArray ) const override;
328 virtual sal_Int32 GetTextBreak(DeviceCoordinate nMaxWidth, DeviceCoordinate nCharExtra, int nFactor) const override;
329 virtual void GetCaretPositions( int nArraySize, long* pCaretXArray ) const override;
331 // used by display layers
332 virtual int GetNextGlyphs(int nLen, const GlyphItem** pGlyphs, Point& rPos, int&,
333 const PhysicalFontFace** pFallbackFonts = nullptr) const override;
335 protected:
336 GenericSalLayout();
337 virtual ~GenericSalLayout() override;
339 // for glyph+font+script fallback
340 virtual void MoveGlyph( int nStart, long nNewXPos ) override;
341 virtual void DropGlyph( int nStart ) override;
342 virtual void Simplify( bool bIsBase ) override;
344 virtual bool GetCharWidths(DeviceCoordinate* pCharWidths) const = 0;
346 std::vector<GlyphItem> m_GlyphItems;
348 private:
349 mutable Point maBasePoint;
351 GenericSalLayout( const GenericSalLayout& ) = delete;
352 GenericSalLayout& operator=( const GenericSalLayout& ) = delete;
355 #undef SalGraphics
357 #endif // INCLUDED_VCL_INC_SALLAYOUT_HXX
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */