~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/3rdparty/freetype/src/type1/t1gload.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************/
 
2
/*                                                                         */
 
3
/*  t1gload.c                                                              */
 
4
/*                                                                         */
 
5
/*    Type 1 Glyph Loader (body).                                          */
 
6
/*                                                                         */
 
7
/*  Copyright 1996-2001, 2002, 2003, 2004 by                               */
 
8
/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
 
9
/*                                                                         */
 
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.                                        */
 
15
/*                                                                         */
 
16
/***************************************************************************/
 
17
 
 
18
 
 
19
#include <ft2build.h>
 
20
#include "t1gload.h"
 
21
#include FT_INTERNAL_DEBUG_H
 
22
#include FT_INTERNAL_STREAM_H
 
23
#include FT_OUTLINE_H
 
24
#include FT_INTERNAL_POSTSCRIPT_AUX_H
 
25
 
 
26
#include "t1errors.h"
 
27
 
 
28
 
 
29
  /*************************************************************************/
 
30
  /*                                                                       */
 
31
  /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
 
32
  /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
 
33
  /* messages during execution.                                            */
 
34
  /*                                                                       */
 
35
#undef  FT_COMPONENT
 
36
#define FT_COMPONENT  trace_t1gload
 
37
 
 
38
 
 
39
  /*************************************************************************/
 
40
  /*************************************************************************/
 
41
  /*************************************************************************/
 
42
  /**********                                                      *********/
 
43
  /**********            COMPUTE THE MAXIMUM ADVANCE WIDTH         *********/
 
44
  /**********                                                      *********/
 
45
  /**********    The following code is in charge of computing      *********/
 
46
  /**********    the maximum advance width of the font.  It        *********/
 
47
  /**********    quickly processes each glyph charstring to        *********/
 
48
  /**********    extract the value from either a `sbw' or `seac'   *********/
 
49
  /**********    operator.                                         *********/
 
50
  /**********                                                      *********/
 
51
  /*************************************************************************/
 
52
  /*************************************************************************/
 
53
  /*************************************************************************/
 
54
 
 
55
 
 
56
  FT_LOCAL_DEF( FT_Error )
 
57
  T1_Parse_Glyph_And_Get_Char_String( T1_Decoder  decoder,
 
58
                                      FT_UInt     glyph_index,
 
59
                                      FT_Data*    char_string )
 
60
  {
 
61
    T1_Face   face  = (T1_Face)decoder->builder.face;
 
62
    T1_Font   type1 = &face->type1;
 
63
    FT_Error  error = T1_Err_Ok;
 
64
 
 
65
 
 
66
    decoder->font_matrix = type1->font_matrix;
 
67
    decoder->font_offset = type1->font_offset;
 
68
 
 
69
#ifdef FT_CONFIG_OPTION_INCREMENTAL
 
70
 
 
71
    /* For incremental fonts get the character data using the */
 
72
    /* callback function.                                     */
 
73
    if ( face->root.internal->incremental_interface )
 
74
      error = face->root.internal->incremental_interface->funcs->get_glyph_data(
 
75
                face->root.internal->incremental_interface->object,
 
76
                glyph_index, char_string );
 
77
    else
 
78
 
 
79
#endif /* FT_CONFIG_OPTION_INCREMENTAL */
 
80
 
 
81
    /* For ordinary fonts get the character data stored in the face record. */
 
82
    {
 
83
      char_string->pointer = type1->charstrings[glyph_index];
 
84
      char_string->length  = (FT_Int)type1->charstrings_len[glyph_index];
 
85
    }
 
86
 
 
87
    if ( !error )
 
88
      error = decoder->funcs.parse_charstrings(
 
89
                decoder, (FT_Byte*)char_string->pointer,
 
90
                char_string->length );
 
91
 
 
92
#ifdef FT_CONFIG_OPTION_INCREMENTAL
 
93
 
 
94
    /* Incremental fonts can optionally override the metrics. */
 
95
    if ( !error && face->root.internal->incremental_interface                 &&
 
96
         face->root.internal->incremental_interface->funcs->get_glyph_metrics )
 
97
    {
 
98
      FT_Incremental_MetricsRec  metrics;
 
99
 
 
100
 
 
101
      metrics.bearing_x = decoder->builder.left_bearing.x;
 
102
      metrics.bearing_y = decoder->builder.left_bearing.y;
 
103
      metrics.advance   = decoder->builder.advance.x;
 
104
      error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
 
105
                face->root.internal->incremental_interface->object,
 
106
                glyph_index, FALSE, &metrics );
 
107
      decoder->builder.left_bearing.x = metrics.bearing_x;
 
108
      decoder->builder.left_bearing.y = metrics.bearing_y;
 
109
      decoder->builder.advance.x      = metrics.advance;
 
110
      decoder->builder.advance.y      = 0;
 
111
    }
 
112
 
 
113
#endif /* FT_CONFIG_OPTION_INCREMENTAL */
 
114
 
 
115
    return error;
 
116
  }
 
117
 
 
118
 
 
119
  FT_CALLBACK_DEF( FT_Error )
 
120
  T1_Parse_Glyph( T1_Decoder  decoder,
 
121
                  FT_UInt     glyph_index )
 
122
  {
 
123
    FT_Data   glyph_data;
 
124
    FT_Error  error = T1_Parse_Glyph_And_Get_Char_String(
 
125
                        decoder, glyph_index, &glyph_data );
 
126
 
 
127
 
 
128
#ifdef FT_CONFIG_OPTION_INCREMENTAL
 
129
 
 
130
    if ( !error )
 
131
    {
 
132
      T1_Face  face = (T1_Face)decoder->builder.face;
 
133
 
 
134
 
 
135
      if ( face->root.internal->incremental_interface )
 
136
        face->root.internal->incremental_interface->funcs->free_glyph_data(
 
137
          face->root.internal->incremental_interface->object,
 
138
          &glyph_data );
 
139
    }
 
140
 
 
141
#endif /* FT_CONFIG_OPTION_INCREMENTAL */
 
142
 
 
143
    return error;
 
144
  }
 
145
 
 
146
 
 
147
  FT_LOCAL_DEF( FT_Error )
 
148
  T1_Compute_Max_Advance( T1_Face  face,
 
149
                          FT_Pos*  max_advance )
 
150
  {
 
151
    FT_Error       error;
 
152
    T1_DecoderRec  decoder;
 
153
    FT_Int         glyph_index;
 
154
    T1_Font        type1 = &face->type1;
 
155
    PSAux_Service  psaux = (PSAux_Service)face->psaux;
 
156
 
 
157
 
 
158
    *max_advance = 0;
 
159
 
 
160
    /* initialize load decoder */
 
161
    error = psaux->t1_decoder_funcs->init( &decoder,
 
162
                                           (FT_Face)face,
 
163
                                           0, /* size       */
 
164
                                           0, /* glyph slot */
 
165
                                           (FT_Byte**)type1->glyph_names,
 
166
                                           face->blend,
 
167
                                           0,
 
168
                                           FT_RENDER_MODE_NORMAL,
 
169
                                           T1_Parse_Glyph );
 
170
    if ( error )
 
171
      return error;
 
172
 
 
173
    decoder.builder.metrics_only = 1;
 
174
    decoder.builder.load_points  = 0;
 
175
 
 
176
    decoder.num_subrs = type1->num_subrs;
 
177
    decoder.subrs     = type1->subrs;
 
178
    decoder.subrs_len = type1->subrs_len;
 
179
 
 
180
    *max_advance = 0;
 
181
 
 
182
    /* for each glyph, parse the glyph charstring and extract */
 
183
    /* the advance width                                      */
 
184
    for ( glyph_index = 0; glyph_index < type1->num_glyphs; glyph_index++ )
 
185
    {
 
186
      /* now get load the unscaled outline */
 
187
      error = T1_Parse_Glyph( &decoder, glyph_index );
 
188
      if ( glyph_index == 0 || decoder.builder.advance.x > *max_advance )
 
189
        *max_advance = decoder.builder.advance.x;
 
190
 
 
191
      /* ignore the error if one occurred - skip to next glyph */
 
192
    }
 
193
 
 
194
    return T1_Err_Ok;
 
195
  }
 
196
 
 
197
 
 
198
  /*************************************************************************/
 
199
  /*************************************************************************/
 
200
  /*************************************************************************/
 
201
  /**********                                                      *********/
 
202
  /**********               UNHINTED GLYPH LOADER                  *********/
 
203
  /**********                                                      *********/
 
204
  /**********    The following code is in charge of loading a      *********/
 
205
  /**********    single outline.  It completely ignores hinting    *********/
 
206
  /**********    and is used when FT_LOAD_NO_HINTING is set.       *********/
 
207
  /**********                                                      *********/
 
208
  /**********      The Type 1 hinter is located in `t1hint.c'      *********/
 
209
  /**********                                                      *********/
 
210
  /*************************************************************************/
 
211
  /*************************************************************************/
 
212
  /*************************************************************************/
 
213
 
 
214
 
 
215
  FT_LOCAL_DEF( FT_Error )
 
216
  T1_Load_Glyph( T1_GlyphSlot  glyph,
 
217
                 T1_Size       size,
 
218
                 FT_UInt       glyph_index,
 
219
                 FT_Int32      load_flags )
 
220
  {
 
221
    FT_Error                error;
 
222
    T1_DecoderRec           decoder;
 
223
    T1_Face                 face = (T1_Face)glyph->root.face;
 
224
    FT_Bool                 hinting;
 
225
    T1_Font                 type1         = &face->type1;
 
226
    PSAux_Service           psaux         = (PSAux_Service)face->psaux;
 
227
    const T1_Decoder_Funcs  decoder_funcs = psaux->t1_decoder_funcs;
 
228
 
 
229
    FT_Matrix               font_matrix;
 
230
    FT_Vector               font_offset;
 
231
    FT_Data                 glyph_data;
 
232
#ifdef FT_CONFIG_OPTION_INCREMENTAL
 
233
    FT_Bool                 glyph_data_loaded = 0;
 
234
#endif
 
235
 
 
236
 
 
237
    if ( load_flags & FT_LOAD_NO_RECURSE )
 
238
      load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
 
239
 
 
240
    glyph->x_scale = size->root.metrics.x_scale;
 
241
    glyph->y_scale = size->root.metrics.y_scale;
 
242
 
 
243
    glyph->root.outline.n_points   = 0;
 
244
    glyph->root.outline.n_contours = 0;
 
245
 
 
246
    hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE   ) == 0 &&
 
247
                       ( load_flags & FT_LOAD_NO_HINTING ) == 0 );
 
248
 
 
249
    glyph->root.format = FT_GLYPH_FORMAT_OUTLINE;
 
250
 
 
251
    error = decoder_funcs->init( &decoder,
 
252
                                 (FT_Face)face,
 
253
                                 (FT_Size)size,
 
254
                                 (FT_GlyphSlot)glyph,
 
255
                                 (FT_Byte**)type1->glyph_names,
 
256
                                 face->blend,
 
257
                                 FT_BOOL( hinting ),
 
258
                                 FT_LOAD_TARGET_MODE( load_flags ),
 
259
                                 T1_Parse_Glyph );
 
260
    if ( error )
 
261
      goto Exit;
 
262
 
 
263
    decoder.builder.no_recurse = FT_BOOL(
 
264
                                   ( load_flags & FT_LOAD_NO_RECURSE ) != 0 );
 
265
 
 
266
    decoder.num_subrs = type1->num_subrs;
 
267
    decoder.subrs     = type1->subrs;
 
268
    decoder.subrs_len = type1->subrs_len;
 
269
 
 
270
    /* now load the unscaled outline */
 
271
    error = T1_Parse_Glyph_And_Get_Char_String( &decoder, glyph_index,
 
272
                                                &glyph_data );
 
273
    if ( error )
 
274
      goto Exit;
 
275
#ifdef FT_CONFIG_OPTION_INCREMENTAL
 
276
    glyph_data_loaded = 1;
 
277
#endif
 
278
 
 
279
    font_matrix = decoder.font_matrix;
 
280
    font_offset = decoder.font_offset;
 
281
 
 
282
    /* save new glyph tables */
 
283
    decoder_funcs->done( &decoder );
 
284
 
 
285
    /* now, set the metrics -- this is rather simple, as   */
 
286
    /* the left side bearing is the xMin, and the top side */
 
287
    /* bearing the yMax                                    */
 
288
    if ( !error )
 
