bmf.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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,file_size,read_size;
  35. font_bmf_header header;
  36. fd = openfile(font->file,0);
  37. if(fd<0)
  38. return -1;
  39. file_size = 256 * sizeof(charmetric_bmf)+sizeof(font_bmf_header);
  40. font->charmetric = (char*)Xalloc(file_size);
  41. read_size = readfile(fd, font->charmetric, file_size);
  42. memcpy(&header,font->charmetric,sizeof(font_bmf_header));
  43. font->Fmetrics.linespace = header.linespace;
  44. font->Fmetrics.ascent = header.ascent;
  45. font->Fmetrics.descent = header.descent;
  46. #ifdef WIKIPCF
  47. font->file_size = lseek(fd, 0, SEEK_END);
  48. #else
  49. wl_fsize(fd, &(font->file_size));
  50. #endif
  51. return fd;
  52. }
  53. int
  54. pres_bmfbm(ucs4_t val, pcffont_bmf_t *font, bmf_bm_t **bitmap,charmetric_bmf *Cmetrics)
  55. {
  56. int size = 0;
  57. int read_size = 0, offset = 0;
  58. char buffer[1024];
  59. int font_header;
  60. memset(buffer,0,1024);
  61. if(font==NULL || font->fd < 0)
  62. return -1;
  63. font_header = sizeof(font_bmf_header);
  64. size = val*sizeof(charmetric_bmf)+font_header;
  65. if(val <= 256)
  66. {
  67. memcpy(Cmetrics,font->charmetric+val*sizeof(charmetric_bmf)+font_header,sizeof(charmetric_bmf));
  68. }
  69. else
  70. {
  71. offset = val*sizeof(charmetric_bmf)+font_header;
  72. if (offset <= font->file_size - sizeof(charmetric_bmf))
  73. {
  74. #ifdef WIKIPCF
  75. lseek(font->fd,offset,SEEK_SET);
  76. #else
  77. //closefile(font->fd);
  78. //font->fd = openfile(font->file,0);
  79. //wl_seek(font->fd,0);
  80. wl_seek(font->fd,offset);
  81. #endif
  82. }
  83. else
  84. {
  85. if (font->bPartialFont)
  86. { // character not defined in the current font file (and it is intended to include partial characters)
  87. font = font->supplement_font;
  88. return pres_bmfbm(val, font, bitmap, Cmetrics);
  89. //return -1;
  90. }
  91. else
  92. return -1;
  93. }
  94. size = 1024; // Due to the nature of wl_read, the read size needs to be 1024.
  95. read_size = readfile(font->fd,buffer,size);
  96. memcpy(Cmetrics,buffer,sizeof(charmetric_bmf));
  97. if((Cmetrics->height*Cmetrics->widthBytes)==0)
  98. {
  99. font = font->supplement_font;
  100. return pres_bmfbm(val, font, bitmap, Cmetrics);
  101. //return -1;
  102. }
  103. }
  104. if(Cmetrics->width>0)
  105. *bitmap = (bmf_bm_t*)Cmetrics+8;
  106. else
  107. return -1;
  108. return 1;
  109. }
  110. unsigned long *
  111. Xalloc (int m)
  112. {
  113. #ifdef WIKIPCF
  114. void * mem = malloc(m);
  115. return (unsigned long *)mem;
  116. #else
  117. void * mem = malloc_simple(m,MEM_TAG_ARTICLE_F1);
  118. return (unsigned long *)mem;
  119. #endif
  120. }
  121. /*int load_bmf(pcffont_bmf_t *font)
  122. {
  123. int fd,file_size,read_size;
  124. char buffer[1024];
  125. font_bmf_header header;
  126. fd = openfile(font->file,0);
  127. if(fd<0)
  128. return -1;
  129. file_size = readfile(fd, buffer, 1024);
  130. memcpy(&header,buffer,sizeof(font_bmf_header));
  131. closefile(fd);
  132. fd = openfile(font->file,0);
  133. if(fd<0)
  134. return -1;
  135. file_size = 65535*sizeof(charmetric_bmf_header) + sizeof(font_bmf_header)+header.bmp_buffer_len*256;
  136. font->charmetric = (char*)Xalloc(file_size);
  137. read_size = readfile(fd, font->charmetric, file_size);
  138. font->Fmetrics.linespace = header.linespace;
  139. font->Fmetrics.ascent = header.ascent;
  140. font->Fmetrics.descent = header.descent;
  141. font->bmp_buffer_len = header.bmp_buffer_len;
  142. #ifdef WIKIPCF
  143. font->file_size = lseek(fd, 0, SEEK_END);
  144. #else
  145. wl_fsize(fd, &(font->file_size));
  146. #endif
  147. return fd;
  148. }*/
  149. /*
  150. int
  151. pres_bmfbm(ucs4_t val, pcffont_bmf_t *font, bmf_bm_t **bitmap,charmetric_bmf *Cmetrics)
  152. {
  153. int size = 0;
  154. int read_size = 0, offset = 0;
  155. char buffer[1024];
  156. charmetric_bmf_header bmf_header;
  157. int font_header;
  158. font_header = sizeof(font_bmf_header);
  159. if(val <= 256)
  160. {
  161. offset = val*sizeof(charmetric_bmf_header)+font_header;
  162. memcpy(&bmf_header,font->charmetric+val*sizeof(charmetric_bmf_header)+font_header,sizeof(charmetric_bmf_header));
  163. memcpy(Cmetrics,&bmf_header,sizeof(charmetric_bmf)-font->bmp_buffer_len);
  164. memcpy(Cmetrics->bitmap,font->charmetric+bmf_header.pos,font->bmp_buffer_len);
  165. *bitmap = (bmf_bm_t*)Cmetrics+8;
  166. return 1;
  167. }
  168. else
  169. {
  170. offset = val*sizeof(charmetric_bmf_header)+font_header;
  171. if (offset <= font->file_size - sizeof(charmetric_bmf))
  172. #ifdef WIKIPCF
  173. lseek(font->fd,offset,SEEK_SET);
  174. #else
  175. wl_seek(font->fd,offset);
  176. #endif
  177. else
  178. {
  179. if (font->bPartialFont)
  180. { // character not defined in the current font file (and it is intended to include partial characters)
  181. font = font->supplement_font;
  182. return pres_bmfbm(val, font, bitmap, Cmetrics);
  183. }
  184. else
  185. return -1;
  186. }
  187. // size = sizeof(charmetric_bmf);
  188. size = 1024; // Due to the nature of wl_read, the read size needs to be 1024.
  189. read_size = readfile(font->fd,buffer,size);
  190. memcpy(&bmf_header,buffer,sizeof(charmetric_bmf_header));
  191. if(bmf_header.pos<=0)
  192. return -1;
  193. #ifdef WIKIPCF
  194. lseek(font->fd,bmf_header.pos,SEEK_SET);
  195. #else
  196. wl_seek(font->fd,bmf_header.pos);
  197. #endif
  198. memset(buffer,0,1024);
  199. read_size = readfile(font->fd,buffer,size);
  200. memcpy(Cmetrics,&bmf_header,sizeof(charmetric_bmf)-font->bmp_buffer_len);
  201. }
  202. if(Cmetrics->height>0 && Cmetrics->widthBytes>0)
  203. {
  204. memcpy(Cmetrics->bitmap,buffer,font->bmp_buffer_len);
  205. *bitmap = (bmf_bm_t*)Cmetrics+8;
  206. }
  207. else
  208. return -1;
  209. return 1;
  210. }*/
  211. /*int load_bmf(pcffont_bmf_t *font)
  212. {
  213. int fd,file_size,read_size;
  214. char buffer[1024];
  215. font_bmf_header header;
  216. fd = openfile(font->file,0);
  217. if(fd<0)
  218. return -1;
  219. file_size = readfile(fd, buffer, 1024);
  220. memcpy(&header,buffer,sizeof(font_bmf_header));
  221. closefile(fd);
  222. fd = openfile(font->file,0);
  223. if(fd<0)
  224. return -1;
  225. file_size = 65535*sizeof(charmetric_bmf_header) + sizeof(font_bmf_header)+64*256;
  226. font->charmetric = (char*)Xalloc(file_size);
  227. read_size = readfile(fd, font->charmetric, file_size);
  228. font->Fmetrics.linespace = header.linespace;
  229. font->Fmetrics.ascent = header.ascent;
  230. font->Fmetrics.descent = header.descent;
  231. #ifdef WIKIPCF
  232. font->file_size = lseek(fd, 0, SEEK_END);
  233. #else
  234. wl_fsize(fd, &(font->file_size));
  235. #endif
  236. return fd;
  237. }
  238. int
  239. pres_bmfbm(ucs4_t val, pcffont_bmf_t *font, bmf_bm_t **bitmap,charmetric_bmf *Cmetrics)
  240. {
  241. int size = 0;
  242. int read_size = 0, offset = 0;
  243. char buffer[1024];
  244. charmetric_bmf_header bmf_header;
  245. int font_header;
  246. font_header = sizeof(font_bmf_header);
  247. if(val <= 256)
  248. {
  249. offset = val*sizeof(charmetric_bmf_header)+font_header;
  250. memcpy(&bmf_header,font->charmetric+val*sizeof(charmetric_bmf_header)+font_header,sizeof(charmetric_bmf_header));
  251. memcpy(Cmetrics,&bmf_header,sizeof(charmetric_bmf)-64);
  252. memcpy(Cmetrics->bitmap,font->charmetric+bmf_header.pos,64);
  253. *bitmap = (bmf_bm_t*)Cmetrics+8;
  254. return 1;
  255. }
  256. else
  257. {
  258. offset = val*sizeof(charmetric_bmf_header)+font_header;
  259. if (offset <= font->file_size - sizeof(charmetric_bmf))
  260. #ifdef WIKIPCF
  261. lseek(font->fd,offset,SEEK_SET);
  262. #else
  263. wl_seek(font->fd,offset);
  264. #endif
  265. else
  266. {
  267. if (font->bPartialFont)
  268. { // character not defined in the current font file (and it is intended to include partial characters)
  269. font++;
  270. return pres_bmfbm(val, font, bitmap, Cmetrics);
  271. }
  272. else
  273. return -1;
  274. }
  275. // size = sizeof(charmetric_bmf);
  276. size = 1024; // Due to the nature of wl_read, the read size needs to be 1024.
  277. read_size = readfile(font->fd,buffer,size);
  278. memcpy(&bmf_header,buffer,sizeof(charmetric_bmf_header));
  279. if(bmf_header.pos<=0)
  280. return -1;
  281. #ifdef WIKIPCF
  282. lseek(font->fd,bmf_header.pos,SEEK_SET);
  283. #else
  284. wl_seek(font->fd,bmf_header.pos);
  285. #endif
  286. memset(buffer,0,1024);
  287. read_size = readfile(font->fd,buffer,size);
  288. memcpy(Cmetrics,&bmf_header,sizeof(charmetric_bmf)-64);
  289. }
  290. if(Cmetrics->height>0 && Cmetrics->widthBytes>0)
  291. {
  292. memcpy(Cmetrics->bitmap,buffer,64);
  293. *bitmap = (bmf_bm_t*)Cmetrics+8;
  294. }
  295. else
  296. return -1;
  297. return 1;
  298. }
  299. unsigned long *
  300. Xalloc (int m)
  301. {
  302. #ifdef WIKIPCF
  303. void * mem = malloc(m);
  304. return (unsigned long *)mem;
  305. #else
  306. void * mem = malloc_simple(m,MEM_TAG_ARTICLE_F1);
  307. return (unsigned long *)mem;
  308. #endif
  309. }*/
  310. /*
  311. int load_bmf(pcffont_bmf_t *font)
  312. {
  313. int fd,file_size,read_size;
  314. fd = openfile(font->file,0);
  315. if(fd<0)
  316. return -1;
  317. file_size = (256+1) * sizeof(charmetric_bmf);
  318. font->charmetric = (char*)Xalloc(file_size);
  319. read_size = readfile(fd, font->charmetric, file_size);
  320. #ifdef WIKIPCF
  321. font->file_size = lseek(fd, 0, SEEK_END);
  322. #else
  323. wl_fsize(fd, &(font->file_size));
  324. #endif
  325. return fd;
  326. }
  327. int
  328. pres_bmfbm(ucs4_t val, pcffont_bmf_t *font, bmf_bm_t **bitmap,charmetric_bmf *Cmetrics)
  329. {
  330. int size = 0;
  331. int read_size = 0, offset = 0;
  332. char buffer[1024];
  333. if(val <= 256)
  334. memcpy(Cmetrics,font->charmetric+(val+1)*sizeof(charmetric_bmf),sizeof(charmetric_bmf));
  335. else
  336. {
  337. offset = (val+1)*sizeof(charmetric_bmf);
  338. if (offset <= font->file_size - sizeof(charmetric_bmf))
  339. #ifdef WIKIPCF
  340. lseek(font->fd,offset,SEEK_SET);
  341. #else
  342. wl_seek(font->fd,offset);
  343. #endif
  344. else
  345. {
  346. if (font->bPartialFont)
  347. { // character not defined in the current font file (and it is intended to include partial characters)
  348. font++;
  349. return pres_bmfbm(val, font, bitmap, Cmetrics);
  350. }
  351. else
  352. return -1;
  353. }
  354. // size = sizeof(charmetric_bmf);
  355. size = 1024; // Due to the nature of wl_read, the read size needs to be 1024.
  356. read_size = readfile(font->fd,buffer,size);
  357. memcpy(Cmetrics,buffer,sizeof(charmetric_bmf));
  358. }
  359. if(Cmetrics->width>0)
  360. *bitmap = (bmf_bm_t*)Cmetrics+8;
  361. else
  362. return -1;
  363. return 1;
  364. }
  365. unsigned long *
  366. Xalloc (int m)
  367. {
  368. #ifdef WIKIPCF
  369. void * mem = malloc(m);
  370. return (unsigned long *)mem;
  371. #else
  372. void * mem = malloc_simple(m,MEM_TAG_ARTICLE_F1);
  373. return (unsigned long *)mem;
  374. #endif
  375. }*/