pdf.c

Thu, 13 Mar 2003 08:57:05 +0000

author
eric
date
Thu, 13 Mar 2003 08:57:05 +0000
changeset 125
e2ef1c2f9eca
parent 124
ba64dfca82e9
child 128
3401fe143d49
permissions
-rw-r--r--

changed program name from t2p to tumble.

     1 /*
     2  * tumble: build a PDF file from image files
     3  *
     4  * PDF routines
     5  * $Id: pdf.c,v 1.10 2003/03/13 00:57:05 eric Exp $
     6  * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License version 2 as
    10  * published by the Free Software Foundation.  Note that permission is
    11  * not granted to redistribute this program under the terms of any
    12  * other version of the General Public License.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  *
    19  * You should have received a copy of the GNU General Public License
    20  * along with this program; if not, write to the Free Software
    21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
    22  */
    25 #include <stdbool.h>
    26 #include <stdint.h>
    27 #include <stdio.h>
    28 #include <stdlib.h>
    31 #include "bitblt.h"
    32 #include "pdf.h"
    33 #include "pdf_util.h"
    34 #include "pdf_prim.h"
    35 #include "pdf_private.h"
    36 #include "pdf_name_tree.h"
    39 static void pdf_set_info (pdf_file_handle pdf_file, char *key, char *val)
    40 {
    41   if (! pdf_file->info)
    42     pdf_file->info = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    44   pdf_set_dict_entry (pdf_file->info, key, pdf_new_string (val));
    45 }
    48 void pdf_init (void)
    49 {
    50 }
    53 struct pdf_pages *pdf_new_pages (pdf_file_handle pdf_file)
    54 {
    55   struct pdf_pages *pages = pdf_calloc (1, sizeof (struct pdf_pages));
    57   pages->kids = pdf_new_obj (PT_ARRAY);
    58   /* The PDF 1.0 spec doesn't say that kids can't be an indirect object,
    59      but Acrobat 4.0 fails to optimize files if it is. */
    61   pages->count = pdf_new_integer (0);
    62   pages->pages_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    63   pdf_set_dict_entry (pages->pages_dict, "Type", pdf_new_name ("Pages"));
    64   pdf_set_dict_entry (pages->pages_dict, "Kids", pages->kids);
    65   pdf_set_dict_entry (pages->pages_dict, "Count", pages->count);
    66   return (pages);
    67 }
    70 pdf_file_handle pdf_create (char *filename, int page_mode)
    71 {
    72   pdf_file_handle pdf_file;
    73   char *page_mode_string;
    75   switch (page_mode)
    76     {
    77     case PDF_PAGE_MODE_USE_NONE:      page_mode_string = "UseNone";     break;
    78     case PDF_PAGE_MODE_USE_OUTLINES:  page_mode_string = "UseOutlines"; break;
    79     case PDF_PAGE_MODE_USE_THUMBS:    page_mode_string = "UseThumbs";   break;
    80     default:
    81       pdf_fatal ("invalid page mode\n");
    82     }
    84   pdf_file = pdf_calloc (1, sizeof (struct pdf_file));
    86   pdf_file->f = fopen (filename, "wb");
    87   if (! pdf_file->f)
    88     {
    89       pdf_fatal ("error opening output file\n");
    90     }
    92   pdf_file->root = pdf_new_pages (pdf_file);
    94   pdf_file->catalog = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    95   pdf_set_dict_entry (pdf_file->catalog, "Type", pdf_new_name ("Catalog"));
    96   pdf_set_dict_entry (pdf_file->catalog, "Pages", pdf_file->root->pages_dict);
    97   /* Outlines dictionary will be created later if needed */
    98   pdf_set_dict_entry (pdf_file->catalog,
    99 		      "PageMode",
   100 		      pdf_new_name (page_mode_string));
   102   pdf_file->info    = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
   103   pdf_set_info (pdf_file, "Producer", "tumble by Eric Smith <eric@brouhaha.com>");
   105   pdf_file->trailer_dict = pdf_new_obj (PT_DICTIONARY);
   106   /* Size key will be added later */
   107   pdf_set_dict_entry (pdf_file->trailer_dict, "Root", pdf_file->catalog);
   108   pdf_set_dict_entry (pdf_file->trailer_dict, "Info", pdf_file->info);
   110   /* write file header */
   111   fprintf (pdf_file->f, "%%PDF-1.2\r\n");
   113   return (pdf_file);
   114 }
   117 void pdf_close (pdf_file_handle pdf_file)
   118 {
   119   /* finalize all data structures */
   120   pdf_finalize_name_trees (pdf_file);
   122   /* write body */
   123   pdf_write_all_ind_obj (pdf_file);
   125   /* write cross reference table and get maximum object number */
   126   pdf_set_dict_entry (pdf_file->trailer_dict, "Size", pdf_new_integer (pdf_write_xref (pdf_file)));
   128   /* write trailer */
   129   fprintf (pdf_file->f, "trailer\r\n");
   130   pdf_write_obj (pdf_file, pdf_file->trailer_dict);
   131   fprintf (pdf_file->f, "startxref\r\n");
   132   fprintf (pdf_file->f, "%ld\r\n", pdf_file->xref_offset);
   133   fprintf (pdf_file->f, "%%%%EOF\r\n");
   135   fclose (pdf_file->f);
   136   /* should free stuff here */
   137 }
   140 void pdf_set_author   (pdf_file_handle pdf_file, char *author)
   141 {
   142   pdf_set_info (pdf_file, "Author", author);
   143 }
   145 void pdf_set_creator  (pdf_file_handle pdf_file, char *creator)
   146 {
   147   pdf_set_info (pdf_file, "Creator", creator);
   148 }
   150 void pdf_set_producer  (pdf_file_handle pdf_file, char *producer)
   151 {
   152   pdf_set_info (pdf_file, "Producer", producer);
   153 }
   155 void pdf_set_title    (pdf_file_handle pdf_file, char *title)
   156 {
   157   pdf_set_info (pdf_file, "Title", title);
   158 }
   160 void pdf_set_subject  (pdf_file_handle pdf_file, char *subject)
   161 {
   162   pdf_set_info (pdf_file, "Subject", subject);
   163 }
   165 void pdf_set_keywords (pdf_file_handle pdf_file, char *keywords)
   166 {
   167   pdf_set_info (pdf_file, "Keywords", keywords);
   168 }
   171 pdf_page_handle pdf_new_page (pdf_file_handle pdf_file,
   172 			      double width,
   173 			      double height)
   174 {
   175   pdf_page_handle page = pdf_calloc (1, sizeof (struct pdf_page));
   177   page->pdf_file = pdf_file;
   179   page->media_box = pdf_new_obj (PT_ARRAY);
   180   pdf_add_array_elem (page->media_box, pdf_new_real (0));
   181   pdf_add_array_elem (page->media_box, pdf_new_real (0));
   182   pdf_add_array_elem (page->media_box, pdf_new_real (width));
   183   pdf_add_array_elem (page->media_box, pdf_new_real (height));
   185   page->procset = pdf_new_obj (PT_ARRAY);
   186   pdf_add_array_elem_unique (page->procset, pdf_new_name ("PDF"));
   188   page->resources = pdf_new_obj (PT_DICTIONARY);
   189   pdf_set_dict_entry (page->resources, "ProcSet", page->procset);
   191   page->page_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
   192   pdf_set_dict_entry (page->page_dict, "Type", pdf_new_name ("Page"));
   193   pdf_set_dict_entry (page->page_dict, "MediaBox", page->media_box);
   194   pdf_set_dict_entry (page->page_dict, "Resources", page->resources);
   196   /* $$$ currently only support a single-level pages tree */
   197   pdf_set_dict_entry (page->page_dict, "Parent", pdf_file->root->pages_dict);
   198   pdf_add_array_elem (pdf_file->root->kids, page->page_dict);
   199   pdf_set_integer (pdf_file->root->count,
   200 		   pdf_get_integer (pdf_file->root->count) + 1);
   202   page->last_XObject_name = '@';  /* first name will be "ImA" */
   204   return (page);
   205 }
   207 void pdf_close_page (pdf_page_handle pdf_page)
   208 {
   209 }
   212 void pdf_set_page_number (pdf_page_handle pdf_page, char *page_number)
   213 {
   214 }
   216 void pdf_bookmark (pdf_page_handle pdf_page, int level, char *name)
   217 {
   218 }