pshrec.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. /***************************************************************************/
  2. /* */
  3. /* pshrec.c */
  4. /* */
  5. /* FreeType PostScript hints recorder (body). */
  6. /* */
  7. /* Copyright 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_FREETYPE_H
  19. #include FT_INTERNAL_OBJECTS_H
  20. #include FT_INTERNAL_DEBUG_H
  21. #include "pshrec.h"
  22. #include "pshalgo.h"
  23. #undef FT_COMPONENT
  24. #define FT_COMPONENT trace_pshrec
  25. #ifdef DEBUG_HINTER
  26. PS_Hints ps_debug_hints = 0;
  27. int ps_debug_no_horz_hints = 0;
  28. int ps_debug_no_vert_hints = 0;
  29. #endif
  30. /*************************************************************************/
  31. /*************************************************************************/
  32. /***** *****/
  33. /***** PS_HINT MANAGEMENT *****/
  34. /***** *****/
  35. /*************************************************************************/
  36. /*************************************************************************/
  37. /* destroy hints table */
  38. static void
  39. ps_hint_table_done( PS_Hint_Table table,
  40. FT_Memory memory )
  41. {
  42. FT_FREE( table->hints );
  43. table->num_hints = 0;
  44. table->max_hints = 0;
  45. }
  46. /* ensure that a table can contain "count" elements */
  47. static FT_Error
  48. ps_hint_table_ensure( PS_Hint_Table table,
  49. FT_UInt count,
  50. FT_Memory memory )
  51. {
  52. FT_UInt old_max = table->max_hints;
  53. FT_UInt new_max = count;
  54. FT_Error error = 0;
  55. if ( new_max > old_max )
  56. {
  57. /* try to grow the table */
  58. new_max = ( new_max + 7 ) & -8;
  59. if ( !FT_RENEW_ARRAY( table->hints, old_max, new_max ) )
  60. table->max_hints = new_max;
  61. }
  62. return error;
  63. }
  64. static FT_Error
  65. ps_hint_table_alloc( PS_Hint_Table table,
  66. FT_Memory memory,
  67. PS_Hint *ahint )
  68. {
  69. FT_Error error = 0;
  70. FT_UInt count;
  71. PS_Hint hint = 0;
  72. count = table->num_hints;
  73. count++;
  74. if ( count >= table->max_hints )
  75. {
  76. error = ps_hint_table_ensure( table, count, memory );
  77. if ( error )
  78. goto Exit;
  79. }
  80. hint = table->hints + count - 1;
  81. hint->pos = 0;
  82. hint->len = 0;
  83. hint->flags = 0;
  84. table->num_hints = count;
  85. Exit:
  86. *ahint = hint;
  87. return error;
  88. }
  89. /*************************************************************************/
  90. /*************************************************************************/
  91. /***** *****/
  92. /***** PS_MASK MANAGEMENT *****/
  93. /***** *****/
  94. /*************************************************************************/
  95. /*************************************************************************/
  96. /* destroy mask */
  97. static void
  98. ps_mask_done( PS_Mask mask,
  99. FT_Memory memory )
  100. {
  101. FT_FREE( mask->bytes );
  102. mask->num_bits = 0;
  103. mask->max_bits = 0;
  104. mask->end_point = 0;
  105. }
  106. /* ensure that a mask can contain "count" bits */
  107. static FT_Error
  108. ps_mask_ensure( PS_Mask mask,
  109. FT_UInt count,
  110. FT_Memory memory )
  111. {
  112. FT_UInt old_max = ( mask->max_bits + 7 ) >> 3;
  113. FT_UInt new_max = ( count + 7 ) >> 3;
  114. FT_Error error = 0;
  115. if ( new_max > old_max )
  116. {
  117. new_max = ( new_max + 7 ) & -8;
  118. if ( !FT_RENEW_ARRAY( mask->bytes, old_max, new_max ) )
  119. mask->max_bits = new_max * 8;
  120. }
  121. return error;
  122. }
  123. /* test a bit value in a given mask */
  124. static FT_Int
  125. ps_mask_test_bit( PS_Mask mask,
  126. FT_Int idx )
  127. {
  128. if ( (FT_UInt)idx >= mask->num_bits )
  129. return 0;
  130. return mask->bytes[idx >> 3] & ( 0x80 >> ( idx & 7 ) );
  131. }
  132. /* clear a given bit */
  133. static void
  134. ps_mask_clear_bit( PS_Mask mask,
  135. FT_Int idx )
  136. {
  137. FT_Byte* p;
  138. if ( (FT_UInt)idx >= mask->num_bits )
  139. return;
  140. p = mask->bytes + ( idx >> 3 );
  141. p[0] = (FT_Byte)( p[0] & ~( 0x80 >> ( idx & 7 ) ) );
  142. }
  143. /* set a given bit, possibly grow the mask */
  144. static FT_Error
  145. ps_mask_set_bit( PS_Mask mask,
  146. FT_Int idx,
  147. FT_Memory memory )
  148. {
  149. FT_Error error = 0;
  150. FT_Byte* p;
  151. if ( idx < 0 )
  152. goto Exit;
  153. if ( (FT_UInt)idx >= mask->num_bits )
  154. {
  155. error = ps_mask_ensure( mask, idx + 1, memory );
  156. if ( error )
  157. goto Exit;
  158. mask->num_bits = idx + 1;
  159. }
  160. p = mask->bytes + ( idx >> 3 );
  161. p[0] = (FT_Byte)( p[0] | ( 0x80 >> ( idx & 7 ) ) );
  162. Exit:
  163. return error;
  164. }
  165. /* destroy mask table */
  166. static void
  167. ps_mask_table_done( PS_Mask_Table table,
  168. FT_Memory memory )
  169. {
  170. FT_UInt count = table->max_masks;
  171. PS_Mask mask = table->masks;
  172. for ( ; count > 0; count--, mask++ )
  173. ps_mask_done( mask, memory );
  174. FT_FREE( table->masks );
  175. table->num_masks = 0;
  176. table->max_masks = 0;
  177. }
  178. /* ensure that a mask table can contain "count" masks */
  179. static FT_Error
  180. ps_mask_table_ensure( PS_Mask_Table table,
  181. FT_UInt count,
  182. FT_Memory memory )
  183. {
  184. FT_UInt old_max = table->max_masks;
  185. FT_UInt new_max = count;
  186. FT_Error error = 0;
  187. if ( new_max > old_max )
  188. {
  189. new_max = ( new_max + 7 ) & -8;
  190. if ( !FT_RENEW_ARRAY( table->masks, old_max, new_max ) )
  191. table->max_masks = new_max;
  192. }
  193. return error;
  194. }
  195. /* allocate a new mask in a table */
  196. static FT_Error
  197. ps_mask_table_alloc( PS_Mask_Table table,
  198. FT_Memory memory,
  199. PS_Mask *amask )
  200. {
  201. FT_UInt count;
  202. FT_Error error = 0;
  203. PS_Mask mask = 0;
  204. count = table->num_masks;
  205. count++;
  206. if ( count > table->max_masks )
  207. {
  208. error = ps_mask_table_ensure( table, count, memory );
  209. if ( error )
  210. goto Exit;
  211. }
  212. mask = table->masks + count - 1;
  213. mask->num_bits = 0;
  214. mask->end_point = 0;
  215. table->num_masks = count;
  216. Exit:
  217. *amask = mask;
  218. return error;
  219. }
  220. /* return last hint mask in a table, create one if the table is empty */
  221. static FT_Error
  222. ps_mask_table_last( PS_Mask_Table table,
  223. FT_Memory memory,
  224. PS_Mask *amask )
  225. {
  226. FT_Error error = 0;
  227. FT_UInt count;
  228. PS_Mask mask;
  229. count = table->num_masks;
  230. if ( count == 0 )
  231. {
  232. error = ps_mask_table_alloc( table, memory, &mask );
  233. if ( error )
  234. goto Exit;
  235. }
  236. else
  237. mask = table->masks + count - 1;
  238. Exit:
  239. *amask = mask;
  240. return error;
  241. }
  242. /* set a new mask to a given bit range */
  243. static FT_Error
  244. ps_mask_table_set_bits( PS_Mask_Table table,
  245. FT_Byte* source,
  246. FT_UInt bit_pos,
  247. FT_UInt bit_count,
  248. FT_Memory memory )
  249. {
  250. FT_Error error = 0;
  251. PS_Mask mask;
  252. /* allocate new mask, and grow it to "bit_count" bits */
  253. error = ps_mask_table_alloc( table, memory, &mask );
  254. if ( error )
  255. goto Exit;
  256. error = ps_mask_ensure( mask, bit_count, memory );
  257. if ( error )
  258. goto Exit;
  259. mask->num_bits = bit_count;
  260. /* now, copy bits */
  261. {
  262. FT_Byte* read = source + ( bit_pos >> 3 );
  263. FT_Int rmask = 0x80 >> ( bit_pos & 7 );
  264. FT_Byte* write = mask->bytes;
  265. FT_Int wmask = 0x80;
  266. FT_Int val;
  267. for ( ; bit_count > 0; bit_count-- )
  268. {
  269. val = write[0] & ~wmask;
  270. if ( read[0] & rmask )
  271. val |= wmask;
  272. write[0] = (FT_Byte)val;
  273. rmask >>= 1;
  274. if ( rmask == 0 )
  275. {
  276. read++;
  277. rmask = 0x80;
  278. }
  279. wmask >>= 1;
  280. if ( wmask == 0 )
  281. {
  282. write++;
  283. wmask = 0x80;
  284. }
  285. }
  286. }
  287. Exit:
  288. return error;
  289. }
  290. /* test whether two masks in a table intersect */
  291. static FT_Int
  292. ps_mask_table_test_intersect( PS_Mask_Table table,
  293. FT_Int index1,
  294. FT_Int index2 )
  295. {
  296. PS_Mask mask1 = table->masks + index1;
  297. PS_Mask mask2 = table->masks + index2;
  298. FT_Byte* p1 = mask1->bytes;
  299. FT_Byte* p2 = mask2->bytes;
  300. FT_UInt count1 = mask1->num_bits;
  301. FT_UInt count2 = mask2->num_bits;
  302. FT_UInt count;
  303. count = ( count1 <= count2 ) ? count1 : count2;
  304. for ( ; count >= 8; count -= 8 )
  305. {
  306. if ( p1[0] & p2[0] )
  307. return 1;
  308. p1++;
  309. p2++;
  310. }
  311. if ( count == 0 )
  312. return 0;
  313. return ( p1[0] & p2[0] ) & ~( 0xFF >> count );
  314. }
  315. /* merge two masks, used by ps_mask_table_merge_all */
  316. static FT_Error
  317. ps_mask_table_merge( PS_Mask_Table table,
  318. FT_Int index1,
  319. FT_Int index2,
  320. FT_Memory memory )
  321. {
  322. FT_UInt temp;
  323. FT_Error error = 0;
  324. /* swap index1 and index2 so that index1 < index2 */
  325. if ( index1 > index2 )
  326. {
  327. temp = index1;
  328. index1 = index2;
  329. index2 = temp;
  330. }
  331. if ( index1 < index2 && index1 >= 0 && index2 < (FT_Int)table->num_masks )
  332. {
  333. /* we need to merge the bitsets of index1 and index2 with a */
  334. /* simple union */
  335. PS_Mask mask1 = table->masks + index1;
  336. PS_Mask mask2 = table->masks + index2;
  337. FT_UInt count1 = mask1->num_bits;
  338. FT_UInt count2 = mask2->num_bits;
  339. FT_Int delta;
  340. if ( count2 > 0 )
  341. {
  342. FT_UInt pos;
  343. FT_Byte* read;
  344. FT_Byte* write;
  345. /* if "count2" is greater than "count1", we need to grow the */
  346. /* first bitset, and clear the highest bits */
  347. if ( count2 > count1 )
  348. {
  349. error = ps_mask_ensure( mask1, count2, memory );
  350. if ( error )
  351. goto Exit;
  352. for ( pos = count1; pos < count2; pos++ )
  353. ps_mask_clear_bit( mask1, pos );
  354. }
  355. /* merge (unite) the bitsets */
  356. read = mask2->bytes;
  357. write = mask1->bytes;
  358. pos = (FT_UInt)( ( count2 + 7 ) >> 3 );
  359. for ( ; pos > 0; pos-- )
  360. {
  361. write[0] = (FT_Byte)( write[0] | read[0] );
  362. write++;
  363. read++;
  364. }
  365. }
  366. /* Now, remove "mask2" from the list. We need to keep the masks */
  367. /* sorted in order of importance, so move table elements. */
  368. mask2->num_bits = 0;
  369. mask2->end_point = 0;
  370. delta = table->num_masks - 1 - index2; /* number of masks to move */
  371. if ( delta > 0 )
  372. {
  373. /* move to end of table for reuse */
  374. PS_MaskRec dummy = *mask2;
  375. ft_memmove( mask2, mask2 + 1, delta * sizeof ( PS_MaskRec ) );
  376. mask2[delta] = dummy;
  377. }
  378. table->num_masks--;
  379. }
  380. else
  381. FT_ERROR(( "ps_mask_table_merge: ignoring invalid indices (%d,%d)\n",
  382. index1, index2 ));
  383. Exit:
  384. return error;
  385. }
  386. /* Try to merge all masks in a given table. This is used to merge */
  387. /* all counter masks into independent counter "paths". */
  388. /* */
  389. static FT_Error
  390. ps_mask_table_merge_all( PS_Mask_Table table,
  391. FT_Memory memory )
  392. {
  393. FT_Int index1, index2;
  394. FT_Error error = 0;
  395. for ( index1 = table->num_masks - 1; index1 > 0; index1-- )
  396. {
  397. for ( index2 = index1 - 1; index2 >= 0; index2-- )
  398. {
  399. if ( ps_mask_table_test_intersect( table, index1, index2 ) )
  400. {
  401. error = ps_mask_table_merge( table, index2, index1, memory );
  402. if ( error )
  403. goto Exit;
  404. break;
  405. }
  406. }
  407. }
  408. Exit:
  409. return error;
  410. }
  411. /*************************************************************************/
  412. /*************************************************************************/
  413. /***** *****/
  414. /***** PS_DIMENSION MANAGEMENT *****/
  415. /***** *****/
  416. /*************************************************************************/
  417. /*************************************************************************/
  418. /* finalize a given dimension */
  419. static void
  420. ps_dimension_done( PS_Dimension dimension,
  421. FT_Memory memory )
  422. {
  423. ps_mask_table_done( &dimension->counters, memory );
  424. ps_mask_table_done( &dimension->masks, memory );
  425. ps_hint_table_done( &dimension->hints, memory );
  426. }
  427. /* initialize a given dimension */
  428. static void
  429. ps_dimension_init( PS_Dimension dimension )
  430. {
  431. dimension->hints.num_hints = 0;
  432. dimension->masks.num_masks = 0;
  433. dimension->counters.num_masks = 0;
  434. }
  435. #if 0
  436. /* set a bit at a given index in the current hint mask */
  437. static FT_Error
  438. ps_dimension_set_mask_bit( PS_Dimension dim,
  439. FT_UInt idx,
  440. FT_Memory memory )
  441. {
  442. PS_Mask mask;
  443. FT_Error error = 0;
  444. /* get last hint mask */
  445. error = ps_mask_table_last( &dim->masks, memory, &mask );
  446. if ( error )
  447. goto Exit;
  448. error = ps_mask_set_bit( mask, idx, memory );
  449. Exit:
  450. return error;
  451. }
  452. #endif
  453. /* set the end point in a mask, called from "End" & "Reset" methods */
  454. static void
  455. ps_dimension_end_mask( PS_Dimension dim,
  456. FT_UInt end_point )
  457. {
  458. FT_UInt count = dim->masks.num_masks;
  459. PS_Mask mask;
  460. if ( count > 0 )
  461. {
  462. mask = dim->masks.masks + count - 1;
  463. mask->end_point = end_point;
  464. }
  465. }
  466. /* set the end point in the current mask, then create a new empty one */
  467. /* (called by "Reset" method) */
  468. static FT_Error
  469. ps_dimension_reset_mask( PS_Dimension dim,
  470. FT_UInt end_point,
  471. FT_Memory memory )
  472. {
  473. PS_Mask mask;
  474. /* end current mask */
  475. ps_dimension_end_mask( dim, end_point );
  476. /* allocate new one */
  477. return ps_mask_table_alloc( &dim->masks, memory, &mask );
  478. }
  479. /* set a new mask, called from the "T2Stem" method */
  480. static FT_Error
  481. ps_dimension_set_mask_bits( PS_Dimension dim,
  482. const FT_Byte* source,
  483. FT_UInt source_pos,
  484. FT_UInt source_bits,
  485. FT_UInt end_point,
  486. FT_Memory memory )
  487. {
  488. FT_Error error = 0;
  489. /* reset current mask, if any */
  490. error = ps_dimension_reset_mask( dim, end_point, memory );
  491. if ( error )
  492. goto Exit;
  493. /* set bits in new mask */
  494. error = ps_mask_table_set_bits( &dim->masks, (FT_Byte*)source,
  495. source_pos, source_bits, memory );
  496. Exit:
  497. return error;
  498. }
  499. /* add a new single stem (called from "T1Stem" method) */
  500. static FT_Error
  501. ps_dimension_add_t1stem( PS_Dimension dim,
  502. FT_Int pos,
  503. FT_Int len,
  504. FT_Memory memory,
  505. FT_Int *aindex )
  506. {
  507. FT_Error error = 0;
  508. FT_UInt flags = 0;
  509. /* detect ghost stem */
  510. if ( len < 0 )
  511. {
  512. flags |= PS_HINT_FLAG_GHOST;
  513. if ( len == -21 )
  514. {
  515. flags |= PS_HINT_FLAG_BOTTOM;
  516. pos += len;
  517. }
  518. len = 0;
  519. }
  520. if ( aindex )
  521. *aindex = -1;
  522. /* now, lookup stem in the current hints table */
  523. {
  524. PS_Mask mask;
  525. FT_UInt idx;
  526. FT_UInt max = dim->hints.num_hints;
  527. PS_Hint hint = dim->hints.hints;
  528. for ( idx = 0; idx < max; idx++, hint++ )
  529. {
  530. if ( hint->pos == pos && hint->len == len )
  531. break;
  532. }
  533. /* we need to create a new hint in the table */
  534. if ( idx >= max )
  535. {
  536. error = ps_hint_table_alloc( &dim->hints, memory, &hint );
  537. if ( error )
  538. goto Exit;
  539. hint->pos = pos;
  540. hint->len = len;
  541. hint->flags = flags;
  542. }
  543. /* now, store the hint in the current mask */
  544. error = ps_mask_table_last( &dim->masks, memory, &mask );
  545. if ( error )
  546. goto Exit;
  547. error = ps_mask_set_bit( mask, idx, memory );
  548. if ( error )
  549. goto Exit;
  550. if ( aindex )
  551. *aindex = (FT_Int)idx;
  552. }
  553. Exit:
  554. return error;
  555. }
  556. /* add a "hstem3/vstem3" counter to our dimension table */
  557. static FT_Error
  558. ps_dimension_add_counter( PS_Dimension dim,
  559. FT_Int hint1,
  560. FT_Int hint2,
  561. FT_Int hint3,
  562. FT_Memory memory )
  563. {
  564. FT_Error error = 0;
  565. FT_UInt count = dim->counters.num_masks;
  566. PS_Mask counter = dim->counters.masks;
  567. /* try to find an existing counter mask that already uses */
  568. /* one of these stems here */
  569. for ( ; count > 0; count--, counter++ )
  570. {
  571. if ( ps_mask_test_bit( counter, hint1 ) ||
  572. ps_mask_test_bit( counter, hint2 ) ||
  573. ps_mask_test_bit( counter, hint3 ) )
  574. break;
  575. }
  576. /* creat a new counter when needed */
  577. if ( count == 0 )
  578. {
  579. error = ps_mask_table_alloc( &dim->counters, memory, &counter );
  580. if ( error )
  581. goto Exit;
  582. }
  583. /* now, set the bits for our hints in the counter mask */
  584. error = ps_mask_set_bit( counter, hint1, memory );
  585. if ( error )
  586. goto Exit;
  587. error = ps_mask_set_bit( counter, hint2, memory );
  588. if ( error )
  589. goto Exit;
  590. error = ps_mask_set_bit( counter, hint3, memory );
  591. if ( error )
  592. goto Exit;
  593. Exit:
  594. return error;
  595. }
  596. /* end of recording session for a given dimension */
  597. static FT_Error
  598. ps_dimension_end( PS_Dimension dim,
  599. FT_UInt end_point,
  600. FT_Memory memory )
  601. {
  602. /* end hint mask table */
  603. ps_dimension_end_mask( dim, end_point );
  604. /* merge all counter masks into independent "paths" */
  605. return ps_mask_table_merge_all( &dim->counters, memory );
  606. }
  607. /*************************************************************************/
  608. /*************************************************************************/
  609. /***** *****/
  610. /***** PS_RECORDER MANAGEMENT *****/
  611. /***** *****/
  612. /*************************************************************************/
  613. /*************************************************************************/
  614. /* destroy hints */
  615. FT_LOCAL( void )
  616. ps_hints_done( PS_Hints hints )
  617. {
  618. FT_Memory memory = hints->memory;
  619. ps_dimension_done( &hints->dimension[0], memory );
  620. ps_dimension_done( &hints->dimension[1], memory );
  621. hints->error = 0;
  622. hints->memory = 0;
  623. }
  624. FT_LOCAL( FT_Error )
  625. ps_hints_init( PS_Hints hints,
  626. FT_Memory memory )
  627. {
  628. FT_MEM_ZERO( hints, sizeof ( *hints ) );
  629. hints->memory = memory;
  630. return 0;
  631. }
  632. /* initialize a hints for a new session */
  633. static void
  634. ps_hints_open( PS_Hints hints,
  635. PS_Hint_Type hint_type )
  636. {
  637. switch ( hint_type )
  638. {
  639. case PS_HINT_TYPE_1:
  640. case PS_HINT_TYPE_2:
  641. hints->error = 0;
  642. hints->hint_type = hint_type;
  643. ps_dimension_init( &hints->dimension[0] );
  644. ps_dimension_init( &hints->dimension[1] );
  645. break;
  646. default:
  647. hints->error = FT_Err_Invalid_Argument;
  648. hints->hint_type = hint_type;
  649. FT_ERROR(( "ps_hints_open: invalid charstring type!\n" ));
  650. break;
  651. }
  652. }
  653. /* add one or more stems to the current hints table */
  654. static void
  655. ps_hints_stem( PS_Hints hints,
  656. FT_Int dimension,
  657. FT_UInt count,
  658. FT_Long* stems )
  659. {
  660. if ( !hints->error )
  661. {
  662. /* limit "dimension" to 0..1 */
  663. if ( dimension < 0 || dimension > 1 )
  664. {
  665. FT_ERROR(( "ps_hints_stem: invalid dimension (%d) used\n",
  666. dimension ));
  667. dimension = ( dimension != 0 );
  668. }
  669. /* record the stems in the current hints/masks table */
  670. switch ( hints->hint_type )
  671. {
  672. case PS_HINT_TYPE_1: /* Type 1 "hstem" or "vstem" operator */
  673. case PS_HINT_TYPE_2: /* Type 2 "hstem" or "vstem" operator */
  674. {
  675. PS_Dimension dim = &hints->dimension[dimension];
  676. for ( ; count > 0; count--, stems += 2 )
  677. {
  678. FT_Error error;
  679. FT_Memory memory = hints->memory;
  680. error = ps_dimension_add_t1stem( dim, stems[0], stems[1],
  681. memory, NULL );
  682. if ( error )
  683. {
  684. FT_ERROR(( "ps_hints_stem: could not add stem"
  685. " (%d,%d) to hints table\n", stems[0], stems[1] ));
  686. hints->error = error;
  687. return;
  688. }
  689. }
  690. break;
  691. }
  692. default:
  693. FT_ERROR(( "ps_hints_stem: called with invalid hint type (%d)\n",
  694. hints->hint_type ));
  695. break;
  696. }
  697. }
  698. }
  699. /* add one Type1 counter stem to the current hints table */
  700. static void
  701. ps_hints_t1stem3( PS_Hints hints,
  702. FT_Int dimension,
  703. FT_Long* stems )
  704. {
  705. FT_Error error = 0;
  706. if ( !hints->error )
  707. {
  708. PS_Dimension dim;
  709. FT_Memory memory = hints->memory;
  710. FT_Int count;
  711. FT_Int idx[3];
  712. /* limit "dimension" to 0..1 */
  713. if ( dimension < 0 || dimension > 1 )
  714. {
  715. FT_ERROR(( "ps_hints_t1stem3: invalid dimension (%d) used\n",
  716. dimension ));
  717. dimension = ( dimension != 0 );
  718. }
  719. dim = &hints->dimension[dimension];
  720. /* there must be 6 elements in the 'stem' array */
  721. if ( hints->hint_type == PS_HINT_TYPE_1 )
  722. {
  723. /* add the three stems to our hints/masks table */
  724. for ( count = 0; count < 3; count++, stems += 2 )
  725. {
  726. error = ps_dimension_add_t1stem( dim, stems[0], stems[1],
  727. memory, &idx[count] );
  728. if ( error )
  729. goto Fail;
  730. }
  731. /* now, add the hints to the counters table */
  732. error = ps_dimension_add_counter( dim, idx[0], idx[1], idx[2],
  733. memory );
  734. if ( error )
  735. goto Fail;
  736. }
  737. else
  738. {
  739. FT_ERROR(( "ps_hints_t1stem3: called with invalid hint type!\n" ));
  740. error = FT_Err_Invalid_Argument;
  741. goto Fail;
  742. }
  743. }
  744. return;
  745. Fail:
  746. FT_ERROR(( "ps_hints_t1stem3: could not add counter stems to table\n" ));
  747. hints->error = error;
  748. }
  749. /* reset hints (only with Type 1 hints) */
  750. static void
  751. ps_hints_t1reset( PS_Hints hints,
  752. FT_UInt end_point )
  753. {
  754. FT_Error error = 0;
  755. if ( !hints->error )
  756. {
  757. FT_Memory memory = hints->memory;
  758. if ( hints->hint_type == PS_HINT_TYPE_1 )
  759. {
  760. error = ps_dimension_reset_mask( &hints->dimension[0],
  761. end_point, memory );
  762. if ( error )
  763. goto Fail;
  764. error = ps_dimension_reset_mask( &hints->dimension[1],
  765. end_point, memory );
  766. if ( error )
  767. goto Fail;
  768. }
  769. else
  770. {
  771. /* invalid hint type */
  772. error = FT_Err_Invalid_Argument;
  773. goto Fail;
  774. }
  775. }
  776. return;
  777. Fail:
  778. hints->error = error;
  779. }
  780. /* Type2 "hintmask" operator, add a new hintmask to each direction */
  781. static void
  782. ps_hints_t2mask( PS_Hints hints,
  783. FT_UInt end_point,
  784. FT_UInt bit_count,
  785. const FT_Byte* bytes )
  786. {
  787. FT_Error error;
  788. if ( !hints->error )
  789. {
  790. PS_Dimension dim = hints->dimension;
  791. FT_Memory memory = hints->memory;
  792. FT_UInt count1 = dim[0].hints.num_hints;
  793. FT_UInt count2 = dim[1].hints.num_hints;
  794. /* check bit count; must be equal to current total hint count */
  795. if ( bit_count != count1 + count2 )
  796. {
  797. FT_ERROR(( "ps_hints_t2mask: "
  798. "called with invalid bitcount %d (instead of %d)\n",
  799. bit_count, count1 + count2 ));
  800. /* simply ignore the operator */
  801. return;
  802. }
  803. /* set-up new horizontal and vertical hint mask now */
  804. error = ps_dimension_set_mask_bits( &dim[0], bytes, 0, count1,
  805. end_point, memory );
  806. if ( error )
  807. goto Fail;
  808. error = ps_dimension_set_mask_bits( &dim[1], bytes, count1, count2,
  809. end_point, memory );
  810. if ( error )
  811. goto Fail;
  812. }
  813. return;
  814. Fail:
  815. hints->error = error;
  816. }
  817. static void
  818. ps_hints_t2counter( PS_Hints hints,
  819. FT_UInt bit_count,
  820. const FT_Byte* bytes )
  821. {
  822. FT_Error error;
  823. if ( !hints->error )
  824. {
  825. PS_Dimension dim = hints->dimension;
  826. FT_Memory memory = hints->memory;
  827. FT_UInt count1 = dim[0].hints.num_hints;
  828. FT_UInt count2 = dim[1].hints.num_hints;
  829. /* check bit count, must be equal to current total hint count */
  830. if ( bit_count != count1 + count2 )
  831. {
  832. FT_ERROR(( "ps_hints_t2counter: "
  833. "called with invalid bitcount %d (instead of %d)\n",
  834. bit_count, count1 + count2 ));
  835. /* simply ignore the operator */
  836. return;
  837. }
  838. /* set-up new horizontal and vertical hint mask now */
  839. error = ps_dimension_set_mask_bits( &dim[0], bytes, 0, count1,
  840. 0, memory );
  841. if ( error )
  842. goto Fail;
  843. error = ps_dimension_set_mask_bits( &dim[1], bytes, count1, count2,
  844. 0, memory );
  845. if ( error )
  846. goto Fail;
  847. }
  848. return;
  849. Fail:
  850. hints->error = error;
  851. }
  852. /* end recording session */
  853. static FT_Error
  854. ps_hints_close( PS_Hints hints,
  855. FT_UInt end_point )
  856. {
  857. FT_Error error;
  858. error = hints->error;
  859. if ( !error )
  860. {
  861. FT_Memory memory = hints->memory;
  862. PS_Dimension dim = hints->dimension;
  863. error = ps_dimension_end( &dim[0], end_point, memory );
  864. if ( !error )
  865. {
  866. error = ps_dimension_end( &dim[1], end_point, memory );
  867. }
  868. }
  869. #ifdef DEBUG_HINTER
  870. if ( !error )
  871. ps_debug_hints = hints;
  872. #endif
  873. return error;
  874. }
  875. /*************************************************************************/
  876. /*************************************************************************/
  877. /***** *****/
  878. /***** TYPE 1 HINTS RECORDING INTERFACE *****/
  879. /***** *****/
  880. /*************************************************************************/
  881. /*************************************************************************/
  882. static void
  883. t1_hints_open( T1_Hints hints )
  884. {
  885. ps_hints_open( (PS_Hints)hints, PS_HINT_TYPE_1 );
  886. }
  887. static void
  888. t1_hints_stem( T1_Hints hints,
  889. FT_Int dimension,
  890. FT_Long* coords )
  891. {
  892. ps_hints_stem( (PS_Hints)hints, dimension, 1, coords );
  893. }
  894. FT_LOCAL_DEF( void )
  895. t1_hints_funcs_init( T1_Hints_FuncsRec* funcs )
  896. {
  897. FT_MEM_ZERO( (char*)funcs, sizeof ( *funcs ) );
  898. funcs->open = (T1_Hints_OpenFunc) t1_hints_open;
  899. funcs->close = (T1_Hints_CloseFunc) ps_hints_close;
  900. funcs->stem = (T1_Hints_SetStemFunc) t1_hints_stem;
  901. funcs->stem3 = (T1_Hints_SetStem3Func)ps_hints_t1stem3;
  902. funcs->reset = (T1_Hints_ResetFunc) ps_hints_t1reset;
  903. funcs->apply = (T1_Hints_ApplyFunc) PS_HINTS_APPLY_FUNC;
  904. }
  905. /*************************************************************************/
  906. /*************************************************************************/
  907. /***** *****/
  908. /***** TYPE 2 HINTS RECORDING INTERFACE *****/
  909. /***** *****/
  910. /*************************************************************************/
  911. /*************************************************************************/
  912. static void
  913. t2_hints_open( T2_Hints hints )
  914. {
  915. ps_hints_open( (PS_Hints)hints, PS_HINT_TYPE_2 );
  916. }
  917. static void
  918. t2_hints_stems( T2_Hints hints,
  919. FT_Int dimension,
  920. FT_Int count,
  921. FT_Fixed* coords )
  922. {
  923. FT_Pos stems[32], y, n, total = count;
  924. y = 0;
  925. while ( total > 0 )
  926. {
  927. /* determine number of stems to write */
  928. count = total;
  929. if ( count > 16 )
  930. count = 16;
  931. /* compute integer stem positions in font units */
  932. for ( n = 0; n < count * 2; n++ )
  933. {
  934. y += coords[n];
  935. stems[n] = ( y + 0x8000 ) >> 16;
  936. }
  937. /* compute lengths */
  938. for ( n = 0; n < count * 2; n += 2 )
  939. stems[n + 1] = stems[n + 1] - stems[n];
  940. /* add them to the current dimension */
  941. ps_hints_stem( (PS_Hints)hints, dimension, count, stems );
  942. total -= count;
  943. }
  944. }
  945. FT_LOCAL_DEF( void )
  946. t2_hints_funcs_init( T2_Hints_FuncsRec* funcs )
  947. {
  948. FT_MEM_ZERO( funcs, sizeof ( *funcs ) );
  949. funcs->open = (T2_Hints_OpenFunc) t2_hints_open;
  950. funcs->close = (T2_Hints_CloseFunc) ps_hints_close;
  951. funcs->stems = (T2_Hints_StemsFunc) t2_hints_stems;
  952. funcs->hintmask= (T2_Hints_MaskFunc) ps_hints_t2mask;
  953. funcs->counter = (T2_Hints_CounterFunc)ps_hints_t2counter;
  954. funcs->apply = (T2_Hints_ApplyFunc) PS_HINTS_APPLY_FUNC;
  955. }
  956. /* END */