Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #ifndef _MUPDF_H_
  2. #define _MUPDF_H_
  3.  
  4. #ifndef _FITZ_H_
  5. #error "fitz.h must be included before mupdf.h"
  6. #endif
  7.  
  8. typedef struct pdf_xref_s pdf_xref;
  9.  
  10. /*
  11.  * tokenizer and low-level object parser
  12.  */
  13.  
  14. enum
  15. {
  16.         PDF_TOK_ERROR, PDF_TOK_EOF,
  17.         PDF_TOK_OPEN_ARRAY, PDF_TOK_CLOSE_ARRAY,
  18.         PDF_TOK_OPEN_DICT, PDF_TOK_CLOSE_DICT,
  19.         PDF_TOK_OPEN_BRACE, PDF_TOK_CLOSE_BRACE,
  20.         PDF_TOK_NAME, PDF_TOK_INT, PDF_TOK_REAL, PDF_TOK_STRING, PDF_TOK_KEYWORD,
  21.         PDF_TOK_R, PDF_TOK_TRUE, PDF_TOK_FALSE, PDF_TOK_NULL,
  22.         PDF_TOK_OBJ, PDF_TOK_ENDOBJ,
  23.         PDF_TOK_STREAM, PDF_TOK_ENDSTREAM,
  24.         PDF_TOK_XREF, PDF_TOK_TRAILER, PDF_TOK_STARTXREF,
  25.         PDF_NUM_TOKENS
  26. };
  27.  
  28. fz_error pdf_lex(int *tok, fz_stream *f, char *buf, int n, int *len);
  29.  
  30. fz_error pdf_parse_array(fz_obj **op, pdf_xref *xref, fz_stream *f, char *buf, int cap);
  31. fz_error pdf_parse_dict(fz_obj **op, pdf_xref *xref, fz_stream *f, char *buf, int cap);
  32. fz_error pdf_parse_stm_obj(fz_obj **op, pdf_xref *xref, fz_stream *f, char *buf, int cap);
  33. fz_error pdf_parse_ind_obj(fz_obj **op, pdf_xref *xref, fz_stream *f, char *buf, int cap, int *num, int *gen, int *stm_ofs);
  34.  
  35. fz_rect pdf_to_rect(fz_obj *array);
  36. fz_matrix pdf_to_matrix(fz_obj *array);
  37. char *pdf_to_utf8(fz_obj *src);
  38. unsigned short *pdf_to_ucs2(fz_obj *src);
  39. fz_obj *pdf_to_utf8_name(fz_obj *src);
  40. char *pdf_from_ucs2(unsigned short *str);
  41.  
  42. /*
  43.  * xref and object / stream api
  44.  */
  45.  
  46. typedef struct pdf_xref_entry_s pdf_xref_entry;
  47. typedef struct pdf_crypt_s pdf_crypt;
  48.  
  49. struct pdf_xref_entry_s
  50. {
  51.         int ofs;        /* file offset / objstm object number */
  52.         int gen;        /* generation / objstm index */
  53.         int stm_ofs;    /* on-disk stream */
  54.         fz_obj *obj;    /* stored/cached object */
  55.         int type;       /* 0=unset (f)ree i(n)use (o)bjstm */
  56. };
  57.  
  58. struct pdf_xref_s
  59. {
  60.         fz_stream *file;
  61.         int version;
  62.         int startxref;
  63.         int file_size;
  64.         pdf_crypt *crypt;
  65.         fz_obj *trailer;
  66.  
  67.         int len;
  68.         pdf_xref_entry *table;
  69.  
  70.         int page_len;
  71.         int page_cap;
  72.         fz_obj **page_objs;
  73.         fz_obj **page_refs;
  74.  
  75.         struct pdf_store_s *store;
  76.  
  77.         char scratch[65536];
  78. };
  79.  
  80. fz_obj *pdf_resolve_indirect(fz_obj *ref);
  81. fz_error pdf_cache_object(pdf_xref *, int num, int gen);
  82. fz_error pdf_load_object(fz_obj **objp, pdf_xref *, int num, int gen);
  83. void pdf_update_object( pdf_xref *xref, int num, int gen, fz_obj *newobj);
  84.  
  85. int pdf_is_stream(pdf_xref *xref, int num, int gen);
  86. fz_stream *pdf_open_inline_stream(fz_stream *chain, pdf_xref *xref, fz_obj *stmobj, int length);
  87. fz_error pdf_load_raw_stream(fz_buffer **bufp, pdf_xref *xref, int num, int gen);
  88. fz_error pdf_load_stream(fz_buffer **bufp, pdf_xref *xref, int num, int gen);
  89. fz_error pdf_open_raw_stream(fz_stream **stmp, pdf_xref *, int num, int gen);
  90. fz_error pdf_open_stream(fz_stream **stmp, pdf_xref *, int num, int gen);
  91. fz_error pdf_open_stream_at(fz_stream **stmp, pdf_xref *xref, int num, int gen, fz_obj *dict, int stm_ofs);
  92.  
  93. fz_error pdf_open_xref_with_stream(pdf_xref **xrefp, fz_stream *file, char *password);
  94. fz_error pdf_open_xref(pdf_xref **xrefp, const char *filename, char *password);
  95. void pdf_free_xref(pdf_xref *);
  96.  
  97. /* private */
  98. fz_error pdf_repair_xref(pdf_xref *xref, char *buf, int bufsize);
  99. fz_error pdf_repair_obj_stms(pdf_xref *xref);
  100. void pdf_debug_xref(pdf_xref *);
  101. void pdf_resize_xref(pdf_xref *xref, int newcap);
  102.  
  103. /*
  104.  * Encryption
  105.  */
  106.  
  107. enum
  108. {
  109.         PDF_PERM_PRINT = 1 << 2,
  110.         PDF_PERM_CHANGE = 1 << 3,
  111.         PDF_PERM_COPY = 1 << 4,
  112.         PDF_PERM_NOTES = 1 << 5,
  113.         PDF_PERM_FILL_FORM = 1 << 8,
  114.         PDF_PERM_ACCESSIBILITY = 1 << 9,
  115.         PDF_PERM_ASSEMBLE = 1 << 10,
  116.         PDF_PERM_HIGH_RES_PRINT = 1 << 11,
  117.         PDF_DEFAULT_PERM_FLAGS = 0xfffc
  118. };
  119.  
  120. fz_error pdf_new_crypt(pdf_crypt **cp, fz_obj *enc, fz_obj *id);
  121. void pdf_free_crypt(pdf_crypt *crypt);
  122.  
  123. void pdf_crypt_obj(pdf_crypt *crypt, fz_obj *obj, int num, int gen);
  124. fz_stream *pdf_open_crypt(fz_stream *chain, pdf_crypt *crypt, int num, int gen);
  125. fz_stream *pdf_open_crypt_with_filter(fz_stream *chain, pdf_crypt *crypt, char *name, int num, int gen);
  126.  
  127. int pdf_needs_password(pdf_xref *xref);
  128. int pdf_authenticate_password(pdf_xref *xref, char *pw);
  129. int pdf_has_permission(pdf_xref *xref, int p);
  130.  
  131. int pdf_get_crypt_revision(pdf_xref *xref);
  132. char *pdf_get_crypt_method(pdf_xref *xref);
  133. int pdf_get_crypt_length(pdf_xref *xref);
  134. unsigned char *pdf_get_crypt_key(pdf_xref *xref);
  135.  
  136. void pdf_debug_crypt(pdf_crypt *crypt);
  137.  
  138. /*
  139.  * Resource store
  140.  */
  141.  
  142. typedef struct pdf_store_s pdf_store;
  143.  
  144. pdf_store *pdf_new_store(void);
  145. void pdf_free_store(pdf_store *store);
  146. void pdf_debug_store(pdf_store *store);
  147.  
  148. void pdf_store_item(pdf_store *store, void *keepfn, void *dropfn, fz_obj *key, void *val);
  149. void *pdf_find_item(pdf_store *store, void *dropfn, fz_obj *key);
  150. void pdf_remove_item(pdf_store *store, void *dropfn, fz_obj *key);
  151. void pdf_age_store(pdf_store *store, int maxage);
  152.  
  153. /*
  154.  * Functions, Colorspaces, Shadings and Images
  155.  */
  156.  
  157. typedef struct pdf_function_s pdf_function;
  158.  
  159. fz_error pdf_load_function(pdf_function **func, pdf_xref *xref, fz_obj *ref);
  160. void pdf_eval_function(pdf_function *func, float *in, int inlen, float *out, int outlen);
  161. pdf_function *pdf_keep_function(pdf_function *func);
  162. void pdf_drop_function(pdf_function *func);
  163.  
  164. fz_error pdf_load_colorspace(fz_colorspace **csp, pdf_xref *xref, fz_obj *obj);
  165. fz_pixmap *pdf_expand_indexed_pixmap(fz_pixmap *src);
  166.  
  167. fz_error pdf_load_shading(fz_shade **shadep, pdf_xref *xref, fz_obj *obj);
  168.  
  169. fz_error pdf_load_inline_image(fz_pixmap **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict, fz_stream *file);
  170. fz_error pdf_load_image(fz_pixmap **imgp, pdf_xref *xref, fz_obj *obj);
  171. int pdf_is_jpx_image(fz_obj *dict);
  172.  
  173. /*
  174.  * Pattern
  175.  */
  176.  
  177. typedef struct pdf_pattern_s pdf_pattern;
  178.  
  179. struct pdf_pattern_s
  180. {
  181.         int refs;
  182.         int ismask;
  183.         float xstep;
  184.         float ystep;
  185.         fz_matrix matrix;
  186.         fz_rect bbox;
  187.         fz_obj *resources;
  188.         fz_buffer *contents;
  189. };
  190.  
  191. fz_error pdf_load_pattern(pdf_pattern **patp, pdf_xref *xref, fz_obj *obj);
  192. pdf_pattern *pdf_keep_pattern(pdf_pattern *pat);
  193. void pdf_drop_pattern(pdf_pattern *pat);
  194.  
  195. /*
  196.  * XObject
  197.  */
  198.  
  199. typedef struct pdf_xobject_s pdf_xobject;
  200.  
  201. struct pdf_xobject_s
  202. {
  203.         int refs;
  204.         fz_matrix matrix;
  205.         fz_rect bbox;
  206.         int isolated;
  207.         int knockout;
  208.         int transparency;
  209.         fz_colorspace *colorspace;
  210.         fz_obj *resources;
  211.         fz_buffer *contents;
  212. };
  213.  
  214. fz_error pdf_load_xobject(pdf_xobject **xobjp, pdf_xref *xref, fz_obj *obj);
  215. pdf_xobject *pdf_keep_xobject(pdf_xobject *xobj);
  216. void pdf_drop_xobject(pdf_xobject *xobj);
  217.  
  218. /*
  219.  * CMap
  220.  */
  221.  
  222. typedef struct pdf_cmap_s pdf_cmap;
  223. typedef struct pdf_range_s pdf_range;
  224.  
  225. enum { PDF_CMAP_SINGLE, PDF_CMAP_RANGE, PDF_CMAP_TABLE, PDF_CMAP_MULTI };
  226.  
  227. struct pdf_range_s
  228. {
  229.         unsigned short low;
  230.         /* Next, we pack 2 fields into the same unsigned short. Top 14 bits
  231.          * are the extent, bottom 2 bits are flags: single, range, table,
  232.          * multi */
  233.         unsigned short extent_flags;
  234.         unsigned short offset;  /* range-delta or table-index */
  235. };
  236.  
  237. struct pdf_cmap_s
  238. {
  239.         int refs;
  240.         char cmap_name[32];
  241.  
  242.         char usecmap_name[32];
  243.         pdf_cmap *usecmap;
  244.  
  245.         int wmode;
  246.  
  247.         int codespace_len;
  248.         struct
  249.         {
  250.                 unsigned short n;
  251.                 unsigned short low;
  252.                 unsigned short high;
  253.         } codespace[40];
  254.  
  255.         int rlen, rcap;
  256.         pdf_range *ranges;
  257.  
  258.         int tlen, tcap;
  259.         unsigned short *table;
  260. };
  261.  
  262. pdf_cmap *pdf_new_cmap(void);
  263. pdf_cmap *pdf_keep_cmap(pdf_cmap *cmap);
  264. void pdf_drop_cmap(pdf_cmap *cmap);
  265.  
  266. void pdf_debug_cmap(pdf_cmap *cmap);
  267. int pdf_get_wmode(pdf_cmap *cmap);
  268. void pdf_set_wmode(pdf_cmap *cmap, int wmode);
  269. void pdf_set_usecmap(pdf_cmap *cmap, pdf_cmap *usecmap);
  270.  
  271. void pdf_add_codespace(pdf_cmap *cmap, int low, int high, int n);
  272. void pdf_map_range_to_table(pdf_cmap *cmap, int low, int *map, int len);
  273. void pdf_map_range_to_range(pdf_cmap *cmap, int srclo, int srchi, int dstlo);
  274. void pdf_map_one_to_many(pdf_cmap *cmap, int one, int *many, int len);
  275. void pdf_sort_cmap(pdf_cmap *cmap);
  276.  
  277. int pdf_lookup_cmap(pdf_cmap *cmap, int cpt);
  278. int pdf_lookup_cmap_full(pdf_cmap *cmap, int cpt, int *out);
  279. unsigned char *pdf_decode_cmap(pdf_cmap *cmap, unsigned char *s, int *cpt);
  280.  
  281. pdf_cmap *pdf_new_identity_cmap(int wmode, int bytes);
  282. fz_error pdf_parse_cmap(pdf_cmap **cmapp, fz_stream *file);
  283. fz_error pdf_load_embedded_cmap(pdf_cmap **cmapp, pdf_xref *xref, fz_obj *ref);
  284. fz_error pdf_load_system_cmap(pdf_cmap **cmapp, char *name);
  285. pdf_cmap *pdf_find_builtin_cmap(char *cmap_name);
  286.  
  287. /*
  288.  * Font
  289.  */
  290.  
  291. enum
  292. {
  293.         PDF_FD_FIXED_PITCH = 1 << 0,
  294.         PDF_FD_SERIF = 1 << 1,
  295.         PDF_FD_SYMBOLIC = 1 << 2,
  296.         PDF_FD_SCRIPT = 1 << 3,
  297.         PDF_FD_NONSYMBOLIC = 1 << 5,
  298.         PDF_FD_ITALIC = 1 << 6,
  299.         PDF_FD_ALL_CAP = 1 << 16,
  300.         PDF_FD_SMALL_CAP = 1 << 17,
  301.         PDF_FD_FORCE_BOLD = 1 << 18
  302. };
  303.  
  304. enum { PDF_ROS_CNS, PDF_ROS_GB, PDF_ROS_JAPAN, PDF_ROS_KOREA };
  305.  
  306. void pdf_load_encoding(char **estrings, char *encoding);
  307. int pdf_lookup_agl(char *name);
  308. const char **pdf_lookup_agl_duplicates(int ucs);
  309.  
  310. extern const unsigned short pdf_doc_encoding[256];
  311. extern const char * const pdf_mac_roman[256];
  312. extern const char * const pdf_mac_expert[256];
  313. extern const char * const pdf_win_ansi[256];
  314. extern const char * const pdf_standard[256];
  315.  
  316. typedef struct pdf_font_desc_s pdf_font_desc;
  317. typedef struct pdf_hmtx_s pdf_hmtx;
  318. typedef struct pdf_vmtx_s pdf_vmtx;
  319.  
  320. struct pdf_hmtx_s
  321. {
  322.         unsigned short lo;
  323.         unsigned short hi;
  324.         int w;  /* type3 fonts can be big! */
  325. };
  326.  
  327. struct pdf_vmtx_s
  328. {
  329.         unsigned short lo;
  330.         unsigned short hi;
  331.         short x;
  332.         short y;
  333.         short w;
  334. };
  335.  
  336. struct pdf_font_desc_s
  337. {
  338.         int refs;
  339.  
  340.         fz_font *font;
  341.  
  342.         /* FontDescriptor */
  343.         int flags;
  344.         float italic_angle;
  345.         float ascent;
  346.         float descent;
  347.         float cap_height;
  348.         float x_height;
  349.         float missing_width;
  350.  
  351.         /* Encoding (CMap) */
  352.         pdf_cmap *encoding;
  353.         pdf_cmap *to_ttf_cmap;
  354.         int cid_to_gid_len;
  355.         unsigned short *cid_to_gid;
  356.  
  357.         /* ToUnicode */
  358.         pdf_cmap *to_unicode;
  359.         int cid_to_ucs_len;
  360.         unsigned short *cid_to_ucs;
  361.  
  362.         /* Metrics (given in the PDF file) */
  363.         int wmode;
  364.  
  365.         int hmtx_len, hmtx_cap;
  366.         pdf_hmtx dhmtx;
  367.         pdf_hmtx *hmtx;
  368.  
  369.         int vmtx_len, vmtx_cap;
  370.         pdf_vmtx dvmtx;
  371.         pdf_vmtx *vmtx;
  372.  
  373.         int is_embedded;
  374. };
  375.  
  376. void pdf_set_font_wmode(pdf_font_desc *font, int wmode);
  377. void pdf_set_default_hmtx(pdf_font_desc *font, int w);
  378. void pdf_set_default_vmtx(pdf_font_desc *font, int y, int w);
  379. void pdf_add_hmtx(pdf_font_desc *font, int lo, int hi, int w);
  380. void pdf_add_vmtx(pdf_font_desc *font, int lo, int hi, int x, int y, int w);
  381. void pdf_end_hmtx(pdf_font_desc *font);
  382. void pdf_end_vmtx(pdf_font_desc *font);
  383. pdf_hmtx pdf_get_hmtx(pdf_font_desc *font, int cid);
  384. pdf_vmtx pdf_get_vmtx(pdf_font_desc *font, int cid);
  385.  
  386. fz_error pdf_load_to_unicode(pdf_font_desc *font, pdf_xref *xref, char **strings, char *collection, fz_obj *cmapstm);
  387.  
  388. int pdf_font_cid_to_gid(pdf_font_desc *fontdesc, int cid);
  389.  
  390. unsigned char *pdf_find_builtin_font(char *name, unsigned int *len);
  391. unsigned char *pdf_find_substitute_font(int mono, int serif, int bold, int italic, unsigned int *len);
  392. unsigned char *pdf_find_substitute_cjk_font(int ros, int serif, unsigned int *len);
  393.  
  394. fz_error pdf_load_type3_font(pdf_font_desc **fontp, pdf_xref *xref, fz_obj *rdb, fz_obj *obj);
  395. fz_error pdf_load_font(pdf_font_desc **fontp, pdf_xref *xref, fz_obj *rdb, fz_obj *obj);
  396.  
  397. pdf_font_desc *pdf_new_font_desc(void);
  398. pdf_font_desc *pdf_keep_font(pdf_font_desc *fontdesc);
  399. void pdf_drop_font(pdf_font_desc *font);
  400.  
  401. void pdf_debug_font(pdf_font_desc *fontdesc);
  402.  
  403. /*
  404.  * Interactive features
  405.  */
  406.  
  407. typedef struct pdf_link_s pdf_link;
  408. typedef struct pdf_annot_s pdf_annot;
  409. typedef struct pdf_outline_s pdf_outline;
  410.  
  411. typedef enum pdf_link_kind_e
  412. {
  413.         PDF_LINK_GOTO = 0,
  414.         PDF_LINK_URI,
  415.         PDF_LINK_LAUNCH,
  416.         PDF_LINK_NAMED,
  417.         PDF_LINK_ACTION,
  418. } pdf_link_kind;
  419.  
  420. struct pdf_link_s
  421. {
  422.         pdf_link_kind kind;
  423.         fz_rect rect;
  424.         fz_obj *dest;
  425.         pdf_link *next;
  426. };
  427.  
  428. struct pdf_annot_s
  429. {
  430.         fz_obj *obj;
  431.         fz_rect rect;
  432.         pdf_xobject *ap;
  433.         fz_matrix matrix;
  434.         pdf_annot *next;
  435. };
  436.  
  437. struct pdf_outline_s
  438. {
  439.         char *title;
  440.         pdf_link *link;
  441.         int count;
  442.         pdf_outline *child;
  443.         pdf_outline *next;
  444. };
  445.  
  446. fz_obj *pdf_lookup_dest(pdf_xref *xref, fz_obj *needle);
  447. fz_obj *pdf_lookup_name(pdf_xref *xref, char *which, fz_obj *needle);
  448. fz_obj *pdf_load_name_tree(pdf_xref *xref, char *which);
  449.  
  450. pdf_outline *pdf_load_outline(pdf_xref *xref);
  451. void pdf_debug_outline(pdf_outline *outline, int level);
  452. void pdf_free_outline(pdf_outline *outline);
  453.  
  454. pdf_link *pdf_load_link(pdf_xref *xref, fz_obj *dict);
  455. void pdf_load_links(pdf_link **, pdf_xref *, fz_obj *annots);
  456. void pdf_free_link(pdf_link *link);
  457.  
  458. void pdf_load_annots(pdf_annot **, pdf_xref *, fz_obj *annots);
  459. void pdf_free_annot(pdf_annot *link);
  460.  
  461. /*
  462.  * Page tree, pages and related objects
  463.  */
  464.  
  465. typedef struct pdf_page_s pdf_page;
  466.  
  467. struct pdf_page_s
  468. {
  469.         fz_rect mediabox;
  470.         int rotate;
  471.         int transparency;
  472.         fz_obj *resources;
  473.         fz_buffer *contents;
  474.         pdf_link *links;
  475.         pdf_annot *annots;
  476. };
  477.  
  478. fz_error pdf_load_page_tree(pdf_xref *xref);
  479. int pdf_find_page_number(pdf_xref *xref, fz_obj *pageobj);
  480. int pdf_count_pages(pdf_xref *xref);
  481.  
  482. fz_error pdf_load_page(pdf_page **pagep, pdf_xref *xref, int number);
  483. void pdf_free_page(pdf_page *page);
  484.  
  485. /*
  486.  * Content stream parsing
  487.  */
  488.  
  489. fz_error pdf_run_page_with_usage(pdf_xref *xref, pdf_page *page, fz_device *dev, fz_matrix ctm, char *target);
  490. fz_error pdf_run_page(pdf_xref *xref, pdf_page *page, fz_device *dev, fz_matrix ctm);
  491. fz_error pdf_run_glyph(pdf_xref *xref, fz_obj *resources, fz_buffer *contents, fz_device *dev, fz_matrix ctm);
  492.  
  493. #endif
  494.