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
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
21 #include "FontMetrics_decls.h"
23 #include "charptrless.h"
25 #include "charconverters.h"
34 #include FT_FREETYPE_H
39 class CharacterMetrics
42 size_t internalPosition_
; // this is the position of this character in the containing charData_ vector of a WritingDirectionMetrics object.
44 double horizontalCharWidthX_
;
45 double horizontalCharWidthY_
;
46 double verticalCharWidthX_
;
47 double verticalCharWidthY_
;
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_
;
63 CharacterMetrics( size_t internalPosition
)
64 : internalPosition_( internalPosition
),
66 horizontalCharWidthX_( 0 ),
67 horizontalCharWidthY_( 0 ),
68 verticalCharWidthX_( 0 ),
69 verticalCharWidthY_( 0 ),
76 ligatureSetupMap_( 0 )
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
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
105 double underlinePosition_
;
106 double underlineThickness_
;
107 double italicAngleRadians_
;
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;
132 class FreeType2_WritingDirectionMetrics
: public WritingDirectionMetrics
135 FontMetrics::CharacterMetrics default_char_
;
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;
154 TrackKerning( double sizeLow
, double trackLow
, double sizeHigh
, double trackHigh
);
155 double operator () ( double sz
) const;
161 RefCountPtr
< const char > fontName_
;
162 RefCountPtr
< const char > fullName_
;
163 RefCountPtr
< const char > familyName_
;
164 RefCountPtr
< const char > weight_
;
165 size_t weightNumber_
;
167 double fontBBoxXMin_
;
168 double fontBBoxYMin_
;
169 double fontBBoxXMax_
;
170 double fontBBoxYMax_
;
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 >( ) ),
192 isCIDFont_( isCIDFont
),
194 horizontalMetrics_( NullPtr
< WritingDirectionMetrics
>( ) ),
195 verticalMetrics_( NullPtr
< WritingDirectionMetrics
>( ) ),
198 virtual ~FontMetric( );
200 virtual double getHorizontalKernPairXByCode( Shapes::Kernel::UnicodeCodePoint code1
, Shapes::Kernel::UnicodeCodePoint code2
) const = 0;
203 class AFM
: public FontMetric
206 RefCountPtr
< const char > version_
;
207 RefCountPtr
< const char > notice_
;
208 RefCountPtr
< const char > encodingScheme_
;
209 RefCountPtr
< const char > characterSet_
;
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_
;
229 virtual double getHorizontalKernPairXByCode( Shapes::Kernel::UnicodeCodePoint code1
, Shapes::Kernel::UnicodeCodePoint code2
) const;
233 class FreeType2_Metric
: public FontMetric
238 FreeType2_Metric( FT_Face face
);
239 virtual ~FreeType2_Metric( );
241 virtual double getHorizontalKernPairXByCode( Shapes::Kernel::UnicodeCodePoint code1
, Shapes::Kernel::UnicodeCodePoint code2
) const;