pdf.c

changeset 59
e8821eb2fb08
child 60
ffb5b1e54eb2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/pdf.c	Thu Feb 20 12:16:00 2003 +0000
     1.3 @@ -0,0 +1,171 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +
     1.7 +
     1.8 +#include "libpdf.h"
     1.9 +#include "libpdf_util.h"
    1.10 +#include "libpdf_prim.h"
    1.11 +#include "libpdf_private.h"
    1.12 +
    1.13 +
    1.14 +static void pdf_set_info (pdf_file_handle pdf_file, char *key, char *val)
    1.15 +{
    1.16 +  if (! pdf_file->info)
    1.17 +    pdf_file->info = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    1.18 +
    1.19 +  pdf_set_dict_entry (pdf_file->info, key, pdf_new_string (val));
    1.20 +}
    1.21 +
    1.22 +
    1.23 +void pdf_init (void)
    1.24 +{
    1.25 +}
    1.26 +
    1.27 +
    1.28 +struct pdf_pages *pdf_new_pages (pdf_file_handle pdf_file)
    1.29 +{
    1.30 +  struct pdf_pages *pages = pdf_calloc (sizeof (struct pdf_pages));
    1.31 +  pages->kids = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_ARRAY));
    1.32 +  pages->count = pdf_new_integer (0);
    1.33 +  pages->pages_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    1.34 +  pdf_set_dict_entry (pages->pages_dict, "Type", pdf_new_name ("Pages"));
    1.35 +  pdf_set_dict_entry (pages->pages_dict, "Kids", pages->kids);
    1.36 +  pdf_set_dict_entry (pages->pages_dict, "Count", pages->count);
    1.37 +  return (pages);
    1.38 +}
    1.39 +
    1.40 +
    1.41 +pdf_file_handle pdf_create (char *filename)
    1.42 +{
    1.43 +  pdf_file_handle pdf_file;
    1.44 +
    1.45 +  pdf_file = pdf_calloc (sizeof (struct pdf_file));
    1.46 +
    1.47 +  pdf_file->f = fopen (filename, "wb");
    1.48 +  if (! pdf_file->f)
    1.49 +    {
    1.50 +      pdf_fatal ("error opening output file\n");
    1.51 +    }
    1.52 +
    1.53 +  pdf_file->root = pdf_new_pages (pdf_file);
    1.54 +
    1.55 +  pdf_file->catalog = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    1.56 +  pdf_set_dict_entry (pdf_file->catalog, "Type", pdf_new_name ("Catalog"));
    1.57 +  pdf_set_dict_entry (pdf_file->catalog, "Pages", pdf_file->root->pages_dict);
    1.58 +  /* Outlines dictionary will be created later if needed*/
    1.59 +  pdf_set_dict_entry (pdf_file->catalog, "PageMode", pdf_new_name ("UseNone"));
    1.60 +
    1.61 +  pdf_file->info    = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    1.62 +  pdf_set_info (pdf_file, "Producer", "libpdf, Copyright 2003 Eric Smith <eric@brouhaha.com>");
    1.63 +
    1.64 +  pdf_file->trailer_dict = pdf_new_obj (PT_DICTIONARY);
    1.65 +  /* Size key will be added later */
    1.66 +  pdf_set_dict_entry (pdf_file->trailer_dict, "Root", pdf_file->catalog);
    1.67 +  pdf_set_dict_entry (pdf_file->trailer_dict, "Info", pdf_file->info);
    1.68 +
    1.69 +  /* write file header */
    1.70 +  fprintf (pdf_file->f, "%%PDF-1.2\r\n");
    1.71 +
    1.72 +  return (pdf_file);
    1.73 +}
    1.74 +
    1.75 +
    1.76 +void pdf_close (pdf_file_handle pdf_file)
    1.77 +{
    1.78 +  /* write body */
    1.79 +  pdf_write_all_ind_obj (pdf_file);
    1.80 +
    1.81 +  /* write cross reference table and get maximum object number */
    1.82 +  pdf_set_dict_entry (pdf_file->trailer_dict, "Size", pdf_new_integer (pdf_write_xref (pdf_file)));
    1.83 +
    1.84 +  /* write trailer */
    1.85 +  fprintf (pdf_file->f, "trailer\r\n");
    1.86 +  pdf_write_obj (pdf_file, pdf_file->trailer_dict);
    1.87 +  fprintf (pdf_file->f, "startxref\r\n");
    1.88 +  fprintf (pdf_file->f, "%ld\r\n", pdf_file->xref_offset);
    1.89 +  fprintf (pdf_file->f, "%%%%EOF\r\n");
    1.90 +
    1.91 +  fclose (pdf_file->f);
    1.92 +  /* should free stuff here */
    1.93 +}
    1.94 +
    1.95 +
    1.96 +void pdf_set_author   (pdf_file_handle pdf_file, char *author)
    1.97 +{
    1.98 +  pdf_set_info (pdf_file, "Author", author);
    1.99 +}
   1.100 +
   1.101 +void pdf_set_creator  (pdf_file_handle pdf_file, char *creator)
   1.102 +{
   1.103 +  pdf_set_info (pdf_file, "Creator", creator);
   1.104 +}
   1.105 +
   1.106 +void pdf_set_producer  (pdf_file_handle pdf_file, char *producer)
   1.107 +{
   1.108 +  pdf_set_info (pdf_file, "Producer", producer);
   1.109 +}
   1.110 +
   1.111 +void pdf_set_title    (pdf_file_handle pdf_file, char *title)
   1.112 +{
   1.113 +  pdf_set_info (pdf_file, "Title", title);
   1.114 +}
   1.115 +
   1.116 +void pdf_set_subject  (pdf_file_handle pdf_file, char *subject)
   1.117 +{
   1.118 +  pdf_set_info (pdf_file, "Subject", subject);
   1.119 +}
   1.120 +
   1.121 +void pdf_set_keywords (pdf_file_handle pdf_file, char *keywords)
   1.122 +{
   1.123 +  pdf_set_info (pdf_file, "Keywords", keywords);
   1.124 +}
   1.125 +
   1.126 +
   1.127 +pdf_page_handle pdf_new_page (pdf_file_handle pdf_file,
   1.128 +			      double width,
   1.129 +			      double height)
   1.130 +{
   1.131 +  pdf_page_handle page = pdf_calloc (sizeof (struct pdf_page));
   1.132 +
   1.133 +  page->pdf_file = pdf_file;
   1.134 +
   1.135 +  page->media_box = pdf_new_obj (PT_ARRAY);
   1.136 +  pdf_add_array_elem (page->media_box, pdf_new_real (0));
   1.137 +  pdf_add_array_elem (page->media_box, pdf_new_real (0));
   1.138 +  pdf_add_array_elem (page->media_box, pdf_new_real (width));
   1.139 +  pdf_add_array_elem (page->media_box, pdf_new_real (height));
   1.140 +
   1.141 +  page->procset = pdf_new_obj (PT_ARRAY);
   1.142 +  pdf_add_array_elem (page->procset, pdf_new_name ("PDF"));
   1.143 +
   1.144 +  page->resources = pdf_new_obj (PT_DICTIONARY);
   1.145 +  pdf_set_dict_entry (page->resources, "ProcSet", page->procset);
   1.146 +
   1.147 +  page->page_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
   1.148 +  pdf_set_dict_entry (page->page_dict, "Type", pdf_new_name ("Page"));
   1.149 +  pdf_set_dict_entry (page->page_dict, "MediaBox", page->media_box);
   1.150 +  pdf_set_dict_entry (page->page_dict, "Resources", page->resources);
   1.151 +
   1.152 +  /* $$$ currently only support a single-level pages tree */
   1.153 +  pdf_set_dict_entry (page->page_dict, "Parent", pdf_file->root->pages_dict);
   1.154 +  pdf_add_array_elem (pdf_file->root->kids, page->page_dict);
   1.155 +  pdf_set_integer (pdf_file->root->count,
   1.156 +		   pdf_get_integer (pdf_file->root->count) + 1);
   1.157 +
   1.158 +  page->last_XObject_name = '@';  /* first name will be "ImA" */
   1.159 +
   1.160 +  return (page);
   1.161 +}
   1.162 +
   1.163 +void pdf_close_page (pdf_page_handle pdf_page)
   1.164 +{
   1.165 +}
   1.166 +
   1.167 +
   1.168 +void pdf_set_page_number (pdf_page_handle pdf_page, char *page_number)
   1.169 +{
   1.170 +}
   1.171 +
   1.172 +void pdf_bookmark (pdf_page_handle pdf_page, int level, char *name)
   1.173 +{
   1.174 +}