pdf.c

Thu, 20 Feb 2003 12:20:28 +0000

author
eric
date
Thu, 20 Feb 2003 12:20:28 +0000
changeset 60
ffb5b1e54eb2
parent 59
e8821eb2fb08
child 62
9bd354b83e16
permissions
-rw-r--r--

fix includes.

     1 #include <stdio.h>
     2 #include <stdlib.h>
     5 #include "pdf.h"
     6 #include "pdf_util.h"
     7 #include "pdf_prim.h"
     8 #include "pdf_private.h"
    11 static void pdf_set_info (pdf_file_handle pdf_file, char *key, char *val)
    12 {
    13   if (! pdf_file->info)
    14     pdf_file->info = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    16   pdf_set_dict_entry (pdf_file->info, key, pdf_new_string (val));
    17 }
    20 void pdf_init (void)
    21 {
    22 }
    25 struct pdf_pages *pdf_new_pages (pdf_file_handle pdf_file)
    26 {
    27   struct pdf_pages *pages = pdf_calloc (sizeof (struct pdf_pages));
    28   pages->kids = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_ARRAY));
    29   pages->count = pdf_new_integer (0);
    30   pages->pages_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    31   pdf_set_dict_entry (pages->pages_dict, "Type", pdf_new_name ("Pages"));
    32   pdf_set_dict_entry (pages->pages_dict, "Kids", pages->kids);
    33   pdf_set_dict_entry (pages->pages_dict, "Count", pages->count);
    34   return (pages);
    35 }
    38 pdf_file_handle pdf_create (char *filename)
    39 {
    40   pdf_file_handle pdf_file;
    42   pdf_file = pdf_calloc (sizeof (struct pdf_file));
    44   pdf_file->f = fopen (filename, "wb");
    45   if (! pdf_file->f)
    46     {
    47       pdf_fatal ("error opening output file\n");
    48     }
    50   pdf_file->root = pdf_new_pages (pdf_file);
    52   pdf_file->catalog = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    53   pdf_set_dict_entry (pdf_file->catalog, "Type", pdf_new_name ("Catalog"));
    54   pdf_set_dict_entry (pdf_file->catalog, "Pages", pdf_file->root->pages_dict);
    55   /* Outlines dictionary will be created later if needed*/
    56   pdf_set_dict_entry (pdf_file->catalog, "PageMode", pdf_new_name ("UseNone"));
    58   pdf_file->info    = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    59   pdf_set_info (pdf_file, "Producer", "t2p, Copyright 2003 Eric Smith <eric@brouhaha.com>");
    61   pdf_file->trailer_dict = pdf_new_obj (PT_DICTIONARY);
    62   /* Size key will be added later */
    63   pdf_set_dict_entry (pdf_file->trailer_dict, "Root", pdf_file->catalog);
    64   pdf_set_dict_entry (pdf_file->trailer_dict, "Info", pdf_file->info);
    66   /* write file header */
    67   fprintf (pdf_file->f, "%%PDF-1.2\r\n");
    69   return (pdf_file);
    70 }
    73 void pdf_close (pdf_file_handle pdf_file)
    74 {
    75   /* write body */
    76   pdf_write_all_ind_obj (pdf_file);
    78   /* write cross reference table and get maximum object number */
    79   pdf_set_dict_entry (pdf_file->trailer_dict, "Size", pdf_new_integer (pdf_write_xref (pdf_file)));
    81   /* write trailer */
    82   fprintf (pdf_file->f, "trailer\r\n");
    83   pdf_write_obj (pdf_file, pdf_file->trailer_dict);
    84   fprintf (pdf_file->f, "startxref\r\n");
    85   fprintf (pdf_file->f, "%ld\r\n", pdf_file->xref_offset);
    86   fprintf (pdf_file->f, "%%%%EOF\r\n");
    88   fclose (pdf_file->f);
    89   /* should free stuff here */
    90 }
    93 void pdf_set_author   (pdf_file_handle pdf_file, char *author)
    94 {
    95   pdf_set_info (pdf_file, "Author", author);
    96 }
    98 void pdf_set_creator  (pdf_file_handle pdf_file, char *creator)
    99 {
   100   pdf_set_info (pdf_file, "Creator", creator);
   101 }
   103 void pdf_set_producer  (pdf_file_handle pdf_file, char *producer)
   104 {
   105   pdf_set_info (pdf_file, "Producer", producer);
   106 }
   108 void pdf_set_title    (pdf_file_handle pdf_file, char *title)
   109 {
   110   pdf_set_info (pdf_file, "Title", title);
   111 }
   113 void pdf_set_subject  (pdf_file_handle pdf_file, char *subject)
   114 {
   115   pdf_set_info (pdf_file, "Subject", subject);
   116 }
   118 void pdf_set_keywords (pdf_file_handle pdf_file, char *keywords)
   119 {
   120   pdf_set_info (pdf_file, "Keywords", keywords);
   121 }
   124 pdf_page_handle pdf_new_page (pdf_file_handle pdf_file,
   125 			      double width,
   126 			      double height)
   127 {
   128   pdf_page_handle page = pdf_calloc (sizeof (struct pdf_page));
   130   page->pdf_file = pdf_file;
   132   page->media_box = pdf_new_obj (PT_ARRAY);
   133   pdf_add_array_elem (page->media_box, pdf_new_real (0));
   134   pdf_add_array_elem (page->media_box, pdf_new_real (0));
   135   pdf_add_array_elem (page->media_box, pdf_new_real (width));
   136   pdf_add_array_elem (page->media_box, pdf_new_real (height));
   138   page->procset = pdf_new_obj (PT_ARRAY);
   139   pdf_add_array_elem (page->procset, pdf_new_name ("PDF"));
   141   page->resources = pdf_new_obj (PT_DICTIONARY);
   142   pdf_set_dict_entry (page->resources, "ProcSet", page->procset);
   144   page->page_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
   145   pdf_set_dict_entry (page->page_dict, "Type", pdf_new_name ("Page"));
   146   pdf_set_dict_entry (page->page_dict, "MediaBox", page->media_box);
   147   pdf_set_dict_entry (page->page_dict, "Resources", page->resources);
   149   /* $$$ currently only support a single-level pages tree */
   150   pdf_set_dict_entry (page->page_dict, "Parent", pdf_file->root->pages_dict);
   151   pdf_add_array_elem (pdf_file->root->kids, page->page_dict);
   152   pdf_set_integer (pdf_file->root->count,
   153 		   pdf_get_integer (pdf_file->root->count) + 1);
   155   page->last_XObject_name = '@';  /* first name will be "ImA" */
   157   return (page);
   158 }
   160 void pdf_close_page (pdf_page_handle pdf_page)
   161 {
   162 }
   165 void pdf_set_page_number (pdf_page_handle pdf_page, char *page_number)
   166 {
   167 }
   169 void pdf_bookmark (pdf_page_handle pdf_page, int level, char *name)
   170 {
   171 }