Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / vcl / inc / sallayout.hxx
blob62cda5832906c248fe1a0a1c5656b4bd4ca21335
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 <memory>
26 #include <vector>
28 #include <hb.h>
30 #include <com/sun/star/i18n/XBreakIterator.hpp>
32 #include <basegfx/polygon/b2dpolypolygon.hxx>
33 #include <i18nlangtag/languagetag.hxx>
34 #include <tools/gen.hxx>
35 #include <vcl/dllapi.h>
36 #include <vcl/vclenum.hxx> // for typedef sal_UCS4
37 #include <vcl/devicecoordinate.hxx>
39 #include "fontinstance.hxx"
41 #define MAX_FALLBACK 16
44 class SalGraphics;
45 class PhysicalFontFace;
46 struct GlyphItem;
47 enum class SalLayoutFlags;
48 namespace vcl {
49 class TextLayoutCache;
52 // used for managing runs e.g. for BiDi, glyph and script fallback
53 class VCL_PLUGIN_PUBLIC ImplLayoutRuns
55 private:
56 int mnRunIndex;
57 std::vector<int> maRuns;
59 public:
60 ImplLayoutRuns() { mnRunIndex = 0; maRuns.reserve(8); }
62 void Clear() { maRuns.clear(); }
63 void AddPos( int nCharPos, bool bRTL );
64 void AddRun( int nMinRunPos, int nEndRunPos, bool bRTL );
66 bool IsEmpty() const { return maRuns.empty(); }
67 void ResetPos() { mnRunIndex = 0; }
68 void NextRun() { mnRunIndex += 2; }
69 bool GetRun( int* nMinRunPos, int* nEndRunPos, bool* bRTL ) const;
70 bool GetNextPos( int* nCharPos, bool* bRTL );
71 bool PosIsInRun( int nCharPos ) const;
72 bool PosIsInAnyRun( int nCharPos ) const;
75 class ImplLayoutArgs
77 public:
78 // string related inputs
79 LanguageTag maLanguageTag;
80 SalLayoutFlags mnFlags;
81 const OUString& mrStr;
82 int mnMinCharPos;
83 int mnEndCharPos;
85 // performance hack
86 vcl::TextLayoutCache const* m_pTextLayoutCache;
88 // positioning related inputs
89 const DeviceCoordinate* mpDXArray; // in pixel units
90 DeviceCoordinate mnLayoutWidth; // in pixel units
91 int mnOrientation; // in 0-3600 system
93 // data for bidi and glyph+script fallback
94 ImplLayoutRuns maRuns;
95 ImplLayoutRuns maFallbackRuns;
97 ImplLayoutArgs( const OUString& rStr,
98 int nMinCharPos, int nEndCharPos, SalLayoutFlags nFlags,
99 const LanguageTag& rLanguageTag,
100 vcl::TextLayoutCache const* pLayoutCache);
102 void SetLayoutWidth( DeviceCoordinate nWidth ) { mnLayoutWidth = nWidth; }
103 void SetDXArray( const DeviceCoordinate* pDXArray ) { mpDXArray = pDXArray; }
104 void SetOrientation( int nOrientation ) { mnOrientation = nOrientation; }
106 void ResetPos()
107 { maRuns.ResetPos(); }
108 bool GetNextPos( int* nCharPos, bool* bRTL )
109 { return maRuns.GetNextPos( nCharPos, bRTL ); }
110 bool GetNextRun( int* nMinRunPos, int* nEndRunPos, bool* bRTL );
111 void NeedFallback( int nMinRunPos, int nEndRunPos, bool bRTL )
112 { maFallbackRuns.AddRun( nMinRunPos, nEndRunPos, bRTL ); }
113 // methods used by BiDi and glyph fallback
114 bool NeedFallback() const
115 { return !maFallbackRuns.IsEmpty(); }
116 bool PrepareFallback();
118 private:
119 void AddRun( int nMinCharPos, int nEndCharPos, bool bRTL );
122 // For nice SAL_INFO logging of ImplLayoutArgs values
123 std::ostream &operator <<(std::ostream& s, ImplLayoutArgs const &rArgs);
125 // all positions/widths are in font units
126 // one exception: drawposition is in pixel units
128 // Unfortunately there is little documentation to help implementors of
129 // new classes derived from SalLayout ("layout engines"), and the code
130 // and data structures are far from obvious.
132 // For instance, I *think* the important virtual functions in the
133 // layout engines are called in this order:
135 // * InitFont()
136 // * LayoutText()
137 // * AdjustLayout(), any number of times (but presumably
138 // usually not at all or just once)
139 // * Optionally, DrawText()
141 // Functions that just return information like GetTexWidth() and
142 // FillDXArray() are called after LayoutText() and before DrawText().
144 // Another important questions is which parts of an ImplLayoutArgs can
145 // be changed by callers between LayoutText() and AdjustLayout()
146 // calls. It probably makes sense only if one assumes that the "string
147 // related inputs" part are not changed after LayoutText().
149 // But why use the same ImplLayoutArgs structure as parameter for both
150 // LayoutText() and AdjustLayout() in the first place? And why
151 // duplicate some of the fields in both SalLayout and ImplLayoutArgs
152 // (mnMinCharPos, mnEndCharPos, mnLayoutFlags==mnFlags,
153 // mnOrientation)? Lost in history...
155 class VCL_PLUGIN_PUBLIC SalLayout
157 public:
158 virtual ~SalLayout();
159 // used by upper layers
160 Point& DrawBase() { return maDrawBase; }
161 const Point& DrawBase() const { return maDrawBase; }
162 Point& DrawOffset() { return maDrawOffset; }
163 const Point& DrawOffset() const { return maDrawOffset; }
164 Point GetDrawPosition( const Point& rRelative = Point(0,0) ) const;
166 virtual bool LayoutText( ImplLayoutArgs& ) = 0; // first step of layouting
167 virtual void AdjustLayout( ImplLayoutArgs& ); // adjusting after fallback etc.
168 virtual void InitFont() const {}
169 virtual void DrawText( SalGraphics& ) const = 0;
171 int GetUnitsPerPixel() const { return mnUnitsPerPixel; }
172 int GetOrientation() const { return mnOrientation; }
174 // methods using string indexing
175 virtual sal_Int32 GetTextBreak(DeviceCoordinate nMaxWidth, DeviceCoordinate nCharExtra, int nFactor) const = 0;
176 virtual DeviceCoordinate FillDXArray( DeviceCoordinate* pDXArray ) const = 0;
177 virtual DeviceCoordinate GetTextWidth() const { return FillDXArray( nullptr ); }
178 virtual void GetCaretPositions( int nArraySize, long* pCaretXArray ) const = 0;
179 virtual bool IsKashidaPosValid ( int /*nCharPos*/ ) const { return true; } // i60594
181 // methods using glyph indexing
182 virtual bool GetNextGlyph(const GlyphItem** pGlyph, Point& rPos, int&,
183 const PhysicalFontFace** pFallbackFont = nullptr) const = 0;
184 virtual bool GetOutline( SalGraphics&, basegfx::B2DPolyPolygonVector& ) const;
185 virtual bool GetBoundRect( SalGraphics&, tools::Rectangle& ) const;
187 // used by glyph+font+script fallback
188 virtual void MoveGlyph( int nStart, long nNewXPos ) = 0;
189 virtual void DropGlyph( int nStart ) = 0;
190 virtual void Simplify( bool bIsBase ) = 0;
192 virtual std::shared_ptr<vcl::TextLayoutCache>
193 CreateTextLayoutCache(OUString const&) const;
195 protected:
196 // used by layout engines
197 SalLayout();
199 private:
200 SalLayout( const SalLayout& ) = delete;
201 SalLayout& operator=( const SalLayout& ) = delete;
203 protected:
204 int mnMinCharPos;
205 int mnEndCharPos;
207 int mnUnitsPerPixel;
208 int mnOrientation;
210 mutable Point maDrawOffset;
211 Point maDrawBase;
214 class VCL_PLUGIN_PUBLIC MultiSalLayout final : public SalLayout
216 public:
217 void DrawText(SalGraphics&) const override;
218 sal_Int32 GetTextBreak(DeviceCoordinate nMaxWidth, DeviceCoordinate nCharExtra, int nFactor) const override;
219 DeviceCoordinate FillDXArray(DeviceCoordinate* pDXArray) const override;
220 void GetCaretPositions(int nArraySize, long* pCaretXArray) const override;
221 bool GetNextGlyph(const GlyphItem** pGlyph, Point& rPos, int&,
222 const PhysicalFontFace** pFallbackFont = nullptr) const override;
223 bool GetOutline(SalGraphics&, basegfx::B2DPolyPolygonVector&) const override;
224 bool IsKashidaPosValid(int nCharPos) const override;
226 // used only by OutputDevice::ImplLayout, TODO: make friend
227 explicit MultiSalLayout( std::unique_ptr<SalLayout> pBaseLayout );
228 void AddFallback( std::unique_ptr<SalLayout> pFallbackLayout,
229 ImplLayoutRuns const &, const PhysicalFontFace* pFallbackFont );
230 bool LayoutText(ImplLayoutArgs&) override;
231 void AdjustLayout(ImplLayoutArgs&) override;
232 void InitFont() const override;
234 void SetIncomplete(bool bIncomplete);
236 public:
237 virtual ~MultiSalLayout() override;
239 private:
240 // dummy implementations
241 void MoveGlyph(int, long) override {}
242 void DropGlyph(int) override {}
243 void Simplify(bool) override {}
245 MultiSalLayout( const MultiSalLayout& ) = delete;
246 MultiSalLayout& operator=( const MultiSalLayout& ) = delete;
248 private:
249 std::unique_ptr<SalLayout> mpLayouts[ MAX_FALLBACK ];
250 const PhysicalFontFace* mpFallbackFonts[ MAX_FALLBACK ];
251 ImplLayoutRuns maFallbackRuns[ MAX_FALLBACK ];
252 int mnLevel;
253 bool mbIncomplete;
256 typedef sal_uInt16 sal_GlyphId;
258 struct GlyphItem
260 int mnFlags;
261 int mnCharPos; // index in string
262 int mnCharCount; // number of characters making up this glyph
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, int nCharCount, sal_GlyphId aGlyphId, const Point& rLinearPos,
275 long nFlags, int nOrigWidth, int nXOffset )
276 : mnFlags(nFlags)
277 , mnCharPos(nCharPos)
278 , mnCharCount(nCharCount)
279 , mnOrigWidth(nOrigWidth)
280 , mnNewWidth(nOrigWidth)
281 , mnXOffset(nXOffset)
282 , maGlyphId(aGlyphId)
283 , maLinearPos(rLinearPos)
284 , mnFallbackLevel(0)
287 enum {
288 IS_IN_CLUSTER = 0x001,
289 IS_RTL_GLYPH = 0x002,
290 IS_DIACRITIC = 0x004,
291 IS_VERTICAL = 0x008,
292 IS_SPACING = 0x010,
293 ALLOW_KASHIDA = 0x020,
294 IS_DROPPED = 0x040,
295 IS_CLUSTER_START = 0x080
298 bool IsInCluster() const { return ((mnFlags & IS_IN_CLUSTER) != 0); }
299 bool IsRTLGlyph() const { return ((mnFlags & IS_RTL_GLYPH) != 0); }
300 bool IsDiacritic() const { return ((mnFlags & IS_DIACRITIC) != 0); }
301 bool IsVertical() const { return ((mnFlags & IS_VERTICAL) != 0); }
302 bool IsSpacing() const { return ((mnFlags & IS_SPACING) != 0); }
303 bool AllowKashida() const { return ((mnFlags & ALLOW_KASHIDA) != 0); }
304 bool IsDropped() const { return ((mnFlags & IS_DROPPED) != 0); }
305 bool IsClusterStart() const { return ((mnFlags & IS_CLUSTER_START) != 0); }
308 class VCL_PLUGIN_PUBLIC GenericSalLayout : public SalLayout
310 public:
311 GenericSalLayout(LogicalFontInstance&);
312 ~GenericSalLayout() override;
314 void AdjustLayout(ImplLayoutArgs&) final override;
315 bool LayoutText(ImplLayoutArgs&) final override;
316 void DrawText(SalGraphics&) const final override;
317 std::shared_ptr<vcl::TextLayoutCache> CreateTextLayoutCache(OUString const&) const final override;
319 bool IsKashidaPosValid(int nCharPos) const final override;
321 // used by upper layers
322 DeviceCoordinate GetTextWidth() const final override;
323 DeviceCoordinate FillDXArray(DeviceCoordinate* pDXArray) const final override;
324 sal_Int32 GetTextBreak(DeviceCoordinate nMaxWidth, DeviceCoordinate nCharExtra, int nFactor) const final override;
325 void GetCaretPositions(int nArraySize, long* pCaretXArray) const final override;
327 // used by display layers
328 LogicalFontInstance& GetFont() const { return *mpFont; }
330 bool GetNextGlyph(const GlyphItem** pGlyph, Point& rPos, int&,
331 const PhysicalFontFace** pFallbackFont = nullptr) const final override;
333 protected:
334 // for glyph+font+script fallback
335 void MoveGlyph(int nStart, long nNewXPos) final override;
336 void DropGlyph(int nStart) final override;
337 void Simplify(bool bIsBase) final override;
339 private:
340 GenericSalLayout( const GenericSalLayout& ) = delete;
341 GenericSalLayout& operator=( const GenericSalLayout& ) = delete;
343 void ApplyDXArray(ImplLayoutArgs&);
344 void Justify(DeviceCoordinate nNewWidth);
345 void ApplyAsianKerning(const OUString& rStr);
347 bool GetCharWidths(DeviceCoordinate* pCharWidths) const;
349 void SetNeedFallback(ImplLayoutArgs&, sal_Int32, bool);
351 bool HasVerticalAlternate(sal_UCS4 aChar, sal_UCS4 aNextChar);
353 void ParseFeatures(const OUString& name);
355 LogicalFontInstance* const mpFont;
356 css::uno::Reference<css::i18n::XBreakIterator> mxBreak;
358 std::vector<GlyphItem> m_GlyphItems;
360 OString msLanguage;
361 std::vector<hb_feature_t> maFeatures;
363 hb_set_t* mpVertGlyphs;
364 const bool mbFuzzing;
367 #undef SalGraphics
369 #endif // INCLUDED_VCL_INC_SALLAYOUT_HXX
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */