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