Commit 5290d2fb46f33d7cd7e4036216196a7f92b7daa1

David Turner 2004-06-23T15:44:03

performance optimizations regarding face opening

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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
diff --git a/include/freetype/config/ftmodule.h b/include/freetype/config/ftmodule.h
index d0e6f16..c00a489 100644
--- a/include/freetype/config/ftmodule.h
+++ b/include/freetype/config/ftmodule.h
@@ -1,4 +1,6 @@
 FT_USE_MODULE(autohint_module_class)
+FT_USE_MODULE(tt_driver_class)
+FT_USE_MODULE(t1_driver_class)
 FT_USE_MODULE(cff_driver_class)
 FT_USE_MODULE(t1cid_driver_class)
 FT_USE_MODULE(pcf_driver_class)
@@ -11,8 +13,6 @@ FT_USE_MODULE(sfnt_module_class)
 FT_USE_MODULE(ft_smooth_renderer_class)
 FT_USE_MODULE(ft_smooth_lcd_renderer_class)
 FT_USE_MODULE(ft_smooth_lcdv_renderer_class)
-FT_USE_MODULE(tt_driver_class)
-FT_USE_MODULE(t1_driver_class)
 FT_USE_MODULE(t42_driver_class)
 FT_USE_MODULE(pfr_driver_class)
 FT_USE_MODULE(winfnt_driver_class)
diff --git a/include/freetype/internal/ftmemory.h b/include/freetype/internal/ftmemory.h
index 63f37af..e078ca6 100644
--- a/include/freetype/internal/ftmemory.h
+++ b/include/freetype/internal/ftmemory.h
@@ -108,6 +108,11 @@ FT_BEGIN_HEADER
             FT_Long    size,
             void*     *P );
 
+            
+  FT_BASE( FT_Error )
+  FT_QAlloc( FT_Memory  memory,
+             FT_Long    size,
+             void*     *p );            
 
   /*************************************************************************/
   /*                                                                       */
@@ -143,6 +148,12 @@ FT_BEGIN_HEADER
               FT_Long    size,
               void*     *P );
 
+              
+  FT_BASE( FT_Error )
+  FT_QRealloc( FT_Memory  memory,
+               FT_Long    current,
+               FT_Long    size,
+               void*     *p );              
 
   /*************************************************************************/
   /*                                                                       */
@@ -204,6 +215,14 @@ FT_BEGIN_HEADER
           FT_Realloc_Debug( memory, _current_, _size_,                 \
                             (void**)&(_pointer_), __FILE__, __LINE__ )
 
+#define FT_MEM_QALLOC( _pointer_, _size_ )                            \
+          FT_QAlloc_Debug( memory, _size_,                            \
+                          (void**)&(_pointer_), __FILE__, __LINE__ )
+
+#define FT_MEM_QREALLOC( _pointer_, _current_, _size_ )                 \
+          FT_QRealloc_Debug( memory, _current_, _size_,                 \
+                            (void**)&(_pointer_), __FILE__, __LINE__ )
+
 #define FT_MEM_FREE( _pointer_ )                                            \
           FT_Free_Debug( memory, (void**)&(_pointer_), __FILE__, __LINE__ )
 
@@ -220,6 +239,11 @@ FT_BEGIN_HEADER
 #define FT_MEM_REALLOC( _pointer_, _current_, _size_ )                  \
           FT_Realloc( memory, _current_, _size_, (void**)&(_pointer_) )
 
+#define FT_MEM_QALLOC( _pointer_, _size_ )                  \
+          FT_QAlloc( memory, _size_, (void**)&(_pointer_) )
+
+#define FT_MEM_QREALLOC( _pointer_, _current_, _size_ )                  \
+          FT_QRealloc( memory, _current_, _size_, (void**)&(_pointer_) )
 
 #endif /* !FT_DEBUG_MEMORY */
 
@@ -240,6 +264,16 @@ FT_BEGIN_HEADER
           FT_MEM_REALLOC( _pointer_, (_old_) * sizeof ( *(_pointer_) ),  \
                                      (_new_) * sizeof ( *(_pointer_) ) )
 
