1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
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
38 typedef unsigned short LanguageType
;
41 class PhysicalFontFace
;
42 enum class SalLayoutFlags
;
44 class TextLayoutCache
;
47 // used for managing runs e.g. for BiDi, glyph and script fallback
48 class VCL_PLUGIN_PUBLIC ImplLayoutRuns
52 std::vector
<int> maRuns
;
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;
73 // string related inputs
74 LanguageTag maLanguageTag
;
75 SalLayoutFlags mnFlags
;
76 const OUString
& mrStr
;
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
;
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
; }
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 nCharPos
, bool bRTL
)
108 { maFallbackRuns
.AddPos( nCharPos
, bRTL
); }
109 void NeedFallback( int nMinRunPos
, int nEndRunPos
, bool bRTL
)
110 { maFallbackRuns
.AddRun( nMinRunPos
, nEndRunPos
, bRTL
); }
111 // methods used by BiDi and glyph fallback
112 bool NeedFallback() const
113 { return !maFallbackRuns
.IsEmpty(); }
114 bool PrepareFallback();
117 void AddRun( int nMinCharPos
, int nEndCharPos
, bool bRTL
);
120 // For nice SAL_INFO logging of ImplLayoutArgs values
121 std::ostream
&operator <<(std::ostream
& s
, ImplLayoutArgs
&rArgs
);
123 // helper functions often used with ImplLayoutArgs
124 int GetVerticalFlags( sal_UCS4
);
126 // all positions/widths are in font units
127 // one exception: drawposition is in pixel units
129 // Unfortunately there is little documentation to help implementors of
130 // new classes derived from SalLayout ("layout engines"), and the code
131 // and data structures are far from obvious.
133 // For instance, I *think* the important virtual functions in the
134 // layout engines are called in this order:
138 // * AdjustLayout(), any number of times (but presumably
139 // usually not at all or just once)
140 // * Optionally, DrawText()
142 // Functions that just return information like GetTexWidth() and
143 // FillDXArray() are called after LayoutText() and before DrawText().
145 // Another important questions is which parts of an ImplLayoutArgs can
146 // be changed by callers between LayoutText() and AdjustLayout()
147 // calls. It probably makes sense only if one assumes that the "string
148 // related inputs" part are not changed after LayoutText().
150 // But why use the same ImplLayoutArgs structure as parameter for both
151 // LayoutText() and AdjustLayout() in the first place? And why
152 // duplicate some of the fields in both SalLayout and ImplLayoutArgs
153 // (mnMinCharPos, mnEndCharPos, mnLayoutFlags==mnFlags,
154 // mnOrientation)? Lost in history...
156 class VCL_PLUGIN_PUBLIC 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 int GetNextGlyphs( int nLen
, sal_GlyphId
* pGlyphIdAry
, Point
& rPos
, int&,
183 DeviceCoordinate
* pGlyphAdvAry
= nullptr, int* pCharPosAry
= nullptr,
184 const PhysicalFontFace
** pFallbackFonts
= nullptr ) const = 0;
185 virtual bool GetOutline( SalGraphics
&, basegfx::B2DPolyPolygonVector
& ) const;
186 virtual bool GetBoundRect( SalGraphics
&, Rectangle
& ) const;
188 static bool IsSpacingGlyph( sal_GlyphId
);
190 // reference counting
191 void Release() const;
193 // used by glyph+font+script fallback
194 virtual void MoveGlyph( int nStart
, long nNewXPos
) = 0;
195 virtual void DropGlyph( int nStart
) = 0;
196 virtual void Simplify( bool bIsBase
) = 0;
197 virtual void DisableGlyphInjection( bool /*bDisable*/ ) {}
199 virtual std::shared_ptr
<vcl::TextLayoutCache
>
200 CreateTextLayoutCache(OUString
const&) const;
202 static bool UseCommonLayout();
205 // used by layout engines
207 virtual ~SalLayout();
209 // used by layout layers
210 void SetUnitsPerPixel( int n
) { mnUnitsPerPixel
= n
; }
211 void SetOrientation( int nOrientation
) // in 0-3600 system
212 { mnOrientation
= nOrientation
; }
214 static int CalcAsianKerning( sal_UCS4
, bool bLeft
, bool bVertical
);
217 SalLayout( const SalLayout
& ) = delete;
218 SalLayout
& operator=( const SalLayout
& ) = delete;
223 SalLayoutFlags mnLayoutFlags
;
228 mutable int mnRefCount
;
229 mutable Point maDrawOffset
;
233 class VCL_PLUGIN_PUBLIC MultiSalLayout
: public SalLayout
236 virtual void DrawText( SalGraphics
& ) const override
;
237 virtual sal_Int32
GetTextBreak(DeviceCoordinate nMaxWidth
, DeviceCoordinate nCharExtra
, int nFactor
) const override
;
238 virtual DeviceCoordinate
FillDXArray( DeviceCoordinate
* pDXArray
) const override
;
239 virtual void GetCaretPositions( int nArraySize
, long* pCaretXArray
) const override
;
240 virtual int GetNextGlyphs( int nLen
, sal_GlyphId
* pGlyphIdxAry
, Point
& rPos
,
241 int&, DeviceCoordinate
* pGlyphAdvAry
= nullptr, int* pCharPosAry
= nullptr,
242 const PhysicalFontFace
** pFallbackFonts
= nullptr ) const override
;
243 virtual bool GetOutline( SalGraphics
&, basegfx::B2DPolyPolygonVector
& ) const override
;
244 virtual bool IsKashidaPosValid(int nCharPos
) const override
;
246 // used only by OutputDevice::ImplLayout, TODO: make friend
247 explicit MultiSalLayout( SalLayout
& rBaseLayout
);
248 void AddFallback( SalLayout
& rFallbackLayout
,
249 ImplLayoutRuns
&, const PhysicalFontFace
* pFallbackFont
);
250 virtual bool LayoutText( ImplLayoutArgs
& ) override
;
251 virtual void AdjustLayout( ImplLayoutArgs
& ) override
;
252 virtual void InitFont() const override
;
254 void SetIncomplete(bool bIncomplete
);
257 virtual ~MultiSalLayout() override
;
260 // dummy implementations
261 virtual void MoveGlyph( int, long ) override
{}
262 virtual void DropGlyph( int ) override
{}
263 virtual void Simplify( bool ) override
{}
265 MultiSalLayout( const MultiSalLayout
& ) = delete;
266 MultiSalLayout
& operator=( const MultiSalLayout
& ) = delete;
269 SalLayout
* mpLayouts
[ MAX_FALLBACK
];
270 const PhysicalFontFace
* mpFallbackFonts
[ MAX_FALLBACK
];
271 ImplLayoutRuns maFallbackRuns
[ MAX_FALLBACK
];
279 int mnCharPos
; // index in string
281 int mnOrigWidth
; // original glyph width
282 int mnNewWidth
; // width after adjustments
285 sal_GlyphId maGlyphId
;
286 Point maLinearPos
; // absolute position of non rotated string
298 GlyphItem( int nCharPos
, sal_GlyphId aGlyphId
, const Point
& rLinearPos
,
299 long nFlags
, int nOrigWidth
)
300 : mnFlags(nFlags
), mnCharPos(nCharPos
),
301 mnOrigWidth(nOrigWidth
), mnNewWidth(nOrigWidth
),
303 maGlyphId(aGlyphId
), maLinearPos(rLinearPos
)
306 GlyphItem( int nCharPos
, sal_GlyphId aGlyphId
, const Point
& rLinearPos
,
307 long nFlags
, int nOrigWidth
, int nXOffset
)
308 : mnFlags(nFlags
), mnCharPos(nCharPos
),
309 mnOrigWidth(nOrigWidth
), mnNewWidth(nOrigWidth
),
311 maGlyphId(aGlyphId
), maLinearPos(rLinearPos
)
315 IS_IN_CLUSTER
= 0x001,
316 IS_RTL_GLYPH
= 0x002,
317 IS_DIACRITIC
= 0x004,
318 ALLOW_KASHIDA
= 0X010
321 bool IsClusterStart() const { return ((mnFlags
& IS_IN_CLUSTER
) == 0); }
322 bool IsRTLGlyph() const { return ((mnFlags
& IS_RTL_GLYPH
) != 0); }
323 bool IsDiacritic() const { return ((mnFlags
& IS_DIACRITIC
) != 0); }
324 bool AllowKashida() const { return ((mnFlags
& ALLOW_KASHIDA
) != 0); }
327 typedef std::list
<GlyphItem
> GlyphList
;
329 class VCL_PLUGIN_PUBLIC GenericSalLayout
: public SalLayout
332 // used by layout engines
333 void AppendGlyph( const GlyphItem
& );
334 void Reserve(int size
) { m_GlyphItems
.reserve(size
+ 1); }
335 virtual void AdjustLayout( ImplLayoutArgs
& ) override
;
336 virtual void ApplyDXArray( ImplLayoutArgs
& );
337 void Justify( DeviceCoordinate nNewWidth
);
338 void KashidaJustify( long nIndex
, int nWidth
);
339 void ApplyAsianKerning(const OUString
& rStr
);
340 void SortGlyphItems();
342 // used by upper layers
343 virtual DeviceCoordinate
GetTextWidth() const override
;
344 virtual DeviceCoordinate
FillDXArray( DeviceCoordinate
* pDXArray
) const override
;
345 virtual sal_Int32
GetTextBreak(DeviceCoordinate nMaxWidth
, DeviceCoordinate nCharExtra
, int nFactor
) const override
;
346 virtual void GetCaretPositions( int nArraySize
, long* pCaretXArray
) const override
;
348 // used by display layers
349 virtual int GetNextGlyphs( int nLen
, sal_GlyphId
* pGlyphIdxAry
, Point
& rPos
, int&,
350 DeviceCoordinate
* pGlyphAdvAry
= nullptr, int* pCharPosAry
= nullptr,
351 const PhysicalFontFace
** pFallbackFonts
= nullptr ) const override
;
355 virtual ~GenericSalLayout() override
;
357 // for glyph+font+script fallback
358 virtual void MoveGlyph( int nStart
, long nNewXPos
) override
;
359 virtual void DropGlyph( int nStart
) override
;
360 virtual void Simplify( bool bIsBase
) override
;
362 virtual bool GetCharWidths( DeviceCoordinate
* pCharWidths
) const;
364 std::vector
<GlyphItem
> m_GlyphItems
;
367 mutable Point maBasePoint
;
369 GenericSalLayout( const GenericSalLayout
& ) = delete;
370 GenericSalLayout
& operator=( const GenericSalLayout
& ) = delete;
375 #endif // INCLUDED_VCL_INC_SALLAYOUT_HXX
377 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */