xref: /aoo41x/main/vcl/inc/sallayout.hxx (revision 248a599f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _SV_SALLAYOUT_HXX
25 #define _SV_SALLAYOUT_HXX
26 
27 #include <tools/gen.hxx>
28 #include <vector>
29 #include <basegfx/polygon/b2dpolypolygon.hxx>
30 
31 #ifndef _TOOLS_LANG_HXX
32 typedef unsigned short LanguageType;
33 #endif
34 
35 #include <vector>
36 #include <list>
37 #include <vcl/dllapi.h>
38 
39 // for typedef sal_UCS4
40 #include <vcl/vclenum.hxx>
41 #include "salglyphid.hxx"
42 
43 class SalGraphics;
44 class ImplFontData;
45 
46 #define MAX_FALLBACK 16
47 
48 // ----------------
49 // - LayoutOption -
50 // ----------------
51 
52 #define SAL_LAYOUT_BIDI_RTL                 0x0001
53 #define SAL_LAYOUT_BIDI_STRONG              0x0002
54 #define SAL_LAYOUT_RIGHT_ALIGN              0x0004
55 #define SAL_LAYOUT_KERNING_PAIRS            0x0010
56 #define SAL_LAYOUT_KERNING_ASIAN            0x0020
57 #define SAL_LAYOUT_VERTICAL                 0x0040
58 #define SAL_LAYOUT_COMPLEX_DISABLED         0x0100
59 #define SAL_LAYOUT_ENABLE_LIGATURES         0x0200
60 #define SAL_LAYOUT_SUBSTITUTE_DIGITS        0x0400
61 #define SAL_LAYOUT_KASHIDA_JUSTIFICATON     0x0800
62 #define SAL_LAYOUT_DISABLE_GLYPH_PROCESSING 0x1000
63 #define SAL_LAYOUT_FOR_FALLBACK             0x2000
64 
65 // -----------------
66 
67 // used for managing runs e.g. for BiDi, glyph and script fallback
68 class VCL_PLUGIN_PUBLIC ImplLayoutRuns
69 {
70 private:
71     int                 mnRunIndex;
72     std::vector<int>    maRuns;
73 
74 public:
ImplLayoutRuns()75             ImplLayoutRuns() { mnRunIndex = 0; maRuns.reserve(8); }
76 
Clear()77     void    Clear()             { maRuns.clear(); }
78     bool    AddPos( int nCharPos, bool bRTL );
79     bool    AddRun( int nMinRunPos, int nEndRunPos, bool bRTL );
80 
IsEmpty() const81     bool    IsEmpty() const     { return maRuns.empty(); }
ResetPos()82     void    ResetPos()          { mnRunIndex = 0; }
NextRun()83     void    NextRun()           { mnRunIndex += 2; }
84     bool    GetRun( int* nMinRunPos, int* nEndRunPos, bool* bRTL ) const;
85     bool    GetNextPos( int* nCharPos, bool* bRTL );
86     bool    PosIsInRun( int nCharPos ) const;
87     bool    PosIsInAnyRun( int nCharPos ) const;
88 };
89 
90 // -----------------
91 
92 class ImplLayoutArgs
93 {
94 public:
95     // string related inputs
96     int                 mnFlags;
97     int                 mnLength;
98     int                 mnMinCharPos;
99     int                 mnEndCharPos;
100     const xub_Unicode*  mpStr;
101 
102     // positioning related inputs
103     const sal_Int32*    mpDXArray;          // in pixel units
104     long                mnLayoutWidth;      // in pixel units
105     int                 mnOrientation;      // in 0-3600 system
106 
107     // data for bidi and glyph+script fallback
108     ImplLayoutRuns      maRuns;
109     ImplLayoutRuns      maReruns;
110 
111 public:
112                 ImplLayoutArgs( const xub_Unicode* pStr, int nLength,
113                     int nMinCharPos, int nEndCharPos, int nFlags );
114 
SetLayoutWidth(long nWidth)115     void        SetLayoutWidth( long nWidth )       { mnLayoutWidth = nWidth; }
SetDXArray(const sal_Int32 * pDXArray)116     void        SetDXArray( const sal_Int32* pDXArray )  { mpDXArray = pDXArray; }
SetOrientation(int nOrientation)117     void        SetOrientation( int nOrientation )  { mnOrientation = nOrientation; }
118 
ResetPos()119     void        ResetPos()
120                     { maRuns.ResetPos(); }
GetNextPos(int * nCharPos,bool * bRTL)121     bool        GetNextPos( int* nCharPos, bool* bRTL )
122                     { return maRuns.GetNextPos( nCharPos, bRTL ); }
123     bool        GetNextRun( int* nMinRunPos, int* nEndRunPos, bool* bRTL );
NeedFallback(int nCharPos,bool bRTL)124     bool        NeedFallback( int nCharPos, bool bRTL )
125                     { return maReruns.AddPos( nCharPos, bRTL ); }
NeedFallback(int nMinRunPos,int nEndRunPos,bool bRTL)126     bool        NeedFallback( int nMinRunPos, int nEndRunPos, bool bRTL )
127                     { return maReruns.AddRun( nMinRunPos, nEndRunPos, bRTL ); }
128     // methods used by BiDi and glyph fallback
NeedFallback() const129     bool        NeedFallback() const
130                     { return !maReruns.IsEmpty(); }
131     bool        PrepareFallback();
132 
133 protected:
134     void        AddRun( int nMinCharPos, int nEndCharPos, bool bRTL );
135 };
136 
137 // helper functions often used with ImplLayoutArgs
138 bool IsDiacritic( sal_UCS4 );
139 int GetVerticalFlags( sal_UCS4 );
140 sal_UCS4 GetVerticalChar( sal_UCS4 );
141 // #i80090# GetMirroredChar also needed outside vcl, moved to svapp.hxx
142 // VCL_DLLPUBLIC sal_UCS4 GetMirroredChar( sal_UCS4 );
143 sal_UCS4 GetLocalizedChar( sal_UCS4, LanguageType );
144 VCL_PLUGIN_PUBLIC const char* GetAutofallback( sal_UCS4 ) ;
145 
146 // -------------
147 // - SalLayout -
148 // -------------
149 
150 // all positions/widths are in font units
151 // one exception: drawposition is in pixel units
152 
153 class VCL_PLUGIN_PUBLIC SalLayout
154 {
155 public:
156     // used by upper layers
DrawBase()157     Point&          DrawBase()                              { return maDrawBase; }
DrawBase() const158     const Point&    DrawBase() const                        { return maDrawBase; }
DrawOffset()159     Point&          DrawOffset()                            { return maDrawOffset; }
DrawOffset() const160     const Point&    DrawOffset() const                      { return maDrawOffset; }
161     Point           GetDrawPosition( const Point& rRelative = Point(0,0) ) const;
162 
163     virtual bool    LayoutText( ImplLayoutArgs& ) = 0;  // first step of layouting
164     virtual void    AdjustLayout( ImplLayoutArgs& );    // adjusting after fallback etc.
InitFont() const165     virtual void    InitFont() const {}
166     virtual void    DrawText( SalGraphics& ) const = 0;
167 
GetUnitsPerPixel() const168     int             GetUnitsPerPixel() const                { return mnUnitsPerPixel; }
GetOrientation() const169     int             GetOrientation() const                  { return mnOrientation; }
170 
171     virtual const ImplFontData* GetFallbackFontData( sal_GlyphId ) const;
172 
173     // methods using string indexing
174     virtual int     GetTextBreak( long nMaxWidth, long nCharExtra=0, int nFactor=1 ) const = 0;
175     virtual long    FillDXArray( sal_Int32* pDXArray ) const = 0;
GetTextWidth() const176     virtual long    GetTextWidth() const { return FillDXArray( NULL ); }
177     virtual void    GetCaretPositions( int nArraySize, sal_Int32* pCaretXArray ) const = 0;
IsKashidaPosValid(int) const178     virtual bool    IsKashidaPosValid ( int /*nCharPos*/ ) const { return true; } // i60594
179 
180     // methods using glyph indexing
181     virtual int     GetNextGlyphs( int nLen, sal_GlyphId* pGlyphIdAry, Point& rPos, int&,
182                         sal_Int32* pGlyphAdvAry = NULL, int* pCharPosAry = NULL ) const = 0;
183     virtual bool    GetOutline( SalGraphics&, ::basegfx::B2DPolyPolygonVector& ) const;
184     virtual bool    GetBoundRect( SalGraphics&, Rectangle& ) const;
185 
186     virtual bool    IsSpacingGlyph( sal_GlyphId ) const;
187 
188     // reference counting
189     void            Reference() const;
190     void            Release() const;
191 
192     // used by glyph+font+script fallback
193     virtual void    MoveGlyph( int nStart, long nNewXPos ) = 0;
194     virtual void    DropGlyph( int nStart ) = 0;
195     virtual void    Simplify( bool bIsBase ) = 0;
DisableGlyphInjection(bool)196     virtual void    DisableGlyphInjection( bool /*bDisable*/ ) {}
197 
198 protected:
199     // used by layout engines
200                     SalLayout();
201     virtual         ~SalLayout();
202 
203     // used by layout layers
SetUnitsPerPixel(int n)204     void            SetUnitsPerPixel( int n )               { mnUnitsPerPixel = n; }
SetOrientation(int nOrientation)205     void            SetOrientation( int nOrientation )      // in 0-3600 system
206                     { mnOrientation = nOrientation; }
207 
208     static int      CalcAsianKerning( sal_UCS4, bool bLeft, bool bVertical );
209 
210 private:
211     // enforce proper copy semantic
212     SAL_DLLPRIVATE  SalLayout( const SalLayout& );
213     SAL_DLLPRIVATE  SalLayout& operator=( const SalLayout& );
214 
215 protected:
216     int             mnMinCharPos;
217     int             mnEndCharPos;
218     int             mnLayoutFlags;
219 
220     int             mnUnitsPerPixel;
221     int             mnOrientation;
222 
223     mutable int     mnRefCount;
224     mutable Point   maDrawOffset;
225     Point           maDrawBase;
226 };
227 
228 // ------------------
229 // - MultiSalLayout -
230 // ------------------
231 
232 class VCL_PLUGIN_PUBLIC MultiSalLayout : public SalLayout
233 {
234 public:
235     virtual void    DrawText( SalGraphics& ) const;
236     virtual int     GetTextBreak( long nMaxWidth, long nCharExtra, int nFactor ) const;
237     virtual long    FillDXArray( sal_Int32* pDXArray ) const;
238     virtual void    GetCaretPositions( int nArraySize, sal_Int32* pCaretXArray ) const;
239     virtual int     GetNextGlyphs( int nLen, sal_GlyphId* pGlyphIdxAry, Point& rPos,
240                         int&, sal_Int32* pGlyphAdvAry, int* pCharPosAry ) const;
241     virtual bool    GetOutline( SalGraphics&, ::basegfx::B2DPolyPolygonVector& ) const;
242     virtual bool    GetBoundRect( SalGraphics&, Rectangle& ) const;
243 
244     // used only by OutputDevice::ImplLayout, TODO: make friend
245     explicit        MultiSalLayout( SalLayout& rBaseLayout,
246                          const ImplFontData* pBaseFont = NULL );
247     virtual bool    AddFallback( SalLayout& rFallbackLayout,
248                          ImplLayoutRuns&, const ImplFontData* pFallbackFont );
249     virtual bool    LayoutText( ImplLayoutArgs& );
250     virtual void    AdjustLayout( ImplLayoutArgs& );
251     virtual void    InitFont() const;
252 
253     virtual const ImplFontData* GetFallbackFontData( sal_GlyphId ) const;
254 
255     void SetInComplete(bool bInComplete = true);
256 
257 protected:
258     virtual         ~MultiSalLayout();
259 
260 private:
261     // dummy implementations
MoveGlyph(int,long)262     virtual void    MoveGlyph( int, long ) {}
DropGlyph(int)263     virtual void    DropGlyph( int ) {}
Simplify(bool)264     virtual void    Simplify( bool ) {}
265 
266     // enforce proper copy semantic
267     SAL_DLLPRIVATE  MultiSalLayout( const MultiSalLayout& );
268     SAL_DLLPRIVATE  MultiSalLayout& operator=( const MultiSalLayout& );
269 
270 private:
271     SalLayout*      mpLayouts[ MAX_FALLBACK ];
272     const ImplFontData* mpFallbackFonts[ MAX_FALLBACK ];
273     ImplLayoutRuns  maFallbackRuns[ MAX_FALLBACK ];
274     int             mnLevel;
275     bool            mbInComplete;
276 };
277 
278 // --------------------
279 // - GenericSalLayout -
280 // --------------------
281 
282 struct GlyphItem
283 {
284     int     mnFlags;
285     int     mnCharPos;      // index in string
286     int     mnOrigWidth;    // original glyph width
287     int     mnNewWidth;     // width after adjustments
288     sal_GlyphId maGlyphId;
289     Point   maLinearPos;    // absolute position of non rotated string
290 
291 public:
GlyphItemGlyphItem292             GlyphItem() {}
293 
GlyphItemGlyphItem294             GlyphItem( int nCharPos, sal_GlyphId aGlyphId, const Point& rLinearPos,
295                 long nFlags, int nOrigWidth )
296             :   mnFlags(nFlags), mnCharPos(nCharPos),
297                 mnOrigWidth(nOrigWidth), mnNewWidth(nOrigWidth),
298                 maGlyphId(aGlyphId), maLinearPos(rLinearPos)
299             {}
300 
301     enum{ FALLBACK_MASK=0xFF, IS_IN_CLUSTER=0x100, IS_RTL_GLYPH=0x200, IS_DIACRITIC=0x400 };
302 
IsClusterStartGlyphItem303     bool    IsClusterStart() const	{ return ((mnFlags & IS_IN_CLUSTER) == 0); }
IsRTLGlyphGlyphItem304     bool    IsRTLGlyph() const		{ return ((mnFlags & IS_RTL_GLYPH) != 0); }
IsDiacriticGlyphItem305     bool    IsDiacritic() const		{ return ((mnFlags & IS_DIACRITIC) != 0); }
306 };
307 
308 // ---------------
309 
310 typedef std::list<GlyphItem> GlyphList;
311 typedef std::vector<GlyphItem> GlyphVector;
312 
313 // ---------------
314 
315 class VCL_PLUGIN_PUBLIC GenericSalLayout : public SalLayout
316 {
317 public:
318     // used by layout engines
319     void            AppendGlyph( const GlyphItem& );
320     virtual void    AdjustLayout( ImplLayoutArgs& );
321     virtual void    ApplyDXArray( ImplLayoutArgs& );
322     virtual void    Justify( long nNewWidth );
323     void            KashidaJustify( long nIndex, int nWidth );
324     void            ApplyAsianKerning( const sal_Unicode*, int nLength );
325     void            SortGlyphItems();
326 
327     // used by upper layers
328     virtual long    GetTextWidth() const;
329     virtual long    FillDXArray( sal_Int32* pDXArray ) const;
330     virtual int     GetTextBreak( long nMaxWidth, long nCharExtra, int nFactor ) const;
331     virtual void    GetCaretPositions( int nArraySize, sal_Int32* pCaretXArray ) const;
332 
333     // used by display layers
334     virtual int     GetNextGlyphs( int nLen, sal_GlyphId* pGlyphIdxAry, Point& rPos, int&,
335                         sal_Int32* pGlyphAdvAry = NULL, int* pCharPosAry = NULL ) const;
336 
337 protected:
338                     GenericSalLayout();
339     virtual         ~GenericSalLayout();
340 
341     // for glyph+font+script fallback
342     virtual void    MoveGlyph( int nStart, long nNewXPos );
343     virtual void    DropGlyph( int nStart );
344     virtual void    Simplify( bool bIsBase );
345 
346     bool            GetCharWidths( sal_Int32* pCharWidths ) const;
347 
348 private:
349     GlyphItem*      mpGlyphItems;   // TODO: change to GlyphList
350     int             mnGlyphCount;
351     int             mnGlyphCapacity;
352     mutable Point   maBasePoint;
353 
354     // enforce proper copy semantic
355     SAL_DLLPRIVATE  GenericSalLayout( const GenericSalLayout& );
356     SAL_DLLPRIVATE  GenericSalLayout& operator=( const GenericSalLayout& );
357 };
358 
359 #undef SalGraphics
360 
361 #endif // _SV_SALLAYOUT_HXX
362