+#define FT_MEM_QNEW( _pointer_ )                               \
+          FT_MEM_QALLOC( _pointer_, sizeof ( *(_pointer_) ) )
+
+#define FT_MEM_QNEW_ARRAY( _pointer_, _count_ )                           \
+          FT_MEM_QALLOC( _pointer_, (_count_) * sizeof ( *(_pointer_) ) )
+
+#define FT_MEM_QRENEW_ARRAY( _pointer_, _old_, _new_ )                    \
+          FT_MEM_QREALLOC( _pointer_, (_old_) * sizeof ( *(_pointer_) ),  \
+                                      (_new_) * sizeof ( *(_pointer_) ) )
+
 
   /*************************************************************************/
   /*                                                                       */
@@ -270,6 +304,13 @@ FT_BEGIN_HEADER
 #define FT_FREE( _pointer_ )       \
           FT_MEM_FREE( _pointer_ )
 
+#define FT_QALLOC( _pointer_, _size_ )                       \
+          FT_SET_ERROR( FT_MEM_QALLOC( _pointer_, _size_ ) )
+
+#define FT_QREALLOC( _pointer_, _current_, _size_ )                       \
+          FT_SET_ERROR( FT_MEM_QREALLOC( _pointer_, _current_, _size_ ) )
+
+          
 #define FT_NEW( _pointer_ )  \
           FT_SET_ERROR( FT_MEM_NEW( _pointer_ ) )
 
@@ -279,6 +320,18 @@ FT_BEGIN_HEADER
 #define FT_RENEW_ARRAY( _pointer_, _old_, _new_ )   \
           FT_SET_ERROR( FT_MEM_RENEW_ARRAY( _pointer_, _old_, _new_ ) )
 
+#define FT_QNEW( _pointer_ )  \
+          FT_SET_ERROR( FT_MEM_QNEW( _pointer_ ) )
+
+#define FT_QNEW_ARRAY( _pointer_, _count_ )  \
+          FT_SET_ERROR( FT_MEM_QNEW_ARRAY( _pointer_, _count_ ) )
+
+#define FT_QRENEW_ARRAY( _pointer_, _old_, _new_ )   \
+          FT_SET_ERROR( FT_MEM_QRENEW_ARRAY( _pointer_, _old_, _new_ ) )
+
+          
+          
+          
 #define FT_ALLOC_ARRAY( _pointer_, _count_, _type_ )                    \
           FT_SET_ERROR( FT_MEM_ALLOC( _pointer_,                        \
                                       (_count_) * sizeof ( _type_ ) ) )
diff --git a/include/freetype/internal/ftstream.h b/include/freetype/internal/ftstream.h
index 87576fd..9833cd0 100644
--- a/include/freetype/internal/ftstream.h
+++ b/include/freetype/internal/ftstream.h
@@ -269,6 +269,25 @@ FT_BEGIN_HEADER
   /*                                                                       */
   /* Each GET_xxxx() macro uses an implicit `stream' variable.             */
   /*                                                                       */
+#if 0
+#define FT_GET_MACRO( type )    FT_NEXT_ ## type ( stream->cursor )
+
+#define FT_GET_CHAR()       FT_GET_MACRO( CHAR )
+#define FT_GET_BYTE()       FT_GET_MACRO( BYTE )
+#define FT_GET_SHORT()      FT_GET_MACRO( SHORT )
+#define FT_GET_USHORT()     FT_GET_MACRO( USHORT )
+#define FT_GET_OFF3()       FT_GET_MACRO( OFF3 )
+#define FT_GET_UOFF3()      FT_GET_MACRO( UOFF3 )
+#define FT_GET_LONG()       FT_GET_MACRO( LONG )
+#define FT_GET_ULONG()      FT_GET_MACRO( ULONG )
+#define FT_GET_TAG4()       FT_GET_MACRO( ULONG )
+
+#define FT_GET_SHORT_LE()   FT_GET_MACRO( SHORT_LE )
+#define FT_GET_USHORT_LE()  FT_GET_MACRO( USHORT_LE )
+#define FT_GET_LONG_LE()    FT_GET_MACRO( LONG_LE )
+#define FT_GET_ULONG_LE()   FT_GET_MACRO( ULONG_LE )
+
+#else
 #define FT_GET_MACRO( func, type )        ( (type)func( stream ) )
 
 #define FT_GET_CHAR()       FT_GET_MACRO( FT_Stream_GetChar, FT_Char )
