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

« back to all changes in this revision

Viewing changes to src/3rdparty/freetype/src/autofit/afglobal.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
#include "afglobal.h"
 
2
#include "afdummy.h"
 
3
#include "aflatin.h"
 
4
 
 
5
 /* populate this list when you add new scripts
 
6
  */
 
7
  static AF_ScriptClass const   af_script_classes[] =
 
8
  {
 
9
    & af_dummy_script_class,
 
10
    & af_latin_script_class,
 
11
 
 
12
    NULL  /* do not remove */
 
13
  };
 
14
 
 
15
#define AF_SCRIPT_LIST_DEFAULT   1    /* index of default script in 'af_script_classes' */
 
16
#define AF_SCRIPT_LIST_NONE      255  /* indicates an uncovered glyph                      */
 
17
 
 
18
 /*
 
19
  *  note that glyph_scripts[] is used to map each glyph into
 
20
  *  an index into the 'af_script_classes' array.
 
21
  *
 
22
  */
 
23
  typedef struct AF_FaceGlobalsRec_
 
24
  {
 
25
    FT_Face            face;
 
26
    FT_UInt            glyph_count;    /* same as face->num_glyphs     */
 
27
    FT_Byte*           glyph_scripts;
 
28
 
 
29
    AF_ScriptMetrics   metrics[ AF_SCRIPT_MAX ];
 
30
 
 
31
  } AF_FaceGlobalsRec;
 
32
 
 
33
 
 
34
 
 
35
 
 
36
 /* this function is used to compute the script index of each glyph
 
37
  * within a given face
 
38
  */
 
39
  static FT_Error
 
40
  af_face_globals_compute_script_coverage( AF_FaceGlobals  globals )
 
41
  {
 
42
    FT_Error    error       = 0;
 
43
    FT_Face     face        = globals->face;
 
44
    FT_CharMap  old_charmap = face->charmap;
 
45
    FT_Byte*    gscripts    = globals->glyph_scripts;
 
46
    FT_UInt     ss;
 
47
 
 
48
   /* the value 255 means "uncovered glyph"
 
49
    */
 
50
    FT_MEM_SET( globals->glyph_scripts,
 
51
                AF_SCRIPT_LIST_NONE,
 
52
                globals->glyph_count );
 
53
 
 
54
    error = FT_Select_Charmap( face, FT_ENCODING_UNICODE );
 
55
    if ( error )
 
56
    {
 
57
     /* ignore this error, we'll simply use Latin as the standard
 
58
      * script. XXX: Shouldn't we rather disable hinting ??
 
59
      */
 
60
      error = 0;
 
61
      goto Exit;
 
62
    }
 
63
 
 
64
   /* scan each script in a Unicode charmap
 
65
    */
 
66
    for ( ss = 0; af_script_classes[ss]; ss++ )
 
67
    {
 
68
      AF_ScriptClass      clazz = af_script_classes[ss];
 
69
      AF_Script_UniRange  range;
 
70
 
 
71
      if ( clazz->script_uni_ranges == NULL )
 
72
        continue;
 
73
 
 
74
     /* scan all unicode points in the range, and set the corresponding
 
75
      * glyph script index
 
76
      */
 
77
      for ( range = clazz->script_uni_ranges; range->first != 0; range++ )
 
78
      {
 
79
        FT_ULong  charcode = range->first;
 
80
        FT_UInt   gindex;
 
81
 
 
82
        gindex = FT_Get_Char_Index( face, charcode );
 
83
 
 
84
        if ( gindex != 0                               &&
 
85
             gindex < globals->glyph_count             &&
 
86
             gscripts[ gindex ] == AF_SCRIPT_LIST_NONE )
 
87
        {
 
88
          gscripts[ gindex ] = (FT_Byte) ss;
 
89
        }
 
90
        for (;;)
 
91
        {
 
92
          charcode = FT_Get_Next_Char( face, charcode, &gindex );
 
93
 
 
94
          if ( gindex == 0 || charcode > range->last )
 
95
            break;
 
96
 
 
97
          if ( gindex < globals->glyph_count       &&
 
98
               gscripts[ gindex ] == AF_SCRIPT_LIST_NONE )
 
99
          {
 
100
            gscripts[ gindex ] = (FT_Byte) ss;
 
101
          }
 
102
        }
 
103
      }
 
104
    }
 
105
 
 
106
  Exit:
 
107
   /* by default, all uncovered glyphs are set to the latin script
 
108
    * XXX: shouldnt' we disable hinting or do something similar ?
 
109
    */
 
110
    {
 
111
      FT_UInt  nn;
 
112
 
 
113
      for ( nn = 0; nn < globals->glyph_count; nn++ )
 
114
      {
 
115
        if ( gscripts[ nn ] == AF_SCRIPT_LIST_NONE )
 
116
          gscripts[ nn ] = AF_SCRIPT_LIST_DEFAULT;
 
117
      }
 
118
    }
 
119
 
 
120
    FT_Set_Charmap( face, old_charmap );
 
121
    return error;
 
122
  }
 
