ttcmap0.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784
  1. /***************************************************************************/
  2. /* */
  3. /* ttcmap0.c */
  4. /* */
  5. /* TrueType new character mapping table (cmap) support (body). */
  6. /* */
  7. /* Copyright 2002 by */
  8. /* David Turner, Robert Wilhelm, and Werner Lemberg. */
  9. /* */
  10. /* This file is part of the FreeType project, and may only be used, */
  11. /* modified, and distributed under the terms of the FreeType project */
  12. /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
  13. /* this file you indicate that you have read the license and */
  14. /* understand and accept it fully. */
  15. /* */
  16. /***************************************************************************/
  17. #include <ft2build.h>
  18. #include FT_INTERNAL_DEBUG_H
  19. #include FT_INTERNAL_OBJECTS_H
  20. #include FT_INTERNAL_STREAM_H
  21. #include "ttload.h"
  22. #include "ttcmap0.h"
  23. #include "sferrors.h"
  24. /*************************************************************************/
  25. /* */
  26. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  27. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  28. /* messages during execution. */
  29. /* */
  30. #undef FT_COMPONENT
  31. #define FT_COMPONENT trace_ttcmap
  32. #define TT_PEEK_SHORT FT_PEEK_SHORT
  33. #define TT_PEEK_USHORT FT_PEEK_USHORT
  34. #define TT_PEEK_LONG FT_PEEK_LONG
  35. #define TT_PEEK_ULONG FT_PEEK_ULONG
  36. #define TT_NEXT_SHORT FT_NEXT_SHORT
  37. #define TT_NEXT_USHORT FT_NEXT_USHORT
  38. #define TT_NEXT_LONG FT_NEXT_LONG
  39. #define TT_NEXT_ULONG FT_NEXT_ULONG
  40. FT_CALLBACK_DEF( FT_Error )
  41. tt_cmap_init( TT_CMap cmap,
  42. FT_Byte* table )
  43. {
  44. cmap->data = table;
  45. return 0;
  46. }
  47. /*************************************************************************/
  48. /*************************************************************************/
  49. /***** *****/
  50. /***** FORMAT 0 *****/
  51. /***** *****/
  52. /*************************************************************************/
  53. /*************************************************************************/
  54. /*************************************************************************/
  55. /* */
  56. /* TABLE OVERVIEW */
  57. /* -------------- */
  58. /* */
  59. /* NAME OFFSET TYPE DESCRIPTION */
  60. /* */
  61. /* format 0 USHORT must be 0 */
  62. /* length 2 USHORT table length in bytes */
  63. /* language 4 USHORT Mac language code */
  64. /* glyph_ids 6 BYTE[256] array of glyph indices */
  65. /* 262 */
  66. /* */
  67. #ifdef TT_CONFIG_CMAP_FORMAT_0
  68. FT_CALLBACK_DEF( void )
  69. tt_cmap0_validate( FT_Byte* table,
  70. FT_Validator valid )
  71. {
  72. FT_Byte* p = table + 2;
  73. FT_UInt length = TT_NEXT_USHORT( p );
  74. if ( table + length > valid->limit || length < 262 )
  75. FT_INVALID_TOO_SHORT;
  76. /* check glyph indices whenever necessary */
  77. if ( valid->level >= FT_VALIDATE_TIGHT )
  78. {
  79. FT_UInt n, idx;
  80. p = table + 6;
  81. for ( n = 0; n < 256; n++ )
  82. {
  83. idx = *p++;
  84. if ( idx >= TT_VALID_GLYPH_COUNT( valid ) )
  85. FT_INVALID_GLYPH_ID;
  86. }
  87. }
  88. }
  89. FT_CALLBACK_DEF( FT_UInt )
  90. tt_cmap0_char_index( TT_CMap cmap,
  91. FT_UInt32 char_code )
  92. {
  93. FT_Byte* table = cmap->data;
  94. return char_code < 256 ? table[6 + char_code] : 0;
  95. }
  96. FT_CALLBACK_DEF( FT_UInt )
  97. tt_cmap0_char_next( TT_CMap cmap,
  98. FT_UInt32 *pchar_code )
  99. {
  100. FT_Byte* table = cmap->data;
  101. FT_UInt32 charcode = *pchar_code;
  102. FT_UInt32 result = 0;
  103. FT_UInt gindex = 0;
  104. table += 6; /* go to glyph ids */
  105. while ( ++charcode < 256 )
  106. {
  107. gindex = table[charcode];
  108. if ( gindex != 0 )
  109. {
  110. result = charcode;
  111. break;
  112. }
  113. }
  114. *pchar_code = result;
  115. return gindex;
  116. }
  117. FT_CALLBACK_TABLE_DEF
  118. const TT_CMap_ClassRec tt_cmap0_class_rec =
  119. {
  120. {
  121. sizeof( TT_CMapRec ),
  122. (FT_CMap_InitFunc) tt_cmap_init,
  123. (FT_CMap_DoneFunc) NULL,
  124. (FT_CMap_CharIndexFunc)tt_cmap0_char_index,
  125. (FT_CMap_CharNextFunc) tt_cmap0_char_next
  126. },
  127. 0,
  128. (TT_CMap_ValidateFunc) tt_cmap0_validate
  129. };
  130. #endif /* TT_CONFIG_CMAP_FORMAT_0 */
  131. /*************************************************************************/
  132. /*************************************************************************/
  133. /***** *****/
  134. /***** FORMAT 2 *****/
  135. /***** *****/
  136. /***** This is used for certain CJK encodings that encode text in a *****/
  137. /***** mixed 8/16 bits encoding along the following lines: *****/
  138. /***** *****/
  139. /***** * Certain byte values correspond to an 8-bit character code *****/
  140. /***** (typically in the range 0..127 for ASCII compatibility). *****/
  141. /***** *****/
  142. /***** * Certain byte values signal the first byte of a 2-byte *****/
  143. /***** character code (but these values are also valid as the *****/
  144. /***** second byte of a 2-byte character). *****/
  145. /***** *****/
  146. /***** The following charmap lookup and iteration functions all *****/
  147. /***** assume that the value "charcode" correspond to following: *****/
  148. /***** *****/
  149. /***** - For one byte characters, "charcode" is simply the *****/
  150. /***** character code. *****/
  151. /***** *****/
  152. /***** - For two byte characters, "charcode" is the 2-byte *****/
  153. /***** character code in big endian format. More exactly: *****/
  154. /***** *****/
  155. /***** (charcode >> 8) is the first byte value *****/
  156. /***** (charcode & 0xFF) is the second byte value *****/
  157. /***** *****/
  158. /***** Note that not all values of "charcode" are valid according *****/
  159. /***** to these rules, and the function moderately check the *****/
  160. /***** arguments. *****/
  161. /***** *****/
  162. /*************************************************************************/
  163. /*************************************************************************/
  164. /*************************************************************************/
  165. /* */
  166. /* TABLE OVERVIEW */
  167. /* -------------- */
  168. /* */
  169. /* NAME OFFSET TYPE DESCRIPTION */
  170. /* */
  171. /* format 0 USHORT must be 2 */
  172. /* length 2 USHORT table length in bytes */
  173. /* language 4 USHORT Mac language code */
  174. /* keys 6 USHORT[256] sub-header keys */
  175. /* subs 518 SUBHEAD[NSUBS] sub-headers array */
  176. /* glyph_ids 518+NSUB*8 USHORT[] glyph id array */
  177. /* */
  178. /* The `keys' table is used to map charcode high-bytes to sub-headers. */
  179. /* The value of `NSUBS' is the number of sub-headers defined in the */
  180. /* table and is computed by finding the maximum of the `keys' table. */
  181. /* */
  182. /* Note that for any n, `keys[n]' is a byte offset within the `subs' */
  183. /* table, i.e., it is the corresponding sub-header index multiplied */
  184. /* by 8. */
  185. /* */
  186. /* Each sub-header has the following format: */
  187. /* */
  188. /* NAME OFFSET TYPE DESCRIPTION */
  189. /* */
  190. /* first 0 USHORT first valid low-byte */
  191. /* count 2 USHORT number of valid low-bytes */
  192. /* delta 4 SHORT see below */
  193. /* offset 6 USHORT see below */
  194. /* */
  195. /* A sub-header defines, for each high-byte, the range of valid */
  196. /* low-bytes within the charmap. Note that the range defined by `first' */
  197. /* and `count' must be completely included in the interval [0..255] */
  198. /* according to the specification. */
  199. /* */
  200. /* If a character code is contained within a given sub-header, then */
  201. /* mapping it to a glyph index is done as follows: */
  202. /* */
  203. /* * The value of `offset' is read. This is a _byte_ distance from the */
  204. /* location of the `offset' field itself into a slice of the */
  205. /* `glyph_ids' table. Let's call it `slice' (it's a USHORT[] too). */
  206. /* */
  207. /* * The value `slice[char.lo - first]' is read. If it is 0, there is */
  208. /* no glyph for the charcode. Otherwise, the value of `delta' is */
  209. /* added to it (modulo 65536) to form a new glyph index. */
  210. /* */
  211. /* It is up to the validation routine to check that all offsets fall */
  212. /* within the glyph ids table (and not within the `subs' table itself or */
  213. /* outside of the CMap). */
  214. /* */
  215. #ifdef TT_CONFIG_CMAP_FORMAT_2
  216. FT_CALLBACK_DEF( void )
  217. tt_cmap2_validate( FT_Byte* table,
  218. FT_Validator valid )
  219. {
  220. FT_Byte* p = table + 2; /* skip format */
  221. FT_UInt length = TT_PEEK_USHORT( p );
  222. FT_UInt n, max_subs;
  223. FT_Byte* keys; /* keys table */
  224. FT_Byte* subs; /* sub-headers */
  225. FT_Byte* glyph_ids; /* glyph id array */
  226. if ( table + length > valid->limit || length < 6 + 512 )
  227. FT_INVALID_TOO_SHORT;
  228. keys = table + 6;
  229. /* parse keys to compute sub-headers count */
  230. p = keys;
  231. max_subs = 0;
  232. for ( n = 0; n < 256; n++ )
  233. {
  234. FT_UInt idx = TT_NEXT_USHORT( p );
  235. /* value must be multiple of 8 */
  236. if ( valid->level >= FT_VALIDATE_PARANOID && ( idx & 7 ) != 0 )
  237. FT_INVALID_DATA;
  238. idx >>= 3;
  239. if ( idx > max_subs )
  240. max_subs = idx;
  241. }
  242. FT_ASSERT( p == table + 518 );
  243. subs = p;
  244. glyph_ids = subs + (max_subs + 1) * 8;
  245. if ( glyph_ids > valid->limit )
  246. FT_INVALID_TOO_SHORT;
  247. /* parse sub-headers */
  248. for ( n = 0; n <= max_subs; n++ )
  249. {
  250. FT_UInt first_code, code_count, offset;
  251. FT_Int delta;
  252. FT_Byte* ids;
  253. first_code = TT_NEXT_USHORT( p );
  254. code_count = TT_NEXT_USHORT( p );
  255. delta = TT_NEXT_SHORT( p );
  256. offset = TT_NEXT_USHORT( p );
  257. /* check range within 0..255 */
  258. if ( valid->level >= FT_VALIDATE_PARANOID )
  259. {
  260. if ( first_code >= 256 || first_code + code_count > 256 )
  261. FT_INVALID_DATA;
  262. }
  263. /* check offset */
  264. if ( offset != 0 )
  265. {
  266. ids = p - 2 + offset;
  267. if ( ids < glyph_ids || ids + code_count*2 > table + length )
  268. FT_INVALID_OFFSET;
  269. /* check glyph ids */
  270. if ( valid->level >= FT_VALIDATE_TIGHT )
  271. {
  272. FT_Byte* limit = p + code_count * 2;
  273. FT_UInt idx;
  274. for ( ; p < limit; )
  275. {
  276. idx = TT_NEXT_USHORT( p );
  277. if ( idx != 0 )
  278. {
  279. idx = ( idx + delta ) & 0xFFFFU;
  280. if ( idx >= TT_VALID_GLYPH_COUNT( valid ) )
  281. FT_INVALID_GLYPH_ID;
  282. }
  283. }
  284. }
  285. }
  286. }
  287. }
  288. /* return sub header corresponding to a given character code */
  289. /* NULL on invalid charcode */
  290. static FT_Byte*
  291. tt_cmap2_get_subheader( FT_Byte* table,
  292. FT_UInt32 char_code )
  293. {
  294. FT_Byte* result = NULL;
  295. if ( char_code < 0x10000UL )
  296. {
  297. FT_UInt char_lo = (FT_UInt)( char_code & 0xFF );
  298. FT_UInt char_hi = (FT_UInt)( char_code >> 8 );
  299. FT_Byte* p = table + 6; /* keys table */
  300. FT_Byte* subs = table + 518; /* subheaders table */
  301. FT_Byte* sub;
  302. if ( char_hi == 0 )
  303. {
  304. /* an 8-bit character code -- we use subHeader 0 in this case */
  305. /* to test whether the character code is in the charmap */
  306. /* */
  307. sub = subs; /* jump to first sub-header */
  308. /* check that the sub-header for this byte is 0, which */
  309. /* indicates that it's really a valid one-byte value */
  310. /* Otherwise, return 0 */
  311. /* */
  312. p += char_lo * 2;
  313. if ( TT_PEEK_USHORT( p ) != 0 )
  314. goto Exit;
  315. }
  316. else
  317. {
  318. /* a 16-bit character code */
  319. p += char_hi * 2; /* jump to key entry */
  320. sub = subs + ( TT_PEEK_USHORT( p ) & -8 ); /* jump to sub-header */
  321. /* check that the hi byte isn't a valid one-byte value */
  322. if ( sub == subs )
  323. goto Exit;
  324. }
  325. result = sub;
  326. }
  327. Exit:
  328. return result;
  329. }
  330. FT_CALLBACK_DEF( FT_UInt )
  331. tt_cmap2_char_index( TT_CMap cmap,
  332. FT_UInt32 char_code )
  333. {
  334. FT_Byte* table = cmap->data;
  335. FT_UInt result = 0;
  336. FT_Byte* subheader;
  337. subheader = tt_cmap2_get_subheader( table, char_code );
  338. if ( subheader )
  339. {
  340. FT_Byte* p = subheader;
  341. FT_UInt idx = (FT_UInt)(char_code & 0xFF);
  342. FT_UInt start, count;
  343. FT_Int delta;
  344. FT_UInt offset;
  345. start = TT_NEXT_USHORT( p );
  346. count = TT_NEXT_USHORT( p );
  347. delta = TT_NEXT_SHORT ( p );
  348. offset = TT_PEEK_USHORT( p );
  349. idx -= start;
  350. if ( idx < count && offset != 0 )
  351. {
  352. p += offset + 2 * idx;
  353. idx = TT_PEEK_USHORT( p );
  354. if ( idx != 0 )
  355. result = (FT_UInt)( idx + delta ) & 0xFFFFU;
  356. }
  357. }
  358. return result;
  359. }
  360. FT_CALLBACK_DEF( FT_UInt )
  361. tt_cmap2_char_next( TT_CMap cmap,
  362. FT_UInt32 *pcharcode )
  363. {
  364. FT_Byte* table = cmap->data;
  365. FT_UInt gindex = 0;
  366. FT_UInt32 result = 0;
  367. FT_UInt32 charcode = *pcharcode + 1;
  368. FT_Byte* subheader;
  369. while ( charcode < 0x10000UL )
  370. {
  371. subheader = tt_cmap2_get_subheader( table, charcode );
  372. if ( subheader )
  373. {
  374. FT_Byte* p = subheader;
  375. FT_UInt start = TT_NEXT_USHORT( p );
  376. FT_UInt count = TT_NEXT_USHORT( p );
  377. FT_Int delta = TT_NEXT_SHORT ( p );
  378. FT_UInt offset = TT_PEEK_USHORT( p );
  379. FT_UInt char_lo = (FT_UInt)( charcode & 0xFF );
  380. FT_UInt pos, idx;
  381. if ( offset == 0 )
  382. goto Next_SubHeader;
  383. if ( char_lo < start )
  384. {
  385. char_lo = start;
  386. pos = 0;
  387. }
  388. else
  389. pos = (FT_UInt)( char_lo - start );
  390. p += offset + pos * 2;
  391. charcode = ( charcode & -256 ) + char_lo;
  392. for ( ; pos < count; pos++, charcode++ )
  393. {
  394. idx = TT_NEXT_USHORT( p );
  395. if ( idx != 0 )
  396. {
  397. gindex = ( idx + delta ) & 0xFFFFU;
  398. if ( gindex != 0 )
  399. {
  400. result = charcode;
  401. goto Exit;
  402. }
  403. }
  404. }
  405. }
  406. /* jump to next sub-header, i.e. higher byte value */
  407. Next_SubHeader:
  408. charcode = ( charcode & -256 ) + 256;
  409. }
  410. Exit:
  411. *pcharcode = result;
  412. return gindex;
  413. }
  414. FT_CALLBACK_TABLE_DEF
  415. const TT_CMap_ClassRec tt_cmap2_class_rec =
  416. {
  417. {
  418. sizeof( TT_CMapRec ),
  419. (FT_CMap_InitFunc) tt_cmap_init,
  420. (FT_CMap_DoneFunc) NULL,
  421. (FT_CMap_CharIndexFunc)tt_cmap2_char_index,
  422. (FT_CMap_CharNextFunc) tt_cmap2_char_next
  423. },
  424. 2,
  425. (TT_CMap_ValidateFunc) tt_cmap2_validate
  426. };
  427. #endif /* TT_CONFIG_CMAP_FORMAT_2 */
  428. /*************************************************************************/
  429. /*************************************************************************/
  430. /***** *****/
  431. /***** FORMAT 4 *****/
  432. /***** *****/
  433. /*************************************************************************/
  434. /*************************************************************************/
  435. /*************************************************************************/
  436. /* */
  437. /* TABLE OVERVIEW */
  438. /* -------------- */
  439. /* */
  440. /* NAME OFFSET TYPE DESCRIPTION */
  441. /* */
  442. /* format 0 USHORT must be 4 */
  443. /* length 2 USHORT table length */
  444. /* in bytes */
  445. /* language 4 USHORT Mac language code */
  446. /* */
  447. /* segCountX2 6 USHORT 2*NUM_SEGS */
  448. /* searchRange 8 USHORT 2*(1 << LOG_SEGS) */
  449. /* entrySelector 10 USHORT LOG_SEGS */
  450. /* rangeShift 12 USHORT segCountX2 - */
  451. /* searchRange */
  452. /* */
  453. /* endCount 14 USHORT[NUM_SEGS] end charcode for */
  454. /* each segment; last */
  455. /* is 0xFFFF */
  456. /* */
  457. /* pad 14+NUM_SEGS*2 USHORT padding */
  458. /* */
  459. /* startCount 16+NUM_SEGS*2 USHORT[NUM_SEGS] first charcode for */
  460. /* each segment */
  461. /* */
  462. /* idDelta 16+NUM_SEGS*4 SHORT[NUM_SEGS] delta for each */
  463. /* segment */
  464. /* idOffset 16+NUM_SEGS*6 SHORT[NUM_SEGS] range offset for */
  465. /* each segment; can be */
  466. /* zero */
  467. /* */
  468. /* glyphIds 16+NUM_SEGS*8 USHORT[] array of glyph id */
  469. /* ranges */
  470. /* */
  471. /* Character codes are modelled by a series of ordered (increasing) */
  472. /* intervals called segments. Each segment has start and end codes, */
  473. /* provided by the `startCount' and `endCount' arrays. Segments must */
  474. /* not be overlapping and the last segment should always contain the */
  475. /* `0xFFFF' endCount. */
  476. /* */
  477. /* The fields `searchRange', `entrySelector' and `rangeShift' are better */
  478. /* ignored (they are traces of over-engineering in the TrueType */
  479. /* specification). */
  480. /* */
  481. /* Each segment also has a signed `delta', as well as an optional offset */
  482. /* within the `glyphIds' table. */
  483. /* */
  484. /* If a segment's idOffset is 0, the glyph index corresponding to any */
  485. /* charcode within the segment is obtained by adding the value of */
  486. /* `idDelta' directly to the charcode, modulo 65536. */
  487. /* */
  488. /* Otherwise, a glyph index is taken from the glyph ids sub-array for */
  489. /* the segment, and the value of `idDelta' is added to it. */
  490. /* */
  491. /* */
  492. /* Finally, note that certain fonts contain invalid charmaps that */
  493. /* contain end=0xFFFF, start=0xFFFF, delta=0x0001, offset=0xFFFF at the */
  494. /* of their charmaps (e.g. opens___.ttf which comes with OpenOffice.org) */
  495. /* we need special code to deal with them correctly... */
  496. /* */
  497. #ifdef TT_CONFIG_CMAP_FORMAT_4
  498. FT_CALLBACK_DEF( void )
  499. tt_cmap4_validate( FT_Byte* table,
  500. FT_Validator valid )
  501. {
  502. FT_Byte* p = table + 2; /* skip format */
  503. FT_UInt length = TT_NEXT_USHORT( p );
  504. FT_Byte *ends, *starts, *offsets, *deltas, *glyph_ids;
  505. FT_UInt num_segs;
  506. /* in certain fonts, the `length' field is invalid and goes */
  507. /* out of bound. We try to correct this here... */
  508. if ( length < 16 )
  509. FT_INVALID_TOO_SHORT;
  510. if ( table + length > valid->limit )
  511. {
  512. if ( valid->level >= FT_VALIDATE_TIGHT )
  513. FT_INVALID_TOO_SHORT;
  514. length = (FT_UInt)( valid->limit - table );
  515. }
  516. p = table + 6;
  517. num_segs = TT_NEXT_USHORT( p ); /* read segCountX2 */
  518. if ( valid->level >= FT_VALIDATE_PARANOID )
  519. {
  520. /* check that we have an even value here */
  521. if ( num_segs & 1 )
  522. FT_INVALID_DATA;
  523. }
  524. num_segs /= 2;
  525. /* check the search parameters - even though we never use them */
  526. /* */
  527. if ( valid->level >= FT_VALIDATE_PARANOID )
  528. {
  529. /* check the values of 'searchRange', 'entrySelector', 'rangeShift' */
  530. FT_UInt search_range = TT_NEXT_USHORT( p );
  531. FT_UInt entry_selector = TT_NEXT_USHORT( p );
  532. FT_UInt range_shift = TT_NEXT_USHORT( p );
  533. if ( ( search_range | range_shift ) & 1 ) /* must be even values */
  534. FT_INVALID_DATA;
  535. search_range /= 2;
  536. range_shift /= 2;
  537. /* `search range' is the greatest power of 2 that is <= num_segs */
  538. if ( search_range > num_segs ||
  539. search_range * 2 < num_segs ||
  540. search_range + range_shift != num_segs ||
  541. search_range != ( 1U << entry_selector ) )
  542. FT_INVALID_DATA;
  543. }
  544. ends = table + 14;
  545. starts = table + 16 + num_segs * 2;
  546. deltas = starts + num_segs * 2;
  547. offsets = deltas + num_segs * 2;
  548. glyph_ids = offsets + num_segs * 2;
  549. if ( glyph_ids > table + length )
  550. FT_INVALID_TOO_SHORT;
  551. /* check last segment, its end count must be FFFF */
  552. if ( valid->level >= FT_VALIDATE_PARANOID )
  553. {
  554. p = ends + ( num_segs - 1 ) * 2;
  555. if ( TT_PEEK_USHORT( p ) != 0xFFFFU )
  556. FT_INVALID_DATA;
  557. }
  558. /* check that segments are sorted in increasing order and do not */
  559. /* overlap; check also the offsets */
  560. {
  561. FT_UInt start, end, last = 0, offset, n;
  562. FT_Int delta;
  563. for ( n = 0; n < num_segs; n++ )
  564. {
  565. p = starts + n * 2;
  566. start = TT_PEEK_USHORT( p );
  567. p = ends + n * 2;
  568. end = TT_PEEK_USHORT( p );
  569. p = deltas + n * 2;
  570. delta = TT_PEEK_SHORT( p );
  571. p = offsets + n * 2;
  572. offset = TT_PEEK_USHORT( p );
  573. if ( start > end )
  574. FT_INVALID_DATA;
  575. /* this test should be performed at default validation level; */
  576. /* unfortunately, some popular Asian fonts present overlapping */
  577. /* ranges in their charmaps */
  578. /* */
  579. if ( valid->level >= FT_VALIDATE_TIGHT )
  580. {
  581. if ( n > 0 && start <= last )
  582. FT_INVALID_DATA;
  583. }
  584. if ( offset && offset != 0xFFFFU )
  585. {
  586. p += offset; /* start of glyph id array */
  587. /* check that we point within the glyph ids table only */
  588. if ( p < glyph_ids ||
  589. p + ( end - start + 1 ) * 2 > table + length )
  590. FT_INVALID_DATA;
  591. /* check glyph indices within the segment range */
  592. if ( valid->level >= FT_VALIDATE_TIGHT )
  593. {
  594. FT_UInt i, idx;
  595. for ( i = start; i < end; i++ )
  596. {
  597. idx = FT_NEXT_USHORT( p );
  598. if ( idx != 0 )
  599. {
  600. idx = (FT_UInt)( idx + delta ) & 0xFFFFU;
  601. if ( idx >= TT_VALID_GLYPH_COUNT( valid ) )
  602. FT_INVALID_GLYPH_ID;
  603. }
  604. }
  605. }
  606. }
  607. else if ( offset == 0xFFFFU )
  608. {
  609. /* Some fonts (erroneously?) use a range offset of 0xFFFF */
  610. /* to mean missing glyph in cmap table */
  611. /* */
  612. if ( valid->level >= FT_VALIDATE_PARANOID ||
  613. n != num_segs - 1 ||
  614. !( start == 0xFFFFU && end == 0xFFFFU && delta == 0x1U ) )
  615. FT_INVALID_DATA;
  616. }
  617. last = end;
  618. }
  619. }
  620. }
  621. FT_CALLBACK_DEF( FT_UInt )
  622. tt_cmap4_char_index( TT_CMap cmap,
  623. FT_UInt32 char_code )
  624. {
  625. FT_Byte* table = cmap->data;
  626. FT_UInt result = 0;
  627. if ( char_code < 0x10000UL )
  628. {
  629. FT_UInt idx, num_segs2;
  630. FT_Int delta;
  631. FT_UInt code = (FT_UInt)char_code;
  632. FT_Byte* p;
  633. p = table + 6;
  634. num_segs2 = TT_PEEK_USHORT( p ) & -2; /* be paranoid! */
  635. #if 1
  636. /* Some fonts have more than 170 segments in their charmaps! */
  637. /* We changed this function to use a more efficient binary */
  638. /* search for improving performance */
  639. {
  640. FT_UInt min = 0;
  641. FT_UInt max = num_segs2 >> 1;
  642. FT_UInt mid, start, end, offset;
  643. while ( min < max )
  644. {
  645. mid = ( min + max ) >> 1;
  646. p = table + 14 + mid * 2;
  647. end = TT_NEXT_USHORT( p );
  648. p += num_segs2;
  649. start = TT_PEEK_USHORT( p);
  650. if ( code < start )
  651. max = mid;
  652. else if ( code > end )
  653. min = mid + 1;
  654. else
  655. {
  656. /* we found the segment */
  657. idx = code;
  658. p += num_segs2;
  659. delta = TT_PEEK_SHORT( p );
  660. p += num_segs2;
  661. offset = TT_PEEK_USHORT( p );
  662. if ( offset == 0xFFFFU )
  663. goto Exit;
  664. if ( offset != 0 )
  665. {
  666. p += offset + 2 * ( idx - start );
  667. idx = TT_PEEK_USHORT( p );
  668. }
  669. if ( idx != 0 )
  670. result = (FT_UInt)( idx + delta ) & 0xFFFFU;
  671. goto Exit;
  672. }
  673. }
  674. }
  675. #else /* 0 - old code */
  676. {
  677. FT_UInt n;
  678. FT_Byte* q;
  679. p = table + 14; /* ends table */
  680. q = table + 16 + num_segs2; /* starts table */
  681. for ( n = 0; n < num_segs2; n += 2 )
  682. {
  683. FT_UInt end = TT_NEXT_USHORT( p );
  684. FT_UInt start = TT_NEXT_USHORT( q );
  685. FT_UInt offset;
  686. if ( code < start )
  687. break;
  688. if ( code <= end )
  689. {
  690. idx = code;
  691. p = q + num_segs2 - 2;
  692. delta = TT_PEEK_SHORT( p );
  693. p += num_segs2;
  694. offset = TT_PEEK_USHORT( p );
  695. if ( offset == 0xFFFFU )
  696. goto Exit;
  697. if ( offset != 0 )
  698. {
  699. p += offset + 2 * ( idx - start );
  700. idx = TT_PEEK_USHORT( p );
  701. }
  702. if ( idx != 0 )
  703. result = (FT_UInt)( idx + delta ) & 0xFFFFU;
  704. }
  705. }
  706. }
  707. #endif /* 0 */
  708. }
  709. Exit:
  710. return result;
  711. }
  712. FT_CALLBACK_DEF( FT_UInt )
  713. tt_cmap4_char_next( TT_CMap cmap,
  714. FT_UInt32 *pchar_code )
  715. {
  716. FT_Byte* table = cmap->data;
  717. FT_UInt32 result = 0;
  718. FT_UInt32 char_code = *pchar_code + 1;
  719. FT_UInt gindex = 0;
  720. FT_Byte* p;
  721. FT_Byte* q;
  722. FT_UInt code, num_segs2;
  723. if ( char_code >= 0x10000UL )
  724. goto Exit;
  725. code = (FT_UInt)char_code;
  726. p = table + 6;
  727. num_segs2 = TT_PEEK_USHORT(p) & -2; /* ensure even-ness */
  728. for (;;)
  729. {
  730. FT_UInt offset, n;
  731. FT_Int delta;
  732. p = table + 14; /* ends table */
  733. q = table + 16 + num_segs2; /* starts table */
  734. for ( n = 0; n < num_segs2; n += 2 )
  735. {
  736. FT_UInt end = TT_NEXT_USHORT( p );
  737. FT_UInt start = TT_NEXT_USHORT( q );
  738. if ( code < start )
  739. code = start;
  740. if ( code <= end )
  741. {
  742. p = q + num_segs2 - 2;
  743. delta = TT_PEEK_SHORT( p );
  744. p += num_segs2;
  745. offset = TT_PEEK_USHORT( p );
  746. if ( offset != 0 && offset != 0xFFFFU )
  747. {
  748. /* parse the glyph ids array for non-0 index */
  749. p += offset + ( code - start ) * 2;
  750. while ( code <= end )
  751. {
  752. gindex = TT_NEXT_USHORT( p );
  753. if ( gindex != 0 )
  754. {
  755. gindex = (FT_UInt)( gindex + delta ) & 0xFFFFU;
  756. if ( gindex != 0 )
  757. break;
  758. }
  759. code++;
  760. }
  761. }
  762. else if ( offset == 0xFFFFU )
  763. {
  764. /* an offset of 0xFFFF means an empty glyph in certain fonts! */
  765. code = end;
  766. break;
  767. }
  768. else
  769. gindex = (FT_UInt)( code + delta ) & 0xFFFFU;
  770. if ( gindex == 0 )
  771. break;
  772. result = code;
  773. goto Exit;
  774. }
  775. }
  776. /* loop to next trial charcode */
  777. if ( code >= 0xFFFFU )
  778. break;
  779. code++;
  780. }
  781. return (FT_UInt)result;
  782. Exit:
  783. *pchar_code = result;
  784. return gindex;
  785. }
  786. FT_CALLBACK_TABLE_DEF
  787. const TT_CMap_ClassRec tt_cmap4_class_rec =
  788. {
  789. {
  790. sizeof ( TT_CMapRec ),
  791. (FT_CMap_InitFunc) tt_cmap_init,
  792. (FT_CMap_DoneFunc) NULL,
  793. (FT_CMap_CharIndexFunc)tt_cmap4_char_index,
  794. (FT_CMap_CharNextFunc) tt_cmap4_char_next
  795. },
  796. 4,
  797. (TT_CMap_ValidateFunc) tt_cmap4_validate
  798. };
  799. #endif /* TT_CONFIG_CMAP_FORMAT_4 */
  800. /*************************************************************************/
  801. /*************************************************************************/
  802. /***** *****/
  803. /***** FORMAT 6 *****/
  804. /***** *****/
  805. /*************************************************************************/
  806. /*************************************************************************/
  807. /*************************************************************************/
  808. /* */
  809. /* TABLE OVERVIEW */
  810. /* -------------- */
  811. /* */
  812. /* NAME OFFSET TYPE DESCRIPTION */
  813. /* */
  814. /* format 0 USHORT must be 4 */
  815. /* length 2 USHORT table length in bytes */
  816. /* language 4 USHORT Mac language code */
  817. /* */
  818. /* first 6 USHORT first segment code */
  819. /* count 8 USHORT segment size in chars */
  820. /* glyphIds 10 USHORT[count] glyph ids */
  821. /* */
  822. /* A very simplified segment mapping. */
  823. /* */
  824. #ifdef TT_CONFIG_CMAP_FORMAT_6
  825. FT_CALLBACK_DEF( void )
  826. tt_cmap6_validate( FT_Byte* table,
  827. FT_Validator valid )
  828. {
  829. FT_Byte* p;
  830. FT_UInt length, start, count;
  831. if ( table + 10 > valid->limit )
  832. FT_INVALID_TOO_SHORT;
  833. p = table + 2;
  834. length = TT_NEXT_USHORT( p );
  835. p = table + 6; /* skip language */
  836. start = TT_NEXT_USHORT( p );
  837. count = TT_NEXT_USHORT( p );
  838. if ( table + length > valid->limit || length < 10 + count * 2 )
  839. FT_INVALID_TOO_SHORT;
  840. /* check glyph indices */
  841. if ( valid->level >= FT_VALIDATE_TIGHT )
  842. {
  843. FT_UInt gindex;
  844. for ( ; count > 0; count-- )
  845. {
  846. gindex = TT_NEXT_USHORT( p );
  847. if ( gindex >= TT_VALID_GLYPH_COUNT( valid ) )
  848. FT_INVALID_GLYPH_ID;
  849. }
  850. }
  851. }
  852. FT_CALLBACK_DEF( FT_UInt )
  853. tt_cmap6_char_index( TT_CMap cmap,
  854. FT_UInt32 char_code )
  855. {
  856. FT_Byte* table = cmap->data;
  857. FT_UInt result = 0;
  858. FT_Byte* p = table + 6;
  859. FT_UInt start = TT_NEXT_USHORT( p );
  860. FT_UInt count = TT_NEXT_USHORT( p );
  861. FT_UInt idx = (FT_UInt)( char_code - start );
  862. if ( idx < count )
  863. {
  864. p += 2 * idx;
  865. result = TT_PEEK_USHORT( p );
  866. }
  867. return result;
  868. }
  869. FT_CALLBACK_DEF( FT_UInt )
  870. tt_cmap6_char_next( TT_CMap cmap,
  871. FT_UInt32 *pchar_code )
  872. {
  873. FT_Byte* table = cmap->data;
  874. FT_UInt32 result = 0;
  875. FT_UInt32 char_code = *pchar_code + 1;
  876. FT_UInt gindex = 0;
  877. FT_Byte* p = table + 6;
  878. FT_UInt start = TT_NEXT_USHORT( p );
  879. FT_UInt count = TT_NEXT_USHORT( p );
  880. FT_UInt idx;
  881. if ( char_code >= 0x10000UL )
  882. goto Exit;
  883. if ( char_code < start )
  884. char_code = start;
  885. idx = (FT_UInt)( char_code - start );
  886. p += 2 * idx;
  887. for ( ; idx < count; idx++ )
  888. {
  889. gindex = TT_NEXT_USHORT( p );
  890. if ( gindex != 0 )
  891. {
  892. result = char_code;
  893. break;
  894. }
  895. char_code++;
  896. }
  897. Exit:
  898. *pchar_code = result;
  899. return gindex;
  900. }
  901. FT_CALLBACK_TABLE_DEF
  902. const TT_CMap_ClassRec tt_cmap6_class_rec =
  903. {
  904. {
  905. sizeof ( TT_CMapRec ),
  906. (FT_CMap_InitFunc) tt_cmap_init,
  907. (FT_CMap_DoneFunc) NULL,
  908. (FT_CMap_CharIndexFunc)tt_cmap6_char_index,
  909. (FT_CMap_CharNextFunc) tt_cmap6_char_next
  910. },
  911. 6,
  912. (TT_CMap_ValidateFunc) tt_cmap6_validate
  913. };
  914. #endif /* TT_CONFIG_CMAP_FORMAT_6 */
  915. /*************************************************************************/
  916. /*************************************************************************/
  917. /***** *****/
  918. /***** FORMAT 8 *****/
  919. /***** *****/
  920. /***** It's hard to completely understand what the OpenType spec *****/
  921. /***** says about this format, but here is my conclusion. *****/
  922. /***** *****/
  923. /***** The purpose of this format is to easily map UTF-16 text to *****/
  924. /***** glyph indices. Basically, the `char_code' must be in one of *****/
  925. /***** the following formats: *****/
  926. /***** *****/
  927. /***** - A 16-bit value that isn't part of the Unicode Surrogates *****/
  928. /***** Area (i.e. U+D800-U+DFFF). *****/
  929. /***** *****/
  930. /***** - A 32-bit value, made of two surrogate values, i.e.. if *****/
  931. /***** `char_code = (char_hi << 16) | char_lo', then both *****/
  932. /***** `char_hi' and `char_lo' must be in the Surrogates Area. *****/
  933. /***** Area. *****/
  934. /***** *****/
  935. /***** The 'is32' table embedded in the charmap indicates whether a *****/
  936. /***** given 16-bit value is in the surrogates area or not. *****/
  937. /***** *****/
  938. /***** So, for any given `char_code', we can assert the following: *****/
  939. /***** *****/
  940. /***** If `char_hi == 0' then we must have `is32[char_lo] == 0'. *****/
  941. /***** *****/
  942. /***** If `char_hi != 0' then we must have both *****/
  943. /***** `is32[char_hi] != 0' and `is32[char_lo] != 0'. *****/
  944. /***** *****/
  945. /*************************************************************************/
  946. /*************************************************************************/
  947. /*************************************************************************/
  948. /* */
  949. /* TABLE OVERVIEW */
  950. /* -------------- */
  951. /* */
  952. /* NAME OFFSET TYPE DESCRIPTION */
  953. /* */
  954. /* format 0 USHORT must be 8 */
  955. /* reseved 2 USHORT reserved */
  956. /* length 4 ULONG length in bytes */
  957. /* language 8 ULONG Mac language code */
  958. /* is32 12 BYTE[8192] 32-bitness bitmap */
  959. /* count 8204 ULONG number of groups */
  960. /* */
  961. /* This header is followed by 'count' groups of the following format: */
  962. /* */
  963. /* start 0 ULONG first charcode */
  964. /* end 4 ULONG last charcode */
  965. /* startId 8 ULONG start glyph id for the group */
  966. /* */
  967. #ifdef TT_CONFIG_CMAP_FORMAT_8
  968. FT_CALLBACK_DEF( void )
  969. tt_cmap8_validate( FT_Byte* table,
  970. FT_Validator valid )
  971. {
  972. FT_Byte* p = table + 4;
  973. FT_Byte* is32;
  974. FT_UInt32 length;
  975. FT_UInt32 num_groups;
  976. if ( table + 16 + 8192 > valid->limit )
  977. FT_INVALID_TOO_SHORT;
  978. length = TT_NEXT_ULONG( p );
  979. if ( table + length > valid->limit || length < 8208 )
  980. FT_INVALID_TOO_SHORT;
  981. is32 = table + 12;
  982. p = is32 + 8192; /* skip `is32' array */
  983. num_groups = TT_NEXT_ULONG( p );
  984. if ( p + num_groups * 12 > valid->limit )
  985. FT_INVALID_TOO_SHORT;
  986. /* check groups, they must be in increasing order */
  987. {
  988. FT_UInt32 n, start, end, start_id, count, last = 0;
  989. for ( n = 0; n < num_groups; n++ )
  990. {
  991. FT_UInt hi, lo;
  992. start = TT_NEXT_ULONG( p );
  993. end = TT_NEXT_ULONG( p );
  994. start_id = TT_NEXT_ULONG( p );
  995. if ( start > end )
  996. FT_INVALID_DATA;
  997. if ( n > 0 && start <= last )
  998. FT_INVALID_DATA;
  999. if ( valid->level >= FT_VALIDATE_TIGHT )
  1000. {
  1001. if ( start_id + end - start >= TT_VALID_GLYPH_COUNT( valid ) )
  1002. FT_INVALID_GLYPH_ID;
  1003. count = (FT_UInt32)( end - start + 1 );
  1004. if ( start & ~0xFFFFU )
  1005. {
  1006. /* start_hi != 0; check that is32[i] is 1 for each i in */
  1007. /* the `hi' and `lo' of the range [start..end] */
  1008. for ( ; count > 0; count--, start++ )
  1009. {
  1010. hi = (FT_UInt)( start >> 16 );
  1011. lo = (FT_UInt)( start & 0xFFFFU );
  1012. if ( (is32[hi >> 3] & ( 0x80 >> ( hi & 7 ) ) ) == 0 )
  1013. FT_INVALID_DATA;
  1014. if ( (is32[lo >> 3] & ( 0x80 >> ( lo & 7 ) ) ) == 0 )
  1015. FT_INVALID_DATA;
  1016. }
  1017. }
  1018. else
  1019. {
  1020. /* start_hi == 0; check that is32[i] is 0 for each i in */
  1021. /* the range [start..end] */
  1022. /* end_hi cannot be != 0! */
  1023. if ( end & ~0xFFFFU )
  1024. FT_INVALID_DATA;
  1025. for ( ; count > 0; count--, start++ )
  1026. {
  1027. lo = (FT_UInt)( start & 0xFFFFU );
  1028. if ( (is32[lo >> 3] & ( 0x80 >> ( lo & 7 ) ) ) != 0 )
  1029. FT_INVALID_DATA;
  1030. }
  1031. }
  1032. }
  1033. last = end;
  1034. }
  1035. }
  1036. }
  1037. FT_CALLBACK_DEF( FT_UInt )
  1038. tt_cmap8_char_index( TT_CMap cmap,
  1039. FT_UInt32 char_code )
  1040. {
  1041. FT_Byte* table = cmap->data;
  1042. FT_UInt result = 0;
  1043. FT_Byte* p = table + 8204;
  1044. FT_UInt32 num_groups = TT_NEXT_ULONG( p );
  1045. FT_UInt32 start, end, start_id;
  1046. for ( ; num_groups > 0; num_groups-- )
  1047. {
  1048. start = TT_NEXT_ULONG( p );
  1049. end = TT_NEXT_ULONG( p );
  1050. start_id = TT_NEXT_ULONG( p );
  1051. if ( char_code < start )
  1052. break;
  1053. if ( char_code <= end )
  1054. {
  1055. result = (FT_UInt)( start_id + char_code - start );
  1056. break;
  1057. }
  1058. }
  1059. return result;
  1060. }
  1061. FT_CALLBACK_DEF( FT_UInt )
  1062. tt_cmap8_char_next( TT_CMap cmap,
  1063. FT_UInt32 *pchar_code )
  1064. {
  1065. FT_UInt32 result = 0;
  1066. FT_UInt32 char_code = *pchar_code + 1;
  1067. FT_UInt gindex = 0;
  1068. FT_Byte* table = cmap->data;
  1069. FT_Byte* p = table + 8204;
  1070. FT_UInt32 num_groups = TT_NEXT_ULONG( p );
  1071. FT_UInt32 start, end, start_id;
  1072. p = table + 8208;
  1073. for ( ; num_groups > 0; num_groups-- )
  1074. {
  1075. start = TT_NEXT_ULONG( p );
  1076. end = TT_NEXT_ULONG( p );
  1077. start_id = TT_NEXT_ULONG( p );
  1078. if ( char_code < start )
  1079. char_code = start;
  1080. if ( char_code <= end )
  1081. {
  1082. gindex = (FT_UInt)( char_code - start + start_id );
  1083. if ( gindex != 0 )
  1084. {
  1085. result = char_code;
  1086. goto Exit;
  1087. }
  1088. }
  1089. }
  1090. Exit:
  1091. *pchar_code = result;
  1092. return gindex;
  1093. }
  1094. FT_CALLBACK_TABLE_DEF
  1095. const TT_CMap_ClassRec tt_cmap8_class_rec =
  1096. {
  1097. {
  1098. sizeof ( TT_CMapRec ),
  1099. (FT_CMap_InitFunc) tt_cmap_init,
  1100. (FT_CMap_DoneFunc) NULL,
  1101. (FT_CMap_CharIndexFunc)tt_cmap8_char_index,
  1102. (FT_CMap_CharNextFunc) tt_cmap8_char_next
  1103. },
  1104. 8,
  1105. (TT_CMap_ValidateFunc) tt_cmap8_validate
  1106. };
  1107. #endif /* TT_CONFIG_CMAP_FORMAT_8 */
  1108. /*************************************************************************/
  1109. /*************************************************************************/
  1110. /***** *****/
  1111. /***** FORMAT 10 *****/
  1112. /***** *****/
  1113. /*************************************************************************/
  1114. /*************************************************************************/
  1115. /*************************************************************************/
  1116. /* */
  1117. /* TABLE OVERVIEW */
  1118. /* -------------- */
  1119. /* */
  1120. /* NAME OFFSET TYPE DESCRIPTION */
  1121. /* */
  1122. /* format 0 USHORT must be 10 */
  1123. /* reserved 2 USHORT reserved */
  1124. /* length 4 ULONG length in bytes */
  1125. /* language 8 ULONG Mac language code */
  1126. /* */
  1127. /* start 12 ULONG first char in range */
  1128. /* count 16 ULONG number of chars in range */
  1129. /* glyphIds 20 USHORT[count] glyph indices covered */
  1130. /* */
  1131. #ifdef TT_CONFIG_CMAP_FORMAT_10
  1132. FT_CALLBACK_DEF( void )
  1133. tt_cmap10_validate( FT_Byte* table,
  1134. FT_Validator valid )
  1135. {
  1136. FT_Byte* p = table + 4;
  1137. FT_ULong length, start, count;
  1138. if ( table + 20 > valid->limit )
  1139. FT_INVALID_TOO_SHORT;
  1140. length = TT_NEXT_ULONG( p );
  1141. p = table + 12;
  1142. start = TT_NEXT_ULONG( p );
  1143. count = TT_NEXT_ULONG( p );
  1144. if ( table + length > valid->limit || length < 20 + count * 2 )
  1145. FT_INVALID_TOO_SHORT;
  1146. /* check glyph indices */
  1147. if ( valid->level >= FT_VALIDATE_TIGHT )
  1148. {
  1149. FT_UInt gindex;
  1150. for ( ; count > 0; count-- )
  1151. {
  1152. gindex = TT_NEXT_USHORT( p );
  1153. if ( gindex >= TT_VALID_GLYPH_COUNT( valid ) )
  1154. FT_INVALID_GLYPH_ID;
  1155. }
  1156. }
  1157. }
  1158. FT_CALLBACK_DEF( FT_UInt )
  1159. tt_cmap10_char_index( TT_CMap cmap,
  1160. FT_UInt32 char_code )
  1161. {
  1162. FT_Byte* table = cmap->data;
  1163. FT_UInt result = 0;
  1164. FT_Byte* p = table + 12;
  1165. FT_UInt32 start = TT_NEXT_ULONG( p );
  1166. FT_UInt32 count = TT_NEXT_ULONG( p );
  1167. FT_UInt32 idx = (FT_ULong)( char_code - start );
  1168. if ( idx < count )
  1169. {
  1170. p += 2 * idx;
  1171. result = TT_PEEK_USHORT( p );
  1172. }
  1173. return result;
  1174. }
  1175. FT_CALLBACK_DEF( FT_UInt )
  1176. tt_cmap10_char_next( TT_CMap cmap,
  1177. FT_UInt32 *pchar_code )
  1178. {
  1179. FT_Byte* table = cmap->data;
  1180. FT_UInt32 result = 0;
  1181. FT_UInt32 char_code = *pchar_code + 1;
  1182. FT_UInt gindex = 0;
  1183. FT_Byte* p = table + 12;
  1184. FT_UInt32 start = TT_NEXT_ULONG( p );
  1185. FT_UInt32 count = TT_NEXT_ULONG( p );
  1186. FT_UInt32 idx;
  1187. if ( char_code < start )
  1188. char_code = start;
  1189. idx = (FT_UInt32)( char_code - start );
  1190. p += 2 * idx;
  1191. for ( ; idx < count; idx++ )
  1192. {
  1193. gindex = TT_NEXT_USHORT( p );
  1194. if ( gindex != 0 )
  1195. {
  1196. result = char_code;
  1197. break;
  1198. }
  1199. char_code++;
  1200. }
  1201. *pchar_code = char_code;
  1202. return gindex;
  1203. }
  1204. FT_CALLBACK_TABLE_DEF
  1205. const TT_CMap_ClassRec tt_cmap10_class_rec =
  1206. {
  1207. {
  1208. sizeof ( TT_CMapRec ),
  1209. (FT_CMap_InitFunc) tt_cmap_init,
  1210. (FT_CMap_DoneFunc) NULL,
  1211. (FT_CMap_CharIndexFunc)tt_cmap10_char_index,
  1212. (FT_CMap_CharNextFunc) tt_cmap10_char_next
  1213. },
  1214. 10,
  1215. (TT_CMap_ValidateFunc) tt_cmap10_validate
  1216. };
  1217. #endif /* TT_CONFIG_CMAP_FORMAT_10 */
  1218. /*************************************************************************/
  1219. /*************************************************************************/
  1220. /***** *****/
  1221. /***** FORMAT 12 *****/
  1222. /***** *****/
  1223. /*************************************************************************/
  1224. /*************************************************************************/
  1225. /*************************************************************************/
  1226. /* */
  1227. /* TABLE OVERVIEW */
  1228. /* -------------- */
  1229. /* */
  1230. /* NAME OFFSET TYPE DESCRIPTION */
  1231. /* */
  1232. /* format 0 USHORT must be 12 */
  1233. /* reserved 2 USHORT reserved */
  1234. /* length 4 ULONG length in bytes */
  1235. /* language 8 ULONG Mac language code */
  1236. /* count 12 ULONG number of groups */
  1237. /* 16 */
  1238. /* */
  1239. /* This header is followed by `count' groups of the following format: */
  1240. /* */
  1241. /* start 0 ULONG first charcode */
  1242. /* end 4 ULONG last charcode */
  1243. /* startId 8 ULONG start glyph id for the group */
  1244. /* */
  1245. #ifdef TT_CONFIG_CMAP_FORMAT_12
  1246. FT_CALLBACK_DEF( void )
  1247. tt_cmap12_validate( FT_Byte* table,
  1248. FT_Validator valid )
  1249. {
  1250. FT_Byte* p;
  1251. FT_ULong length;
  1252. FT_ULong num_groups;
  1253. if ( table + 16 > valid->limit )
  1254. FT_INVALID_TOO_SHORT;
  1255. p = table + 4;
  1256. length = TT_NEXT_ULONG( p );
  1257. p = table + 12;
  1258. num_groups = TT_NEXT_ULONG( p );
  1259. if ( table + length > valid->limit || length < 16 + 12 * num_groups )
  1260. FT_INVALID_TOO_SHORT;
  1261. /* check groups, they must be in increasing order */
  1262. {
  1263. FT_ULong n, start, end, start_id, last = 0;
  1264. for ( n = 0; n < num_groups; n++ )
  1265. {
  1266. start = TT_NEXT_ULONG( p );
  1267. end = TT_NEXT_ULONG( p );
  1268. start_id = TT_NEXT_ULONG( p );
  1269. if ( start > end )
  1270. FT_INVALID_DATA;
  1271. if ( n > 0 && start <= last )
  1272. FT_INVALID_DATA;
  1273. if ( valid->level >= FT_VALIDATE_TIGHT )
  1274. {
  1275. if ( start_id + end - start >= TT_VALID_GLYPH_COUNT( valid ) )
  1276. FT_INVALID_GLYPH_ID;
  1277. }
  1278. last = end;
  1279. }
  1280. }
  1281. }
  1282. FT_CALLBACK_DEF( FT_UInt )
  1283. tt_cmap12_char_index( TT_CMap cmap,
  1284. FT_UInt32 char_code )
  1285. {
  1286. FT_UInt result = 0;
  1287. FT_Byte* table = cmap->data;
  1288. FT_Byte* p = table + 12;
  1289. FT_UInt32 num_groups = TT_NEXT_ULONG( p );
  1290. FT_UInt32 start, end, start_id;
  1291. for ( ; num_groups > 0; num_groups-- )
  1292. {
  1293. start = TT_NEXT_ULONG( p );
  1294. end = TT_NEXT_ULONG( p );
  1295. start_id = TT_NEXT_ULONG( p );
  1296. if ( char_code < start )
  1297. break;
  1298. if ( char_code <= end )
  1299. {
  1300. result = (FT_UInt)( start_id + char_code - start );
  1301. break;
  1302. }
  1303. }
  1304. return result;
  1305. }
  1306. FT_CALLBACK_DEF( FT_UInt )
  1307. tt_cmap12_char_next( TT_CMap cmap,
  1308. FT_UInt32 *pchar_code )
  1309. {
  1310. FT_Byte* table = cmap->data;
  1311. FT_UInt32 result = 0;
  1312. FT_UInt32 char_code = *pchar_code + 1;
  1313. FT_UInt gindex = 0;
  1314. FT_Byte* p = table + 12;
  1315. FT_UInt32 num_groups = TT_NEXT_ULONG( p );
  1316. FT_UInt32 start, end, start_id;
  1317. p = table + 16;
  1318. for ( ; num_groups > 0; num_groups-- )
  1319. {
  1320. start = TT_NEXT_ULONG( p );
  1321. end = TT_NEXT_ULONG( p );
  1322. start_id = TT_NEXT_ULONG( p );
  1323. if ( char_code < start )
  1324. char_code = start;
  1325. if ( char_code <= end )
  1326. {
  1327. gindex = (FT_UInt)(char_code - start + start_id);
  1328. if ( gindex != 0 )
  1329. {
  1330. result = char_code;
  1331. goto Exit;
  1332. }
  1333. }
  1334. }
  1335. Exit:
  1336. *pchar_code = result;
  1337. return gindex;
  1338. }
  1339. FT_CALLBACK_TABLE_DEF
  1340. const TT_CMap_ClassRec tt_cmap12_class_rec =
  1341. {
  1342. {
  1343. sizeof ( TT_CMapRec ),
  1344. (FT_CMap_InitFunc) tt_cmap_init,
  1345. (FT_CMap_DoneFunc) NULL,
  1346. (FT_CMap_CharIndexFunc)tt_cmap12_char_index,
  1347. (FT_CMap_CharNextFunc) tt_cmap12_char_next
  1348. },
  1349. 12,
  1350. (TT_CMap_ValidateFunc) tt_cmap12_validate
  1351. };
  1352. #endif /* TT_CONFIG_CMAP_FORMAT_12 */
  1353. static const TT_CMap_Class tt_cmap_classes[] =
  1354. {
  1355. #ifdef TT_CONFIG_CMAP_FORMAT_0
  1356. &tt_cmap0_class_rec,
  1357. #endif
  1358. #ifdef TT_CONFIG_CMAP_FORMAT_2
  1359. &tt_cmap2_class_rec,
  1360. #endif
  1361. #ifdef TT_CONFIG_CMAP_FORMAT_4
  1362. &tt_cmap4_class_rec,
  1363. #endif
  1364. #ifdef TT_CONFIG_CMAP_FORMAT_6
  1365. &tt_cmap6_class_rec,
  1366. #endif
  1367. #ifdef TT_CONFIG_CMAP_FORMAT_8
  1368. &tt_cmap8_class_rec,
  1369. #endif
  1370. #ifdef TT_CONFIG_CMAP_FORMAT_10
  1371. &tt_cmap10_class_rec,
  1372. #endif
  1373. #ifdef TT_CONFIG_CMAP_FORMAT_12
  1374. &tt_cmap12_class_rec,
  1375. #endif
  1376. NULL,
  1377. };
  1378. /* parse the `cmap' table and build the corresponding TT_CMap objects */
  1379. /* in the current face */
  1380. /* */
  1381. FT_LOCAL_DEF( FT_Error )
  1382. tt_face_build_cmaps( TT_Face face )
  1383. {
  1384. FT_Byte* table = face->cmap_table;
  1385. FT_Byte* limit = table + face->cmap_size;
  1386. FT_UInt volatile num_cmaps;
  1387. FT_Byte* volatile p = table;
  1388. if ( p + 4 > limit )
  1389. return FT_Err_Invalid_Table;
  1390. /* only recognize format 0 */
  1391. if ( TT_NEXT_USHORT( p ) != 0 )
  1392. {
  1393. p -= 2;
  1394. FT_ERROR(( "tt_face_build_cmaps: unsupported `cmap' table format = %d\n",
  1395. TT_PEEK_USHORT( p ) ));
  1396. return FT_Err_Invalid_Table;
  1397. }
  1398. num_cmaps = TT_NEXT_USHORT( p );
  1399. for ( ; num_cmaps > 0 && p + 8 <= limit; num_cmaps-- )
  1400. {
  1401. FT_CharMapRec charmap;
  1402. FT_UInt32 offset;
  1403. charmap.platform_id = TT_NEXT_USHORT( p );
  1404. charmap.encoding_id = TT_NEXT_USHORT( p );
  1405. charmap.face = FT_FACE( face );
  1406. charmap.encoding = FT_ENCODING_NONE; /* will be filled later */
  1407. offset = TT_NEXT_ULONG( p );
  1408. if ( offset && table + offset + 2 < limit )
  1409. {
  1410. FT_Byte* cmap = table + offset;
  1411. FT_UInt format = TT_PEEK_USHORT( cmap );
  1412. const TT_CMap_Class* volatile pclazz = tt_cmap_classes;
  1413. TT_CMap_Class clazz;
  1414. for ( ; *pclazz; pclazz++ )
  1415. {
  1416. clazz = *pclazz;
  1417. if ( clazz->format == format )
  1418. {
  1419. volatile TT_ValidatorRec valid;
  1420. ft_validator_init( FT_VALIDATOR( &valid ), cmap, limit,
  1421. FT_VALIDATE_DEFAULT );
  1422. valid.num_glyphs = (FT_UInt)face->root.num_glyphs;
  1423. if ( ft_setjmp( FT_VALIDATOR( &valid )->jump_buffer ) == 0 )
  1424. {
  1425. /* validate this cmap sub-table */
  1426. clazz->validate( cmap, FT_VALIDATOR( &valid ) );
  1427. }
  1428. if ( valid.validator.error == 0 )
  1429. (void)FT_CMap_New( (FT_CMap_Class)clazz, cmap, &charmap, NULL );
  1430. else
  1431. {
  1432. FT_ERROR(( "tt_face_build_cmaps:" ));
  1433. FT_ERROR(( " broken cmap sub-table ignored!\n" ));
  1434. }
  1435. }
  1436. }
  1437. }
  1438. }
  1439. return 0;
  1440. }
  1441. /* END */