ftobjs.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505
  1. /***************************************************************************/
  2. /* */
  3. /* ftobjs.c */
  4. /* */
  5. /* The FreeType private base classes (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_LIST_H
  19. #include FT_OUTLINE_H
  20. #include FT_INTERNAL_OBJECTS_H
  21. #include FT_INTERNAL_DEBUG_H
  22. #include FT_INTERNAL_STREAM_H
  23. #include FT_TRUETYPE_TABLES_H
  24. #include FT_OUTLINE_H
  25. FT_BASE_DEF( void )
  26. ft_validator_init( FT_Validator valid,
  27. const FT_Byte* base,
  28. const FT_Byte* limit,
  29. FT_ValidationLevel level )
  30. {
  31. valid->base = base;
  32. valid->limit = limit;
  33. valid->level = level;
  34. valid->error = 0;
  35. }
  36. FT_BASE_DEF( FT_Int )
  37. ft_validator_run( FT_Validator valid )
  38. {
  39. int result;
  40. result = ft_setjmp( valid->jump_buffer );
  41. return result;
  42. }
  43. FT_BASE_DEF( void )
  44. ft_validator_error( FT_Validator valid,
  45. FT_Error error )
  46. {
  47. valid->error = error;
  48. ft_longjmp( valid->jump_buffer, 1 );
  49. }
  50. /*************************************************************************/
  51. /*************************************************************************/
  52. /*************************************************************************/
  53. /**** ****/
  54. /**** ****/
  55. /**** S T R E A M ****/
  56. /**** ****/
  57. /**** ****/
  58. /*************************************************************************/
  59. /*************************************************************************/
  60. /*************************************************************************/
  61. /* create a new input stream from a FT_Open_Args structure */
  62. /* */
  63. static FT_Error
  64. ft_input_stream_new( FT_Library library,
  65. const FT_Open_Args* args,
  66. FT_Stream* astream )
  67. {
  68. FT_Error error;
  69. FT_Memory memory;
  70. FT_Stream stream;
  71. if ( !library )
  72. return FT_Err_Invalid_Library_Handle;
  73. if ( !args )
  74. return FT_Err_Invalid_Argument;
  75. *astream = 0;
  76. memory = library->memory;
  77. if ( FT_NEW( stream ) )
  78. goto Exit;
  79. stream->memory = memory;
  80. if ( args->flags & FT_OPEN_MEMORY )
  81. {
  82. /* create a memory-based stream */
  83. FT_Stream_OpenMemory( stream,
  84. (const FT_Byte*)args->memory_base,
  85. args->memory_size );
  86. }
  87. else if ( args->flags & FT_OPEN_PATHNAME )
  88. {
  89. /* create a normal system stream */
  90. error = FT_Stream_Open( stream, args->pathname );
  91. stream->pathname.pointer = args->pathname;
  92. }
  93. else if ( ( args->flags & FT_OPEN_STREAM ) && args->stream )
  94. {
  95. /* use an existing, user-provided stream */
  96. /* in this case, we do not need to allocate a new stream object */
  97. /* since the caller is responsible for closing it himself */
  98. FT_FREE( stream );
  99. stream = args->stream;
  100. }
  101. else
  102. error = FT_Err_Invalid_Argument;
  103. if ( error )
  104. FT_FREE( stream );
  105. else
  106. stream->memory = memory; /* just to be certain */
  107. *astream = stream;
  108. Exit:
  109. return error;
  110. }
  111. static void
  112. ft_input_stream_free( FT_Stream stream,
  113. FT_Int external )
  114. {
  115. if ( stream )
  116. {
  117. FT_Memory memory = stream->memory;
  118. FT_Stream_Close( stream );
  119. if ( !external )
  120. FT_FREE( stream );
  121. }
  122. }
  123. #undef FT_COMPONENT
  124. #define FT_COMPONENT trace_objs
  125. /*************************************************************************/
  126. /*************************************************************************/
  127. /*************************************************************************/
  128. /**** ****/
  129. /**** ****/
  130. /**** FACE, SIZE & GLYPH SLOT OBJECTS ****/
  131. /**** ****/
  132. /**** ****/
  133. /*************************************************************************/
  134. /*************************************************************************/
  135. /*************************************************************************/
  136. static FT_Error
  137. ft_glyphslot_init( FT_GlyphSlot slot )
  138. {
  139. FT_Driver driver = slot->face->driver;
  140. FT_Driver_Class clazz = driver->clazz;
  141. FT_Memory memory = driver->root.memory;
  142. FT_Error error = FT_Err_Ok;
  143. FT_Slot_Internal internal;
  144. slot->library = driver->root.library;
  145. if ( FT_NEW( internal ) )
  146. goto Exit;
  147. slot->internal = internal;
  148. if ( FT_DRIVER_USES_OUTLINES( driver ) )
  149. error = FT_GlyphLoader_New( memory, &internal->loader );
  150. if ( !error && clazz->init_slot )
  151. error = clazz->init_slot( slot );
  152. Exit:
  153. return error;
  154. }
  155. static void
  156. ft_glyphslot_clear( FT_GlyphSlot slot )
  157. {
  158. /* free bitmap if needed */
  159. if ( slot->flags & FT_GLYPH_OWN_BITMAP )
  160. {
  161. FT_Memory memory = FT_FACE_MEMORY( slot->face );
  162. FT_FREE( slot->bitmap.buffer );
  163. slot->flags &= ~FT_GLYPH_OWN_BITMAP;
  164. }
  165. /* clear all public fields in the glyph slot */
  166. FT_ZERO( &slot->metrics );
  167. FT_ZERO( &slot->outline );
  168. slot->bitmap.width = 0;
  169. slot->bitmap.rows = 0;
  170. slot->bitmap.pitch = 0;
  171. slot->bitmap.pixel_mode = 0;
  172. /* don't touch 'slot->bitmap.buffer' !! */
  173. slot->bitmap_left = 0;
  174. slot->bitmap_top = 0;
  175. slot->num_subglyphs = 0;
  176. slot->subglyphs = 0;
  177. slot->control_data = 0;
  178. slot->control_len = 0;
  179. slot->other = 0;
  180. slot->format = FT_GLYPH_FORMAT_NONE;
  181. slot->linearHoriAdvance = 0;
  182. slot->linearVertAdvance = 0;
  183. }
  184. static void
  185. ft_glyphslot_done( FT_GlyphSlot slot )
  186. {
  187. FT_Driver driver = slot->face->driver;
  188. FT_Driver_Class clazz = driver->clazz;
  189. FT_Memory memory = driver->root.memory;
  190. if ( clazz->done_slot )
  191. clazz->done_slot( slot );
  192. /* free bitmap buffer if needed */
  193. if ( slot->flags & FT_GLYPH_OWN_BITMAP )
  194. FT_FREE( slot->bitmap.buffer );
  195. /* free glyph loader */
  196. if ( FT_DRIVER_USES_OUTLINES( driver ) )
  197. {
  198. FT_GlyphLoader_Done( slot->internal->loader );
  199. slot->internal->loader = 0;
  200. }
  201. FT_FREE( slot->internal );
  202. }
  203. /* documentation is in ftobjs.h */
  204. FT_BASE_DEF( FT_Error )
  205. FT_New_GlyphSlot( FT_Face face,
  206. FT_GlyphSlot *aslot )
  207. {
  208. FT_Error error;
  209. FT_Driver driver;
  210. FT_Driver_Class clazz;
  211. FT_Memory memory;
  212. FT_GlyphSlot slot;
  213. if ( !face || !aslot || !face->driver )
  214. return FT_Err_Invalid_Argument;
  215. *aslot = 0;
  216. driver = face->driver;
  217. clazz = driver->clazz;
  218. memory = driver->root.memory;
  219. FT_TRACE4(( "FT_New_GlyphSlot: Creating new slot object\n" ));
  220. if ( !FT_ALLOC( slot, clazz->slot_object_size ) )
  221. {
  222. slot->face = face;
  223. error = ft_glyphslot_init( slot );
  224. if ( error )
  225. {
  226. ft_glyphslot_done( slot );
  227. FT_FREE( slot );
  228. goto Exit;
  229. }
  230. *aslot = slot;
  231. }
  232. Exit:
  233. FT_TRACE4(( "FT_New_GlyphSlot: Return %d\n", error ));
  234. return error;
  235. }
  236. /* documentation is in ftobjs.h */
  237. FT_BASE_DEF( void )
  238. FT_Done_GlyphSlot( FT_GlyphSlot slot )
  239. {
  240. if ( slot )
  241. {
  242. FT_Driver driver = slot->face->driver;
  243. FT_Memory memory = driver->root.memory;
  244. FT_GlyphSlot* parent;
  245. FT_GlyphSlot cur;
  246. /* Remove slot from its parent face's list */
  247. parent = &slot->face->glyph;
  248. cur = *parent;
  249. while ( cur )
  250. {
  251. if ( cur == slot )
  252. {
  253. *parent = cur->next;
  254. ft_glyphslot_done( slot );
  255. FT_FREE( slot );
  256. break;
  257. }
  258. cur = cur->next;
  259. }
  260. }
  261. }
  262. /* documentation is in freetype.h */
  263. FT_EXPORT_DEF( void )
  264. FT_Set_Transform( FT_Face face,
  265. FT_Matrix* matrix,
  266. FT_Vector* delta )
  267. {
  268. FT_Face_Internal internal;
  269. if ( !face )
  270. return;
  271. internal = face->internal;
  272. internal->transform_flags = 0;
  273. if ( !matrix )
  274. {
  275. internal->transform_matrix.xx = 0x10000L;
  276. internal->transform_matrix.xy = 0;
  277. internal->transform_matrix.yx = 0;
  278. internal->transform_matrix.yy = 0x10000L;
  279. matrix = &internal->transform_matrix;
  280. }
  281. else
  282. internal->transform_matrix = *matrix;
  283. /* set transform_flags bit flag 0 if `matrix' isn't the identity */
  284. if ( ( matrix->xy | matrix->yx ) ||
  285. matrix->xx != 0x10000L ||
  286. matrix->yy != 0x10000L )
  287. internal->transform_flags |= 1;
  288. if ( !delta )
  289. {
  290. internal->transform_delta.x = 0;
  291. internal->transform_delta.y = 0;
  292. delta = &internal->transform_delta;
  293. }
  294. else
  295. internal->transform_delta = *delta;
  296. /* set transform_flags bit flag 1 if `delta' isn't the null vector */
  297. if ( delta->x | delta->y )
  298. internal->transform_flags |= 2;
  299. }
  300. /* documentation is in freetype.h */
  301. FT_EXPORT_DEF( void )
  302. FT_Set_Hint_Flags( FT_Face face,
  303. FT_ULong flags )
  304. {
  305. FT_Face_Internal internal;
  306. if ( !face )
  307. return;
  308. internal = face->internal;
  309. internal->hint_flags = (FT_UInt)flags;
  310. }
  311. static FT_Renderer
  312. ft_lookup_glyph_renderer( FT_GlyphSlot slot );
  313. /* documentation is in freetype.h */
  314. FT_EXPORT_DEF( FT_Error )
  315. FT_Load_Glyph( FT_Face face,
  316. FT_UInt glyph_index,
  317. FT_Int32 load_flags )
  318. {
  319. FT_Error error;
  320. FT_Driver driver;
  321. FT_GlyphSlot slot;
  322. FT_Library library;
  323. FT_Bool autohint;
  324. FT_Module hinter;
  325. if ( !face || !face->size || !face->glyph )
  326. return FT_Err_Invalid_Face_Handle;
  327. if ( glyph_index > (FT_UInt)face->num_glyphs )
  328. return FT_Err_Invalid_Argument;
  329. slot = face->glyph;
  330. ft_glyphslot_clear( slot );
  331. driver = face->driver;
  332. /* if the flag NO_RECURSE is set, we disable hinting and scaling */
  333. if ( load_flags & FT_LOAD_NO_RECURSE )
  334. {
  335. /* disable scaling, hinting, and transformation */
  336. load_flags |= FT_LOAD_NO_SCALE |
  337. FT_LOAD_NO_HINTING |
  338. FT_LOAD_NO_BITMAP |
  339. FT_LOAD_IGNORE_TRANSFORM;
  340. /* disable bitmap rendering */
  341. load_flags &= ~FT_LOAD_RENDER;
  342. }
  343. /* do we need to load the glyph through the auto-hinter? */
  344. library = driver->root.library;
  345. hinter = library->auto_hinter;
  346. autohint =
  347. FT_BOOL( hinter &&
  348. !( load_flags & ( FT_LOAD_NO_SCALE |
  349. FT_LOAD_NO_HINTING |
  350. FT_LOAD_NO_AUTOHINT ) ) &&
  351. FT_DRIVER_IS_SCALABLE( driver ) &&
  352. FT_DRIVER_USES_OUTLINES( driver ) );
  353. if ( autohint )
  354. {
  355. if ( FT_DRIVER_HAS_HINTER( driver ) &&
  356. !( load_flags & FT_LOAD_FORCE_AUTOHINT ) )
  357. autohint = 0;
  358. }
  359. if ( autohint )
  360. {
  361. FT_AutoHinter_Service hinting;
  362. /* try to load embedded bitmaps first if available */
  363. /* */
  364. /* XXX: This is really a temporary hack that should disappear */
  365. /* promptly with FreeType 2.1! */
  366. /* */
  367. if ( FT_HAS_FIXED_SIZES( face ) &&
  368. ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
  369. {
  370. error = driver->clazz->load_glyph( slot, face->size,
  371. glyph_index,
  372. load_flags | FT_LOAD_SBITS_ONLY );
  373. if ( !error && slot->format == FT_GLYPH_FORMAT_BITMAP )
  374. goto Load_Ok;
  375. }
  376. /* load auto-hinted outline */
  377. hinting = (FT_AutoHinter_Service)hinter->clazz->module_interface;
  378. error = hinting->load_glyph( (FT_AutoHinter)hinter,
  379. slot, face->size,
  380. glyph_index, load_flags );
  381. }
  382. else
  383. {
  384. error = driver->clazz->load_glyph( slot,
  385. face->size,
  386. glyph_index,
  387. load_flags );
  388. if ( error )
  389. goto Exit;
  390. /* check that the loaded outline is correct */
  391. error = FT_Outline_Check( &slot->outline );
  392. if ( error )
  393. goto Exit;
  394. }
  395. Load_Ok:
  396. /* compute the advance */
  397. if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
  398. {
  399. slot->advance.x = 0;
  400. slot->advance.y = slot->metrics.vertAdvance;
  401. }
  402. else
  403. {
  404. slot->advance.x = slot->metrics.horiAdvance;
  405. slot->advance.y = 0;
  406. }
  407. /* compute the linear advance in 16.16 pixels */
  408. if ( ( load_flags & FT_LOAD_LINEAR_DESIGN ) == 0 )
  409. {
  410. FT_UInt EM = face->units_per_EM;
  411. FT_Size_Metrics* metrics = &face->size->metrics;
  412. slot->linearHoriAdvance = FT_MulDiv( slot->linearHoriAdvance,
  413. (FT_Long)metrics->x_ppem << 16, EM );
  414. slot->linearVertAdvance = FT_MulDiv( slot->linearVertAdvance,
  415. (FT_Long)metrics->y_ppem << 16, EM );
  416. }
  417. if ( ( load_flags & FT_LOAD_IGNORE_TRANSFORM ) == 0 )
  418. {
  419. FT_Face_Internal internal = face->internal;
  420. /* now, transform the glyph image if needed */
  421. if ( internal->transform_flags )
  422. {
  423. /* get renderer */
  424. FT_Renderer renderer = ft_lookup_glyph_renderer( slot );
  425. if ( renderer )
  426. error = renderer->clazz->transform_glyph(
  427. renderer, slot,
  428. &internal->transform_matrix,
  429. &internal->transform_delta );
  430. /* transform advance */
  431. FT_Vector_Transform( &slot->advance, &internal->transform_matrix );
  432. }
  433. }
  434. /* do we need to render the image now? */
  435. if ( !error &&
  436. slot->format != FT_GLYPH_FORMAT_BITMAP &&
  437. slot->format != FT_GLYPH_FORMAT_COMPOSITE &&
  438. load_flags & FT_LOAD_RENDER )
  439. {
  440. FT_Render_Mode mode = FT_LOAD_TARGET_MODE( load_flags );
  441. if ( mode == FT_RENDER_MODE_NORMAL &&
  442. (load_flags & FT_LOAD_MONOCHROME ) )
  443. mode = FT_RENDER_MODE_MONO;
  444. error = FT_Render_Glyph( slot, mode );
  445. }
  446. Exit:
  447. return error;
  448. }
  449. /* documentation is in freetype.h */
  450. FT_EXPORT_DEF( FT_Error )
  451. FT_Load_Char( FT_Face face,
  452. FT_ULong char_code,
  453. FT_Int32 load_flags )
  454. {
  455. FT_UInt glyph_index;
  456. if ( !face )
  457. return FT_Err_Invalid_Face_Handle;
  458. glyph_index = (FT_UInt)char_code;
  459. if ( face->charmap )
  460. glyph_index = FT_Get_Char_Index( face, char_code );
  461. return FT_Load_Glyph( face, glyph_index, load_flags );
  462. }
  463. /* destructor for sizes list */
  464. static void
  465. destroy_size( FT_Memory memory,
  466. FT_Size size,
  467. FT_Driver driver )
  468. {
  469. /* finalize client-specific data */
  470. if ( size->generic.finalizer )
  471. size->generic.finalizer( size );
  472. /* finalize format-specific stuff */
  473. if ( driver->clazz->done_size )
  474. driver->clazz->done_size( size );
  475. FT_FREE( size->internal );
  476. FT_FREE( size );
  477. }
  478. /* destructor for faces list */
  479. static void
  480. destroy_face( FT_Memory memory,
  481. FT_Face face,
  482. FT_Driver driver )
  483. {
  484. FT_Driver_Class clazz = driver->clazz;
  485. /* discard auto-hinting data */
  486. if ( face->autohint.finalizer )
  487. face->autohint.finalizer( face->autohint.data );
  488. /* Discard glyph slots for this face. */
  489. /* Beware! FT_Done_GlyphSlot() changes the field `face->glyph' */
  490. while ( face->glyph )
  491. FT_Done_GlyphSlot( face->glyph );
  492. /* discard all sizes for this face */
  493. FT_List_Finalize( &face->sizes_list,
  494. (FT_List_Destructor)destroy_size,
  495. memory,
  496. driver );
  497. face->size = 0;
  498. /* now discard client data */
  499. if ( face->generic.finalizer )
  500. face->generic.finalizer( face );
  501. /* discard charmaps */
  502. {
  503. FT_Int n;
  504. for ( n = 0; n < face->num_charmaps; n++ )
  505. {
  506. FT_CMap cmap = FT_CMAP( face->charmaps[n] );
  507. FT_CMap_Done( cmap );
  508. face->charmaps[n] = NULL;
  509. }
  510. FT_FREE( face->charmaps );
  511. face->num_charmaps = 0;
  512. }
  513. /* finalize format-specific stuff */
  514. if ( clazz->done_face )
  515. clazz->done_face( face );
  516. /* close the stream for this face if needed */
  517. ft_input_stream_free(
  518. face->stream,
  519. ( face->face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0 );
  520. face->stream = 0;
  521. /* get rid of it */
  522. if ( face->internal )
  523. {
  524. FT_FREE( face->internal->postscript_name );
  525. FT_FREE( face->internal );
  526. }
  527. FT_FREE( face );
  528. }
  529. static void
  530. Destroy_Driver( FT_Driver driver )
  531. {
  532. FT_List_Finalize( &driver->faces_list,
  533. (FT_List_Destructor)destroy_face,
  534. driver->root.memory,
  535. driver );
  536. /* check whether we need to drop the driver's glyph loader */
  537. if ( FT_DRIVER_USES_OUTLINES( driver ) )
  538. FT_GlyphLoader_Done( driver->glyph_loader );
  539. }
  540. /*************************************************************************/
  541. /* */
  542. /* <Function> */
  543. /* open_face */
  544. /* */
  545. /* <Description> */
  546. /* This function does some work for FT_Open_Face(). */
  547. /* */
  548. static FT_Error
  549. open_face( FT_Driver driver,
  550. FT_Stream stream,
  551. FT_Long face_index,
  552. FT_Int num_params,
  553. FT_Parameter* params,
  554. FT_Face* aface )
  555. {
  556. FT_Memory memory;
  557. FT_Driver_Class clazz;
  558. FT_Face face = 0;
  559. FT_Error error;
  560. FT_Face_Internal internal;
  561. clazz = driver->clazz;
  562. memory = driver->root.memory;
  563. /* allocate the face object and perform basic initialization */
  564. if ( FT_ALLOC( face, clazz->face_object_size ) )
  565. goto Fail;
  566. if ( FT_NEW( internal ) )
  567. goto Fail;
  568. face->internal = internal;
  569. face->driver = driver;
  570. face->memory = memory;
  571. face->stream = stream;
  572. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  573. {
  574. int i;
  575. face->internal->incremental_interface = 0;
  576. for ( i = 0; i < num_params && !face->internal->incremental_interface;
  577. i++ )
  578. if ( params[i].tag == FT_PARAM_TAG_INCREMENTAL )
  579. face->internal->incremental_interface = params[i].data;
  580. }
  581. #endif
  582. error = clazz->init_face( stream,
  583. face,
  584. (FT_Int)face_index,
  585. num_params,
  586. params );
  587. if ( error )
  588. goto Fail;
  589. /* select Unicode charmap by default */
  590. {
  591. FT_Int nn;
  592. FT_CharMap unicmap = NULL, cmap;
  593. for ( nn = 0; nn < face->num_charmaps; nn++ )
  594. {
  595. cmap = face->charmaps[nn];
  596. if ( cmap->encoding == FT_ENCODING_UNICODE )
  597. {
  598. unicmap = cmap;
  599. break;
  600. }
  601. }
  602. if ( unicmap != NULL )
  603. face->charmap = unicmap;
  604. }
  605. *aface = face;
  606. Fail:
  607. if ( error )
  608. {
  609. clazz->done_face( face );
  610. FT_FREE( face->internal );
  611. FT_FREE( face );
  612. *aface = 0;
  613. }
  614. return error;
  615. }
  616. /* there's a Mac-specific extended implementation of FT_New_Face() */
  617. /* in src/base/ftmac.c */
  618. #ifndef FT_MACINTOSH
  619. /* documentation is in freetype.h */
  620. FT_EXPORT_DEF( FT_Error )
  621. FT_New_Face( FT_Library library,
  622. const char* pathname,
  623. FT_Long face_index,
  624. FT_Face *aface )
  625. {
  626. FT_Open_Args args;
  627. /* test for valid `library' and `aface' delayed to FT_Open_Face() */
  628. if ( !pathname )
  629. return FT_Err_Invalid_Argument;
  630. args.flags = FT_OPEN_PATHNAME;
  631. args.pathname = (char*)pathname;
  632. return FT_Open_Face( library, &args, face_index, aface );
  633. }
  634. #endif /* !FT_MACINTOSH */
  635. /* documentation is in freetype.h */
  636. FT_EXPORT_DEF( FT_Error )
  637. FT_New_Memory_Face( FT_Library library,
  638. const FT_Byte* file_base,
  639. FT_Long file_size,
  640. FT_Long face_index,
  641. FT_Face *aface )
  642. {
  643. FT_Open_Args args;
  644. /* test for valid `library' and `face' delayed to FT_Open_Face() */
  645. if ( !file_base )
  646. return FT_Err_Invalid_Argument;
  647. args.flags = FT_OPEN_MEMORY;
  648. args.memory_base = file_base;
  649. args.memory_size = file_size;
  650. return FT_Open_Face( library, &args, face_index, aface );
  651. }
  652. /* documentation is in freetype.h */
  653. FT_EXPORT_DEF( FT_Error )
  654. FT_Open_Face( FT_Library library,
  655. const FT_Open_Args* args,
  656. FT_Long face_index,
  657. FT_Face *aface )
  658. {
  659. FT_Error error;
  660. FT_Driver driver;
  661. FT_Memory memory;
  662. FT_Stream stream;
  663. FT_Face face = 0;
  664. FT_ListNode node = 0;
  665. FT_Bool external_stream;
  666. /* test for valid `library' delayed to */
  667. /* ft_input_stream_new() */
  668. if ( !aface || !args )
  669. return FT_Err_Invalid_Argument;
  670. *aface = 0;
  671. external_stream = FT_BOOL( ( args->flags & FT_OPEN_STREAM ) &&
  672. args->stream );
  673. /* create input stream */
  674. error = ft_input_stream_new( library, args, &stream );
  675. if ( error )
  676. goto Exit;
  677. memory = library->memory;
  678. /* If the font driver is specified in the `args' structure, use */
  679. /* it. Otherwise, we scan the list of registered drivers. */
  680. if ( ( args->flags & FT_OPEN_DRIVER ) && args->driver )
  681. {
  682. driver = FT_DRIVER( args->driver );
  683. /* not all modules are drivers, so check... */
  684. if ( FT_MODULE_IS_DRIVER( driver ) )
  685. {
  686. FT_Int num_params = 0;
  687. FT_Parameter* params = 0;
  688. if ( args->flags & FT_OPEN_PARAMS )
  689. {
  690. num_params = args->num_params;
  691. params = args->params;
  692. }
  693. error = open_face( driver, stream, face_index,
  694. num_params, params, &face );
  695. if ( !error )
  696. goto Success;
  697. }
  698. else
  699. error = FT_Err_Invalid_Handle;
  700. ft_input_stream_free( stream, external_stream );
  701. goto Fail;
  702. }
  703. else
  704. {
  705. /* check each font driver for an appropriate format */
  706. FT_Module* cur = library->modules;
  707. FT_Module* limit = cur + library->num_modules;
  708. for ( ; cur < limit; cur++ )
  709. {
  710. /* not all modules are font drivers, so check... */
  711. if ( FT_MODULE_IS_DRIVER( cur[0] ) )
  712. {
  713. FT_Int num_params = 0;
  714. FT_Parameter* params = 0;
  715. driver = FT_DRIVER( cur[0] );
  716. if ( args->flags & FT_OPEN_PARAMS )
  717. {
  718. num_params = args->num_params;
  719. params = args->params;
  720. }
  721. error = open_face( driver, stream, face_index,
  722. num_params, params, &face );
  723. if ( !error )
  724. goto Success;
  725. if ( FT_ERROR_BASE( error ) != FT_Err_Unknown_File_Format )
  726. goto Fail2;
  727. }
  728. }
  729. /* no driver is able to handle this format */
  730. error = FT_Err_Unknown_File_Format;
  731. Fail2:
  732. ft_input_stream_free( stream, external_stream );
  733. goto Fail;
  734. }
  735. Success:
  736. FT_TRACE4(( "FT_Open_Face: New face object, adding to list\n" ));
  737. /* set the FT_FACE_FLAG_EXTERNAL_STREAM bit for FT_Done_Face */
  738. if ( external_stream )
  739. face->face_flags |= FT_FACE_FLAG_EXTERNAL_STREAM;
  740. /* add the face object to its driver's list */
  741. if ( FT_NEW( node ) )
  742. goto Fail;
  743. node->data = face;
  744. /* don't assume driver is the same as face->driver, so use */
  745. /* face->driver instead. */
  746. FT_List_Add( &face->driver->faces_list, node );
  747. /* now allocate a glyph slot object for the face */
  748. {
  749. FT_GlyphSlot slot;
  750. FT_TRACE4(( "FT_Open_Face: Creating glyph slot\n" ));
  751. error = FT_New_GlyphSlot( face, &slot );
  752. if ( error )
  753. goto Fail;
  754. face->glyph = slot;
  755. }
  756. /* finally, allocate a size object for the face */
  757. {
  758. FT_Size size;
  759. FT_TRACE4(( "FT_Open_Face: Creating size object\n" ));
  760. error = FT_New_Size( face, &size );
  761. if ( error )
  762. goto Fail;
  763. face->size = size;
  764. }
  765. /* initialize internal face data */
  766. {
  767. FT_Face_Internal internal = face->internal;
  768. internal->transform_matrix.xx = 0x10000L;
  769. internal->transform_matrix.xy = 0;
  770. internal->transform_matrix.yx = 0;
  771. internal->transform_matrix.yy = 0x10000L;
  772. internal->transform_delta.x = 0;
  773. internal->transform_delta.y = 0;
  774. }
  775. *aface = face;
  776. goto Exit;
  777. Fail:
  778. FT_Done_Face( face );
  779. Exit:
  780. FT_TRACE4(( "FT_Open_Face: Return %d\n", error ));
  781. return error;
  782. }
  783. /* documentation is in freetype.h */
  784. FT_EXPORT_DEF( FT_Error )
  785. FT_Attach_File( FT_Face face,
  786. const char* filepathname )
  787. {
  788. FT_Open_Args open;
  789. /* test for valid `face' delayed to FT_Attach_Stream() */
  790. if ( !filepathname )
  791. return FT_Err_Invalid_Argument;
  792. open.flags = FT_OPEN_PATHNAME;
  793. open.pathname = (char*)filepathname;
  794. return FT_Attach_Stream( face, &open );
  795. }
  796. /* documentation is in freetype.h */
  797. FT_EXPORT_DEF( FT_Error )
  798. FT_Attach_Stream( FT_Face face,
  799. FT_Open_Args* parameters )
  800. {
  801. FT_Stream stream;
  802. FT_Error error;
  803. FT_Driver driver;
  804. FT_Driver_Class clazz;
  805. /* test for valid `parameters' delayed to ft_input_stream_new() */
  806. if ( !face )
  807. return FT_Err_Invalid_Face_Handle;
  808. driver = face->driver;
  809. if ( !driver )
  810. return FT_Err_Invalid_Driver_Handle;
  811. error = ft_input_stream_new( driver->root.library, parameters, &stream );
  812. if ( error )
  813. goto Exit;
  814. /* we implement FT_Attach_Stream in each driver through the */
  815. /* `attach_file' interface */
  816. error = FT_Err_Unimplemented_Feature;
  817. clazz = driver->clazz;
  818. if ( clazz->attach_file )
  819. error = clazz->attach_file( face, stream );
  820. /* close the attached stream */
  821. ft_input_stream_free( stream,
  822. (FT_Bool)( parameters->stream &&
  823. ( parameters->flags & FT_OPEN_STREAM ) ) );
  824. Exit:
  825. return error;
  826. }
  827. /* documentation is in freetype.h */
  828. FT_EXPORT_DEF( FT_Error )
  829. FT_Done_Face( FT_Face face )
  830. {
  831. FT_Error error;
  832. FT_Driver driver;
  833. FT_Memory memory;
  834. FT_ListNode node;
  835. error = FT_Err_Invalid_Face_Handle;
  836. if ( face && face->driver )
  837. {
  838. driver = face->driver;
  839. memory = driver->root.memory;
  840. /* find face in driver's list */
  841. node = FT_List_Find( &driver->faces_list, face );
  842. if ( node )
  843. {
  844. /* remove face object from the driver's list */
  845. FT_List_Remove( &driver->faces_list, node );
  846. FT_FREE( node );
  847. /* now destroy the object proper */
  848. destroy_face( memory, face, driver );
  849. error = FT_Err_Ok;
  850. }
  851. }
  852. return error;
  853. }
  854. /* documentation is in ftobjs.h */
  855. FT_EXPORT_DEF( FT_Error )
  856. FT_New_Size( FT_Face face,
  857. FT_Size *asize )
  858. {
  859. FT_Error error;
  860. FT_Memory memory;
  861. FT_Driver driver;
  862. FT_Driver_Class clazz;
  863. FT_Size size = 0;
  864. FT_ListNode node = 0;
  865. if ( !face )
  866. return FT_Err_Invalid_Face_Handle;
  867. if ( !asize )
  868. return FT_Err_Invalid_Size_Handle;
  869. if ( !face->driver )
  870. return FT_Err_Invalid_Driver_Handle;
  871. *asize = 0;
  872. driver = face->driver;
  873. clazz = driver->clazz;
  874. memory = face->memory;
  875. /* Allocate new size object and perform basic initialisation */
  876. if ( FT_ALLOC( size, clazz->size_object_size ) || FT_NEW( node ) )
  877. goto Exit;
  878. size->face = face;
  879. /* for now, do not use any internal fields in size objects */
  880. size->internal = 0;
  881. if ( clazz->init_size )
  882. error = clazz->init_size( size );
  883. /* in case of success, add to the face's list */
  884. if ( !error )
  885. {
  886. *asize = size;
  887. node->data = size;
  888. FT_List_Add( &face->sizes_list, node );
  889. }
  890. Exit:
  891. if ( error )
  892. {
  893. FT_FREE( node );
  894. FT_FREE( size );
  895. }
  896. return error;
  897. }
  898. /* documentation is in ftobjs.h */
  899. FT_EXPORT_DEF( FT_Error )
  900. FT_Done_Size( FT_Size size )
  901. {
  902. FT_Error error;
  903. FT_Driver driver;
  904. FT_Memory memory;
  905. FT_Face face;
  906. FT_ListNode node;
  907. if ( !size )
  908. return FT_Err_Invalid_Size_Handle;
  909. face = size->face;
  910. if ( !face )
  911. return FT_Err_Invalid_Face_Handle;
  912. driver = face->driver;
  913. if ( !driver )
  914. return FT_Err_Invalid_Driver_Handle;
  915. memory = driver->root.memory;
  916. error = FT_Err_Ok;
  917. node = FT_List_Find( &face->sizes_list, size );
  918. if ( node )
  919. {
  920. FT_List_Remove( &face->sizes_list, node );
  921. FT_FREE( node );
  922. if ( face->size == size )
  923. {
  924. face->size = 0;
  925. if ( face->sizes_list.head )
  926. face->size = (FT_Size)(face->sizes_list.head->data);
  927. }
  928. destroy_size( memory, size, driver );
  929. }
  930. else
  931. error = FT_Err_Invalid_Size_Handle;
  932. return error;
  933. }
  934. static void
  935. ft_recompute_scaled_metrics( FT_Face face,
  936. FT_Size_Metrics* metrics )
  937. {
  938. /* Compute root ascender, descender, test height, and max_advance */
  939. metrics->ascender = ( FT_MulFix( face->ascender,
  940. metrics->y_scale ) + 32 ) & -64;
  941. metrics->descender = ( FT_MulFix( face->descender,
  942. metrics->y_scale ) + 32 ) & -64;
  943. metrics->height = ( FT_MulFix( face->height,
  944. metrics->y_scale ) + 32 ) & -64;
  945. metrics->max_advance = ( FT_MulFix( face->max_advance_width,
  946. metrics->x_scale ) + 32 ) & -64;
  947. }
  948. /* documentation is in freetype.h */
  949. FT_EXPORT_DEF( FT_Error )
  950. FT_Set_Char_Size( FT_Face face,
  951. FT_F26Dot6 char_width,
  952. FT_F26Dot6 char_height,
  953. FT_UInt horz_resolution,
  954. FT_UInt vert_resolution )
  955. {
  956. FT_Error error = FT_Err_Ok;
  957. FT_Driver driver;
  958. FT_Driver_Class clazz;
  959. FT_Size_Metrics* metrics;
  960. FT_Long dim_x, dim_y;
  961. if ( !face || !face->size || !face->driver )
  962. return FT_Err_Invalid_Face_Handle;
  963. driver = face->driver;
  964. metrics = &face->size->metrics;
  965. if ( !char_width )
  966. char_width = char_height;
  967. else if ( !char_height )
  968. char_height = char_width;
  969. if ( !horz_resolution )
  970. horz_resolution = 72;
  971. if ( !vert_resolution )
  972. vert_resolution = 72;
  973. driver = face->driver;
  974. clazz = driver->clazz;
  975. /* default processing -- this can be overridden by the driver */
  976. if ( char_width < 1 * 64 )
  977. char_width = 1 * 64;
  978. if ( char_height < 1 * 64 )
  979. char_height = 1 * 64;
  980. /* Compute pixel sizes in 26.6 units */
  981. dim_x = ( ( ( char_width * horz_resolution ) / 72 ) + 32 ) & -64;
  982. dim_y = ( ( ( char_height * vert_resolution ) / 72 ) + 32 ) & -64;
  983. metrics->x_ppem = (FT_UShort)( dim_x >> 6 );
  984. metrics->y_ppem = (FT_UShort)( dim_y >> 6 );
  985. metrics->x_scale = 0x10000L;
  986. metrics->y_scale = 0x10000L;
  987. if ( face->face_flags & FT_FACE_FLAG_SCALABLE )
  988. {
  989. metrics->x_scale = FT_DivFix( dim_x, face->units_per_EM );
  990. metrics->y_scale = FT_DivFix( dim_y, face->units_per_EM );
  991. ft_recompute_scaled_metrics( face, metrics );
  992. }
  993. if ( clazz->set_char_sizes )
  994. error = clazz->set_char_sizes( face->size,
  995. char_width,
  996. char_height,
  997. horz_resolution,
  998. vert_resolution );
  999. return error;
  1000. }
  1001. /* documentation is in freetype.h */
  1002. FT_EXPORT_DEF( FT_Error )
  1003. FT_Set_Pixel_Sizes( FT_Face face,
  1004. FT_UInt pixel_width,
  1005. FT_UInt pixel_height )
  1006. {
  1007. FT_Error error = FT_Err_Ok;
  1008. FT_Driver driver;
  1009. FT_Driver_Class clazz;
  1010. FT_Size_Metrics* metrics = &face->size->metrics;
  1011. if ( !face || !face->size || !face->driver )
  1012. return FT_Err_Invalid_Face_Handle;
  1013. driver = face->driver;
  1014. clazz = driver->clazz;
  1015. /* default processing -- this can be overridden by the driver */
  1016. if ( pixel_width == 0 )
  1017. pixel_width = pixel_height;
  1018. else if ( pixel_height == 0 )
  1019. pixel_height = pixel_width;
  1020. if ( pixel_width < 1 )
  1021. pixel_width = 1;
  1022. if ( pixel_height < 1 )
  1023. pixel_height = 1;
  1024. metrics->x_ppem = (FT_UShort)pixel_width;
  1025. metrics->y_ppem = (FT_UShort)pixel_height;
  1026. if ( face->face_flags & FT_FACE_FLAG_SCALABLE )
  1027. {
  1028. metrics->x_scale = FT_DivFix( metrics->x_ppem << 6,
  1029. face->units_per_EM );
  1030. metrics->y_scale = FT_DivFix( metrics->y_ppem << 6,
  1031. face->units_per_EM );
  1032. ft_recompute_scaled_metrics( face, metrics );
  1033. }
  1034. if ( clazz->set_pixel_sizes )
  1035. error = clazz->set_pixel_sizes( face->size,
  1036. pixel_width,
  1037. pixel_height );
  1038. return error;
  1039. }
  1040. /* documentation is in freetype.h */
  1041. FT_EXPORT_DEF( FT_Error )
  1042. FT_Get_Kerning( FT_Face face,
  1043. FT_UInt left_glyph,
  1044. FT_UInt right_glyph,
  1045. FT_UInt kern_mode,
  1046. FT_Vector *akerning )
  1047. {
  1048. FT_Error error = FT_Err_Ok;
  1049. FT_Driver driver;
  1050. if ( !face )
  1051. return FT_Err_Invalid_Face_Handle;
  1052. if ( !akerning )
  1053. return FT_Err_Invalid_Argument;
  1054. driver = face->driver;
  1055. akerning->x = 0;
  1056. akerning->y = 0;
  1057. if ( driver->clazz->get_kerning )
  1058. {
  1059. error = driver->clazz->get_kerning( face,
  1060. left_glyph,
  1061. right_glyph,
  1062. akerning );
  1063. if ( !error )
  1064. {
  1065. if ( kern_mode != FT_KERNING_UNSCALED )
  1066. {
  1067. akerning->x = FT_MulFix( akerning->x, face->size->metrics.x_scale );
  1068. akerning->y = FT_MulFix( akerning->y, face->size->metrics.y_scale );
  1069. if ( kern_mode != FT_KERNING_UNFITTED )
  1070. {
  1071. akerning->x = ( akerning->x + 32 ) & -64;
  1072. akerning->y = ( akerning->y + 32 ) & -64;
  1073. }
  1074. }
  1075. }
  1076. }
  1077. return error;
  1078. }
  1079. /* documentation is in freetype.h */
  1080. FT_EXPORT_DEF( FT_Error )
  1081. FT_Select_Charmap( FT_Face face,
  1082. FT_Encoding encoding )
  1083. {
  1084. FT_CharMap* cur;
  1085. FT_CharMap* limit;
  1086. if ( !face )
  1087. return FT_Err_Invalid_Face_Handle;
  1088. cur = face->charmaps;
  1089. if ( !cur )
  1090. return FT_Err_Invalid_CharMap_Handle;
  1091. limit = cur + face->num_charmaps;
  1092. for ( ; cur < limit; cur++ )
  1093. {
  1094. if ( cur[0]->encoding == encoding )
  1095. {
  1096. face->charmap = cur[0];
  1097. return 0;
  1098. }
  1099. }
  1100. return FT_Err_Invalid_Argument;
  1101. }
  1102. /* documentation is in freetype.h */
  1103. FT_EXPORT_DEF( FT_Error )
  1104. FT_Set_Charmap( FT_Face face,
  1105. FT_CharMap charmap )
  1106. {
  1107. FT_CharMap* cur;
  1108. FT_CharMap* limit;
  1109. if ( !face )
  1110. return FT_Err_Invalid_Face_Handle;
  1111. cur = face->charmaps;
  1112. if ( !cur )
  1113. return FT_Err_Invalid_CharMap_Handle;
  1114. limit = cur + face->num_charmaps;
  1115. for ( ; cur < limit; cur++ )
  1116. {
  1117. if ( cur[0] == charmap )
  1118. {
  1119. face->charmap = cur[0];
  1120. return 0;
  1121. }
  1122. }
  1123. return FT_Err_Invalid_Argument;
  1124. }
  1125. FT_BASE_DEF( void )
  1126. FT_CMap_Done( FT_CMap cmap )
  1127. {
  1128. if ( cmap )
  1129. {
  1130. FT_CMap_Class clazz = cmap->clazz;
  1131. FT_Face face = cmap->charmap.face;
  1132. FT_Memory memory = FT_FACE_MEMORY(face);
  1133. if ( clazz->done )
  1134. clazz->done( cmap );
  1135. FT_FREE( cmap );
  1136. }
  1137. }
  1138. FT_BASE_DEF( FT_Error )
  1139. FT_CMap_New( FT_CMap_Class clazz,
  1140. FT_Pointer init_data,
  1141. FT_CharMap charmap,
  1142. FT_CMap *acmap )
  1143. {
  1144. FT_Error error = 0;
  1145. FT_Face face;
  1146. FT_Memory memory;
  1147. FT_CMap cmap;
  1148. if ( clazz == NULL || charmap == NULL || charmap->face == NULL )
  1149. return FT_Err_Invalid_Argument;
  1150. face = charmap->face;
  1151. memory = FT_FACE_MEMORY(face);
  1152. if ( !FT_ALLOC( cmap, clazz->size ) )
  1153. {
  1154. cmap->charmap = *charmap;
  1155. cmap->clazz = clazz;
  1156. if ( clazz->init )
  1157. {
  1158. error = clazz->init( cmap, init_data );
  1159. if ( error )
  1160. goto Fail;
  1161. }
  1162. /* add it to our list of charmaps */
  1163. if ( FT_RENEW_ARRAY( face->charmaps,
  1164. face->num_charmaps,
  1165. face->num_charmaps+1 ) )
  1166. goto Fail;
  1167. face->charmaps[face->num_charmaps++] = (FT_CharMap)cmap;
  1168. }
  1169. Exit:
  1170. if ( acmap )
  1171. *acmap = cmap;
  1172. return error;
  1173. Fail:
  1174. FT_CMap_Done( cmap );
  1175. cmap = NULL;
  1176. goto Exit;
  1177. }
  1178. /* documentation is in freetype.h */
  1179. FT_EXPORT_DEF( FT_UInt )
  1180. FT_Get_Char_Index( FT_Face face,
  1181. FT_ULong charcode )
  1182. {
  1183. FT_UInt result = 0;
  1184. if ( face && face->charmap )
  1185. {
  1186. FT_CMap cmap = FT_CMAP( face->charmap );
  1187. result = cmap->clazz->char_index( cmap, charcode );
  1188. }
  1189. return result;
  1190. }
  1191. /* documentation is in freetype.h */
  1192. FT_EXPORT_DEF( FT_ULong )
  1193. FT_Get_First_Char( FT_Face face,
  1194. FT_UInt *agindex )
  1195. {
  1196. FT_ULong result = 0;
  1197. FT_UInt gindex = 0;
  1198. if ( face && face->charmap )
  1199. {
  1200. gindex = FT_Get_Char_Index( face, 0 );
  1201. if ( gindex == 0 )
  1202. result = FT_Get_Next_Char( face, 0, &gindex );
  1203. }
  1204. if ( agindex )
  1205. *agindex = gindex;
  1206. return result;
  1207. }
  1208. /* documentation is in freetype.h */
  1209. FT_EXPORT_DEF( FT_ULong )
  1210. FT_Get_Next_Char( FT_Face face,
  1211. FT_ULong charcode,
  1212. FT_UInt *agindex )
  1213. {
  1214. FT_ULong result = 0;
  1215. FT_UInt gindex = 0;
  1216. if ( face && face->charmap )
  1217. {
  1218. FT_UInt32 code = (FT_UInt32)charcode;
  1219. FT_CMap cmap = FT_CMAP( face->charmap );
  1220. gindex = cmap->clazz->char_next( cmap, &code );
  1221. result = ( gindex == 0 ) ? 0 : code;
  1222. }
  1223. if ( agindex )
  1224. *agindex = gindex;
  1225. return result;
  1226. }
  1227. /* documentation is in freetype.h */
  1228. FT_EXPORT_DEF( FT_UInt )
  1229. FT_Get_Name_Index( FT_Face face,
  1230. FT_String* glyph_name )
  1231. {
  1232. FT_UInt result = 0;
  1233. if ( face && FT_HAS_GLYPH_NAMES( face ) )
  1234. {
  1235. /* now, lookup for glyph name */
  1236. FT_Driver driver = face->driver;
  1237. FT_Module_Class* clazz = FT_MODULE_CLASS( driver );
  1238. if ( clazz->get_interface )
  1239. {
  1240. FT_Face_GetGlyphNameIndexFunc requester;
  1241. requester = (FT_Face_GetGlyphNameIndexFunc)clazz->get_interface(
  1242. FT_MODULE( driver ), "name_index" );
  1243. if ( requester )
  1244. result = requester( face, glyph_name );
  1245. }
  1246. }
  1247. return result;
  1248. }
  1249. /* documentation is in freetype.h */
  1250. FT_EXPORT_DEF( FT_Error )
  1251. FT_Get_Glyph_Name( FT_Face face,
  1252. FT_UInt glyph_index,
  1253. FT_Pointer buffer,
  1254. FT_UInt buffer_max )
  1255. {
  1256. FT_Error error = FT_Err_Invalid_Argument;
  1257. /* clean up buffer */
  1258. if ( buffer && buffer_max > 0 )
  1259. ((FT_Byte*)buffer)[0] = 0;
  1260. if ( face &&
  1261. glyph_index <= (FT_UInt)face->num_glyphs &&
  1262. FT_HAS_GLYPH_NAMES( face ) )
  1263. {
  1264. /* now, lookup for glyph name */
  1265. FT_Driver driver = face->driver;
  1266. FT_Module_Class* clazz = FT_MODULE_CLASS( driver );
  1267. if ( clazz->get_interface )
  1268. {
  1269. FT_Face_GetGlyphNameFunc requester;
  1270. requester = (FT_Face_GetGlyphNameFunc)clazz->get_interface(
  1271. FT_MODULE( driver ), "glyph_name" );
  1272. if ( requester )
  1273. error = requester( face, glyph_index, buffer, buffer_max );
  1274. }
  1275. }
  1276. return error;
  1277. }
  1278. /* documentation is in freetype.h */
  1279. FT_EXPORT_DEF( const char* )
  1280. FT_Get_Postscript_Name( FT_Face face )
  1281. {
  1282. const char* result = NULL;
  1283. if ( !face )
  1284. goto Exit;
  1285. result = face->internal->postscript_name;
  1286. if ( !result )
  1287. {
  1288. /* now, look up glyph name */
  1289. FT_Driver driver = face->driver;
  1290. FT_Module_Class* clazz = FT_MODULE_CLASS( driver );
  1291. if ( clazz->get_interface )
  1292. {
  1293. FT_Face_GetPostscriptNameFunc requester;
  1294. requester = (FT_Face_GetPostscriptNameFunc)clazz->get_interface(
  1295. FT_MODULE( driver ), "postscript_name" );
  1296. if ( requester )
  1297. result = requester( face );
  1298. }
  1299. }
  1300. Exit:
  1301. return result;
  1302. }
  1303. /* documentation is in tttables.h */
  1304. FT_EXPORT_DEF( void* )
  1305. FT_Get_Sfnt_Table( FT_Face face,
  1306. FT_Sfnt_Tag tag )
  1307. {
  1308. void* table = 0;
  1309. FT_Get_Sfnt_Table_Func func;
  1310. FT_Driver driver;
  1311. if ( !face || !FT_IS_SFNT( face ) )
  1312. goto Exit;
  1313. driver = face->driver;
  1314. func = (FT_Get_Sfnt_Table_Func)driver->root.clazz->get_interface(
  1315. FT_MODULE( driver ), "get_sfnt" );
  1316. if ( func )
  1317. table = func( face, tag );
  1318. Exit:
  1319. return table;
  1320. }
  1321. FT_EXPORT_DEF( FT_Error )
  1322. FT_Activate_Size( FT_Size size )
  1323. {
  1324. FT_Face face;
  1325. if ( size == NULL )
  1326. return FT_Err_Bad_Argument;
  1327. face = size->face;
  1328. if ( face == NULL || face->driver == NULL )
  1329. return FT_Err_Bad_Argument;
  1330. /* we don't need anything more complex than that; all size objects */
  1331. /* are already listed by the face */
  1332. face->size = size;
  1333. return FT_Err_Ok;
  1334. }
  1335. /*************************************************************************/
  1336. /*************************************************************************/
  1337. /*************************************************************************/
  1338. /**** ****/
  1339. /**** ****/
  1340. /**** R E N D E R E R S ****/
  1341. /**** ****/
  1342. /**** ****/
  1343. /*************************************************************************/
  1344. /*************************************************************************/
  1345. /*************************************************************************/
  1346. /* lookup a renderer by glyph format in the library's list */
  1347. FT_BASE_DEF( FT_Renderer )
  1348. FT_Lookup_Renderer( FT_Library library,
  1349. FT_Glyph_Format format,
  1350. FT_ListNode* node )
  1351. {
  1352. FT_ListNode cur;
  1353. FT_Renderer result = 0;
  1354. if ( !library )
  1355. goto Exit;
  1356. cur = library->renderers.head;
  1357. if ( node )
  1358. {
  1359. if ( *node )
  1360. cur = (*node)->next;
  1361. *node = 0;
  1362. }
  1363. while ( cur )
  1364. {
  1365. FT_Renderer renderer = FT_RENDERER( cur->data );
  1366. if ( renderer->glyph_format == format )
  1367. {
  1368. if ( node )
  1369. *node = cur;
  1370. result = renderer;
  1371. break;
  1372. }
  1373. cur = cur->next;
  1374. }
  1375. Exit:
  1376. return result;
  1377. }
  1378. static FT_Renderer
  1379. ft_lookup_glyph_renderer( FT_GlyphSlot slot )
  1380. {
  1381. FT_Face face = slot->face;
  1382. FT_Library library = FT_FACE_LIBRARY( face );
  1383. FT_Renderer result = library->cur_renderer;
  1384. if ( !result || result->glyph_format != slot->format )
  1385. result = FT_Lookup_Renderer( library, slot->format, 0 );
  1386. return result;
  1387. }
  1388. static void
  1389. ft_set_current_renderer( FT_Library library )
  1390. {
  1391. FT_Renderer renderer;
  1392. renderer = FT_Lookup_Renderer( library, FT_GLYPH_FORMAT_OUTLINE, 0 );
  1393. library->cur_renderer = renderer;
  1394. }
  1395. static FT_Error
  1396. ft_add_renderer( FT_Module module )
  1397. {
  1398. FT_Library library = module->library;
  1399. FT_Memory memory = library->memory;
  1400. FT_Error error;
  1401. FT_ListNode node;
  1402. if ( FT_NEW( node ) )
  1403. goto Exit;
  1404. {
  1405. FT_Renderer render = FT_RENDERER( module );
  1406. FT_Renderer_Class* clazz = (FT_Renderer_Class*)module->clazz;
  1407. render->clazz = clazz;
  1408. render->glyph_format = clazz->glyph_format;
  1409. /* allocate raster object if needed */
  1410. if ( clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
  1411. clazz->raster_class->raster_new )
  1412. {
  1413. error = clazz->raster_class->raster_new( memory, &render->raster );
  1414. if ( error )
  1415. goto Fail;
  1416. render->raster_render = clazz->raster_class->raster_render;
  1417. render->render = clazz->render_glyph;
  1418. }
  1419. /* add to list */
  1420. node->data = module;
  1421. FT_List_Add( &library->renderers, node );
  1422. ft_set_current_renderer( library );
  1423. }
  1424. Fail:
  1425. if ( error )
  1426. FT_FREE( node );
  1427. Exit:
  1428. return error;
  1429. }
  1430. static void
  1431. ft_remove_renderer( FT_Module module )
  1432. {
  1433. FT_Library library = module->library;
  1434. FT_Memory memory = library->memory;
  1435. FT_ListNode node;
  1436. node = FT_List_Find( &library->renderers, module );
  1437. if ( node )
  1438. {
  1439. FT_Renderer render = FT_RENDERER( module );
  1440. /* release raster object, if any */
  1441. if ( render->raster )
  1442. render->clazz->raster_class->raster_done( render->raster );
  1443. /* remove from list */
  1444. FT_List_Remove( &library->renderers, node );
  1445. FT_FREE( node );
  1446. ft_set_current_renderer( library );
  1447. }
  1448. }
  1449. /* documentation is in ftrender.h */
  1450. FT_EXPORT_DEF( FT_Renderer )
  1451. FT_Get_Renderer( FT_Library library,
  1452. FT_Glyph_Format format )
  1453. {
  1454. /* test for valid `library' delayed to FT_Lookup_Renderer() */
  1455. return FT_Lookup_Renderer( library, format, 0 );
  1456. }
  1457. /* documentation is in ftrender.h */
  1458. FT_EXPORT_DEF( FT_Error )
  1459. FT_Set_Renderer( FT_Library library,
  1460. FT_Renderer renderer,
  1461. FT_UInt num_params,
  1462. FT_Parameter* parameters )
  1463. {
  1464. FT_ListNode node;
  1465. FT_Error error = FT_Err_Ok;
  1466. if ( !library )
  1467. return FT_Err_Invalid_Library_Handle;
  1468. if ( !renderer )
  1469. return FT_Err_Invalid_Argument;
  1470. node = FT_List_Find( &library->renderers, renderer );
  1471. if ( !node )
  1472. {
  1473. error = FT_Err_Invalid_Argument;
  1474. goto Exit;
  1475. }
  1476. FT_List_Up( &library->renderers, node );
  1477. if ( renderer->glyph_format == FT_GLYPH_FORMAT_OUTLINE )
  1478. library->cur_renderer = renderer;
  1479. if ( num_params > 0 )
  1480. {
  1481. FT_Renderer_SetModeFunc set_mode = renderer->clazz->set_mode;
  1482. for ( ; num_params > 0; num_params-- )
  1483. {
  1484. error = set_mode( renderer, parameters->tag, parameters->data );
  1485. if ( error )
  1486. break;
  1487. }
  1488. }
  1489. Exit:
  1490. return error;
  1491. }
  1492. FT_BASE_DEF( FT_Error )
  1493. FT_Render_Glyph_Internal( FT_Library library,
  1494. FT_GlyphSlot slot,
  1495. FT_Render_Mode render_mode )
  1496. {
  1497. FT_Error error = FT_Err_Ok;
  1498. FT_Renderer renderer;
  1499. /* if it is already a bitmap, no need to do anything */
  1500. switch ( slot->format )
  1501. {
  1502. case FT_GLYPH_FORMAT_BITMAP: /* already a bitmap, don't do anything */
  1503. break;
  1504. default:
  1505. {
  1506. FT_ListNode node = 0;
  1507. FT_Bool update = 0;
  1508. /* small shortcut for the very common case */
  1509. if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
  1510. {
  1511. renderer = library->cur_renderer;
  1512. node = library->renderers.head;
  1513. }
  1514. else
  1515. renderer = FT_Lookup_Renderer( library, slot->format, &node );
  1516. error = FT_Err_Unimplemented_Feature;
  1517. while ( renderer )
  1518. {
  1519. error = renderer->render( renderer, slot, render_mode, NULL );
  1520. if ( !error ||
  1521. FT_ERROR_BASE( error ) != FT_Err_Cannot_Render_Glyph )
  1522. break;
  1523. /* FT_Err_Cannot_Render_Glyph is returned if the render mode */
  1524. /* is unsupported by the current renderer for this glyph image */
  1525. /* format. */
  1526. /* now, look for another renderer that supports the same */
  1527. /* format. */
  1528. renderer = FT_Lookup_Renderer( library, slot->format, &node );
  1529. update = 1;
  1530. }
  1531. /* if we changed the current renderer for the glyph image format */
  1532. /* we need to select it as the next current one */
  1533. if ( !error && update && renderer )
  1534. FT_Set_Renderer( library, renderer, 0, 0 );
  1535. }
  1536. }
  1537. return error;
  1538. }
  1539. /* documentation is in freetype.h */
  1540. FT_EXPORT_DEF( FT_Error )
  1541. FT_Render_Glyph( FT_GlyphSlot slot,
  1542. FT_Render_Mode render_mode )
  1543. {
  1544. FT_Library library;
  1545. if ( !slot )
  1546. return FT_Err_Invalid_Argument;
  1547. library = FT_FACE_LIBRARY( slot->face );
  1548. return FT_Render_Glyph_Internal( library, slot, render_mode );
  1549. }
  1550. /*************************************************************************/
  1551. /*************************************************************************/
  1552. /*************************************************************************/
  1553. /**** ****/
  1554. /**** ****/
  1555. /**** M O D U L E S ****/
  1556. /**** ****/
  1557. /**** ****/
  1558. /*************************************************************************/
  1559. /*************************************************************************/
  1560. /*************************************************************************/
  1561. /*************************************************************************/
  1562. /* */
  1563. /* <Function> */
  1564. /* Destroy_Module */
  1565. /* */
  1566. /* <Description> */
  1567. /* Destroys a given module object. For drivers, this also destroys */
  1568. /* all child faces. */
  1569. /* */
  1570. /* <InOut> */
  1571. /* module :: A handle to the target driver object. */
  1572. /* */
  1573. /* <Note> */
  1574. /* The driver _must_ be LOCKED! */
  1575. /* */
  1576. static void
  1577. Destroy_Module( FT_Module module )
  1578. {
  1579. FT_Memory memory = module->memory;
  1580. FT_Module_Class* clazz = module->clazz;
  1581. FT_Library library = module->library;
  1582. /* finalize client-data - before anything else */
  1583. if ( module->generic.finalizer )
  1584. module->generic.finalizer( module );
  1585. if ( library && library->auto_hinter == module )
  1586. library->auto_hinter = 0;
  1587. /* if the module is a renderer */
  1588. if ( FT_MODULE_IS_RENDERER( module ) )
  1589. ft_remove_renderer( module );
  1590. /* if the module is a font driver, add some steps */
  1591. if ( FT_MODULE_IS_DRIVER( module ) )
  1592. Destroy_Driver( FT_DRIVER( module ) );
  1593. /* finalize the module object */
  1594. if ( clazz->module_done )
  1595. clazz->module_done( module );
  1596. /* discard it */
  1597. FT_FREE( module );
  1598. }
  1599. /* documentation is in ftmodule.h */
  1600. FT_EXPORT_DEF( FT_Error )
  1601. FT_Add_Module( FT_Library library,
  1602. const FT_Module_Class* clazz )
  1603. {
  1604. FT_Error error;
  1605. FT_Memory memory;
  1606. FT_Module module;
  1607. FT_UInt nn;
  1608. #define FREETYPE_VER_FIXED ( ( (FT_Long)FREETYPE_MAJOR << 16 ) | \
  1609. FREETYPE_MINOR )
  1610. if ( !library )
  1611. return FT_Err_Invalid_Library_Handle;
  1612. if ( !clazz )
  1613. return FT_Err_Invalid_Argument;
  1614. /* check freetype version */
  1615. if ( clazz->module_requires > FREETYPE_VER_FIXED )
  1616. return FT_Err_Invalid_Version;
  1617. /* look for a module with the same name in the library's table */
  1618. for ( nn = 0; nn < library->num_modules; nn++ )
  1619. {
  1620. module = library->modules[nn];
  1621. if ( ft_strcmp( module->clazz->module_name, clazz->module_name ) == 0 )
  1622. {
  1623. /* this installed module has the same name, compare their versions */
  1624. if ( clazz->module_version <= module->clazz->module_version )
  1625. return FT_Err_Lower_Module_Version;
  1626. /* remove the module from our list, then exit the loop to replace */
  1627. /* it by our new version.. */
  1628. FT_Remove_Module( library, module );
  1629. break;
  1630. }
  1631. }
  1632. memory = library->memory;
  1633. error = FT_Err_Ok;
  1634. if ( library->num_modules >= FT_MAX_MODULES )
  1635. {
  1636. error = FT_Err_Too_Many_Drivers;
  1637. goto Exit;
  1638. }
  1639. /* allocate module object */
  1640. if ( FT_ALLOC( module, clazz->module_size ) )
  1641. goto Exit;
  1642. /* base initialization */
  1643. module->library = library;
  1644. module->memory = memory;
  1645. module->clazz = (FT_Module_Class*)clazz;
  1646. /* check whether the module is a renderer - this must be performed */
  1647. /* before the normal module initialization */
  1648. if ( FT_MODULE_IS_RENDERER( module ) )
  1649. {
  1650. /* add to the renderers list */
  1651. error = ft_add_renderer( module );
  1652. if ( error )
  1653. goto Fail;
  1654. }
  1655. /* is the module a auto-hinter? */
  1656. if ( FT_MODULE_IS_HINTER( module ) )
  1657. library->auto_hinter = module;
  1658. /* if the module is a font driver */
  1659. if ( FT_MODULE_IS_DRIVER( module ) )
  1660. {
  1661. /* allocate glyph loader if needed */
  1662. FT_Driver driver = FT_DRIVER( module );
  1663. driver->clazz = (FT_Driver_Class)module->clazz;
  1664. if ( FT_DRIVER_USES_OUTLINES( driver ) )
  1665. {
  1666. error = FT_GlyphLoader_New( memory, &driver->glyph_loader );
  1667. if ( error )
  1668. goto Fail;
  1669. }
  1670. }
  1671. if ( clazz->module_init )
  1672. {
  1673. error = clazz->module_init( module );
  1674. if ( error )
  1675. goto Fail;
  1676. }
  1677. /* add module to the library's table */
  1678. library->modules[library->num_modules++] = module;
  1679. Exit:
  1680. return error;
  1681. Fail:
  1682. if ( FT_MODULE_IS_DRIVER( module ) )
  1683. {
  1684. FT_Driver driver = FT_DRIVER( module );
  1685. if ( FT_DRIVER_USES_OUTLINES( driver ) )
  1686. FT_GlyphLoader_Done( driver->glyph_loader );
  1687. }
  1688. if ( FT_MODULE_IS_RENDERER( module ) )
  1689. {
  1690. FT_Renderer renderer = FT_RENDERER( module );
  1691. if ( renderer->raster )
  1692. renderer->clazz->raster_class->raster_done( renderer->raster );
  1693. }
  1694. FT_FREE( module );
  1695. goto Exit;
  1696. }
  1697. /* documentation is in ftmodule.h */
  1698. FT_EXPORT_DEF( FT_Module )
  1699. FT_Get_Module( FT_Library library,
  1700. const char* module_name )
  1701. {
  1702. FT_Module result = 0;
  1703. FT_Module* cur;
  1704. FT_Module* limit;
  1705. if ( !library || !module_name )
  1706. return result;
  1707. cur = library->modules;
  1708. limit = cur + library->num_modules;
  1709. for ( ; cur < limit; cur++ )
  1710. if ( ft_strcmp( cur[0]->clazz->module_name, module_name ) == 0 )
  1711. {
  1712. result = cur[0];
  1713. break;
  1714. }
  1715. return result;
  1716. }
  1717. /* documentation is in ftobjs.h */
  1718. FT_BASE_DEF( const void* )
  1719. FT_Get_Module_Interface( FT_Library library,
  1720. const char* mod_name )
  1721. {
  1722. FT_Module module;
  1723. /* test for valid `library' delayed to FT_Get_Module() */
  1724. module = FT_Get_Module( library, mod_name );
  1725. return module ? module->clazz->module_interface : 0;
  1726. }
  1727. /* documentation is in ftmodule.h */
  1728. FT_EXPORT_DEF( FT_Error )
  1729. FT_Remove_Module( FT_Library library,
  1730. FT_Module module )
  1731. {
  1732. /* try to find the module from the table, then remove it from there */
  1733. if ( !library )
  1734. return FT_Err_Invalid_Library_Handle;
  1735. if ( module )
  1736. {
  1737. FT_Module* cur = library->modules;
  1738. FT_Module* limit = cur + library->num_modules;
  1739. for ( ; cur < limit; cur++ )
  1740. {
  1741. if ( cur[0] == module )
  1742. {
  1743. /* remove it from the table */
  1744. library->num_modules--;
  1745. limit--;
  1746. while ( cur < limit )
  1747. {
  1748. cur[0] = cur[1];
  1749. cur++;
  1750. }
  1751. limit[0] = 0;
  1752. /* destroy the module */
  1753. Destroy_Module( module );
  1754. return FT_Err_Ok;
  1755. }
  1756. }
  1757. }
  1758. return FT_Err_Invalid_Driver_Handle;
  1759. }
  1760. /*************************************************************************/
  1761. /*************************************************************************/
  1762. /*************************************************************************/
  1763. /**** ****/
  1764. /**** ****/
  1765. /**** L I B R A R Y ****/
  1766. /**** ****/
  1767. /**** ****/
  1768. /*************************************************************************/
  1769. /*************************************************************************/
  1770. /*************************************************************************/
  1771. /* documentation is in ftmodule.h */
  1772. FT_EXPORT_DEF( FT_Error )
  1773. FT_New_Library( FT_Memory memory,
  1774. FT_Library *alibrary )
  1775. {
  1776. FT_Library library = 0;
  1777. FT_Error error;
  1778. if ( !memory )
  1779. return FT_Err_Invalid_Argument;
  1780. #ifdef FT_DEBUG_LEVEL_ERROR
  1781. /* init debugging support */
  1782. ft_debug_init();
  1783. #endif
  1784. /* first of all, allocate the library object */
  1785. if ( FT_NEW( library ) )
  1786. return error;
  1787. library->memory = memory;
  1788. /* allocate the render pool */
  1789. library->raster_pool_size = FT_RENDER_POOL_SIZE;
  1790. if ( FT_ALLOC( library->raster_pool, FT_RENDER_POOL_SIZE ) )
  1791. goto Fail;
  1792. /* That's ok now */
  1793. *alibrary = library;
  1794. return FT_Err_Ok;
  1795. Fail:
  1796. FT_FREE( library );
  1797. return error;
  1798. }
  1799. /* documentation is in freetype.h */
  1800. FT_EXPORT_DEF( void )
  1801. FT_Library_Version( FT_Library library,
  1802. FT_Int *amajor,
  1803. FT_Int *aminor,
  1804. FT_Int *apatch )
  1805. {
  1806. FT_Int major = 0;
  1807. FT_Int minor = 0;
  1808. FT_Int patch = 0;
  1809. if ( library )
  1810. {
  1811. major = library->version_major;
  1812. minor = library->version_minor;
  1813. patch = library->version_patch;
  1814. }
  1815. if ( amajor )
  1816. *amajor = major;
  1817. if ( aminor )
  1818. *aminor = minor;
  1819. if ( apatch )
  1820. *apatch = patch;
  1821. }
  1822. /* documentation is in ftmodule.h */
  1823. FT_EXPORT_DEF( FT_Error )
  1824. FT_Done_Library( FT_Library library )
  1825. {
  1826. FT_Memory memory;
  1827. if ( !library )
  1828. return FT_Err_Invalid_Library_Handle;
  1829. memory = library->memory;
  1830. /* Discard client-data */
  1831. if ( library->generic.finalizer )
  1832. library->generic.finalizer( library );
  1833. /* Close all modules in the library */
  1834. #if 1
  1835. while ( library->num_modules > 0 )
  1836. FT_Remove_Module( library, library->modules[0] );
  1837. #else
  1838. {
  1839. FT_UInt n;
  1840. for ( n = 0; n < library->num_modules; n++ )
  1841. {
  1842. FT_Module module = library->modules[n];
  1843. if ( module )
  1844. {
  1845. Destroy_Module( module );
  1846. library->modules[n] = 0;
  1847. }
  1848. }
  1849. }
  1850. #endif
  1851. /* Destroy raster objects */
  1852. FT_FREE( library->raster_pool );
  1853. library->raster_pool_size = 0;
  1854. FT_FREE( library );
  1855. return FT_Err_Ok;
  1856. }
  1857. /* documentation is in ftmodule.h */
  1858. FT_EXPORT_DEF( void )
  1859. FT_Set_Debug_Hook( FT_Library library,
  1860. FT_UInt hook_index,
  1861. FT_DebugHook_Func debug_hook )
  1862. {
  1863. if ( library && debug_hook &&
  1864. hook_index <
  1865. ( sizeof ( library->debug_hooks ) / sizeof ( void* ) ) )
  1866. library->debug_hooks[hook_index] = debug_hook;
  1867. }
  1868. /* END */