xref: /aoo41x/main/vcl/inc/sallayout.hxx (revision ddde725d)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _SV_SALLAYOUT_HXX
29 #define _SV_SALLAYOUT_HXX
30 
31 #include <tools/gen.hxx>
32 #include <vector>
33 #include <basegfx/polygon/b2dpolypolygon.hxx>
34 
35 #ifndef _TOOLS_LANG_HXX
36 typedef unsigned short LanguageType;
37 #endif
38 
39 #include <vector>
40 #include <list>
41 #include <vcl/dllapi.h>
42 
43 // for typedef sal_UCS4
44 #include <vcl/vclenum.hxx>
45 
46 class SalGraphics;
47 class ImplFontData;
48 
49 #define MAX_FALLBACK 16
50 
51 // ----------------
52 // - LayoutOption -
53 // ----------------
54 
55 #define SAL_LAYOUT_BIDI_RTL                 0x0001
56 #define SAL_LAYOUT_BIDI_STRONG              0x0002
57 #define SAL_LAYOUT_RIGHT_ALIGN              0x0004
58 #define SAL_LAYOUT_KERNING_PAIRS            0x0010
59 #define SAL_LAYOUT_KERNING_ASIAN            0x0020
60 #define SAL_LAYOUT_VERTICAL                 0x0040
61 #define SAL_LAYOUT_COMPLEX_DISABLED         0x0100
62 #define SAL_LAYOUT_ENABLE_LIGATURES         0x0200
63 #define SAL_LAYOUT_SUBSTITUTE_DIGITS        0x0400
64 #define SAL_LAYOUT_KASHIDA_JUSTIFICATON     0x0800
65 #define SAL_LAYOUT_DISABLE_GLYPH_PROCESSING 0x1000
66 #define SAL_LAYOUT_FOR_FALLBACK             0x2000
67 
68 // -----------------
69 
70 // used for managing runs e.g. for BiDi, glyph and script fallback
71 class VCL_PLUGIN_PUBLIC ImplLayoutRuns
72 {
73 private:
74     int                 mnRunIndex;
75     std::vector<int>    maRuns;
76 
77 public:
78             ImplLayoutRuns() { mnRunIndex = 0; maRuns.reserve(8); }
79 
80     void    Clear()             { maRuns.clear(); }
81     bool    AddPos( int nCharPos, bool bRTL );
82     bool    AddRun( int nMinRunPos, int nEndRunPos, bool bRTL );
83 
84     bool    IsEmpty() const     { return maRuns.empty(); }
85     void    ResetPos()          { mnRunIndex = 0; }
86     void    NextRun()           { mnRunIndex += 2; }
87     bool    GetRun( int* nMinRunPos, int* nEndRunPos, bool* bRTL ) const;
88     bool    GetNextPos( int* nCharPos, bool* bRTL );
89     bool    PosIsInRun( int nCharPos ) const;
90     bool    PosIsInAnyRun( int nCharPos ) const;
91 };
92 
93 // -----------------
94 
95 class ImplLayoutArgs
96 {
97 public:
98     // string related inputs
99     int                 mnFlags;
100     int                 mnLength;
101     int                 mnMinCharPos;
102     int                 mnEndCharPos;
103     const xub_Unicode*  mpStr;
104 
105     // positioning related inputs
106     const sal_Int32*    mpDXArray;          // in pixel units
107     long                mnLayoutWidth;      // in pixel units
108     int                 mnOrientation;      // in 0-3600 system
109 
110     // data for bidi and glyph+script fallback
111     ImplLayoutRuns      maRuns;
112     ImplLayoutRuns      maReruns;
113 
114 public:
115                 ImplLayoutArgs( const xub_Unicode* pStr, int nLength,
116                     int nMinCharPos, int nEndCharPos, int nFlags );
117 
118     void        SetLayoutWidth( long nWidth )       { mnLayoutWidth = nWidth; }
119     void        SetDXArray( const sal_Int32* pDXArray )  { mpDXArray = pDXArray; }
120     void        SetOrientation( int nOrientation )  { mnOrientation = nOrientation; }
121 
122     void        ResetPos()
123                     { maRuns.ResetPos(); }
124     bool        GetNextPos( int* nCharPos, bool* bRTL )
125                     { return maRuns.GetNextPos( nCharPos, bRTL ); }
126     bool        GetNextRun( int* nMinRunPos, int* nEndRunPos, bool* bRTL );
127     bool        NeedFallback( int nCharPos, bool bRTL )
128                     { return maReruns.AddPos( nCharPos, bRTL ); }
129     bool        NeedFallback( int nMinRunPos, int nEndRunPos, bool bRTL )
130                     { return maReruns.AddRun( nMinRunPos, nEndRunPos, bRTL ); }
131     // methods used by BiDi and glyph fallback
132     bool        NeedFallback() const
133                     { return !maReruns.IsEmpty(); }
134     bool        PrepareFallback();
135 
136 protected:
137     void        AddRun( int nMinCharPos, int nEndCharPos, bool bRTL );
138 };
139 
140 // helper functions often used with ImplLayoutArgs
141 bool IsDiacritic( sal_UCS4 );
142 int GetVerticalFlags( sal_UCS4 );
143 sal_UCS4 GetVerticalChar( sal_UCS4 );
144 // #i80090# GetMirroredChar also needed outside vcl, moved to svapp.hxx
145 // VCL_DLLPUBLIC sal_UCS4 GetMirroredChar( sal_UCS4 );
146 sal_UCS4 GetLocalizedChar( sal_UCS4, LanguageType );
147 VCL_PLUGIN_PUBLIC const char* GetAutofallback( sal_UCS4 ) ;
148 
149 // -------------
150 // - SalLayout -
151 // -------------
152 
153 typedef sal_uInt32 sal_GlyphId;
154 
155 // Glyph Flags
156 #define GF_NONE     0x00000000
157 #define GF_FLAGMASK 0xFF800000
158 #define GF_IDXMASK  ~GF_FLAGMASK
159 #define GF_ISCHAR   0x00800000
160 #define GF_ROTL     0x01000000
161 // caution !!!
162 #define GF_VERT     0x02000000
163 // GF_VERT is only for windows implementation
164 // (win/source/gdi/salgdi3.cxx, win/source/gdi/winlayout.cxx)
165 // don't use this elsewhere !!!
166 #define GF_ROTR     0x03000000
167 #define GF_ROTMASK  0x03000000
168 #define GF_UNHINTED 0x04000000
169 #define GF_GSUB     0x08000000
170 #define GF_FONTMASK 0xF0000000
171 #define GF_FONTSHIFT 28
172 
173 #define GF_DROPPED  0xFFFFFFFF
174 
175 // all positions/widths are in font units
176 // one exception: drawposition is in pixel units
177 
178 class VCL_PLUGIN_PUBLIC SalLayout
179 {
180 public:
181     // used by upper layers
182     Point&          DrawBase()                              { return maDrawBase; }
183     const Point&    DrawBase() const                        { return maDrawBase; }
184     Point&          DrawOffset()                            { return maDrawOffset; }
185     const Point&    DrawOffset() const                      { return maDrawOffset; }
186     Point           GetDrawPosition( const Point& rRelative = Point(0,0) ) const;
187 
188     virtual bool    LayoutText( ImplLayoutArgs& ) = 0;  // first step of layouting
189     virtual void    AdjustLayout( ImplLayoutArgs& );    // adjusting after fallback etc.
190     virtual void    InitFont() const {}
191     virtual void    DrawText( SalGraphics& ) const = 0;
192 
193     int             GetUnitsPerPixel() const                { return mnUnitsPerPixel; }
194     int             GetOrientation() const                  { return mnOrientation; }
195 
196     virtual const ImplFontData* GetFallbackFontData( sal_GlyphId ) const;
197 
198     // methods using string indexing
199     virtual int     GetTextBreak( long nMaxWidth, long nCharExtra=0, int nFactor=1 ) const = 0;
200     virtual long    FillDXArray( sal_Int32* pDXArray ) const = 0;
201     virtual long    GetTextWidth() const { return FillDXArray( NULL ); }
202     virtual void    GetCaretPositions( int nArraySize, sal_Int32* pCaretXArray ) const = 0;
203     virtual bool    IsKashidaPosValid ( int /*nCharPos*/ ) const { return true; } // i60594
204 
205     // methods using glyph indexing
206     virtual int     GetNextGlyphs( int nLen, sal_GlyphId* pGlyphIdAry, Point& rPos, int&,
207                         sal_Int32* pGlyphAdvAry = NULL, int* pCharPosAry = NULL ) const = 0;
208     virtual bool    GetOutline( SalGraphics&, ::basegfx::B2DPolyPolygonVector& ) const;
209     virtual bool    GetBoundRect( SalGraphics&, Rectangle& ) const;
210 
211     virtual bool    IsSpacingGlyph( sal_GlyphId ) const;
212 
213     // reference counting
214     void            Reference() const;
215     void            Release() const;
216 
217     // used by glyph+font+script fallback
218     virtual void    MoveGlyph( int nStart, long nNewXPos ) = 0;
219     virtual void    DropGlyph( int nStart ) = 0;
220     virtual void    Simplify( bool bIsBase ) = 0;
221     virtual void    DisableGlyphInjection( bool /*bDisable*/ ) {}
222 
223 protected:
224     // used by layout engines
225                     SalLayout();
226     virtual         ~SalLayout();
227 
228     // used by layout layers
229     void            SetUnitsPerPixel( int n )               { mnUnitsPerPixel = n; }
230     void            SetOrientation( int nOrientation )      // in 0-3600 system
231                     { mnOrientation = nOrientation; }
232 
233     static int      CalcAsianKerning( sal_UCS4, bool bLeft, bool bVertical );
234 
235 private:
236     // enforce proper copy semantic
237     SAL_DLLPRIVATE  SalLayout( const SalLayout& );
238     SAL_DLLPRIVATE  SalLayout& operator=( const SalLayout& );
239 
240 protected:
241     int             mnMinCharPos;
242     int             mnEndCharPos;
243     int             mnLayoutFlags;
244 
245     int             mnUnitsPerPixel;
246     int             mnOrientation;
247 
248     mutable int     mnRefCount;
249     mutable Point   maDrawOffset;
250     Point           maDrawBase;
251 };
252 
253 // ------------------
254 // - MultiSalLayout -
255 // ------------------
256 
257 class VCL_PLUGIN_PUBLIC MultiSalLayout : public SalLayout
258 {
259 public:
260     virtual void    DrawText( SalGraphics& ) const;
261     virtual int     GetTextBreak( long nMaxWidth, long nCharExtra, int nFactor ) const;
262     virtual long    FillDXArray( sal_Int32* pDXArray ) const;
263     virtual void    GetCaretPositions( int nArraySize, sal_Int32* pCaretXArray ) const;
264     virtual int     GetNextGlyphs( int nLen, sal_GlyphId* pGlyphIdxAry, Point& rPos,
265                         int&, sal_Int32* pGlyphAdvAry, int* pCharPosAry ) const;
266     virtual bool    GetOutline( SalGraphics&, ::basegfx::B2DPolyPolygonVector& ) const;
267     virtual bool    GetBoundRect( SalGraphics&, Rectangle& ) const;
268 
269     // used only by OutputDevice::ImplLayout, TODO: make friend
270     explicit        MultiSalLayout( SalLayout& rBaseLayout,
271                          const ImplFontData* pBaseFont = NULL );
272     virtual bool    AddFallback( SalLayout& rFallbackLayout,
273                          ImplLayoutRuns&, const ImplFontData* pFallbackFont );
274     virtual bool    LayoutText( ImplLayoutArgs& );
275     virtual void    AdjustLayout( ImplLayoutArgs& );
276     virtual void    InitFont() const;
277 
278     virtual const ImplFontData* GetFallbackFontData( sal_GlyphId ) const;
279 
280     void SetInComplete(bool bInComplete = true);
281 
282 protected:
283     virtual         ~MultiSalLayout();
284 
285 private:
286     // dummy implementations
287     virtual void    MoveGlyph( int, long ) {}
288     virtual void    DropGlyph( int ) {}
289     virtual void    Simplify( bool ) {}
290 
291     // enforce proper copy semantic
292     SAL_DLLPRIVATE  MultiSalLayout( const MultiSalLayout& );
293     SAL_DLLPRIVATE  MultiSalLayout& operator=( const MultiSalLayout& );
294 
295 private:
296     SalLayout*      mpLayouts[ MAX_FALLBACK ];
297     const ImplFontData* mpFallbackFonts[ MAX_FALLBACK ];
298     ImplLayoutRuns  maFallbackRuns[ MAX_FALLBACK ];
299     int             mnLevel;
300     bool            mbInComplete;
301 };
302 
303 // --------------------
304 // - GenericSalLayout -
305 // --------------------
306 
307 struct GlyphItem
308 {
309     int     mnFlags;
310     int     mnCharPos;      // index in string
311     int     mnOrigWidth;    // original glyph width
312     int     mnNewWidth;     // width after adjustments
313     sal_GlyphId mnGlyphIndex;
314     Point   maLinearPos;    // absolute position of non rotated string
315 
316 public:
317             GlyphItem() {}
318 
319             GlyphItem( int nCharPos, sal_GlyphId nGlyphIndex, const Point& rLinearPos,
320                 long nFlags, int nOrigWidth )
321             :   mnFlags(nFlags), mnCharPos(nCharPos),
322                 mnOrigWidth(nOrigWidth), mnNewWidth(nOrigWidth),
323                 mnGlyphIndex(nGlyphIndex), maLinearPos(rLinearPos)
324             {}
325 
326     enum{ FALLBACK_MASK=0xFF, IS_IN_CLUSTER=0x100, IS_RTL_GLYPH=0x200, IS_DIACRITIC=0x400 };
327 
328     bool    IsClusterStart() const	{ return ((mnFlags & IS_IN_CLUSTER) == 0); }
329     bool    IsRTLGlyph() const		{ return ((mnFlags & IS_RTL_GLYPH) != 0); }
330     bool    IsDiacritic() const		{ return ((mnFlags & IS_DIACRITIC) != 0); }
331 };
332 
333 // ---------------
334 
335 typedef std::list<GlyphItem> GlyphList;
336 typedef std::vector<GlyphItem> GlyphVector;
337 
338 // ---------------
339 
340 class VCL_PLUGIN_PUBLIC GenericSalLayout : public SalLayout
341 {
342 public:
343     // used by layout engines
344     void            AppendGlyph( const GlyphItem& );
345     virtual void    AdjustLayout( ImplLayoutArgs& );
346     virtual void    ApplyDXArray( ImplLayoutArgs& );
347     virtual void    Justify( long nNewWidth );
348     void            KashidaJustify( long nIndex, int nWidth );
349     void            ApplyAsianKerning( const sal_Unicode*, int nLength );
350     void            SortGlyphItems();
351 
352     // used by upper layers
353     virtual long    GetTextWidth() const;
354     virtual long    FillDXArray( sal_Int32* pDXArray ) const;
355     virtual int     GetTextBreak( long nMaxWidth, long nCharExtra, int nFactor ) const;
356     virtual void    GetCaretPositions( int nArraySize, sal_Int32* pCaretXArray ) const;
357 
358     // used by display layers
359     virtual int     GetNextGlyphs( int nLen, sal_GlyphId* pGlyphIdxAry, Point& rPos, int&,
360                         sal_Int32* pGlyphAdvAry = NULL, int* pCharPosAry = NULL ) const;
361 
362 protected:
363                     GenericSalLayout();
364     virtual         ~GenericSalLayout();
365 
366     // for glyph+font+script fallback
367     virtual void    MoveGlyph( int nStart, long nNewXPos );
368     virtual void    DropGlyph( int nStart );
369     virtual void    Simplify( bool bIsBase );
370 
371     bool            GetCharWidths( sal_Int32* pCharWidths ) const;
372 
373 private:
374     GlyphItem*      mpGlyphItems;   // TODO: change to GlyphList
375     int             mnGlyphCount;
376     int             mnGlyphCapacity;
377     mutable Point   maBasePoint;
378 
379     // enforce proper copy semantic
380     SAL_DLLPRIVATE  GenericSalLayout( const GenericSalLayout& );
381     SAL_DLLPRIVATE  GenericSalLayout& operator=( const GenericSalLayout& );
382 };
383 
384 #undef SalGraphics
385 
386 #endif // _SV_SALLAYOUT_HXX
387