bmf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * Copyright (c) 2009 Openmoko Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #ifdef WIKIPCF
  22. #include <assert.h>
  23. #include <wchar.h>
  24. #include <fcntl.h>
  25. #include <unistd.h>
  26. #else
  27. #include <file-io.h>
  28. #include <malloc-simple.h>
  29. #include <msg.h>
  30. #endif
  31. #include "bmf.h"
  32. int load_bmf(pcffont_bmf_t *font)
  33. {
  34. int fd, read_size;
  35. font_bmf_header header;
  36. fd = openfile(font->file,0);
  37. if(fd<0)
  38. return -1;
  39. #ifdef WIKIPCF
  40. font->file_size = lseek(fd, 0, SEEK_END);
  41. lseek(fd, 0, SEEK_SET);
  42. #else
  43. wl_fsize(fd, &(font->file_size));
  44. #endif
  45. font->charmetric = (char*)Xalloc(font->file_size);
  46. memset(font->charmetric, 0, font->file_size);
  47. read_size = readfile(fd, font->charmetric, 256 * sizeof(charmetric_bmf)+sizeof(font_bmf_header));
  48. memcpy(&header,font->charmetric,sizeof(font_bmf_header));
  49. font->Fmetrics.linespace = header.linespace;
  50. font->Fmetrics.ascent = header.ascent;
  51. font->Fmetrics.descent = header.descent;
  52. font->Fmetrics.default_char = header.default_char;
  53. return fd;
  54. }
  55. int
  56. pres_bmfbm(ucs4_t val, pcffont_bmf_t *font, bmf_bm_t **bitmap,charmetric_bmf *Cmetrics)
  57. {
  58. int size = 0;
  59. int read_size = 0, offset = 0;
  60. char buffer[1024];
  61. int font_header;
  62. int bFound = 0;
  63. memset(buffer,0,1024);
  64. if(font==NULL || font->fd < 0)
  65. return -1;
  66. font_header = sizeof(font_bmf_header);
  67. size = val*sizeof(charmetric_bmf)+font_header;
  68. if(val <= 256)
  69. {
  70. memcpy(Cmetrics,font->charmetric+val*sizeof(charmetric_bmf)+font_header,sizeof(charmetric_bmf));
  71. }
  72. else
  73. {
  74. offset = val*sizeof(charmetric_bmf)+font_header;
  75. if (offset <= font->file_size - sizeof(charmetric_bmf))
  76. {
  77. memcpy(Cmetrics,font->charmetric+val*sizeof(charmetric_bmf)+font_header,sizeof(charmetric_bmf));
  78. if (Cmetrics->width)
  79. {
  80. bFound = 1;
  81. }
  82. else
  83. {
  84. #ifdef WIKIPCF
  85. lseek(font->fd,offset,SEEK_SET);
  86. #else
  87. wl_seek(font->fd,offset);
  88. }
  89. #endif
  90. }
  91. else
  92. {
  93. if (font->bPartialFont)
  94. { // character not defined in the current font file (and it is intended to include partial characters)
  95. font = font->supplement_font;
  96. return pres_bmfbm(val, font, bitmap, Cmetrics);
  97. }
  98. else
  99. return -1;
  100. }
  101. if (!bFound)
  102. {
  103. size = 1024; // Due to the nature of wl_read, the read size needs to be 1024.
  104. read_size = readfile(font->fd,buffer,size);
  105. memcpy(Cmetrics,buffer,sizeof(charmetric_bmf));
  106. memcpy(font->charmetric+val*sizeof(charmetric_bmf)+font_header,Cmetrics,sizeof(charmetric_bmf));
  107. }
  108. }
  109. if(Cmetrics->width>0)
  110. *bitmap = (bmf_bm_t*)Cmetrics+8;
  111. else
  112. {
  113. if (val > 256 && offset <= font->file_size - sizeof(charmetric_bmf))
  114. {
  115. if (font->Fmetrics.default_char && val != font->Fmetrics.default_char)
  116. {
  117. pres_bmfbm(font->Fmetrics.default_char, font, bitmap, Cmetrics);
  118. }
  119. if (!Cmetrics->width)
  120. {
  121. Cmetrics->width = 1;
  122. Cmetrics->height = 0;
  123. }
  124. memcpy(font->charmetric+val*sizeof(charmetric_bmf)+font_header,Cmetrics,sizeof(charmetric_bmf));
  125. }
  126. }
  127. return 1;
  128. }
  129. unsigned long *
  130. Xalloc (int m)
  131. {
  132. #ifdef WIKIPCF
  133. void * mem = malloc(m);
  134. return (unsigned long *)mem;
  135. #else
  136. void * mem = malloc_simple(m,MEM_TAG_ARTICLE_F1);
  137. return (unsigned long *)mem;
  138. #endif
  139. }
  140. /*int load_bmf(pcffont_bmf_t *font)
  141. {
  142. int fd,file_size,read_size;
  143. char buffer[1024];
  144. font_bmf_header header;
  145. fd = openfile(font->file,0);
  146. if(fd<0)
  147. return -1;
  148. file_size = readfile(fd, buffer, 1024);
  149. memcpy(&header,buffer,sizeof(font_bmf_header));
  150. closefile(fd);
  151. fd = openfile(font->file,0);
  152. if(fd<0)
  153. return -1;
  154. file_size = 65535*sizeof(charmetric_bmf_header) + sizeof(font_bmf_header)+header.bmp_buffer_len*256;
  155. font->charmetric = (char*)Xalloc(file_size);
  156. read_size = readfile(fd, font->charmetric, file_size);
  157. font->Fmetrics.linespace = header.linespace;
  158. font->Fmetrics.ascent = header.ascent;
  159. font->Fmetrics.descent = header.descent;
  160. font->bmp_buffer_len = header.bmp_buffer_len;
  161. #ifdef WIKIPCF
  162. font->file_size = lseek(fd, 0, SEEK_END);
  163. #else
  164. wl_fsize(fd, &(font->file_size));
  165. #endif
  166. return fd;
  167. }*/
  168. /*
  169. int
  170. pres_bmfbm(ucs4_t val, pcffont_bmf_t *font, bmf_bm_t **bitmap,charmetric_bmf *Cmetrics)
  171. {
  172. int size = 0;
  173. int read_size = 0, offset = 0;
  174. char buffer[1024];
  175. charmetric_bmf_header bmf_header;
  176. int font_header;
  177. font_header = sizeof(font_bmf_header);
  178. if(val <= 256)
  179. {
  180. offset = val*sizeof(charmetric_bmf_header)+font_header;
  181. memcpy(&bmf_header,font->charmetric+val*sizeof(charmetric_bmf_header)+font_header,sizeof(charmetric_bmf_header));
  182. memcpy(Cmetrics,&bmf_header,sizeof(charmetric_bmf)-font->bmp_buffer_len);
  183. memcpy(Cmetrics->bitmap,font->charmetric+bmf_header.pos,font->bmp_buffer_len);
  184. *bitmap = (bmf_bm_t*)Cmetrics+8;
  185. return 1;
  186. }
  187. else
  188. {
  189. offset = val*sizeof(charmetric_bmf_header)+font_header;
  190. if (offset <= font->file_size - sizeof(charmetric_bmf))
  191. #ifdef WIKIPCF
  192. lseek(font->fd,offset,SEEK_SET);
  193. #else
  194. wl_seek(font->fd,offset);
  195. #endif
  196. else
  197. {
  198. if (font->bPartialFont)
  199. { // character not defined in the current font file (and it is intended to include partial characters)
  200. font = font->supplement_font;
  201. return pres_bmfbm(val, font, bitmap, Cmetrics);
  202. }
  203. else
  204. return -1;
  205. }
  206. // size = sizeof(charmetric_bmf);
  207. size = 1024; // Due to the nature of wl_read, the read size needs to be 1024.
  208. read_size = readfile(font->fd,buffer,size);
  209. memcpy(&bmf_header,buffer,sizeof(charmetric_bmf_header));
  210. if(bmf_header.pos<=0)
  211. return -1;
  212. #ifdef WIKIPCF
  213. lseek(font->fd,bmf_header.pos,SEEK_SET);
  214. #else
  215. wl_seek(font->fd,bmf_header.pos);
  216. #endif
  217. memset(buffer,0,1024);
  218. read_size = readfile(font->fd,buffer,size);
  219. memcpy(Cmetrics,&bmf_header,sizeof(charmetric_bmf)-font->bmp_buffer_len);
  220. }
  221. if(Cmetrics->height>0 && Cmetrics->widthBytes>0)
  222. {
  223. memcpy(Cmetrics->bitmap,buffer,font->bmp_buffer_len);
  224. *bitmap = (bmf_bm_t*)Cmetrics+8;
  225. }
  226. else
  227. return -1;
  228. return 1;
  229. }*/
  230. /*int load_bmf(pcffont_bmf_t *font)
  231. {
  232. int fd,file_size,read_size;
  233. char buffer[1024];
  234. font_bmf_header header;
  235. fd = openfile(font->file,0);
  236. if(fd<0)
  237. return -1;
  238. file_size = readfile(fd, buffer, 1024);
  239. memcpy(&header,buffer,sizeof(font_bmf_header));
  240. closefile(fd);
  241. fd = openfile(font->file,0);
  242. if(fd<0)
  243. return -1;
  244. file_size = 65535*sizeof(charmetric_bmf_header) + sizeof(font_bmf_header)+64*256;
  245. font->charmetric = (char*)Xalloc(file_size);
  246. read_size = readfile(fd, font->charmetric, file_size);
  247. font->Fmetrics.linespace = header.linespace;
  248. font->Fmetrics.ascent = header.ascent;
  249. font->Fmetrics.descent = header.descent;
  250. #ifdef WIKIPCF
  251. font->file_size = lseek(fd, 0, SEEK_END);
  252. #else
  253. wl_fsize(fd, &(font->file_size));
  254. #endif
  255. return fd;
  256. }
  257. int
  258. pres_bmfbm(ucs4_t val, pcffont_bmf_t *font, bmf_bm_t **bitmap,charmetric_bmf *Cmetrics)
  259. {
  260. int size = 0;
  261. int read_size = 0, offset = 0;
  262. char buffer[1024];
  263. charmetric_bmf_header bmf_header;
  264. int font_header;
  265. font_header = sizeof(font_bmf_header);
  266. if(val <= 256)
  267. {
  268. offset = val*sizeof(charmetric_bmf_header)+font_header;
  269. memcpy(&bmf_header,font->charmetric+val*sizeof(charmetric_bmf_header)+font_header,sizeof(charmetric_bmf_header));
  270. memcpy(Cmetrics,&bmf_header,sizeof(charmetric_bmf)-64);
  271. memcpy(Cmetrics->bitmap,font->charmetric+bmf_header.pos,64);
  272. *bitmap = (bmf_bm_t*)Cmetrics+8;
  273. return 1;
  274. }
  275. else
  276. {
  277. offset = val*sizeof(charmetric_bmf_header)+font_header;
  278. if (offset <= font->file_size - sizeof(charmetric_bmf))
  279. #ifdef WIKIPCF
  280. lseek(font->fd,offset,SEEK_SET);
  281. #else
  282. wl_seek(font->fd,offset);
  283. #endif
  284. else
  285. {
  286. if (font->bPartialFont)
  287. { // character not defined in the current font file (and it is intended to include partial characters)
  288. font++;
  289. return pres_bmfbm(val, font, bitmap, Cmetrics);
  290. }
  291. else
  292. return -1;
  293. }
  294. // size = sizeof(charmetric_bmf);
  295. size = 1024; // Due to the nature of wl_read, the read size needs to be 1024.
  296. read_size = readfile(font->fd,buffer,size);
  297. memcpy(&bmf_header,buffer,sizeof(charmetric_bmf_header));
  298. if(bmf_header.pos<=0)
  299. return -1;
  300. #ifdef WIKIPCF
  301. lseek(font->fd,bmf_header.pos,SEEK_SET);
  302. #else
  303. wl_seek(font->fd,bmf_header.pos);
  304. #endif
  305. memset(buffer,0,1024);
  306. read_size = readfile(font->fd,buffer,size);
  307. memcpy(Cmetrics,&bmf_header,sizeof(charmetric_bmf)-64);
  308. }
  309. if(Cmetrics->height>0 && Cmetrics->widthBytes>0)
  310. {
  311. memcpy(Cmetrics->bitmap,buffer,64);
  312. *bitmap = (bmf_bm_t*)Cmetrics+8;
  313. }
  314. else
  315. return -1;
  316. return 1;
  317. }
  318. unsigned long *
  319. Xalloc (int m)
  320. {
  321. #ifdef WIKIPCF
  322. void * mem = malloc(m);
  323. return (unsigned long *)mem;
  324. #else
  325. void * mem = malloc_simple(m,MEM_TAG_ARTICLE_F1);
  326. return (unsigned long *)mem;
  327. #endif
  328. }*/
  329. /*
  330. int load_bmf(pcffont_bmf_t *font)
  331. {
  332. int fd,file_size,read_size;
  333. fd = openfile(font->file,0);
  334. if(fd<0)
  335. return -1;
  336. file_size = (256+1) * sizeof(charmetric_bmf);
  337. font->charmetric = (char*)Xalloc(file_size);
  338. read_size = readfile(fd, font->charmetric, file_size);
  339. #ifdef WIKIPCF
  340. font->file_size = lseek(fd, 0, SEEK_END);
  341. #else
  342. wl_fsize(fd, &(font->file_size));
  343. #endif
  344. return fd;
  345. }
  346. int
  347. pres_bmfbm(ucs4_t val, pcffont_bmf_t *font, bmf_bm_t **bitmap,charmetric_bmf *Cmetrics)
  348. {
  349. int size = 0;
  350. int read_size = 0, offset = 0;
  351. char buffer[1024];
  352. if(val <= 256)
  353. memcpy(Cmetrics,font->charmetric+(val+1)*sizeof(charmetric_bmf),sizeof(charmetric_bmf));
  354. else
  355. {
  356. offset = (val+1)*sizeof(charmetric_bmf);
  357. if (offset <= font->file_size - sizeof(charmetric_bmf))
  358. #ifdef WIKIPCF
  359. lseek(font->fd,offset,SEEK_SET);
  360. #else
  361. wl_seek(font->fd,offset);
  362. #endif
  363. else
  364. {
  365. if (font->bPartialFont)
  366. { // character not defined in the current font file (and it is intended to include partial characters)
  367. font++;
  368. return pres_bmfbm(val, font, bitmap, Cmetrics);
  369. }
  370. else
  371. return -1;
  372. }
  373. // size = sizeof(charmetric_bmf);
  374. size = 1024; // Due to the nature of wl_read, the read size needs to be 1024.
  375. read_size = readfile(font->fd,buffer,size);
  376. memcpy(Cmetrics,buffer,sizeof(charmetric_bmf));
  377. }
  378. if(Cmetrics->width>0)
  379. *bitmap = (bmf_bm_t*)Cmetrics+8;
  380. else
  381. return -1;
  382. return 1;
  383. }
  384. unsigned long *
  385. Xalloc (int m)
  386. {
  387. #ifdef WIKIPCF
  388. void * mem = malloc(m);
  389. return (unsigned long *)mem;
  390. #else
  391. void * mem = malloc_simple(m,MEM_TAG_ARTICLE_F1);
  392. return (unsigned long *)mem;
  393. #endif
  394. }*/