* src/cff/cffload.c, src/cff/cffload.h, src/cff/cffgload.c, src/cff/cfftypes.h: formatting + do not load the CFF index offsets into memory, since this wastes a *lot* of heap memory with large Asian CFF fonts. There is no significant performance loss
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 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
diff --git a/ChangeLog b/ChangeLog
index 6d61661..112aa4e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,10 @@
2007-01-04 David Turner <david@freetype.org>
- * src/cff/cffload.c: formatting
+ * src/cff/cffload.c, src/cff/cffload.h, src/cff/cffgload.c,
+ src/cff/cfftypes.h: formatting + do not load the CFF index
+ offsets into memory, since this wastes a *lot* of heap memory
+ with large Asian CFF fonts. There is no significant performance
+ loss
2007-01-04 David Turner <david@freetype.org>
diff --git a/src/cff/cffgload.c b/src/cff/cffgload.c
index ca6bb0f..c031e03 100644
--- a/src/cff/cffgload.c
+++ b/src/cff/cffgload.c
@@ -2291,9 +2291,8 @@
if ( cff->top_font.font_dict.cid_registry != 0xFFFFU &&
cff->charset.cids )
{
- if ( glyph_index < cff->charset.max_cid )
- glyph_index = cff->charset.cids[glyph_index];
- else
+ glyph_index = cff_charset_cid_to_gindex( &cff->charset, glyph_index );
+ if ( glyph_index == 0 )
return CFF_Err_Invalid_Argument;
}
else if ( glyph_index >= cff->num_glyphs )
@@ -2423,13 +2422,13 @@
/* See how charstring loads at cff_index_access_element() in */
/* cffload.c. */
{
- CFF_IndexRec csindex = cff->charstrings_index;
-
+ CFF_Index csindex = &cff->charstrings_index;
- glyph->root.control_data =
- csindex.bytes + csindex.offsets[glyph_index] - 1;
- glyph->root.control_len =
- charstring_len;
+ if (csindex->offsets)
+ {
+ glyph->root.control_data = csindex->bytes + csindex->offsets[glyph_index] - 1;
+ glyph->root.control_len = charstring_len;
+ }
}
}
diff --git a/src/cff/cffload.c b/src/cff/cffload.c
index 5494bf6..cca1759 100644
--- a/src/cff/cffload.c
+++ b/src/cff/cffload.c
@@ -193,11 +193,33 @@
#undef FT_COMPONENT
#define FT_COMPONENT trace_cffload
+ /* read an offset from the index's stream current position */
+ static FT_ULong
+ cff_index_read_offset( CFF_Index idx,
+ FT_Error *perror )
+ {
+ FT_Error error;
+ FT_Stream stream = idx->stream;
+ FT_Byte tmp[4];
+ FT_ULong result = 0;
+
+ if ( !FT_STREAM_READ( tmp, idx->off_size ) )
+ {
+ FT_Int nn;
+
+ for ( nn = 0; nn < idx->off_size; nn++ )
+ result = (result << 8) | tmp[nn];
+ }
+
+ *perror = error;
+ return result;
+ }
+
static FT_Error
- cff_new_index( CFF_Index idx,
- FT_Stream stream,
- FT_Bool load )
+ cff_index_init( CFF_Index idx,
+ FT_Stream stream,
+ FT_Bool load )
{
FT_Error error;
FT_Memory memory = stream->memory;
@@ -207,14 +229,12 @@
FT_MEM_ZERO( idx, sizeof ( *idx ) );
idx->stream = stream;
+ idx->start = FT_STREAM_POS();
if ( !FT_READ_USHORT( count ) &&
count > 0 )
{
- FT_Byte* p;
FT_Byte offsize;
- FT_ULong data_size;
- FT_ULong* poff;
- FT_Byte* p_end;
+ FT_ULong size, last_offset;
/* there is at least one element; read the offset size, */
@@ -228,56 +248,37 @@
goto Exit;
}
- idx->stream = stream;
idx->count = count;
idx->off_size = offsize;
- data_size = (FT_ULong)( count + 1 ) * offsize;
+ size = (FT_ULong)( count + 1 ) * offsize;
- if ( FT_NEW_ARRAY( idx->offsets, count + 1 ) ||
- FT_FRAME_ENTER( data_size ) )
+ idx->data_offset = idx->start + 3 + size;
+
+ if ( FT_STREAM_SKIP( size - offsize ) )
goto Exit;
- poff = idx->offsets;
- p = (FT_Byte*)stream->cursor;
- p_end = p + data_size;
+ size = cff_index_read_offset( idx, &error );
+ if (error)
+ goto Exit;
- switch ( offsize )
+ if ( size == 0 )
{
- case 1:
- for ( ; p < p_end; p++, poff++ )
- poff[0] = p[0];
- break;
-
- case 2:
- for ( ; p < p_end; p += 2, poff++ )
- poff[0] = FT_PEEK_USHORT( p );
- break;
-
- case 3:
- for ( ; p < p_end; p += 3, poff++ )
- poff[0] = FT_PEEK_OFF3( p );
- break;
-
- default:
- for ( ; p < p_end; p += 4, poff++ )
- poff[0] = FT_PEEK_ULONG( p );
+ error = CFF_Err_Invalid_Table;
+ goto Exit;
}
- FT_FRAME_EXIT();
-
- idx->data_offset = FT_STREAM_POS();
- data_size = poff[-1] - 1;
+ idx->data_size = --size;
if ( load )
{
/* load the data */
- if ( FT_FRAME_EXTRACT( data_size, idx->bytes ) )
+ if ( FT_FRAME_EXTRACT( size, idx->bytes ) )
goto Exit;
}
else
{
/* skip the data */
- if ( FT_STREAM_SKIP( data_size ) )
+ if ( FT_STREAM_SKIP( size ) )
goto Exit;
}
}
@@ -291,7 +292,7 @@
static void
- cff_done_index( CFF_Index idx )
+ cff_index_done( CFF_Index idx )
{
if ( idx->stream )
{
@@ -308,6 +309,67 @@
}
+ static FT_Error
+ cff_index_load_offsets( CFF_Index idx )
+ {
+ FT_Error error = 0;
+ FT_Stream stream = idx->stream;
+ FT_Memory memory = stream->memory;
+
+ if ( idx->count > 0 && idx->offsets == NULL )
+ {
+ FT_Byte offsize = idx->off_size;
+ FT_ULong data_size;
+ FT_Byte* p;
+ FT_Byte* p_end;
+ FT_ULong* poff;
+
+ data_size = (FT_ULong)( idx->count + 1 ) * offsize;
+
+ if ( FT_NEW_ARRAY( idx->offsets, idx->count + 1 ) ||
+ FT_STREAM_SEEK( idx->start + 3 ) ||
+ FT_FRAME_ENTER( data_size ) )
+ goto Exit;
+
+ poff = idx->offsets;
+ p = (FT_Byte*)stream->cursor;
+ p_end = p + data_size;
+
+ switch ( offsize )
+ {
+ case 1:
+ for ( ; p < p_end; p++, poff++ )
+ poff[0] = p[0];
+ break;
+
+ case 2:
+ for ( ; p < p_end; p += 2, poff++ )
+ poff[0] = FT_PEEK_USHORT( p );
+ break;
+
+ case 3:
+ for ( ; p < p_end; p += 3, poff++ )
+ poff[0] = FT_PEEK_OFF3( p );
+ break;
+
+ default:
+ for ( ; p < p_end; p += 4, poff++ )
+ poff[0] = FT_PEEK_ULONG( p );
+ }
+
+ FT_FRAME_EXIT();
+ }
+
+ Exit:
+ if (error)
+ FT_FREE( idx->offsets );
+
+ return error;
+ }
+
+
+
+
/* allocate a table containing pointers to an index's elements */
static FT_Error
cff_index_get_pointers( CFF_Index idx,
@@ -321,6 +383,13 @@
*table = 0;
+ if ( idx->offsets == NULL )
+ {
+ error = cff_index_load_offsets( idx );
+ if ( error )
+ goto Exit;
+ }
+
if ( idx->count > 0 && !FT_NEW_ARRAY( t, idx->count + 1 ) )
{
old_offset = 1;
@@ -330,6 +399,10 @@
if ( !offset )
offset = old_offset;
+ /* sanity check for invalid offset tables */
+ else if ( offset < old_offset || offset-1 >= idx->data_size )
+ offset = old_offset;
+
t[n] = idx->bytes + offset - 1;
old_offset = offset;
@@ -337,6 +410,7 @@
*table = t;
}
+ Exit:
return error;
}
@@ -353,21 +427,43 @@
if ( idx && idx->count > element )
{
/* compute start and end offsets */
- FT_ULong off1, off2 = 0;
+ FT_Stream stream = idx->stream;
+ FT_ULong off1, off2 = 0;
- off1 = idx->offsets[element];
- if ( off1 )
+ /* load offsets from file or the offset table */
+ if ( !idx->offsets )
{
- do
- {
- element++;
- off2 = idx->offsets[element];
+ FT_ULong pos = element*idx->off_size;
- } while ( off2 == 0 && element < idx->count );
+ if ( FT_STREAM_SEEK( idx->start + 3 + pos ) )
+ goto Exit;
+
+ off1 = cff_index_read_offset( idx, &error );
+ if (error) goto Exit;
+
+ if ( off1 != 0 )
+ {
+ do
+ {
+ element++;
+ off2 = cff_index_read_offset( idx, &error );
+ }
+ while ( off2 == 0 && element < idx->count );
+ }
+ }
+ else /* use offsets table */
+ {
+ off1 = idx->offsets[element];
+ if ( off1 )
+ {
+ do
+ {
+ element++;
+ off2 = idx->offsets[element];
- if ( !off2 )
- off1 = 0;
+ } while ( off2 == 0 && element < idx->count );
+ }
}
/* access element */
@@ -383,9 +479,6 @@
else
{
/* this index is still on disk/file, access it through a frame */
- FT_Stream stream = idx->stream;
-
-
if ( FT_STREAM_SEEK( idx->data_offset + off1 - 1 ) ||
FT_FRAME_EXTRACT( off2 - off1, *pbytes ) )
goto Exit;
@@ -660,6 +753,18 @@
}
+ FT_LOCAL_DEF( FT_UInt )
+ cff_charset_cid_to_gindex( CFF_Charset charset,
+ FT_UInt cid )
+ {
+ FT_UInt result = 0;
+
+ if ( cid < charset->max_cid )
+ result = charset->cids[cid];
+
+ return result;
+ }
+
static void
cff_charset_free_cids( CFF_Charset charset,
FT_Memory memory )
@@ -1209,7 +1314,7 @@
priv->local_subrs_offset ) )
goto Exit;
- error = cff_new_index( &font->local_subrs_index, stream, 1 );
+ error = cff_index_init( &font->local_subrs_index, stream, 1 );
if ( error )
goto Exit;
@@ -1231,7 +1336,7 @@
{
if ( subfont )
{
- cff_done_index( &subfont->local_subrs_index );
+ cff_index_done( &subfont->local_subrs_index );
FT_FREE( subfont->local_subrs );
}
}
@@ -1287,10 +1392,10 @@
goto Exit;
/* read the name, top dict, string and global subrs index */
- if ( FT_SET_ERROR( cff_new_index( &font->name_index, stream, 0 )) ||
- FT_SET_ERROR( cff_new_index( &font->font_dict_index, stream, 0 )) ||
- FT_SET_ERROR( cff_new_index( &font->string_index, stream, 0 )) ||
- FT_SET_ERROR( cff_new_index( &font->global_subrs_index, stream, 1 )) )
+ if ( FT_SET_ERROR( cff_index_init( &font->name_index, stream, 0 )) ||
+ FT_SET_ERROR( cff_index_init( &font->font_dict_index, stream, 0 )) ||
+ FT_SET_ERROR( cff_index_init( &font->string_index, stream, 0 )) ||
+ FT_SET_ERROR( cff_index_init( &font->global_subrs_index, stream, 1 )) )
goto Exit;
/* well, we don't really forget the `disabled' fonts... */
@@ -1318,7 +1423,7 @@
if ( FT_STREAM_SEEK( base_offset + dict->charstrings_offset ) )
goto Exit;
- error = cff_new_index( &font->charstrings_index, stream, 0 );
+ error = cff_index_init( &font->charstrings_index, stream, 0 );
if ( error )
goto Exit;
@@ -1335,7 +1440,7 @@
if ( FT_STREAM_SEEK( base_offset + dict->cid_fd_array_offset ) )
goto Exit;
- error = cff_new_index( &fd_index, stream, 0 );
+ error = cff_index_init( &fd_index, stream, 0 );
if ( error )
goto Exit;
@@ -1371,7 +1476,7 @@
base_offset + dict->cid_fd_select_offset );
Fail_CID:
- cff_done_index( &fd_index );
+ cff_index_done( &fd_index );
if ( error )
goto Exit;
@@ -1441,11 +1546,11 @@
FT_UInt idx;
- cff_done_index( &font->global_subrs_index );
- cff_done_index( &font->string_index );
- cff_done_index( &font->font_dict_index );
- cff_done_index( &font->name_index );
- cff_done_index( &font->charstrings_index );
+ cff_index_done( &font->global_subrs_index );
+ cff_index_done( &font->string_index );
+ cff_index_done( &font->font_dict_index );
+ cff_index_done( &font->name_index );
+ cff_index_done( &font->charstrings_index );
/* release font dictionaries, but only if working with */
/* a CID keyed CFF font */
diff --git a/src/cff/cffload.h b/src/cff/cffload.h
index ccf8ada..13ee09d 100644
--- a/src/cff/cffload.h
+++ b/src/cff/cffload.h
@@ -52,6 +52,10 @@ FT_BEGIN_HEADER
FT_Byte** pbytes );
+ FT_LOCAL( FT_UInt )
+ cff_charset_cid_to_gindex( CFF_Charset charset,
+ FT_UInt cid );
+
FT_LOCAL( FT_Error )
cff_font_load( FT_Stream stream,
FT_Int face_index,
diff --git a/src/cff/cfftypes.h b/src/cff/cfftypes.h
index 364b7cb..07edfbd 100644
--- a/src/cff/cfftypes.h
+++ b/src/cff/cfftypes.h
@@ -39,6 +39,9 @@ FT_BEGIN_HEADER
/* <Fields> */
/* stream :: The source input stream. */
/* */
+ /* start :: The position of the first index byte in the */
+ /* input stream */
+ /* */
/* count :: The number of elements in the index. */
/* */
/* off_size :: The size in bytes of object offsets in index. */
@@ -46,16 +49,21 @@ FT_BEGIN_HEADER
/* data_offset :: The position of first data byte in the index's */
/* bytes. */
/* */
- /* offsets :: A table of element offsets in the index. */
+ /* data_size :: size of data table in this index */
+ /* */
+ /* offsets :: A table of element offsets in the index. must be */
+ /* loaded explicitely */
/* */
/* bytes :: If the index is loaded in memory, its bytes. */
/* */
typedef struct CFF_IndexRec_
{
FT_Stream stream;
+ FT_ULong start;
FT_UInt count;
FT_Byte off_size;
FT_ULong data_offset;
+ FT_ULong data_size;
FT_ULong* offsets;
FT_Byte* bytes;
@@ -85,6 +93,8 @@ FT_BEGIN_HEADER
FT_UShort* cids; /* the inverse mapping of `sids'; only needed */
/* for CID-keyed fonts */
FT_UInt max_cid;
+ FT_UInt num_glyphs;
+
} CFF_CharsetRec, *CFF_Charset;