Hash :
f9b8dec4
Author :
Date :
2000-06-16T19:34:52
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 512 513
/***************************************************************************/
/* */
/* t2load.c */
/* */
/* TrueType glyph data/program tables loader (body). */
/* */
/* Copyright 1996-2000 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 <freetype/internal/ftdebug.h>
#include <freetype/internal/ftobjs.h>
#include <freetype/internal/ftstream.h>
#include <freetype/internal/psnames.h>
#include <freetype/internal/t2errors.h>
#include <freetype/tttags.h>
#include <t2load.h>
#include <t2parse.h>
/*************************************************************************/
/* */
/* The macro FT_COMPONENT is used in trace mode. It is an implicit */
/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
/* messages during execution. */
/* */
#undef FT_COMPONENT
#define FT_COMPONENT trace_t2load
/* read a CFF offset from memory */
static
FT_ULong t2_get_offset( FT_Byte* p,
FT_Byte off_size )
{
FT_ULong result;
for ( result = 0; off_size > 0; off_size-- )
result = ( result <<= 8 ) | *p++;
return result;
}
static
FT_Error t2_new_cff_index( CFF_Index* index,
FT_Stream stream,
FT_Bool load )
{
FT_Error error;
FT_Memory memory = stream->memory;
FT_UShort count;
MEM_Set( index, 0, sizeof ( *index ) );
index->stream = stream;
if ( !READ_UShort( count ) &&
count > 0 )
{
FT_Byte* p;
FT_Byte offsize;
FT_ULong data_size;
FT_ULong* poff;
/* there is at least one element; read the offset size */
/* then access the offset table to compute the index's total size */
if ( READ_Byte( offsize ) )
goto Exit;
index->stream = stream;
index->count = count;
index->off_size = offsize;
data_size = (FT_ULong)( count + 1 ) * offsize;
if ( ALLOC_ARRAY( index->offsets, count + 1, FT_ULong ) ||
ACCESS_Frame( data_size ) )
goto Exit;
poff = index->offsets;
p = (FT_Byte*)stream->cursor;
for ( ; (FT_Short)count >= 0; count-- )
{
poff[0] = t2_get_offset( p, offsize );
poff++;
p += offsize;
}
FORGET_Frame();
index->data_offset = FILE_Pos();
data_size = poff[-1] - 1;
if ( load )
{
/* load the data */
if ( EXTRACT_Frame( data_size, index->bytes ) )
goto Exit;
}
else
{
/* skip the data */
(void)FILE_Skip( data_size );
}
}
Exit:
if ( error )
FREE( index->offsets );
return error;
}
static
void t2_done_cff_index( CFF_Index* index )
{
if ( index->stream )
{
FT_Stream stream = index->stream;
FT_Memory memory = stream->memory;
if ( index->bytes )
RELEASE_Frame( index->bytes );
FREE( index->offsets );
MEM_Set( index, 0, sizeof ( *index ) );
}
}
static
FT_Error t2_explicit_cff_index( CFF_Index* index,
FT_Byte*** table )
{
FT_Error error = 0;
FT_Memory memory = index->stream->memory;
FT_UInt n, offset, old_offset;
FT_Byte** t;
*table = 0;
if ( index->count > 0 && !ALLOC_ARRAY( t, index->count + 1, FT_Byte* ) )
{
old_offset = 1;
for ( n = 0; n <= index->count; n++ )
{
offset = index->offsets[n];
if ( !offset )
offset = old_offset;
t[n] = index->bytes + offset - 1;
old_offset = offset;
}
*table = t;
}
return error;
}
LOCAL_FUNC
FT_Error T2_Access_Element( CFF_Index* index,
FT_UInt element,
FT_Byte** pbytes,
FT_ULong* pbyte_len )
{
FT_Error error = 0;
if ( index && index->count > element )
{
/* compute start and end offsets */
FT_ULong off1, off2;
off1 = index->offsets[element];
if ( off1 )
{
do
{
element++;
off2 = index->offsets[element];
}
while ( off2 == 0 && element < index->count );
if ( !off2 )
off1 = 0;
}
/* access element */
if ( off1 )
{
*pbyte_len = off2 - off1;
if ( index->bytes )
{
/* this index was completely loaded in memory, that's easy */
*pbytes = index->bytes + off1 - 1;
}
else
{
/* this index is still on disk/file, access it through a frame */
FT_Stream stream = index->stream;
if ( FILE_Seek( index->data_offset + off1 - 1 ) ||
EXTRACT_Frame( off2 - off1, *pbytes ) )
goto Exit;
}
}
else
{
/* empty index element */
*pbytes = 0;
*pbyte_len = 0;
}
}
else
error = T2_Err_Invalid_Argument;
Exit:
return error;
}
LOCAL_FUNC
void T2_Forget_Element( CFF_Index* index,
FT_Byte** pbytes )
{
if ( index->bytes == 0 )
{
FT_Stream stream = index->stream;
RELEASE_Frame( *pbytes );
}
}
LOCAL_FUNC
FT_String* T2_Get_Name( CFF_Index* index,
FT_UInt element )
{
FT_Memory memory = index->stream->memory;
FT_Byte* bytes;
FT_ULong byte_len;
FT_Error error;
FT_String* name = 0;
error = T2_Access_Element( index, element, &bytes, &byte_len );
if ( error )
goto Exit;
if ( !ALLOC( name, byte_len + 1 ) )
{
MEM_Copy( name, bytes, byte_len );
name[byte_len] = 0;
}
T2_Forget_Element( index, &bytes );
Exit:
return name;
}
#if 0 /* unused until we fully support pure-CFF fonts */
LOCAL_FUNC
FT_String* T2_Get_String( CFF_Index* index,
FT_UInt sid,
PSNames_Interface* interface )
{
/* if it is not a standard string, return it */
if ( sid > 390 )
return T2_Get_Name( index, sid - 390 );
/* that's a standard string, fetch a copy from the PSName module */
{
FT_String* name = 0;
const char* adobe_name = interface->adobe_std_strings( sid );
FT_UInt len;
if ( adobe_name )
{
FT_Memory memory = index->stream->memory;
FT_Error error;
len = (FT_UInt)strlen( adobe_name );
if ( !ALLOC( name, len + 1 ) )
{
MEM_Copy( name, adobe_name, len );
name[len] = 0;
}
}
return name;
}
}
#endif /* 0 */
LOCAL_FUNC
FT_Error T2_Load_CFF_Font( FT_Stream stream,
FT_Int face_index,
CFF_Font* font )
{
static const FT_Frame_Field cff_header_fields[] =
{
FT_FRAME_START( 4 ),
FT_FRAME_BYTE( CFF_Font, version_major ),
FT_FRAME_BYTE( CFF_Font, version_minor ),
FT_FRAME_BYTE( CFF_Font, header_size ),
FT_FRAME_BYTE( CFF_Font, absolute_offsize ),
FT_FRAME_END
};
FT_Error error;
FT_Memory memory = stream->memory;
FT_ULong base_offset;
MEM_Set( font, 0, sizeof ( *font ) );
font->stream = stream;
font->memory = memory;
base_offset = FILE_Pos();
/* read CFF font header */
if ( READ_Fields( cff_header_fields, font ) )
goto Exit;
/* check format */
if ( font->version_major != 1 ||
font->header_size < 4 ||
font->absolute_offsize > 4 )
{
FT_ERROR(( "incorrect CFF font header!\n" ));
error = FT_Err_Unknown_File_Format;
goto Exit;
}
/* skip the rest of the header */
(void)FILE_Skip( font->header_size - 4 );
/* read the name, top dict, string and global subrs index */
error = t2_new_cff_index( &font->name_index, stream, 0 ) ||
t2_new_cff_index( &font->top_dict_index, stream, 0 ) ||
t2_new_cff_index( &font->string_index, stream, 0 ) ||
t2_new_cff_index( &font->global_subrs_index, stream, 1 );
if ( error )
goto Exit;
/* well, we don't really forget the `disabled' fonts... */
font->num_faces = font->name_index.count;
if ( face_index >= font->num_faces )
{
FT_ERROR(( "T2_Load_CFF_Font: incorrect face index = %d\n",
face_index ));
error = T2_Err_Invalid_Argument;
}
/* in case of a font format check, simply exit now */
if ( face_index >= 0 )
{
T2_Parser parser;
FT_Byte* dict;
FT_ULong dict_len;
CFF_Index* index = &font->top_dict_index;
CFF_Top_Dict* top = &font->top_dict;
/* parse the top-level font dictionary */
T2_Parser_Init( &parser, T2CODE_TOPDICT, &font->top_dict );
/* set defaults */
memset( top, 0, sizeof ( *top ) );
top->underline_position = -100;
top->underline_thickness = 50;
top->charstring_type = 2;
top->font_matrix.xx = 0x10000L;
top->font_matrix.yy = 0x10000L;
top->cid_count = 8720;
error = T2_Access_Element( index, face_index, &dict, &dict_len ) ||
T2_Parser_Run( &parser, dict, dict + dict_len );
T2_Forget_Element( &font->top_dict_index, &dict );
if ( error )
goto Exit;
/* parse the private dictionary, if any */
if (font->top_dict.private_offset && font->top_dict.private_size)
{
CFF_Private* priv = &font->private_dict;
/* set defaults */
priv->blue_shift = 7;
priv->blue_fuzz = 1;
priv->lenIV = -1;
priv->expansion_factor = (FT_Fixed)0.06 * 0x10000L;
priv->blue_scale = (FT_Fixed)0.039625 * 0x10000L;
T2_Parser_Init( &parser, T2CODE_PRIVATE, priv );
if ( FILE_Seek( base_offset + font->top_dict.private_offset ) ||
ACCESS_Frame( font->top_dict.private_size ) )
goto Exit;
error = T2_Parser_Run( &parser,
(FT_Byte*)stream->cursor,
(FT_Byte*)stream->limit );
FORGET_Frame();
if ( error )
goto Exit;
}
/* read the charstrings index now */
if ( font->top_dict.charstrings_offset == 0 )
{
FT_ERROR(( "T2_Load_CFF_Font: no charstrings offset!\n" ));
error = FT_Err_Unknown_File_Format;
goto Exit;
}
if ( FILE_Seek( base_offset + font->top_dict.charstrings_offset ) )
goto Exit;
error = t2_new_cff_index( &font->charstrings_index, stream, 0 );
if ( error )
goto Exit;
/* read the local subrs, if any */
if ( font->private_dict.local_subrs_offset )
{
if ( FILE_Seek( base_offset + font->top_dict.private_offset +
font->private_dict.local_subrs_offset ) )
goto Exit;
error = t2_new_cff_index( &font->local_subrs_index, stream, 1 );
if ( error )
goto Exit;
}
/* explicit the global and local subrs */
if ( font->private_dict.local_subrs_offset )
font->num_local_subrs = font->local_subrs_index.count;
else
font->num_local_subrs = 0;
font->num_global_subrs = font->global_subrs_index.count;
error = t2_explicit_cff_index( &font->global_subrs_index,
&font->global_subrs ) ;
if ( font->private_dict.local_subrs_offset )
error |= t2_explicit_cff_index( &font->local_subrs_index,
&font->local_subrs ) ;
if ( error )
goto Exit;
}
/* get the font name */
font->font_name = T2_Get_Name( &font->name_index, face_index );
Exit:
return error;
}
LOCAL_FUNC
void T2_Done_CFF_Font( CFF_Font* font )
{
FT_Memory memory = font->memory;
t2_done_cff_index( &font->global_subrs_index );
t2_done_cff_index( &font->string_index );
t2_done_cff_index( &font->top_dict_index );
t2_done_cff_index( &font->name_index );
t2_done_cff_index( &font->charstrings_index );
FREE( font->local_subrs );
FREE( font->global_subrs );
FREE( font->font_name );
}
/* END */