@@ -285,6 +304,7 @@ FT_BEGIN_HEADER
 #define FT_GET_USHORT_LE()  FT_GET_MACRO( FT_Stream_GetShortLE, FT_UShort )
 #define FT_GET_LONG_LE()    FT_GET_MACRO( FT_Stream_GetLongLE, FT_Long )
 #define FT_GET_ULONG_LE()   FT_GET_MACRO( FT_Stream_GetLongLE, FT_ULong )
+#endif
 
 #define FT_READ_MACRO( func, type, var )        \
           ( var = (type)func( stream, &error ), \
diff --git a/src/base/ftdbgmem.c b/src/base/ftdbgmem.c
index 05226e5..5cee7dc 100644
--- a/src/base/ftdbgmem.c
+++ b/src/base/ftdbgmem.c
@@ -694,6 +694,45 @@
   }
 
 
+  FT_BASE_DEF( FT_Error )
+  FT_QAlloc_Debug( FT_Memory    memory,
+                  FT_Long      size,
+                  void*       *P,
+                  const char*  file_name,
+                  FT_Long      line_no )
+  {
+    FT_MemTable  table = (FT_MemTable)memory->user;
+
+
+    if ( table )
+    {
+      table->file_name = file_name;
+      table->line_no   = line_no;
+    }
+    return FT_QAlloc( memory, size, P );
+  }
+
+
+  FT_BASE_DEF( FT_Error )
+  FT_QRealloc_Debug( FT_Memory    memory,
+                    FT_Long      current,
+                    FT_Long      size,
+                    void*       *P,
+                    const char*  file_name,
+                    FT_Long      line_no )
+  {
+    FT_MemTable  table = (FT_MemTable)memory->user;
+
+
+    if ( table )
+    {
+      table->file_name = file_name;
+      table->line_no   = line_no;
+    }
+    return FT_QRealloc( memory, current, size, P );
+  }
+
+  
   FT_BASE_DEF( void )
   FT_Free_Debug( FT_Memory    memory,
                  FT_Pointer   block,
diff --git a/src/base/ftstream.c b/src/base/ftstream.c
index 7ff1e90..932fe1b 100644
--- a/src/base/ftstream.c
+++ b/src/base/ftstream.c
@@ -210,7 +210,7 @@
       FT_Memory  memory = stream->memory;
 
 
-      if ( FT_ALLOC( stream->base, count ) )
+      if ( FT_QALLOC( stream->base, count ) )
         goto Exit;
 
       /* read it */
diff --git a/src/base/ftutil.c b/src/base/ftutil.c
index 451bee5..eff08ee 100644
--- a/src/base/ftutil.c
+++ b/src/base/ftutil.c
@@ -77,6 +77,36 @@
   }
 
 
+  FT_BASE_DEF( FT_Error )
+  FT_QAlloc( FT_Memory  memory,
+             FT_Long    size,
+             void*     *P )
+  {
+    FT_ASSERT( P != 0 );
+
+    if ( size > 0 )
+    {
+      *P = memory->alloc( memory, size );
+      if ( !*P )
+      {
+        FT_ERROR(( "FT_Alloc:" ));
+        FT_ERROR(( " Out of memory? (%ld requested)\n",
+                   size ));
+
+        return FT_Err_Out_Of_Memory;
+      }
+    }
+    else
+      *P = NULL;
+
+    FT_TRACE7(( "FT_Alloc:" ));
+    FT_TRACE7(( " size = %ld, block = 0x%08p, ref = 0x%08p\n",
+                size, *P, P ));
+
+    return FT_Err_Ok;
+  }
+
+  
   /* documentation is in ftmemory.h */
 
   FT_BASE_DEF( FT_Error )
