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