summaryrefslogtreecommitdiff
path: root/src/cache/ftcimage.c
blob: 291b1cc13e7e5da31911da368fed39b4711c272b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/***************************************************************************/
/*                                                                         */
/*  ftcimage.c                                                             */
/*                                                                         */
/*    FreeType Image cache (body).                                         */
/*                                                                         */
/*  Copyright 2000-2001 by                                                 */
/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
/*                                                                         */
/*  This file is part of the FreeType project, and may only be used,       */
/*  modified, and distributed under the terms of the FreeType project      */
/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
/*  this file you indicate that you have read the license and              */
/*  understand and accept it fully.                                        */
/*                                                                         */
/***************************************************************************/


#include <ft2build.h>
#include FT_CACHE_H
#include FT_CACHE_IMAGE_H
#include FT_CACHE_INTERNAL_GLYPH_H
#include FT_INTERNAL_MEMORY_H

#include "ftcerror.h"

#include <string.h>     /* memcmp() */
#include <stdlib.h>     /* labs()   */


  /* the FT_Glyph image node type */
  typedef struct  FTC_ImageNodeRec_
  {
    FTC_GlyphNodeRec  gnode;
    FT_Glyph          glyph;

  } FTC_ImageNodeRec, *FTC_ImageNode;


#define FTC_IMAGE_NODE( x )         ((FTC_ImageNode)( x ))
#define FTC_IMAGE_NODE_GINDEX( x )  FTC_GLYPH_NODE_GINDEX( x )


  /* the glyph image query */
  typedef struct  FTC_ImageQueryRec_
  {
    FTC_GlyphQueryRec  gquery;
    FTC_ImageDesc      desc;

  } FTC_ImageQueryRec, *FTC_ImageQuery;

#define  FTC_IMAGE_QUERY(x)  ((FTC_ImageQuery)(x))



  /* the glyph image set type */
  typedef struct  FTC_ImageFamilyRec_
  {
    FTC_GlyphFamilyRec  gfam;
    FTC_ImageDesc       desc;

  } FTC_ImageFamilyRec, *FTC_ImageFamily;


