glyph.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. // Copyright (C) 2004-2021 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include "glyph-imp.h"
  24. #include "pixmap-imp.h"
  25. #include <string.h>
  26. #define RLE_THRESHOLD 256
  27. fz_glyph *
  28. fz_keep_glyph(fz_context *ctx, fz_glyph *glyph)
  29. {
  30. return fz_keep_storable(ctx, &glyph->storable);
  31. }
  32. void
  33. fz_drop_glyph(fz_context *ctx, fz_glyph *glyph)
  34. {
  35. fz_drop_storable(ctx, &glyph->storable);
  36. }
  37. static void
  38. fz_drop_glyph_imp(fz_context *ctx, fz_storable *glyph_)
  39. {
  40. fz_glyph *glyph = (fz_glyph *)glyph_;
  41. fz_drop_pixmap(ctx, glyph->pixmap);
  42. fz_free(ctx, glyph);
  43. }
  44. fz_irect
  45. fz_glyph_bbox(fz_context *ctx, fz_glyph *glyph)
  46. {
  47. fz_irect bbox;
  48. bbox.x0 = glyph->x;
  49. bbox.y0 = glyph->y;
  50. bbox.x1 = glyph->x + glyph->w;
  51. bbox.y1 = glyph->y + glyph->h;
  52. return bbox;
  53. }
  54. fz_irect
  55. fz_glyph_bbox_no_ctx(fz_glyph *glyph)
  56. {
  57. fz_irect bbox;
  58. bbox.x0 = glyph->x;
  59. bbox.y0 = glyph->y;
  60. bbox.x1 = glyph->x + glyph->w;
  61. bbox.y1 = glyph->y + glyph->h;
  62. return bbox;
  63. }
  64. int
  65. fz_glyph_width(fz_context *ctx, fz_glyph *glyph)
  66. {
  67. return glyph->w;
  68. }
  69. int
  70. fz_glyph_height(fz_context *ctx, fz_glyph *glyph)
  71. {
  72. return glyph->h;
  73. }
  74. #ifndef NDEBUG
  75. #include <stdio.h>
  76. void
  77. fz_dump_glyph(fz_glyph *glyph)
  78. {
  79. int x, y;
  80. if (glyph->pixmap)
  81. {
  82. printf("pixmap glyph\n");
  83. return;
  84. }
  85. printf("glyph: %dx%d @ (%d,%d)\n", glyph->w, glyph->h, glyph->x, glyph->y);
  86. for (y = 0; y < glyph->h; y++)
  87. {
  88. int offset = ((int *)(glyph->data))[y];
  89. if (offset >= 0)
  90. {
  91. int extend = 0;
  92. int eol = 0;
  93. x = glyph->w;
  94. do
  95. {
  96. int v = glyph->data[offset++];
  97. int len;
  98. char c;
  99. switch(v&3)
  100. {
  101. case 0: /* extend */
  102. extend = v>>2;
  103. len = 0;
  104. break;
  105. case 1: /* Transparent pixels */
  106. len = 1 + (v>>2) + (extend<<6);
  107. extend = 0;
  108. c = '.';
  109. break;
  110. case 2: /* Solid pixels */
  111. len = 1 + (v>>3) + (extend<<5);
  112. extend = 0;
  113. eol = v & 4;
  114. c = (eol ? '$' :'#');
  115. break;
  116. default: /* Intermediate pixels */
  117. len = 1 + (v>>3) + (extend<<5);
  118. extend = 0;
  119. offset += len;
  120. eol = v & 4;
  121. c = (eol ? '!' : '?');
  122. break;
  123. }
  124. x -= len;
  125. while (len--)
  126. fputc(c, stdout);
  127. if (eol)
  128. break;
  129. }
  130. while (x > 0);
  131. }
  132. printf("\n");
  133. }
  134. }
  135. #endif
  136. fz_glyph *
  137. fz_new_glyph_from_pixmap(fz_context *ctx, fz_pixmap *pix)
  138. {
  139. fz_glyph *glyph = NULL;
  140. if (pix == NULL)
  141. return NULL;
  142. fz_var(glyph);
  143. fz_try(ctx)
  144. {
  145. if (pix->n != 1 || pix->w * pix->h < RLE_THRESHOLD)
  146. {
  147. glyph = fz_malloc_struct(ctx, fz_glyph);
  148. FZ_INIT_STORABLE(glyph, 1, fz_drop_glyph_imp);
  149. glyph->x = pix->x;
  150. glyph->y = pix->y;
  151. glyph->w = pix->w;
  152. glyph->h = pix->h;
  153. glyph->size = fz_pixmap_size(ctx, pix);
  154. glyph->pixmap = fz_keep_pixmap(ctx, pix);
  155. }
  156. else
  157. glyph = fz_new_glyph_from_8bpp_data(ctx, pix->x, pix->y, pix->w, pix->h, pix->samples, pix->stride);
  158. }
  159. fz_always(ctx)
  160. {
  161. fz_drop_pixmap(ctx, pix);
  162. }
  163. fz_catch(ctx)
  164. {
  165. fz_rethrow(ctx);
  166. }
  167. return glyph;
  168. }
  169. fz_glyph *
  170. fz_new_glyph_from_8bpp_data(fz_context *ctx, int x, int y, int w, int h, unsigned char *sp, int span)
  171. {
  172. fz_glyph *glyph = NULL;
  173. fz_pixmap *pix = NULL;
  174. int size, fill, yy;
  175. unsigned char *orig_sp = sp;
  176. fz_var(glyph);
  177. fz_var(pix);
  178. fz_try(ctx)
  179. {
  180. /* We start out by allocating space as large as the pixmap.
  181. * If we need more than that give up on using RLE. We can
  182. * never hope to beat the pixmap for really small sizes. */
  183. if (w <= 6 || w * h < RLE_THRESHOLD)
  184. goto try_pixmap;
  185. size = h * w;
  186. fill = h * sizeof(int);
  187. glyph = Memento_label(fz_malloc(ctx, sizeof(fz_glyph) + size), "fz_glyph(8)");
  188. FZ_INIT_STORABLE(glyph, 1, fz_drop_glyph_imp);
  189. glyph->x = x;
  190. glyph->y = y;
  191. glyph->w = w;
  192. glyph->h = h;
  193. glyph->pixmap = NULL;
  194. if (h == 0)
  195. {
  196. glyph->size = 0;
  197. break;
  198. }
  199. for (yy=0; yy < h; yy++)
  200. {
  201. int nonblankfill = fill;
  202. int nonblankfill_end = fill;
  203. int linefill = fill;
  204. int ww = w;
  205. do
  206. {
  207. int code;
  208. int len = ww;
  209. int needed;
  210. unsigned char *ep;
  211. switch (*sp)
  212. {
  213. case 0:
  214. if (len > 0x1000)
  215. len = 0x1000;
  216. ep = sp+len;
  217. while (++sp != ep && *sp == 0);
  218. code = 1;
  219. len -= ep-sp;
  220. ww -= len;
  221. needed = fill + 1 + (len > 0x40);
  222. break;
  223. case 255:
  224. if (len > 0x800)
  225. len = 0x800;
  226. ep = sp+len;
  227. while (++sp != ep && *sp == 255);
  228. code = 2;
  229. len -= ep-sp;
  230. ww -= len;
  231. needed = fill + 1 + (len > 0x20);
  232. break;
  233. default:
  234. {
  235. unsigned char c;
  236. if (len > 0x800)
  237. len = 0x800;
  238. ep = sp+len;
  239. while (++sp != ep && (c = *sp) != 255 && c != 0);
  240. len -= ep-sp;
  241. ww -= len;
  242. needed = fill + 1 + len + (len > 0x20);
  243. code = 3;
  244. }
  245. }
  246. if (needed > size)
  247. goto try_pixmap;
  248. if (code == 1)
  249. {
  250. if (len > 0x40)
  251. glyph->data[fill++] = ((len-1)>>6)<<2;
  252. glyph->data[fill++] = 1 | (((len-1)&63)<<2);
  253. }
  254. else
  255. {
  256. if (len > 0x20)
  257. glyph->data[fill++] = ((len-1)>>5)<<2;
  258. nonblankfill = fill;
  259. glyph->data[fill++] = code | (((len-1)&31)<<3);
  260. if (code == 3)
  261. {
  262. memcpy(&glyph->data[fill], sp - len, len);
  263. fill += len;
  264. }
  265. nonblankfill_end = fill;
  266. }
  267. }
  268. while (ww > 0);
  269. if (nonblankfill_end == linefill)
  270. {
  271. ((int *)(glyph->data))[yy] = -1;
  272. fill = linefill;
  273. }
  274. else
  275. {
  276. glyph->data[nonblankfill] |= 4;
  277. fill = nonblankfill_end;
  278. ((int *)(glyph->data))[yy] = linefill;
  279. }
  280. sp += span - w;
  281. }
  282. if (fill != size)
  283. {
  284. glyph = fz_realloc(ctx, glyph, sizeof(fz_glyph) + fill);
  285. size = fill;
  286. }
  287. glyph->size = size;
  288. break;
  289. /* Nasty use of a goto here, but it saves us having to exit
  290. * and reenter the try context, and this routine is speed
  291. * critical. */
  292. try_pixmap:
  293. glyph = Memento_label(fz_realloc(ctx, glyph, sizeof(fz_glyph)), "fz_glyph(8r)");
  294. FZ_INIT_STORABLE(glyph, 1, fz_drop_glyph_imp);
  295. pix = fz_new_pixmap_from_8bpp_data(ctx, x, y, w, h, orig_sp, span);
  296. glyph->x = pix->x;
  297. glyph->y = pix->y;
  298. glyph->w = pix->w;
  299. glyph->h = pix->h;
  300. glyph->size = fz_pixmap_size(ctx, pix);
  301. glyph->pixmap = pix;
  302. }
  303. fz_catch(ctx)
  304. {
  305. fz_drop_pixmap(ctx, pix);
  306. fz_free(ctx, glyph);
  307. fz_rethrow(ctx);
  308. }
  309. return glyph;
  310. }
  311. fz_glyph *
  312. fz_new_glyph_from_1bpp_data(fz_context *ctx, int x, int y, int w, int h, unsigned char *sp, int span)
  313. {
  314. fz_pixmap *pix = NULL;
  315. fz_glyph *glyph = NULL;
  316. int size, fill, yy;
  317. unsigned char *orig_sp = sp;
  318. fz_var(glyph);
  319. fz_var(pix);
  320. fz_try(ctx)
  321. {
  322. /* We start out by allocating space as large as the pixmap.
  323. * If we need more than that give up on using RLE. We can
  324. * never hope to beat the pixmap for really small sizes. */
  325. if (w <= 6 || w * h < RLE_THRESHOLD)
  326. goto try_pixmap;
  327. size = h * w;
  328. fill = h * sizeof(int);
  329. glyph = Memento_label(fz_malloc(ctx, sizeof(fz_glyph) + size), "fz_glyph(1)");
  330. FZ_INIT_STORABLE(glyph, 1, fz_drop_glyph_imp);
  331. glyph->x = x;
  332. glyph->y = y;
  333. glyph->w = w;
  334. glyph->h = h;
  335. glyph->pixmap = NULL;
  336. if (h == 0)
  337. {
  338. glyph->size = 0;
  339. break;
  340. }
  341. for (yy=0; yy < h; yy++)
  342. {
  343. int nonblankfill = fill;
  344. int nonblankfill_end = fill;
  345. int linefill = fill;
  346. int ww = w;
  347. int bit = 0x80;
  348. do
  349. {
  350. int len = 0;
  351. int needed;
  352. int b = *sp & bit;
  353. bit >>= 1;
  354. if (bit == 0)
  355. bit = 0x80, sp++;
  356. ww--;
  357. if (b == 0)
  358. {
  359. while (ww > 0 && len < 0xfff && (*sp & bit) == 0)
  360. {
  361. bit >>= 1;
  362. if (bit == 0)
  363. bit = 0x80, sp++;
  364. len++;
  365. ww--;
  366. }
  367. needed = fill + (len >= 0x40) + 1;
  368. if (needed > size)
  369. goto try_pixmap;
  370. if (len >= 0x40)
  371. glyph->data[fill++] = (len>>6)<<2;
  372. glyph->data[fill++] = 1 | ((len&63)<<2);
  373. }
  374. else
  375. {
  376. while (ww > 0 && len < 0x7ff && (*sp & bit) != 0)
  377. {
  378. bit >>= 1;
  379. if (bit == 0)
  380. bit = 0x80, sp++;
  381. len++;
  382. ww--;
  383. }
  384. needed = fill + (len >= 0x20) + 1;
  385. if (needed > size)
  386. goto try_pixmap;
  387. if (len >= 0x20)
  388. glyph->data[fill++] = (len>>5)<<2;
  389. nonblankfill = fill;
  390. glyph->data[fill++] = 2 | ((len&31)<<3);
  391. nonblankfill_end = fill;
  392. }
  393. }
  394. while (ww > 0);
  395. if (nonblankfill_end == linefill)
  396. {
  397. ((int *)(glyph->data))[yy] = -1;
  398. fill = linefill;
  399. }
  400. else
  401. {
  402. glyph->data[nonblankfill] |= 4;
  403. fill = nonblankfill_end;
  404. ((int *)(glyph->data))[yy] = linefill;
  405. }
  406. sp += span - (w>>3);
  407. }
  408. if (fill != size)
  409. {
  410. glyph = fz_realloc(ctx, glyph, sizeof(fz_glyph) + fill);
  411. size = fill;
  412. }
  413. glyph->size = size;
  414. break;
  415. /* Nasty use of a goto here, but it saves us having to exit
  416. * and reenter the try context, and this routine is speed
  417. * critical. */
  418. try_pixmap:
  419. glyph = fz_realloc(ctx, glyph, sizeof(fz_glyph));
  420. FZ_INIT_STORABLE(glyph, 1, fz_drop_glyph_imp);
  421. pix = fz_new_pixmap_from_1bpp_data(ctx, x, y, w, h, orig_sp, span);
  422. glyph->x = pix->x;
  423. glyph->y = pix->y;
  424. glyph->w = pix->w;
  425. glyph->h = pix->h;
  426. glyph->size = fz_pixmap_size(ctx, pix);
  427. glyph->pixmap = pix;
  428. }
  429. fz_catch(ctx)
  430. {
  431. fz_drop_pixmap(ctx, pix);
  432. fz_free(ctx, glyph);
  433. fz_rethrow(ctx);
  434. }
  435. return glyph;
  436. }