psobjs.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. /***************************************************************************/
  2. /* */
  3. /* psobjs.c */
  4. /* */
  5. /* Auxiliary functions for PostScript fonts (body). */
  6. /* */
  7. /* Copyright 1996-2001, 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_POSTSCRIPT_AUX_H
  19. #include FT_INTERNAL_DEBUG_H
  20. #include "psobjs.h"
  21. #include "psauxerr.h"
  22. /*************************************************************************/
  23. /*************************************************************************/
  24. /***** *****/
  25. /***** PS_TABLE *****/
  26. /***** *****/
  27. /*************************************************************************/
  28. /*************************************************************************/
  29. /*************************************************************************/
  30. /* */
  31. /* <Function> */
  32. /* ps_table_new */
  33. /* */
  34. /* <Description> */
  35. /* Initializes a PS_Table. */
  36. /* */
  37. /* <InOut> */
  38. /* table :: The address of the target table. */
  39. /* */
  40. /* <Input> */
  41. /* count :: The table size = the maximum number of elements. */
  42. /* */
  43. /* memory :: The memory object to use for all subsequent */
  44. /* reallocations. */
  45. /* */
  46. /* <Return> */
  47. /* FreeType error code. 0 means success. */
  48. /* */
  49. FT_LOCAL_DEF( FT_Error )
  50. ps_table_new( PS_Table table,
  51. FT_Int count,
  52. FT_Memory memory )
  53. {
  54. FT_Error error;
  55. table->memory = memory;
  56. if ( FT_NEW_ARRAY( table->elements, count ) ||
  57. FT_NEW_ARRAY( table->lengths, count ) )
  58. goto Exit;
  59. table->max_elems = count;
  60. table->init = 0xDEADBEEFUL;
  61. table->num_elems = 0;
  62. table->block = 0;
  63. table->capacity = 0;
  64. table->cursor = 0;
  65. *(PS_Table_FuncsRec*)&table->funcs = ps_table_funcs;
  66. Exit:
  67. if ( error )
  68. FT_FREE( table->elements );
  69. return error;
  70. }
  71. static void
  72. shift_elements( PS_Table table,
  73. FT_Byte* old_base )
  74. {
  75. FT_Long delta = (FT_Long)( table->block - old_base );
  76. FT_Byte** offset = table->elements;
  77. FT_Byte** limit = offset + table->max_elems;
  78. for ( ; offset < limit; offset++ )
  79. {
  80. if ( offset[0] )
  81. offset[0] += delta;
  82. }
  83. }
  84. static FT_Error
  85. reallocate_t1_table( PS_Table table,
  86. FT_Long new_size )
  87. {
  88. FT_Memory memory = table->memory;
  89. FT_Byte* old_base = table->block;
  90. FT_Error error;
  91. /* allocate new base block */
  92. if ( FT_ALLOC( table->block, new_size ) )
  93. return error;
  94. /* copy elements and shift offsets */
  95. if (old_base )
  96. {
  97. FT_MEM_COPY( table->block, old_base, table->capacity );
  98. shift_elements( table, old_base );
  99. FT_FREE( old_base );
  100. }
  101. table->capacity = new_size;
  102. return PSaux_Err_Ok;
  103. }
  104. /*************************************************************************/
  105. /* */
  106. /* <Function> */
  107. /* ps_table_add */
  108. /* */
  109. /* <Description> */
  110. /* Adds an object to a PS_Table, possibly growing its memory block. */
  111. /* */
  112. /* <InOut> */
  113. /* table :: The target table. */
  114. /* */
  115. /* <Input> */
  116. /* idx :: The index of the object in the table. */
  117. /* */
  118. /* object :: The address of the object to copy in memory. */
  119. /* */
  120. /* length :: The length in bytes of the source object. */
  121. /* */
  122. /* <Return> */
  123. /* FreeType error code. 0 means success. An error is returned if a */
  124. /* reallocation fails. */
  125. /* */
  126. FT_LOCAL_DEF( FT_Error )
  127. ps_table_add( PS_Table table,
  128. FT_Int idx,
  129. void* object,
  130. FT_Int length )
  131. {
  132. if ( idx < 0 || idx > table->max_elems )
  133. {
  134. FT_ERROR(( "ps_table_add: invalid index\n" ));
  135. return PSaux_Err_Invalid_Argument;
  136. }
  137. /* grow the base block if needed */
  138. if ( table->cursor + length > table->capacity )
  139. {
  140. FT_Error error;
  141. FT_Offset new_size = table->capacity;
  142. FT_Long in_offset;
  143. in_offset = (FT_Long)((FT_Byte*)object - table->block);
  144. if ( (FT_ULong)in_offset >= table->capacity )
  145. in_offset = -1;
  146. while ( new_size < table->cursor + length )
  147. {
  148. /* increase size by 25% and round up to the nearest multiple of 1024 */
  149. new_size += (new_size >> 2) + 1;
  150. new_size = ( new_size + 1023 ) & -1024;
  151. }
  152. error = reallocate_t1_table( table, new_size );
  153. if ( error )
  154. return error;
  155. if ( in_offset >= 0 )
  156. object = table->block + in_offset;
  157. }
  158. /* add the object to the base block and adjust offset */
  159. table->elements[idx] = table->block + table->cursor;
  160. table->lengths [idx] = length;
  161. FT_MEM_COPY( table->block + table->cursor, object, length );
  162. table->cursor += length;
  163. return PSaux_Err_Ok;
  164. }
  165. /*************************************************************************/
  166. /* */
  167. /* <Function> */
  168. /* ps_table_done */
  169. /* */
  170. /* <Description> */
  171. /* Finalizes a PS_TableRec (i.e., reallocate it to its current */
  172. /* cursor). */
  173. /* */
  174. /* <InOut> */
  175. /* table :: The target table. */
  176. /* */
  177. /* <Note> */
  178. /* This function does NOT release the heap's memory block. It is up */
  179. /* to the caller to clean it, or reference it in its own structures. */
  180. /* */
  181. FT_LOCAL_DEF( void )
  182. ps_table_done( PS_Table table )
  183. {
  184. FT_Memory memory = table->memory;
  185. FT_Error error;
  186. FT_Byte* old_base = table->block;
  187. /* should never fail, because rec.cursor <= rec.size */
  188. if ( !old_base )
  189. return;
  190. if ( FT_ALLOC( table->block, table->cursor ) )
  191. return;
  192. FT_MEM_COPY( table->block, old_base, table->cursor );
  193. shift_elements( table, old_base );
  194. table->capacity = table->cursor;
  195. FT_FREE( old_base );
  196. FT_UNUSED( error );
  197. }
  198. FT_LOCAL_DEF( void )
  199. ps_table_release( PS_Table table )
  200. {
  201. FT_Memory memory = table->memory;
  202. if ( (FT_ULong)table->init == 0xDEADBEEFUL )
  203. {
  204. FT_FREE( table->block );
  205. FT_FREE( table->elements );
  206. FT_FREE( table->lengths );
  207. table->init = 0;
  208. }
  209. }
  210. /*************************************************************************/
  211. /*************************************************************************/
  212. /***** *****/
  213. /***** T1 PARSER *****/
  214. /***** *****/
  215. /*************************************************************************/
  216. /*************************************************************************/
  217. #define IS_T1_WHITESPACE( c ) ( (c) == ' ' || (c) == '\t' )
  218. #define IS_T1_LINESPACE( c ) ( (c) == '\r' || (c) == '\n' )
  219. #define IS_T1_SPACE( c ) ( IS_T1_WHITESPACE( c ) || IS_T1_LINESPACE( c ) )
  220. FT_LOCAL_DEF( void )
  221. ps_parser_skip_spaces( PS_Parser parser )
  222. {
  223. FT_Byte* cur = parser->cursor;
  224. FT_Byte* limit = parser->limit;
  225. while ( cur < limit )
  226. {
  227. FT_Byte c = *cur;
  228. if ( !IS_T1_SPACE( c ) )
  229. break;
  230. cur++;
  231. }
  232. parser->cursor = cur;
  233. }
  234. FT_LOCAL_DEF( void )
  235. ps_parser_skip_alpha( PS_Parser parser )
  236. {
  237. FT_Byte* cur = parser->cursor;
  238. FT_Byte* limit = parser->limit;
  239. while ( cur < limit )
  240. {
  241. FT_Byte c = *cur;
  242. if ( IS_T1_SPACE( c ) )
  243. break;
  244. cur++;
  245. }
  246. parser->cursor = cur;
  247. }
  248. FT_LOCAL_DEF( void )
  249. ps_parser_to_token( PS_Parser parser,
  250. T1_Token token )
  251. {
  252. FT_Byte* cur;
  253. FT_Byte* limit;
  254. FT_Byte starter, ender;
  255. FT_Int embed;
  256. token->type = T1_TOKEN_TYPE_NONE;
  257. token->start = 0;
  258. token->limit = 0;
  259. /* first of all, skip space */
  260. ps_parser_skip_spaces( parser );
  261. cur = parser->cursor;
  262. limit = parser->limit;
  263. if ( cur < limit )
  264. {
  265. switch ( *cur )
  266. {
  267. /************* check for strings ***********************/
  268. case '(':
  269. token->type = T1_TOKEN_TYPE_STRING;
  270. ender = ')';
  271. goto Lookup_Ender;
  272. /************* check for programs/array ****************/
  273. case '{':
  274. token->type = T1_TOKEN_TYPE_ARRAY;
  275. ender = '}';
  276. goto Lookup_Ender;
  277. /************* check for table/array ******************/
  278. case '[':
  279. token->type = T1_TOKEN_TYPE_ARRAY;
  280. ender = ']';
  281. Lookup_Ender:
  282. embed = 1;
  283. starter = *cur++;
  284. token->start = cur;
  285. while ( cur < limit )
  286. {
  287. if ( *cur == starter )
  288. embed++;
  289. else if ( *cur == ender )
  290. {
  291. embed--;
  292. if ( embed <= 0 )
  293. {
  294. token->limit = cur++;
  295. break;
  296. }
  297. }
  298. cur++;
  299. }
  300. break;
  301. /* **************** otherwise, it's any token **********/
  302. default:
  303. token->start = cur++;
  304. token->type = T1_TOKEN_TYPE_ANY;
  305. while ( cur < limit && !IS_T1_SPACE( *cur ) )
  306. cur++;
  307. token->limit = cur;
  308. }
  309. if ( !token->limit )
  310. {
  311. token->start = 0;
  312. token->type = T1_TOKEN_TYPE_NONE;
  313. }
  314. parser->cursor = cur;
  315. }
  316. }
  317. FT_LOCAL_DEF( void )
  318. ps_parser_to_token_array( PS_Parser parser,
  319. T1_Token tokens,
  320. FT_UInt max_tokens,
  321. FT_Int* pnum_tokens )
  322. {
  323. T1_TokenRec master;
  324. *pnum_tokens = -1;
  325. ps_parser_to_token( parser, &master );
  326. if ( master.type == T1_TOKEN_TYPE_ARRAY )
  327. {
  328. FT_Byte* old_cursor = parser->cursor;
  329. FT_Byte* old_limit = parser->limit;
  330. T1_Token cur = tokens;
  331. T1_Token limit = cur + max_tokens;
  332. parser->cursor = master.start;
  333. parser->limit = master.limit;
  334. while ( parser->cursor < parser->limit )
  335. {
  336. T1_TokenRec token;
  337. ps_parser_to_token( parser, &token );
  338. if ( !token.type )
  339. break;
  340. if ( cur < limit )
  341. *cur = token;
  342. cur++;
  343. }
  344. *pnum_tokens = (FT_Int)( cur - tokens );
  345. parser->cursor = old_cursor;
  346. parser->limit = old_limit;
  347. }
  348. }
  349. static FT_Long
  350. T1Radix( FT_Long radixBase,
  351. FT_Byte** cur,
  352. FT_Byte* limit )
  353. {
  354. FT_Long result = 0;
  355. FT_Byte radixEndChar0 =
  356. (FT_Byte)( radixBase > 10 ? '9' + 1 : '0' + radixBase );
  357. FT_Byte radixEndChar1 =
  358. (FT_Byte)( 'A' + radixBase - 10 );
  359. FT_Byte radixEndChar2 =
  360. (FT_Byte)( 'a' + radixBase - 10 );
  361. while( *cur < limit )
  362. {
  363. if ( (*cur)[0] >= '0' && (*cur)[0] < radixEndChar0 )
  364. result = result * radixBase + (*cur)[0] - '0';
  365. else if ( radixBase > 10 &&
  366. (*cur)[0] >= 'A' && (*cur)[0] < radixEndChar1 )
  367. result = result * radixBase + ( (*cur)[0] - 'A' + 10 );
  368. else if ( radixBase > 10 &&
  369. (*cur)[0] >= 'a' && (*cur)[0] < radixEndChar2 )
  370. result = result * radixBase + ( (*cur)[0] - 'a' + 10 );
  371. else
  372. return result;
  373. (*cur)++;
  374. }
  375. return result;
  376. }
  377. static FT_Long
  378. t1_toint( FT_Byte** cursor,
  379. FT_Byte* limit )
  380. {
  381. FT_Long result = 0;
  382. FT_Byte* cur = *cursor;
  383. FT_Byte c = '\0', d;
  384. for ( ; cur < limit; cur++ )
  385. {
  386. c = *cur;
  387. d = (FT_Byte)( c - '0' );
  388. if ( d < 10 )
  389. break;
  390. if ( c == '-' )
  391. {
  392. cur++;
  393. break;
  394. }
  395. }
  396. if ( cur < limit )
  397. {
  398. do
  399. {
  400. d = (FT_Byte)( cur[0] - '0' );
  401. if ( d >= 10 )
  402. {
  403. if ( cur[0] == '#' )
  404. {
  405. cur++;
  406. result = T1Radix( result, &cur, limit );
  407. }
  408. break;
  409. }
  410. result = result * 10 + d;
  411. cur++;
  412. } while ( cur < limit );
  413. if ( c == '-' )
  414. result = -result;
  415. }
  416. *cursor = cur;
  417. return result;
  418. }
  419. static FT_Long
  420. t1_tofixed( FT_Byte** cursor,
  421. FT_Byte* limit,
  422. FT_Long power_ten )
  423. {
  424. FT_Byte* cur = *cursor;
  425. FT_Long num, divider, result;
  426. FT_Int sign = 0;
  427. FT_Byte d;
  428. if ( cur >= limit )
  429. return 0;
  430. /* first of all, check the sign */
  431. if ( *cur == '-' )
  432. {
  433. sign = 1;
  434. cur++;
  435. }
  436. /* then, read the integer part, if any */
  437. if ( *cur != '.' )
  438. result = t1_toint( &cur, limit ) << 16;
  439. else
  440. result = 0;
  441. num = 0;
  442. divider = 1;
  443. if ( cur >= limit )
  444. goto Exit;
  445. /* read decimal part, if any */
  446. if ( *cur == '.' && cur + 1 < limit )
  447. {
  448. cur++;
  449. for (;;)
  450. {
  451. d = (FT_Byte)( *cur - '0' );
  452. if ( d >= 10 )
  453. break;
  454. if ( divider < 10000000L )
  455. {
  456. num = num * 10 + d;
  457. divider *= 10;
  458. }
  459. cur++;
  460. if ( cur >= limit )
  461. break;
  462. }
  463. }
  464. /* read exponent, if any */
  465. if ( cur + 1 < limit && ( *cur == 'e' || *cur == 'E' ) )
  466. {
  467. cur++;
  468. power_ten += t1_toint( &cur, limit );
  469. }
  470. Exit:
  471. /* raise to power of ten if needed */
  472. while ( power_ten > 0 )
  473. {
  474. result = result * 10;
  475. num = num * 10;
  476. power_ten--;
  477. }
  478. while ( power_ten < 0 )
  479. {
  480. result = result / 10;
  481. divider = divider * 10;
  482. power_ten++;
  483. }
  484. if ( num )
  485. result += FT_DivFix( num, divider );
  486. if ( sign )
  487. result = -result;
  488. *cursor = cur;
  489. return result;
  490. }
  491. static FT_Int
  492. t1_tocoordarray( FT_Byte** cursor,
  493. FT_Byte* limit,
  494. FT_Int max_coords,
  495. FT_Short* coords )
  496. {
  497. FT_Byte* cur = *cursor;
  498. FT_Int count = 0;
  499. FT_Byte c, ender;
  500. if ( cur >= limit )
  501. goto Exit;
  502. /* check for the beginning of an array; if not, only one number will */
  503. /* be read */
  504. c = *cur;
  505. ender = 0;
  506. if ( c == '[' )
  507. ender = ']';
  508. if ( c == '{' )
  509. ender = '}';
  510. if ( ender )
  511. cur++;
  512. /* now, read the coordinates */
  513. for ( ; cur < limit; )
  514. {
  515. /* skip whitespace in front of data */
  516. for (;;)
  517. {
  518. c = *cur;
  519. if ( c != ' ' && c != '\t' )
  520. break;
  521. cur++;
  522. if ( cur >= limit )
  523. goto Exit;
  524. }
  525. if ( count >= max_coords || c == ender )
  526. break;
  527. coords[count] = (FT_Short)( t1_tofixed( &cur, limit, 0 ) >> 16 );
  528. count++;
  529. if ( !ender )
  530. break;
  531. }
  532. Exit:
  533. *cursor = cur;
  534. return count;
  535. }
  536. static FT_Int
  537. t1_tofixedarray( FT_Byte** cursor,
  538. FT_Byte* limit,
  539. FT_Int max_values,
  540. FT_Fixed* values,
  541. FT_Int power_ten )
  542. {
  543. FT_Byte* cur = *cursor;
  544. FT_Int count = 0;
  545. FT_Byte c, ender;
  546. if ( cur >= limit ) goto Exit;
  547. /* check for the beginning of an array. If not, only one number will */
  548. /* be read */
  549. c = *cur;
  550. ender = 0;
  551. if ( c == '[' )
  552. ender = ']';
  553. if ( c == '{' )
  554. ender = '}';
  555. if ( ender )
  556. cur++;
  557. /* now, read the values */
  558. for ( ; cur < limit; )
  559. {
  560. /* skip whitespace in front of data */
  561. for (;;)
  562. {
  563. c = *cur;
  564. if ( c != ' ' && c != '\t' )
  565. break;
  566. cur++;
  567. if ( cur >= limit )
  568. goto Exit;
  569. }
  570. if ( count >= max_values || c == ender )
  571. break;
  572. values[count] = t1_tofixed( &cur, limit, power_ten );
  573. count++;
  574. if ( !ender )
  575. break;
  576. }
  577. Exit:
  578. *cursor = cur;
  579. return count;
  580. }
  581. #if 0
  582. static FT_String*
  583. t1_tostring( FT_Byte** cursor,
  584. FT_Byte* limit,
  585. FT_Memory memory )
  586. {
  587. FT_Byte* cur = *cursor;
  588. FT_PtrDist len = 0;
  589. FT_Int count;
  590. FT_String* result;
  591. FT_Error error;
  592. /* XXX: some stupid fonts have a `Notice' or `Copyright' string */
  593. /* that simply doesn't begin with an opening parenthesis, even */
  594. /* though they have a closing one! E.g. "amuncial.pfb" */
  595. /* */
  596. /* We must deal with these ill-fated cases there. Note that */
  597. /* these fonts didn't work with the old Type 1 driver as the */
  598. /* notice/copyright was not recognized as a valid string token */
  599. /* and made the old token parser commit errors. */
  600. while ( cur < limit && ( *cur == ' ' || *cur == '\t' ) )
  601. cur++;
  602. if ( cur + 1 >= limit )
  603. return 0;
  604. if ( *cur == '(' )
  605. cur++; /* skip the opening parenthesis, if there is one */
  606. *cursor = cur;
  607. count = 0;
  608. /* then, count its length */
  609. for ( ; cur < limit; cur++ )
  610. {
  611. if ( *cur == '(' )
  612. count++;
  613. else if ( *cur == ')' )
  614. {
  615. count--;
  616. if ( count < 0 )
  617. break;
  618. }
  619. }
  620. len = cur - *cursor;
  621. if ( cur >= limit || FT_ALLOC( result, len + 1 ) )
  622. return 0;
  623. /* now copy the string */
  624. FT_MEM_COPY( result, *cursor, len );
  625. result[len] = '\0';
  626. *cursor = cur;
  627. return result;
  628. }
  629. #endif /* 0 */
  630. static int
  631. t1_tobool( FT_Byte** cursor,
  632. FT_Byte* limit )
  633. {
  634. FT_Byte* cur = *cursor;
  635. FT_Bool result = 0;
  636. /* return 1 if we find `true', 0 otherwise */
  637. if ( cur + 3 < limit &&
  638. cur[0] == 't' &&
  639. cur[1] == 'r' &&
  640. cur[2] == 'u' &&
  641. cur[3] == 'e' )
  642. {
  643. result = 1;
  644. cur += 5;
  645. }
  646. else if ( cur + 4 < limit &&
  647. cur[0] == 'f' &&
  648. cur[1] == 'a' &&
  649. cur[2] == 'l' &&
  650. cur[3] == 's' &&
  651. cur[4] == 'e' )
  652. {
  653. result = 0;
  654. cur += 6;
  655. }
  656. *cursor = cur;
  657. return result;
  658. }
  659. /* Load a simple field (i.e. non-table) into the current list of objects */
  660. FT_LOCAL_DEF( FT_Error )
  661. ps_parser_load_field( PS_Parser parser,
  662. const T1_Field field,
  663. void** objects,
  664. FT_UInt max_objects,
  665. FT_ULong* pflags )
  666. {
  667. T1_TokenRec token;
  668. FT_Byte* cur;
  669. FT_Byte* limit;
  670. FT_UInt count;
  671. FT_UInt idx;
  672. FT_Error error;
  673. ps_parser_to_token( parser, &token );
  674. if ( !token.type )
  675. goto Fail;
  676. count = 1;
  677. idx = 0;
  678. cur = token.start;
  679. limit = token.limit;
  680. /* we must detect arrays */
  681. if ( field->type == T1_FIELD_TYPE_BBOX )
  682. {
  683. T1_TokenRec token2;
  684. FT_Byte* old_cur = parser->cursor;
  685. FT_Byte* old_limit = parser->limit;
  686. parser->cursor = token.start;
  687. parser->limit = token.limit;
  688. ps_parser_to_token( parser, &token2 );
  689. parser->cursor = old_cur;
  690. parser->limit = old_limit;
  691. if ( token2.type == T1_TOKEN_TYPE_ARRAY )
  692. goto FieldArray;
  693. }
  694. else if ( token.type == T1_TOKEN_TYPE_ARRAY )
  695. {
  696. FieldArray:
  697. /* if this is an array, and we have no blend, an error occurs */
  698. if ( max_objects == 0 )
  699. goto Fail;
  700. count = max_objects;
  701. idx = 1;
  702. }
  703. for ( ; count > 0; count--, idx++ )
  704. {
  705. FT_Byte* q = (FT_Byte*)objects[idx] + field->offset;
  706. FT_Long val;
  707. FT_String* string;
  708. switch ( field->type )
  709. {
  710. case T1_FIELD_TYPE_BOOL:
  711. val = t1_tobool( &cur, limit );
  712. goto Store_Integer;
  713. case T1_FIELD_TYPE_FIXED:
  714. val = t1_tofixed( &cur, limit, 3 );
  715. goto Store_Integer;
  716. case T1_FIELD_TYPE_INTEGER:
  717. val = t1_toint( &cur, limit );
  718. Store_Integer:
  719. switch ( field->size )
  720. {
  721. case 1:
  722. *(FT_Byte*)q = (FT_Byte)val;
  723. break;
  724. case 2:
  725. *(FT_UShort*)q = (FT_UShort)val;
  726. break;
  727. case 4:
  728. *(FT_UInt32*)q = (FT_UInt32)val;
  729. break;
  730. default: /* for 64-bit systems */
  731. *(FT_Long*)q = val;
  732. }
  733. break;
  734. case T1_FIELD_TYPE_STRING:
  735. {
  736. FT_Memory memory = parser->memory;
  737. FT_UInt len = (FT_UInt)( limit - cur );
  738. if ( *(FT_String**)q )
  739. /* with synthetic fonts, it's possible to find a field twice */
  740. break;
  741. if ( FT_ALLOC( string, len + 1 ) )
  742. goto Exit;
  743. FT_MEM_COPY( string, cur, len );
  744. string[len] = 0;
  745. *(FT_String**)q = string;
  746. }
  747. break;
  748. case T1_FIELD_TYPE_BBOX:
  749. {
  750. FT_Fixed temp[4];
  751. FT_BBox* bbox = (FT_BBox*)q;
  752. /* we need the '[' and ']' delimiters */
  753. token.start--;
  754. token.limit++;
  755. (void)t1_tofixedarray( &token.start, token.limit, 4, temp, 0 );
  756. bbox->xMin = FT_RoundFix( temp[0] );
  757. bbox->yMin = FT_RoundFix( temp[1] );
  758. bbox->xMax = FT_RoundFix( temp[2] );
  759. bbox->yMax = FT_RoundFix( temp[3] );
  760. }
  761. break;
  762. default:
  763. /* an error occured */
  764. goto Fail;
  765. }
  766. }
  767. #if 0 /* obsolete - keep for reference */
  768. if ( pflags )
  769. *pflags |= 1L << field->flag_bit;
  770. #else
  771. FT_UNUSED( pflags );
  772. #endif
  773. error = PSaux_Err_Ok;
  774. Exit:
  775. return error;
  776. Fail:
  777. error = PSaux_Err_Invalid_File_Format;
  778. goto Exit;
  779. }
  780. #define T1_MAX_TABLE_ELEMENTS 32
  781. FT_LOCAL_DEF( FT_Error )
  782. ps_parser_load_field_table( PS_Parser parser,
  783. const T1_Field field,
  784. void** objects,
  785. FT_UInt max_objects,
  786. FT_ULong* pflags )
  787. {
  788. T1_TokenRec elements[T1_MAX_TABLE_ELEMENTS];
  789. T1_Token token;
  790. FT_Int num_elements;
  791. FT_Error error = 0;
  792. FT_Byte* old_cursor;
  793. FT_Byte* old_limit;
  794. T1_FieldRec fieldrec = *(T1_Field)field;
  795. #if 1
  796. fieldrec.type = T1_FIELD_TYPE_INTEGER;
  797. if ( field->type == T1_FIELD_TYPE_FIXED_ARRAY )
  798. fieldrec.type = T1_FIELD_TYPE_FIXED;
  799. #endif
  800. ps_parser_to_token_array( parser, elements, 32, &num_elements );
  801. if ( num_elements < 0 )
  802. goto Fail;
  803. if ( num_elements > T1_MAX_TABLE_ELEMENTS )
  804. num_elements = T1_MAX_TABLE_ELEMENTS;
  805. old_cursor = parser->cursor;
  806. old_limit = parser->limit;
  807. /* we store the elements count */
  808. *(FT_Byte*)( (FT_Byte*)objects[0] + field->count_offset ) =
  809. (FT_Byte)num_elements;
  810. /* we now load each element, adjusting the field.offset on each one */
  811. token = elements;
  812. for ( ; num_elements > 0; num_elements--, token++ )
  813. {
  814. parser->cursor = token->start;
  815. parser->limit = token->limit;
  816. ps_parser_load_field( parser, &fieldrec, objects, max_objects, 0 );
  817. fieldrec.offset += fieldrec.size;
  818. }
  819. #if 0 /* obsolete -- keep for reference */
  820. if ( pflags )
  821. *pflags |= 1L << field->flag_bit;
  822. #else
  823. FT_UNUSED( pflags );
  824. #endif
  825. parser->cursor = old_cursor;
  826. parser->limit = old_limit;
  827. Exit:
  828. return error;
  829. Fail:
  830. error = PSaux_Err_Invalid_File_Format;
  831. goto Exit;
  832. }
  833. FT_LOCAL_DEF( FT_Long )
  834. ps_parser_to_int( PS_Parser parser )
  835. {
  836. return t1_toint( &parser->cursor, parser->limit );
  837. }
  838. FT_LOCAL_DEF( FT_Fixed )
  839. ps_parser_to_fixed( PS_Parser parser,
  840. FT_Int power_ten )
  841. {
  842. return t1_tofixed( &parser->cursor, parser->limit, power_ten );
  843. }
  844. FT_LOCAL_DEF( FT_Int )
  845. ps_parser_to_coord_array( PS_Parser parser,
  846. FT_Int max_coords,
  847. FT_Short* coords )
  848. {
  849. return t1_tocoordarray( &parser->cursor, parser->limit,
  850. max_coords, coords );
  851. }
  852. FT_LOCAL_DEF( FT_Int )
  853. ps_parser_to_fixed_array( PS_Parser parser,
  854. FT_Int max_values,
  855. FT_Fixed* values,
  856. FT_Int power_ten )
  857. {
  858. return t1_tofixedarray( &parser->cursor, parser->limit,
  859. max_values, values, power_ten );
  860. }
  861. #if 0
  862. FT_LOCAL_DEF( FT_String* )
  863. T1_ToString( PS_Parser parser )
  864. {
  865. return t1_tostring( &parser->cursor, parser->limit, parser->memory );
  866. }
  867. FT_LOCAL_DEF( FT_Bool )
  868. T1_ToBool( PS_Parser parser )
  869. {
  870. return t1_tobool( &parser->cursor, parser->limit );
  871. }
  872. #endif /* 0 */
  873. FT_LOCAL_DEF( void )
  874. ps_parser_init( PS_Parser parser,
  875. FT_Byte* base,
  876. FT_Byte* limit,
  877. FT_Memory memory )
  878. {
  879. parser->error = 0;
  880. parser->base = base;
  881. parser->limit = limit;
  882. parser->cursor = base;
  883. parser->memory = memory;
  884. parser->funcs = ps_parser_funcs;
  885. }
  886. FT_LOCAL_DEF( void )
  887. ps_parser_done( PS_Parser parser )
  888. {
  889. FT_UNUSED( parser );
  890. }
  891. /*************************************************************************/
  892. /*************************************************************************/
  893. /***** *****/
  894. /***** T1 BUILDER *****/
  895. /***** *****/
  896. /*************************************************************************/
  897. /*************************************************************************/
  898. /*************************************************************************/
  899. /* */
  900. /* <Function> */
  901. /* t1_builder_init */
  902. /* */
  903. /* <Description> */
  904. /* Initializes a given glyph builder. */
  905. /* */
  906. /* <InOut> */
  907. /* builder :: A pointer to the glyph builder to initialize. */
  908. /* */
  909. /* <Input> */
  910. /* face :: The current face object. */
  911. /* */
  912. /* size :: The current size object. */
  913. /* */
  914. /* glyph :: The current glyph object. */
  915. /* */
  916. /* hinting :: Whether hinting should be applied. */
  917. /* */
  918. FT_LOCAL_DEF( void )
  919. t1_builder_init( T1_Builder builder,
  920. FT_Face face,
  921. FT_Size size,
  922. FT_GlyphSlot glyph,
  923. FT_Bool hinting )
  924. {
  925. builder->path_begun = 0;
  926. builder->load_points = 1;
  927. builder->face = face;
  928. builder->glyph = glyph;
  929. builder->memory = face->memory;
  930. if ( glyph )
  931. {
  932. FT_GlyphLoader loader = glyph->internal->loader;
  933. builder->loader = loader;
  934. builder->base = &loader->base.outline;
  935. builder->current = &loader->current.outline;
  936. FT_GlyphLoader_Rewind( loader );
  937. builder->hints_globals = size->internal;
  938. builder->hints_funcs = 0;
  939. if ( hinting )
  940. builder->hints_funcs = glyph->internal->glyph_hints;
  941. }
  942. if ( size )
  943. {
  944. builder->scale_x = size->metrics.x_scale;
  945. builder->scale_y = size->metrics.y_scale;
  946. }
  947. builder->pos_x = 0;
  948. builder->pos_y = 0;
  949. builder->left_bearing.x = 0;
  950. builder->left_bearing.y = 0;
  951. builder->advance.x = 0;
  952. builder->advance.y = 0;
  953. builder->funcs = t1_builder_funcs;
  954. }
  955. /*************************************************************************/
  956. /* */
  957. /* <Function> */
  958. /* t1_builder_done */
  959. /* */
  960. /* <Description> */
  961. /* Finalizes a given glyph builder. Its contents can still be used */
  962. /* after the call, but the function saves important information */
  963. /* within the corresponding glyph slot. */
  964. /* */
  965. /* <Input> */
  966. /* builder :: A pointer to the glyph builder to finalize. */
  967. /* */
  968. FT_LOCAL_DEF( void )
  969. t1_builder_done( T1_Builder builder )
  970. {
  971. FT_GlyphSlot glyph = builder->glyph;
  972. if ( glyph )
  973. glyph->outline = *builder->base;
  974. }
  975. /* check that there is enough space for `count' more points */
  976. FT_LOCAL_DEF( FT_Error )
  977. t1_builder_check_points( T1_Builder builder,
  978. FT_Int count )
  979. {
  980. return FT_GlyphLoader_CheckPoints( builder->loader, count, 0 );
  981. }
  982. /* add a new point, do not check space */
  983. FT_LOCAL_DEF( void )
  984. t1_builder_add_point( T1_Builder builder,
  985. FT_Pos x,
  986. FT_Pos y,
  987. FT_Byte flag )
  988. {
  989. FT_Outline* outline = builder->current;
  990. if ( builder->load_points )
  991. {
  992. FT_Vector* point = outline->points + outline->n_points;
  993. FT_Byte* control = (FT_Byte*)outline->tags + outline->n_points;
  994. if ( builder->shift )
  995. {
  996. x >>= 16;
  997. y >>= 16;
  998. }
  999. point->x = x;
  1000. point->y = y;
  1001. *control = (FT_Byte)( flag ? FT_CURVE_TAG_ON : FT_CURVE_TAG_CUBIC );
  1002. builder->last = *point;
  1003. }
  1004. outline->n_points++;
  1005. }
  1006. /* check space for a new on-curve point, then add it */
  1007. FT_LOCAL_DEF( FT_Error )
  1008. t1_builder_add_point1( T1_Builder builder,
  1009. FT_Pos x,
  1010. FT_Pos y )
  1011. {
  1012. FT_Error error;
  1013. error = t1_builder_check_points( builder, 1 );
  1014. if ( !error )
  1015. t1_builder_add_point( builder, x, y, 1 );
  1016. return error;
  1017. }
  1018. /* check room for a new contour, then add it */
  1019. FT_LOCAL_DEF( FT_Error )
  1020. t1_builder_add_contour( T1_Builder builder )
  1021. {
  1022. FT_Outline* outline = builder->current;
  1023. FT_Error error;
  1024. if ( !builder->load_points )
  1025. {
  1026. outline->n_contours++;
  1027. return PSaux_Err_Ok;
  1028. }
  1029. error = FT_GlyphLoader_CheckPoints( builder->loader, 0, 1 );
  1030. if ( !error )
  1031. {
  1032. if ( outline->n_contours > 0 )
  1033. outline->contours[outline->n_contours - 1] =
  1034. (short)( outline->n_points - 1 );
  1035. outline->n_contours++;
  1036. }
  1037. return error;
  1038. }
  1039. /* if a path was begun, add its first on-curve point */
  1040. FT_LOCAL_DEF( FT_Error )
  1041. t1_builder_start_point( T1_Builder builder,
  1042. FT_Pos x,
  1043. FT_Pos y )
  1044. {
  1045. FT_Error error = 0;
  1046. /* test whether we are building a new contour */
  1047. if ( !builder->path_begun )
  1048. {
  1049. builder->path_begun = 1;
  1050. error = t1_builder_add_contour( builder );
  1051. if ( !error )
  1052. error = t1_builder_add_point1( builder, x, y );
  1053. }
  1054. return error;
  1055. }
  1056. /* close the current contour */
  1057. FT_LOCAL_DEF( void )
  1058. t1_builder_close_contour( T1_Builder builder )
  1059. {
  1060. FT_Outline* outline = builder->current;
  1061. /* XXXX: We must not include the last point in the path if it */
  1062. /* is located on the first point. */
  1063. if ( outline->n_points > 1 )
  1064. {
  1065. FT_Int first = 0;
  1066. FT_Vector* p1 = outline->points + first;
  1067. FT_Vector* p2 = outline->points + outline->n_points - 1;
  1068. FT_Byte* control = (FT_Byte*)outline->tags + outline->n_points - 1;
  1069. if ( outline->n_contours > 1 )
  1070. {
  1071. first = outline->contours[outline->n_contours - 2] + 1;
  1072. p1 = outline->points + first;
  1073. }
  1074. /* `delete' last point only if it coincides with the first */
  1075. /* point and it is not a control point (which can happen). */
  1076. if ( p1->x == p2->x && p1->y == p2->y )
  1077. if ( *control == FT_CURVE_TAG_ON )
  1078. outline->n_points--;
  1079. }
  1080. if ( outline->n_contours > 0 )
  1081. outline->contours[outline->n_contours - 1] =
  1082. (short)( outline->n_points - 1 );
  1083. }
  1084. /*************************************************************************/
  1085. /*************************************************************************/
  1086. /***** *****/
  1087. /***** OTHER *****/
  1088. /***** *****/
  1089. /*************************************************************************/
  1090. /*************************************************************************/
  1091. FT_LOCAL_DEF( void )
  1092. t1_decrypt( FT_Byte* buffer,
  1093. FT_Offset length,
  1094. FT_UShort seed )
  1095. {
  1096. while ( length > 0 )
  1097. {
  1098. FT_Byte plain;
  1099. plain = (FT_Byte)( *buffer ^ ( seed >> 8 ) );
  1100. seed = (FT_UShort)( ( *buffer + seed ) * 52845U + 22719 );
  1101. *buffer++ = plain;
  1102. length--;
  1103. }
  1104. }
  1105. /* END */