@@ -119,6 +149,42 @@
   }
 
 
+  FT_BASE_DEF( FT_Error )
+  FT_QRealloc( FT_Memory  memory,
+               FT_Long    current,
+               FT_Long    size,
+               void**     P )
+  {
+    void*  Q;
+
+
+    FT_ASSERT( P != 0 );
+
+    /* if the original pointer is NULL, call FT_Alloc() */
+    if ( !*P )
+      return FT_Alloc( memory, size, P );
+
+    /* if the new block if zero-sized, clear the current one */
+    if ( size <= 0 )
+    {
+      FT_Free( memory, P );
+      return FT_Err_Ok;
+    }
+
+    Q = memory->realloc( memory, current, size, *P );
+    if ( !Q )
+      goto Fail;
+
+    *P = Q;
+    return FT_Err_Ok;
+
+  Fail:
+    FT_ERROR(( "FT_Realloc:" ));
+    FT_ERROR(( " Failed (current %ld, requested %ld)\n",
+               current, size ));
+    return FT_Err_Out_Of_Memory;
+  }
+  
   /* documentation is in ftmemory.h */
 
   FT_BASE_DEF( void )
diff --git a/src/gzip/ftgzip.c b/src/gzip/ftgzip.c
index e35c073..a78c195 100644
--- a/src/gzip/ftgzip.c
+++ b/src/gzip/ftgzip.c
@@ -565,7 +565,7 @@
     FT_ZERO( stream );
     stream->memory = memory;
 
