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
33 #ifndef _TOOLS_LANG_HXX
34 typedef unsigned short LanguageType
;
38 #include "salglyphid.hxx"
41 class PhysicalFontFace
;
44 #define SAL_LAYOUT_BIDI_RTL 0x0001
45 #define SAL_LAYOUT_BIDI_STRONG 0x0002
46 #define SAL_LAYOUT_RIGHT_ALIGN 0x0004
47 #define SAL_LAYOUT_KERNING_PAIRS 0x0010
48 #define SAL_LAYOUT_KERNING_ASIAN 0x0020
49 #define SAL_LAYOUT_VERTICAL 0x0040
50 #define SAL_LAYOUT_COMPLEX_DISABLED 0x0100
51 #define SAL_LAYOUT_ENABLE_LIGATURES 0x0200
52 #define SAL_LAYOUT_SUBSTITUTE_DIGITS 0x0400
53 #define SAL_LAYOUT_KASHIDA_JUSTIFICATON 0x0800
54 #define SAL_LAYOUT_DISABLE_GLYPH_PROCESSING 0x1000
55 #define SAL_LAYOUT_FOR_FALLBACK 0x2000
57 // used for managing runs e.g. for BiDi, glyph and script fallback
58 class VCL_PLUGIN_PUBLIC ImplLayoutRuns
62 std::vector
<int> maRuns
;
65 ImplLayoutRuns() { mnRunIndex
= 0; maRuns
.reserve(8); }
67 void Clear() { maRuns
.clear(); }
68 bool AddPos( int nCharPos
, bool bRTL
);
69 bool AddRun( int nMinRunPos
, int nEndRunPos
, bool bRTL
);
71 bool IsEmpty() const { return maRuns
.empty(); }
72 void ResetPos() { mnRunIndex
= 0; }
73 void NextRun() { mnRunIndex
+= 2; }
74 bool GetRun( int* nMinRunPos
, int* nEndRunPos
, bool* bRTL
) const;
75 bool GetNextPos( int* nCharPos
, bool* bRTL
);
76 bool PosIsInRun( int nCharPos
) const;
77 bool PosIsInAnyRun( int nCharPos
) const;
83 // string related inputs
84 LanguageTag maLanguageTag
;
89 const sal_Unicode
* mpStr
;
91 // positioning related inputs
92 const sal_Int32
* mpDXArray
; // in pixel units
93 long mnLayoutWidth
; // in pixel units
94 int mnOrientation
; // in 0-3600 system
96 // data for bidi and glyph+script fallback
97 ImplLayoutRuns maRuns
;
98 ImplLayoutRuns maFallbackRuns
;
101 ImplLayoutArgs( const sal_Unicode
* pStr
, int nLength
,
102 int nMinCharPos
, int nEndCharPos
, int nFlags
, const LanguageTag
& rLanguageTag
);
104 void SetLayoutWidth( long nWidth
) { mnLayoutWidth
= nWidth
; }
105 void SetDXArray( const sal_Int32
* pDXArray
) { mpDXArray
= pDXArray
; }
106 void SetOrientation( int nOrientation
) { mnOrientation
= nOrientation
; }
109 { maRuns
.ResetPos(); }
110 bool GetNextPos( int* nCharPos
, bool* bRTL
)
111 { return maRuns
.GetNextPos( nCharPos
, bRTL
); }
112 bool GetNextRun( int* nMinRunPos
, int* nEndRunPos
, bool* bRTL
);
113 bool NeedFallback( int nCharPos
, bool bRTL
)
114 { return maFallbackRuns
.AddPos( nCharPos
, bRTL
); }
115 bool NeedFallback( int nMinRunPos
, int nEndRunPos
, bool bRTL
)
116 { return maFallbackRuns
.AddRun( nMinRunPos
, nEndRunPos
, bRTL
); }
117 // methods used by BiDi and glyph fallback
118 bool NeedFallback() const
119 { return !maFallbackRuns
.IsEmpty(); }
120 bool PrepareFallback();
123 void AddRun( int nMinCharPos
, int nEndCharPos
, bool bRTL
);
126 // For nice SAL_INFO logging of ImplLayoutArgs values
127 std::ostream
&operator <<(std::ostream
& s
, ImplLayoutArgs
&rArgs
);
129 // helper functions often used with ImplLayoutArgs
130 bool IsDiacritic( sal_UCS4
);
131 int GetVerticalFlags( sal_UCS4
);
132 sal_UCS4
GetVerticalChar( sal_UCS4
);
134 // all positions/widths are in font units
135 // one exception: drawposition is in pixel units
137 // Unfortunately there is little documentation to help implementors of
138 // new classes derived from SalLayout ("layout engines"), and the code
139 // and data structures are far from obvious.
141 // For instance, I *think* the important virtual functions in the
142 // layout engines are called in this order:
146 // * AdjustLayout(), any number of times (but presumably
147 // usually not at all or just once)
148 // * Optionally, DrawText()
150 // Functions that just return information like GetTexWidth() and
151 // FillDXArray() are called after LayoutText() and before DrawText().
153 // Another important questions is which parts of an ImplLayoutArgs can
154 // be changed by callers between LayoutText() and AdjustLayout()
155 // calls. It probably makes sense only if one assumes that the "string
156 // related inputs" part are not changed after LayoutText().
158 // But why use the same ImplLayoutArgs structure as parameter for both
159 // LayoutText() and AdjustLayout() in the first place? And why
160 // duplicate some of the fields in both SalLayout and ImplLayoutArgs
161 // (mnMinCharPos, mnEndCharPos, mnLayoutFlags==mnFlags,
162 // mnOrientation)? Lost in history...
164 class VCL_PLUGIN_PUBLIC SalLayout
167 // used by upper layers
168 Point
& DrawBase() { return maDrawBase
; }
169 const Point
& DrawBase() const { return maDrawBase
; }
170 Point
& DrawOffset() { return maDrawOffset
; }
171 const Point
& DrawOffset() const { return maDrawOffset
; }
172 Point
GetDrawPosition( const Point
& rRelative
= Point(0,0) ) const;
174 virtual bool LayoutText( ImplLayoutArgs
& ) = 0; // first step of layouting
175 virtual void AdjustLayout( ImplLayoutArgs
& ); // adjusting after fallback etc.
176 virtual void InitFont() const {}
177 virtual void DrawText( SalGraphics
& ) const = 0;
178 virtual bool DrawTextSpecial( SalGraphics
& /* rGraphics */, sal_uInt32
/* flags */ ) const { return false; }
179 #define DRAWTEXT_F_OUTLINE ((sal_uInt32)(1<<0))
181 int GetUnitsPerPixel() const { return mnUnitsPerPixel
; }
182 int GetOrientation() const { return mnOrientation
; }
184 // methods using string indexing
185 virtual sal_Int32
GetTextBreak(long nMaxWidth
, long nCharExtra
=0, int nFactor
=1) const = 0;
186 virtual long FillDXArray( sal_Int32
* pDXArray
) const = 0;
187 virtual long GetTextWidth() const { return FillDXArray( NULL
); }
188 virtual void GetCaretPositions( int nArraySize
, sal_Int32
* pCaretXArray
) const = 0;
189 virtual bool IsKashidaPosValid ( int /*nCharPos*/ ) const { return true; } // i60594
191 // methods using glyph indexing
192 virtual int GetNextGlyphs( int nLen
, sal_GlyphId
* pGlyphIdAry
, Point
& rPos
, int&,
193 sal_Int32
* pGlyphAdvAry
= NULL
, int* pCharPosAry
= NULL
,
194 const PhysicalFontFace
** pFallbackFonts
= NULL
) const = 0;
195 virtual bool GetOutline( SalGraphics
&, ::basegfx::B2DPolyPolygonVector
& ) const;
196 virtual bool GetBoundRect( SalGraphics
&, Rectangle
& ) const;
198 virtual bool IsSpacingGlyph( sal_GlyphId
) const;
200 // reference counting
201 void Release() const;
203 // used by glyph+font+script fallback
204 virtual void MoveGlyph( int nStart
, long nNewXPos
) = 0;
205 virtual void DropGlyph( int nStart
) = 0;
206 virtual void Simplify( bool bIsBase
) = 0;
207 virtual void DisableGlyphInjection( bool /*bDisable*/ ) {}
210 // used by layout engines
212 virtual ~SalLayout();
214 // used by layout layers
215 void SetUnitsPerPixel( int n
) { mnUnitsPerPixel
= n
; }
216 void SetOrientation( int nOrientation
) // in 0-3600 system
217 { mnOrientation
= nOrientation
; }
219 static int CalcAsianKerning( sal_UCS4
, bool bLeft
, bool bVertical
);
222 // enforce proper copy semantic
223 SAL_DLLPRIVATE
SalLayout( const SalLayout
& );
224 SAL_DLLPRIVATE SalLayout
& operator=( const SalLayout
& );
234 mutable int mnRefCount
;
235 mutable Point maDrawOffset
;
239 class VCL_PLUGIN_PUBLIC MultiSalLayout
: public SalLayout
242 virtual void DrawText( SalGraphics
& ) const SAL_OVERRIDE
;
243 virtual sal_Int32
GetTextBreak(long nMaxWidth
, long nCharExtra
, int nFactor
) const SAL_OVERRIDE
;
244 virtual long FillDXArray( sal_Int32
* pDXArray
) const SAL_OVERRIDE
;
245 virtual void GetCaretPositions( int nArraySize
, sal_Int32
* pCaretXArray
) const SAL_OVERRIDE
;
246 virtual int GetNextGlyphs( int nLen
, sal_GlyphId
* pGlyphIdxAry
, Point
& rPos
,
247 int&, sal_Int32
* pGlyphAdvAry
, int* pCharPosAry
,
248 const PhysicalFontFace
** pFallbackFonts
) const SAL_OVERRIDE
;
249 virtual bool GetOutline( SalGraphics
&, ::basegfx::B2DPolyPolygonVector
& ) const SAL_OVERRIDE
;
251 // used only by OutputDevice::ImplLayout, TODO: make friend
252 explicit MultiSalLayout( SalLayout
& rBaseLayout
,
253 const PhysicalFontFace
* pBaseFont
= NULL
);
254 virtual bool AddFallback( SalLayout
& rFallbackLayout
,
255 ImplLayoutRuns
&, const PhysicalFontFace
* pFallbackFont
);
256 virtual bool LayoutText( ImplLayoutArgs
& ) SAL_OVERRIDE
;
257 virtual void AdjustLayout( ImplLayoutArgs
& ) SAL_OVERRIDE
;
258 virtual void InitFont() const SAL_OVERRIDE
;
260 void SetInComplete(bool bInComplete
= true);
263 virtual ~MultiSalLayout();
266 // dummy implementations
267 virtual void MoveGlyph( int, long ) SAL_OVERRIDE
{}
268 virtual void DropGlyph( int ) SAL_OVERRIDE
{}
269 virtual void Simplify( bool ) SAL_OVERRIDE
{}
271 // enforce proper copy semantic
272 SAL_DLLPRIVATE
MultiSalLayout( const MultiSalLayout
& );
273 SAL_DLLPRIVATE MultiSalLayout
& operator=( const MultiSalLayout
& );
276 SalLayout
* mpLayouts
[ MAX_FALLBACK
];
277 const PhysicalFontFace
* mpFallbackFonts
[ MAX_FALLBACK
];
278 ImplLayoutRuns maFallbackRuns
[ MAX_FALLBACK
];
286 int mnCharPos
; // index in string
287 int mnOrigWidth
; // original glyph width
288 int mnNewWidth
; // width after adjustments
290 sal_GlyphId maGlyphId
;
291 Point maLinearPos
; // absolute position of non rotated string
303 GlyphItem( int nCharPos
, sal_GlyphId aGlyphId
, const Point
& rLinearPos
,
304 long nFlags
, int nOrigWidth
)
305 : mnFlags(nFlags
), mnCharPos(nCharPos
),
306 mnOrigWidth(nOrigWidth
), mnNewWidth(nOrigWidth
),
308 maGlyphId(aGlyphId
), maLinearPos(rLinearPos
)
311 GlyphItem( int nCharPos
, sal_GlyphId aGlyphId
, const Point
& rLinearPos
,
312 long nFlags
, int nOrigWidth
, int nXOffset
)
313 : mnFlags(nFlags
), mnCharPos(nCharPos
),
314 mnOrigWidth(nOrigWidth
), mnNewWidth(nOrigWidth
),
316 maGlyphId(aGlyphId
), maLinearPos(rLinearPos
)
319 enum{ FALLBACK_MASK
=0xFF, IS_IN_CLUSTER
=0x100, IS_RTL_GLYPH
=0x200, IS_DIACRITIC
=0x400 };
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); }
326 typedef std::list
<GlyphItem
> GlyphList
;
327 typedef std::vector
<GlyphItem
> GlyphVector
;
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
& ) SAL_OVERRIDE
;
336 virtual void ApplyDXArray( ImplLayoutArgs
& );
337 virtual void Justify( long nNewWidth
);
338 void KashidaJustify( long nIndex
, int nWidth
);
339 void ApplyAsianKerning( const sal_Unicode
*, int nLength
);
340 void SortGlyphItems();
342 // used by upper layers
343 virtual long GetTextWidth() const SAL_OVERRIDE
;
344 virtual long FillDXArray( sal_Int32
* pDXArray
) const SAL_OVERRIDE
;
345 virtual sal_Int32
GetTextBreak(long nMaxWidth
, long nCharExtra
, int nFactor
) const SAL_OVERRIDE
;
346 virtual void GetCaretPositions( int nArraySize
, sal_Int32
* pCaretXArray
) const SAL_OVERRIDE
;
348 // used by display layers
349 virtual int GetNextGlyphs( int nLen
, sal_GlyphId
* pGlyphIdxAry
, Point
& rPos
, int&,
350 sal_Int32
* pGlyphAdvAry
= NULL
, int* pCharPosAry
= NULL
,
351 const PhysicalFontFace
** pFallbackFonts
= NULL
) const SAL_OVERRIDE
;
355 virtual ~GenericSalLayout();
357 // for glyph+font+script fallback
358 virtual void MoveGlyph( int nStart
, long nNewXPos
) SAL_OVERRIDE
;
359 virtual void DropGlyph( int nStart
) SAL_OVERRIDE
;
360 virtual void Simplify( bool bIsBase
) SAL_OVERRIDE
;
362 bool GetCharWidths( sal_Int32
* pCharWidths
) const;
364 GlyphVector m_GlyphItems
;
367 mutable Point maBasePoint
;
369 // enforce proper copy semantic
370 SAL_DLLPRIVATE
GenericSalLayout( const GenericSalLayout
& );
371 SAL_DLLPRIVATE GenericSalLayout
& operator=( const GenericSalLayout
& );
376 #endif // INCLUDED_VCL_INC_SALLAYOUT_HXX
378 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */