Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_VCL_INC_SALLAYOUT_HXX
21 : #define INCLUDED_VCL_INC_SALLAYOUT_HXX
22 :
23 : #include <iostream>
24 : #include <list>
25 : #include <vector>
26 :
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>
34 :
35 : #ifndef _TOOLS_LANG_HXX
36 : typedef unsigned short LanguageType;
37 : #endif
38 :
39 : #include "magic.h"
40 : #include "salglyphid.hxx"
41 :
42 : class SalGraphics;
43 : class PhysicalFontFace;
44 : enum class SalLayoutFlags;
45 : namespace vcl {
46 : class TextLayoutCache;
47 : }
48 :
49 : // used for managing runs e.g. for BiDi, glyph and script fallback
50 3243721 : class VCL_PLUGIN_PUBLIC ImplLayoutRuns
51 : {
52 : private:
53 : int mnRunIndex;
54 : std::vector<int> maRuns;
55 :
56 : public:
57 3213317 : ImplLayoutRuns() { mnRunIndex = 0; maRuns.reserve(8); }
58 :
59 5658 : void Clear() { maRuns.clear(); }
60 : bool AddPos( int nCharPos, bool bRTL );
61 : bool AddRun( int nMinRunPos, int nEndRunPos, bool bRTL );
62 :
63 1534708 : bool IsEmpty() const { return maRuns.empty(); }
64 1787066 : void ResetPos() { mnRunIndex = 0; }
65 7116574 : 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;
70 : };
71 :
72 1589824 : class ImplLayoutArgs
73 : {
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;
82 :
83 : // performance hack
84 : vcl::TextLayoutCache const* m_pTextLayoutCache;
85 :
86 : // positioning related inputs
87 : const DeviceCoordinate* mpDXArray; // in pixel units
88 : DeviceCoordinate mnLayoutWidth; // in pixel units
89 : int mnOrientation; // in 0-3600 system
90 :
91 : // data for bidi and glyph+script fallback
92 : ImplLayoutRuns maRuns;
93 : ImplLayoutRuns maFallbackRuns;
94 :
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);
100 :
101 1584868 : void SetLayoutWidth( DeviceCoordinate nWidth ) { mnLayoutWidth = nWidth; }
102 1584868 : void SetDXArray( const DeviceCoordinate* pDXArray ) { mpDXArray = pDXArray; }
103 1584868 : void SetOrientation( int nOrientation ) { mnOrientation = nOrientation; }
104 :
105 10614 : void ResetPos()
106 10614 : { maRuns.ResetPos(); }
107 1084234 : bool GetNextPos( int* nCharPos, bool* bRTL )
108 1084234 : { return maRuns.GetNextPos( nCharPos, bRTL ); }
109 : bool GetNextRun( int* nMinRunPos, int* nEndRunPos, bool* bRTL );
110 0 : bool NeedFallback( int nCharPos, bool bRTL )
111 0 : { return maFallbackRuns.AddPos( nCharPos, bRTL ); }
112 63946 : bool NeedFallback( int nMinRunPos, int nEndRunPos, bool bRTL )
113 63946 : { return maFallbackRuns.AddRun( nMinRunPos, nEndRunPos, bRTL ); }
114 : // methods used by BiDi and glyph fallback
115 1529050 : bool NeedFallback() const
116 1529050 : { return !maFallbackRuns.IsEmpty(); }
117 : bool PrepareFallback();
118 :
119 : protected:
120 : void AddRun( int nMinCharPos, int nEndCharPos, bool bRTL );
121 : };
122 :
123 : // For nice SAL_INFO logging of ImplLayoutArgs values
124 : std::ostream &operator <<(std::ostream& s, ImplLayoutArgs &rArgs);
125 :
126 : // helper functions often used with ImplLayoutArgs
127 : bool IsDiacritic( sal_UCS4 );
128 : int GetVerticalFlags( sal_UCS4 );
129 :
130 : // all positions/widths are in font units
131 : // one exception: drawposition is in pixel units
132 :
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.
136 :
137 : // For instance, I *think* the important virtual functions in the
138 : // layout engines are called in this order:
139 :
140 : // * InitFont()
141 : // * LayoutText()
142 : // * AdjustLayout(), any number of times (but presumably
143 : // usually not at all or just once)
144 : // * Optionally, DrawText()
145 :
146 : // Functions that just return information like GetTexWidth() and
147 : // FillDXArray() are called after LayoutText() and before DrawText().
148 :
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().
153 :
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...
159 :
160 : class VCL_PLUGIN_PUBLIC SalLayout
161 : {
162 : public:
163 : // used by upper layers
164 2323224 : Point& DrawBase() { return maDrawBase; }
165 11301 : const Point& DrawBase() const { return maDrawBase; }
166 11377 : Point& DrawOffset() { return maDrawOffset; }
167 : const Point& DrawOffset() const { return maDrawOffset; }
168 : Point GetDrawPosition( const Point& rRelative = Point(0,0) ) const;
169 :
170 : virtual bool LayoutText( ImplLayoutArgs& ) = 0; // first step of layouting
171 : virtual void AdjustLayout( ImplLayoutArgs& ); // adjusting after fallback etc.
172 3200 : virtual void InitFont() const {}
173 : virtual void DrawText( SalGraphics& ) const = 0;
174 285 : virtual bool DrawTextSpecial( SalGraphics& /* rGraphics */, sal_uInt32 /* flags */ ) const { return false; }
175 : #define DRAWTEXT_F_OUTLINE ((sal_uInt32)(1<<0))
176 :
177 1389345 : int GetUnitsPerPixel() const { return mnUnitsPerPixel; }
178 3 : int GetOrientation() const { return mnOrientation; }
179 :
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 6 : virtual DeviceCoordinate GetTextWidth() const { return FillDXArray( NULL ); }
184 : virtual void GetCaretPositions( int nArraySize, long* pCaretXArray ) const = 0;
185 0 : virtual bool IsKashidaPosValid ( int /*nCharPos*/ ) const { return true; } // i60594
186 :
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;
193 :
194 : static bool IsSpacingGlyph( sal_GlyphId );
195 :
196 : // reference counting
197 : void Release() const;
198 :
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 9643 : virtual void DisableGlyphInjection( bool /*bDisable*/ ) {}
204 :
205 : virtual std::shared_ptr<vcl::TextLayoutCache>
206 : CreateTextLayoutCache(OUString const&) const;
207 :
208 : protected:
209 : // used by layout engines
210 : SalLayout();
211 : virtual ~SalLayout();
212 :
213 : // used by layout layers
214 45 : void SetUnitsPerPixel( int n ) { mnUnitsPerPixel = n; }
215 45 : void SetOrientation( int nOrientation ) // in 0-3600 system
216 45 : { mnOrientation = nOrientation; }
217 :
218 : static int CalcAsianKerning( sal_UCS4, bool bLeft, bool bVertical );
219 :
220 : private:
221 : SalLayout( const SalLayout& ) SAL_DELETED_FUNCTION;
222 : SalLayout& operator=( const SalLayout& ) SAL_DELETED_FUNCTION;
223 :
224 : protected:
225 : int mnMinCharPos;
226 : int mnEndCharPos;
227 : SalLayoutFlags mnLayoutFlags;
228 :
229 : int mnUnitsPerPixel;
230 : int mnOrientation;
231 :
232 : mutable int mnRefCount;
233 : mutable Point maDrawOffset;
234 : Point maDrawBase;
235 : };
236 :
237 : class VCL_PLUGIN_PUBLIC MultiSalLayout : public SalLayout
238 : {
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;
248 :
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;
257 :
258 : void SetInComplete(bool bInComplete = true);
259 :
260 : protected:
261 : virtual ~MultiSalLayout();
262 :
263 : private:
264 : // dummy implementations
265 0 : virtual void MoveGlyph( int, long ) SAL_OVERRIDE {}
266 0 : virtual void DropGlyph( int ) SAL_OVERRIDE {}
267 0 : virtual void Simplify( bool ) SAL_OVERRIDE {}
268 :
269 : MultiSalLayout( const MultiSalLayout& ) SAL_DELETED_FUNCTION;
270 : MultiSalLayout& operator=( const MultiSalLayout& ) SAL_DELETED_FUNCTION;
271 :
272 : private:
273 : SalLayout* mpLayouts[ MAX_FALLBACK ];
274 : const PhysicalFontFace* mpFallbackFonts[ MAX_FALLBACK ];
275 : ImplLayoutRuns maFallbackRuns[ MAX_FALLBACK ];
276 : int mnLevel;
277 : bool mbInComplete;
278 : };
279 :
280 : struct GlyphItem
281 : {
282 : int mnFlags;
283 : int mnCharPos; // index in string
284 :
285 : int mnOrigWidth; // original glyph width
286 : int mnNewWidth; // width after adjustments
287 : int mnXOffset;
288 :
289 : int mnYOffset;
290 :
291 : sal_GlyphId maGlyphId;
292 : Point maLinearPos; // absolute position of non rotated string
293 :
294 : public:
295 45 : GlyphItem()
296 : : mnFlags(0)
297 : , mnCharPos(0)
298 : , mnOrigWidth(0)
299 : , mnNewWidth(0)
300 : , mnXOffset(0)
301 : , mnYOffset(0)
302 45 : , maGlyphId(0)
303 45 : {}
304 :
305 2086 : 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 2086 : maGlyphId(aGlyphId), maLinearPos(rLinearPos)
312 2086 : {}
313 :
314 24816148 : 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 24816148 : maGlyphId(aGlyphId), maLinearPos(rLinearPos)
321 24816148 : {}
322 :
323 : enum{ FALLBACK_MASK=0xFF, IS_IN_CLUSTER=0x100, IS_RTL_GLYPH=0x200, IS_DIACRITIC=0x400 };
324 :
325 38420805 : bool IsClusterStart() const { return ((mnFlags & IS_IN_CLUSTER) == 0); }
326 1161374 : bool IsRTLGlyph() const { return ((mnFlags & IS_RTL_GLYPH) != 0); }
327 24831324 : bool IsDiacritic() const { return ((mnFlags & IS_DIACRITIC) != 0); }
328 : };
329 :
330 : typedef std::list<GlyphItem> GlyphList;
331 : typedef std::vector<GlyphItem> GlyphVector;
332 :
333 : class VCL_PLUGIN_PUBLIC GenericSalLayout : public SalLayout
334 : {
335 : public:
336 : // used by layout engines
337 : void AppendGlyph( const GlyphItem& );
338 1531610 : 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();
345 :
346 : // used by upper layers
347 : virtual DeviceCoordinate GetTextWidth() const SAL_OVERRIDE;
348 : 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;
352 :
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;
357 :
358 : protected:
359 : GenericSalLayout();
360 : virtual ~GenericSalLayout();
361 :
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;
366 :
367 : bool GetCharWidths( DeviceCoordinate* pCharWidths ) const;
368 :
369 : GlyphVector m_GlyphItems;
370 :
371 : private:
372 : mutable Point maBasePoint;
373 :
374 : GenericSalLayout( const GenericSalLayout& ) SAL_DELETED_FUNCTION;
375 : GenericSalLayout& operator=( const GenericSalLayout& ) SAL_DELETED_FUNCTION;
376 : };
377 :
378 : #undef SalGraphics
379 :
380 : #endif // INCLUDED_VCL_INC_SALLAYOUT_HXX
381 :
382 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|