289
    {
 
290
      glyph->root.outline.flags &= FT_OUTLINE_OWNER;
 
291
      glyph->root.outline.flags |= FT_OUTLINE_REVERSE_FILL;
 
292
 
 
293
      /* for composite glyphs, return only left side bearing and */
 
294
      /* advance width                                           */
 
295
      if ( load_flags & FT_LOAD_NO_RECURSE )
 
296
      {
 
297
        FT_Slot_Internal  internal = glyph->root.internal;
 
298
 
 
299
 
 
300
        glyph->root.metrics.horiBearingX = decoder.builder.left_bearing.x;
 
301
        glyph->root.metrics.horiAdvance  = decoder.builder.advance.x;
 
302
        internal->glyph_matrix           = font_matrix;
 
303
        internal->glyph_delta            = font_offset;
 
304
        internal->glyph_transformed      = 1;
 
305
      }
 
306
      else
 
307
      {
 
308
        FT_BBox            cbox;
 
309
        FT_Glyph_Metrics*  metrics = &glyph->root.metrics;
 
310
        FT_Vector          advance;
 
311
 
 
312
 
 
313
        /* copy the _unscaled_ advance width */
 
314
        metrics->horiAdvance                    = decoder.builder.advance.x;
 
315
        glyph->root.linearHoriAdvance           = decoder.builder.advance.x;
 
316
        glyph->root.internal->glyph_transformed = 0;
 
317
 
 
318
        /* make up vertical metrics */
 
319
        metrics->vertBearingX = 0;
 
320
        metrics->vertBearingY = 0;
 
321
        metrics->vertAdvance  = 0;
 
322
 
 
323
        glyph->root.linearVertAdvance = 0;
 
324
 
 
325
        glyph->root.format = FT_GLYPH_FORMAT_OUTLINE;
 
326
 
 
327
        if ( size && size->root.metrics.y_ppem < 24 )
 
328
          glyph->root.outline.flags |= FT_OUTLINE_HIGH_PRECISION;
 
329
 
 
330
#if 1
 
331
        /* apply the font matrix, if any */
 
332
        FT_Outline_Transform( &glyph->root.outline, &font_matrix );
 
333
 
 
334
        FT_Outline_Translate( &glyph->root.outline,
 
335
                              font_offset.x,
 
336
                              font_offset.y );
 
337
 
 
338
        advance.x = metrics->horiAdvance;
 
339
        advance.y = 0;
 
340
        FT_Vector_Transform( &advance, &font_matrix );
 
341
        metrics->horiAdvance = advance.x + font_offset.x;
 
342
        advance.x = 0;
 
343
        advance.y = metrics->vertAdvance;
 
344
        FT_Vector_Transform( &advance, &font_matrix );
 
345
        metrics->vertAdvance = advance.y + font_offset.y;
 
346
#endif
 
347
 
 
348
        if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 )
 
349
        {
 
350
          /* scale the outline and the metrics */
 
351
          FT_Int       n;
 
352
          FT_Outline*  cur = decoder.builder.base;
 
353
          FT_Vector*   vec = cur->points;
 
354
          FT_Fixed     x_scale = glyph->x_scale;
 
355
          FT_Fixed     y_scale = glyph->y_scale;
 
356
 
 
357
 
 
358
          /* First of all, scale the points, if we are not hinting */
 
359
          if ( !hinting )
 
360
            for ( n = cur->n_points; n > 0; n--, vec++ )
 
361
            {
 
362
              vec->x = FT_MulFix( vec->x, x_scale );
 
363
              vec->y = FT_MulFix( vec->y, y_scale );
 
364
            }
 
365
 
 
366
          FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
 
367
 
 
368
          /* Then scale the metrics */
 
369
          metrics->horiAdvance  = FT_MulFix( metrics->horiAdvance,  x_scale );
 
370
          metrics->vertAdvance  = FT_MulFix( metrics->vertAdvance,  y_scale );
 
371
 
 
372
          metrics->vertBearingX = FT_MulFix( metrics->vertBearingX, x_scale );
 
373
          metrics->vertBearingY = FT_MulFix( metrics->vertBearingY, y_scale );
 
374
 
 
375
          if ( hinting )
 
376
          {
 
377
            metrics->horiAdvance = FT_PIX_ROUND( metrics->horiAdvance );
 
378
            metrics->vertAdvance = FT_PIX_ROUND( metrics->vertAdvance );
 
379
 
 
380
            metrics->vertBearingX = FT_PIX_ROUND( metrics->vertBearingX );
 
381
            metrics->vertBearingY = FT_PIX_ROUND( metrics->vertBearingY );
 
382
          }
 
383
        }
 
384
 
 
385
        /* compute the other metrics */
 
386
        FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
 
387
 
 
388
        /* grid fit the bounding box if necessary */
 
389
        if ( hinting )
 
390
        {
 
391
          cbox.xMin = FT_PIX_FLOOR( cbox.xMin );
 
392
          cbox.yMin = FT_PIX_FLOOR( cbox.yMin );
 
393
          cbox.xMax = FT_PIX_CEIL( cbox.xMax );
 
394
          cbox.yMax = FT_PIX_CEIL( cbox.yMax );
 
395
        }
 
396
 
 
397
        metrics->width  = cbox.xMax - cbox.xMin;
 
398
        metrics->height = cbox.yMax - cbox.yMin;
 
399
 
 
400
        metrics->horiBearingX = cbox.xMin;
 
401
        metrics->horiBearingY = cbox.yMax;
 
402
      }
 
403
 
 
404
      /* Set control data to the glyph charstrings.  Note that this is */
 
405
      /* _not_ zero-terminated.                                        */
 
406
      glyph->root.control_data = (FT_Byte*)glyph_data.pointer;
 
407
      glyph->root.control_len  = glyph_data.length;
 
408
    }
 
409
 
 
410
 
 
411
  Exit:
 
412
 
 
413
#ifdef FT_CONFIG_OPTION_INCREMENTAL
 
414
    if ( glyph_data_loaded && face->root.internal->incremental_interface )
 
415
    {
 
416
      face->root.internal->incremental_interface->funcs->free_glyph_data(
 
417
        face->root.internal->incremental_interface->object,
 
418
        &glyph_data );
 
419
 
 
420
      /* Set the control data to null - it is no longer available if   */
 
421
      /* loaded incrementally.                                         */
 
422
      glyph->root.control_data = 0;
 
423
      glyph->root.control_len  = 0;
 
424
    }
 
425
#endif
 
426
 
 
427
    return error;
 
428
  }
 
429
 
 
430
 
 
431
/* END */