home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / UsingPDF / GhostScript / source / gs5.10 / gdevpdfx.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-30  |  14.0 KB  |  446 lines

  1. /* Copyright (C) 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* Internal definitions for PDF-writing driver. */
  20. #include "gsparam.h"
  21. #include "gxdevice.h"
  22. #include "gxline.h"
  23. #include "stream.h"
  24. #include "gdevpstr.h"
  25. #include "gdevpsdf.h"
  26.  
  27. /* ---------------- Statically allocated sizes ---------------- */
  28. /* These should all really be dynamic.... */
  29.  
  30. /* Define the maximum size of the output file name. */
  31. #define fname_size 80
  32.  
  33. /* Define the maximum number of contents fragments on a page. */
  34. #define max_contents_ids 300
  35.  
  36. /* Define the maximum depth of an outline tree. */
  37. /* Note that there is no limit on the breadth of the tree. */
  38. #define max_outline_depth 8
  39.  
  40. /* Define the maximum size of a destination array string. */
  41. #define max_dest_string 80
  42.  
  43. /* ================ Types and structures ================ */
  44.  
  45. /* ---------------- Resources ---------------- */
  46.  
  47. typedef enum {
  48.   resourceFont,
  49.   resourceEncoding,
  50.   resourceFontDescriptor,
  51.   resourceColorSpace,
  52.   resourceXObject,
  53.   resourceCharProc,
  54.   num_resource_types
  55. } pdf_resource_type;
  56. #define pdf_resource_type_names\
  57.   "Font", "Encoding", "FontDescriptor", "ColorSpace", "XObject", 0
  58. #define pdf_resource_type_structs\
  59.   &st_pdf_font, &st_pdf_resource, &st_pdf_resource, &st_pdf_resource,\
  60.   &st_pdf_resource, &st_pdf_char_proc
  61.  
  62. #define pdf_resource_common(typ)\
  63.     typ *next;            /* next resource of this type */\
  64.     pdf_resource *prev;        /* previously allocated resource */\
  65.     gs_id rid;            /* optional key */\
  66.     long id
  67. typedef struct pdf_resource_s pdf_resource;
  68. struct pdf_resource_s {
  69.     pdf_resource_common(pdf_resource);
  70. };
  71. #define private_st_pdf_resource()\
  72.   gs_private_st_ptrs2(st_pdf_resource, pdf_resource, "pdf_resource",\
  73.     pdf_resource_enum_ptrs, pdf_resource_reloc_ptrs, next, prev)
  74.  
  75. /* Font resources */
  76. typedef struct pdf_char_proc_s pdf_char_proc;  /* forward reference */
  77. typedef struct pdf_font_s pdf_font;
  78. typedef struct pdf_font_name_s {
  79.     byte chars[40];        /* arbitrary, must be large enough for */
  80.                 /* the 14 built-in fonts */
  81.     uint size;
  82. } pdf_font_name;
  83. struct pdf_font_s {
  84.     pdf_resource_common(pdf_font);
  85.     pdf_font_name fname;
  86.     bool used_on_page;
  87.     char frname[6+1];    /* xxxxxx\0 */
  88.   /* Encoding differences for base fonts. */
  89.     byte chars_used[32];    /* 1 bit per character code */
  90.     gs_const_string *differences;
  91.     long diff_id;
  92.   /* Bookkeeping for embedded fonts. */
  93.     int num_chars;
  94. #define font_is_embedded(font) ((font)->num_chars != 0)
  95.     pdf_char_proc *char_procs;
  96.     int max_y_offset;
  97.   /* Pseudo-characters for spacing. */
  98.   /* The range should be determined by the device resolution.... */
  99. #define x_space_min 24
  100. #define x_space_max 150
  101.     byte spaces[x_space_max - x_space_min + 1];
  102. };
  103. #define private_st_pdf_font()\
  104.   gs_private_st_suffix_add2(st_pdf_font, pdf_font, "pdf_font",\
  105.     pdf_font_enum_ptrs, pdf_font_reloc_ptrs, st_pdf_resource,\
  106.     differences, char_procs)
  107.  
  108. /* CharProc pseudo-resources for embedded fonts */
  109. struct pdf_char_proc_s {
  110.     pdf_resource_common(pdf_char_proc);
  111.     pdf_font *font;
  112.     pdf_char_proc *char_next; /* next char_proc for same font */
  113.     int width, height;
  114.     int x_width;        /* X escapement */
  115.     int y_offset;        /* of character (0,0) */
  116.     byte char_code;
  117. };
  118. #define private_st_pdf_char_proc()\
  119.   gs_private_st_suffix_add2(st_pdf_char_proc, pdf_char_proc,\
  120.     "pdf_char_proc", pdf_char_proc_enum_ptrs,\
  121.     pdf_char_proc_reloc_ptrs, st_pdf_resource, font, char_next)
  122.  
  123. /* ---------------- Other auxiliary structures ---------------- */
  124.  
  125. /* Outline nodes and levels */
  126. typedef struct pdf_outline_node_s {
  127.   long id, parent_id, prev_id, first_id, last_id;
  128.   int count;
  129.   gs_string action_string;
  130. } pdf_outline_node;
  131. typedef struct pdf_outline_level_s {
  132.   pdf_outline_node first;
  133.   pdf_outline_node last;
  134.   int left;
  135. } pdf_outline_level;
  136.  
  137. /* Articles */
  138. typedef struct pdf_bead_s {
  139.   long id, article_id, prev_id, next_id;
  140.   char dest[max_dest_string];
  141.   gs_rect rect;
  142. } pdf_bead;
  143. typedef struct pdf_article_s pdf_article;
  144. struct pdf_article_s {
  145.   pdf_article *next;
  146.   gs_string title;
  147.   long id;
  148.   pdf_bead first;
  149.   pdf_bead last;
  150. };
  151. #define private_st_pdf_article()\
  152.   gs_private_st_ptrs1_strings1(st_pdf_article, pdf_article, "pdf_article",\
  153.     pdf_article_enum_ptrs, pdf_article_reloc_ptrs, next, title)
  154.  
  155. /* Named destinations */
  156. typedef struct pdf_named_dest_s pdf_named_dest;
  157. struct pdf_named_dest_s {
  158.   pdf_named_dest *next;
  159.   gs_string key;
  160.   char dest[max_dest_string];
  161. };
  162. #define private_st_pdf_named_dest()\
  163.   gs_private_st_ptrs1_strings1(st_pdf_named_dest, pdf_named_dest,\
  164.     "pdf_named_dest", pdf_named_dest_enum_ptrs, pdf_named_dest_reloc_ptrs,\
  165.     next, key)
  166.  
  167. /* ---------------- The device structure ---------------- */
  168.  
  169. /* Text state */
  170. typedef struct pdf_text_state_s {
  171.   /* State parameters */
  172.     float character_spacing;
  173.     pdf_font *font;
  174.     floatp size;
  175.     float word_spacing;
  176.     float horizontal_scaling;
  177.   /* Bookkeeping */
  178.     gs_matrix matrix;    /* relative to device space, not user space */
  179.     gs_point line_start;
  180.     gs_point current;
  181. #define max_text_buffer 200    /* arbitrary, but overflow costs 5 chars */
  182.     byte buffer[max_text_buffer];
  183.     int buffer_count;
  184. } pdf_text_state;
  185. #define pdf_text_state_default\
  186.   0, NULL, 0, 0, 100,\
  187.   { identity_matrix_body }, { 0, 0 }, { 0, 0 }, { 0 }, 0
  188.  
  189. /* Resource lists */
  190. #define num_resource_chains 16
  191. typedef struct pdf_resource_list_s {
  192.   pdf_resource *chains[num_resource_chains];
  193. } pdf_resource_list;
  194.  
  195. /* Define the bookkeeping for an open stream. */
  196. typedef struct pdf_stream_position_s {
  197.   long length_id;
  198.   long start_pos;
  199. } pdf_stream_position;
  200.  
  201. /* Define the device structure. */
  202. typedef enum {
  203.   NoMarks = 0,
  204.   ImageB = 1,
  205.   ImageC = 2,
  206.   ImageI = 4,
  207.   Text = 8
  208. } pdf_procset;
  209. typedef enum {
  210.   pdf_in_none,
  211.   pdf_in_stream,
  212.   pdf_in_text,
  213.   pdf_in_string
  214. } pdf_context;
  215. typedef struct gx_device_pdf_s {
  216.     gx_device_psdf_common;
  217.       /* PDF-specific distiller parameters */
  218.     float CompatibilityLevel;
  219.     bool DoThumbnails;        /* ****** OBSOLETE ****** */
  220.       /* End of distiller parameters */
  221.       /* Other parameters */
  222.     bool ReAssignCharacters;
  223.     bool ReEncodeCharacters;
  224.     long FirstObjectNumber;
  225.       /* End of parameters */
  226.     bool binary_ok;            /* if true, OK to output binary info */
  227.         /* End of settable parameters. */
  228.         /* Following are set when device is opened. */
  229.     enum {
  230.       pdf_compress_none,
  231.       pdf_compress_LZW,    /* not currently used, thanks to Unisys */
  232.       pdf_compress_Flate
  233.     } compression;
  234. #define pdf_memory v_memory
  235.     char tfname[fname_size + 1];
  236.     FILE *tfile;
  237.     char rfname[fname_size + 1];
  238.     FILE *rfile;
  239.     stream *rstrm;
  240.     byte *rstrmbuf;
  241.     stream *rsave_strm;
  242.     pdf_font *open_font;
  243.     long embedded_encoding_id;
  244.         /* ................ */
  245.     long next_id;
  246.         /* The following 2 IDs, and only these, are allocated */
  247.         /* when the file is opened. */
  248.     long root_id;
  249.     long info_id;
  250. #define pdf_num_initial_ids 2
  251.     long pages_id;
  252.     long outlines_id;
  253.     int next_page;
  254.     long contents_id;
  255.     pdf_context context;
  256.     long contents_length_id;
  257.     long contents_pos;
  258.     pdf_procset procsets;        /* used on this page */
  259.     float flatness;        /****** SHOULD USE state ******/
  260.     /* The line width, dash offset, and dash pattern */
  261.     /* are in default user space units. */
  262.     gx_line_params line_params;    /* current values */
  263.                 /****** SHOULD USE state ******/
  264.     pdf_text_state text;
  265.     long space_char_ids[x_space_max - x_space_min + 1];
  266. #define initial_num_page_ids 50
  267.     long *page_ids;
  268.     int num_page_ids;
  269.     int pages_referenced;
  270.     pdf_resource_list resources[num_resource_types];
  271.     pdf_resource *annots;        /* rid = page # */
  272.     pdf_resource *last_resource;
  273.     gs_string catalog_string;
  274.     gs_string pages_string;
  275.     gs_string page_string;
  276.     pdf_outline_level outline_levels[max_outline_depth];
  277.     int outline_depth;
  278.     int closed_outline_depth;
  279.     int outlines_open;
  280.     pdf_article *articles;
  281.     pdf_named_dest *named_dests;
  282. } gx_device_pdf;
  283. #define is_in_page(pdev)\
  284.   ((pdev)->contents_id != 0)
  285. #define is_in_document(pdev)\
  286.   (is_in_page(pdev) || (pdev)->last_resource != 0)
  287.  
  288. /* Enumerate the individual pointers in a gx_device_pdf */
  289. #define gx_device_pdf_do_ptrs(m)\
  290.  m(0,rstrm) m(1,rstrmbuf) m(2,rsave_strm) m(3,open_font)\
  291.  m(4,line_params.dash.pattern) m(5,text.font) m(6,page_ids) m(7,annots)\
  292.  m(8,last_resource) m(9,articles) m(10,named_dests)
  293. #define gx_device_pdf_num_ptrs 11 /* + num_resource_types */
  294. #define gx_device_pdf_do_strings(m)\
  295.  m(0,catalog_string) m(1,pages_string) m(2,page_string)
  296. #define gx_device_pdf_num_strings 3 /* + max_outline_depth * 2 */
  297. #define st_device_pdf_max_ptrs\
  298.   (st_device_psdf_max_ptrs + gx_device_pdf_num_ptrs +\
  299.    gx_device_pdf_num_strings + num_resource_types * num_resource_chains +\
  300.    max_outline_depth * 2)
  301.  
  302. #define private_st_device_pdfwrite()    /* in gdevpdf.c */\
  303.   gs_private_st_composite_final(st_device_pdfwrite, gx_device_pdf,\
  304.     "gx_device_pdf", device_pdfwrite_enum_ptrs, device_pdfwrite_reloc_ptrs,\
  305.     device_pdfwrite_finalize)
  306.  
  307. /* ================ Utility procedures ================ */
  308.  
  309. /* ---------------- Exported by gdevpdf.c ---------------- */
  310.  
  311. /* ------ Document ------ */
  312.  
  313. /* Initialize the IDs allocated at startup. */
  314. void pdf_initialize_ids(P1(gx_device_pdf *pdev));
  315.  
  316. /* Open the document if necessary. */
  317. void pdf_open_document(P1(gx_device_pdf *pdev));
  318.  
  319. /* ------ Objects ------ */
  320.  
  321. /* Allocate an ID for a future object. */
  322. long pdf_obj_ref(P1(gx_device_pdf *pdev));
  323.  
  324. /* Read the current position in the output stream. */
  325. long pdf_stell(P1(gx_device_pdf *pdev));
  326.  
  327. /* Begin an object, optionally allocating an ID. */
  328. long pdf_open_obj(P2(gx_device_pdf *pdev, long id));
  329.  
  330. /* Begin an object, allocating an ID. */
  331. #define pdf_begin_obj(pdev) pdf_open_obj(pdev, 0)
  332.  
  333. /* End an object. */
  334. int pdf_end_obj(P1(gx_device_pdf *pdev));
  335.  
  336. /* ------ Graphics ------ */
  337.  
  338. /* Reset the graphics state parameters to initial values. */
  339. void pdf_reset_graphics(P1(gx_device_pdf *pdev));
  340.  
  341. /* Set the fill or stroke color. */
  342. int pdf_set_color(P4(gx_device_pdf *pdev, gx_color_index color,
  343.              gx_drawing_color *pdcolor, const char *rgs));
  344.  
  345. /* Write matrix values. */
  346. void pdf_put_matrix(P4(gx_device_pdf *pdev, const char *before,
  347.                const gs_matrix *pmat, const char *after));
  348.  
  349. /* Write a string in its shortest form ( () or <> ). */
  350. void pdf_put_string(P3(gx_device_pdf *pdev, const byte *str, uint size));
  351.  
  352. /* ------ Page contents ------ */
  353.  
  354. /* Open a page contents part. */
  355. /* Return an error if the page has too many contents parts. */
  356. int pdf_open_contents(P2(gx_device_pdf *pdev, pdf_context context));
  357.  
  358. /* Close the current contents part if we are in one. */
  359. int pdf_close_contents(P2(gx_device_pdf *pdev, bool last));
  360.  
  361. /* ------ Resources et al ------ */
  362.  
  363. /* Begin an object logically separate from the contents. */
  364. /* (I.e., an object in the resource file.) */
  365. long pdf_open_separate(P2(gx_device_pdf *pdev, long id));
  366. #define pdf_begin_separate(pdev) pdf_open_separate(pdev, 0L)
  367.  
  368. /* Begin an aside (resource, annotation, ...). */
  369. int pdf_begin_aside(P4(gx_device_pdf *pdev, pdf_resource **plist,
  370.                const gs_memory_struct_type_t *pst,
  371.                pdf_resource **ppres));
  372.  
  373. /* Begin a resource of a given type. */
  374. int pdf_begin_resource(P4(gx_device_pdf *pdev, pdf_resource_type type,
  375.               gs_id rid, pdf_resource **ppres));
  376.  
  377. /* Allocate a resource, but don't open the stream. */
  378. int pdf_alloc_resource(P4(gx_device_pdf *pdev, pdf_resource_type type,
  379.               gs_id rid, pdf_resource **ppres));
  380.  
  381. /* Find a resource of a given type by gs_id. */
  382. pdf_resource *pdf_find_resource_by_gs_id(P3(gx_device_pdf *pdev,
  383.                         pdf_resource_type type,
  384.                         gs_id rid));
  385.  
  386. /* End a separate object. */
  387. #define pdf_end_separate(pdev) pdf_end_aside(pdev)
  388.  
  389. /* End an aside. */
  390. int pdf_end_aside(P1(gx_device_pdf *pdev));
  391.  
  392. /* End a resource. */
  393. int pdf_end_resource(P1(gx_device_pdf *pdev));
  394.  
  395. /* ------ Pages ------ */
  396.  
  397. /* Get or assign the ID for a page. */
  398. /* Returns 0 if the page number is out of range. */
  399. long pdf_page_id(P2(gx_device_pdf *pdev, int page_num));
  400.  
  401. /* Open a page for writing. */
  402. int pdf_open_page(P2(gx_device_pdf *pdev, pdf_context context));
  403.  
  404. /* Write saved page- or document-level information. */
  405. int pdf_write_saved_string(P2(gx_device_pdf *pdev, gs_string *pstr));
  406.  
  407. /* Write the default entries of the Info dictionary. */
  408. int pdf_write_default_info(P1(gx_device_pdf *pdev));
  409.  
  410. /* ------ Path drawing ------ */
  411.  
  412. bool pdf_must_put_clip_path(P2(gx_device_pdf *pdev, const gx_clip_path *pcpath));
  413.  
  414. int pdf_put_clip_path(P2(gx_device_pdf *pdev, const gx_clip_path *pcpath));
  415.  
  416. /* ---------------- Exported by gdevpdfm.c ---------------- */
  417.  
  418. /* Compare a C string and a gs_param_string. */
  419. bool pdf_key_eq(P2(const gs_param_string *pcs, const char *str));
  420.  
  421. /* Process a pdfmark (called from pdf_put_params). */
  422. int pdfmark_process(P2(gx_device_pdf *pdev, const gs_param_string_array *pma));
  423.  
  424. /* Close the current level of the outline tree. */
  425. int pdfmark_close_outline(P1(gx_device_pdf *pdev));
  426.  
  427. /* Write an article bead. */
  428. int pdfmark_write_article(P2(gx_device_pdf *pdev, const pdf_bead *pbead));
  429.  
  430. /* ---------------- Exported by gdevpdft.c ---------------- */
  431.  
  432. /* Process a show operation (called from pdf_put_params). */
  433. int pdfshow_process(P2(gx_device_pdf *pdev, const gs_param_dict *ptd));
  434.  
  435. /* Begin a CharProc for an embedded (bitmap) font. */
  436. int pdf_begin_char_proc(P8(gx_device_pdf *pdev, int w, int h, int x_width,
  437.                int y_offset, gs_id id, pdf_char_proc **ppcp,
  438.                pdf_stream_position *ppos));
  439.  
  440. /* End a CharProc. */
  441. int pdf_end_char_proc(P2(gx_device_pdf *pdev, pdf_stream_position *ppos));
  442.  
  443. /* Put out a reference to an image as a character in an embedded font. */
  444. int pdf_do_char_image(P3(gx_device_pdf *pdev, const pdf_char_proc *pcp,
  445.              const gs_matrix *pimat));
  446.