123
 
 
124
 
 
125
 
 
126
  FT_LOCAL_DEF( FT_Error )
 
127
  af_face_globals_new( FT_Face          face,
 
128
                       AF_FaceGlobals  *aglobals )
 
129
  {
 
130
    FT_Error        error;
 
131
    FT_Memory       memory;
 
132
    AF_FaceGlobals  globals;
 
133
 
 
134
    memory = face->memory;
 
135
 
 
136
    if ( !FT_ALLOC( globals, sizeof(*globals) +
 
137
                             face->num_glyphs*sizeof(FT_Byte) ) )
 
138
    {
 
139
      globals->face          = face;
 
140
      globals->glyph_count   = face->num_glyphs;
 
141
      globals->glyph_scripts = (FT_Byte*)( globals+1 );
 
142
 
 
143
      error = af_face_globals_compute_script_coverage( globals );
 
144
      if ( error )
 
145
      {
 
146
        af_face_globals_free( globals );
 
147
        globals = NULL;
 
148
      }
 
149
    }
 
150
 
 
151
    *aglobals = globals;
 
152
    return error;
 
153
  }
 
154
 
 
155
 
 
156
  FT_LOCAL_DEF( void )
 
157
  af_face_globals_free( AF_FaceGlobals  globals )
 
158
  {
 
159
    if ( globals )
 
160
    {
 
161
      FT_Memory  memory = globals->face->memory;
 
162
      FT_UInt    nn;
 
163
 
 
164
      for ( nn = 0; nn < AF_SCRIPT_MAX; nn++ )
 
165
      {
 
166
        if ( globals->metrics[nn] )
 
167
        {
 
168
          AF_ScriptClass  clazz = af_script_classes[nn];
 
169
 
 
170
          FT_ASSERT( globals->metrics[nn]->clazz == clazz );
 
171
 
 
172
          if ( clazz->script_metrics_done )
 
173
            clazz->script_metrics_done( globals->metrics[nn] );
 
174
 
 
175
          FT_FREE( globals->metrics[nn] );
 
176
        }
 
177
      }
 
178
 
 
179
      globals->glyph_count   = 0;
 
180
      globals->glyph_scripts = NULL;  /* no need to free this one !! */
 
181
      globals->face          = NULL;
 
182
      FT_FREE( globals );
 
183
    }
 
184
  }
 
185
 
 
186
 
 
187
  FT_LOCAL_DEF( FT_Error )
 
188
  af_face_globals_get_metrics( AF_FaceGlobals     globals,
 
189
                               FT_UInt            gindex,
 
190
                               AF_ScriptMetrics  *ametrics )
 
191
  {
 
192
    AF_ScriptMetrics  metrics = NULL;
 
193
    FT_UInt           index;
 
194
    AF_ScriptClass    clazz;
 
195
    FT_Error          error = 0;
 
196
 
 
197
    if ( gindex >= globals->glyph_count )
 
198
    {
 
199
      error = FT_Err_Invalid_Argument;
 
200
      goto Exit;
 
201
    }
 
202
 
 
203
    index   = globals->glyph_scripts[ gindex ];
 
204
    clazz   = af_script_classes[ index ];
 
205
    metrics = globals->metrics[ clazz->script ];
 
206
    if ( metrics == NULL )
 
207
    {
 
208
     /* create the global metrics object when needed
 
209
      */
 
210
      FT_Memory  memory = globals->face->memory;
 
211
 
 
212
      if ( FT_ALLOC( metrics, clazz->script_metrics_size ) )
 
213
        goto Exit;
 
214
 
 
215
      metrics->clazz = clazz;
 
216
 
 
217
      if ( clazz->script_metrics_init )
 
218
      {
 
219
        error = clazz->script_metrics_init( metrics, globals->face );
 
220
        if ( error )
 
221
        {
 
222
          if ( clazz->script_metrics_done )
 
223
            clazz->script_metrics_done( metrics );
 
224
 
 
225
          FT_FREE( metrics );
 
226
          goto Exit;
 
227
        }
 
228
      }
 
229
 
 
230
      globals->metrics[ clazz->script ] = metrics;
 
231
    }
 
232
 
 
233
  Exit:
 
234
    *ametrics = metrics;
 
235
    return error;
 
236
  }