pfrobjs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /***************************************************************************/
  2. /* */
  3. /* pfrobjs.c */
  4. /* */
  5. /* FreeType PFR object methods (body). */
  6. /* */
  7. /* Copyright 2002 by */
  8. /* David Turner, Robert Wilhelm, and Werner Lemberg. */
  9. /* */
  10. /* This file is part of the FreeType project, and may only be used, */
  11. /* modified, and distributed under the terms of the FreeType project */
  12. /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
  13. /* this file you indicate that you have read the license and */
  14. /* understand and accept it fully. */
  15. /* */
  16. /***************************************************************************/
  17. #include "pfrobjs.h"
  18. #include "pfrload.h"
  19. #include "pfrgload.h"
  20. #include "pfrcmap.h"
  21. #include "pfrsbit.h"
  22. #include FT_OUTLINE_H
  23. #include FT_INTERNAL_DEBUG_H
  24. #include "pfrerror.h"
  25. #undef FT_COMPONENT
  26. #define FT_COMPONENT trace_pfr
  27. /*************************************************************************/
  28. /*************************************************************************/
  29. /***** *****/
  30. /***** FACE OBJECT METHODS *****/
  31. /***** *****/
  32. /*************************************************************************/
  33. /*************************************************************************/
  34. FT_LOCAL_DEF( void )
  35. pfr_face_done( PFR_Face face )
  36. {
  37. /* finalize the physical font record */
  38. pfr_phy_font_done( &face->phy_font, FT_FACE_MEMORY( face ) );
  39. /* no need to finalize the logical font or the header */
  40. }
  41. FT_LOCAL_DEF( FT_Error )
  42. pfr_face_init( FT_Stream stream,
  43. PFR_Face face,
  44. FT_Int face_index,
  45. FT_Int num_params,
  46. FT_Parameter* params )
  47. {
  48. FT_Error error;
  49. FT_UNUSED( num_params );
  50. FT_UNUSED( params );
  51. /* load the header and check it */
  52. error = pfr_header_load( &face->header, stream );
  53. if ( error )
  54. goto Exit;
  55. if ( !pfr_header_check( &face->header ) )
  56. {
  57. FT_TRACE4(( "pfr_face_init: not a valid PFR font\n" ));
  58. error = PFR_Err_Unknown_File_Format;
  59. goto Exit;
  60. }
  61. /* check face index */
  62. {
  63. FT_UInt num_faces;
  64. error = pfr_log_font_count( stream,
  65. face->header.log_dir_offset,
  66. &num_faces );
  67. if ( error )
  68. goto Exit;
  69. face->root.num_faces = num_faces;
  70. }
  71. if ( face_index < 0 )
  72. goto Exit;
  73. if ( face_index >= face->root.num_faces )
  74. {
  75. FT_ERROR(( "pfr_face_init: invalid face index\n" ));
  76. error = PFR_Err_Invalid_Argument;
  77. goto Exit;
  78. }
  79. /* load the face */
  80. error = pfr_log_font_load(
  81. &face->log_font, stream, face_index,
  82. face->header.log_dir_offset,
  83. FT_BOOL( face->header.phy_font_max_size_high != 0 ) );
  84. if ( error )
  85. goto Exit;
  86. /* now load the physical font descriptor */
  87. error = pfr_phy_font_load( &face->phy_font, stream,
  88. face->log_font.phys_offset,
  89. face->log_font.phys_size );
  90. if ( error )
  91. goto Exit;
  92. /* now, set-up all root face fields */
  93. {
  94. FT_Face root = FT_FACE( face );
  95. PFR_PhyFont phy_font = &face->phy_font;
  96. root->face_index = face_index;
  97. root->num_glyphs = phy_font->num_chars;
  98. root->face_flags = FT_FACE_FLAG_SCALABLE;
  99. if ( (phy_font->flags & PFR_PHY_PROPORTIONAL) == 0 )
  100. root->face_flags |= FT_FACE_FLAG_FIXED_WIDTH;
  101. if ( phy_font->flags & PFR_PHY_VERTICAL )
  102. root->face_flags |= FT_FACE_FLAG_VERTICAL;
  103. else
  104. root->face_flags |= FT_FACE_FLAG_HORIZONTAL;
  105. if ( phy_font->num_strikes > 0 )
  106. root->face_flags |= FT_FACE_FLAG_FIXED_SIZES;
  107. if ( phy_font->num_kern_pairs > 0 )
  108. root->face_flags |= FT_FACE_FLAG_KERNING;
  109. root->family_name = phy_font->font_id;
  110. root->style_name = NULL; /* no style name in font file */
  111. root->num_fixed_sizes = 0;
  112. root->available_sizes = 0;
  113. root->bbox = phy_font->bbox;
  114. root->units_per_EM = (FT_UShort)phy_font->outline_resolution;
  115. root->ascender = (FT_Short) phy_font->bbox.yMax;
  116. root->descender = (FT_Short) phy_font->bbox.yMin;
  117. root->height = (FT_Short)
  118. ( ( ( root->ascender - root->descender ) * 12 )
  119. / 10 );
  120. /* now compute maximum advance width */
  121. if ( ( phy_font->flags & PFR_PHY_PROPORTIONAL ) == 0 )
  122. root->max_advance_width = (FT_Short)phy_font->standard_advance;
  123. else
  124. {
  125. FT_Int max = 0;
  126. FT_UInt count = phy_font->num_chars;
  127. PFR_Char gchar = phy_font->chars;
  128. for ( ; count > 0; count--, gchar++ )
  129. {
  130. if ( max < gchar->advance )
  131. max = gchar->advance;
  132. }
  133. root->max_advance_width = (FT_Short)max;
  134. }
  135. root->max_advance_height = root->height;
  136. root->underline_position = (FT_Short)( - root->units_per_EM / 10 );
  137. root->underline_thickness = (FT_Short)( root->units_per_EM / 30 );
  138. /* create charmap */
  139. {
  140. FT_CharMapRec charmap;
  141. charmap.face = root;
  142. charmap.platform_id = 3;
  143. charmap.encoding_id = 1;
  144. charmap.encoding = FT_ENCODING_UNICODE;
  145. FT_CMap_New( &pfr_cmap_class_rec, NULL, &charmap, NULL );
  146. #if 0
  147. /* Select default charmap */
  148. if (root->num_charmaps)
  149. root->charmap = root->charmaps[0];
  150. #endif
  151. }
  152. /* check whether we've loaded any kerning pairs */
  153. if ( phy_font->num_kern_pairs )
  154. root->face_flags |= FT_FACE_FLAG_KERNING;
  155. }
  156. Exit:
  157. return error;
  158. }
  159. /*************************************************************************/
  160. /*************************************************************************/
  161. /***** *****/
  162. /***** SLOT OBJECT METHOD *****/
  163. /***** *****/
  164. /*************************************************************************/
  165. /*************************************************************************/
  166. FT_LOCAL_DEF( FT_Error )
  167. pfr_slot_init( PFR_Slot slot )
  168. {
  169. FT_GlyphLoader loader = slot->root.internal->loader;
  170. pfr_glyph_init( &slot->glyph, loader );
  171. return 0;
  172. }
  173. FT_LOCAL_DEF( void )
  174. pfr_slot_done( PFR_Slot slot )
  175. {
  176. pfr_glyph_done( &slot->glyph );
  177. }
  178. FT_LOCAL_DEF( FT_Error )
  179. pfr_slot_load( PFR_Slot slot,
  180. PFR_Size size,
  181. FT_UInt gindex,
  182. FT_Int32 load_flags )
  183. {
  184. FT_Error error;
  185. PFR_Face face = (PFR_Face)slot->root.face;
  186. PFR_Char gchar;
  187. FT_Outline* outline = &slot->root.outline;
  188. FT_ULong gps_offset;
  189. if (gindex > 0)
  190. gindex--;
  191. /* check that the glyph index is correct */
  192. FT_ASSERT( gindex < face->phy_font.num_chars );
  193. /* try to load an embedded bitmap */
  194. if ( ( load_flags & ( FT_LOAD_NO_SCALE | FT_LOAD_NO_BITMAP ) ) == 0 )
  195. {
  196. error = pfr_slot_load_bitmap( slot, size, gindex );
  197. if ( error == 0 )
  198. goto Exit;
  199. }
  200. gchar = face->phy_font.chars + gindex;
  201. slot->root.format = FT_GLYPH_FORMAT_OUTLINE;
  202. outline->n_points = 0;
  203. outline->n_contours = 0;
  204. gps_offset = face->header.gps_section_offset;
  205. /* load the glyph outline (FT_LOAD_NO_RECURSE isn't supported) */
  206. error = pfr_glyph_load( &slot->glyph, face->root.stream,
  207. gps_offset, gchar->gps_offset, gchar->gps_size );
  208. if ( !error )
  209. {
  210. FT_BBox cbox;
  211. FT_Glyph_Metrics* metrics = &slot->root.metrics;
  212. FT_Pos advance;
  213. FT_Int em_metrics, em_outline;
  214. FT_Bool scaling;
  215. scaling = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 );
  216. /* copy outline data */
  217. *outline = slot->glyph.loader->base.outline;
  218. outline->flags &= ~FT_OUTLINE_OWNER;
  219. outline->flags |= FT_OUTLINE_REVERSE_FILL;
  220. if ( size && size->root.metrics.y_ppem < 24 )
  221. outline->flags |= FT_OUTLINE_HIGH_PRECISION;
  222. /* compute the advance vector */
  223. metrics->horiAdvance = 0;
  224. metrics->vertAdvance = 0;
  225. advance = gchar->advance;
  226. em_metrics = face->phy_font.metrics_resolution;
  227. em_outline = face->phy_font.outline_resolution;
  228. if ( em_metrics != em_outline )
  229. advance = FT_MulDiv( advance, em_outline, em_metrics );
  230. if ( face->phy_font.flags & PFR_PHY_VERTICAL )
  231. metrics->vertAdvance = advance;
  232. else
  233. metrics->horiAdvance = advance;
  234. slot->root.linearHoriAdvance = metrics->horiAdvance;
  235. slot->root.linearVertAdvance = metrics->vertAdvance;
  236. /* make-up vertical metrics(?) */
  237. metrics->vertBearingX = 0;
  238. metrics->vertBearingY = 0;
  239. /* scale when needed */
  240. if ( scaling )
  241. {
  242. FT_Int n;
  243. FT_Fixed x_scale = size->root.metrics.x_scale;
  244. FT_Fixed y_scale = size->root.metrics.y_scale;
  245. FT_Vector* vec = outline->points;
  246. /* scale outline points */
  247. for ( n = 0; n < outline->n_points; n++, vec++ )
  248. {
  249. vec->x = FT_MulFix( vec->x, x_scale );
  250. vec->y = FT_MulFix( vec->y, y_scale );
  251. }
  252. /* scale the advance */
  253. metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale );
  254. metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale );
  255. }
  256. /* compute the rest of the metrics */
  257. FT_Outline_Get_CBox( outline, &cbox );
  258. metrics->width = cbox.xMax - cbox.xMin;
  259. metrics->height = cbox.yMax - cbox.yMin;
  260. metrics->horiBearingX = cbox.xMin;
  261. metrics->horiBearingY = cbox.yMax - metrics->height;
  262. }
  263. Exit:
  264. return error;
  265. }
  266. /*************************************************************************/
  267. /*************************************************************************/
  268. /***** *****/
  269. /***** KERNING METHOD *****/
  270. /***** *****/
  271. /*************************************************************************/
  272. /*************************************************************************/
  273. FT_LOCAL_DEF( FT_Error )
  274. pfr_face_get_kerning( PFR_Face face,
  275. FT_UInt glyph1,
  276. FT_UInt glyph2,
  277. FT_Vector* kerning )
  278. {
  279. FT_Error error;
  280. PFR_PhyFont phy_font = &face->phy_font;
  281. PFR_KernItem item = phy_font->kern_items;
  282. FT_UInt32 idx = PFR_KERN_INDEX( glyph1, glyph2 );
  283. kerning->x = 0;
  284. kerning->y = 0;
  285. /* find the kerning item containing our pair */
  286. while ( item )
  287. {
  288. if ( item->pair1 <= idx && idx <= item->pair2 )
  289. goto Found_Item;
  290. item = item->next;
  291. }
  292. /* not found */
  293. goto Exit;
  294. Found_Item:
  295. {
  296. /* perform simply binary search within the item */
  297. FT_UInt min, mid, max;
  298. FT_Stream stream = face->root.stream;
  299. FT_Byte* p;
  300. if ( FT_STREAM_SEEK( item->offset ) ||
  301. FT_FRAME_ENTER( item->pair_count * item->pair_size ) )
  302. goto Exit;
  303. min = 0;
  304. max = item->pair_count;
  305. while ( min < max )
  306. {
  307. FT_UInt char1, char2, charcode;
  308. mid = ( min + max ) >> 1;
  309. p = stream->cursor + mid*item->pair_size;
  310. if ( item->flags & PFR_KERN_2BYTE_CHAR )
  311. {
  312. char1 = FT_NEXT_USHORT( p );
  313. char2 = FT_NEXT_USHORT( p );
  314. }
  315. else
  316. {
  317. char1 = FT_NEXT_USHORT( p );
  318. char2 = FT_NEXT_USHORT( p );
  319. }
  320. charcode = PFR_KERN_INDEX( char1, char2 );
  321. if ( idx == charcode )
  322. {
  323. if ( item->flags & PFR_KERN_2BYTE_ADJ )
  324. kerning->x = item->base_adj + FT_NEXT_SHORT( p );
  325. else
  326. kerning->x = item->base_adj + FT_NEXT_CHAR( p );
  327. break;
  328. }
  329. if ( idx > charcode )
  330. min = mid + 1;
  331. else
  332. max = mid;
  333. }
  334. FT_FRAME_EXIT();
  335. }
  336. Exit:
  337. return 0;
  338. }
  339. /* END */