ttsbit.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. /***************************************************************************/
  2. /* */
  3. /* ttsbit.c */
  4. /* */
  5. /* TrueType and OpenType embedded bitmap support (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_DEBUG_H
  19. #include FT_INTERNAL_STREAM_H
  20. #include FT_TRUETYPE_TAGS_H
  21. #include "ttsbit.h"
  22. #include "sferrors.h"
  23. /*************************************************************************/
  24. /* */
  25. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  26. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  27. /* messages during execution. */
  28. /* */
  29. #undef FT_COMPONENT
  30. #define FT_COMPONENT trace_ttsbit
  31. /*************************************************************************/
  32. /* */
  33. /* <Function> */
  34. /* blit_sbit */
  35. /* */
  36. /* <Description> */
  37. /* Blits a bitmap from an input stream into a given target. Supports */
  38. /* x and y offsets as well as byte padded lines. */
  39. /* */
  40. /* <Input> */
  41. /* target :: The target bitmap/pixmap. */
  42. /* */
  43. /* source :: The input packed bitmap data. */
  44. /* */
  45. /* line_bits :: The number of bits per line. */
  46. /* */
  47. /* byte_padded :: A flag which is true if lines are byte-padded. */
  48. /* */
  49. /* x_offset :: The horizontal offset. */
  50. /* */
  51. /* y_offset :: The vertical offset. */
  52. /* */
  53. /* <Note> */
  54. /* IMPORTANT: The x and y offsets are relative to the top corner of */
  55. /* the target bitmap (unlike the normal TrueType */
  56. /* convention). A positive y offset indicates a downwards */
  57. /* direction! */
  58. /* */
  59. static void
  60. blit_sbit( FT_Bitmap* target,
  61. FT_Byte* source,
  62. FT_Int line_bits,
  63. FT_Bool byte_padded,
  64. FT_Int x_offset,
  65. FT_Int y_offset )
  66. {
  67. FT_Byte* line_buff;
  68. FT_Int line_incr;
  69. FT_Int height;
  70. FT_UShort acc;
  71. FT_UInt loaded;
  72. /* first of all, compute starting write position */
  73. line_incr = target->pitch;
  74. line_buff = target->buffer;
  75. if ( line_incr < 0 )
  76. line_buff -= line_incr * ( target->rows - 1 );
  77. line_buff += ( x_offset >> 3 ) + y_offset * line_incr;
  78. /***********************************************************************/
  79. /* */
  80. /* We use the extra-classic `accumulator' trick to extract the bits */
  81. /* from the source byte stream. */
  82. /* */
  83. /* Namely, the variable `acc' is a 16-bit accumulator containing the */
  84. /* last `loaded' bits from the input stream. The bits are shifted to */
  85. /* the upmost position in `acc'. */
  86. /* */
  87. /***********************************************************************/
  88. acc = 0; /* clear accumulator */
  89. loaded = 0; /* no bits were loaded */
  90. for ( height = target->rows; height > 0; height-- )
  91. {
  92. FT_Byte* cur = line_buff; /* current write cursor */
  93. FT_Int count = line_bits; /* # of bits to extract per line */
  94. FT_Byte shift = (FT_Byte)( x_offset & 7 ); /* current write shift */
  95. FT_Byte space = (FT_Byte)( 8 - shift );
  96. /* first of all, read individual source bytes */
  97. if ( count >= 8 )
  98. {
  99. count -= 8;
  100. {
  101. do
  102. {
  103. FT_Byte val;
  104. /* ensure that there are at least 8 bits in the accumulator */
  105. if ( loaded < 8 )
  106. {
  107. acc |= (FT_UShort)((FT_UShort)*source++ << ( 8 - loaded ));
  108. loaded += 8;
  109. }
  110. /* now write one byte */
  111. val = (FT_Byte)( acc >> 8 );
  112. if ( shift )
  113. {
  114. cur[0] |= (FT_Byte)( val >> shift );
  115. cur[1] |= (FT_Byte)( val << space );
  116. }
  117. else
  118. cur[0] |= val;
  119. cur++;
  120. acc <<= 8; /* remove bits from accumulator */
  121. loaded -= 8;
  122. count -= 8;
  123. } while ( count >= 0 );
  124. }
  125. /* restore `count' to correct value */
  126. count += 8;
  127. }
  128. /* now write remaining bits (count < 8) */
  129. if ( count > 0 )
  130. {
  131. FT_Byte val;
  132. /* ensure that there are at least `count' bits in the accumulator */
  133. if ( (FT_Int)loaded < count )
  134. {
  135. acc |= (FT_UShort)((FT_UShort)*source++ << ( 8 - loaded ));
  136. loaded += 8;
  137. }
  138. /* now write remaining bits */
  139. val = (FT_Byte)( ( (FT_Byte)( acc >> 8 ) ) & ~( 0xFF >> count ) );
  140. cur[0] |= (FT_Byte)( val >> shift );
  141. if ( count > space )
  142. cur[1] |= (FT_Byte)( val << space );
  143. acc <<= count;
  144. loaded -= count;
  145. }
  146. /* now, skip to next line */
  147. if ( byte_padded )
  148. {
  149. acc = 0;
  150. loaded = 0; /* clear accumulator on byte-padded lines */
  151. }
  152. line_buff += line_incr;
  153. }
  154. }
  155. const FT_Frame_Field sbit_metrics_fields[] =
  156. {
  157. #undef FT_STRUCTURE
  158. #define FT_STRUCTURE TT_SBit_MetricsRec
  159. FT_FRAME_START( 8 ),
  160. FT_FRAME_BYTE( height ),
  161. FT_FRAME_BYTE( width ),
  162. FT_FRAME_CHAR( horiBearingX ),
  163. FT_FRAME_CHAR( horiBearingY ),
  164. FT_FRAME_BYTE( horiAdvance ),
  165. FT_FRAME_CHAR( vertBearingX ),
  166. FT_FRAME_CHAR( vertBearingY ),
  167. FT_FRAME_BYTE( vertAdvance ),
  168. FT_FRAME_END
  169. };
  170. /*************************************************************************/
  171. /* */
  172. /* <Function> */
  173. /* TT_Load_SBit_Const_Metrics */
  174. /* */
  175. /* <Description> */
  176. /* Loads the metrics for `EBLC' index tables format 2 and 5. */
  177. /* */
  178. /* <Input> */
  179. /* range :: The target range. */
  180. /* */
  181. /* stream :: The input stream. */
  182. /* */
  183. /* <Return> */
  184. /* FreeType error code. 0 means success. */
  185. /* */
  186. static FT_Error
  187. Load_SBit_Const_Metrics( TT_SBit_Range range,
  188. FT_Stream stream )
  189. {
  190. FT_Error error;
  191. if ( FT_READ_ULONG( range->image_size ) )
  192. return error;
  193. return FT_STREAM_READ_FIELDS( sbit_metrics_fields, &range->metrics );
  194. }
  195. /*************************************************************************/
  196. /* */
  197. /* <Function> */
  198. /* TT_Load_SBit_Range_Codes */
  199. /* */
  200. /* <Description> */
  201. /* Loads the range codes for `EBLC' index tables format 4 and 5. */
  202. /* */
  203. /* <Input> */
  204. /* range :: The target range. */
  205. /* */
  206. /* stream :: The input stream. */
  207. /* */
  208. /* load_offsets :: A flag whether to load the glyph offset table. */
  209. /* */
  210. /* <Return> */
  211. /* FreeType error code. 0 means success. */
  212. /* */
  213. static FT_Error
  214. Load_SBit_Range_Codes( TT_SBit_Range range,
  215. FT_Stream stream,
  216. FT_Bool load_offsets )
  217. {
  218. FT_Error error;
  219. FT_ULong count, n, size;
  220. FT_Memory memory = stream->memory;
  221. if ( FT_READ_ULONG( count ) )
  222. goto Exit;
  223. range->num_glyphs = count;
  224. /* Allocate glyph offsets table if needed */
  225. if ( load_offsets )
  226. {
  227. if ( FT_NEW_ARRAY( range->glyph_offsets, count ) )
  228. goto Exit;
  229. size = count * 4L;
  230. }
  231. else
  232. size = count * 2L;
  233. /* Allocate glyph codes table and access frame */
  234. if ( FT_NEW_ARRAY ( range->glyph_codes, count ) ||
  235. FT_FRAME_ENTER( size ) )
  236. goto Exit;
  237. for ( n = 0; n < count; n++ )
  238. {
  239. range->glyph_codes[n] = FT_GET_USHORT();
  240. if ( load_offsets )
  241. range->glyph_offsets[n] = (FT_ULong)range->image_offset +
  242. FT_GET_USHORT();
  243. }
  244. FT_FRAME_EXIT();
  245. Exit:
  246. return error;
  247. }
  248. /*************************************************************************/
  249. /* */
  250. /* <Function> */
  251. /* TT_Load_SBit_Range */
  252. /* */
  253. /* <Description> */
  254. /* Loads a given `EBLC' index/range table. */
  255. /* */
  256. /* <Input> */
  257. /* range :: The target range. */
  258. /* */
  259. /* stream :: The input stream. */
  260. /* */
  261. /* <Return> */
  262. /* FreeType error code. 0 means success. */
  263. /* */
  264. static FT_Error
  265. Load_SBit_Range( TT_SBit_Range range,
  266. FT_Stream stream )
  267. {
  268. FT_Error error;
  269. FT_Memory memory = stream->memory;
  270. switch( range->index_format )
  271. {
  272. case 1: /* variable metrics with 4-byte offsets */
  273. case 3: /* variable metrics with 2-byte offsets */
  274. {
  275. FT_ULong num_glyphs, n;
  276. FT_Int size_elem;
  277. FT_Bool large = FT_BOOL( range->index_format == 1 );
  278. num_glyphs = range->last_glyph - range->first_glyph + 1L;
  279. range->num_glyphs = num_glyphs;
  280. num_glyphs++; /* XXX: BEWARE - see spec */
  281. size_elem = large ? 4 : 2;
  282. if ( FT_NEW_ARRAY( range->glyph_offsets, num_glyphs ) ||
  283. FT_FRAME_ENTER( num_glyphs * size_elem ) )
  284. goto Exit;
  285. for ( n = 0; n < num_glyphs; n++ )
  286. range->glyph_offsets[n] = (FT_ULong)( range->image_offset +
  287. ( large ? FT_GET_ULONG()
  288. : FT_GET_USHORT() ) );
  289. FT_FRAME_EXIT();
  290. }
  291. break;
  292. case 2: /* all glyphs have identical metrics */
  293. error = Load_SBit_Const_Metrics( range, stream );
  294. break;
  295. case 4:
  296. error = Load_SBit_Range_Codes( range, stream, 1 );
  297. break;
  298. case 5:
  299. error = Load_SBit_Const_Metrics( range, stream ) ||
  300. Load_SBit_Range_Codes( range, stream, 0 );
  301. break;
  302. default:
  303. error = SFNT_Err_Invalid_File_Format;
  304. }
  305. Exit:
  306. return error;
  307. }
  308. /*************************************************************************/
  309. /* */
  310. /* <Function> */
  311. /* tt_face_load_sbit_strikes */
  312. /* */
  313. /* <Description> */
  314. /* Loads the table of embedded bitmap sizes for this face. */
  315. /* */
  316. /* <Input> */
  317. /* face :: The target face object. */
  318. /* */
  319. /* stream :: The input stream. */
  320. /* */
  321. /* <Return> */
  322. /* FreeType error code. 0 means success. */
  323. /* */
  324. FT_LOCAL_DEF( FT_Error )
  325. tt_face_load_sbit_strikes( TT_Face face,
  326. FT_Stream stream )
  327. {
  328. FT_Error error = 0;
  329. FT_Memory memory = stream->memory;
  330. FT_Fixed version;
  331. FT_ULong num_strikes;
  332. FT_ULong table_base;
  333. const FT_Frame_Field sbit_line_metrics_fields[] =
  334. {
  335. #undef FT_STRUCTURE
  336. #define FT_STRUCTURE TT_SBit_LineMetricsRec
  337. /* no FT_FRAME_START */
  338. FT_FRAME_CHAR( ascender ),
  339. FT_FRAME_CHAR( descender ),
  340. FT_FRAME_BYTE( max_width ),
  341. FT_FRAME_CHAR( caret_slope_numerator ),
  342. FT_FRAME_CHAR( caret_slope_denominator ),
  343. FT_FRAME_CHAR( caret_offset ),
  344. FT_FRAME_CHAR( min_origin_SB ),
  345. FT_FRAME_CHAR( min_advance_SB ),
  346. FT_FRAME_CHAR( max_before_BL ),
  347. FT_FRAME_CHAR( min_after_BL ),
  348. FT_FRAME_CHAR( pads[0] ),
  349. FT_FRAME_CHAR( pads[1] ),
  350. FT_FRAME_END
  351. };
  352. const FT_Frame_Field strike_start_fields[] =
  353. {
  354. #undef FT_STRUCTURE
  355. #define FT_STRUCTURE TT_SBit_StrikeRec
  356. /* no FT_FRAME_START */
  357. FT_FRAME_ULONG( ranges_offset ),
  358. FT_FRAME_SKIP_LONG,
  359. FT_FRAME_ULONG( num_ranges ),
  360. FT_FRAME_ULONG( color_ref ),
  361. FT_FRAME_END
  362. };
  363. const FT_Frame_Field strike_end_fields[] =
  364. {
  365. /* no FT_FRAME_START */
  366. FT_FRAME_USHORT( start_glyph ),
  367. FT_FRAME_USHORT( end_glyph ),
  368. FT_FRAME_BYTE ( x_ppem ),
  369. FT_FRAME_BYTE ( y_ppem ),
  370. FT_FRAME_BYTE ( bit_depth ),
  371. FT_FRAME_CHAR ( flags ),
  372. FT_FRAME_END
  373. };
  374. face->num_sbit_strikes = 0;
  375. /* this table is optional */
  376. error = face->goto_table( face, TTAG_EBLC, stream, 0 );
  377. if ( error )
  378. error = face->goto_table( face, TTAG_bloc, stream, 0 );
  379. if ( error )
  380. goto Exit;
  381. table_base = FT_STREAM_POS();
  382. if ( FT_FRAME_ENTER( 8L ) )
  383. goto Exit;
  384. version = FT_GET_LONG();
  385. num_strikes = FT_GET_ULONG();
  386. FT_FRAME_EXIT();
  387. /* check version number and strike count */
  388. if ( version != 0x00020000L ||
  389. num_strikes >= 0x10000L )
  390. {
  391. FT_ERROR(( "tt_face_load_sbit_strikes: invalid table version!\n" ));
  392. error = SFNT_Err_Invalid_File_Format;
  393. goto Exit;
  394. }
  395. /* allocate the strikes table */
  396. if ( FT_NEW_ARRAY( face->sbit_strikes, num_strikes ) )
  397. goto Exit;
  398. face->num_sbit_strikes = num_strikes;
  399. /* now read each strike table separately */
  400. {
  401. TT_SBit_Strike strike = face->sbit_strikes;
  402. FT_ULong count = num_strikes;
  403. if ( FT_FRAME_ENTER( 48L * num_strikes ) )
  404. goto Exit;
  405. while ( count > 0 )
  406. {
  407. if ( FT_STREAM_READ_FIELDS( strike_start_fields, strike ) ||
  408. FT_STREAM_READ_FIELDS( sbit_line_metrics_fields, &strike->hori ) ||
  409. FT_STREAM_READ_FIELDS( sbit_line_metrics_fields, &strike->vert ) ||
  410. FT_STREAM_READ_FIELDS( strike_end_fields, strike ) )
  411. break;
  412. count--;
  413. strike++;
  414. }
  415. FT_FRAME_EXIT();
  416. }
  417. /* allocate the index ranges for each strike table */
  418. {
  419. TT_SBit_Strike strike = face->sbit_strikes;
  420. FT_ULong count = num_strikes;
  421. while ( count > 0 )
  422. {
  423. TT_SBit_Range range;
  424. FT_ULong count2 = strike->num_ranges;
  425. if ( FT_NEW_ARRAY( strike->sbit_ranges, strike->num_ranges ) )
  426. goto Exit;
  427. /* read each range */
  428. if ( FT_STREAM_SEEK( table_base + strike->ranges_offset ) ||
  429. FT_FRAME_ENTER( strike->num_ranges * 8L ) )
  430. goto Exit;
  431. range = strike->sbit_ranges;
  432. while ( count2 > 0 )
  433. {
  434. range->first_glyph = FT_GET_USHORT();
  435. range->last_glyph = FT_GET_USHORT();
  436. range->table_offset = table_base + strike->ranges_offset +
  437. FT_GET_ULONG();
  438. count2--;
  439. range++;
  440. }
  441. FT_FRAME_EXIT();
  442. /* Now, read each index table */
  443. count2 = strike->num_ranges;
  444. range = strike->sbit_ranges;
  445. while ( count2 > 0 )
  446. {
  447. /* Read the header */
  448. if ( FT_STREAM_SEEK( range->table_offset ) ||
  449. FT_FRAME_ENTER( 8L ) )
  450. goto Exit;
  451. range->index_format = FT_GET_USHORT();
  452. range->image_format = FT_GET_USHORT();
  453. range->image_offset = FT_GET_ULONG();
  454. FT_FRAME_EXIT();
  455. error = Load_SBit_Range( range, stream );
  456. if ( error )
  457. goto Exit;
  458. count2--;
  459. range++;
  460. }
  461. count--;
  462. strike++;
  463. }
  464. }
  465. Exit:
  466. return error;
  467. }
  468. /*************************************************************************/
  469. /* */
  470. /* <Function> */
  471. /* tt_face_free_sbit_strikes */
  472. /* */
  473. /* <Description> */
  474. /* Releases the embedded bitmap tables. */
  475. /* */
  476. /* <Input> */
  477. /* face :: The target face object. */
  478. /* */
  479. FT_LOCAL_DEF( void )
  480. tt_face_free_sbit_strikes( TT_Face face )
  481. {
  482. FT_Memory memory = face->root.memory;
  483. TT_SBit_Strike strike = face->sbit_strikes;
  484. TT_SBit_Strike strike_limit = strike + face->num_sbit_strikes;
  485. if ( strike )
  486. {
  487. for ( ; strike < strike_limit; strike++ )
  488. {
  489. TT_SBit_Range range = strike->sbit_ranges;
  490. TT_SBit_Range range_limit = range + strike->num_ranges;
  491. if ( range )
  492. {
  493. for ( ; range < range_limit; range++ )
  494. {
  495. /* release the glyph offsets and codes tables */
  496. /* where appropriate */
  497. FT_FREE( range->glyph_offsets );
  498. FT_FREE( range->glyph_codes );
  499. }
  500. }
  501. FT_FREE( strike->sbit_ranges );
  502. strike->num_ranges = 0;
  503. }
  504. FT_FREE( face->sbit_strikes );
  505. }
  506. face->num_sbit_strikes = 0;
  507. }
  508. FT_LOCAL_DEF( FT_Error )
  509. tt_face_set_sbit_strike( TT_Face face,
  510. FT_Int x_ppem,
  511. FT_Int y_ppem,
  512. FT_ULong *astrike_index )
  513. {
  514. FT_ULong i;
  515. if ( x_ppem < 0 || x_ppem > 255 ||
  516. y_ppem < 1 || y_ppem > 255 )
  517. return SFNT_Err_Invalid_PPem;
  518. for ( i = 0; i < face->num_sbit_strikes; i++ )
  519. {
  520. if ( ( face->sbit_strikes[i].y_ppem == y_ppem ) &&
  521. ( ( x_ppem == 0 ) ||
  522. ( face->sbit_strikes[i].x_ppem == x_ppem ) ) )
  523. {
  524. *astrike_index = i;
  525. return SFNT_Err_Ok;
  526. }
  527. }
  528. return SFNT_Err_Invalid_PPem;
  529. }
  530. /*************************************************************************/
  531. /* */
  532. /* <Function> */
  533. /* find_sbit_range */
  534. /* */
  535. /* <Description> */
  536. /* Scans a given strike's ranges and return, for a given glyph */
  537. /* index, the corresponding sbit range, and `EBDT' offset. */
  538. /* */
  539. /* <Input> */
  540. /* glyph_index :: The glyph index. */
  541. /* */
  542. /* strike :: The source/current sbit strike. */
  543. /* */
  544. /* <Output> */
  545. /* arange :: The sbit range containing the glyph index. */
  546. /* */
  547. /* aglyph_offset :: The offset of the glyph data in `EBDT' table. */
  548. /* */
  549. /* <Return> */
  550. /* FreeType error code. 0 means the glyph index was found. */
  551. /* */
  552. static FT_Error
  553. find_sbit_range( FT_UInt glyph_index,
  554. TT_SBit_Strike strike,
  555. TT_SBit_Range *arange,
  556. FT_ULong *aglyph_offset )
  557. {
  558. TT_SBit_RangeRec *range, *range_limit;
  559. /* check whether the glyph index is within this strike's */
  560. /* glyph range */
  561. if ( glyph_index < (FT_UInt)strike->start_glyph ||
  562. glyph_index > (FT_UInt)strike->end_glyph )
  563. goto Fail;
  564. /* scan all ranges in strike */
  565. range = strike->sbit_ranges;
  566. range_limit = range + strike->num_ranges;
  567. if ( !range )
  568. goto Fail;
  569. for ( ; range < range_limit; range++ )
  570. {
  571. if ( glyph_index >= (FT_UInt)range->first_glyph &&
  572. glyph_index <= (FT_UInt)range->last_glyph )
  573. {
  574. FT_UShort delta = (FT_UShort)( glyph_index - range->first_glyph );
  575. switch ( range->index_format )
  576. {
  577. case 1:
  578. case 3:
  579. *aglyph_offset = range->glyph_offsets[delta];
  580. break;
  581. case 2:
  582. *aglyph_offset = range->image_offset +
  583. range->image_size * delta;
  584. break;
  585. case 4:
  586. case 5:
  587. {
  588. FT_ULong n;
  589. for ( n = 0; n < range->num_glyphs; n++ )
  590. {
  591. if ( (FT_UInt)range->glyph_codes[n] == glyph_index )
  592. {
  593. if ( range->index_format == 4 )
  594. *aglyph_offset = range->glyph_offsets[n];
  595. else
  596. *aglyph_offset = range->image_offset +
  597. n * range->image_size;
  598. goto Found;
  599. }
  600. }
  601. }
  602. /* fall-through */
  603. default:
  604. goto Fail;
  605. }
  606. Found:
  607. /* return successfully! */
  608. *arange = range;
  609. return 0;
  610. }
  611. }
  612. Fail:
  613. *arange = 0;
  614. *aglyph_offset = 0;
  615. return SFNT_Err_Invalid_Argument;
  616. }
  617. /*************************************************************************/
  618. /* */
  619. /* <Function> */
  620. /* find_sbit_image */
  621. /* */
  622. /* <Description> */
  623. /* Checks whether an embedded bitmap (an `sbit') exists for a given */
  624. /* glyph, at a given strike. */
  625. /* */
  626. /* <Input> */
  627. /* face :: The target face object. */
  628. /* */
  629. /* glyph_index :: The glyph index. */
  630. /* */
  631. /* strike_index :: The current strike index. */
  632. /* */
  633. /* <Output> */
  634. /* arange :: The SBit range containing the glyph index. */
  635. /* */
  636. /* astrike :: The SBit strike containing the glyph index. */
  637. /* */
  638. /* aglyph_offset :: The offset of the glyph data in `EBDT' table. */
  639. /* */
  640. /* <Return> */
  641. /* FreeType error code. 0 means success. Returns */
  642. /* SFNT_Err_Invalid_Argument if no sbit exists for the requested */
  643. /* glyph. */
  644. /* */
  645. static FT_Error
  646. find_sbit_image( TT_Face face,
  647. FT_UInt glyph_index,
  648. FT_ULong strike_index,
  649. TT_SBit_Range *arange,
  650. TT_SBit_Strike *astrike,
  651. FT_ULong *aglyph_offset )
  652. {
  653. FT_Error error;
  654. TT_SBit_Strike strike;
  655. if ( !face->sbit_strikes ||
  656. ( face->num_sbit_strikes <= strike_index ) )
  657. goto Fail;
  658. strike = &face->sbit_strikes[strike_index];
  659. error = find_sbit_range( glyph_index, strike,
  660. arange, aglyph_offset );
  661. if ( error )
  662. goto Fail;
  663. *astrike = strike;
  664. return SFNT_Err_Ok;
  665. Fail:
  666. /* no embedded bitmap for this glyph in face */
  667. *arange = 0;
  668. *astrike = 0;
  669. *aglyph_offset = 0;
  670. return SFNT_Err_Invalid_Argument;
  671. }
  672. /*************************************************************************/
  673. /* */
  674. /* <Function> */
  675. /* load_sbit_metrics */
  676. /* */
  677. /* <Description> */
  678. /* Gets the big metrics for a given SBit. */
  679. /* */
  680. /* <Input> */
  681. /* stream :: The input stream. */
  682. /* */
  683. /* range :: The SBit range containing the glyph. */
  684. /* */
  685. /* <Output> */
  686. /* big_metrics :: A big SBit metrics structure for the glyph. */
  687. /* */
  688. /* <Return> */
  689. /* FreeType error code. 0 means success. */
  690. /* */
  691. /* <Note> */
  692. /* The stream cursor must be positioned at the glyph's offset within */
  693. /* the `EBDT' table before the call. */
  694. /* */
  695. /* If the image format uses variable metrics, the stream cursor is */
  696. /* positioned just after the metrics header in the `EBDT' table on */
  697. /* function exit. */
  698. /* */
  699. static FT_Error
  700. load_sbit_metrics( FT_Stream stream,
  701. TT_SBit_Range range,
  702. TT_SBit_Metrics metrics )
  703. {
  704. FT_Error error = SFNT_Err_Ok;
  705. switch ( range->image_format )
  706. {
  707. case 1:
  708. case 2:
  709. case 8:
  710. /* variable small metrics */
  711. {
  712. TT_SBit_SmallMetricsRec smetrics;
  713. const FT_Frame_Field sbit_small_metrics_fields[] =
  714. {
  715. #undef FT_STRUCTURE
  716. #define FT_STRUCTURE TT_SBit_SmallMetricsRec
  717. FT_FRAME_START( 5 ),
  718. FT_FRAME_BYTE( height ),
  719. FT_FRAME_BYTE( width ),
  720. FT_FRAME_CHAR( bearingX ),
  721. FT_FRAME_CHAR( bearingY ),
  722. FT_FRAME_BYTE( advance ),
  723. FT_FRAME_END
  724. };
  725. /* read small metrics */
  726. if ( FT_STREAM_READ_FIELDS( sbit_small_metrics_fields, &smetrics ) )
  727. goto Exit;
  728. /* convert it to a big metrics */
  729. metrics->height = smetrics.height;
  730. metrics->width = smetrics.width;
  731. metrics->horiBearingX = smetrics.bearingX;
  732. metrics->horiBearingY = smetrics.bearingY;
  733. metrics->horiAdvance = smetrics.advance;
  734. /* these metrics are made up at a higher level when */
  735. /* needed. */
  736. metrics->vertBearingX = 0;
  737. metrics->vertBearingY = 0;
  738. metrics->vertAdvance = 0;
  739. }
  740. break;
  741. case 6:
  742. case 7:
  743. case 9:
  744. /* variable big metrics */
  745. if ( FT_STREAM_READ_FIELDS( sbit_metrics_fields, metrics ) )
  746. goto Exit;
  747. break;
  748. case 5:
  749. default: /* constant metrics */
  750. if ( range->index_format == 2 || range->index_format == 5 )
  751. *metrics = range->metrics;
  752. else
  753. return SFNT_Err_Invalid_File_Format;
  754. }
  755. Exit:
  756. return error;
  757. }
  758. /*************************************************************************/
  759. /* */
  760. /* <Function> */
  761. /* crop_bitmap */
  762. /* */
  763. /* <Description> */
  764. /* Crops a bitmap to its tightest bounding box, and adjusts its */
  765. /* metrics. */
  766. /* */
  767. /* <InOut> */
  768. /* map :: The bitmap. */
  769. /* */
  770. /* metrics :: The corresponding metrics structure. */
  771. /* */
  772. static void
  773. crop_bitmap( FT_Bitmap* map,
  774. TT_SBit_Metrics metrics )
  775. {
  776. /***********************************************************************/
  777. /* */
  778. /* In this situation, some bounding boxes of embedded bitmaps are too */
  779. /* large. We need to crop it to a reasonable size. */
  780. /* */
  781. /* --------- */
  782. /* | | ----- */
  783. /* | *** | |***| */
  784. /* | * | | * | */
  785. /* | * | ------> | * | */
  786. /* | * | | * | */
  787. /* | * | | * | */
  788. /* | *** | |***| */
  789. /* --------- ----- */
  790. /* */
  791. /***********************************************************************/
  792. FT_Int rows, count;
  793. FT_Long line_len;
  794. FT_Byte* line;
  795. /***********************************************************************/
  796. /* */
  797. /* first of all, check the top-most lines of the bitmap, and remove */
  798. /* them if they're empty. */
  799. /* */
  800. {
  801. line = (FT_Byte*)map->buffer;
  802. rows = map->rows;
  803. line_len = map->pitch;
  804. for ( count = 0; count < rows; count++ )
  805. {
  806. FT_Byte* cur = line;
  807. FT_Byte* limit = line + line_len;
  808. for ( ; cur < limit; cur++ )
  809. if ( cur[0] )
  810. goto Found_Top;
  811. /* the current line was empty - skip to next one */
  812. line = limit;
  813. }
  814. Found_Top:
  815. /* check that we have at least one filled line */
  816. if ( count >= rows )
  817. goto Empty_Bitmap;
  818. /* now, crop the empty upper lines */
  819. if ( count > 0 )
  820. {
  821. line = (FT_Byte*)map->buffer;
  822. FT_MEM_MOVE( line, line + count * line_len,
  823. ( rows - count ) * line_len );
  824. metrics->height = (FT_Byte)( metrics->height - count );
  825. metrics->horiBearingY = (FT_Char)( metrics->horiBearingY - count );
  826. metrics->vertBearingY = (FT_Char)( metrics->vertBearingY - count );
  827. map->rows -= count;
  828. rows -= count;
  829. }
  830. }
  831. /***********************************************************************/
  832. /* */
  833. /* second, crop the lower lines */
  834. /* */
  835. {
  836. line = (FT_Byte*)map->buffer + ( rows - 1 ) * line_len;
  837. for ( count = 0; count < rows; count++ )
  838. {
  839. FT_Byte* cur = line;
  840. FT_Byte* limit = line + line_len;
  841. for ( ; cur < limit; cur++ )
  842. if ( cur[0] )
  843. goto Found_Bottom;
  844. /* the current line was empty - skip to previous one */
  845. line -= line_len;
  846. }
  847. Found_Bottom:
  848. if ( count > 0 )
  849. {
  850. metrics->height = (FT_Byte)( metrics->height - count );
  851. rows -= count;
  852. map->rows -= count;
  853. }
  854. }
  855. /***********************************************************************/
  856. /* */
  857. /* third, get rid of the space on the left side of the glyph */
  858. /* */
  859. do
  860. {
  861. FT_Byte* limit;
  862. line = (FT_Byte*)map->buffer;
  863. limit = line + rows * line_len;
  864. for ( ; line < limit; line += line_len )
  865. if ( line[0] & 0x80 )
  866. goto Found_Left;
  867. /* shift the whole glyph one pixel to the left */
  868. line = (FT_Byte*)map->buffer;
  869. limit = line + rows * line_len;
  870. for ( ; line < limit; line += line_len )
  871. {
  872. FT_Int n, width = map->width;
  873. FT_Byte old;
  874. FT_Byte* cur = line;
  875. old = (FT_Byte)(cur[0] << 1);
  876. for ( n = 8; n < width; n += 8 )
  877. {
  878. FT_Byte val;
  879. val = cur[1];
  880. cur[0] = (FT_Byte)( old | ( val >> 7 ) );
  881. old = (FT_Byte)( val << 1 );
  882. cur++;
  883. }
  884. cur[0] = old;
  885. }
  886. map->width--;
  887. metrics->horiBearingX++;
  888. metrics->vertBearingX++;
  889. metrics->width--;
  890. } while ( map->width > 0 );
  891. Found_Left:
  892. /***********************************************************************/
  893. /* */
  894. /* finally, crop the bitmap width to get rid of the space on the right */
  895. /* side of the glyph. */
  896. /* */
  897. do
  898. {
  899. FT_Int right = map->width - 1;
  900. FT_Byte* limit;
  901. FT_Byte mask;
  902. line = (FT_Byte*)map->buffer + ( right >> 3 );
  903. limit = line + rows * line_len;
  904. mask = (FT_Byte)( 0x80 >> ( right & 7 ) );
  905. for ( ; line < limit; line += line_len )
  906. if ( line[0] & mask )
  907. goto Found_Right;
  908. /* crop the whole glyph to the right */
  909. map->width--;
  910. metrics->width--;
  911. } while ( map->width > 0 );
  912. Found_Right:
  913. /* all right, the bitmap was cropped */
  914. return;
  915. Empty_Bitmap:
  916. map->width = 0;
  917. map->rows = 0;
  918. map->pitch = 0;
  919. map->pixel_mode = FT_PIXEL_MODE_MONO;
  920. }
  921. static FT_Error
  922. Load_SBit_Single( FT_Bitmap* map,
  923. FT_Int x_offset,
  924. FT_Int y_offset,
  925. FT_Int pix_bits,
  926. FT_UShort image_format,
  927. TT_SBit_Metrics metrics,
  928. FT_Stream stream )
  929. {
  930. FT_Error error;
  931. /* check that the source bitmap fits into the target pixmap */
  932. if ( x_offset < 0 || x_offset + metrics->width > map->width ||
  933. y_offset < 0 || y_offset + metrics->height > map->rows )
  934. {
  935. error = SFNT_Err_Invalid_Argument;
  936. goto Exit;
  937. }
  938. {
  939. FT_Int glyph_width = metrics->width;
  940. FT_Int glyph_height = metrics->height;
  941. FT_Int glyph_size;
  942. FT_Int line_bits = pix_bits * glyph_width;
  943. FT_Bool pad_bytes = 0;
  944. /* compute size of glyph image */
  945. switch ( image_format )
  946. {
  947. case 1: /* byte-padded formats */
  948. case 6:
  949. {
  950. FT_Int line_length;
  951. switch ( pix_bits )
  952. {
  953. case 1:
  954. line_length = ( glyph_width + 7 ) >> 3;
  955. break;
  956. case 2:
  957. line_length = ( glyph_width + 3 ) >> 2;
  958. break;
  959. case 4:
  960. line_length = ( glyph_width + 1 ) >> 1;
  961. break;
  962. default:
  963. line_length = glyph_width;
  964. }
  965. glyph_size = glyph_height * line_length;
  966. pad_bytes = 1;
  967. }
  968. break;
  969. case 2:
  970. case 5:
  971. case 7:
  972. line_bits = glyph_width * pix_bits;
  973. glyph_size = ( glyph_height * line_bits + 7 ) >> 3;
  974. break;
  975. default: /* invalid format */
  976. return SFNT_Err_Invalid_File_Format;
  977. }
  978. /* Now read data and draw glyph into target pixmap */
  979. if ( FT_FRAME_ENTER( glyph_size ) )
  980. goto Exit;
  981. /* don't forget to multiply `x_offset' by `map->pix_bits' as */
  982. /* the sbit blitter doesn't make a difference between pixmap */
  983. /* depths. */
  984. blit_sbit( map, (FT_Byte*)stream->cursor, line_bits, pad_bytes,
  985. x_offset * pix_bits, y_offset );
  986. FT_FRAME_EXIT();
  987. }
  988. Exit:
  989. return error;
  990. }
  991. static FT_Error
  992. Load_SBit_Image( TT_SBit_Strike strike,
  993. TT_SBit_Range range,
  994. FT_ULong ebdt_pos,
  995. FT_ULong glyph_offset,
  996. FT_Bitmap* map,
  997. FT_Int x_offset,
  998. FT_Int y_offset,
  999. FT_Stream stream,
  1000. TT_SBit_Metrics metrics )
  1001. {
  1002. FT_Memory memory = stream->memory;
  1003. FT_Error error;
  1004. /* place stream at beginning of glyph data and read metrics */
  1005. if ( FT_STREAM_SEEK( ebdt_pos + glyph_offset ) )
  1006. goto Exit;
  1007. error = load_sbit_metrics( stream, range, metrics );
  1008. if ( error )
  1009. goto Exit;
  1010. /* this function is recursive. At the top-level call, the */
  1011. /* field map.buffer is NULL. We thus begin by finding the */
  1012. /* dimensions of the higher-level glyph to allocate the */
  1013. /* final pixmap buffer */
  1014. if ( map->buffer == 0 )
  1015. {
  1016. FT_Long size;
  1017. map->width = metrics->width;
  1018. map->rows = metrics->height;
  1019. switch ( strike->bit_depth )
  1020. {
  1021. case 1:
  1022. map->pixel_mode = FT_PIXEL_MODE_MONO;
  1023. map->pitch = ( map->width + 7 ) >> 3;
  1024. break;
  1025. case 2:
  1026. map->pixel_mode = FT_PIXEL_MODE_GRAY2;
  1027. map->pitch = ( map->width + 3 ) >> 2;
  1028. break;
  1029. case 4:
  1030. map->pixel_mode = FT_PIXEL_MODE_GRAY4;
  1031. map->pitch = ( map->width + 1 ) >> 1;
  1032. break;
  1033. case 8:
  1034. map->pixel_mode = FT_PIXEL_MODE_GRAY;
  1035. map->pitch = map->width;
  1036. break;
  1037. default:
  1038. return SFNT_Err_Invalid_File_Format;
  1039. }
  1040. size = map->rows * map->pitch;
  1041. /* check that there is no empty image */
  1042. if ( size == 0 )
  1043. goto Exit; /* exit successfully! */
  1044. if ( FT_ALLOC( map->buffer, size ) )
  1045. goto Exit;
  1046. }
  1047. switch ( range->image_format )
  1048. {
  1049. case 1: /* single sbit image - load it */
  1050. case 2:
  1051. case 5:
  1052. case 6:
  1053. case 7:
  1054. return Load_SBit_Single( map, x_offset, y_offset, strike->bit_depth,
  1055. range->image_format, metrics, stream );
  1056. case 8: /* compound format */
  1057. FT_Stream_Skip( stream, 1L );
  1058. /* fallthrough */
  1059. case 9:
  1060. break;
  1061. default: /* invalid image format */
  1062. return SFNT_Err_Invalid_File_Format;
  1063. }
  1064. /* All right, we have a compound format. First of all, read */
  1065. /* the array of elements. */
  1066. {
  1067. TT_SBit_Component components;
  1068. TT_SBit_Component comp;
  1069. FT_UShort num_components, count;
  1070. if ( FT_READ_USHORT( num_components ) ||
  1071. FT_NEW_ARRAY( components, num_components ) )
  1072. goto Exit;
  1073. count = num_components;
  1074. if ( FT_FRAME_ENTER( 4L * num_components ) )
  1075. goto Fail_Memory;
  1076. for ( comp = components; count > 0; count--, comp++ )
  1077. {
  1078. comp->glyph_code = FT_GET_USHORT();
  1079. comp->x_offset = FT_GET_CHAR();
  1080. comp->y_offset = FT_GET_CHAR();
  1081. }
  1082. FT_FRAME_EXIT();
  1083. /* Now recursively load each element glyph */
  1084. count = num_components;
  1085. comp = components;
  1086. for ( ; count > 0; count--, comp++ )
  1087. {
  1088. TT_SBit_Range elem_range;
  1089. TT_SBit_MetricsRec elem_metrics;
  1090. FT_ULong elem_offset;
  1091. /* find the range for this element */
  1092. error = find_sbit_range( comp->glyph_code,
  1093. strike,
  1094. &elem_range,
  1095. &elem_offset );
  1096. if ( error )
  1097. goto Fail_Memory;
  1098. /* now load the element, recursively */
  1099. error = Load_SBit_Image( strike,
  1100. elem_range,
  1101. ebdt_pos,
  1102. elem_offset,
  1103. map,
  1104. x_offset + comp->x_offset,
  1105. y_offset + comp->y_offset,
  1106. stream,
  1107. &elem_metrics );
  1108. if ( error )
  1109. goto Fail_Memory;
  1110. }
  1111. Fail_Memory:
  1112. FT_FREE( components );
  1113. }
  1114. Exit:
  1115. return error;
  1116. }
  1117. /*************************************************************************/
  1118. /* */
  1119. /* <Function> */
  1120. /* tt_face_load_sbit_image */
  1121. /* */
  1122. /* <Description> */
  1123. /* Loads a given glyph sbit image from the font resource. This also */
  1124. /* returns its metrics. */
  1125. /* */
  1126. /* <Input> */
  1127. /* face :: The target face object. */
  1128. /* */
  1129. /* strike_index :: The current strike index. */
  1130. /* */
  1131. /* glyph_index :: The current glyph index. */
  1132. /* */
  1133. /* load_flags :: The glyph load flags (the code checks for the flag */
  1134. /* FT_LOAD_CROP_BITMAP). */
  1135. /* */
  1136. /* stream :: The input stream. */
  1137. /* */
  1138. /* <Output> */
  1139. /* map :: The target pixmap. */
  1140. /* */
  1141. /* metrics :: A big sbit metrics structure for the glyph image. */
  1142. /* */
  1143. /* <Return> */
  1144. /* FreeType error code. 0 means success. Returns an error if no */
  1145. /* glyph sbit exists for the index. */
  1146. /* */
  1147. /* <Note> */
  1148. /* The `map.buffer' field is always freed before the glyph is loaded. */
  1149. /* */
  1150. FT_LOCAL_DEF( FT_Error )
  1151. tt_face_load_sbit_image( TT_Face face,
  1152. FT_ULong strike_index,
  1153. FT_UInt glyph_index,
  1154. FT_UInt load_flags,
  1155. FT_Stream stream,
  1156. FT_Bitmap *map,
  1157. TT_SBit_MetricsRec *metrics )
  1158. {
  1159. FT_Error error;
  1160. FT_Memory memory = stream->memory;
  1161. FT_ULong ebdt_pos, glyph_offset;
  1162. TT_SBit_Strike strike;
  1163. TT_SBit_Range range;
  1164. /* Check whether there is a glyph sbit for the current index */
  1165. error = find_sbit_image( face, glyph_index, strike_index,
  1166. &range, &strike, &glyph_offset );
  1167. if ( error )
  1168. goto Exit;
  1169. /* now, find the location of the `EBDT' table in */
  1170. /* the font file */
  1171. error = face->goto_table( face, TTAG_EBDT, stream, 0 );
  1172. if ( error )
  1173. error = face->goto_table( face, TTAG_bdat, stream, 0 );
  1174. if (error)
  1175. goto Exit;
  1176. ebdt_pos = FT_STREAM_POS();
  1177. /* clear the bitmap & load the bitmap */
  1178. if ( face->root.glyph->flags & FT_GLYPH_OWN_BITMAP )
  1179. FT_FREE( map->buffer );
  1180. map->rows = map->pitch = map->width = 0;
  1181. error = Load_SBit_Image( strike, range, ebdt_pos, glyph_offset,
  1182. map, 0, 0, stream, metrics );
  1183. if ( error )
  1184. goto Exit;
  1185. /* the glyph slot owns this bitmap buffer */
  1186. face->root.glyph->flags |= FT_GLYPH_OWN_BITMAP;
  1187. /* setup vertical metrics if needed */
  1188. if ( strike->flags & 1 )
  1189. {
  1190. /* in case of a horizontal strike only */
  1191. FT_Int advance;
  1192. advance = strike->hori.ascender - strike->hori.descender;
  1193. /* some heuristic values */
  1194. metrics->vertBearingX = (FT_Char)(-metrics->width / 2 );
  1195. metrics->vertBearingY = (FT_Char)( advance / 10 );
  1196. metrics->vertAdvance = (FT_Char)( advance * 12 / 10 );
  1197. }
  1198. /* Crop the bitmap now, unless specified otherwise */
  1199. if ( load_flags & FT_LOAD_CROP_BITMAP )
  1200. crop_bitmap( map, metrics );
  1201. Exit:
  1202. return error;
  1203. }
  1204. /* END */