xref: /aoo41x/main/vcl/inc/graphite_layout.hxx (revision 248a599f)
1*ebfcd9afSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ebfcd9afSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ebfcd9afSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ebfcd9afSAndrew Rist  * distributed with this work for additional information
6*ebfcd9afSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ebfcd9afSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ebfcd9afSAndrew Rist  * "License"); you may not use this file except in compliance
9*ebfcd9afSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ebfcd9afSAndrew Rist  *
11*ebfcd9afSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ebfcd9afSAndrew Rist  *
13*ebfcd9afSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ebfcd9afSAndrew Rist  * software distributed under the License is distributed on an
15*ebfcd9afSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ebfcd9afSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ebfcd9afSAndrew Rist  * specific language governing permissions and limitations
18*ebfcd9afSAndrew Rist  * under the License.
19*ebfcd9afSAndrew Rist  *
20*ebfcd9afSAndrew Rist  *************************************************************/
21*ebfcd9afSAndrew Rist 
22*ebfcd9afSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _SV_GRAPHITELAYOUT_HXX
25cdf0e10cSrcweir #define _SV_GRAPHITELAYOUT_HXX
26cdf0e10cSrcweir // Description: An implementation of the SalLayout interface that uses the
27cdf0e10cSrcweir //              Graphite engine.
28cdf0e10cSrcweir 
29cdf0e10cSrcweir // We need this to enable namespace support in libgrengine headers.
30cdf0e10cSrcweir #define GR_NAMESPACE
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #define GRCACHE 1
33cdf0e10cSrcweir 
34cdf0e10cSrcweir // Standard Library
35cdf0e10cSrcweir #include <memory>
36cdf0e10cSrcweir #include <vector>
37cdf0e10cSrcweir #include <utility>
38cdf0e10cSrcweir // Libraries
39cdf0e10cSrcweir #include <preextstl.h>
40cdf0e10cSrcweir #include <graphite/GrClient.h>
41cdf0e10cSrcweir #include <graphite/Font.h>
42cdf0e10cSrcweir #include <graphite/GrConstants.h>
43cdf0e10cSrcweir #include <graphite/GrAppData.h>
44cdf0e10cSrcweir #include <graphite/SegmentAux.h>
45cdf0e10cSrcweir #include <postextstl.h>
46cdf0e10cSrcweir // Platform
47cdf0e10cSrcweir #include <sallayout.hxx>
48cdf0e10cSrcweir #include <vcl/dllapi.h>
49cdf0e10cSrcweir // Module
50cdf0e10cSrcweir 
51cdf0e10cSrcweir // Module type definitions and forward declarations.
52cdf0e10cSrcweir //
53cdf0e10cSrcweir class TextSourceAdaptor;
54cdf0e10cSrcweir class GraphiteFontAdaptor;
55cdf0e10cSrcweir class GrSegRecord;
56cdf0e10cSrcweir // SAL/VCL types
57cdf0e10cSrcweir class ServerFont;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir #ifdef WNT
60cdf0e10cSrcweir // The GraphiteWinFont is just a wrapper to enable GrFontHasher to be a friend
61cdf0e10cSrcweir // so that UniqueCacheInfo can be called.
62cdf0e10cSrcweir #include <graphite/WinFont.h>
63cdf0e10cSrcweir class GraphiteWinFont : public gr::WinFont
64cdf0e10cSrcweir {
65cdf0e10cSrcweir     friend class GrFontHasher;
66cdf0e10cSrcweir public:
GraphiteWinFont(HDC hdc)67cdf0e10cSrcweir     GraphiteWinFont(HDC hdc) : gr::WinFont(hdc) {};
~GraphiteWinFont()68cdf0e10cSrcweir     virtual ~GraphiteWinFont() {};
69cdf0e10cSrcweir };
70cdf0e10cSrcweir #endif
71cdf0e10cSrcweir // Graphite types
72cdf0e10cSrcweir namespace gr { class Segment; class GlyphIterator; }
73cdf0e10cSrcweir namespace grutils { class GrFeatureParser; }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir // This class uses the SIL Graphite engine to provide complex text layout services to the VCL
76cdf0e10cSrcweir // @author tse
77cdf0e10cSrcweir //
78cdf0e10cSrcweir class VCL_PLUGIN_PUBLIC GraphiteLayout : public SalLayout
79cdf0e10cSrcweir {
80cdf0e10cSrcweir public:
81cdf0e10cSrcweir     // Mask to allow Word break status to be stored within mvChar2BaseGlyph
82cdf0e10cSrcweir     enum {
83cdf0e10cSrcweir         WORD_BREAK_BEFORE   = 0x40000000,
84cdf0e10cSrcweir         HYPHEN_BREAK_BEFORE = 0x80000000,
85cdf0e10cSrcweir         BREAK_MASK          = 0xC0000000,
86cdf0e10cSrcweir         GLYPH_INDEX_MASK    = 0x3FFFFFFF
87cdf0e10cSrcweir     } LineBreakMask;
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     class Glyphs : public std::vector<GlyphItem>
90cdf0e10cSrcweir     {
91cdf0e10cSrcweir     public:
92cdf0e10cSrcweir         typedef std::pair<Glyphs::const_iterator, Glyphs::const_iterator> iterator_pair_t;
93cdf0e10cSrcweir 
94cdf0e10cSrcweir         void    fill_from(gr::Segment & rSeg, ImplLayoutArgs & rArgs,
95cdf0e10cSrcweir             bool bRtl, long &rWidth, float fScaling,
96cdf0e10cSrcweir             std::vector<int> & rChar2Base, std::vector<int> & rGlyph2Char,
97cdf0e10cSrcweir             std::vector<int> & rCharDxs);
98cdf0e10cSrcweir         void    move_glyph(Glyphs::iterator, long dx);
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         const_iterator    cluster_base(const_iterator) const;
101cdf0e10cSrcweir         iterator_pair_t    neighbour_clusters(const_iterator) const;
102cdf0e10cSrcweir     private:
103cdf0e10cSrcweir         std::pair<float,float> appendCluster(gr::Segment & rSeg, ImplLayoutArgs & rArgs,
104cdf0e10cSrcweir             bool bRtl, float fSegmentAdvance, int nFirstCharInCluster, int nNextChar,
105cdf0e10cSrcweir             int nFirstGlyphInCluster, int nNextGlyph, float fScaling,
106cdf0e10cSrcweir             std::vector<int> & rChar2Base, std::vector<int> & rGlyph2Char,
107cdf0e10cSrcweir             std::vector<int> & rCharDxs, long & rDXOffset);
108cdf0e10cSrcweir         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);
109cdf0e10cSrcweir     };
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     mutable Glyphs          mvGlyphs;
112cdf0e10cSrcweir     void clear();
113cdf0e10cSrcweir 
114cdf0e10cSrcweir private:
115cdf0e10cSrcweir     TextSourceAdaptor     * mpTextSrc; // Text source.
116cdf0e10cSrcweir     gr::LayoutEnvironment   maLayout;
117cdf0e10cSrcweir     const gr::Font         &mrFont;
118cdf0e10cSrcweir     long                    mnWidth;
119cdf0e10cSrcweir     std::vector<int>        mvCharDxs;
120cdf0e10cSrcweir     std::vector<int>        mvChar2BaseGlyph;
121cdf0e10cSrcweir     std::vector<int>        mvGlyph2Char;
122cdf0e10cSrcweir     float                   mfScaling;
123cdf0e10cSrcweir     const grutils::GrFeatureParser * mpFeatures;
124cdf0e10cSrcweir 
125cdf0e10cSrcweir public:
126cdf0e10cSrcweir     explicit GraphiteLayout( const gr::Font& font, const grutils::GrFeatureParser* features = NULL ) throw();
127cdf0e10cSrcweir 
128cdf0e10cSrcweir     // used by upper layers
129cdf0e10cSrcweir     virtual bool  LayoutText( ImplLayoutArgs& );    // first step of layout
130cdf0e10cSrcweir     // split into two stages to allow dc to be restored on the segment
131cdf0e10cSrcweir #ifdef GRCACHE
132cdf0e10cSrcweir     gr::Segment * CreateSegment(ImplLayoutArgs& rArgs, GrSegRecord ** pRecord = NULL);
133cdf0e10cSrcweir     bool LayoutGlyphs(ImplLayoutArgs& rArgs, gr::Segment * pSegment, GrSegRecord * pSegRecord);
134cdf0e10cSrcweir #else
135cdf0e10cSrcweir     gr::Segment * CreateSegment(ImplLayoutArgs& rArgs);
136cdf0e10cSrcweir     bool LayoutGlyphs(ImplLayoutArgs& rArgs, gr::Segment * pSegment);
137cdf0e10cSrcweir #endif
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     virtual void  AdjustLayout( ImplLayoutArgs& );  // adjusting positions
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     // methods using string indexing
142cdf0e10cSrcweir     virtual int   GetTextBreak( long nMaxWidth, long nCharExtra=0, int nFactor=1 ) const;
143cdf0e10cSrcweir     virtual long  FillDXArray( sal_Int32* pDXArray ) const;
144cdf0e10cSrcweir     virtual void  ApplyDXArray(ImplLayoutArgs &rArgs, std::vector<int> & rDeltaWidth);
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     virtual void  GetCaretPositions( int nArraySize, sal_Int32* pCaretXArray ) const;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     // methods using glyph indexing
149cdf0e10cSrcweir     virtual int   GetNextGlyphs(int nLen, sal_GlyphId* pGlyphIdxAry, ::Point & rPos, int&,
150cdf0e10cSrcweir             sal_Int32* pGlyphAdvAry = 0, int* pCharPosAry = 0 ) const;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     // used by glyph+font+script fallback
153cdf0e10cSrcweir     virtual void    MoveGlyph( int nStart, long nNewXPos );
154cdf0e10cSrcweir     virtual void    DropGlyph( int nStart );
155cdf0e10cSrcweir     virtual void    Simplify( bool bIsBase );
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     // Dummy implementation so layout can be shared between Linux/Windows
DrawText(SalGraphics &) const158cdf0e10cSrcweir     virtual void    DrawText(SalGraphics&) const {};
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     virtual ~GraphiteLayout() throw();
SetFeatures(grutils::GrFeatureParser * aFeature)161cdf0e10cSrcweir     void SetFeatures(grutils::GrFeatureParser * aFeature) { mpFeatures = aFeature; }
SetFontScale(float s)162cdf0e10cSrcweir     void SetFontScale(float s) { mfScaling = s; };
textSrc() const163cdf0e10cSrcweir     const TextSourceAdaptor * textSrc() const { return mpTextSrc; };
164cdf0e10cSrcweir     virtual sal_GlyphId getKashidaGlyph(int & width) = 0;
165cdf0e10cSrcweir     void kashidaJustify(std::vector<int> & rDeltaWidth, sal_GlyphId, int width);
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     static const int EXTRA_CONTEXT_LENGTH;
168cdf0e10cSrcweir private:
169cdf0e10cSrcweir     int                   glyph_to_char(Glyphs::iterator);
170cdf0e10cSrcweir     std::pair<int,int>    glyph_to_chars(const GlyphItem &) const;
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     std::pair<long,long>  caret_positions(size_t) const;
173cdf0e10cSrcweir     void expandOrCondense(ImplLayoutArgs &rArgs);
174cdf0e10cSrcweir };
175cdf0e10cSrcweir 
176cdf0e10cSrcweir #endif // _SV_GRAPHITELAYOUT_HXX
177