pcf.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583
  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 <malloc.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 wcwidth(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:%x\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 open file\n",count);
  459. fd = fopen(name, "wb");
  460. if(fd!=NULL)
  461. fwrite(buf,1,count*sizeof(font_bmf),fd);
  462. fclose(fd);
  463. free(buf);
  464. }
  465. void Generate_new_font_with_header(pcffont_t *font)
  466. {
  467. int i,width,height,widthBytes,widthBits,LSBearing,RSBearing,ascent,descent;
  468. CharInfoPtr ci;
  469. char *buf;
  470. FILE *fd;
  471. font_bmf font_create;
  472. char name[256];
  473. int offset = 0;
  474. int count = 0;
  475. //int last_char = 0;
  476. //int bitmap_offset = 0;
  477. int header_len = 0;
  478. //int last_val = 0;
  479. //int bmf_buffer_len = 0;
  480. font_bmf_header font_header_t;
  481. strcpy(name,sOutFilename);
  482. //font_count = font->Fmetrics.lastchar;
  483. font_header_t.ascent = font->Fmetrics.ascent;
  484. font_header_t.descent = font->Fmetrics.descent;
  485. font_header_t.linespace = font->Fmetrics.linespace;
  486. //font_header_t.last_char = font->Fmetrics.lastchar;
  487. count = font->Fmetrics.lastchar;
  488. buf = (char*)malloc(count*sizeof(font_bmf)+sizeof(font_bmf_header));
  489. memset(buf,0,count*sizeof(font_bmf)+sizeof(font_bmf_header));
  490. memcpy(buf+offset,&font_header_t,sizeof(font_header_t));
  491. offset+=sizeof(font_header_t);
  492. header_len = sizeof(font_header_t);
  493. for(i=0; i<count;i++)
  494. {
  495. ci = font->bitmaps[i];
  496. memset(&font_create,0,sizeof(font_bmf));
  497. if(ci != NULL)
  498. {
  499. width = GLYPHWIDTHPIXELS(ci);
  500. height = GLYPHHEIGHTPIXELS(ci);
  501. widthBytes = GLYPHWIDTHBYTES(ci);
  502. widthBits = GLYPHWIDTHBYTES(ci) * NBPB;
  503. LSBearing = ci->metrics.leftSideBearing;
  504. RSBearing = ci->metrics.rightSideBearing;
  505. ascent = ci->metrics.ascent;
  506. descent = ci->metrics.descent;
  507. if(i==32 && width==0)
  508. {
  509. width = 2;
  510. height = 1;
  511. widthBytes = 1;
  512. widthBits = 8;
  513. RSBearing = 2;
  514. }
  515. font_create.width = (INT8)width;
  516. font_create.height = (INT8)height;
  517. font_create.widthBytes =(INT8)widthBytes;
  518. font_create.widthBits =(INT8)widthBits;
  519. font_create.ascent =(INT8)ascent;
  520. font_create.descent =(INT8)descent;
  521. font_create.LSBearing = (INT8)LSBearing;
  522. font_create.RSBearing = (INT8)RSBearing;
  523. //printf("char:%d,height:%d,widthBytes:%d\n",i,font_create.height,font_create.widthBytes);
  524. if(font_create.height<0)
  525. font_create.height = 0;
  526. if(font_create.widthBytes<0)
  527. font_create.widthBytes = 0;
  528. if((widthBytes*height)>48)
  529. {
  530. //printf("widthBytes*height>=48\n");
  531. memcpy(font_create.bitmap,ci->bits,48);
  532. }
  533. else
  534. memcpy(font_create.bitmap,ci->bits,widthBytes*height);
  535. }
  536. memcpy(buf+offset,&font_create,sizeof(font_bmf));
  537. offset+=sizeof(font_bmf);
  538. }
  539. printf("count is:%d,before open file\n",count);
  540. fd = fopen(name, "wb");
  541. if(fd!=NULL)
  542. fwrite(buf,1,count*sizeof(font_bmf)+header_len,fd);
  543. fclose(fd);
  544. free(buf);
  545. }
  546. /*void Generate_new_font_with_header(pcffont_t *font)
  547. {
  548. int i,width,height,widthBytes,widthBits,LSBearing,RSBearing,ascent,descent;
  549. CharInfoPtr ci;
  550. char *buf;
  551. FILE *fd;
  552. font_bmf_ex font_create;
  553. char name[256];
  554. int offset = 0;
  555. int count = 0;
  556. int last_char = 0;
  557. int bitmap_offset = 0;
  558. int header_len = 0;
  559. int last_val = 0;
  560. int bmf_buffer_len = 0;
  561. font_bmf_header font_header_t;
  562. strcpy(name,sOutFilename);
  563. //font_count = font->Fmetrics.lastchar;
  564. font_header_t.ascent = font->Fmetrics.ascent;
  565. font_header_t.descent = font->Fmetrics.descent;
  566. font_header_t.linespace = font->Fmetrics.linespace;
  567. font_header_t.last_char = font->Fmetrics.lastchar;
  568. count = font->Fmetrics.lastchar;
  569. for(i=0; i<count;i++)
  570. {
  571. ci = font->bitmaps[i];
  572. if(ci != NULL)
  573. {
  574. width = GLYPHWIDTHPIXELS(ci);
  575. height = GLYPHHEIGHTPIXELS(ci);
  576. widthBytes = GLYPHWIDTHBYTES(ci);
  577. if((widthBytes*height)>last_val)
  578. last_val = widthBytes*height;
  579. }
  580. }
  581. printf("last val:%d\n",last_val);
  582. last_val = 64;
  583. font_header_t.bmp_buffer_len = last_val;
  584. bmf_buffer_len = last_val;
  585. buf = (char*)malloc(count*(sizeof(font_bmf_ex)+bmf_buffer_len)+sizeof(font_bmf_header));
  586. memset(buf,0,count*(sizeof(font_bmf_ex)+bmf_buffer_len)+sizeof(font_bmf_header));
  587. //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));
  588. memcpy(buf+offset,&font_header_t,sizeof(font_header_t));
  589. offset+=sizeof(font_header_t);
  590. header_len = sizeof(font_bmf_ex)*count+sizeof(font_header_t);
  591. for(i=0; i<count;i++)
  592. {
  593. ci = font->bitmaps[i];
  594. //printf("before memset font_create\n");
  595. memset(&font_create,0,sizeof(font_bmf_ex));
  596. if(ci != NULL)
  597. {
  598. width = GLYPHWIDTHPIXELS(ci);
  599. height = GLYPHHEIGHTPIXELS(ci);
  600. widthBytes = GLYPHWIDTHBYTES(ci);
  601. widthBits = GLYPHWIDTHBYTES(ci) * NBPB;
  602. LSBearing = ci->metrics.leftSideBearing;
  603. RSBearing = ci->metrics.rightSideBearing;
  604. ascent = ci->metrics.ascent;
  605. descent = ci->metrics.descent;
  606. if(i==32 && width==0)
  607. {
  608. width = 2;
  609. height = 1;
  610. widthBytes = 1;
  611. widthBits = 8;
  612. RSBearing = 2;
  613. }
  614. font_create.width = (INT8)width;
  615. font_create.height = (INT8)height;
  616. font_create.widthBytes =(INT8)widthBytes;
  617. font_create.widthBits =(INT8)widthBits;
  618. font_create.ascent =(INT8)ascent;
  619. font_create.descent =(INT8)descent;
  620. font_create.LSBearing = (INT8)LSBearing;
  621. font_create.RSBearing = (INT8)RSBearing;
  622. if(width==0 || height==0 || widthBytes==0)
  623. font_create.pos = 0;
  624. else
  625. {
  626. if((widthBytes*height)>bmf_buffer_len)
  627. memcpy(buf+header_len+bitmap_offset,ci->bits,bmf_buffer_len);
  628. else
  629. memcpy(buf+header_len+bitmap_offset,ci->bits,widthBytes*height);
  630. font_create.pos = header_len+bitmap_offset;//record char pos
  631. bitmap_offset+=bmf_buffer_len;
  632. }
  633. }
  634. memcpy(buf+offset,&font_create,sizeof(font_bmf_ex));
  635. offset+=sizeof(font_bmf_ex);
  636. }
  637. printf("count is:%d,before open file\n",count);
  638. fd = fopen(name, "wb");
  639. if(fd!=NULL)
  640. fwrite(buf,1,header_len+bitmap_offset,fd);
  641. fclose(fd);
  642. free(buf);
  643. }*/
  644. /*void Generate_new_font_with_header(pcffont_t *font)
  645. {
  646. int i,width,height,widthBytes,widthBits,LSBearing,RSBearing,ascent,descent;
  647. CharInfoPtr ci;
  648. char *buf;
  649. FILE *fd;
  650. font_bmf_ex font_create;
  651. char name[256];
  652. int offset = 0;
  653. int count = 0;
  654. int last_char = 0;
  655. int bitmap_offset = 0;
  656. int header_len = 0;
  657. int last_val = 0;
  658. font_bmf_header font_header_t;
  659. strcpy(name,sOutFilename);
  660. //font_count = font->Fmetrics.lastchar;
  661. font_header_t.ascent = font->Fmetrics.ascent;
  662. font_header_t.descent = font->Fmetrics.descent;
  663. font_header_t.linespace = font->Fmetrics.linespace;
  664. font_header_t.last_char = font->Fmetrics.lastchar;
  665. count = font->Fmetrics.lastchar;
  666. for(i=0; i<count;i++)
  667. {
  668. ci = font->bitmaps[i];
  669. if(ci != NULL)
  670. {
  671. width = GLYPHWIDTHPIXELS(ci);
  672. height = GLYPHHEIGHTPIXELS(ci);
  673. widthBytes = GLYPHWIDTHBYTES(ci);
  674. if(widthBytes*height>last_val)
  675. {
  676. last_val = widthBytes*height;
  677. //printf("char:%d,width:%d,height:%d,widthBytes:%d\n",i,width,height,widthBytes);
  678. }
  679. }
  680. }
  681. printf("last char:%d\n",last_char);
  682. font_header_t.bmp_buffern_len = last_val;
  683. buf = (char*)malloc(count*(sizeof(font_bmf_ex)+64)+sizeof(font_bmf_header));
  684. memset(buf,0,count*(sizeof(font_bmf_ex)+64)+sizeof(font_bmf_header));
  685. //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));
  686. memcpy(buf+offset,&font_header_t,sizeof(font_header_t));
  687. offset+=sizeof(font_header_t);
  688. header_len = sizeof(font_bmf_ex)*count+sizeof(font_header_t);
  689. for(i=0; i<count;i++)
  690. {
  691. ci = font->bitmaps[i];
  692. //printf("before memset font_create\n");
  693. memset(&font_create,0,sizeof(font_bmf_ex));
  694. if(ci != NULL)
  695. {
  696. width = GLYPHWIDTHPIXELS(ci);
  697. height = GLYPHHEIGHTPIXELS(ci);
  698. widthBytes = GLYPHWIDTHBYTES(ci);
  699. widthBits = GLYPHWIDTHBYTES(ci) * NBPB;
  700. LSBearing = ci->metrics.leftSideBearing;
  701. RSBearing = ci->metrics.rightSideBearing;
  702. ascent = ci->metrics.ascent;
  703. descent = ci->metrics.descent;
  704. if(i==32 && width==0)
  705. {
  706. width = 2;
  707. height = 1;
  708. widthBytes = 1;
  709. widthBits = 8;
  710. RSBearing = 2;
  711. }
  712. font_create.width = (INT8)width;
  713. font_create.height = (INT8)height;
  714. font_create.widthBytes =(INT8)widthBytes;
  715. font_create.widthBits =(INT8)widthBits;
  716. font_create.ascent =(INT8)ascent;
  717. font_create.descent =(INT8)descent;
  718. font_create.LSBearing = (INT8)LSBearing;
  719. font_create.RSBearing = (INT8)RSBearing;
  720. if(width==0 || height==0 || widthBytes==0)
  721. font_create.pos = 0;
  722. else
  723. {
  724. if((widthBytes*height)>64)
  725. memcpy(buf+header_len+bitmap_offset,ci->bits,64);
  726. else
  727. memcpy(buf+header_len+bitmap_offset,ci->bits,widthBytes*height);
  728. font_create.pos = header_len+bitmap_offset;//record char pos
  729. bitmap_offset+=64;
  730. }
  731. }
  732. memcpy(buf+offset,&font_create,sizeof(font_bmf_ex));
  733. offset+=sizeof(font_bmf_ex);
  734. }
  735. printf("count is:%d,before open file\n",count);
  736. fd = fopen(name, "wb");
  737. if(fd!=NULL)
  738. fwrite(buf,1,header_len+bitmap_offset,fd);
  739. fclose(fd);
  740. free(buf);
  741. }*/
  742. /*void Generate_new_font_with_header(pcffont_t *font)
  743. {
  744. int i,width,height,widthBytes,widthBits,LSBearing,RSBearing,ascent,descent;
  745. CharInfoPtr ci;
  746. char *buf;
  747. FILE *fd;
  748. font_bmf font_create;
  749. char name[256];
  750. int offset = 0;
  751. int count = 0;
  752. font_bmf_header font_header_t;
  753. strcpy(name,sOutFilename);
  754. //font_count = font->Fmetrics.lastchar;
  755. font_header_t.ascent = font->Fmetrics.ascent;
  756. font_header_t.descent = font->Fmetrics.descent;
  757. font_header_t.linespace = font->Fmetrics.linespace;
  758. //font_header_t.ptsz = font->Fmetrics.ptsz;
  759. //font_header_t.ptsz = font->Fmetrics.ptsz;
  760. //font_header_t.Yres = font->Fmetrics.Yres;
  761. buf = (char*)malloc(65535*sizeof(font_bmf)+sizeof(font_bmf_header));
  762. memset(buf,0,65535*sizeof(font_bmf)+sizeof(font_bmf_header));
  763. printf("pcf_font_header size is:%d,total size is:%d\n",sizeof(pcf_font_header),65535*sizeof(font_bmf)+sizeof(font_bmf_header));
  764. memcpy(buf+offset,&font_header_t,sizeof(font_header_t));
  765. offset+=sizeof(font_header_t);
  766. for(i=0; i<65535;i++)
  767. {
  768. ci = font->bitmaps[i];
  769. //printf("before memset font_create\n");
  770. memset(&font_create,0,sizeof(font_bmf));
  771. if(ci != NULL)
  772. {
  773. width = GLYPHWIDTHPIXELS(ci);
  774. height = GLYPHHEIGHTPIXELS(ci);
  775. widthBytes = GLYPHWIDTHBYTES(ci);
  776. widthBits = GLYPHWIDTHBYTES(ci) * NBPB;
  777. LSBearing = ci->metrics.leftSideBearing;
  778. RSBearing = ci->metrics.rightSideBearing;
  779. ascent = ci->metrics.ascent;
  780. descent = ci->metrics.descent;
  781. font_create.width = (INT8)width;
  782. font_create.height = (INT8)height;
  783. font_create.widthBytes =(INT8)widthBytes;
  784. font_create.widthBits =(INT8)widthBits;
  785. font_create.ascent =(INT8)ascent;
  786. font_create.descent =(INT8)descent;
  787. font_create.LSBearing = (INT8)LSBearing;
  788. font_create.RSBearing = (INT8)RSBearing;
  789. //printf("before memcpy bitmap\n");
  790. //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);
  791. if((widthBytes*height)>64)
  792. {
  793. //free(buf);
  794. printf("char:%d,widthBytes:%d*height:%d>=64:%d\n",i,widthBytes,height,widthBytes*height);
  795. memcpy(font_create.bitmap,ci->bits,64);
  796. //return;
  797. }
  798. else
  799. memcpy(font_create.bitmap,ci->bits,widthBytes*height);
  800. count++;
  801. }
  802. memcpy(buf+offset,&font_create,sizeof(font_bmf));
  803. offset+=sizeof(font_bmf);
  804. }
  805. printf("count is:%d,before open file\n",count);
  806. fd = fopen(name, "wb");
  807. if(fd!=NULL)
  808. fwrite(buf,1,65535*sizeof(font_bmf)+sizeof(font_header_t),fd);
  809. fclose(fd);
  810. free(buf);
  811. }*/
  812. void
  813. scaling_factors(pcffont_t *font, double ptsz, int Xres, int Yres)
  814. {
  815. font->Yscale = 1;
  816. font->Xscale = 1;
  817. // font->Yscale = ((ptsz * (double)Yres)
  818. // / ((double)font->Fmetrics.ptsz * (double)font->Fmetrics.Xres));
  819. //
  820. // font->Xscale = ((((double)Xres * (double)font->Fmetrics.Yres)
  821. // / ((double)Yres * (double)font->Fmetrics.Xres))
  822. // * (font->Yscale));
  823. #if 0
  824. if (xsflag == OPTARG_SET)
  825. font->Xscale = xs_argval;
  826. else if (xsflag == OPTARG_ADD)
  827. font->Xscale += xs_argval;
  828. else if (xsflag == OPTARG_SUB) /* for clarity, we subract a positive */
  829. font->Xscale -= xs_argval; /* rather than add a negative. */
  830. if (ysflag == OPTARG_SET)
  831. font->Yscale = ys_argval;
  832. else if (ysflag == OPTARG_ADD)
  833. font->Yscale += ys_argval;
  834. else if (ysflag == OPTARG_SUB)
  835. font->Yscale -= ys_argval;
  836. #endif
  837. #ifdef SDEBUG
  838. fprintf(stderr,"%f -- font->Yscale\n %f -- font->Xscale\n", font->Yscale, font->Xscale);
  839. #endif
  840. }
  841. void
  842. scale_Fmetrics(pcffont_t *font) {
  843. font->scFmetrics.ascent = font->Fmetrics.ascent * font->Yscale;
  844. font->scFmetrics.descent = font->Fmetrics.descent * font->Yscale;
  845. font->scFmetrics.linespace = font->Fmetrics.linespace * font->Yscale;
  846. #ifdef SDEBUG
  847. fprintf(stderr,"%f font->scFmetrics.ascent\n \
  848. %f font->scFmetrics.descent\n\
  849. %f font->scFmetrics.linespace\n", font->scFmetrics.ascent, font->scFmetrics.descent, font->scFmetrics.linespace);
  850. #endif
  851. }
  852. void
  853. scale_Cmetrics(pcf_charmet_t *Cm, pcf_SCcharmet_t *Sm)
  854. {
  855. Sm->width = Cm->width * CurrentFont->Xscale;
  856. Sm->height = Cm->height * CurrentFont->Yscale;
  857. Sm->widthBits = Cm->widthBits * CurrentFont->Xscale;
  858. Sm->ascent = Cm->ascent * CurrentFont->Yscale;
  859. Sm->descent = Cm->descent * CurrentFont->Yscale;
  860. Sm->origin_xoff = Cm->origin_xoff * CurrentFont->Xscale;
  861. #ifdef SDEBUG
  862. fprintf(stderr, "%f is CurrentFont->Xscale\n", CurrentFont->Xscale);
  863. fprintf(stderr, "%f is CurrentFont->Yscale\n", CurrentFont->Yscale);
  864. fprintf(stderr, "%d is Cm->widthBits\n", Cm->widthBits);
  865. fprintf(stderr, "%d is Cm->height\n", Cm->height);
  866. fprintf(stderr, "%f is Sm->widthBits\n", Sm->widthBits);
  867. fprintf(stderr, "%f is Sm->height\n", Sm->height);
  868. #endif
  869. /*
  870. debug(dump_scCmetrics(Sm));
  871. */
  872. }
  873. int
  874. pres_pcfbm(ucs4_t *val, pcffont_t *font, pcf_bm_t **bitmap,
  875. pcf_charmet_t *Cmetrics, pcf_SCcharmet_t *scCmetrics, int dontcache)
  876. {
  877. ucs4_t v;
  878. #if SDEBUG
  879. fprintf(stderr, "0x%08x -- ucs4val \n", *val);
  880. #endif
  881. v = *val;
  882. if (is_motion_char(v)) {
  883. *bitmap = NULL;
  884. return XU_MOTION_CHAR;
  885. }
  886. if (non_graphic_char(v)) {
  887. *bitmap = NULL;
  888. return XU_IGNORE;
  889. }
  890. if ((*bitmap = (pcf_bm_t *)xpcf_getcbm(v, font, Cmetrics)) == NULL) {
  891. if (handle_nobitmap(&v, font, Cmetrics, bitmap) == XU_IGNORE)
  892. return XU_IGNORE;
  893. }
  894. #if SDEBUG
  895. fprintf(stderr, "0x%02x -- **bitmap \n", (unsigned char)**bitmap);
  896. #endif
  897. *val = v;
  898. scale_Cmetrics(Cmetrics, scCmetrics);
  899. return(1);
  900. }
  901. #ifdef DEBUG
  902. static void
  903. dump_Cmetrics(pcf_charmet_t *cm)
  904. {
  905. dprintf2("cmetrics: ascent:%d, descent:%d, ", cm->ascent, cm->descent);
  906. dprintf3("width:%d, height:%d, widthBytes:%d, ",
  907. cm->width, cm->height, cm->widthBytes);
  908. dprintf3("LSB:%d,RSB: %d, xoff:%d\n ",
  909. cm->LSBearing, cm->RSBearing, cm->origin_xoff);
  910. }
  911. static void
  912. dump_Fmetrics(pcffont_t *fm)
  913. {
  914. dprintf6("Fmetrics: ascent:%d, descent:%d, linespace:%d, "
  915. "ptsz:%d, Xres:%d, Yres:%d\n", fm->ascent, fm->descent,
  916. fm->linespace, fm->ptsz, fm->Xres, fm->Yres);
  917. }
  918. #endif
  919. unsigned long *
  920. Xrealloc (unsigned long *n, int m)
  921. {
  922. if (!n)
  923. return (unsigned long *) malloc (m);
  924. else
  925. return (unsigned long *) realloc ((char *) n, m);
  926. }
  927. unsigned long *
  928. Xalloc (int m)
  929. {
  930. return (unsigned long *) malloc (m);
  931. }
  932. /*
  933. * Invert byte order within each 16-bits of an array.
  934. */
  935. void
  936. TwoByteSwap(unsigned char *buf, int nbytes)
  937. {
  938. register unsigned char c;
  939. for (; nbytes > 0; nbytes -= 2, buf += 2)
  940. {
  941. c = buf[0];
  942. buf[0] = buf[1];
  943. buf[1] = c;
  944. }
  945. }
  946. /*
  947. * Invert byte order within each 32-bits of an array.
  948. */
  949. static void
  950. FourByteSwap(unsigned char *buf, int nbytes)
  951. {
  952. register unsigned char c;
  953. for (; nbytes > 0; nbytes -= 4, buf += 4)
  954. {
  955. c = buf[0];
  956. buf[0] = buf[3];
  957. buf[3] = c;
  958. c = buf[1];
  959. buf[1] = buf[2];
  960. buf[2] = c;
  961. }
  962. }
  963. #if 0
  964. static int
  965. handle_nongraphchar(ucs4_t *val, int *ndx)
  966. {
  967. return XU_IGNORE;
  968. }
  969. #endif
  970. static Bool
  971. pcfHasType (PCFTablePtr tables, int ntables, CARD32 type)
  972. {
  973. int i;
  974. for (i = 0; i < ntables; i++)
  975. if (tables[i].type == type)
  976. return TRUE;
  977. return FALSE;
  978. }
  979. /*
  980. * Invert bit order within each BYTE of an array.
  981. */
  982. static void
  983. BitOrderInvert(unsigned char *buf, int nbytes)
  984. {
  985. register unsigned char *rev = _reverse_byte;
  986. for (; --nbytes >= 0; buf++)
  987. *buf = rev[*buf];
  988. }
  989. static pcf_bm_t *
  990. xpcf_getcbm(ucs4_t code, pcffont_t *font, pcf_charmet_t *Cmetrics)
  991. {
  992. int j;
  993. CharInfoPtr ci;
  994. assert(font->loaded);
  995. /*
  996. * For the default no glyph character to appear in
  997. * output stream
  998. */
  999. onemoretime:
  1000. if (code < font->Fmetrics.firstchar || code > font->Fmetrics.lastchar)
  1001. return NULL;
  1002. j = (code - (((code - font->Fmetrics.firstchar) / 256)
  1003. * (font->Fmetrics.firstCol + (255 - font->Fmetrics.lastCol))))
  1004. - font->Fmetrics.firstchar;
  1005. #ifdef SDEBUG
  1006. fprintf(stderr, "%d is -- codewidth of C%x\n", wcwidth(code), code);
  1007. fprintf(stderr, "%d is -- j value C%x\n", wcwidth(code), code);
  1008. #endif
  1009. assert(j >= 0);
  1010. if (font->bitmaps[j] == NULL)
  1011. {
  1012. /*
  1013. * code added to replace the codepoints with
  1014. * no-glyph code
  1015. */
  1016. if (wcwidth(code) == -1)
  1017. {
  1018. code = 0;
  1019. goto onemoretime;
  1020. }
  1021. return NULL;
  1022. }
  1023. ci = font->bitmaps[j];
  1024. Cmetrics->width = GLYPHWIDTHPIXELS(ci);
  1025. Cmetrics->height = GLYPHHEIGHTPIXELS(ci);
  1026. Cmetrics->widthBytes = GLYPHWIDTHBYTES(ci);
  1027. Cmetrics->widthBits = GLYPHWIDTHBYTES(ci) * NBPB;
  1028. Cmetrics->ascent = ci->metrics.ascent;
  1029. Cmetrics->descent = ci->metrics.descent;
  1030. Cmetrics->LSBearing = ci->metrics.leftSideBearing;
  1031. Cmetrics->RSBearing = ci->metrics.rightSideBearing;
  1032. Cmetrics->origin_xoff = ci->metrics.characterWidth;
  1033. #ifdef SDEBUG
  1034. fprintf(stderr, "%d is -- Cmetrics->widthBytes of C%x\n", Cmetrics->widthBytes, code);
  1035. fprintf(stderr, "%d is -- Cmetrics->height value of C%x\n", Cmetrics->height, code);
  1036. #endif
  1037. #if 0
  1038. if (cwflag) {
  1039. if (cwflag == OPTARG_ADD)
  1040. Cmetrics->origin_xoff += cw_argval;
  1041. else if (cwflag == OPTARG_SUB)
  1042. Cmetrics->origin_xoff -= cw_argval;
  1043. else if (cwflag == OPTARG_SET)
  1044. Cmetrics->origin_xoff = cw_argval;
  1045. }
  1046. #endif
  1047. debug(dump_Cmetrics(Cmetrics));
  1048. return (pcf_bm_t*) ci->bits;
  1049. }
  1050. /*
  1051. * Repad a bitmap
  1052. */
  1053. static int
  1054. RepadBitmap (char *pSrc, char *pDst, unsigned srcPad, unsigned dstPad, int width, int height)
  1055. {
  1056. int srcWidthBytes,dstWidthBytes;
  1057. int row,col;
  1058. char *pTmpSrc,*pTmpDst;
  1059. switch (srcPad) {
  1060. case 1:
  1061. srcWidthBytes = (width+7)>>3;
  1062. break;
  1063. case 2:
  1064. srcWidthBytes = ((width+15)>>4)<<1;
  1065. break;
  1066. case 4:
  1067. srcWidthBytes = ((width+31)>>5)<<2;
  1068. break;
  1069. case 8:
  1070. srcWidthBytes = ((width+63)>>6)<<3;
  1071. break;
  1072. default:
  1073. return 0;
  1074. }
  1075. switch (dstPad) {
  1076. case 1:
  1077. dstWidthBytes = (width+7)>>3;
  1078. break;
  1079. case 2:
  1080. dstWidthBytes = ((width+15)>>4)<<1;
  1081. break;
  1082. case 4:
  1083. dstWidthBytes = ((width+31)>>5)<<2;
  1084. break;
  1085. case 8:
  1086. dstWidthBytes = ((width+63)>>6)<<3;
  1087. break;
  1088. default:
  1089. return 0;
  1090. }
  1091. width = srcWidthBytes;
  1092. if (width > dstWidthBytes)
  1093. width = dstWidthBytes;
  1094. pTmpSrc= pSrc;
  1095. pTmpDst= pDst;
  1096. for (row = 0; row < height; row++)
  1097. {
  1098. for (col = 0; col < width; col++)
  1099. *pTmpDst++ = *pTmpSrc++;
  1100. while (col < dstWidthBytes)
  1101. {
  1102. *pTmpDst++ = '\0';
  1103. col++;
  1104. }
  1105. pTmpSrc += srcWidthBytes - width;
  1106. }
  1107. return dstWidthBytes * height;
  1108. }
  1109. #if 0
  1110. static int
  1111. handle_cuferr(int err, ucs4_t *v, int *ndx)
  1112. {
  1113. if (err == CUF_ILCH) {
  1114. return handle_illegalchar(v, ndx);
  1115. } else if (err == CUF_NICH) {
  1116. return handle_nonidentchar(v, ndx);
  1117. } else {
  1118. return 0;
  1119. }
  1120. }
  1121. static int
  1122. handle_illegalchar(ucs4_t *val, int *ndx)
  1123. {
  1124. return XU_IGNORE;
  1125. }
  1126. static int
  1127. handle_nonidentchar(ucs4_t *val, int *ndx)
  1128. {
  1129. return XU_IGNORE;
  1130. }
  1131. #endif
  1132. static int
  1133. handle_nobitmap(ucs4_t *val, pcffont_t *font, pcf_charmet_t *Cm, pcf_bm_t **bitmap) {
  1134. int fv;
  1135. /* Becasue NOBITMAPREPL is hard coded for FALLBACK_FONT */
  1136. if(strstr(font->file, FALLBACK_FONT)==NULL)
  1137. return XU_IGNORE;
  1138. *val = NOBITMAPREPL;
  1139. if ((fv = font->cuf(*val)) < 0) return XU_IGNORE;
  1140. if ((*bitmap = xpcf_getcbm(fv, font, Cm)) == NULL) return XU_IGNORE;
  1141. return(1);
  1142. }
  1143. static int
  1144. BufFileRawFill (BufFilePtr f)
  1145. {
  1146. int left;
  1147. // left = read (FileDes(f), f->buffer, BUFFILESIZE);
  1148. left = fread (f->buffer, 1, BUFFILESIZE, FileDes(f));
  1149. if (left <= 0) {
  1150. f->left = 0;
  1151. return BUFFILEEOF;
  1152. }
  1153. f->left = left - 1;
  1154. f->bufp = f->buffer + 1;
  1155. return f->buffer[0];
  1156. }
  1157. static int
  1158. BufFileRawSkip (BufFilePtr f, int count)
  1159. {
  1160. int curoff;
  1161. int fileoff;
  1162. int todo;
  1163. curoff = f->bufp - f->buffer;
  1164. fileoff = curoff + f->left;
  1165. if (curoff + count <= fileoff) {
  1166. f->bufp += count;
  1167. f->left -= count;
  1168. } else {
  1169. todo = count - (fileoff - curoff);
  1170. // if (lseek (FileDes(f), todo, 1) == -1) {
  1171. if (fseek (FileDes(f), todo, 1) == -1) {
  1172. if (errno != ESPIPE)
  1173. return BUFFILEEOF;
  1174. while (todo) {
  1175. curoff = BUFFILESIZE;
  1176. if (curoff > todo)
  1177. curoff = todo;
  1178. // fileoff = read (FileDes(f), f->buffer, curoff);
  1179. fileoff = fread (f->buffer, 1, curoff, FileDes(f));
  1180. if (fileoff <= 0)
  1181. return BUFFILEEOF;
  1182. todo -= fileoff;
  1183. }
  1184. }
  1185. f->left = 0;
  1186. }
  1187. return count;
  1188. }
  1189. static int
  1190. BufFileRawClose (BufFilePtr f, int doClose)
  1191. {
  1192. if (doClose)
  1193. // close (FileDes (f));
  1194. fclose (FileDes (f));
  1195. return 1;
  1196. }
  1197. static Bool
  1198. NameEqual (char *a, char *b, int l)
  1199. {
  1200. while (l--)
  1201. if (*a++ != *b++)
  1202. return FALSE;
  1203. return TRUE;
  1204. }
  1205. static BufFilePtr
  1206. //BufFileOpenRead(int fd)
  1207. BufFileOpenRead(FILE *fd)
  1208. {
  1209. return BufFileCreate ((char *) fd, BufFileRawFill, BufFileRawSkip, BufFileRawClose);
  1210. }
  1211. static AtomListPtr *reverseMap;
  1212. static int reverseMapSize;
  1213. static Atom lastAtom;
  1214. static Bool ResizeReverseMap ()
  1215. {
  1216. if (reverseMapSize == 0)
  1217. reverseMapSize = 1000;
  1218. else
  1219. reverseMapSize *= 2;
  1220. reverseMap = (AtomListPtr *) Xrealloc ((ulong_t *)reverseMap, reverseMapSize * sizeof (AtomListPtr));
  1221. if (!reverseMap)
  1222. return FALSE;
  1223. else
  1224. return TRUE;
  1225. }
  1226. static char *
  1227. NameForAtom(Atom atom)
  1228. {
  1229. if (atom != None && atom <= lastAtom)
  1230. return reverseMap[atom]->name;
  1231. return 0;
  1232. }
  1233. /*****************************************************************
  1234. * TAG( getcode )
  1235. *
  1236. * Read one code from the standard input. If BUFFILEEOF, return -1.
  1237. * Inputs:
  1238. * stdin
  1239. * Outputs:
  1240. * code or -1 is returned.
  1241. */
  1242. static char_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
  1243. static code_int
  1244. getcode(CompressedFile *file)
  1245. {
  1246. register code_int code;
  1247. register int r_off, bits;
  1248. register char_type *bp = file->buf;
  1249. register BufFilePtr raw;
  1250. if ( file->clear_flg > 0 || file->offset >= file->size ||
  1251. file->free_ent > file->maxcode )
  1252. {
  1253. /*
  1254. * If the next entry will be too big for the current code
  1255. * size, then we must increase the size. This implies reading
  1256. * a new buffer full, too.
  1257. */
  1258. if ( file->free_ent > file->maxcode ) {
  1259. file->n_bits++;
  1260. if ( file->n_bits == file->maxbits )
  1261. file->maxcode = file->maxmaxcode; /* won't get any bigger now */
  1262. else
  1263. file->maxcode = MAXCODE(file->n_bits);
  1264. }
  1265. if ( file->clear_flg > 0) {
  1266. file->maxcode = MAXCODE (file->n_bits = INIT_BITS);
  1267. file->clear_flg = 0;
  1268. }
  1269. bits = file->n_bits;
  1270. raw = file->file;
  1271. while (bits > 0 && (code = BufFileGet (raw)) != BUFFILEEOF)
  1272. {
  1273. *bp++ = code;
  1274. --bits;
  1275. }
  1276. bp = file->buf;
  1277. if (bits == file->n_bits)
  1278. return -1; /* end of file */
  1279. file->size = file->n_bits - bits;
  1280. file->offset = 0;
  1281. /* Round size down to integral number of codes */
  1282. file->size = (file->size << 3) - (file->n_bits - 1);
  1283. }
  1284. r_off = file->offset;
  1285. bits = file->n_bits;
  1286. /*
  1287. * Get to the first byte.
  1288. */
  1289. bp += (r_off >> 3);
  1290. r_off &= 7;
  1291. /* Get first part (low order bits) */
  1292. #ifdef NO_UCHAR
  1293. code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff;
  1294. #else
  1295. code = (*bp++ >> r_off);
  1296. #endif /* NO_UCHAR */
  1297. bits -= (8 - r_off);
  1298. r_off = 8 - r_off; /* now, offset into code word */
  1299. /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
  1300. if ( bits >= 8 ) {
  1301. #ifdef NO_UCHAR
  1302. code |= (*bp++ & 0xff) << r_off;
  1303. #else
  1304. code |= *bp++ << r_off;
  1305. #endif /* NO_UCHAR */
  1306. r_off += 8;
  1307. bits -= 8;
  1308. }
  1309. /* high order bits. */
  1310. if (rmask[bits])
  1311. code |= (*bp & rmask[bits]) << r_off;
  1312. file->offset += file->n_bits;
  1313. return code;
  1314. }
  1315. void
  1316. BufFileClose (BufFilePtr f, int doClose)
  1317. {
  1318. (void) (*f->close) (f, doClose);
  1319. xfree (f);
  1320. }
  1321. static int
  1322. get_font_property(FontPtr Fp, char *name, ulong_t *value)
  1323. {
  1324. int j;
  1325. int n;
  1326. char *prop_name;
  1327. n = Fp->info.nprops;
  1328. for (j = 0; j < n; j++) {
  1329. if (!(prop_name = NameForAtom(Fp->info.props[j].name)))
  1330. continue;
  1331. if (eq(name, prop_name)) {
  1332. *value = (ulong_t) Fp->info.props[j].value;
  1333. return 0;
  1334. }
  1335. }
  1336. return -1;
  1337. }
  1338. static BufFilePtr
  1339. BufFileCreate (char *_private, int (*io)(BufFilePtr), int (*skip)(BufFilePtr, int), int (*close)(BufFilePtr, int))
  1340. {
  1341. BufFilePtr f;
  1342. f = (BufFilePtr) xalloc (sizeof *f);
  1343. if (!f)
  1344. return 0;
  1345. f->_private = _private;
  1346. f->bufp = f->buffer;
  1347. f->left = 0;
  1348. f->io = io;
  1349. f->skip = skip;
  1350. f->close = close;
  1351. return f;
  1352. }
  1353. int hsize_table[] = {
  1354. 5003, /* 12 bits - 80% occupancy */
  1355. 9001, /* 13 bits - 91% occupancy */
  1356. 18013, /* 14 bits - 91% occupancy */
  1357. 35023, /* 15 bits - 94% occupancy */
  1358. 69001 /* 16 bits - 95% occupancy */
  1359. };
  1360. static int BufCompressedFill(), BufCompressedSkip(), BufCompressedClose();
  1361. static int
  1362. BufCompressedClose (BufFilePtr f, int doClose)
  1363. {
  1364. CompressedFile *file;
  1365. BufFilePtr raw;
  1366. file = (CompressedFile *) f->_private;
  1367. raw = file->file;
  1368. xfree (file);
  1369. BufFileClose (raw, doClose);
  1370. return 1;
  1371. }
  1372. static int
  1373. BufCompressedSkip (BufFilePtr f, int bytes)
  1374. {
  1375. //int c;
  1376. while (bytes--) {
  1377. if (BufFileGet(f) == BUFFILEEOF)
  1378. return BUFFILEEOF;
  1379. }
  1380. return 0;
  1381. }
  1382. static int
  1383. BufCompressedFill (BufFilePtr f)
  1384. {
  1385. CompressedFile *file;
  1386. register char_type *stackp, *de_stack;
  1387. register char_type finchar;
  1388. register code_int code, oldcode, incode;
  1389. BufChar *buf, *bufend;
  1390. file = (CompressedFile *) f->_private;
  1391. buf = f->buffer;
  1392. bufend = buf + BUFFILESIZE;
  1393. stackp = file->stackp;
  1394. de_stack = file->de_stack;
  1395. finchar = file->finchar;
  1396. oldcode = file->oldcode;
  1397. while (buf < bufend) {
  1398. while (stackp > de_stack && buf < bufend)
  1399. *buf++ = *--stackp;
  1400. if (buf == bufend)
  1401. break;
  1402. if (oldcode == -1)
  1403. break;
  1404. code = getcode (file);
  1405. if (code == -1)
  1406. break;
  1407. if ( (code == CLEAR) && file->block_compress ) {
  1408. for ( code = 255; code >= 0; code-- )
  1409. file->tab_prefix[code] = 0;
  1410. file->clear_flg = 1;
  1411. file->free_ent = FIRST - 1;
  1412. if ( (code = getcode (file)) == -1 ) /* O, untimely death! */
  1413. break;
  1414. }
  1415. incode = code;
  1416. /*
  1417. * Special case for KwKwK string.
  1418. */
  1419. if ( code >= file->free_ent ) {
  1420. *stackp++ = finchar;
  1421. code = oldcode;
  1422. }
  1423. /*
  1424. * Generate output characters in reverse order
  1425. */
  1426. while ( code >= 256 )
  1427. {
  1428. *stackp++ = file->tab_suffix[code];
  1429. code = file->tab_prefix[code];
  1430. }
  1431. finchar = file->tab_suffix[code];
  1432. *stackp++ = finchar;
  1433. /*
  1434. * Generate the new entry.
  1435. */
  1436. if ( (code=file->free_ent) < file->maxmaxcode ) {
  1437. file->tab_prefix[code] = (unsigned short)oldcode;
  1438. file->tab_suffix[code] = finchar;
  1439. file->free_ent = code+1;
  1440. }
  1441. /*
  1442. * Remember previous code.
  1443. */
  1444. oldcode = incode;
  1445. }
  1446. file->oldcode = oldcode;
  1447. file->stackp = stackp;
  1448. file->finchar = finchar;
  1449. if (buf == f->buffer) {
  1450. f->left = 0;
  1451. return BUFFILEEOF;
  1452. }
  1453. f->bufp = f->buffer + 1;
  1454. f->left = (buf - f->buffer) - 1;
  1455. return f->buffer[0];
  1456. }
  1457. BufFilePtr
  1458. BufFilePushCompressed (BufFilePtr f)
  1459. {
  1460. int code;
  1461. int maxbits;
  1462. int hsize;
  1463. CompressedFile *file;
  1464. int extra;
  1465. if ((BufFileGet(f) != (magic_header[0] & 0xFF)) ||
  1466. (BufFileGet(f) != (magic_header[1] & 0xFF)))
  1467. {
  1468. return 0;
  1469. }
  1470. code = BufFileGet (f);
  1471. maxbits = code & BIT_MASK;
  1472. if (maxbits > BITS || maxbits < 12)
  1473. return 0;
  1474. hsize = hsize_table[maxbits - 12];
  1475. extra = (1 << maxbits) * sizeof (char_type) +
  1476. hsize * sizeof (unsigned short);
  1477. file = (CompressedFile *) xalloc (sizeof (CompressedFile) + extra);
  1478. if (!file)
  1479. return 0;
  1480. file->file = f;
  1481. file->maxbits = maxbits;
  1482. file->block_compress = code & BLOCK_MASK;
  1483. file->maxmaxcode = 1 << file->maxbits;
  1484. file->tab_suffix = (char_type *) &file[1];
  1485. file->tab_prefix = (unsigned short *) (file->tab_suffix + file->maxmaxcode);
  1486. /*
  1487. * As above, initialize the first 256 entries in the table.
  1488. */
  1489. file->maxcode = MAXCODE(file->n_bits = INIT_BITS);
  1490. for ( code = 255; code >= 0; code-- ) {
  1491. file->tab_prefix[code] = 0;
  1492. file->tab_suffix[code] = (char_type) code;
  1493. }
  1494. file->free_ent = ((file->block_compress) ? FIRST : 256 );
  1495. file->clear_flg = 0;
  1496. file->offset = 0;
  1497. file->size = 0;
  1498. file->stackp = file->de_stack;
  1499. file->finchar = file->oldcode = getcode (file);
  1500. if (file->oldcode != -1)
  1501. *file->stackp++ = file->finchar;
  1502. return BufFileCreate ((char *) file,
  1503. BufCompressedFill,
  1504. BufCompressedSkip,
  1505. BufCompressedClose);
  1506. }
  1507. static FontFilePtr
  1508. FontFileOpen (char *name) {
  1509. // int fd;
  1510. FILE *fd;
  1511. int len;
  1512. BufFilePtr raw, cooked;
  1513. len = strlen (name);
  1514. /*
  1515. * A little hack for .gz file support.
  1516. * We gzcat the file and will treat the
  1517. * resultant fd as a regular file's.
  1518. */
  1519. // if (len > 3 && !strcmp (name + len - 3, ".gz"))
  1520. // fd = gzcatfile (name);
  1521. // else
  1522. // fd = open (name, 0);
  1523. fd = fopen(name, "rb");
  1524. // if (fd < 0)
  1525. if (!fd)
  1526. return 0;
  1527. raw = BufFileOpenRead (fd);
  1528. if (!raw)
  1529. {
  1530. // close (fd);
  1531. fclose (fd);
  1532. return 0;
  1533. }
  1534. if (len > 2 && !strcmp (name + len - 2, ".Z")) {
  1535. cooked = BufFilePushCompressed (raw);
  1536. if (!cooked) {
  1537. BufFileClose (raw, TRUE);
  1538. return 0;
  1539. }
  1540. raw = cooked;
  1541. }
  1542. return (FontFilePtr) raw;
  1543. }
  1544. static void FontFileClose (FontFilePtr f)
  1545. {
  1546. BufFileClose ((BufFilePtr) f, TRUE);
  1547. }
  1548. static int
  1549. pcfGetLSB32(FontFilePtr file)
  1550. {
  1551. int c;
  1552. c = FontFileGetc(file);
  1553. c |= FontFileGetc(file) << 8;
  1554. c |= FontFileGetc(file) << 16;
  1555. c |= FontFileGetc(file) << 24;
  1556. position += 4;
  1557. return c;
  1558. }
  1559. static PCFTablePtr
  1560. pcfReadTOC(FontFilePtr file, int *countp)
  1561. {
  1562. CARD32 version;
  1563. PCFTablePtr tables;
  1564. int count;
  1565. int i;
  1566. position = 0;
  1567. version = pcfGetLSB32(file);
  1568. if (version != PCF_FILE_VERSION)
  1569. return (PCFTablePtr) NULL;
  1570. count = pcfGetLSB32(file);
  1571. tables = (PCFTablePtr) xalloc(count * sizeof(PCFTableRec));
  1572. if (!tables)
  1573. return (PCFTablePtr) NULL;
  1574. for (i = 0; i < count; i++) {
  1575. tables[i].type = pcfGetLSB32(file);
  1576. tables[i].format = pcfGetLSB32(file);
  1577. tables[i].size = pcfGetLSB32(file);
  1578. tables[i].offset = pcfGetLSB32(file);
  1579. }
  1580. *countp = count;
  1581. return tables;
  1582. }
  1583. static Bool
  1584. ResizeHashTable ()
  1585. {
  1586. int newHashSize;
  1587. int newHashMask;
  1588. AtomListPtr *newHashTable;
  1589. int i;
  1590. int h;
  1591. int newRehash;
  1592. int r;
  1593. if (hashSize == 0)
  1594. newHashSize = 1024;
  1595. else
  1596. newHashSize = hashSize * 2;
  1597. newHashTable = (AtomListPtr *) xalloc (newHashSize * sizeof (AtomListPtr));
  1598. if (!newHashTable)
  1599. return FALSE;
  1600. bzero ((char *) newHashTable, newHashSize * sizeof (AtomListPtr));
  1601. newHashMask = newHashSize - 1;
  1602. newRehash = (newHashMask - 2);
  1603. for (i = 0; i < hashSize; i++)
  1604. {
  1605. if (hashTable[i])
  1606. {
  1607. h = (hashTable[i]->hash) & newHashMask;
  1608. if (newHashTable[h])
  1609. {
  1610. r = hashTable[i]->hash % newRehash | 1;
  1611. do {
  1612. h += r;
  1613. if (h >= newHashSize)
  1614. h -= newHashSize;
  1615. } while (newHashTable[h]);
  1616. }
  1617. newHashTable[h] = hashTable[i];
  1618. }
  1619. }
  1620. xfree (hashTable);
  1621. hashTable = newHashTable;
  1622. hashSize = newHashSize;
  1623. hashMask = newHashMask;
  1624. rehash = newRehash;
  1625. return TRUE;
  1626. }
  1627. static int
  1628. Hash(char *string, int len)
  1629. {
  1630. int h;
  1631. h = 0;
  1632. while (len--)
  1633. h = (h << 3) ^ *string++;
  1634. if (h < 0)
  1635. return -h;
  1636. return h;
  1637. }
  1638. static Atom
  1639. MakeAtom(char *string, unsigned len, int makeit)
  1640. {
  1641. AtomListPtr a;
  1642. int hash;
  1643. int h = 0;
  1644. int r;
  1645. hash = Hash (string, len);
  1646. if (hashTable)
  1647. {
  1648. h = hash & hashMask;
  1649. if (hashTable[h])
  1650. {
  1651. if (hashTable[h]->hash == hash && hashTable[h]->len == len &&
  1652. NameEqual (hashTable[h]->name, string, len))
  1653. {
  1654. return hashTable[h]->atom;
  1655. }
  1656. r = (hash % rehash) | 1;
  1657. for (;;)
  1658. {
  1659. h += r;
  1660. if (h >= hashSize)
  1661. h -= hashSize;
  1662. if (!hashTable[h])
  1663. break;
  1664. if (hashTable[h]->hash == hash && hashTable[h]->len == len &&
  1665. NameEqual (hashTable[h]->name, string, len))
  1666. {
  1667. return hashTable[h]->atom;
  1668. }
  1669. }
  1670. }
  1671. }
  1672. if (!makeit)
  1673. return None;
  1674. a = (AtomListPtr) xalloc (sizeof (AtomListRec) + len + 1);
  1675. a->name = (char *) (a + 1);
  1676. a->len = len;
  1677. strncpy (a->name, string, len);
  1678. a->name[len] = '\0';
  1679. a->atom = ++lastAtom;
  1680. a->hash = hash;
  1681. if (hashUsed >= hashSize / 2)
  1682. {
  1683. ResizeHashTable ();
  1684. h = hash & hashMask;
  1685. if (hashTable[h])
  1686. {
  1687. r = (hash % rehash) | 1;
  1688. do {
  1689. h += r;
  1690. if (h >= hashSize)
  1691. h -= hashSize;
  1692. } while (hashTable[h]);
  1693. }
  1694. }
  1695. hashTable[h] = a;
  1696. hashUsed++;
  1697. if (reverseMapSize <= a->atom)
  1698. ResizeReverseMap();
  1699. reverseMap[a->atom] = a;
  1700. return a->atom;
  1701. }
  1702. int BufFileRead (BufFilePtr f, char *b, int n)
  1703. {
  1704. int c, cnt;
  1705. cnt = n;
  1706. while (cnt--) {
  1707. c = BufFileGet (f);
  1708. if (c == BUFFILEEOF)
  1709. break;
  1710. *b++ = c;
  1711. }
  1712. return n - cnt - 1;
  1713. }
  1714. static Bool
  1715. pcfGetProperties(FontInfoPtr pFontInfo, FontFilePtr file, PCFTablePtr tables, int ntables)
  1716. {
  1717. FontPropPtr props = 0;
  1718. int nprops;
  1719. char *isStringProp = 0;
  1720. CARD32 format;
  1721. int i;
  1722. /* changed by suresh 07/12/99 from int to CARD 32*/
  1723. CARD32 size;
  1724. int string_size;
  1725. char *strings;
  1726. /* font properties */
  1727. if (!pcfSeekToType(file, tables, ntables, PCF_PROPERTIES, &format, &size))
  1728. goto Bail;
  1729. format = pcfGetLSB32(file);
  1730. if (!PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT))
  1731. goto Bail;
  1732. nprops = pcfGetINT32(file, format);
  1733. props = (FontPropPtr) xalloc(nprops * sizeof(FontPropRec));
  1734. if (!props)
  1735. goto Bail;
  1736. isStringProp = (char *) xalloc(nprops * sizeof(char));
  1737. if (!isStringProp)
  1738. goto Bail;
  1739. for (i = 0; i < nprops; i++) {
  1740. props[i].name = pcfGetINT32(file, format);
  1741. isStringProp[i] = pcfGetINT8(file, format);
  1742. props[i].value = pcfGetINT32(file, format);
  1743. }
  1744. /* pad the property array */
  1745. /*
  1746. * clever here - nprops is the same as the number of odd-units read, as
  1747. * only isStringProp are odd length
  1748. */
  1749. if (nprops & 3)
  1750. {
  1751. i = 4 - (nprops & 3);
  1752. (void)FontFileSkip(file, i);
  1753. position += i;
  1754. }
  1755. string_size = pcfGetINT32(file, format);
  1756. strings = (char *) xalloc(string_size);
  1757. if (!strings) {
  1758. goto Bail;
  1759. }
  1760. FontFileRead(file, strings, string_size);
  1761. position += string_size;
  1762. for (i = 0; i < nprops; i++) {
  1763. props[i].name = MakeAtom(strings + props[i].name,
  1764. strlen(strings + props[i].name), TRUE);
  1765. if (isStringProp[i]) {
  1766. props[i].value = MakeAtom(strings + props[i].value,
  1767. strlen(strings + props[i].value), TRUE);
  1768. }
  1769. }
  1770. xfree(strings);
  1771. pFontInfo->isStringProp = isStringProp;
  1772. pFontInfo->props = props;
  1773. pFontInfo->nprops = nprops;
  1774. return TRUE;
  1775. Bail:
  1776. xfree(isStringProp);
  1777. xfree(props);
  1778. return FALSE;
  1779. }
  1780. static void
  1781. pcfGetCompressedMetric(FontFilePtr file, CARD32 format, xCharInfo *metric)
  1782. {
  1783. metric->leftSideBearing = pcfGetINT8(file, format) - 0x80;
  1784. metric->rightSideBearing = pcfGetINT8(file, format) - 0x80;
  1785. metric->characterWidth = pcfGetINT8(file, format) - 0x80;
  1786. metric->ascent = pcfGetINT8(file, format) - 0x80;
  1787. metric->descent = pcfGetINT8(file, format) - 0x80;
  1788. metric->attributes = 0;
  1789. }
  1790. static Bool
  1791. pcfSeekToType(FontFilePtr file, PCFTablePtr tables, int ntables, CARD32 type, CARD32 *formatp, CARD32 *sizep)
  1792. {
  1793. int i;
  1794. for (i = 0; i < ntables; i++)
  1795. if (tables[i].type == type) {
  1796. if (position > tables[i].offset)
  1797. return FALSE;
  1798. if (!FontFileSkip(file, tables[i].offset - position))
  1799. return FALSE;
  1800. position = tables[i].offset;
  1801. *sizep = tables[i].size;
  1802. *formatp = tables[i].format;
  1803. return TRUE;
  1804. }
  1805. return FALSE;
  1806. }
  1807. static int
  1808. pcfGetINT32(FontFilePtr file, CARD32 format)
  1809. {
  1810. int c;
  1811. if (PCF_BYTE_ORDER(format) == MSBFirst) {
  1812. c = FontFileGetc(file) << 24;
  1813. c |= FontFileGetc(file) << 16;
  1814. c |= FontFileGetc(file) << 8;
  1815. c |= FontFileGetc(file);
  1816. } else {
  1817. c = FontFileGetc(file);
  1818. c |= FontFileGetc(file) << 8;
  1819. c |= FontFileGetc(file) << 16;
  1820. c |= FontFileGetc(file) << 24;
  1821. }
  1822. position += 4;
  1823. return c;
  1824. }
  1825. static int
  1826. pcfGetINT16(FontFilePtr file, CARD32 format)
  1827. {
  1828. int c;
  1829. if (PCF_BYTE_ORDER(format) == MSBFirst) {
  1830. c = FontFileGetc(file) << 8;
  1831. c |= FontFileGetc(file);
  1832. } else {
  1833. c = FontFileGetc(file);
  1834. c |= FontFileGetc(file) << 8;
  1835. }
  1836. position += 2;
  1837. return c;
  1838. }
  1839. /*
  1840. * pcfReadAccel
  1841. *
  1842. * Fill in the accelerator information from the font file; used
  1843. * to read both BDF_ACCELERATORS and old style ACCELERATORS
  1844. */
  1845. static Bool
  1846. pcfGetAccel(FontInfoPtr pFontInfo, FontFilePtr file, PCFTablePtr tables, int ntables, CARD32 type)
  1847. {
  1848. CARD32 format;
  1849. /* changed by suresh 07/12/99 from int to CARD 32*/
  1850. CARD32 size;
  1851. if (!pcfSeekToType(file, tables, ntables, type, &format, &size))
  1852. goto Bail;
  1853. format = pcfGetLSB32(file);
  1854. if (!PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT) &&
  1855. !PCF_FORMAT_MATCH(format, PCF_ACCEL_W_INKBOUNDS))
  1856. {
  1857. goto Bail;
  1858. }
  1859. pFontInfo->noOverlap = pcfGetINT8(file, format);
  1860. pFontInfo->constantMetrics = pcfGetINT8(file, format);
  1861. pFontInfo->terminalFont = pcfGetINT8(file, format);
  1862. pFontInfo->constantWidth = pcfGetINT8(file, format);
  1863. pFontInfo->inkInside = pcfGetINT8(file, format);
  1864. pFontInfo->inkMetrics = pcfGetINT8(file, format);
  1865. pFontInfo->drawDirection = pcfGetINT8(file, format);
  1866. pFontInfo->anamorphic = FALSE;
  1867. /* natural alignment */ pcfGetINT8(file, format);
  1868. pFontInfo->fontAscent = pcfGetINT32(file, format);
  1869. pFontInfo->fontDescent = pcfGetINT32(file, format);
  1870. pFontInfo->maxOverlap = pcfGetINT32(file, format);
  1871. pcfGetMetric(file, format, &pFontInfo->minbounds);
  1872. pcfGetMetric(file, format, &pFontInfo->maxbounds);
  1873. if (PCF_FORMAT_MATCH(format, PCF_ACCEL_W_INKBOUNDS)) {
  1874. pcfGetMetric(file, format, &pFontInfo->ink_minbounds);
  1875. pcfGetMetric(file, format, &pFontInfo->ink_maxbounds);
  1876. } else {
  1877. pFontInfo->ink_minbounds = pFontInfo->minbounds;
  1878. pFontInfo->ink_maxbounds = pFontInfo->maxbounds;
  1879. }
  1880. return TRUE;
  1881. Bail:
  1882. return FALSE;
  1883. }
  1884. static void
  1885. pcfGetMetric(FontFilePtr file, CARD32 format, xCharInfo *metric)
  1886. {
  1887. metric->leftSideBearing = pcfGetINT16(file, format);
  1888. metric->rightSideBearing = pcfGetINT16(file, format);
  1889. metric->characterWidth = pcfGetINT16(file, format);
  1890. metric->ascent = pcfGetINT16(file, format);
  1891. metric->descent = pcfGetINT16(file, format);
  1892. metric->attributes = pcfGetINT16(file, format);
  1893. }
  1894. int
  1895. bitmapGetGlyphs(FontPtr pFont, unsigned long count, unsigned char *chars, FontEncoding charEncoding, unsigned long *glyphCount, CharInfoPtr *glyphs)
  1896. {
  1897. BitmapFontPtr bitmapFont;
  1898. unsigned int firstCol;
  1899. register unsigned int numCols;
  1900. unsigned int firstRow;
  1901. unsigned int numRows;
  1902. CharInfoPtr *glyphsBase;
  1903. register unsigned int c;
  1904. register CharInfoPtr pci;
  1905. unsigned int r;
  1906. CharInfoPtr *encoding;
  1907. CharInfoPtr pDefault;
  1908. bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
  1909. encoding = bitmapFont->encoding;
  1910. pDefault = bitmapFont->pDefault;
  1911. firstCol = pFont->info.firstCol;
  1912. numCols = pFont->info.lastCol - firstCol + 1;
  1913. glyphsBase = glyphs;
  1914. switch (charEncoding) {
  1915. case Linear8Bit:
  1916. case TwoD8Bit:
  1917. if (pFont->info.firstRow > 0)
  1918. break;
  1919. if (pFont->info.allExist && pDefault) {
  1920. while (count--) {
  1921. c = (*chars++) - firstCol;
  1922. if (c < numCols)
  1923. *glyphs++ = encoding[c];
  1924. else
  1925. *glyphs++ = pDefault;
  1926. }
  1927. } else {
  1928. while (count--) {
  1929. c = (*chars++) - firstCol;
  1930. if (c < numCols && (pci = encoding[c]))
  1931. *glyphs++ = pci;
  1932. else if (pDefault)
  1933. *glyphs++ = pDefault;
  1934. }
  1935. }
  1936. break;
  1937. case Linear16Bit:
  1938. if (pFont->info.allExist && pDefault) {
  1939. while (count--) {
  1940. c = *chars++ << 8;
  1941. c = (c | *chars++) - firstCol;
  1942. if (c < numCols)
  1943. *glyphs++ = encoding[c];
  1944. else
  1945. *glyphs++ = pDefault;
  1946. }
  1947. } else {
  1948. while (count--) {
  1949. c = *chars++ << 8;
  1950. c = (c | *chars++) - firstCol;
  1951. if (c < numCols && (pci = encoding[c]))
  1952. *glyphs++ = pci;
  1953. else if (pDefault)
  1954. *glyphs++ = pDefault;
  1955. }
  1956. }
  1957. break;
  1958. case TwoD16Bit:
  1959. firstRow = pFont->info.firstRow;
  1960. numRows = pFont->info.lastRow - firstRow + 1;
  1961. while (count--) {
  1962. r = (*chars++) - firstRow;
  1963. c = (*chars++) - firstCol;
  1964. if (r < numRows && c < numCols &&
  1965. (pci = encoding[r * numCols + c]))
  1966. *glyphs++ = pci;
  1967. else if (pDefault)
  1968. *glyphs++ = pDefault;
  1969. }
  1970. break;
  1971. }
  1972. *glyphCount = glyphs - glyphsBase;
  1973. return Successful;
  1974. }
  1975. int
  1976. bitmapGetMetrics(FontPtr pFont, unsigned long count, unsigned char *chars, FontEncoding charEncoding, unsigned long *glyphCount, xCharInfo **glyphs)
  1977. {
  1978. int ret;
  1979. xCharInfo *ink_metrics;
  1980. CharInfoPtr metrics;
  1981. BitmapFontPtr bitmapFont;
  1982. CharInfoPtr oldDefault;
  1983. int i;
  1984. bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
  1985. oldDefault = bitmapFont->pDefault;
  1986. bitmapFont->pDefault = &nonExistantChar;
  1987. ret = bitmapGetGlyphs(pFont, count, chars, charEncoding, glyphCount, (CharInfoPtr *) glyphs);
  1988. if (ret == Successful) {
  1989. if (bitmapFont->ink_metrics) {
  1990. metrics = bitmapFont->metrics;
  1991. ink_metrics = bitmapFont->ink_metrics;
  1992. for (i = 0; i < *glyphCount; i++) {
  1993. if (glyphs[i] != (xCharInfo *) & nonExistantChar)
  1994. glyphs[i] = ink_metrics + (((CharInfoPtr) glyphs[i]) - metrics);
  1995. }
  1996. }
  1997. }
  1998. bitmapFont->pDefault = oldDefault;
  1999. return ret;
  2000. }
  2001. void
  2002. pcfUnloadFont(FontPtr pFont)
  2003. {
  2004. BitmapFontPtr bitmapFont;
  2005. bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
  2006. xfree(bitmapFont->ink_metrics);
  2007. xfree(bitmapFont->encoding);
  2008. xfree(bitmapFont->bitmaps);
  2009. xfree(bitmapFont->metrics);
  2010. xfree(pFont->info.isStringProp);
  2011. xfree(pFont->info.props);
  2012. xfree(bitmapFont);
  2013. xfree(pFont);
  2014. }
  2015. static int
  2016. pcfReadFont(FontPtr pFont, FontFilePtr file, int bit, int byte, int glyph, int scan)
  2017. {
  2018. CARD32 format;
  2019. CARD32 size;
  2020. BitmapFontPtr bitmapFont = 0;
  2021. int i;
  2022. PCFTablePtr tables = 0;
  2023. int ntables;
  2024. int nmetrics;
  2025. int nbitmaps;
  2026. int sizebitmaps;
  2027. int nink_metrics;
  2028. CharInfoPtr metrics = 0;
  2029. xCharInfo *ink_metrics = 0;
  2030. char *bitmaps = 0;
  2031. CharInfoPtr *encoding = 0;
  2032. int nencoding;
  2033. int encodingOffset;
  2034. CARD32 bitmapSizes[GLYPHPADOPTIONS];
  2035. CARD32 *offsets = 0;
  2036. Bool hasBDFAccelerators;
  2037. pFont->info.props = 0;
  2038. if (!(tables = pcfReadTOC(file, &ntables)))
  2039. goto Bail;
  2040. /* properties */
  2041. if (!pcfGetProperties(&pFont->info, file, tables, ntables))
  2042. goto Bail;
  2043. /* Use the old accelerators if no BDF accelerators are in the file */
  2044. hasBDFAccelerators = pcfHasType (tables, ntables, PCF_BDF_ACCELERATORS);
  2045. if (!hasBDFAccelerators)
  2046. if (!pcfGetAccel (&pFont->info, file, tables, ntables, PCF_ACCELERATORS))
  2047. goto Bail;
  2048. /* metrics */
  2049. if (!pcfSeekToType(file, tables, ntables, PCF_METRICS, &format, &size)) {
  2050. goto Bail;
  2051. }
  2052. format = pcfGetLSB32(file);
  2053. if (!PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT) &&
  2054. !PCF_FORMAT_MATCH(format, PCF_COMPRESSED_METRICS)) {
  2055. goto Bail;
  2056. }
  2057. if (PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT))
  2058. nmetrics = pcfGetINT32(file, format);
  2059. else
  2060. nmetrics = pcfGetINT16(file, format);
  2061. metrics = (CharInfoPtr) xalloc(nmetrics * sizeof(CharInfoRec));
  2062. if (!metrics) {
  2063. goto Bail;
  2064. }
  2065. for (i = 0; i < nmetrics; i++)
  2066. if (PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT))
  2067. pcfGetMetric(file, format, &(metrics + i)->metrics);
  2068. else
  2069. pcfGetCompressedMetric(file, format, &(metrics + i)->metrics);
  2070. /* bitmaps */
  2071. if (!pcfSeekToType(file, tables, ntables, PCF_BITMAPS, &format, &size))
  2072. goto Bail;
  2073. format = pcfGetLSB32(file);
  2074. if (!PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT))
  2075. goto Bail;
  2076. nbitmaps = pcfGetINT32(file, format);
  2077. if (nbitmaps != nmetrics)
  2078. goto Bail;
  2079. offsets = (CARD32 *) xalloc(nbitmaps * sizeof(CARD32));
  2080. if (!offsets)
  2081. goto Bail;
  2082. for (i = 0; i < nbitmaps; i++)
  2083. offsets[i] = pcfGetINT32(file, format);
  2084. for (i = 0; i < GLYPHPADOPTIONS; i++)
  2085. bitmapSizes[i] = pcfGetINT32(file, format);
  2086. sizebitmaps = bitmapSizes[PCF_GLYPH_PAD_INDEX(format)];
  2087. bitmaps = (char *) xalloc(sizebitmaps);
  2088. if (!bitmaps)
  2089. goto Bail;
  2090. FontFileRead(file, bitmaps, sizebitmaps);
  2091. position += sizebitmaps;
  2092. if (PCF_BIT_ORDER(format) != bit)
  2093. BitOrderInvert((unsigned char*)bitmaps, sizebitmaps);
  2094. if ((PCF_BYTE_ORDER(format) == PCF_BIT_ORDER(format)) != (bit == byte)) {
  2095. switch (bit == byte ? PCF_SCAN_UNIT(format) : scan) {
  2096. case 1:
  2097. break;
  2098. case 2:
  2099. TwoByteSwap((unsigned char*)bitmaps, sizebitmaps);
  2100. break;
  2101. case 4:
  2102. FourByteSwap((unsigned char*)bitmaps, sizebitmaps);
  2103. break;
  2104. }
  2105. }
  2106. if (PCF_GLYPH_PAD(format) != glyph) {
  2107. char *padbitmaps;
  2108. int sizepadbitmaps;
  2109. int old,
  2110. _new;
  2111. xCharInfo *metric;
  2112. sizepadbitmaps = bitmapSizes[PCF_SIZE_TO_INDEX(glyph)];
  2113. padbitmaps = (char *) xalloc(sizepadbitmaps);
  2114. if (!padbitmaps) {
  2115. goto Bail;
  2116. }
  2117. _new = 0;
  2118. for (i = 0; i < nbitmaps; i++) {
  2119. old = offsets[i];
  2120. metric = &metrics[i].metrics;
  2121. offsets[i] = _new;
  2122. _new += RepadBitmap(bitmaps + old, padbitmaps + _new,
  2123. PCF_GLYPH_PAD(format), glyph,
  2124. metric->rightSideBearing - metric->leftSideBearing,
  2125. metric->ascent + metric->descent);
  2126. }
  2127. xfree(bitmaps);
  2128. bitmaps = padbitmaps;
  2129. }
  2130. for (i = 0; i < nbitmaps; i++)
  2131. metrics[i].bits = bitmaps + offsets[i];
  2132. xfree(offsets);
  2133. /* ink metrics ? */
  2134. ink_metrics = NULL;
  2135. if (pcfSeekToType(file, tables, ntables, PCF_INK_METRICS, &format, &size)) {
  2136. format = pcfGetLSB32(file);
  2137. if (!PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT) &&
  2138. !PCF_FORMAT_MATCH(format, PCF_COMPRESSED_METRICS)) {
  2139. goto Bail;
  2140. }
  2141. if (PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT))
  2142. nink_metrics = pcfGetINT32(file, format);
  2143. else
  2144. nink_metrics = pcfGetINT16(file, format);
  2145. if (nink_metrics != nmetrics)
  2146. goto Bail;
  2147. ink_metrics = (xCharInfo *) xalloc(nink_metrics * sizeof(xCharInfo));
  2148. if (!ink_metrics)
  2149. goto Bail;
  2150. for (i = 0; i < nink_metrics; i++)
  2151. if (PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT))
  2152. pcfGetMetric(file, format, ink_metrics + i);
  2153. else
  2154. pcfGetCompressedMetric(file, format, ink_metrics + i);
  2155. }
  2156. /* encoding */
  2157. if (!pcfSeekToType(file, tables, ntables, PCF_BDF_ENCODINGS, &format, &size))
  2158. goto Bail;
  2159. format = pcfGetLSB32(file);
  2160. if (!PCF_FORMAT_MATCH(format, PCF_DEFAULT_FORMAT))
  2161. goto Bail;
  2162. pFont->info.firstCol = pcfGetINT16(file, format);
  2163. pFont->info.lastCol = pcfGetINT16(file, format);
  2164. pFont->info.firstRow = pcfGetINT16(file, format);
  2165. pFont->info.lastRow = pcfGetINT16(file, format);
  2166. pFont->info.defaultCh = pcfGetINT16(file, format);
  2167. nencoding = (pFont->info.lastCol - pFont->info.firstCol + 1) *
  2168. (pFont->info.lastRow - pFont->info.firstRow + 1);
  2169. encoding = (CharInfoPtr *) xalloc(nencoding * sizeof(CharInfoPtr));
  2170. if (!encoding)
  2171. goto Bail;
  2172. pFont->info.allExist = TRUE;
  2173. for (i = 0; i < nencoding; i++) {
  2174. encodingOffset = pcfGetINT16(file, format);
  2175. if (encodingOffset == 0xFFFF) {
  2176. pFont->info.allExist = FALSE;
  2177. encoding[i] = 0;
  2178. } else
  2179. encoding[i] = metrics + encodingOffset;
  2180. }
  2181. /* BDF style accelerators (i.e. bounds based on encoded glyphs) */
  2182. if (hasBDFAccelerators)
  2183. if (!pcfGetAccel (&pFont->info, file, tables, ntables, PCF_BDF_ACCELERATORS))
  2184. goto Bail;
  2185. bitmapFont = (BitmapFontPtr) xalloc(sizeof *bitmapFont);
  2186. if (!bitmapFont)
  2187. goto Bail;
  2188. bitmapFont->version_num = PCF_FILE_VERSION;
  2189. bitmapFont->num_chars = nmetrics;
  2190. bitmapFont->num_tables = ntables;
  2191. bitmapFont->metrics = metrics;
  2192. bitmapFont->ink_metrics = ink_metrics;
  2193. bitmapFont->bitmaps = bitmaps;
  2194. bitmapFont->encoding = encoding;
  2195. bitmapFont->pDefault = (CharInfoPtr) 0;
  2196. if (pFont->info.defaultCh != (unsigned short) NO_SUCH_CHAR) {
  2197. int r,
  2198. c,
  2199. cols;
  2200. r = pFont->info.defaultCh >> 8;
  2201. c = pFont->info.defaultCh & 0xFF;
  2202. if (pFont->info.firstRow <= r && r <= pFont->info.lastRow &&
  2203. pFont->info.firstCol <= c && c <= pFont->info.lastCol) {
  2204. cols = pFont->info.lastCol - pFont->info.firstCol + 1;
  2205. r = r - pFont->info.firstRow;
  2206. c = c - pFont->info.firstCol;
  2207. bitmapFont->pDefault = encoding[r * cols + c];
  2208. }
  2209. }
  2210. bitmapFont->bitmapExtra = (BitmapExtraPtr) 0;
  2211. pFont->fontPrivate = (pointer) bitmapFont;
  2212. pFont->get_glyphs = bitmapGetGlyphs;
  2213. pFont->get_metrics = bitmapGetMetrics;
  2214. pFont->unload_font = pcfUnloadFont;
  2215. pFont->bit = bit;
  2216. pFont->byte = byte;
  2217. pFont->glyph = glyph;
  2218. pFont->scan = scan;
  2219. xfree(tables);
  2220. return Successful;
  2221. Bail:
  2222. xfree(ink_metrics);
  2223. xfree(encoding);
  2224. xfree(bitmaps);
  2225. xfree(offsets);
  2226. xfree(metrics);
  2227. xfree(pFont->info.props);
  2228. pFont->info.props = 0;
  2229. xfree(bitmapFont);
  2230. xfree(tables);
  2231. return AllocError;
  2232. }
  2233. struct interval {
  2234. unsigned short first;
  2235. unsigned short last;
  2236. };
  2237. /* auxiliary function for binary search in interval table */
  2238. static int bisearch(wchar_t ucs, const struct interval *table, int max) {
  2239. int min = 0;
  2240. int mid;
  2241. if (ucs < table[0].first || ucs > table[max].last)
  2242. return 0;
  2243. while (max >= min) {
  2244. mid = (min + max) / 2;
  2245. if (ucs > table[mid].last)
  2246. min = mid + 1;
  2247. else if (ucs < table[mid].first)
  2248. max = mid - 1;
  2249. else
  2250. return 1;
  2251. }
  2252. return 0;
  2253. }
  2254. int wcwidth(wchar_t ucs)
  2255. {
  2256. /* sorted list of non-overlapping intervals of non-spacing characters */
  2257. static const struct interval combining[] = {
  2258. { 0x0300, 0x034E }, { 0x0360, 0x0362 }, { 0x0483, 0x0486 },
  2259. { 0x0488, 0x0489 }, { 0x0591, 0x05A1 }, { 0x05A3, 0x05B9 },
  2260. { 0x05BB, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 },
  2261. { 0x05C4, 0x05C4 }, { 0x064B, 0x0655 }, { 0x0670, 0x0670 },
  2262. { 0x06D6, 0x06E4 }, { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED },
  2263. { 0x070F, 0x070F }, { 0x0711, 0x0711 }, { 0x0730, 0x074A },
  2264. { 0x07A6, 0x07B0 }, { 0x0901, 0x0902 }, { 0x093C, 0x093C },
  2265. { 0x0941, 0x0948 }, { 0x094D, 0x094D }, { 0x0951, 0x0954 },
  2266. { 0x0962, 0x0963 }, { 0x0981, 0x0981 }, { 0x09BC, 0x09BC },
  2267. { 0x09C1, 0x09C4 }, { 0x09CD, 0x09CD }, { 0x09E2, 0x09E3 },
  2268. { 0x0A02, 0x0A02 }, { 0x0A3C, 0x0A3C }, { 0x0A41, 0x0A42 },
  2269. { 0x0A47, 0x0A48 }, { 0x0A4B, 0x0A4D }, { 0x0A70, 0x0A71 },
  2270. { 0x0A81, 0x0A82 }, { 0x0ABC, 0x0ABC }, { 0x0AC1, 0x0AC5 },
  2271. { 0x0AC7, 0x0AC8 }, { 0x0ACD, 0x0ACD }, { 0x0B01, 0x0B01 },
  2272. { 0x0B3C, 0x0B3C }, { 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 },
  2273. { 0x0B4D, 0x0B4D }, { 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 },
  2274. { 0x0BC0, 0x0BC0 }, { 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 },
  2275. { 0x0C46, 0x0C48 }, { 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 },
  2276. { 0x0CBF, 0x0CBF }, { 0x0CC6, 0x0CC6 }, { 0x0CCC, 0x0CCD },
  2277. { 0x0D41, 0x0D43 }, { 0x0D4D, 0x0D4D }, { 0x0DCA, 0x0DCA },
  2278. { 0x0DD2, 0x0DD4 }, { 0x0DD6, 0x0DD6 }, { 0x0E31, 0x0E31 },
  2279. { 0x0E34, 0x0E3A }, { 0x0E47, 0x0E4E }, { 0x0EB1, 0x0EB1 },
  2280. { 0x0EB4, 0x0EB9 }, { 0x0EBB, 0x0EBC }, { 0x0EC8, 0x0ECD },
  2281. { 0x0F18, 0x0F19 }, { 0x0F35, 0x0F35 }, { 0x0F37, 0x0F37 },
  2282. { 0x0F39, 0x0F39 }, { 0x0F71, 0x0F7E }, { 0x0F80, 0x0F84 },
  2283. { 0x0F86, 0x0F87 }, { 0x0F90, 0x0F97 }, { 0x0F99, 0x0FBC },
  2284. { 0x0FC6, 0x0FC6 }, { 0x102D, 0x1030 }, { 0x1032, 0x1032 },
  2285. { 0x1036, 0x1037 }, { 0x1039, 0x1039 }, { 0x1058, 0x1059 },
  2286. { 0x1160, 0x11FF }, { 0x17B7, 0x17BD }, { 0x17C6, 0x17C6 },
  2287. { 0x17C9, 0x17D3 }, { 0x180B, 0x180E }, { 0x18A9, 0x18A9 },
  2288. { 0x200B, 0x200F }, { 0x202A, 0x202E }, { 0x206A, 0x206F },
  2289. { 0x20D0, 0x20E3 }, { 0x302A, 0x302F }, { 0x3099, 0x309A },
  2290. { 0xFB1E, 0xFB1E }, { 0xFE20, 0xFE23 }, { 0xFEFF, 0xFEFF },
  2291. { 0xFFF9, 0xFFFB }
  2292. };
  2293. /* test for 8-bit control characters */
  2294. if (ucs == 0)
  2295. return 0;
  2296. if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
  2297. return -1;
  2298. /* binary search in table of non-spacing characters */
  2299. if (bisearch(ucs, combining,
  2300. sizeof(combining) / sizeof(struct interval) - 1))
  2301. return 0;
  2302. /* if we arrive here, ucs is not a combining or C0/C1 control character */
  2303. return 1 +
  2304. (ucs >= 0x1100 &&
  2305. (ucs <= 0x115f || /* Hangul Jamo init. consonants */
  2306. (ucs >= 0x2e80 && ucs <= 0xa4cf && (ucs & ~0x0011) != 0x300a &&
  2307. ucs != 0x303f) || /* CJK ... Yi */
  2308. (ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */
  2309. (ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */
  2310. (ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */
  2311. (ucs >= 0xff00 && ucs <= 0xff5f) || /* Fullwidth Forms */
  2312. (ucs >= 0xffe0 && ucs <= 0xffe6) ||
  2313. (ucs >= 0x20000 && ucs <= 0x2ffff)));
  2314. }