xref: /trunk/main/vcl/inc/graphite_layout.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_GRAPHITELAYOUT_HXX
25 #define _SV_GRAPHITELAYOUT_HXX
26 // Description: An implementation of the SalLayout interface that uses the
27 //              Graphite engine.
28 
29 // We need this to enable namespace support in libgrengine headers.
30 #define GR_NAMESPACE
31 
32 #define GRCACHE 1
33 
34 // Standard Library
35 #include <memory>
36 #include <vector>
37 #include <utility>
38 // Libraries
39 #include <preextstl.h>
40 #include <graphite/GrClient.h>
41 #include <graphite/Font.h>
42 #include <graphite/GrConstants.h>
43 #include <graphite/GrAppData.h>
44 #include <graphite/SegmentAux.h>
45 #include <postextstl.h>
46 // Platform
47 #include <sallayout.hxx>
48 #include <vcl/dllapi.h>
49 // Module
50 
51 // For backwards compatibility with 2.4.x
52 #if (SUPD == 680)
53 typedef sal_Int32 sal_GlyphId;
54 #endif
55 
56 
57 // Module type definitions and forward declarations.
58 //
59 class TextSourceAdaptor;
60 class GraphiteFontAdaptor;
61 class GrSegRecord;
62 // SAL/VCL types
63 class ServerFont;
64 
65 #ifdef WNT
66 // The GraphiteWinFont is just a wrapper to enable GrFontHasher to be a friend
67 // so that UniqueCacheInfo can be called.
68 #include <graphite/WinFont.h>
69 class GraphiteWinFont : public gr::WinFont
70 {
71     friend class GrFontHasher;
72 public:
73     GraphiteWinFont(HDC hdc) : gr::WinFont(hdc) {};
74     virtual ~GraphiteWinFont() {};
75 };
76 #endif
77 // Graphite types
78 namespace gr { class Segment; class GlyphIterator; }
79 namespace grutils { class GrFeatureParser; }
80 
81 // This class uses the SIL Graphite engine to provide complex text layout services to the VCL
82 // @author tse
83 //
84 class VCL_PLUGIN_PUBLIC GraphiteLayout : public SalLayout
85 {
86 public:
87     // Mask to allow Word break status to be stored within mvChar2BaseGlyph
88     enum {
89         WORD_BREAK_BEFORE   = 0x40000000,
90         HYPHEN_BREAK_BEFORE = 0x80000000,
91         BREAK_MASK          = 0xC0000000,
92         GLYPH_INDEX_MASK    = 0x3FFFFFFF
93     } LineBreakMask;
94 
95     class Glyphs : public std::vector<GlyphItem>
96     {
97     public:
98         typedef std::pair<Glyphs::const_iterator, Glyphs::const_iterator> iterator_pair_t;
99 
100         void    fill_from(gr::Segment & rSeg, ImplLayoutArgs & rArgs,
101             bool bRtl, long &rWidth, float fScaling,
102             std::vector<int> & rChar2Base, std::vector<int> & rGlyph2Char,
103             std::vector<int> & rCharDxs);
104         void    move_glyph(Glyphs::iterator, long dx);
105 
106         const_iterator    cluster_base(const_iterator) const;
107         iterator_pair_t    neighbour_clusters(const_iterator) const;
108     private:
109         std::pair<float,float> appendCluster(gr::Segment & rSeg, ImplLayoutArgs & rArgs,
110             bool bRtl, float fSegmentAdvance, int nFirstCharInCluster, int nNextChar,
111             int nFirstGlyphInCluster, int nNextGlyph, float fScaling,
112             std::vector<int> & rChar2Base, std::vector<int> & rGlyph2Char,
113             std::vector<int> & rCharDxs, long & rDXOffset);
114         void         append(gr::Segment & rSeg, ImplLayoutArgs & rArgs, gr::GlyphInfo & rGi, float nextGlyphOrigin, float fScaling, std::vector<int> & rChar2Base, std::vector<int> & rGlyph2Char, std::vector<int> & rCharDxs, long & rDXOffset, bool bIsBase);
115     };
116 
117     mutable Glyphs          mvGlyphs;
118     void clear();
119 
120 private:
121     TextSourceAdaptor     * mpTextSrc; // Text source.
122     gr::LayoutEnvironment   maLayout;
123     const gr::Font         &mrFont;
124     long                    mnWidth;
125     std::vector<int>        mvCharDxs;
126     std::vector<int>        mvChar2BaseGlyph;
127     std::vector<int>        mvGlyph2Char;
128     float                   mfScaling;
129     const grutils::GrFeatureParser * mpFeatures;
130 
131 public:
132     explicit GraphiteLayout( const gr::Font& font, const grutils::GrFeatureParser* features = NULL ) throw();
133 
134     // used by upper layers
135     virtual bool  LayoutText( ImplLayoutArgs& );    // first step of layout
136     // split into two stages to allow dc to be restored on the segment
137 #ifdef GRCACHE
138     gr::Segment * CreateSegment(ImplLayoutArgs& rArgs, GrSegRecord ** pRecord = NULL);
139     bool LayoutGlyphs(ImplLayoutArgs& rArgs, gr::Segment * pSegment, GrSegRecord * pSegRecord);
140 #else
141     gr::Segment * CreateSegment(ImplLayoutArgs& rArgs);
142     bool LayoutGlyphs(ImplLayoutArgs& rArgs, gr::Segment * pSegment);
143 #endif
144 
145     virtual void  AdjustLayout( ImplLayoutArgs& );  // adjusting positions
146 
147     // methods using string indexing
148     virtual int   GetTextBreak( long nMaxWidth, long nCharExtra=0, int nFactor=1 ) const;
149     virtual long  FillDXArray( sal_Int32* pDXArray ) const;
150     virtual void  ApplyDXArray(ImplLayoutArgs &rArgs, std::vector<int> & rDeltaWidth);
151 
152     virtual void  GetCaretPositions( int nArraySize, sal_Int32* pCaretXArray ) const;
153 
154     // methods using glyph indexing
155     virtual int   GetNextGlyphs(int nLen, sal_GlyphId* pGlyphIdxAry, ::Point & rPos, int&,
156             sal_Int32* pGlyphAdvAry = 0, int* pCharPosAry = 0 ) const;
157 
158     // used by glyph+font+script fallback
159     virtual void    MoveGlyph( int nStart, long nNewXPos );
160     virtual void    DropGlyph( int nStart );
161     virtual void    Simplify( bool bIsBase );
162 
163     // Dummy implementation so layout can be shared between Linux/Windows
164     virtual void    DrawText(SalGraphics&) const {};
165 
166     virtual ~GraphiteLayout() throw();
167     void SetFeatures(grutils::GrFeatureParser * aFeature) { mpFeatures = aFeature; }
168     void SetFontScale(float s) { mfScaling = s; };
169     const TextSourceAdaptor * textSrc() const { return mpTextSrc; };
170     virtual sal_GlyphId getKashidaGlyph(int & width) = 0;
171     void kashidaJustify(std::vector<int> & rDeltaWidth, sal_GlyphId, int width);
172 
173     static const int EXTRA_CONTEXT_LENGTH;
174 private:
175     int                   glyph_to_char(Glyphs::iterator);
176     std::pair<int,int>    glyph_to_chars(const GlyphItem &) const;
177 
178     std::pair<long,long>  caret_positions(size_t) const;
179     void expandOrCondense(ImplLayoutArgs &rArgs);
180 };
181 
182 #endif // _SV_GRAPHITELAYOUT_HXX
183