-    if ( !FT_NEW( zip ) )
+    if ( !FT_QNEW( zip ) )
     {
       error = ft_gzip_file_init( zip, stream, source );
       if ( error )
diff --git a/src/sfnt/sfobjs.c b/src/sfnt/sfobjs.c
index 37b58a3..aef3f5d 100644
--- a/src/sfnt/sfobjs.c
+++ b/src/sfnt/sfobjs.c
@@ -267,7 +267,7 @@
         FT_UNUSED( error );
 
 
-        if ( FT_NEW_ARRAY  ( rec->string, rec->stringLength ) ||
+        if ( FT_QNEW_ARRAY ( rec->string, rec->stringLength ) ||
              FT_STREAM_SEEK( rec->stringOffset )              ||
              FT_STREAM_READ( rec->string, rec->stringLength ) )
         {
diff --git a/src/sfnt/ttload.c b/src/sfnt/ttload.c
index 9e21d7d..d923b88 100644
--- a/src/sfnt/ttload.c
+++ b/src/sfnt/ttload.c
@@ -433,7 +433,7 @@
 
     face->num_tables = sfnt->num_tables;
 
-    if ( FT_NEW_ARRAY( face->dir_tables, face->num_tables ) )
+    if ( FT_QNEW_ARRAY( face->dir_tables, face->num_tables ) )
       goto Exit;
 
     if ( FT_STREAM_SEEK( sfnt->offset + 12 )      ||
@@ -888,8 +888,8 @@
       goto Exit;
     }
 
-    if ( FT_NEW_ARRAY( *longs,  num_longs  ) ||
-         FT_NEW_ARRAY( *shorts, num_shorts ) )
+    if ( FT_QNEW_ARRAY( *longs,  num_longs  ) ||
+         FT_QNEW_ARRAY( *shorts, num_shorts ) )
       goto Exit;
 
     if ( FT_FRAME_ENTER( table_len ) )
@@ -1586,7 +1586,7 @@
     num_ranges = face->gasp.numRanges;
     FT_TRACE3(( "number of ranges = %d\n", num_ranges ));
 
-    if ( FT_NEW_ARRAY( gaspranges, num_ranges ) ||
+    if ( FT_QNEW_ARRAY( gaspranges, num_ranges ) ||
          FT_FRAME_ENTER( num_ranges * 4L )      )
       goto Exit;
 
@@ -1635,6 +1635,11 @@
   /* <Return>                                                              */
   /*    FreeType error code.  0 means success.                             */
   /*                                                                       */
+  
+#undef  TT_KERN_INDEX
+#define TT_KERN_INDEX( g1, g2 )  ( ( (FT_ULong)g1 << 16 ) | g2 )
+
+
   FT_LOCAL_DEF( FT_Error )
   tt_face_load_kern( TT_Face    face,
                      FT_Stream  stream )
@@ -1691,8 +1696,8 @@
         FT_FRAME_EXIT();
 
         /* allocate array of kerning pairs */
-        if ( FT_NEW_ARRAY( face->kern_pairs, num_pairs ) ||
-             FT_FRAME_ENTER( 6L * num_pairs )            )
+        if ( FT_QNEW_ARRAY( face->kern_pairs, num_pairs ) ||
+             FT_FRAME_ENTER( 6L * num_pairs )             )
           goto Exit;
 
         pair  = face->kern_pairs;
@@ -1711,16 +1716,35 @@
 
         /* ensure that the kerning pair table is sorted (yes, some */
         /* fonts have unsorted tables!)                            */
+#if 1
+        if ( num_pairs > 0 )     
+        {
+          TT_Kern0_Pair  pair0 = face->kern_pairs;
+          FT_ULong       prev  = TT_KERN_INDEX( pair0->left, pair0->right );
+          
+          for ( pair0++; pair0 < limit; pair0++ )
+          {
+            FT_ULong  next = TT_KERN_INDEX( pair0->left, pair0->right );
+            
+            if ( next < prev )
+              goto SortIt;
+              
+            prev = next;
+          }
+          goto Exit;
+          
+        SortIt:
+          ft_qsort( (void*)face->kern_pairs, (int)num_pairs,
+                    sizeof ( TT_Kern0_PairRec ), tt_kern_pair_compare );
+        }
+#else        
         {
+          TT_Kern0_Pair  pair0    = face->kern_pairs;
           FT_UInt        i;
-          TT_Kern0_Pair  pair0;
-
-
-          pair0 = face->kern_pairs;
-
+          
           for ( i = 1; i < num_pairs; i++, pair0++ )
           {
-            if ( tt_kern_pair_compare( pair0, pair0 + 1 ) != -1 )
+            if ( tt_kern_pair_compare( pair0, pair0+1 ) != -1 )
             {
               ft_qsort( (void*)face->kern_pairs, (int)num_pairs,
                         sizeof ( TT_Kern0_PairRec ), tt_kern_pair_compare );
@@ -1728,7 +1752,7 @@
             }
           }
         }
-
+#endif
         goto Exit;
       }
 
@@ -1746,10 +1770,6 @@
   }
 
 
-#undef  TT_KERN_INDEX
-#define TT_KERN_INDEX( g1, g2 )  ( ( (FT_ULong)g1 << 16 ) | g2 )
-
-
   FT_CALLBACK_DEF( int )
   tt_kern_pair_compare( const void*  a,
                         const void*  b )
@@ -1767,6 +1787,7 @@
 
 
 #undef TT_KERN_INDEX
+  
 
 
   /*************************************************************************/
@@ -1820,7 +1841,7 @@
     if ( hdmx->version != 0 )
       goto Exit;
 
-    if ( FT_NEW_ARRAY( hdmx->records, num_records ) )
+    if ( FT_QNEW_ARRAY( hdmx->records, num_records ) )
       goto Exit;
 
     hdmx->num_records = num_records;
@@ -1839,7 +1860,7 @@
              FT_READ_BYTE( cur->max_width ) )
           goto Exit;
 
-        if ( FT_ALLOC( cur->widths, num_glyphs )       ||
+        if ( FT_QALLOC( cur->widths, num_glyphs )       ||
              FT_STREAM_READ( cur->widths, num_glyphs ) )
           goto Exit;