#define  FTC_IMAGE_FAMILY( x )         ((FTC_ImageFamily)( x ))
#define  FTC_IMAGE_FAMILY_MEMORY( x )  FTC_GLYPH_FAMILY_MEMORY( &(x)->gfam )


  /*************************************************************************/
  /*************************************************************************/
  /*****                                                               *****/
  /*****                    GLYPH IMAGE NODES                          *****/
  /*****                                                               *****/
  /*************************************************************************/
  /*************************************************************************/


  /* finalize a given glyph image node */
  FT_CALLBACK_DEF( void )
  ftc_image_node_done( FTC_ImageNode  inode,
                       FTC_Cache      cache )
  {
    if ( inode->glyph )
    {
      FT_Done_Glyph( inode->glyph );
      inode->glyph = NULL;
    }
    ftc_glyph_node_done( FTC_GLYPH_NODE(inode), cache );
  }



  /* initialize a new glyph image node */
  FT_CALLBACK_DEF( FT_Error )
  ftc_image_node_init( FTC_ImageNode   inode,
                       FTC_GlyphQuery  gquery,
                       FTC_Cache       cache )
  {
    FTC_ImageFamily ifam = FTC_IMAGE_FAMILY( gquery->query.family );
    FT_Error        error;
    FT_Face         face;
    FT_Size         size;


    /* initialize its inner fields */
    ftc_glyph_node_init( FTC_GLYPH_NODE(inode),
                         gquery->gindex,
                         FTC_GLYPH_FAMILY(ifam) );

    /* we will now load the glyph image */
    error = FTC_Manager_Lookup_Size( FTC_FAMILY(ifam)->cache->manager,
                                     &ifam->desc.font,
                                     &face, &size );
    if ( !error )
    {
      FT_UInt  gindex      = FTC_GLYPH_NODE_GINDEX(inode);
      FT_UInt  load_flags  = FT_LOAD_DEFAULT;
      FT_UInt  type        = ifam->desc.type;


      if ( FTC_IMAGE_FORMAT( type ) == ftc_image_format_bitmap )
      {
        load_flags |= FT_LOAD_RENDER;
        if ( type & ftc_image_flag_monochrome )
          load_flags |= FT_LOAD_MONOCHROME;

        /* disable embedded bitmaps loading if necessary */
        if ( type & ftc_image_flag_no_sbits )
          load_flags |= FT_LOAD_NO_BITMAP;
      }
      else if ( FTC_IMAGE_FORMAT( type ) == ftc_image_format_outline )
      {
        /* disable embedded bitmaps loading */
        load_flags |= FT_LOAD_NO_BITMAP;

        if ( type & ftc_image_flag_unscaled )
          load_flags |= FT_LOAD_NO_SCALE;
      }

      if ( type & ftc_image_flag_unhinted )
        load_flags |= FT_LOAD_NO_HINTING;

      if ( type & ftc_image_flag_autohinted )
        load_flags |= FT_LOAD_FORCE_AUTOHINT;

      error = FT_Load_Glyph( face, gindex, load_flags );
      if ( !error )
      {
        if ( face->glyph->format == ft_glyph_format_bitmap  ||
             face->glyph->format == ft_glyph_format_outline )
        {
          /* ok, copy it */
          FT_Glyph  glyph;


          error = FT_Get_Glyph( face->glyph, &glyph );
          if ( !error )
          {
            inode->glyph = glyph;
            goto Exit;
          }
        }
        else
          error = FTC_Err_Invalid_Argument;
      }
    }

    /* in case of error */
    ftc_glyph_node_done( FTC_GLYPH_NODE(inode), cache );

  Exit:
    return error;
  }


  FT_CALLBACK_DEF( FT_ULong )
  ftc_image_node_weight( FTC_ImageNode  inode )
  {
    FT_ULong  size  = 0;
    FT_Glyph  glyph = inode->glyph;


    switch ( glyph->format )
    {
    case ft_glyph_format_bitmap:
      {
        FT_BitmapGlyph  bitg;


        bitg = (FT_BitmapGlyph)glyph;
        size = bitg->bitmap.rows * labs( bitg->bitmap.pitch ) +
               sizeof ( *bitg );
      }
      break;

    case ft_glyph_format_outline:
      {
        FT_OutlineGlyph  outg;


        outg = (FT_OutlineGlyph)glyph;
        size = outg->outline.n_points *
                 ( sizeof ( FT_Vector ) + sizeof ( FT_Byte ) ) +
               outg->outline.n_contours * sizeof ( FT_Short ) +
               sizeof ( *outg );
      }
      break;

    default:
      ;
    }

    size += sizeof ( *inode );
    return size;
  }


  /*************************************************************************/
  /*************************************************************************/
  /*****                                                               *****/
  /*****                    GLYPH IMAGE SETS                           *****/
  /*****                                                               *****/
  /*************************************************************************/
  /*************************************************************************/


  FT_CALLBACK_DEF( FT_Error )
  ftc_image_family_init( FTC_ImageFamily  ifam,
                         FTC_ImageQuery   iquery,
                         FTC_Cache        cache )
  {
    FTC_Manager  manager = cache->manager;
    FT_Error     error;
    FT_Face      face;


    ifam->desc = iquery->desc;

    /* we need to compute "iquery.item_total" now */
    error = FTC_Manager_Lookup_Face( manager,
                                     iquery->desc.font.face_id,
                                     &face );
    if ( !error )
    {
      error = ftc_glyph_family_init( FTC_GLYPH_FAMILY(ifam),
                                     FTC_IMAGE_DESC_HASH( &ifam->desc ),
                                     1,
                                     face->num_glyphs,
                                     FTC_GLYPH_QUERY(iquery),
                                     cache );
    }

    return error;
  }


  FT_CALLBACK_DEF( FT_Bool )
  ftc_image_family_compare( FTC_ImageFamily  ifam,
                            FTC_ImageQuery   iquery )
  {
    FT_Bool  result;


    result = FT_BOOL( FTC_IMAGE_DESC_COMPARE( &ifam->desc, &iquery->desc ) );
    if ( result )
      FTC_GLYPH_FAMILY_FOUND(ifam,iquery);

    return result;
  }


  /*************************************************************************/
  /*************************************************************************/
  /*****                                                               *****/
  /*****                    GLYPH IMAGE CACHE                          *****/
  /*****                                                               *****/
  /*************************************************************************/
  /*************************************************************************/



  FT_CALLBACK_TABLE_DEF
  const FTC_Cache_ClassRec  ftc_image_cache_class =
  {
    sizeof( FTC_CacheRec ),
    (FTC_Cache_InitFunc)       ftc_cache_init,
    (FTC_Cache_ClearFunc)      ftc_cache_clear,
    (FTC_Cache_DoneFunc)       ftc_cache_done,

    sizeof( FTC_ImageFamilyRec ),
    (FTC_Family_InitFunc)      ftc_image_family_init,
    (FTC_Family_CompareFunc)   ftc_image_family_compare,
    (FTC_Family_DoneFunc)      ftc_glyph_family_done,

    sizeof( FTC_ImageNodeRec ),
    (FTC_Node_InitFunc)        ftc_image_node_init,
    (FTC_Node_WeightFunc)      ftc_image_node_weight,
    (FTC_Node_CompareFunc)     ftc_glyph_node_compare,
    (FTC_Node_DoneFunc)        ftc_image_node_done
  };


  /* documentation is in ftcimage.h */

  FT_EXPORT_DEF( FT_Error )
  FTC_ImageCache_New( FTC_Manager      manager,
                      FTC_ImageCache  *acache )
  {
    return FTC_Manager_Register_Cache(
             manager,
             (FTC_Cache_Class)&ftc_image_cache_class,
             FTC_CACHE_P( acache ) );
  }


  /* documentation is in ftcimage.h */

  FT_EXPORT_DEF( FT_Error )
  FTC_ImageCache_Lookup( FTC_ImageCache  cache,
                         FTC_ImageDesc*  desc,
                         FT_UInt         gindex,
                         FT_Glyph       *aglyph,
                         FTC_Node       *anode )
  {
    FTC_ImageQueryRec  iquery;
    FTC_ImageNode      node;
    FT_Error           error;


    /* some argument checks are delayed to ftc_glyph_cache_lookup */
    if ( aglyph )
      *aglyph = NULL;

    *aglyph = NULL;

    if ( anode )
      *anode  = NULL;

    iquery.gquery.gindex = gindex;
    iquery.desc          = *desc;

    error = ftc_cache_lookup( FTC_CACHE(cache),
                              FTC_QUERY(&iquery),
                              (FTC_Node*) &node );
    if ( !error )
    {
      *aglyph = node->glyph;

      if (anode)
      {
        *anode = (FTC_Node)node;
        FTC_NODE(node)->ref_count++;
      }
    }

    return error;
  }



 /* backwards-compatibility functions */

  FT_EXPORT_DEF( FT_Error )
  FTC_Image_Cache_New( FTC_Manager       manager,
                       FTC_Image_Cache  *acache )
  {
    return FTC_ImageCache_New( manager, (FTC_ImageCache*)acache );
  }


  FT_EXPORT_DEF( FT_Error )
  FTC_Image_Cache_Lookup( FTC_Image_Cache  icache,
                          FTC_Image_Desc*  desc,
                          FT_UInt          gindex,
                          FT_Glyph        *aglyph )
  {
    FTC_ImageDesc  desc0;

    if ( !desc )
      return FT_Err_Invalid_Argument;

    desc0.font = desc->font;
    desc0.type = (FT_UInt32) desc->image_type;

    return FTC_ImageCache_Lookup( (FTC_ImageCache)icache,
                                   &desc0,
                                   gindex,
                                   aglyph,
                                   NULL );
  }


/* END */