pcf.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590
  1. /*
  2. * CDDL HEADER START
  3. *
  4. * The contents of this file are subject to the terms of the
  5. * Common Development and Distribution License (the "License").
  6. * You may not use this file except in compliance with the License.
  7. *
  8. * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE
  9. * or http://www.opensolaris.org/os/licensing.
  10. * See the License for the specific language governing permissions
  11. * and limitations under the License.
  12. *
  13. * When distributing Covered Code, include this CDDL HEADER in each
  14. * file and include the License file at src/OPENSOLARIS.LICENSE.
  15. * If applicable, add the following below this CDDL HEADER, with the
  16. * fields enclosed by brackets "[]" replaced with your own identifying
  17. * information: Portions Copyright [yyyy] [name of copyright owner]
  18. *
  19. * CDDL HEADER END
  20. */
  21. /*
  22. * Copyright (c) 1999 by Sun Microsystems, Inc.
  23. * All rights reserved.
  24. */
  25. /*
  26. * Copyright (C) 1994 X Consortium
  27. *
  28. * Permission is hereby granted, free of charge, to any person obtaining a copy
  29. * of this software and associated documentation files (the "Software"), to
  30. * deal in the Software without restriction, including without limitation the
  31. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  32. * sell copies of the Software, and to permit persons to whom the Software is
  33. * furnished to do so, subject to the following conditions:
  34. *
  35. * The above copyright notice and this permission notice shall be included in
  36. * all copies or substantial portions of the Software.
  37. *
  38. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  39. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  40. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  41. * X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  42. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
  43. * TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  44. *
  45. * Except as contained in this notice, the name of the X Consortium shall not
  46. * be used in advertising or otherwise to promote the sale, use or other deal-
  47. * ings in this Software without prior written authorization from the X Consor-
  48. * tium.
  49. *
  50. * X Window System is a trademark of X Consortium, Inc.
  51. */
  52. //#pragma ident "@(#)pcf.c 1.3 00/02/02 SMI"
  53. #include <stdio.h>
  54. #include <fcntl.h>
  55. /* #include <unistd.h> */
  56. #include <string.h>
  57. /* #include <X11/Xmd.h> */
  58. #include <assert.h>
  59. #include <errno.h>
  60. #include <stdlib.h>
  61. #include <wchar.h>
  62. #include "general_header.h"
  63. /* #include "print_preprocess.h" */
  64. #include "pcf_private.h"
  65. #include "pcf.h"
  66. #define pcfGetINT8(file, format) (position++, FontFileGetc(file))
  67. /* #define FileDes(f) ((int) (f)->_private) */
  68. #define FileDes(f) ((FILE *) (f)->_private)
  69. //extern print_info *print_info_st;
  70. typedef struct font_bmf_header{
  71. INT8 linespace;
  72. INT8 ascent;
  73. INT8 descent;
  74. INT8 bmp_buffer_len;
  75. }font_bmf_header;
  76. typedef struct font_bmf{
  77. INT8 width;
  78. INT8 height;
  79. INT8 widthBytes;
  80. INT8 widthBits;
  81. INT8 ascent;
  82. INT8 descent;
  83. INT8 LSBearing;
  84. INT8 RSBearing;
  85. char bitmap[48];
  86. }font_bmf;
  87. typedef struct font_bmf_ex{
  88. INT8 width;
  89. INT8 height;
  90. INT8 widthBytes;
  91. INT8 widthBits;
  92. INT8 ascent;
  93. INT8 descent;
  94. INT8 LSBearing;
  95. INT8 RSBearing;
  96. INT32 pos;
  97. }font_bmf_ex;
  98. #ifdef DEBUG
  99. static void dump_Fmetrics(pcffont_t *);
  100. #endif
  101. static code_int getcode(CompressedFile *);
  102. static void BufFileClose(BufFilePtr,int);
  103. static int get_font_property(FontPtr, char *, ulong_t *);
  104. static BufFilePtr BufFileCreate ( char *, int (*)(BufFilePtr), int (*)(BufFilePtr, int),int (*)(BufFilePtr, int) );
  105. static int BufCompressedClose(BufFilePtr, int);
  106. static int BufCompressedSkip(BufFilePtr, int);
  107. static int BufCompressedFill(BufFilePtr);
  108. static BufFilePtr BufFilePushCompressed(BufFilePtr);
  109. static FontFilePtr FontFileOpen(char *);
  110. static void FontFileClose (FontFilePtr);
  111. static int pcfGetLSB32(FontFilePtr);
  112. static PCFTablePtr pcfReadTOC(FontFilePtr,int*);
  113. static Bool pcfGetProperties(FontInfoPtr,FontFilePtr, PCFTablePtr, int);
  114. static void pcfGetCompressedMetric(FontFilePtr,CARD32, xCharInfo*);
  115. static Bool pcfSeekToType(FontFilePtr, PCFTablePtr, int, CARD32, CARD32*, CARD32*);
  116. static int pcfGetINT16(FontFilePtr, CARD32);
  117. static int pcfGetINT32(FontFilePtr, CARD32);
  118. static Bool pcfGetAccel(FontInfoPtr, FontFilePtr, PCFTablePtr, int, CARD32);
  119. static void pcfGetMetric(FontFilePtr, CARD32, xCharInfo *);
  120. static int pcfReadFont(FontPtr, FontFilePtr, int, int, int, int);
  121. static char *NameForAtom(Atom);
  122. /* static BufFilePtr BufFileOpenRead(int); */
  123. static BufFilePtr BufFileOpenRead(FILE *);
  124. static int BufFileRawFill(BufFilePtr);
  125. static int BufFileRawSkip(BufFilePtr, int);
  126. static int BufFileRawClose (BufFilePtr, int);
  127. static int bitmapGetGlyphs(FontPtr, unsigned long, unsigned char *, FontEncoding , unsigned long * , CharInfoPtr *);
  128. static int bitmapGetMetrics(FontPtr, unsigned long, unsigned char *, FontEncoding, unsigned long *, xCharInfo **);
  129. static void pcfUnloadFont(FontPtr);
  130. //static int handle_cuferr(int , ucs4_t *, int *);
  131. //static int handle_illegalchar(ucs4_t *, int *);
  132. //static int handle_nonidentchar(ucs4_t *, int *);
  133. static int handle_nobitmap(ucs4_t *, pcffont_t *, pcf_charmet_t *, pcf_bm_t **) ;
  134. //static int handle_nongraphchar(ucs4_t *, int *);
  135. static pcf_bm_t * xpcf_getcbm(ucs4_t , pcffont_t *, pcf_charmet_t *);
  136. static void BitOrderInvert(unsigned char *, int);
  137. static Bool pcfHasType ( PCFTablePtr, int, CARD32);
  138. static void TwoByteSwap(unsigned char *, int);
  139. static void FourByteSwap(unsigned char *, int);
  140. static int RepadBitmap(char *, char *, unsigned int, unsigned int, int, int);
  141. static Atom MakeAtom(char *, unsigned, int );
  142. static Bool ResizeReverseMap ();
  143. static Bool NameEqual (char *, char *,int );
  144. static int Hash(char *, int);
  145. static Bool ResizeHashTable();
  146. void put_PSbitmap(ucs4_t , pcf_bm_t *, pcf_charmet_t *, pcf_SCcharmet_t *);
  147. //static int gzcatfile(char *);
  148. void init_putPS(void);
  149. unsigned long * Xrealloc(unsigned long *, int);
  150. unsigned long * Xalloc(int);
  151. int BufFileRead (BufFilePtr, char *, int);
  152. void pcf_postscript(ucs4_t c, pcf_bm_t *, pcf_charmet_t *, pcf_SCcharmet_t *);
  153. int pres_pcfbm(ucs4_t *, pcffont_t *, pcf_bm_t **, pcf_charmet_t *, pcf_SCcharmet_t *, int);
  154. int wide_char_width(wchar_t ucs);
  155. int load_pcf_font(pcffont_t *font);
  156. void scaling_factors(pcffont_t *font, double ptsz, int Xres, int Yres);
  157. void scale_Fmetrics(pcffont_t *font);
  158. void Generate_new_font(pcffont_t *font);
  159. void Generate_new_font_with_header(pcffont_t *font);
  160. double SPACINGwidth = -1;
  161. ucs4_t SPACINGchar = (ucs4_t) 0x20;
  162. ucs4_t REFERENCEchar = (ucs4_t) 0x20;
  163. /* extern pcffont_t *pcf_fonts;
  164. extern pcffont_t *CurrentFont; */
  165. //static pcffont_t *pcf_fonts;
  166. static pcffont_t *CurrentFont;
  167. static int position;
  168. static CharInfoRec nonExistantChar;
  169. static AtomListPtr *hashTable;
  170. static int hashMask;
  171. static int hashSize, hashUsed;
  172. //static int hashMask;
  173. static int rehash;
  174. extern char sOutFilename[256];
  175. extern int nFontCount;
  176. /*
  177. static ucs4_t pcf_bmap[4][UCS4_MAXVAL/sizeof(ucs4_t)];
  178. static int dictcnt = 0;
  179. */
  180. static unsigned char _reverse_byte[0x100] = {
  181. 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
  182. 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
  183. 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
  184. 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
  185. 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
  186. 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
  187. 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
  188. 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
  189. 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
  190. 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
  191. 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
  192. 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
  193. 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
  194. 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
  195. 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
  196. 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
  197. 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
  198. 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
  199. 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
  200. 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
  201. 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
  202. 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
  203. 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
  204. 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
  205. 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
  206. 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
  207. 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
  208. 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
  209. 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
  210. 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
  211. 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
  212. 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
  213. };
  214. void
  215. draw_bitmap(int line_ascent, int line_descent, int line_space, pcf_bm_t *bitmap, pcf_charmet_t *Cmetrics)
  216. {
  217. //int i;
  218. int j;
  219. int k;
  220. int m;
  221. int n;
  222. if (bitmap == NULL)
  223. return;
  224. fprintf(stdout,"[ %d %d %d %d %d %d %d %d %d ]\n",
  225. Cmetrics->width, Cmetrics->height,
  226. Cmetrics->widthBits, Cmetrics->widthBytes,
  227. Cmetrics->ascent, Cmetrics->descent,
  228. Cmetrics->LSBearing, Cmetrics->RSBearing,
  229. Cmetrics->origin_xoff);
  230. k = Cmetrics->widthBytes * Cmetrics->height;
  231. for (j = 0; j < line_ascent - Cmetrics->ascent; j++)
  232. {
  233. for (m = 0; m <= Cmetrics->width; m++)
  234. fprintf(stdout, ".");
  235. fprintf(stdout, "\n");
  236. }
  237. m = 0;
  238. for (j = 0; j < k; j++)
  239. {
  240. n = 7;
  241. while (n >= 0)
  242. {
  243. if (m >= Cmetrics->widthBits)
  244. {
  245. if (m <= Cmetrics->width)
  246. {
  247. fprintf(stdout, ".");
  248. m++;
  249. }
  250. fprintf(stdout, "\n");
  251. m = 0;
  252. }
  253. if (m <= Cmetrics->width)
  254. {
  255. if (bitmap[j] & (1<<n))
  256. fprintf(stdout, "*");
  257. else
  258. fprintf(stdout, ".");
  259. }
  260. m++;
  261. n--;
  262. }
  263. n = 0;
  264. }
  265. if (m <= Cmetrics->width)
  266. {
  267. fprintf(stdout, ".");
  268. m++;
  269. }
  270. fprintf(stdout, "\n");
  271. /*
  272. dictcnt++;
  273. */
  274. }
  275. void
  276. put_PSbitmap(ucs4_t code, pcf_bm_t *bitmap,
  277. pcf_charmet_t *Cmetrics, pcf_SCcharmet_t *Smetrics)
  278. {
  279. int j;
  280. int k;
  281. if (bitmap == NULL)
  282. return;
  283. fprintf(stdout,"/C%x { GR %.2f %.2f S %d %d T [%d 0 0 -%d 0 %d]\n\t{<",
  284. code,
  285. Smetrics->widthBits, Smetrics->height,
  286. Cmetrics->widthBits, Cmetrics->height,
  287. Cmetrics->widthBits, Cmetrics->height,
  288. Cmetrics->ascent);
  289. k = Cmetrics->widthBytes * Cmetrics->height;
  290. #ifdef SDEBUG
  291. fprintf(stderr, "%d is -------Cmetrics->widthBytes for :C%x \n", Cmetrics->widthBytes, code);
  292. fprintf(stderr, "%d is -------Cmetrics->height for :C%x \n", Cmetrics->height, code);
  293. fprintf(stderr, "%d is ------ no. of glyph elems for:C%x \n", k, code);
  294. #endif
  295. for (j = 0; j < k; j++)
  296. fprintf(stdout,"%.2x", bitmap[j]);
  297. fprintf(stdout,">} IG } def\n");
  298. fprintf(stdout,"C%x\n", code);
  299. /*
  300. dictcnt++;
  301. */
  302. }
  303. pcf_SCcharmet_t *
  304. get_SCmetrics(pcffont_t *font, ucs4_t val)
  305. {
  306. ucs4_t v;
  307. pcf_bm_t *bm;
  308. pcf_charmet_t cm;
  309. static pcf_SCcharmet_t sm;
  310. /* map ucs4 val to a 'pcf_fonts' array index */
  311. v = val;
  312. if ( pres_pcfbm(&v, font, &bm, &cm, &sm, 1) < 0) {
  313. return NULL;
  314. } else if (v != val) { /* return NULL if val got mapped to */
  315. /* another character */
  316. return NULL;
  317. } else {
  318. return &sm;
  319. }
  320. }
  321. void
  322. pcf_postscript(ucs4_t c, pcf_bm_t *pcfbm,
  323. pcf_charmet_t *Cmetrics, pcf_SCcharmet_t *scCmetrics)
  324. {
  325. put_PSbitmap(c, pcfbm, Cmetrics, scCmetrics);
  326. }
  327. int
  328. load_pcf( pcffont_t *font ) {
  329. #ifdef SDEBUG
  330. fprintf(stderr, "%f --- target_ptsz\n", target_ptsz);
  331. #endif
  332. if (load_pcf_font(font) == -1) {
  333. /*
  334. err_exit(catgets(cat_fd, WARN_SET,2,\
  335. "%s: cannot load font file (%s)\n"), progname,
  336. pcf_fonts[ndx].file);
  337. */
  338. return -1;
  339. }
  340. scaling_factors(font, 1, 1, 1);
  341. scale_Fmetrics(font);
  342. font->loaded = 1;
  343. font->cuf = NULL;
  344. CurrentFont = font;
  345. return 0;
  346. }
  347. int
  348. load_pcf_font(pcffont_t *font)
  349. {
  350. FontRec fr;
  351. FontInfoPtr fi;
  352. FontFilePtr ff;
  353. ulong_t value;
  354. if ((ff = FontFileOpen(font->file)) == NULL)
  355. return -1;
  356. if (pcfReadFont(&fr, ff, MSBFirst, MSBFirst, 1, 1) != Successful)
  357. return -1;
  358. fi = &(fr.info);
  359. font->Fmetrics.ascent = fi->fontAscent;
  360. font->Fmetrics.descent = fi->fontDescent;
  361. font->Fmetrics.linespace = fi->fontAscent + fi->fontDescent;
  362. font->Fmetrics.firstchar = (fi->firstRow * 256) + fi->firstCol;
  363. font->Fmetrics.lastchar = (fi->lastRow * 256) + fi->lastCol;
  364. font->Fmetrics.lastCol = fi->lastCol;
  365. font->Fmetrics.firstCol = fi->firstCol;
  366. font->bitmaps = ((BitmapFontPtr)(fr.fontPrivate))->encoding;
  367. printf("firstchar:%d,lastchar:%d\n",font->Fmetrics.firstchar, font->Fmetrics.lastchar);
  368. //Generate_new_font(font);
  369. Generate_new_font_with_header(font);
  370. if (get_font_property(&fr, "POINT_SIZE", &value) == -1)
  371. return -1;
  372. font->Fmetrics.ptsz = (int) value;
  373. if (get_font_property(&fr, "RESOLUTION_X", &value) == -1)
  374. return -1;
  375. font->Fmetrics.Xres = (int) value;
  376. if (get_font_property(&fr, "RESOLUTION_Y", &value) == -1)
  377. return -1;
  378. font->Fmetrics.Yres = (int) value;
  379. #ifdef SDEBUG
  380. fprintf(stderr,"Fmetrics: ascent:%d, descent:%d, linespace:%d, "
  381. "ptsz:%d, Xres:%d, Yres:%d\n", font->Fmetrics.ascent, font->Fmetrics.descent,
  382. (&(font->Fmetrics))->linespace, (&(font->Fmetrics))->ptsz, (&(font->Fmetrics))->Xres, (&(font->Fmetrics))->Yres);
  383. #endif
  384. debug(dump_Fmetrics(&(font->Fmetrics)));
  385. FontFileClose(ff);
  386. return 0;
  387. }
  388. void Generate_new_font(pcffont_t *font)
  389. {
  390. int i,width,height,widthBytes,widthBits,LSBearing,RSBearing,ascent,descent;
  391. CharInfoPtr ci;
  392. char *buf;
  393. FILE *fd;
  394. font_bmf font_create;
  395. char name[256];
  396. int offset = 0;
  397. int count = 0;
  398. int font_count;
  399. strcpy(name,sOutFilename);
  400. font_count = font->Fmetrics.lastchar;
  401. buf = (char*)malloc(font_count*sizeof(font_bmf));
  402. memset(buf,0,font_count*sizeof(font_bmf));
  403. for(i=0; i<font_count;i++)
  404. {
  405. ci = font->bitmaps[i];
  406. //printf("before memset font_create\n");
  407. memset(&font_create,0,sizeof(font_bmf));
  408. if(ci != NULL)
  409. {
  410. width = GLYPHWIDTHPIXELS(ci);
  411. height = GLYPHHEIGHTPIXELS(ci);
  412. widthBytes = GLYPHWIDTHBYTES(ci);
  413. widthBits = GLYPHWIDTHBYTES(ci) * NBPB;
  414. LSBearing = ci->metrics.leftSideBearing;
  415. RSBearing = ci->metrics.rightSideBearing;
  416. ascent = ci->metrics.ascent;
  417. descent = ci->metrics.descent;
  418. /*if(i==0)
  419. {
  420. width = font->Fmetrics.linespace;
  421. height = font->Fmetrics.ascent;
  422. widthBytes = font->Fmetrics.descent;
  423. }*/
  424. if(i==32 && width==0)
  425. {
  426. width = 2;
  427. height = 1;
  428. widthBytes = 1;
  429. widthBits = 8;
  430. RSBearing = 2;
  431. }
  432. font_create.width = (INT8)width;
  433. font_create.height = (INT8)height;
  434. font_create.widthBytes =(INT8)widthBytes;
  435. font_create.widthBits =(INT8)widthBits;
  436. font_create.ascent =(INT8)ascent;
  437. font_create.descent =(INT8)descent;
  438. font_create.LSBearing = (INT8)LSBearing;
  439. font_create.RSBearing = (INT8)RSBearing;
  440. //printf("before memcpy bitmap\n");
  441. //printf("char i:%d,width:%d,height:%d,widthBytes:%d,widthBits:%d,LSBearing:%d,RSBearing:%d,bitmap:%p\n",i,width,height,widthBytes,widthBits,LSBearing,RSBearing,ci->bits);
  442. if((widthBytes*height)>48)
  443. {
  444. //free(buf);
  445. printf("widthBytes*height>=48\n");
  446. memcpy(font_create.bitmap,ci->bits,48);
  447. //return;
  448. }
  449. else
  450. memcpy(font_create.bitmap,ci->bits,widthBytes*height);
  451. }
  452. count++;
  453. memcpy(buf+offset,&font_create,sizeof(font_bmf));
  454. offset+=sizeof(font_bmf);
  455. if(i>nFontCount)
  456. break;
  457. }
  458. printf("count is:%d, before opening file: %s\n",count,name);
  459. fd = fopen(name, "wb");
  460. if(fd!=NULL)
  461. {
  462. size_t bs = fwrite(buf,1,count*sizeof(font_bmf),fd);
  463. assert(bs==count*sizeof(font_bmf));
  464. }
  465. fclose(fd);
  466. free(buf);
  467. }
  468. void Generate_new_font_with_header(pcffont_t *font)
  469. {
  470. int i,width,height,widthBytes,widthBits,LSBearing,RSBearing,ascent,descent;
  471. CharInfoPtr ci;
  472. char *buf;
  473. FILE *fd;
  474. font_bmf font_create;
  475. char name[256];
  476. int offset = 0;
  477. int count = 0;
  478. //int last_char = 0;
  479. //int bitmap_offset = 0;
  480. int header_len = 0;
  481. //int last_val = 0;
  482. //int bmf_buffer_len = 0;
  483. font_bmf_header font_header_t;
  484. strcpy(name,sOutFilename);
  485. //font_count = font->Fmetrics.lastchar;
  486. font_header_t.ascent = font->Fmetrics.ascent;
  487. font_header_t.descent = font->Fmetrics.descent;
  488. font_header_t.linespace = font->Fmetrics.linespace;
  489. //font_header_t.last_char = font->Fmetrics.lastchar;
  490. count = font->Fmetrics.lastchar;
  491. buf = (char*)malloc(count*sizeof(font_bmf)+sizeof(font_bmf_header));
  492. memset(buf,0,count*sizeof(font_bmf)+sizeof(font_bmf_header));
  493. memcpy(buf+offset,&font_header_t,sizeof(font_header_t));
  494. offset+=sizeof(font_header_t);
  495. header_len = sizeof(font_header_t);
  496. for(i=0; i<count;i++)
  497. {
  498. ci = font->bitmaps[i];
  499. memset(&font_create,0,sizeof(font_bmf));
  500. if(ci != NULL)
  501. {
  502. width = GLYPHWIDTHPIXELS(ci);
  503. height = GLYPHHEIGHTPIXELS(ci);
  504. widthBytes = GLYPHWIDTHBYTES(ci);
  505. widthBits = GLYPHWIDTHBYTES(ci) * NBPB;
  506. LSBearing = ci->metrics.leftSideBearing;
  507. RSBearing = ci->metrics.rightSideBearing;
  508. ascent = ci->metrics.ascent;
  509. descent = ci->metrics.descent;
  510. if(i==32 && width==0)
  511. {
  512. width = 2;
  513. height = 1;
  514. widthBytes = 1;
  515. widthBits = 8;
  516. RSBearing = 2;
  517. }
  518. font_create.width = (INT8)width;
  519. font_create.height = (INT8)height;
  520. font_create.widthBytes =(INT8)widthBytes;
  521. font_create.widthBits =(INT8)widthBits;
  522. font_create.ascent =(INT8)ascent;
  523. font_create.descent =(INT8)descent;
  524. font_create.LSBearing = (INT8)LSBearing;
  525. font_create.RSBearing = (INT8)RSBearing;
  526. //printf("char:%d,height:%d,width:%d,widthBytes:%d\n",
  527. //i,font_create.width,font_create.height,font_create.widthBytes);
  528. if(font_create.height<0)
  529. font_create.height = 0;
  530. if(font_create.widthBytes<0)
  531. font_create.widthBytes = 0;
  532. if((widthBytes*height)>48)
  533. {
  534. //printf("widthBytes*height>=48\n");
  535. memcpy(font_create.bitmap,ci->bits,48);
  536. }
  537. else
  538. memcpy(font_create.bitmap,ci->bits,widthBytes*height);
  539. }
  540. memcpy(buf+offset,&font_create,sizeof(font_bmf));
  541. offset+=sizeof(font_bmf);
  542. }
  543. printf("count is:%d, before opening file: %s\n",count,name);
  544. fd = fopen(name, "wb");
  545. if(fd!=NULL)
  546. {
  547. size_t bs = fwrite(buf,1,count*sizeof(font_bmf)+header_len,fd);
  548. assert(bs == count*sizeof(font_bmf)+header_len);
  549. }
  550. fclose(fd);
  551. free(buf);
  552. }
  553. /*void Generate_new_font_with_header(pcffont_t *font)
  554. {
  555. int i,width,height,widthBytes,widthBits,LSBearing,RSBearing,ascent,descent;
  556. CharInfoPtr ci;
  557. char *buf;
  558. FILE *fd;
  559. font_bmf_ex font_create;
  560. char name[256];
  561. int offset = 0;
  562. int count = 0;
  563. int last_char = 0;
  564. int bitmap_offset = 0;
  565. int header_len = 0;
  566. int last_val = 0;
  567. int bmf_buffer_len = 0;
  568. font_bmf_header font_header_t;
  569. strcpy(name,sOutFilename);
  570. //font_count = font->Fmetrics.lastchar;
  571. font_header_t.ascent = font->Fmetrics.ascent;
  572. font_header_t.descent = font->Fmetrics.descent;
  573. font_header_t.linespace = font->Fmetrics.linespace;
  574. font_header_t.last_char = font->Fmetrics.lastchar;
  575. count = font->Fmetrics.lastchar;
  576. for(i=0; i<count;i++)
  577. {
  578. ci = font->bitmaps[i];
  579. if(ci != NULL)
  580. {
  581. width = GLYPHWIDTHPIXELS(ci);
  582. height = GLYPHHEIGHTPIXELS(ci);
  583. widthBytes = GLYPHWIDTHBYTES(ci);
  584. if((widthBytes*height)>last_val)
  585. last_val = widthBytes*height;
  586. }
  587. }
  588. printf("last val:%d\n",last_val);
  589. last_val = 64;
  590. font_header_t.bmp_buffer_len = last_val;
  591. bmf_buffer_len = last_val;
  592. buf = (char*)malloc(count*(sizeof(font_bmf_ex)+bmf_buffer_len)+sizeof(font_bmf_header));
  593. memset(buf,0,count*(sizeof(font_bmf_ex)+bmf_buffer_len)+sizeof(font_bmf_header));
  594. //printf("pcf_font_header size is:%d,total size is:%d\n",sizeof(pcf_font_header),count*sizeof(font_bmf_ex)+sizeof(font_bmf_header));
  595. memcpy(buf+offset,&font_header_t,sizeof(font_header_t));
  596. offset+=sizeof(font_header_t);
  597. header_len = sizeof(font_bmf_ex)*count+sizeof(font_header_t);
  598. for(i=0; i<count;i++)
  599. {
  600. ci = font->bitmaps[i];
  601. //printf("before memset font_create\n");
  602. memset(&font_create,0,sizeof(font_bmf_ex));
  603. if(ci != NULL)
  604. {
  605. width = GLYPHWIDTHPIXELS(ci);
  606. height = GLYPHHEIGHTPIXELS(ci);
  607. widthBytes = GLYPHWIDTHBYTES(ci);
  608. widthBits = GLYPHWIDTHBYTES(ci) * NBPB;
  609. LSBearing = ci->metrics.leftSideBearing;
  610. RSBearing = ci->metrics.rightSideBearing;
  611. ascent = ci->metrics.ascent;
  612. descent = ci->metrics.descent;
  613. if(i==32 && width==0)
  614. {
  615. width = 2;
  616. height = 1;
  617. widthBytes = 1;
  618. widthBits = 8;
  619. RSBearing = 2;
  620. }
  621. font_create.width = (INT8)width;
  622. font_create.height = (INT8)height;
  623. font_create.widthBytes =(INT8)widthBytes;
  624. font_create.widthBits =(INT8)widthBits;
  625. font_create.ascent =(INT8)ascent;
  626. font_create.descent =(INT8)descent;
  627. font_create.LSBearing = (INT8)LSBearing;
  628. font_create.RSBearing = (INT8)RSBearing;
  629. if(width==0 || height==0 || widthBytes==0)
  630. font_create.pos = 0;
  631. else
  632. {
  633. if((widthBytes*height)>bmf_buffer_len)
  634. memcpy(buf+header_len+bitmap_offset,ci->bits,bmf_buffer_len);
  635. else
  636. memcpy(buf+header_len+bitmap_offset,ci->bits,widthBytes*height);
  637. font_create.pos = header_len+bitmap_offset;//record char pos
  638. bitmap_offset+=bmf_buffer_len;
  639. }
  640. }
  641. memcpy(buf+offset,&font_create,sizeof(font_bmf_ex));
  642. offset+=sizeof(font_bmf_ex);
  643. }
  644. printf("count is:%d,before open file\n",count);
  645. fd = fopen(name, "wb");
  646. if(fd!=NULL)
  647. fwrite(buf,1,header_len+bitmap_offset,fd);
  648. fclose(fd);
  649. free(buf);
  650. }*/
  651. /*void Generate_new_font_with_header(pcffont_t *font)
  652. {
  653. int i,width,height,widthBytes,widthBits,LSBearing,RSBearing,ascent,descent;
  654. CharInfoPtr ci;
  655. char *buf;
  656. FILE *fd;
  657. font_bmf_ex font_create;
  658. char name[256];
  659. int offset = 0;
  660. int count = 0;
  661. int last_char = 0;
  662. int bitmap_offset = 0;
  663. int header_len = 0;
  664. int last_val = 0;
  665. font_bmf_header font_header_t;
  666. strcpy(name,sOutFilename);
  667. //font_count = font->Fmetrics.lastchar;
  668. font_header_t.ascent = font->Fmetrics.ascent;
  669. font_header_t.descent = font->Fmetrics.descent;
  670. font_header_t.linespace = font->Fmetrics.linespace;
  671. font_header_t.last_char = font->Fmetrics.lastchar;
  672. count = font->Fmetrics.lastchar;
  673. for(i=0; i<count;i++)
  674. {
  675. ci = font->bitmaps[i];
  676. if(ci != NULL)
  677. {
  678. width = GLYPHWIDTHPIXELS(ci);
  679. height = GLYPHHEIGHTPIXELS(ci);
  680. widthBytes = GLYPHWIDTHBYTES(ci);
  681. if(widthBytes*height>last_val)
  682. {
  683. last_val = widthBytes*height;
  684. //printf("char:%d,width:%d,height:%d,widthBytes:%d\n",i,width,height,widthBytes);
  685. }
  686. }
  687. }
  688. printf("last char:%d\n",last_char);
  689. font_header_t.bmp_buffern_len = last_val;
  690. buf = (char*)malloc(count*(sizeof(font_bmf_ex)+64)+sizeof(font_bmf_header));
  691. memset(buf,0,count*(sizeof(font_bmf_ex)+64)+sizeof(font_bmf_header));
  692. //printf("pcf_font_header size is:%d,total size is:%d\n",sizeof(pcf_font_header),count*sizeof(font_bmf_ex)+sizeof(font_bmf_header));
  693. memcpy(buf+offset,&font_header_t,sizeof(font_header_t));
  694. offset+=sizeof(font_header_t);
  695. header_len = sizeof(font_bmf_ex)*count+sizeof(font_header_t);
  696. for(i=0; i<count;i++)
  697. {
  698. ci = font->bitmaps[i];
  699. //printf("before memset font_create\n");
  700. memset(&font_create,0,sizeof(font_bmf_ex));
  701. if(ci != NULL)
  702. {
  703. width = GLYPHWIDTHPIXELS(ci);
  704. height = GLYPHHEIGHTPIXELS(ci);
  705. widthBytes = GLYPHWIDTHBYTES(ci);
  706. widthBits = GLYPHWIDTHBYTES(ci) * NBPB;
  707. LSBearing = ci->metrics.leftSideBearing;
  708. RSBearing = ci->metrics.rightSideBearing;
  709. ascent = ci->metrics.ascent;
  710. descent = ci->metrics.descent;
  711. if(i==32 && width==0)
  712. {
  713. width = 2;
  714. height = 1;
  715. widthBytes = 1;
  716. widthBits = 8;
  717. RSBearing = 2;
  718. }
  719. font_create.width = (INT8)width;
  720. font_create.height = (INT8)height;
  721. font_create.widthBytes =(INT8)widthBytes;
  722. font_create.widthBits =(INT8)widthBits;
  723. font_create.ascent =(INT8)ascent;
  724. font_create.descent =(INT8)descent;
  725. font_create.LSBearing = (INT8)LSBearing;
  726. font_create.RSBearing = (INT8)RSBearing;
  727. if(width==0 || height==0 || widthBytes==0)
  728. font_create.pos = 0;
  729. else
  730. {
  731. if((widthBytes*height)>64)
  732. memcpy(buf+header_len+bitmap_offset,ci->bits,64);
  733. else
  734. memcpy(buf+header_len+bitmap_offset,ci->bits,widthBytes*height);
  735. font_create.pos = header_len+bitmap_offset;//record char pos
  736. bitmap_offset+=64;
  737. }
  738. }
  739. memcpy(buf+offset,&font_create,sizeof(font_bmf_ex));
  740. offset+=sizeof(font_bmf_ex);
  741. }
  742. printf("count is:%d,before open file\n",count);
  743. fd = fopen(name, "wb");
  744. if(fd!=NULL)
  745. fwrite(buf,1,header_len+bitmap_offset,fd);
  746. fclose(fd);
  747. free(buf);
  748. }*/
  749. /*void Generate_new_font_with_header(pcffont_t *font)
  750. {
  751. int i,width,height,widthBytes,widthBits,LSBearing,RSBearing,ascent,descent;
  752. CharInfoPtr ci;
  753. char *buf;
  754. FILE *fd;
  755. font_bmf font_create;
  756. char name[256];
  757. int offset = 0;
  758. int count = 0;
  759. font_bmf_header font_header_t;
  760. strcpy(name,sOutFilename);
  761. //font_count = font->Fmetrics.lastchar;
  762. font_header_t.ascent = font->Fmetrics.ascent;
  763. font_header_t.descent = font->Fmetrics.descent;
  764. font_header_t.linespace = font->Fmetrics.linespace;
  765. //font_header_t.ptsz = font->Fmetrics.ptsz;
  766. //font_header_t.ptsz = font->Fmetrics.ptsz;
  767. //font_header_t.Yres = font->Fmetrics.Yres;
  768. buf = (char*)malloc(65535*sizeof(font_bmf)+sizeof(font_bmf_header));
  769. memset(buf,0,65535*sizeof(font_bmf)+sizeof(font_bmf_header));
  770. printf("pcf_font_header size is:%d,total size is:%d\n",sizeof(pcf_font_header),65535*sizeof(font_bmf)+sizeof(font_bmf_header));
  771. memcpy(buf+offset,&font_header_t,sizeof(font_header_t));
  772. offset+=sizeof(font_header_t);
  773. for(i=0; i<65535;i++)
  774. {
  775. ci = font->bitmaps[i];
  776. //printf("before memset font_create\n");
  777. memset(&font_create,0,sizeof(font_bmf));
  778. if(ci != NULL)
  779. {
  780. width = GLYPHWIDTHPIXELS(ci);
  781. height = GLYPHHEIGHTPIXELS(ci);
  782. widthBytes = GLYPHWIDTHBYTES(ci);
  783. widthBits = GLYPHWIDTHBYTES(ci) * NBPB;
  784. LSBearing = ci->metrics.leftSideBearing;
  785. RSBearing = ci->metrics.rightSideBearing;
  786. ascent = ci->metrics.ascent;
  787. descent = ci->metrics.descent;
  788. font_create.width = (INT8)width;
  789. font_create.height = (INT8)height;
  790. font_create.widthBytes =(INT8)widthBytes;
  791. font_create.widthBits =(INT8)widthBits;
  792. font_create.ascent =(INT8)ascent;
  793. font_create.descent =(INT8)descent;
  794. font_create.LSBearing = (INT8)LSBearing;
  795. font_create.RSBearing = (INT8)RSBearing;
  796. //printf("before memcpy bitmap\n");
  797. //printf("char i:%d,width:%d,height:%d,widthBytes:%d,widthBits:%d,LSBearing:%d,RSBearing:%d,bitmap:%x\n",i,width,height,widthBytes,widthBits,LSBearing,RSBearing,ci->bits);
  798. if((widthBytes*height)>64)
  799. {
  800. //free(buf);
  801. printf("char:%d,widthBytes:%d*height:%d>=64:%d\n",i,widthBytes,height,widthBytes*height);
  802. memcpy(font_create.bitmap,ci->bits,64);
  803. //return;
  804. }
  805. else
  806. memcpy(font_create.bitmap,ci->bits,widthBytes*height);
  807. count++;
  808. }
  809. memcpy(buf+offset,&font_create,sizeof(font_bmf));
  810. offset+=sizeof(font_bmf);
  811. }
  812. printf("count is:%d,before open file\n",count);
  813. fd = fopen(name, "wb");
  814. if(fd!=NULL)
  815. fwrite(buf,1,65535*sizeof(font_bmf)+sizeof(font_header_t),fd);
  816. fclose(fd);
  817. free(buf);
  818. }*/
  819. void
  820. scaling_factors(pcffont_t *font, double ptsz, int Xres, int Yres)
  821. {
  822. font->Yscale = 1;
  823. font->Xscale = 1;
  824. // font->Yscale = ((ptsz * (double)Yres)
  825. // / ((double)font->Fmetrics.ptsz * (double)font->Fmetrics.Xres));
  826. //
  827. // font->Xscale = ((((double)Xres * (double)font->Fmetrics.Yres)
  828. // / ((double)Yres * (double)font->Fmetrics.Xres))
  829. // * (font->Yscale));
  830. #if 0
  831. if (xsflag == OPTARG_SET)
  832. font->Xscale = xs_argval;
  833. else if (xsflag == OPTARG_ADD)
  834. font->Xscale += xs_argval;
  835. else if (xsflag == OPTARG_SUB) /* for clarity, we subract a positive */
  836. font->Xscale -= xs_argval; /* rather than add a negative. */
  837. if (ysflag == OPTARG_SET)
  838. font->Yscale = ys_argval;
  839. else if (ysflag == OPTARG_ADD)
  840. font->Yscale += ys_argval;
  841. else if (ysflag == OPTARG_SUB)
  842. font->Yscale -= ys_argval;
  843. #endif
  844. #ifdef SDEBUG
  845. fprintf(stderr,"%f -- font->Yscale\n %f -- font->Xscale\n", font->Yscale, font->Xscale);
  846. #endif
  847. }
  848. void
  849. scale_Fmetrics(pcffont_t *font) {
  850. font->scFmetrics.ascent = font->Fmetrics.ascent * font->Yscale;
  851. font->scFmetrics.descent = font->Fmetrics.descent * font->Yscale;
  852. font->scFmetrics.linespace = font->Fmetrics.linespace * font->Yscale;
  853. #ifdef SDEBUG
  854. fprintf(stderr,"%f font->scFmetrics.ascent\n \
  855. %f font->scFmetrics.descent\n\
  856. %f font->scFmetrics.linespace\n", font->scFmetrics.ascent, font->scFmetrics.descent, font->scFmetrics.linespace);
  857. #endif
  858. }
  859. void
  860. scale_Cmetrics(pcf_charmet_t *Cm, pcf_SCcharmet_t *Sm)
  861. {
  862. Sm->width = Cm->width * CurrentFont->Xscale;
  863. Sm->height = Cm->height * CurrentFont->Yscale;
  864. Sm->widthBits = Cm->widthBits * CurrentFont->Xscale;
  865. Sm->ascent = Cm->ascent * CurrentFont->Yscale;
  866. Sm->descent = Cm->descent * CurrentFont->Yscale;
  867. Sm->origin_xoff = Cm->origin_xoff * CurrentFont->Xscale;
  868. #ifdef SDEBUG
  869. fprintf(stderr, "%f is CurrentFont->Xscale\n", CurrentFont->Xscale);
  870. fprintf(stderr, "%f is CurrentFont->Yscale\n", CurrentFont->Yscale);
  871. fprintf(stderr, "%d is Cm->widthBits\n", Cm->widthBits);
  872. fprintf(stderr, "%d is Cm->height\n", Cm->height);
  873. fprintf(stderr, "%f is Sm->widthBits\n", Sm->widthBits);
  874. fprintf(stderr, "%f is Sm->height\n", Sm->height);
  875. #endif
  876. /*
  877. debug(dump_scCmetrics(Sm));
  878. */
  879. }
  880. int
  881. pres_pcfbm(ucs4_t *val, pcffont_t *font, pcf_bm_t **bitmap,
  882. pcf_charmet_t *Cmetrics, pcf_SCcharmet_t *scCmetrics, int dontcache)
  883. {
  884. ucs4_t v;
  885. #if SDEBUG
  886. fprintf(stderr, "0x%08x -- ucs4val \n", *val);
  887. #endif
  888. v = *val;
  889. if (is_motion_char(v)) {
  890. *bitmap = NULL;
  891. return XU_MOTION_CHAR;
  892. }
  893. if (non_graphic_char(v)) {
  894. *bitmap = NULL;
  895. return XU_IGNORE;
  896. }
  897. if ((*bitmap = (pcf_bm_t *)xpcf_getcbm(v, font, Cmetrics)) == NULL) {
  898. if (handle_nobitmap(&v, font, Cmetrics, bitmap) == XU_IGNORE)
  899. return XU_IGNORE;
  900. }
  901. #if SDEBUG
  902. fprintf(stderr, "0x%02x -- **bitmap \n", (unsigned char)**bitmap);
  903. #endif
  904. *val = v;
  905. scale_Cmetrics(Cmetrics, scCmetrics);
  906. return(1);
  907. }
  908. #ifdef DEBUG
  909. static void
  910. dump_Cmetrics(pcf_charmet_t *cm)
  911. {
  912. dprintf2("cmetrics: ascent:%d, descent:%d, ", cm->ascent, cm->descent);
  913. dprintf3("width:%d, height:%d, widthBytes:%d, ",
  914. cm->width, cm->height, cm->widthBytes);
  915. dprintf3("LSB:%d,RSB: %d, xoff:%d\n ",
  916. cm->LSBearing, cm->RSBearing, cm->origin_xoff);
  917. }
  918. static void
  919. dump_Fmetrics(pcffont_t *fm)
  920. {
  921. dprintf6("Fmetrics: ascent:%d, descent:%d, linespace:%d, "
  922. "ptsz:%d, Xres:%d, Yres:%d\n", fm->ascent, fm->descent,
  923. fm->linespace, fm->ptsz, fm->Xres, fm->Yres);
  924. }
  925. #endif
  926. unsigned long *
  927. Xrealloc (unsigned long *n, int m)
  928. {
  929. if (!n)
  930. return (unsigned long *) malloc (m);
  931. else
  932. return (unsigned long *) realloc ((char *) n, m);
  933. }
  934. unsigned long *
  935. Xalloc (int m)
  936. {
  937. return (unsigned long *) malloc (m);
  938. }
  939. /*
  940. * Invert byte order within each 16-bits of an array.
  941. */
  942. void
  943. TwoByteSwap(unsigned char *buf, int nbytes)
  944. {
  945. register unsigned char c;
  946. for (; nbytes > 0; nbytes -= 2, buf += 2)
  947. {
  948. c = buf[0];
  949. buf[0] = buf[1];
  950. buf[1] = c;
  951. }
  952. }
  953. /*
  954. * Invert byte order within each 32-bits of an array.
  955. */
  956. static void
  957. FourByteSwap(unsigned char *buf, int nbytes)
  958. {
  959. register unsigned char c;
  960. for (; nbytes > 0; nbytes -= 4, buf += 4)
  961. {
  962. c = buf[0];
  963. buf[0] = buf[3];
  964. buf[3] = c;
  965. c = buf[1];
  966. buf[1] = buf[2];
  967. buf[2] = c;
  968. }
  969. }
  970. #if 0
  971. static int
  972. handle_nongraphchar(ucs4_t *val, int *ndx)
  973. {
  974. return XU_IGNORE;
  975. }
  976. #endif
  977. static Bool
  978. pcfHasType (PCFTablePtr tables, int ntables, CARD32 type)
  979. {
  980. int i;
  981. for (i = 0; i < ntables; i++)
  982. if (tables[i].type == type)
  983. return TRUE;
  984. return FALSE;
  985. }
  986. /*
  987. * Invert bit order within each BYTE of an array.
  988. */
  989. static void
  990. BitOrderInvert(unsigned char *buf, int nbytes)
  991. {
  992. register unsigned char *rev = _reverse_byte;
  993. for (; --nbytes >= 0; buf++)
  994. *buf = rev[*buf];
  995. }
  996. static pcf_bm_t *
  997. xpcf_getcbm(ucs4_t code, pcffont_t *font, pcf_charmet_t *Cmetrics)
  998. {
  999. int j;
  1000. CharInfoPtr ci;
  1001. assert(font->loaded);
  1002. /*
  1003. * For the default no glyph character to appear in
  1004. * output stream
  1005. */
  1006. onemoretime:
  1007. if (code < font->Fmetrics.firstchar || code > font->Fmetrics.lastchar)
  1008. return NULL;
  1009. j = (code - (((code - font->Fmetrics.firstchar) / 256)
  1010. * (font->Fmetrics.firstCol + (255 - font->Fmetrics.lastCol))))
  1011. - font->Fmetrics.firstchar;
  1012. #ifdef SDEBUG
  1013. fprintf(stderr, "%d is -- codewidth of C%x\n", wide_char_width(code), code);
  1014. fprintf(stderr, "%d is -- j value C%x\n", wide_char_width(code), code);
  1015. #endif
  1016. assert(j >= 0);
  1017. if (font->bitmaps[j] == NULL)
  1018. {
  1019. /*
  1020. * code added to replace the codepoints with
  1021. * no-glyph code
  1022. */
  1023. if (wide_char_width(code) == -1)
  1024. {
  1025. code = 0;
  1026. goto onemoretime;
  1027. }
  1028. return NULL;
  1029. }
  1030. ci = font->bitmaps[j];
  1031. Cmetrics->width = GLYPHWIDTHPIXELS(ci);
  1032. Cmetrics->height = GLYPHHEIGHTPIXELS(ci);
  1033. Cmetrics->widthBytes = GLYPHWIDTHBYTES(ci);
  1034. Cmetrics->widthBits = GLYPHWIDTHBYTES(ci) * NBPB;
  1035. Cmetrics->ascent = ci->metrics.ascent;
  1036. Cmetrics->descent = ci->metrics.descent;
  1037. Cmetrics->LSBearing = ci->metrics.leftSideBearing;
  1038. Cmetrics->RSBearing = ci->metrics.rightSideBearing;
  1039. Cmetrics->origin_xoff = ci->metrics.characterWidth;
  1040. #ifdef SDEBUG
  1041. fprintf(stderr, "%d is -- Cmetrics->widthBytes of C%x\n", Cmetrics->widthBytes, code);
  1042. fprintf(stderr, "%d is -- Cmetrics->height value of C%x\n", Cmetrics->height, code);
  1043. #endif
  1044. #if 0
  1045. if (cwflag) {
  1046. if (cwflag == OPTARG_ADD)
  1047. Cmetrics->origin_xoff += cw_argval;
  1048. else if (cwflag == OPTARG_SUB)
  1049. Cmetrics->origin_xoff -= cw_argval;
  1050. else if (cwflag == OPTARG_SET)
  1051. Cmetrics->origin_xoff = cw_argval;
  1052. }
  1053. #endif
  1054. debug(dump_Cmetrics(Cmetrics));
  1055. return (pcf_bm_t*) ci->bits;
  1056. }
  1057. /*
  1058. * Repad a bitmap
  1059. */
  1060. static int
  1061. RepadBitmap (char *pSrc, char *pDst, unsigned srcPad, unsigned dstPad, int width, int height)
  1062. {
  1063. int srcWidthBytes,dstWidthBytes;
  1064. int row,col;
  1065. char *pTmpSrc,*pTmpDst;
  1066. switch (srcPad) {
  1067. case 1:
  1068. srcWidthBytes = (width+7)>>3;
  1069. break;
  1070. case 2:
  1071. srcWidthBytes = ((width+15)>>4)<<1;
  1072. break;
  1073. case 4:
  1074. srcWidthBytes = ((width+31)>>5)<<2;
  1075. break;
  1076. case 8:
  1077. srcWidthBytes = ((width+63)>>6)<<3;
  1078. break;
  1079. default:
  1080. return 0;
  1081. }
  1082. switch (dstPad) {
  1083. case 1:
  1084. dstWidthBytes = (width+7)>>3;
  1085. break;
  1086. case 2:
  1087. dstWidthBytes = ((width+15)>>4)<<1;
  1088. break;
  1089. case 4:
  1090. dstWidthBytes = ((width+31)>>5)<<2;
  1091. break;
  1092. case 8:
  1093. dstWidthBytes = ((width+63)>>6)<<3;
  1094. break;
  1095. default:
  1096. return 0;
  1097. }
  1098. width = srcWidthBytes;
  1099. if (width > dstWidthBytes)
  1100. width = dstWidthBytes;
  1101. pTmpSrc= pSrc;
  1102. pTmpDst= pDst;
  1103. for (row = 0; row < height; row++)
  1104. {
  1105. for (col = 0; col < width; col++)
  1106. *pTmpDst++ = *pTmpSrc++;
  1107. while (col < dstWidthBytes)
  1108. {
  1109. *pTmpDst++ = '\0';
  1110. col++;
  1111. }
  1112. pTmpSrc += srcWidthBytes - width;
  1113. }
  1114. return dstWidthBytes * height;
  1115. }
  1116. #if 0
  1117. static int
  1118. handle_cuferr(int err, ucs4_t *v, int *ndx)
  1119. {
  1120. if (err == CUF_ILCH) {
  1121. return handle_illegalchar(v, ndx);
  1122. } else if (err == CUF_NICH) {
  1123. return handle_nonidentchar(v, ndx);
  1124. } else {
  1125. return 0;
  1126. }
  1127. }
  1128. static int
  1129. handle_illegalchar(ucs4_t *val, int *ndx)
  1130. {
  1131. return XU_IGNORE;
  1132. }
  1133. static int
  1134. handle_nonidentchar(ucs4_t *val, int *ndx)
  1135. {
  1136. return XU_IGNORE;
  1137. }
  1138. #endif
  1139. static int
  1140. handle_nobitmap(ucs4_t *val, pcffont_t *font, pcf_charmet_t *Cm, pcf_bm_t **bitmap) {
  1141. int fv;
  1142. /* Becasue NOBITMAPREPL is hard coded for FALLBACK_FONT */
  1143. if(strstr(font->file, FALLBACK_FONT)==NULL)
  1144. return XU_IGNORE;
  1145. *val = NOBITMAPREPL;
  1146. if ((fv = font->cuf(*val)) < 0) return XU_IGNORE;
  1147. if ((*bitmap = xpcf_getcbm(fv, font, Cm)) == NULL) return XU_IGNORE;
  1148. return(1);
  1149. }
  1150. static int
  1151. BufFileRawFill (BufFilePtr f)
  1152. {
  1153. int left;
  1154. // left = read (FileDes(f), f->buffer, BUFFILESIZE);
  1155. left = fread (f->buffer, 1, BUFFILESIZE, FileDes(f));
  1156. if (left <= 0) {
  1157. f->left = 0;
  1158. return BUFFILEEOF;
  1159. }
  1160. f->left = left - 1;
  1161. f->bufp = f->buffer + 1;
  1162. return f->buffer[0];
  1163. }
  1164. static int
  1165. BufFileRawSkip (BufFilePtr f, int count)
  1166. {
  1167. int curoff;
  1168. int fileoff;
  1169. int todo;
  1170. curoff = f->bufp - f->buffer;
  1171. fileoff = curoff + f->left;
  1172. if (curoff + count <= fileoff) {
  1173. f->bufp += count;
  1174. f->left -= count;
  1175. } else {
  1176. todo = count - (fileoff - curoff);
  1177. // if (lseek (FileDes(f), todo, 1) == -1) {
  1178. if (fseek (FileDes(f), todo, 1) == -1) {
  1179. if (errno != ESPIPE)
  1180. return BUFFILEEOF;
  1181. while (todo) {
  1182. curoff = BUFFILESIZE;
  1183. if (curoff > todo)
  1184. curoff = todo;
  1185. // fileoff = read (FileDes(f), f->buffer, curoff);
  1186. fileoff = fread (f->buffer, 1, curoff, FileDes(f));
  1187. if (fileoff <= 0)
  1188. return BUFFILEEOF;
  1189. todo -= fileoff;
  1190. }
  1191. }
  1192. f->left = 0;
  1193. }
  1194. return count;
  1195. }
  1196. static int
  1197. BufFileRawClose (BufFilePtr f, int doClose)
  1198. {
  1199. if (doClose)
  1200. // close (FileDes (f));
  1201. fclose (FileDes (f));
  1202. return 1;
  1203. }
  1204. static Bool
  1205. NameEqual (char *a, char *b, int l)
  1206. {
  1207. while (l--)
  1208. if (*a++ != *b++)
  1209. return FALSE;
  1210. return TRUE;
  1211. }
  1212. static BufFilePtr
  1213. //BufFileOpenRead(int fd)
  1214. BufFileOpenRead(FILE *fd)
  1215. {
  1216. return BufFileCreate ((char *) fd, BufFileRawFill, BufFileRawSkip, BufFileRawClose);
  1217. }
  1218. static AtomListPtr *reverseMap;
  1219. static int reverseMapSize;
  1220. static Atom lastAtom;
  1221. static Bool ResizeReverseMap ()
  1222. {
  1223. if (reverseMapSize == 0)
  1224. reverseMapSize = 1000;
  1225. else
  1226. reverseMapSize *= 2;
  1227. reverseMap = (AtomListPtr *) Xrealloc ((ulong_t *)reverseMap, reverseMapSize * sizeof (AtomListPtr));
  1228. if (!reverseMap)
  1229. return FALSE;
  1230. else
  1231. return TRUE;
  1232. }
  1233. static char *
  1234. NameForAtom(Atom atom)
  1235. {
  1236. if (atom != None && atom <= lastAtom)
  1237. return reverseMap[atom]->name;
  1238. return 0;
  1239. }
  1240. /*****************************************************************
  1241. * TAG( getcode )
  1242. *
  1243. * Read one code from the standard input. If BUFFILEEOF, return -1.
  1244. * Inputs:
  1245. * stdin
  1246. * Outputs:
  1247. * code or -1 is returned.
  1248. */
  1249. static char_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
  1250. static code_int
  1251. getcode(CompressedFile *file)
  1252. {
  1253. register code_int code;
  1254. register int r_off, bits;
  1255. register char_type *bp = file->buf;
  1256. register BufFilePtr raw;
  1257. if ( file->clear_flg > 0 || file->offset >= file->size ||
  1258. file->free_ent > file->maxcode )
  1259. {
  1260. /*
  1261. * If the next entry will be too big for the current code
  1262. * size, then we must increase the size. This implies reading
  1263. * a new buffer full, too.
  1264. */
  1265. if ( file->free_ent > file->maxcode ) {
  1266. file->n_bits++;
  1267. if ( file->n_bits == file->maxbits )
  1268. file->maxcode = file->maxmaxcode; /* won't get any bigger now */
  1269. else
  1270. file->maxcode = MAXCODE(file->n_bits);
  1271. }
  1272. if ( file->clear_flg > 0) {
  1273. file->maxcode = MAXCODE (file->n_bits = INIT_BITS);
  1274. file->clear_flg = 0;
  1275. }
  1276. bits = file->n_bits;
  1277. raw = file->file;
  1278. while (bits > 0 && (code = BufFileGet (raw)) != BUFFILEEOF)
  1279. {
  1280. *bp++ = code;
  1281. --bits;
  1282. }
  1283. bp = file->buf;
  1284. if (bits == file->n_bits)
  1285. return -1; /* end of file */
  1286. file->size = file->n_bits - bits;
  1287. file->offset = 0;
  1288. /* Round size down to integral number of codes */
  1289. file->size = (file->size << 3) - (file->n_bits - 1);
  1290. }
  1291. r_off = file->offset;
  1292. bits = file->n_bits;
  1293. /*
  1294. * Get to the first byte.
  1295. */
  1296. bp += (r_off >> 3);
  1297. r_off &= 7;
  1298. /* Get first part (low order bits) */
  1299. #ifdef NO_UCHAR
  1300. code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff;
  1301. #else
  1302. code = (*bp++ >> r_off);
  1303. #endif /* NO_UCHAR */
  1304. bits -= (8 - r_off);
  1305. r_off = 8 - r_off; /* now, offset into code word */
  1306. /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
  1307. if ( bits >= 8 ) {
  1308. #ifdef NO_UCHAR
  1309. code |= (*bp++ & 0xff) << r_off;
  1310. #else
  1311. code |= *bp++ << r_off;
  1312. #endif /* NO_UCHAR */
  1313. r_off += 8;
  1314. bits -= 8;
  1315. }
  1316. /* high order bits. */
  1317. if (rmask[bits])
  1318. code |= (*bp & rmask[bits]) << r_off;
  1319. file->offset += file->n_bits;
  1320. return code;
  1321. }
  1322. void
  1323. BufFileClose (BufFilePtr f, int doClose)
  1324. {
  1325. (void) (*f->close) (f, doClose);
  1326. xfree (f);
  1327. }
  1328. static int
  1329. get_font_property(FontPtr Fp, char *name, ulong_t *value)
  1330. {
  1331. int j;
  1332. int n;
  1333. char *prop_name;
  1334. n = Fp->info.nprops;
  1335. for (j = 0; j < n; j++) {
  1336. if (!(prop_name = NameForAtom(Fp->info.props[j].name)))
  1337. continue;
  1338. if (eq(name, prop_name)) {
  1339. *value = (ulong_t) Fp->info.props[j].value;
  1340. return 0;
  1341. }
  1342. }
  1343. return -1;
  1344. }
  1345. static BufFilePtr
  1346. BufFileCreate (char *_private, int (*io)(BufFilePtr), int (*skip)(BufFilePtr, int), int (*close)(BufFilePtr, int))
  1347. {
  1348. BufFilePtr f;
  1349. f = (BufFilePtr) xalloc (sizeof *f);
  1350. if (!f)
  1351. return 0;
  1352. f->_private = _private;
  1353. f->bufp = f->buffer;
  1354. f->left = 0;
  1355. f->io = io;
  1356. f->skip = skip;
  1357. f->close = close;
  1358. return f;
  1359. }
  1360. int hsize_table[] = {
  1361. 5003, /* 12 bits - 80% occupancy */
  1362. 9001, /* 13 bits - 91% occupancy */
  1363. 18013, /* 14 bits - 91% occupancy */
  1364. 35023, /* 15 bits - 94% occupancy */
  1365. 69001 /* 16 bits - 95% occupancy */
  1366. };
  1367. static int BufCompressedFill(), BufCompressedSkip(), BufCompressedClose();
  1368. static int
  1369. BufCompressedClose (BufFilePtr f, int doClose)
  1370. {
  1371. CompressedFile *file;
  1372. BufFilePtr raw;
  1373. file = (CompressedFile *) f->_private;
  1374. raw = file->file;
  1375. xfree (file);
  1376. BufFileClose (raw, doClose);
  1377. return 1;
  1378. }
  1379. static int
  1380. BufCompressedSkip (BufFilePtr f, int bytes)
  1381. {
  1382. //int c;
  1383. while (bytes--) {
  1384. if (BufFileGet(f) == BUFFILEEOF)
  1385. return BUFFILEEOF;
  1386. }
  1387. return 0;
  1388. }
  1389. static int
  1390. BufCompressedFill (BufFilePtr f)
  1391. {
  1392. CompressedFile *file;
  1393. register char_type *stackp, *de_stack;
  1394. register char_type finchar;
  1395. register code_int code, oldcode, incode;
  1396. BufChar *buf, *bufend;
  1397. file = (CompressedFile *) f->_private;
  1398. buf = f->buffer;
  1399. bufend = buf + BUFFILESIZE;
  1400. stackp = file->stackp;
  1401. de_stack = file->de_stack;
  1402. finchar = file->finchar;
  1403. oldcode = file->oldcode;
  1404. while (buf < bufend) {
  1405. while (stackp > de_stack && buf < bufend)
  1406. *buf++ = *--stackp;
  1407. if (buf == bufend)
  1408. break;
  1409. if (oldcode == -1)
  1410. break;
  1411. code = getcode (file);
  1412. if (code == -1)
  1413. break;
  1414. if ( (code == CLEAR) && file->block_compress ) {
  1415. for ( code = 255; code >= 0; code-- )
  1416. file->tab_prefix[code] = 0;
  1417. file->clear_flg = 1;
  1418. file->free_ent = FIRST - 1;
  1419. if ( (code = getcode (file)) == -1 ) /* O, untimely death! */
  1420. break;
  1421. }
  1422. incode = code;
  1423. /*
  1424. * Special case for KwKwK string.
  1425. */
  1426. if ( code >= file->free_ent ) {
  1427. *stackp++ = finchar;
  1428. code = oldcode;
  1429. }
  1430. /*
  1431. * Generate output characters in reverse order
  1432. */
  1433. while ( code >= 256 )
  1434. {
  1435. *stackp++ = file->tab_suffix[code];
  1436. code = file->tab_prefix[code];
  1437. }
  1438. finchar = file->tab_suffix[code];
  1439. *stackp++ = finchar;
  1440. /*
  1441. * Generate the new entry.
  1442. */
  1443. if ( (code=file->free_ent) < file->maxmaxcode ) {
  1444. file->tab_prefix[code] = (unsigned short)oldcode;
  1445. file->tab_suffix[code] = finchar;
  1446. file->free_ent = code+1;
  1447. }
  1448. /*
  1449. * Remember previous code.
  1450. */
  1451. oldcode = incode;
  1452. }
  1453. file->oldcode = oldcode;
  1454. file->stackp = stackp;
  1455. file->finchar = finchar;
  1456. if (buf == f->buffer) {
  1457. f->left = 0;
  1458. return BUFFILEEOF;
  1459. }
  1460. f->bufp = f->buffer + 1;
  1461. f->left = (buf - f->buffer) - 1;
  1462. return f->buffer[0];
  1463. }
  1464. BufFilePtr
  1465. BufFilePushCompressed (BufFilePtr f)
  1466. {
  1467. int code;
  1468. int maxbits;
  1469. int hsize;
  1470. CompressedFile *file;
  1471. int extra;
  1472. if ((BufFileGet(f) != (magic_header[0] & 0xFF)) ||
  1473. (BufFileGet(f) != (magic_header[1] & 0xFF)))
  1474. {
  1475. return 0;
  1476. }
  1477. code = BufFileGet (f);
  1478. maxbits = code & BIT_MASK;
  1479. if (maxbits > BITS || maxbits < 12)
  1480. return 0;
  1481. hsize = hsize_table[maxbits - 12];
  1482. extra = (1 << maxbits) * sizeof (char_type) +
  1483. hsize * sizeof (unsigned short);
  1484. file = (CompressedFile *) xalloc (sizeof (CompressedFile) + extra);
  1485. if (!file)
  1486. return 0;
  1487. file->file = f;
  1488. file->maxbits = maxbits;
  1489. file->block_compress = code & BLOCK_MASK;
  1490. file->maxmaxcode = 1 << file->maxbits;
  1491. file->tab_suffix = (char_type *) &file[1];
  1492. file->tab_prefix = (unsigned short *) (file->tab_suffix + file->maxmaxcode);
  1493. /*
  1494. * As above, initialize the first 256 entries in the table.
  1495. */
  1496. file->maxcode = MAXCODE(file->n_bits = INIT_BITS);
  1497. for ( code = 255; code >= 0; code-- ) {
  1498. file->tab_prefix[code] = 0;
  1499. file->tab_suffix[code] = (char_type) code;
  1500. }
  1501. file->free_ent = ((file->block_compress) ? FIRST : 256 );
  1502. file->clear_flg = 0;
  1503. file->offset = 0;
  1504. file->size = 0;
  1505. file->stackp = file->de_stack;
  1506. file->finchar = file->oldcode = getcode (file);
  1507. if (file->oldcode != -1)
  1508. *file->stackp++ = file->finchar;
  1509. return BufFileCreate ((char *) file,
  1510. BufCompressedFill,
  1511. BufCompressedSkip,
  1512. BufCompressedClose);
  1513. }
  1514. static FontFilePtr
  1515. FontFileOpen (char *name) {
  1516. // int fd;
  1517. FILE *fd;
  1518. int len;
  1519. BufFilePtr raw, cooked;
  1520. len = strlen (name);
  1521. /*
  1522. * A little hack for .gz file support.
  1523. * We gzcat the file and will treat the
  1524. * resultant fd as a regular file's.
  1525. */
  1526. // if (len > 3 && !strcmp (name + len - 3, ".gz"))
  1527. // fd = gzcatfile (name);
  1528. // else
  1529. // fd = open (name, 0);
  1530. fd = fopen(name, "rb");
  1531. // if (fd < 0)
  1532. if (!fd)
  1533. return 0;
  1534. raw = BufFileOpenRead (fd);
  1535. if (!raw)
  1536. {
  1537. // close (fd);
  1538. fclose (fd);
  1539. return 0;
  1540. }
  1541. if (len > 2 && !strcmp (name + len - 2, ".Z")) {
  1542. cooked = BufFilePushCompressed (raw);
  1543. if (!cooked) {
  1544. BufFileClose (raw, TRUE);
  1545. return 0;
  1546. }
  1547. raw = cooked;
  1548. }
  1549. return (FontFilePtr) raw;
  1550. }
  1551. static void FontFileClose (FontFilePtr f)
  1552. {
  1553. BufFileClose ((BufFilePtr) f, TRUE);
  1554. }
  1555. static int
  1556. pcfGetLSB32(FontFilePtr file)
  1557. {
  1558. int c;
  1559. c = FontFileGetc(file);
  1560. c |= FontFileGetc(file) << 8;
  1561. c |= FontFileGetc(file) << 16;
  1562. c |= FontFileGetc(file) << 24;
  1563. position += 4;
  1564. return c;
  1565. }
  1566. static PCFTablePtr
  1567. pcfReadTOC(FontFilePtr file, int *countp)
  1568. {
  1569. CARD32 version;
  1570. PCFTablePtr tables;
  1571. int count;
  1572. int i;
  1573. position = 0;
  1574. version = pcfGetLSB32(file);
  1575. if (version != PCF_FILE_VERSION)
  1576. return (PCFTablePtr) NULL;
  1577. count = pcfGetLSB32(file);
  1578. tables = (PCFTablePtr) xalloc(count * sizeof(PCFTableRec));
  1579. if (!tables)
  1580. return (PCFTablePtr) NULL;
  1581. for (i = 0; i < count; i++) {
  1582. tables[i].type = pcfGetLSB32(file);
  1583. tables[i].format = pcfGetLSB32(file);
  1584. tables[i].size = pcfGetLSB32(file);
  1585. tables[i].offset = pcfGetLSB32(file);
  1586. }
  1587. *countp = count;
  1588. return tables;
  1589. }
  1590. static Bool
  1591. ResizeHashTable ()
  1592. {
  1593. int newHashSize;
  1594. int newHashMask;
  1595. AtomListPtr *newHashTable;
  1596. int i;
  1597. int h;
  1598. int newRehash;
  1599. int r;
  1600. if (hashSize == 0)
  1601. newHashSize = 1024;
  1602. else
  1603. newHashSize = hashSize * 2;
  1604. newHashTable = (AtomListPtr *) xalloc (newHashSize * sizeof (AtomListPtr));
  1605. if (!newHashTable)
  1606. return FALSE;
  1607. bzero ((char *) newHashTable, newHashSize * sizeof (AtomListPtr));
  1608. newHashMask = newHashSize - 1;
  1609. newRehash = (newHashMask - 2);
  1610. for (i = 0; i < hashSize; i++)
  1611. {
  1612. if (hashTable[i])
  1613. {
  1614. h = (hashTable[i]->hash) & newHashMask;
  1615. if (newHashTable[h])
  1616. {
  1617. r = hashTable[i]->hash % newRehash | 1;
  1618. do {
  1619. h += r;
  1620. if (h >= newHashSize)
  1621. h -= newHashSize;
  1622. } while (newHashTable[h]);
  1623. }
  1624. newHashTable[h] = hashTable[i];
  1625. }
  1626. }
  1627. xfree (hashTable);
  1628. hashTable = newHashTable;
  1629. hashSize = newHashSize;
  1630. hashMask = newHashMask;
  1631. rehash = newRehash;
  1632. return TRUE;
  1633. }
  1634. static int
  1635. Hash(char *string, int len)
  1636. {
  1637. int h;
  1638. h = 0;
  1639. while (len--)
  1640. h = (h << 3) ^ *string++;
  1641. if (h < 0)
  1642. return -h;
  1643. return h;
  1644. }
  1645. static Atom
  1646. MakeAtom(char *string, unsigned len, int makeit)
  1647. {
  1648. AtomListPtr a;
  1649. int hash;
  1650. int h = 0;
  1651. int r;
  1652. hash = Hash (string, len);
  1653. if (hashTable)
  1654. {
  1655. h = hash & hashMask;
  1656. if (hashTable[h])
  1657. {
  1658. if (hashTable[h]->hash == hash && hashTable[h]->len == len &&
  1659. NameEqual (hashTable[h]->name, string, len))
  1660. {
  1661. return hashTable[h]->atom;
  1662. }
  1663. r = (hash % rehash) | 1;
  1664. for (;;)
  1665. {
  1666. h += r;
  1667. if (h >= hashSize)
  1668. h -= hashSize;
  1669. if (!hashTable[h])
  1670. break;
  1671. if (hashTable[h]->hash == hash && hashTable[h]->len == len &&
  1672. NameEqual (hashTable[h]->name, string, len))
  1673. {
  1674. return hashTable[h]->atom;
  1675. }
  1676. }
  1677. }
  1678. }
  1679. if (!makeit)
  1680. return None;
  1681. a = (AtomListPtr) xalloc (sizeof (AtomListRec) + len + 1);
  1682. a->name = (char *) (a + 1);
  1683. a->len = len;
  1684. strncpy (a->name, string, len);
  1685. a->name[len] = '\0';
  1686. a->atom = ++lastAtom;
  1687. a->hash = hash;
  1688. if (hashUsed >= hashSize / 2)
  1689. {
  1690. ResizeHashTable ();
  1691. h = hash & hashMask;
  1692. if (hashTable[h])
  1693. {
  1694. r = (hash % rehash) | 1;
  1695. do {
  1696. h += r;
  1697. if (h >= hashSize)
  1698. h -= hashSize;
  1699. } while (hashTable[h]);
  1700. }
  1701. }
  1702. hashTable[h] = a;
  1703. hashUsed++;
  1704. if (reverseMapSize <= a->atom)
  1705. ResizeReverseMap();
  1706. reverseMap[a->atom] = a;
  1707. return a->atom;
  1708. }
  1709. int BufFileRead (BufFilePtr f, char *b, int n)
  1710. {
  1711. int c, cnt;
  1712. cnt = n;
  1713. while (cnt--) {
  1714. c = BufFileGet (f);
  1715. if (c == BUFFILEEOF)
  1716. break;
  1717. *b++ = c;
  1718. }
  1719. return n - cnt - 1;
  1720. }
  1721. static Bool
  1722. pcfGetProperties(FontInfoPtr pFontInfo, FontFilePtr file, PCFTablePtr tables, int ntables)
  1723. {
  1724. FontPropPtr props = 0;
  1725. int nprops;
  1726. char *isStringProp = 0;
  1727. CARD32 format;
  1728. int i;
  1729. /* changed by suresh 07/12/99 from int to CARD 32*/
  1730. CARD32 size;
  1731. int string_size;
  1732. char *strings;
  1733. /* font properties */
  1734. if (!pcfSeekToType(file, tables, ntables, PCF_PROPERTIES, &format, &size))
  1735. goto Bail;
  1736. format = pcfGetLSB32(file);
  1737. if (!PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT))
  1738. goto Bail;
  1739. nprops = pcfGetINT32(file, format);
  1740. props = (FontPropPtr) xalloc(nprops * sizeof(FontPropRec));
  1741. if (!props)
  1742. goto Bail;
  1743. isStringProp = (char *) xalloc(nprops * sizeof(char));
  1744. if (!isStringProp)
  1745. goto Bail;
  1746. for (i = 0; i < nprops; i++) {
  1747. props[i].name = pcfGetINT32(file, format);
  1748. isStringProp[i] = pcfGetINT8(file, format);
  1749. props[i].value = pcfGetINT32(file, format);
  1750. }
  1751. /* pad the property array */
  1752. /*
  1753. * clever here - nprops is the same as the number of odd-units read, as
  1754. * only isStringProp are odd length
  1755. */
  1756. if (nprops & 3)
  1757. {
  1758. i = 4 - (nprops & 3);
  1759. (void)FontFileSkip(file, i);
  1760. position += i;
  1761. }
  1762. string_size = pcfGetINT32(file, format);
  1763. strings = (char *) xalloc(string_size);
  1764. if (!strings) {
  1765. goto Bail;
  1766. }
  1767. FontFileRead(file, strings, string_size);
  1768. position += string_size;
  1769. for (i = 0; i < nprops; i++) {
  1770. props[i].name = MakeAtom(strings + props[i].name,
  1771. strlen(strings + props[i].name), TRUE);
  1772. if (isStringProp[i]) {
  1773. props[i].value = MakeAtom(strings + props[i].value,
  1774. strlen(strings + props[i].value), TRUE);
  1775. }
  1776. }
  1777. xfree(strings);
  1778. pFontInfo->isStringProp = isStringProp;
  1779. pFontInfo->props = props;
  1780. pFontInfo->nprops = nprops;
  1781. return TRUE;
  1782. Bail:
  1783. xfree(isStringProp);
  1784. xfree(props);
  1785. return FALSE;
  1786. }
  1787. static void
  1788. pcfGetCompressedMetric(FontFilePtr file, CARD32 format, xCharInfo *metric)
  1789. {
  1790. metric->leftSideBearing = pcfGetINT8(file, format) - 0x80;
  1791. metric->rightSideBearing = pcfGetINT8(file, format) - 0x80;
  1792. metric->characterWidth = pcfGetINT8(file, format) - 0x80;
  1793. metric->ascent = pcfGetINT8(file, format) - 0x80;
  1794. metric->descent = pcfGetINT8(file, format) - 0x80;
  1795. metric->attributes = 0;
  1796. }
  1797. static Bool
  1798. pcfSeekToType(FontFilePtr file, PCFTablePtr tables, int ntables, CARD32 type, CARD32 *formatp, CARD32 *sizep)
  1799. {
  1800. int i;
  1801. for (i = 0; i < ntables; i++)
  1802. if (tables[i].type == type) {
  1803. if (position > tables[i].offset)
  1804. return FALSE;
  1805. if (!FontFileSkip(file, tables[i].offset - position))
  1806. return FALSE;
  1807. position = tables[i].offset;
  1808. *sizep = tables[i].size;
  1809. *formatp = tables[i].format;
  1810. return TRUE;
  1811. }
  1812. return FALSE;
  1813. }
  1814. static int
  1815. pcfGetINT32(FontFilePtr file, CARD32 format)
  1816. {
  1817. int c;
  1818. if (PCF_BYTE_ORDER(format) == MSBFirst) {
  1819. c = FontFileGetc(file) << 24;
  1820. c |= FontFileGetc(file) << 16;
  1821. c |= FontFileGetc(file) << 8;
  1822. c |= FontFileGetc(file);
  1823. } else {
  1824. c = FontFileGetc(file);
  1825. c |= FontFileGetc(file) << 8;
  1826. c |= FontFileGetc(file) << 16;
  1827. c |= FontFileGetc(file) << 24;
  1828. }
  1829. position += 4;
  1830. return c;
  1831. }
  1832. static int
  1833. pcfGetINT16(FontFilePtr file, CARD32 format)
  1834. {
  1835. int c;
  1836. if (PCF_BYTE_ORDER(format) == MSBFirst) {
  1837. c = FontFileGetc(file) << 8;
  1838. c |= FontFileGetc(file);
  1839. } else {
  1840. c = FontFileGetc(file);
  1841. c |= FontFileGetc(file) << 8;
  1842. }
  1843. position += 2;
  1844. return c;
  1845. }
  1846. /*
  1847. * pcfReadAccel
  1848. *
  1849. * Fill in the accelerator information from the font file; used
  1850. * to read both BDF_ACCELERATORS and old style ACCELERATORS
  1851. */
  1852. static Bool
  1853. pcfGetAccel(FontInfoPtr pFontInfo, FontFilePtr file, PCFTablePtr tables, int ntables, CARD32 type)
  1854. {
  1855. CARD32 format;
  1856. /* changed by suresh 07/12/99 from int to CARD 32*/
  1857. CARD32 size;
  1858. if (!pcfSeekToType(file, tables, ntables, type, &format, &size))
  1859. goto Bail;
  1860. format = pcfGetLSB32(file);
  1861. if (!PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT) &&
  1862. !PCF_FORMAT_MATCH(format, PCF_ACCEL_W_INKBOUNDS))
  1863. {
  1864. goto Bail;
  1865. }
  1866. pFontInfo->noOverlap = pcfGetINT8(file, format);
  1867. pFontInfo->constantMetrics = pcfGetINT8(file, format);
  1868. pFontInfo->terminalFont = pcfGetINT8(file, format);
  1869. pFontInfo->constantWidth = pcfGetINT8(file, format);
  1870. pFontInfo->inkInside = pcfGetINT8(file, format);
  1871. pFontInfo->inkMetrics = pcfGetINT8(file, format);
  1872. pFontInfo->drawDirection = pcfGetINT8(file, format);
  1873. pFontInfo->anamorphic = FALSE;
  1874. /* natural alignment */ pcfGetINT8(file, format);
  1875. pFontInfo->fontAscent = pcfGetINT32(file, format);
  1876. pFontInfo->fontDescent = pcfGetINT32(file, format);
  1877. pFontInfo->maxOverlap = pcfGetINT32(file, format);
  1878. pcfGetMetric(file, format, &pFontInfo->minbounds);
  1879. pcfGetMetric(file, format, &pFontInfo->maxbounds);
  1880. if (PCF_FORMAT_MATCH(format, PCF_ACCEL_W_INKBOUNDS)) {
  1881. pcfGetMetric(file, format, &pFontInfo->ink_minbounds);
  1882. pcfGetMetric(file, format, &pFontInfo->ink_maxbounds);
  1883. } else {
  1884. pFontInfo->ink_minbounds = pFontInfo->minbounds;
  1885. pFontInfo->ink_maxbounds = pFontInfo->maxbounds;
  1886. }
  1887. return TRUE;
  1888. Bail:
  1889. return FALSE;
  1890. }
  1891. static void
  1892. pcfGetMetric(FontFilePtr file, CARD32 format, xCharInfo *metric)
  1893. {
  1894. metric->leftSideBearing = pcfGetINT16(file, format);
  1895. metric->rightSideBearing = pcfGetINT16(file, format);
  1896. metric->characterWidth = pcfGetINT16(file, format);
  1897. metric->ascent = pcfGetINT16(file, format);
  1898. metric->descent = pcfGetINT16(file, format);
  1899. metric->attributes = pcfGetINT16(file, format);
  1900. }
  1901. int
  1902. bitmapGetGlyphs(FontPtr pFont, unsigned long count, unsigned char *chars, FontEncoding charEncoding, unsigned long *glyphCount, CharInfoPtr *glyphs)
  1903. {
  1904. BitmapFontPtr bitmapFont;
  1905. unsigned int firstCol;
  1906. register unsigned int numCols;
  1907. unsigned int firstRow;
  1908. unsigned int numRows;
  1909. CharInfoPtr *glyphsBase;
  1910. register unsigned int c;
  1911. register CharInfoPtr pci;
  1912. unsigned int r;
  1913. CharInfoPtr *encoding;
  1914. CharInfoPtr pDefault;
  1915. bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
  1916. encoding = bitmapFont->encoding;
  1917. pDefault = bitmapFont->pDefault;
  1918. firstCol = pFont->info.firstCol;
  1919. numCols = pFont->info.lastCol - firstCol + 1;
  1920. glyphsBase = glyphs;
  1921. switch (charEncoding) {
  1922. case Linear8Bit:
  1923. case TwoD8Bit:
  1924. if (pFont->info.firstRow > 0)
  1925. break;
  1926. if (pFont->info.allExist && pDefault) {
  1927. while (count--) {
  1928. c = (*chars++) - firstCol;
  1929. if (c < numCols)
  1930. *glyphs++ = encoding[c];
  1931. else
  1932. *glyphs++ = pDefault;
  1933. }
  1934. } else {
  1935. while (count--) {
  1936. c = (*chars++) - firstCol;
  1937. if (c < numCols && (pci = encoding[c]))
  1938. *glyphs++ = pci;
  1939. else if (pDefault)
  1940. *glyphs++ = pDefault;
  1941. }
  1942. }
  1943. break;
  1944. case Linear16Bit:
  1945. if (pFont->info.allExist && pDefault) {
  1946. while (count--) {
  1947. c = *chars++ << 8;
  1948. c = (c | *chars++) - firstCol;
  1949. if (c < numCols)
  1950. *glyphs++ = encoding[c];
  1951. else
  1952. *glyphs++ = pDefault;
  1953. }
  1954. } else {
  1955. while (count--) {
  1956. c = *chars++ << 8;
  1957. c = (c | *chars++) - firstCol;
  1958. if (c < numCols && (pci = encoding[c]))
  1959. *glyphs++ = pci;
  1960. else if (pDefault)
  1961. *glyphs++ = pDefault;
  1962. }
  1963. }
  1964. break;
  1965. case TwoD16Bit:
  1966. firstRow = pFont->info.firstRow;
  1967. numRows = pFont->info.lastRow - firstRow + 1;
  1968. while (count--) {
  1969. r = (*chars++) - firstRow;
  1970. c = (*chars++) - firstCol;
  1971. if (r < numRows && c < numCols &&
  1972. (pci = encoding[r * numCols + c]))
  1973. *glyphs++ = pci;
  1974. else if (pDefault)
  1975. *glyphs++ = pDefault;
  1976. }
  1977. break;
  1978. }
  1979. *glyphCount = glyphs - glyphsBase;
  1980. return Successful;
  1981. }
  1982. int
  1983. bitmapGetMetrics(FontPtr pFont, unsigned long count, unsigned char *chars, FontEncoding charEncoding, unsigned long *glyphCount, xCharInfo **glyphs)
  1984. {
  1985. int ret;
  1986. xCharInfo *ink_metrics;
  1987. CharInfoPtr metrics;
  1988. BitmapFontPtr bitmapFont;
  1989. CharInfoPtr oldDefault;
  1990. int i;
  1991. bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
  1992. oldDefault = bitmapFont->pDefault;
  1993. bitmapFont->pDefault = &nonExistantChar;
  1994. ret = bitmapGetGlyphs(pFont, count, chars, charEncoding, glyphCount, (CharInfoPtr *) glyphs);
  1995. if (ret == Successful) {
  1996. if (bitmapFont->ink_metrics) {
  1997. metrics = bitmapFont->metrics;
  1998. ink_metrics = bitmapFont->ink_metrics;
  1999. for (i = 0; i < *glyphCount; i++) {
  2000. if (glyphs[i] != (xCharInfo *) & nonExistantChar)
  2001. glyphs[i] = ink_metrics + (((CharInfoPtr) glyphs[i]) - metrics);
  2002. }
  2003. }
  2004. }
  2005. bitmapFont->pDefault = oldDefault;
  2006. return ret;
  2007. }
  2008. void
  2009. pcfUnloadFont(FontPtr pFont)
  2010. {
  2011. BitmapFontPtr bitmapFont;
  2012. bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
  2013. xfree(bitmapFont->ink_metrics);
  2014. xfree(bitmapFont->encoding);
  2015. xfree(bitmapFont->bitmaps);
  2016. xfree(bitmapFont->metrics);
  2017. xfree(pFont->info.isStringProp);
  2018. xfree(pFont->info.props);
  2019. xfree(bitmapFont);
  2020. xfree(pFont);
  2021. }
  2022. static int
  2023. pcfReadFont(FontPtr pFont, FontFilePtr file, int bit, int byte, int glyph, int scan)
  2024. {
  2025. CARD32 format;
  2026. CARD32 size;
  2027. BitmapFontPtr bitmapFont = 0;
  2028. int i;
  2029. PCFTablePtr tables = 0;
  2030. int ntables;
  2031. int nmetrics;
  2032. int nbitmaps;
  2033. int sizebitmaps;
  2034. int nink_metrics;
  2035. CharInfoPtr metrics = 0;
  2036. xCharInfo *ink_metrics = 0;
  2037. char *bitmaps = 0;
  2038. CharInfoPtr *encoding = 0;
  2039. int nencoding;
  2040. int encodingOffset;
  2041. CARD32 bitmapSizes[GLYPHPADOPTIONS];
  2042. CARD32 *offsets = 0;
  2043. Bool hasBDFAccelerators;
  2044. pFont->info.props = 0;
  2045. if (!(tables = pcfReadTOC(file, &ntables)))
  2046. goto Bail;
  2047. /* properties */
  2048. if (!pcfGetProperties(&pFont->info, file, tables, ntables))
  2049. goto Bail;
  2050. /* Use the old accelerators if no BDF accelerators are in the file */
  2051. hasBDFAccelerators = pcfHasType (tables, ntables, PCF_BDF_ACCELERATORS);
  2052. if (!hasBDFAccelerators)
  2053. if (!pcfGetAccel (&pFont->info, file, tables, ntables, PCF_ACCELERATORS))
  2054. goto Bail;
  2055. /* metrics */
  2056. if (!pcfSeekToType(file, tables, ntables, PCF_METRICS, &format, &size)) {
  2057. goto Bail;
  2058. }
  2059. format = pcfGetLSB32(file);
  2060. if (!PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT) &&
  2061. !PCF_FORMAT_MATCH(format, PCF_COMPRESSED_METRICS)) {
  2062. goto Bail;
  2063. }
  2064. if (PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT))
  2065. nmetrics = pcfGetINT32(file, format);
  2066. else
  2067. nmetrics = pcfGetINT16(file, format);
  2068. metrics = (CharInfoPtr) xalloc(nmetrics * sizeof(CharInfoRec));
  2069. if (!metrics) {
  2070. goto Bail;
  2071. }
  2072. for (i = 0; i < nmetrics; i++)
  2073. if (PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT))
  2074. pcfGetMetric(file, format, &(metrics + i)->metrics);
  2075. else
  2076. pcfGetCompressedMetric(file, format, &(metrics + i)->metrics);
  2077. /* bitmaps */
  2078. if (!pcfSeekToType(file, tables, ntables, PCF_BITMAPS, &format, &size))
  2079. goto Bail;
  2080. format = pcfGetLSB32(file);
  2081. if (!PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT))
  2082. goto Bail;
  2083. nbitmaps = pcfGetINT32(file, format);
  2084. if (nbitmaps != nmetrics)
  2085. goto Bail;
  2086. offsets = (CARD32 *) xalloc(nbitmaps * sizeof(CARD32));
  2087. if (!offsets)
  2088. goto Bail;
  2089. for (i = 0; i < nbitmaps; i++)
  2090. offsets[i] = pcfGetINT32(file, format);
  2091. for (i = 0; i < GLYPHPADOPTIONS; i++)
  2092. bitmapSizes[i] = pcfGetINT32(file, format);
  2093. sizebitmaps = bitmapSizes[PCF_GLYPH_PAD_INDEX(format)];
  2094. bitmaps = (char *) xalloc(sizebitmaps);
  2095. if (!bitmaps)
  2096. goto Bail;
  2097. FontFileRead(file, bitmaps, sizebitmaps);
  2098. position += sizebitmaps;
  2099. if (PCF_BIT_ORDER(format) != bit)
  2100. BitOrderInvert((unsigned char*)bitmaps, sizebitmaps);
  2101. if ((PCF_BYTE_ORDER(format) == PCF_BIT_ORDER(format)) != (bit == byte)) {
  2102. switch (bit == byte ? PCF_SCAN_UNIT(format) : scan) {
  2103. case 1:
  2104. break;
  2105. case 2:
  2106. TwoByteSwap((unsigned char*)bitmaps, sizebitmaps);
  2107. break;
  2108. case 4:
  2109. FourByteSwap((unsigned char*)bitmaps, sizebitmaps);
  2110. break;
  2111. }
  2112. }
  2113. if (PCF_GLYPH_PAD(format) != glyph) {
  2114. char *padbitmaps;
  2115. int sizepadbitmaps;
  2116. int old,
  2117. _new;
  2118. xCharInfo *metric;
  2119. sizepadbitmaps = bitmapSizes[PCF_SIZE_TO_INDEX(glyph)];
  2120. padbitmaps = (char *) xalloc(sizepadbitmaps);
  2121. if (!padbitmaps) {
  2122. goto Bail;
  2123. }
  2124. _new = 0;
  2125. for (i = 0; i < nbitmaps; i++) {
  2126. old = offsets[i];
  2127. metric = &metrics[i].metrics;
  2128. offsets[i] = _new;
  2129. _new += RepadBitmap(bitmaps + old, padbitmaps + _new,
  2130. PCF_GLYPH_PAD(format), glyph,
  2131. metric->rightSideBearing - metric->leftSideBearing,
  2132. metric->ascent + metric->descent);
  2133. }
  2134. xfree(bitmaps);
  2135. bitmaps = padbitmaps;
  2136. }
  2137. for (i = 0; i < nbitmaps; i++)
  2138. metrics[i].bits = bitmaps + offsets[i];
  2139. xfree(offsets);
  2140. /* ink metrics ? */
  2141. ink_metrics = NULL;
  2142. if (pcfSeekToType(file, tables, ntables, PCF_INK_METRICS, &format, &size)) {
  2143. format = pcfGetLSB32(file);
  2144. if (!PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT) &&
  2145. !PCF_FORMAT_MATCH(format, PCF_COMPRESSED_METRICS)) {
  2146. goto Bail;
  2147. }
  2148. if (PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT))
  2149. nink_metrics = pcfGetINT32(file, format);
  2150. else
  2151. nink_metrics = pcfGetINT16(file, format);
  2152. if (nink_metrics != nmetrics)
  2153. goto Bail;
  2154. ink_metrics = (xCharInfo *) xalloc(nink_metrics * sizeof(xCharInfo));
  2155. if (!ink_metrics)
  2156. goto Bail;
  2157. for (i = 0; i < nink_metrics; i++)
  2158. if (PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT))
  2159. pcfGetMetric(file, format, ink_metrics + i);
  2160. else
  2161. pcfGetCompressedMetric(file, format, ink_metrics + i);
  2162. }
  2163. /* encoding */
  2164. if (!pcfSeekToType(file, tables, ntables, PCF_BDF_ENCODINGS, &format, &size))
  2165. goto Bail;
  2166. format = pcfGetLSB32(file);
  2167. if (!PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT))
  2168. goto Bail;
  2169. pFont->info.firstCol = pcfGetINT16(file, format);
  2170. pFont->info.lastCol = pcfGetINT16(file, format);
  2171. pFont->info.firstRow = pcfGetINT16(file, format);
  2172. pFont->info.lastRow = pcfGetINT16(file, format);
  2173. pFont->info.defaultCh = pcfGetINT16(file, format);
  2174. nencoding = (pFont->info.lastCol - pFont->info.firstCol + 1) *
  2175. (pFont->info.lastRow - pFont->info.firstRow + 1);
  2176. encoding = (CharInfoPtr *) xalloc(nencoding * sizeof(CharInfoPtr));
  2177. if (!encoding)
  2178. goto Bail;
  2179. pFont->info.allExist = TRUE;
  2180. for (i = 0; i < nencoding; i++) {
  2181. encodingOffset = pcfGetINT16(file, format);
  2182. if (encodingOffset == 0xFFFF) {
  2183. pFont->info.allExist = FALSE;
  2184. encoding[i] = 0;
  2185. } else
  2186. encoding[i] = metrics + encodingOffset;
  2187. }
  2188. /* BDF style accelerators (i.e. bounds based on encoded glyphs) */
  2189. if (hasBDFAccelerators)
  2190. if (!pcfGetAccel (&pFont->info, file, tables, ntables, PCF_BDF_ACCELERATORS))
  2191. goto Bail;
  2192. bitmapFont = (BitmapFontPtr) xalloc(sizeof *bitmapFont);
  2193. if (!bitmapFont)
  2194. goto Bail;
  2195. bitmapFont->version_num = PCF_FILE_VERSION;
  2196. bitmapFont->num_chars = nmetrics;
  2197. bitmapFont->num_tables = ntables;
  2198. bitmapFont->metrics = metrics;
  2199. bitmapFont->ink_metrics = ink_metrics;
  2200. bitmapFont->bitmaps = bitmaps;
  2201. bitmapFont->encoding = encoding;
  2202. bitmapFont->pDefault = (CharInfoPtr) 0;
  2203. if (pFont->info.defaultCh != (unsigned short) NO_SUCH_CHAR) {
  2204. int r,
  2205. c,
  2206. cols;
  2207. r = pFont->info.defaultCh >> 8;
  2208. c = pFont->info.defaultCh & 0xFF;
  2209. if (pFont->info.firstRow <= r && r <= pFont->info.lastRow &&
  2210. pFont->info.firstCol <= c && c <= pFont->info.lastCol) {
  2211. cols = pFont->info.lastCol - pFont->info.firstCol + 1;
  2212. r = r - pFont->info.firstRow;
  2213. c = c - pFont->info.firstCol;
  2214. bitmapFont->pDefault = encoding[r * cols + c];
  2215. }
  2216. }
  2217. bitmapFont->bitmapExtra = (BitmapExtraPtr) 0;
  2218. pFont->fontPrivate = (pointer) bitmapFont;
  2219. pFont->get_glyphs = bitmapGetGlyphs;
  2220. pFont->get_metrics = bitmapGetMetrics;
  2221. pFont->unload_font = pcfUnloadFont;
  2222. pFont->bit = bit;
  2223. pFont->byte = byte;
  2224. pFont->glyph = glyph;
  2225. pFont->scan = scan;
  2226. xfree(tables);
  2227. return Successful;
  2228. Bail:
  2229. xfree(ink_metrics);
  2230. xfree(encoding);
  2231. xfree(bitmaps);
  2232. xfree(offsets);
  2233. xfree(metrics);
  2234. xfree(pFont->info.props);
  2235. pFont->info.props = 0;
  2236. xfree(bitmapFont);
  2237. xfree(tables);
  2238. return AllocError;
  2239. }
  2240. struct interval {
  2241. unsigned short first;
  2242. unsigned short last;
  2243. };
  2244. /* auxiliary function for binary search in interval table */
  2245. static int bisearch(wchar_t ucs, const struct interval *table, int max) {
  2246. int min = 0;
  2247. int mid;
  2248. if (ucs < table[0].first || ucs > table[max].last)
  2249. return 0;
  2250. while (max >= min) {
  2251. mid = (min + max) / 2;
  2252. if (ucs > table[mid].last)
  2253. min = mid + 1;
  2254. else if (ucs < table[mid].first)
  2255. max = mid - 1;
  2256. else
  2257. return 1;
  2258. }
  2259. return 0;
  2260. }
  2261. int wide_char_width(wchar_t ucs)
  2262. {
  2263. /* sorted list of non-overlapping intervals of non-spacing characters */
  2264. static const struct interval combining[] = {
  2265. { 0x0300, 0x034E }, { 0x0360, 0x0362 }, { 0x0483, 0x0486 },
  2266. { 0x0488, 0x0489 }, { 0x0591, 0x05A1 }, { 0x05A3, 0x05B9 },
  2267. { 0x05BB, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 },
  2268. { 0x05C4, 0x05C4 }, { 0x064B, 0x0655 }, { 0x0670, 0x0670 },
  2269. { 0x06D6, 0x06E4 }, { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED },
  2270. { 0x070F, 0x070F }, { 0x0711, 0x0711 }, { 0x0730, 0x074A },
  2271. { 0x07A6, 0x07B0 }, { 0x0901, 0x0902 }, { 0x093C, 0x093C },
  2272. { 0x0941, 0x0948 }, { 0x094D, 0x094D }, { 0x0951, 0x0954 },
  2273. { 0x0962, 0x0963 }, { 0x0981, 0x0981 }, { 0x09BC, 0x09BC },
  2274. { 0x09C1, 0x09C4 }, { 0x09CD, 0x09CD }, { 0x09E2, 0x09E3 },
  2275. { 0x0A02, 0x0A02 }, { 0x0A3C, 0x0A3C }, { 0x0A41, 0x0A42 },
  2276. { 0x0A47, 0x0A48 }, { 0x0A4B, 0x0A4D }, { 0x0A70, 0x0A71 },
  2277. { 0x0A81, 0x0A82 }, { 0x0ABC, 0x0ABC }, { 0x0AC1, 0x0AC5 },
  2278. { 0x0AC7, 0x0AC8 }, { 0x0ACD, 0x0ACD }, { 0x0B01, 0x0B01 },
  2279. { 0x0B3C, 0x0B3C }, { 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 },
  2280. { 0x0B4D, 0x0B4D }, { 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 },
  2281. { 0x0BC0, 0x0BC0 }, { 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 },
  2282. { 0x0C46, 0x0C48 }, { 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 },
  2283. { 0x0CBF, 0x0CBF }, { 0x0CC6, 0x0CC6 }, { 0x0CCC, 0x0CCD },
  2284. { 0x0D41, 0x0D43 }, { 0x0D4D, 0x0D4D }, { 0x0DCA, 0x0DCA },
  2285. { 0x0DD2, 0x0DD4 }, { 0x0DD6, 0x0DD6 }, { 0x0E31, 0x0E31 },
  2286. { 0x0E34, 0x0E3A }, { 0x0E47, 0x0E4E }, { 0x0EB1, 0x0EB1 },
  2287. { 0x0EB4, 0x0EB9 }, { 0x0EBB, 0x0EBC }, { 0x0EC8, 0x0ECD },
  2288. { 0x0F18, 0x0F19 }, { 0x0F35, 0x0F35 }, { 0x0F37, 0x0F37 },
  2289. { 0x0F39, 0x0F39 }, { 0x0F71, 0x0F7E }, { 0x0F80, 0x0F84 },
  2290. { 0x0F86, 0x0F87 }, { 0x0F90, 0x0F97 }, { 0x0F99, 0x0FBC },
  2291. { 0x0FC6, 0x0FC6 }, { 0x102D, 0x1030 }, { 0x1032, 0x1032 },
  2292. { 0x1036, 0x1037 }, { 0x1039, 0x1039 }, { 0x1058, 0x1059 },
  2293. { 0x1160, 0x11FF }, { 0x17B7, 0x17BD }, { 0x17C6, 0x17C6 },
  2294. { 0x17C9, 0x17D3 }, { 0x180B, 0x180E }, { 0x18A9, 0x18A9 },
  2295. { 0x200B, 0x200F }, { 0x202A, 0x202E }, { 0x206A, 0x206F },
  2296. { 0x20D0, 0x20E3 }, { 0x302A, 0x302F }, { 0x3099, 0x309A },
  2297. { 0xFB1E, 0xFB1E }, { 0xFE20, 0xFE23 }, { 0xFEFF, 0xFEFF },
  2298. { 0xFFF9, 0xFFFB }
  2299. };
  2300. /* test for 8-bit control characters */
  2301. if (ucs == 0)
  2302. return 0;
  2303. if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
  2304. return -1;
  2305. /* binary search in table of non-spacing characters */
  2306. if (bisearch(ucs, combining,
  2307. sizeof(combining) / sizeof(struct interval) - 1))
  2308. return 0;
  2309. /* if we arrive here, ucs is not a combining or C0/C1 control character */
  2310. return 1 +
  2311. (ucs >= 0x1100 &&
  2312. (ucs <= 0x115f || /* Hangul Jamo init. consonants */
  2313. (ucs >= 0x2e80 && ucs <= 0xa4cf && (ucs & ~0x0011) != 0x300a &&
  2314. ucs != 0x303f) || /* CJK ... Yi */
  2315. (ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */
  2316. (ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */
  2317. (ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */
  2318. (ucs >= 0xff00 && ucs <= 0xff5f) || /* Fullwidth Forms */
  2319. (ucs >= 0xffe0 && ucs <= 0xffe6) ||
  2320. (ucs >= 0x20000 && ucs <= 0x2ffff)));
  2321. }