1 /***************************************************************************/
5 /* Load the metrics tables common to TTF and OTF fonts (body). */
7 /* Copyright 2006-2009, 2011-2014 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
16 /***************************************************************************/
20 #include FT_INTERNAL_DEBUG_H
21 #include FT_INTERNAL_STREAM_H
22 #include FT_TRUETYPE_TAGS_H
28 /*************************************************************************/
30 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
31 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
32 /* messages during execution. */
35 #define FT_COMPONENT trace_ttmtx
38 /*************************************************************************/
41 /* tt_face_load_hmtx */
44 /* Load the `hmtx' or `vmtx' table into a face object. */
47 /* face :: A handle to the target face object. */
49 /* stream :: The input stream. */
51 /* vertical :: A boolean flag. If set, load `vmtx'. */
54 /* FreeType error code. 0 means success. */
56 FT_LOCAL_DEF( FT_Error )
57 tt_face_load_hmtx( TT_Face face,
62 FT_ULong tag, table_size;
63 FT_ULong* ptable_offset;
64 FT_ULong* ptable_size;
70 ptable_offset = &face->vert_metrics_offset;
71 ptable_size = &face->vert_metrics_size;
76 ptable_offset = &face->horz_metrics_offset;
77 ptable_size = &face->horz_metrics_size;
80 error = face->goto_table( face, tag, stream, &table_size );
84 *ptable_size = table_size;
85 *ptable_offset = FT_STREAM_POS();
92 /*************************************************************************/
95 /* tt_face_load_hhea */
98 /* Load the `hhea' or 'vhea' table into a face object. */
101 /* face :: A handle to the target face object. */
103 /* stream :: The input stream. */
105 /* vertical :: A boolean flag. If set, load `vhea'. */
108 /* FreeType error code. 0 means success. */
110 FT_LOCAL_DEF( FT_Error )
111 tt_face_load_hhea( TT_Face face,
116 TT_HoriHeader* header;
118 static const FT_Frame_Field metrics_header_fields[] =
121 #define FT_STRUCTURE TT_HoriHeader
123 FT_FRAME_START( 36 ),
124 FT_FRAME_ULONG ( Version ),
125 FT_FRAME_SHORT ( Ascender ),
126 FT_FRAME_SHORT ( Descender ),
127 FT_FRAME_SHORT ( Line_Gap ),
128 FT_FRAME_USHORT( advance_Width_Max ),
129 FT_FRAME_SHORT ( min_Left_Side_Bearing ),
130 FT_FRAME_SHORT ( min_Right_Side_Bearing ),
131 FT_FRAME_SHORT ( xMax_Extent ),
132 FT_FRAME_SHORT ( caret_Slope_Rise ),
133 FT_FRAME_SHORT ( caret_Slope_Run ),
134 FT_FRAME_SHORT ( caret_Offset ),
135 FT_FRAME_SHORT ( Reserved[0] ),
136 FT_FRAME_SHORT ( Reserved[1] ),
137 FT_FRAME_SHORT ( Reserved[2] ),
138 FT_FRAME_SHORT ( Reserved[3] ),
139 FT_FRAME_SHORT ( metric_Data_Format ),
140 FT_FRAME_USHORT( number_Of_HMetrics ),
147 void *v = &face->vertical;
150 error = face->goto_table( face, TTAG_vhea, stream, 0 );
154 header = (TT_HoriHeader*)v;
158 error = face->goto_table( face, TTAG_hhea, stream, 0 );
162 header = &face->horizontal;
165 if ( FT_STREAM_READ_FIELDS( metrics_header_fields, header ) )
168 FT_TRACE3(( "Ascender: %5d\n", header->Ascender ));
169 FT_TRACE3(( "Descender: %5d\n", header->Descender ));
170 FT_TRACE3(( "number_Of_Metrics: %5u\n", header->number_Of_HMetrics ));
172 header->long_metrics = NULL;
173 header->short_metrics = NULL;
180 /*************************************************************************/
183 /* tt_face_get_metrics */
186 /* Return the horizontal or vertical metrics in font units for a */
187 /* given glyph. The values are the left side bearing (top side */
188 /* bearing for vertical metrics) and advance width (advance height */
189 /* for vertical metrics). */
192 /* face :: A pointer to the TrueType face structure. */
194 /* vertical :: If set to TRUE, get vertical metrics. */
196 /* gindex :: The glyph index. */
199 /* abearing :: The bearing, either left side or top side. */
201 /* aadvance :: The advance width or advance height, depending on */
202 /* the `vertical' flag. */
205 tt_face_get_metrics( TT_Face face,
209 FT_UShort *aadvance )
212 FT_Stream stream = face->root.stream;
213 TT_HoriHeader* header;
214 FT_ULong table_pos, table_size, table_end;
220 void* v = &face->vertical;
223 header = (TT_HoriHeader*)v;
224 table_pos = face->vert_metrics_offset;
225 table_size = face->vert_metrics_size;
229 header = &face->horizontal;
230 table_pos = face->horz_metrics_offset;
231 table_size = face->horz_metrics_size;
234 table_end = table_pos + table_size;
236 k = header->number_Of_HMetrics;
240 if ( gindex < (FT_UInt)k )
242 table_pos += 4 * gindex;
243 if ( table_pos + 4 > table_end )
246 if ( FT_STREAM_SEEK( table_pos ) ||
247 FT_READ_USHORT( *aadvance ) ||
248 FT_READ_SHORT( *abearing ) )
253 table_pos += 4 * ( k - 1 );
254 if ( table_pos + 4 > table_end )
257 if ( FT_STREAM_SEEK( table_pos ) ||
258 FT_READ_USHORT( *aadvance ) )
261 table_pos += 4 + 2 * ( gindex - k );
262 if ( table_pos + 2 > table_end )
266 if ( !FT_STREAM_SEEK( table_pos ) )
267 (void)FT_READ_SHORT( *abearing );