pdf.c

Fri, 21 Feb 2003 08:49:11 +0000

author
eric
date
Fri, 21 Feb 2003 08:49:11 +0000
changeset 63
6eddf63aa517
parent 62
9bd354b83e16
child 67
7add7411c1c2
permissions
-rw-r--r--

GPL.

     1 /*
     2  * t2p: Create a PDF file from the contents of one or more TIFF
     3  *      bilevel image files.  The images in the resulting PDF file
     4  *      will be compressed using ITU-T T.6 (G4) fax encoding.
     5  *
     6  * PDF routines
     7  * $Id: pdf.c,v 1.3 2003/02/20 04:44:17 eric Exp $
     8  * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
     9  *
    10  * This program is free software; you can redistribute it and/or modify
    11  * it under the terms of the GNU General Public License version 2 as
    12  * published by the Free Software Foundation.  Note that permission is
    13  * not granted to redistribute this program under the terms of any
    14  * other version of the General Public License.
    15  *
    16  * This program is distributed in the hope that it will be useful,
    17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19  * GNU General Public License for more details.
    20  *
    21  * You should have received a copy of the GNU General Public License
    22  * along with this program; if not, write to the Free Software
    23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
    24  */
    27 #include <stdbool.h>
    28 #include <stdint.h>
    29 #include <stdio.h>
    30 #include <stdlib.h>
    33 #include "bitblt.h"
    34 #include "pdf.h"
    35 #include "pdf_util.h"
    36 #include "pdf_prim.h"
    37 #include "pdf_private.h"
    40 static void pdf_set_info (pdf_file_handle pdf_file, char *key, char *val)
    41 {
    42   if (! pdf_file->info)
    43     pdf_file->info = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    45   pdf_set_dict_entry (pdf_file->info, key, pdf_new_string (val));
    46 }
    49 void pdf_init (void)
    50 {
    51 }
    54 struct pdf_pages *pdf_new_pages (pdf_file_handle pdf_file)
    55 {
    56   struct pdf_pages *pages = pdf_calloc (sizeof (struct pdf_pages));
    57   pages->kids = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_ARRAY));
    58   pages->count = pdf_new_integer (0);
    59   pages->pages_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    60   pdf_set_dict_entry (pages->pages_dict, "Type", pdf_new_name ("Pages"));
    61   pdf_set_dict_entry (pages->pages_dict, "Kids", pages->kids);
    62   pdf_set_dict_entry (pages->pages_dict, "Count", pages->count);
    63   return (pages);
    64 }
    67 pdf_file_handle pdf_create (char *filename)
    68 {
    69   pdf_file_handle pdf_file;
    71   pdf_file = pdf_calloc (sizeof (struct pdf_file));
    73   pdf_file->f = fopen (filename, "wb");
    74   if (! pdf_file->f)
    75     {
    76       pdf_fatal ("error opening output file\n");
    77     }
    79   pdf_file->root = pdf_new_pages (pdf_file);
    81   pdf_file->catalog = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    82   pdf_set_dict_entry (pdf_file->catalog, "Type", pdf_new_name ("Catalog"));
    83   pdf_set_dict_entry (pdf_file->catalog, "Pages", pdf_file->root->pages_dict);
    84   /* Outlines dictionary will be created later if needed*/
    85   pdf_set_dict_entry (pdf_file->catalog, "PageMode", pdf_new_name ("UseNone"));
    87   pdf_file->info    = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    88   pdf_set_info (pdf_file, "Producer", "t2p, Copyright 2003 Eric Smith <eric@brouhaha.com>");
    90   pdf_file->trailer_dict = pdf_new_obj (PT_DICTIONARY);
    91   /* Size key will be added later */
    92   pdf_set_dict_entry (pdf_file->trailer_dict, "Root", pdf_file->catalog);
    93   pdf_set_dict_entry (pdf_file->trailer_dict, "Info", pdf_file->info);
    95   /* write file header */
    96   fprintf (pdf_file->f, "%%PDF-1.2\r\n");
    98   return (pdf_file);
    99 }
   102 void pdf_close (pdf_file_handle pdf_file)
   103 {
   104   /* write body */
   105   pdf_write_all_ind_obj (pdf_file);
   107   /* write cross reference table and get maximum object number */
   108   pdf_set_dict_entry (pdf_file->trailer_dict, "Size", pdf_new_integer (pdf_write_xref (pdf_file)));
   110   /* write trailer */
   111   fprintf (pdf_file->f, "trailer\r\n");
   112   pdf_write_obj (pdf_file, pdf_file->trailer_dict);
   113   fprintf (pdf_file->f, "startxref\r\n");
   114   fprintf (pdf_file->f, "%ld\r\n", pdf_file->xref_offset);
   115   fprintf (pdf_file->f, "%%%%EOF\r\n");
   117   fclose (pdf_file->f);
   118   /* should free stuff here */
   119 }
   122 void pdf_set_author   (pdf_file_handle pdf_file, char *author)
   123 {
   124   pdf_set_info (pdf_file, "Author", author);
   125 }
   127 void pdf_set_creator  (pdf_file_handle pdf_file, char *creator)
   128 {
   129   pdf_set_info (pdf_file, "Creator", creator);
   130 }
   132 void pdf_set_producer  (pdf_file_handle pdf_file, char *producer)
   133 {
   134   pdf_set_info (pdf_file, "Producer", producer);
   135 }
   137 void pdf_set_title    (pdf_file_handle pdf_file, char *title)
   138 {
   139   pdf_set_info (pdf_file, "Title", title);
   140 }
   142 void pdf_set_subject  (pdf_file_handle pdf_file, char *subject)
   143 {
   144   pdf_set_info (pdf_file, "Subject", subject);
   145 }
   147 void pdf_set_keywords (pdf_file_handle pdf_file, char *keywords)
   148 {
   149   pdf_set_info (pdf_file, "Keywords", keywords);
   150 }
   153 pdf_page_handle pdf_new_page (pdf_file_handle pdf_file,
   154 			      double width,
   155 			      double height)
   156 {
   157   pdf_page_handle page = pdf_calloc (sizeof (struct pdf_page));
   159   page->pdf_file = pdf_file;
   161   page->media_box = pdf_new_obj (PT_ARRAY);
   162   pdf_add_array_elem (page->media_box, pdf_new_real (0));
   163   pdf_add_array_elem (page->media_box, pdf_new_real (0));
   164   pdf_add_array_elem (page->media_box, pdf_new_real (width));
   165   pdf_add_array_elem (page->media_box, pdf_new_real (height));
   167   page->procset = pdf_new_obj (PT_ARRAY);
   168   pdf_add_array_elem (page->procset, pdf_new_name ("PDF"));
   170   page->resources = pdf_new_obj (PT_DICTIONARY);
   171   pdf_set_dict_entry (page->resources, "ProcSet", page->procset);
   173   page->page_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
   174   pdf_set_dict_entry (page->page_dict, "Type", pdf_new_name ("Page"));
   175   pdf_set_dict_entry (page->page_dict, "MediaBox", page->media_box);
   176   pdf_set_dict_entry (page->page_dict, "Resources", page->resources);
   178   /* $$$ currently only support a single-level pages tree */
   179   pdf_set_dict_entry (page->page_dict, "Parent", pdf_file->root->pages_dict);
   180   pdf_add_array_elem (pdf_file->root->kids, page->page_dict);
   181   pdf_set_integer (pdf_file->root->count,
   182 		   pdf_get_integer (pdf_file->root->count) + 1);
   184   page->last_XObject_name = '@';  /* first name will be "ImA" */
   186   return (page);
   187 }
   189 void pdf_close_page (pdf_page_handle pdf_page)
   190 {
   191 }
   194 void pdf_set_page_number (pdf_page_handle pdf_page, char *page_number)
   195 {
   196 }
   198 void pdf_bookmark (pdf_page_handle pdf_page, int level, char *name)
   199 {
   200 }