Update suitable examples and tests to use blank mode
[shapes.git] / source / fontmetrics.h
blob2413c0132e8b73a256076eff5635e6e46d35d09a
1 /* This file is part of Shapes.
3 * Shapes is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * any later version.
8 * Shapes is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with Shapes. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright 2008 Henrik Tidefelt
19 #pragma once
21 #include "FontMetrics_decls.h"
23 #include "charptrless.h"
24 #include "ptrowner.h"
25 #include "charconverters.h"
26 #include "config.h"
28 #include <vector>
29 #include <map>
30 #include <list>
32 #ifdef HAVE_FT2
33 #include <ft2build.h>
34 #include FT_FREETYPE_H
35 #endif
37 namespace FontMetrics
39 class CharacterMetrics
41 public:
42 size_t internalPosition_; // this is the position of this character in the containing charData_ vector of a WritingDirectionMetrics object.
43 int characterCode_;
44 double horizontalCharWidthX_;
45 double horizontalCharWidthY_;
46 double verticalCharWidthX_;
47 double verticalCharWidthY_;
48 double xmin_;
49 double ymin_;
50 double xmax_;
51 double ymax_;
52 double vX_;
53 double vY_;
55 private:
56 // The following fields are mutable since they cannot be synchronized at an early stage, yet the object is not really
57 // ready for access until synchronized. Hence, it should be safe to store const pointers to objects of this type
58 // in WritingDirectionMetrics, which are then synchronized once all characters are known by name.
59 mutable std::map< size_t, size_t > ligatures_;
60 mutable std::map< RefCountPtr< const char >, RefCountPtr< const char >, charRefPtrLess > * ligatureSetupMap_;
62 public:
63 CharacterMetrics( size_t internalPosition )
64 : internalPosition_( internalPosition ),
65 characterCode_( -1 ),
66 horizontalCharWidthX_( 0 ),
67 horizontalCharWidthY_( 0 ),
68 verticalCharWidthX_( 0 ),
69 verticalCharWidthY_( 0 ),
70 xmin_( 0 ),
71 ymin_( 0 ),
72 xmax_( 0 ),
73 ymax_( 0 ),
74 vX_( 0 ),
75 vY_( 0 ),
76 ligatureSetupMap_( 0 )
77 { }
78 ~CharacterMetrics( );
79 bool isEmpty( ) const;
80 bool hasLigature( size_t otherInternalPosition, size_t * ligatureInternalPosition ) const;
81 void addLigature( RefCountPtr< const char > otherName, RefCountPtr< const char > ligatureName );
82 void setupLigatures( const std::map< RefCountPtr< const char >, size_t, charRefPtrLess > & nameMap ) const;
84 void display( std::ostream & os ) const;
87 class WritingDirectionMetrics
89 public:
90 WritingDirectionMetrics( );
91 virtual ~WritingDirectionMetrics( );
93 /* The result of this operation may, but does not have to, be the same pointer as is passed as <mem>. */
94 virtual const CharacterMetrics * charByCode( Shapes::Kernel::UnicodeCodePoint code, CharacterMetrics * mem ) const = 0;
96 /* This will always produce a pointer which is owned by the metrics object.
97 * The returned pointer is the special pointer only returned when a character is not found by charByCode.
99 virtual const CharacterMetrics * default_char( ) const = 0;
102 class SingleByte_WritingDirectionMetrics : public WritingDirectionMetrics
104 public:
105 double underlinePosition_;
106 double underlineThickness_;
107 double italicAngleRadians_;
108 double charWidthX_;
109 double charWidthY_;
110 bool isFixedPitch_;
112 PtrOwner_back_Access< std::vector< const CharacterMetrics * > > charData_;
114 // The size_t is an index into charData_;
115 std::map< RefCountPtr< const char >, size_t, charRefPtrLess > nameMap_;
116 std::vector< size_t > codeMap_;
118 SingleByte_WritingDirectionMetrics( );
119 virtual ~SingleByte_WritingDirectionMetrics( );
121 void setupLigatures( );
122 const CharacterMetrics * charByName( const char * name ) const;
123 const CharacterMetrics * charByCode_MacRoman( unsigned char code ) const;
125 void display( std::ostream & os ) const;
127 virtual const CharacterMetrics * charByCode( Shapes::Kernel::UnicodeCodePoint code, CharacterMetrics * mem ) const;
128 virtual const CharacterMetrics * default_char( ) const;
131 #ifdef HAVE_FT2
132 class FreeType2_WritingDirectionMetrics : public WritingDirectionMetrics
134 FT_Face face_;
135 FontMetrics::CharacterMetrics default_char_;
136 bool vertical_;
137 double font_unit_;
138 public:
139 FreeType2_WritingDirectionMetrics( FT_Face face, const FontMetric & parent, bool vertical );
140 virtual ~FreeType2_WritingDirectionMetrics( );
142 virtual const CharacterMetrics * charByCode( Shapes::Kernel::UnicodeCodePoint code, CharacterMetrics * mem ) const;
143 virtual const CharacterMetrics * default_char( ) const;
145 #endif
147 class TrackKerning
149 double sizeLow_;
150 double trackLow_;
151 double sizeHigh_;
152 double trackHigh_;
153 public:
154 TrackKerning( double sizeLow, double trackLow, double sizeHigh, double trackHigh );
155 double operator () ( double sz ) const;
158 class FontMetric
160 public:
161 RefCountPtr< const char > fontName_;
162 RefCountPtr< const char > fullName_;
163 RefCountPtr< const char > familyName_;
164 RefCountPtr< const char > weight_;
165 size_t weightNumber_;
166 bool isCIDFont_;
167 double fontBBoxXMin_;
168 double fontBBoxYMin_;
169 double fontBBoxXMax_;
170 double fontBBoxYMax_;
171 double ascender_;
172 double descender_;
173 double leading_;
175 // Either of these may be null.
176 RefCountPtr< WritingDirectionMetrics > horizontalMetrics_;
177 RefCountPtr< WritingDirectionMetrics > verticalMetrics_;
179 bool hasKerning_; /* This flag should only be true if there are no kern pairs, and only if it is expected that there _should_ have been at least some pairs defined. */
180 typedef std::map< std::pair< Shapes::Kernel::UnicodeCodePoint, Shapes::Kernel::UnicodeCodePoint >, double > KernPairMap;
181 KernPairMap horizontalKernPairsX_;
182 KernPairMap horizontalKernPairsY_;
183 KernPairMap verticalKernPairsX_;
184 KernPairMap verticalKernPairsY_;
186 FontMetric( bool isCIDFont )
187 : fontName_( NullPtr< const char >( ) ),
188 fullName_( NullPtr< const char >( ) ),
189 familyName_( NullPtr< const char >( ) ),
190 weight_( NullPtr< const char >( ) ),
191 weightNumber_( 0 ),
192 isCIDFont_( isCIDFont ),
193 leading_( 0 ),
194 horizontalMetrics_( NullPtr< WritingDirectionMetrics >( ) ),
195 verticalMetrics_( NullPtr< WritingDirectionMetrics >( ) ),
196 hasKerning_( false )
198 virtual ~FontMetric( );
200 virtual double getHorizontalKernPairXByCode( Shapes::Kernel::UnicodeCodePoint code1, Shapes::Kernel::UnicodeCodePoint code2 ) const = 0;
203 class AFM : public FontMetric
205 public:
206 RefCountPtr< const char > version_;
207 RefCountPtr< const char > notice_;
208 RefCountPtr< const char > encodingScheme_;
209 RefCountPtr< const char > characterSet_;
210 size_t charCount_;
211 double capHeight_;
212 double xHeight_;
213 double stdHW_;
214 double stdVW_;
216 double vVectorX_;
217 double vVectorY_;
218 bool isFixedV_;
220 std::map< int, RefCountPtr< TrackKerning > > trackKernings_;
222 typedef std::pair< RefCountPtr< const char >, RefCountPtr< const char > > AssortedInfo;
223 std::list< AssortedInfo > assortedGlobalInfo_;
224 std::list< AssortedInfo > comments_;
226 AFM( );
227 virtual ~AFM( );
229 virtual double getHorizontalKernPairXByCode( Shapes::Kernel::UnicodeCodePoint code1, Shapes::Kernel::UnicodeCodePoint code2 ) const;
232 #ifdef HAVE_FT2
233 class FreeType2_Metric : public FontMetric
235 FT_Face face_;
236 double font_unit_;
237 public:
238 FreeType2_Metric( FT_Face face );
239 virtual ~FreeType2_Metric( );
241 virtual double getHorizontalKernPairXByCode( Shapes::Kernel::UnicodeCodePoint code1, Shapes::Kernel::UnicodeCodePoint code2 ) const;
243 #endif