Hash :
b1aabf4c
Author :
Date :
2000-11-30T23:48:22
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 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
/***************************************************************************/
/* */
/* ttcmap.c */
/* */
/* TrueType character mapping table (cmap) support (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/tterrors.h>
#ifdef FT_FLAT_COMPILE
#include "ttload.h"
#include "ttcmap.h"
#else
#include <sfnt/ttload.h>
#include <sfnt/ttcmap.h>
#endif
/*************************************************************************/
/* */
/* 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_ttcmap
FT_CALLBACK_DEF
FT_UInt code_to_index0( TT_CMapTable* charmap,
FT_ULong char_code );
FT_CALLBACK_DEF
FT_UInt code_to_index2( TT_CMapTable* charmap,
FT_ULong char_code );
FT_CALLBACK_DEF
FT_UInt code_to_index4( TT_CMapTable* charmap,
FT_ULong char_code );
FT_CALLBACK_DEF
FT_UInt code_to_index6( TT_CMapTable* charmap,
FT_ULong char_code );
/*************************************************************************/
/* */
/* <Function> */
/* TT_CharMap_Load */
/* */
/* <Description> */
/* Loads a given TrueType character map into memory. */
/* */
/* <Input> */
/* face :: A handle to the parent face object. */
/* stream :: A handle to the current stream object. */
/* */
/* <InOut> */
/* table :: A pointer to a cmap object. */
/* */
/* <Return> */
/* FreeType error code. 0 means success. */
/* */
/* <Note> */
/* The function assumes that the stream is already in use (i.e., */
/* opened). In case of error, all partially allocated tables are */
/* released. */
/* */
FT_LOCAL_DEF
FT_Error TT_CharMap_Load( TT_Face face,
TT_CMapTable* cmap,
FT_Stream stream )
{
FT_Error error;
FT_Memory memory;
FT_UShort num_SH, num_Seg, i;
FT_UShort u, l;
TT_CMap0* cmap0;
TT_CMap2* cmap2;
TT_CMap4* cmap4;
TT_CMap6* cmap6;
TT_CMap2SubHeader* cmap2sub;
TT_CMap4Segment* segments;
if ( cmap->loaded )
return TT_Err_Ok;
memory = stream->memory;
if ( FILE_Seek( cmap->offset ) )
return error;
switch ( cmap->format )
{
case 0:
cmap0 = &cmap->c.cmap0;
if ( ALLOC( cmap0->glyphIdArray, 256L ) ||
FILE_Read( cmap0->glyphIdArray, 256L ) )
goto Fail;
cmap->get_index = code_to_index0;
break;
case 2:
num_SH = 0;
cmap2 = &cmap->c.cmap2;
/* allocate subheader keys */
if ( ALLOC_ARRAY( cmap2->subHeaderKeys, 256, FT_UShort ) ||
ACCESS_Frame( 512L ) )
goto Fail;
for ( i = 0; i < 256; i++ )
{
u = GET_UShort() / 8;
cmap2->subHeaderKeys[i] = u;
if ( num_SH < u )
num_SH = u;
}
FORGET_Frame();
/* load subheaders */
cmap2->numGlyphId = l =
( ( cmap->length - 2L * ( 256 + 3 ) - num_SH * 8L ) & 0xFFFF ) / 2;
if ( ALLOC_ARRAY( cmap2->subHeaders,
num_SH + 1,
TT_CMap2SubHeader ) ||
ACCESS_Frame( ( num_SH + 1 ) * 8L ) )
goto Fail;
cmap2sub = cmap2->subHeaders;
for ( i = 0; i <= num_SH; i++ )
{
cmap2sub->firstCode = GET_UShort();
cmap2sub->entryCount = GET_UShort();
cmap2sub->idDelta = GET_Short();
/* we apply the location offset immediately */
cmap2sub->idRangeOffset = GET_UShort() - ( num_SH - i ) * 8 - 2;
cmap2sub++;
}
FORGET_Frame();
/* load glyph IDs */
if ( ALLOC_ARRAY( cmap2->glyphIdArray, l, FT_UShort ) ||
ACCESS_Frame( l * 2L ) )
goto Fail;
for ( i = 0; i < l; i++ )
cmap2->glyphIdArray[i] = GET_UShort();
FORGET_Frame();
cmap->get_index = code_to_index2;
break;
case 4:
cmap4 = &cmap->c.cmap4;
/* load header */
if ( ACCESS_Frame( 8L ) )
goto Fail;
cmap4->segCountX2 = GET_UShort();
cmap4->searchRange = GET_UShort();
cmap4->entrySelector = GET_UShort();
cmap4->rangeShift = GET_UShort();
num_Seg = cmap4->segCountX2 / 2;
FORGET_Frame();
/* load segments */
if ( ALLOC_ARRAY( cmap4->segments,
num_Seg,
TT_CMap4Segment ) ||
ACCESS_Frame( ( num_Seg * 4 + 1 ) * 2L ) )
goto Fail;
segments = cmap4->segments;
for ( i = 0; i < num_Seg; i++ )
segments[i].endCount = GET_UShort();
(void)GET_UShort();
for ( i = 0; i < num_Seg; i++ )
segments[i].startCount = GET_UShort();
for ( i = 0; i < num_Seg; i++ )
segments[i].idDelta = GET_Short();
for ( i = 0; i < num_Seg; i++ )
segments[i].idRangeOffset = GET_UShort();
FORGET_Frame();
cmap4->numGlyphId = l =
( ( cmap->length - ( 16L + 8L * num_Seg ) ) & 0xFFFF ) / 2;
/* load IDs */
if ( ALLOC_ARRAY( cmap4->glyphIdArray, l, FT_UShort ) ||
ACCESS_Frame( l * 2L ) )
goto Fail;
for ( i = 0; i < l; i++ )
cmap4->glyphIdArray[i] = GET_UShort();
FORGET_Frame();
cmap->get_index = code_to_index4;
cmap4->last_segment = cmap4->segments;
break;
case 6:
cmap6 = &cmap->c.cmap6;
if ( ACCESS_Frame( 4L ) )
goto Fail;
cmap6->firstCode = GET_UShort();
cmap6->entryCount = GET_UShort();
FORGET_Frame();
l = cmap6->entryCount;
if ( ALLOC_ARRAY( cmap6->glyphIdArray,
cmap6->entryCount,
FT_Short ) ||
ACCESS_Frame( l * 2L ) )
goto Fail;
for ( i = 0; i < l; i++ )
cmap6->glyphIdArray[i] = GET_UShort();
FORGET_Frame();
cmap->get_index = code_to_index6;
break;
default: /* corrupt character mapping table */
return TT_Err_Invalid_CharMap_Format;
}
return TT_Err_Ok;
Fail:
TT_CharMap_Free( face, cmap );
return error;
}
/*************************************************************************/
/* */
/* <Function> */
/* TT_CharMap_Free */
/* */
/* <Description> */
/* Destroys a character mapping table. */
/* */
/* <Input> */
/* face :: A handle to the parent face object. */
/* cmap :: A handle to a cmap object. */
/* */
/* <Return> */
/* FreeType error code. 0 means success. */
/* */
FT_LOCAL_DEF
FT_Error TT_CharMap_Free( TT_Face face,
TT_CMapTable* cmap )
{
FT_Memory memory;
if ( !cmap )
return TT_Err_Ok;
memory = face->root.driver->root.memory;
switch ( cmap->format )
{
case 0:
FREE( cmap->c.cmap0.glyphIdArray );
break;
case 2:
FREE( cmap->c.cmap2.subHeaderKeys );
FREE( cmap->c.cmap2.subHeaders );
FREE( cmap->c.cmap2.glyphIdArray );
break;
case 4:
FREE( cmap->c.cmap4.segments );
FREE( cmap->c.cmap4.glyphIdArray );
cmap->c.cmap4.segCountX2 = 0;
break;
case 6:
FREE( cmap->c.cmap6.glyphIdArray );
cmap->c.cmap6.entryCount = 0;
break;
default:
/* invalid table format, do nothing */
;
}
cmap->loaded = FALSE;
return TT_Err_Ok;
}
/*************************************************************************/
/* */
/* <Function> */
/* code_to_index0 */
/* */
/* <Description> */
/* Converts the character code into a glyph index. Uses format 0. */
/* `charCode' must be in the range 0x00-0xFF (otherwise 0 is */
/* returned). */
/* */
/* <Input> */
/* charCode :: The wanted character code. */
/* cmap0 :: A pointer to a cmap table in format 0. */
/* */
/* <Return> */
/* Glyph index into the glyphs array. 0 if the glyph does not exist. */
/* */
FT_CALLBACK_DEF
FT_UInt code_to_index0( TT_CMapTable* cmap,
FT_ULong charCode )
{
TT_CMap0* cmap0 = &cmap->c.cmap0;
return ( charCode <= 0xFF ? cmap0->glyphIdArray[charCode] : 0 );
}
/*************************************************************************/
/* */
/* <Function> */
/* code_to_index2 */
/* */
/* <Description> */
/* Converts the character code into a glyph index. Uses format 2. */
/* */
/* <Input> */
/* charCode :: The wanted character code. */
/* cmap2 :: A pointer to a cmap table in format 2. */
/* */
/* <Return> */
/* Glyph index into the glyphs array. 0 if the glyph does not exist. */
/* */
FT_CALLBACK_DEF
FT_UInt code_to_index2( TT_CMapTable* cmap,
FT_ULong charCode )
{
FT_UInt result, index1, offset;
FT_UInt char_lo;
FT_ULong char_hi;
TT_CMap2SubHeader* sh2;
TT_CMap2* cmap2;
cmap2 = &cmap->c.cmap2;
result = 0;
char_lo = (FT_UInt)( charCode & 0xFF );
char_hi = charCode >> 8;
if ( char_hi == 0 )
{
/* an 8-bit character code -- we use the subHeader 0 in this case */
/* to test whether the character code is in the charmap */
if ( cmap2->subHeaderKeys[char_lo] == 0 )
result = cmap2->glyphIdArray[char_lo];
}
else
{
/* a 16-bit character code */
index1 = cmap2->subHeaderKeys[char_hi & 0xFF];
if ( index1 )
{
sh2 = cmap2->subHeaders + index1;
char_lo -= sh2->firstCode;
if ( char_lo < (FT_UInt)sh2->entryCount )
{
offset = sh2->idRangeOffset / 2 + char_lo;
if ( offset < (FT_UInt)cmap2->numGlyphId )
{
result = cmap2->glyphIdArray[offset];
if ( result )
result = ( result + sh2->idDelta ) & 0xFFFF;
}
}
}
}
return result;
}
/*************************************************************************/
/* */
/* <Function> */
/* code_to_index4 */
/* */
/* <Description> */
/* Converts the character code into a glyph index. Uses format 4. */
/* */
/* <Input> */
/* charCode :: The wanted character code. */
/* cmap4 :: A pointer to a cmap table in format 4. */
/* */
/* <Return> */
/* Glyph index into the glyphs array. 0 if the glyph does not exist. */
/* */
FT_CALLBACK_DEF
FT_UInt code_to_index4( TT_CMapTable* cmap,
FT_ULong charCode )
{
FT_UInt result, index1, segCount;
TT_CMap4* cmap4;
TT_CMap4Segment *seg4, *limit;
cmap4 = &cmap->c.cmap4;
result = 0;
segCount = cmap4->segCountX2 / 2;
seg4 = cmap4->segments;
limit = seg4 + segCount;
/* check against the last segment */
seg4 = cmap4->last_segment;
/* the following is equivalent to performing two tests, as in */
/* */
/* if ( charCode >= seg4->startCount && charCode <= seg4->endCount ) */
/* */
/* Yes, that's a bit strange, but it's faster, and the idea behind */
/* the cache is to significantly speed up charcode to glyph index */
/* conversion. */
if ( (FT_ULong)(charCode - seg4->startCount) <
(FT_ULong)(seg4->endCount - seg4->startCount) )
goto Found;
for ( seg4 = cmap4->segments; seg4 < limit; seg4++ )
{
/* the ranges are sorted in increasing order. If we are out of */
/* the range here, the char code isn't in the charmap, so exit. */
if ( charCode > (FT_UInt)seg4->endCount )
continue;
if ( charCode >= (FT_UInt)seg4->startCount )
goto Found;
}
return 0;
Found:
cmap4->last_segment = seg4;
/* if the idRangeOffset is 0, we can compute the glyph index */
/* directly */
if ( seg4->idRangeOffset == 0 )
result = ( charCode + seg4->idDelta ) & 0xFFFF;
else
{
/* otherwise, we must use the glyphIdArray to do it */
index1 = seg4->idRangeOffset / 2
+ ( charCode - seg4->startCount )
+ ( seg4 - cmap4->segments )
- segCount;
if ( index1 < (FT_UInt)cmap4->numGlyphId &&
cmap4->glyphIdArray[index1] != 0 )
result = ( cmap4->glyphIdArray[index1] + seg4->idDelta ) & 0xFFFF;
}
return result;
}
/*************************************************************************/
/* */
/* <Function> */
/* code_to_index6 */
/* */
/* <Description> */
/* Converts the character code into a glyph index. Uses format 6. */
/* */
/* <Input> */
/* charCode :: The wanted character code. */
/* cmap6 :: A pointer to a cmap table in format 6. */
/* */
/* <Return> */
/* Glyph index into the glyphs array. 0 if the glyph does not exist. */
/* */
FT_CALLBACK_DEF
FT_UInt code_to_index6( TT_CMapTable* cmap,
FT_ULong charCode )
{
TT_CMap6* cmap6;
FT_UInt result = 0;
cmap6 = &cmap->c.cmap6;
result = 0;
charCode -= cmap6->firstCode;
if ( charCode < (FT_UInt)cmap6->entryCount )
result = cmap6->glyphIdArray[charCode];
return result;
}
/* END */