pdf.c

Fri, 14 Mar 2003 08:57:40 +0000

author
eric
date
Fri, 14 Mar 2003 08:57:40 +0000
changeset 133
76c197fe2eeb
parent 131
4b8c80d77f76
permissions
-rw-r--r--

specify page mode when file is closed rather than when it is initially created. if USE_OUTLINES but no bookmarks, use USE_NONE instead.

     1 /*
     2  * tumble: build a PDF file from image files
     3  *
     4  * PDF routines
     5  * $Id: pdf.c,v 1.13 2003/03/14 00:57:40 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)
    71 {
    72   pdf_file_handle pdf_file;
    74   pdf_file = pdf_calloc (1, sizeof (struct pdf_file));
    76   pdf_file->f = fopen (filename, "wb");
    77   if (! pdf_file->f)
    78     {
    79       pdf_fatal ("error opening output file\n");
    80     }
    82   pdf_file->root = pdf_new_pages (pdf_file);
    84   pdf_file->catalog = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    85   pdf_set_dict_entry (pdf_file->catalog, "Type", pdf_new_name ("Catalog"));
    86   pdf_set_dict_entry (pdf_file->catalog, "Pages", pdf_file->root->pages_dict);
    87   /* Outlines dictionary will be created later if needed */
    88   pdf_set_dict_entry (pdf_file->catalog, "PageLayout", pdf_new_name ("SinglePage"));
    90   pdf_file->info    = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    91   pdf_set_info (pdf_file, "Producer", "tumble by Eric Smith -- http://tumble.brouhaha.com/");
    93   pdf_file->trailer_dict = pdf_new_obj (PT_DICTIONARY);
    94   /* Size key will be added later */
    95   pdf_set_dict_entry (pdf_file->trailer_dict, "Root", pdf_file->catalog);
    96   pdf_set_dict_entry (pdf_file->trailer_dict, "Info", pdf_file->info);
    98   /* write file header */
    99   fprintf (pdf_file->f, "%%PDF-1.3\r\n");
   101   /* write comment containing 8-bit chars as a hint that the file is binary */
   102   /* PDF 1.4 spec, section 3.4.1 */
   103   fprintf (pdf_file->f, "%%\342\343\317\323\r\n");
   105   return (pdf_file);
   106 }
   109 void pdf_close (pdf_file_handle pdf_file, int page_mode)
   110 {
   111   char *page_mode_string;
   113   page_mode_string = "UseNone";
   115   switch (page_mode)
   116     {
   117     case PDF_PAGE_MODE_USE_NONE:
   118       break;
   119     case PDF_PAGE_MODE_USE_OUTLINES:
   120       if (pdf_file->outline_root)
   121 	page_mode_string = "UseOutlines";
   122       break;
   123     case PDF_PAGE_MODE_USE_THUMBS:
   124       page_mode_string = "UseThumbs";
   125       break;
   126     default:
   127       pdf_fatal ("invalid page mode\n");
   128     }
   130   pdf_set_dict_entry (pdf_file->catalog,
   131 		      "PageMode",
   132 		      pdf_new_name (page_mode_string));
   134   /* finalize trees, object numbers aren't allocated until this step */
   135   pdf_finalize_name_trees (pdf_file);
   137   /* add the page label number tree, if it exists, to the catalog */
   138   if (pdf_file->page_label_tree)
   139     pdf_set_dict_entry (pdf_file->catalog,
   140 			"PageLabels",
   141 			pdf_file->page_label_tree->root->dict);
   143   /* write body */
   144   pdf_write_all_ind_obj (pdf_file);
   146   /* write cross reference table and get maximum object number */
   147   pdf_set_dict_entry (pdf_file->trailer_dict, "Size", pdf_new_integer (pdf_write_xref (pdf_file)));
   149   /* write trailer */
   150   fprintf (pdf_file->f, "trailer\r\n");
   151   pdf_write_obj (pdf_file, pdf_file->trailer_dict);
   152   fprintf (pdf_file->f, "startxref\r\n");
   153   fprintf (pdf_file->f, "%ld\r\n", pdf_file->xref_offset);
   154   fprintf (pdf_file->f, "%%%%EOF\r\n");
   156   fclose (pdf_file->f);
   157   /* should free stuff here */
   158 }
   161 void pdf_set_author   (pdf_file_handle pdf_file, char *author)
   162 {
   163   pdf_set_info (pdf_file, "Author", author);
   164 }
   166 void pdf_set_creator  (pdf_file_handle pdf_file, char *creator)
   167 {
   168   pdf_set_info (pdf_file, "Creator", creator);
   169 }
   171 void pdf_set_producer  (pdf_file_handle pdf_file, char *producer)
   172 {
   173   pdf_set_info (pdf_file, "Producer", producer);
   174 }
   176 void pdf_set_title    (pdf_file_handle pdf_file, char *title)
   177 {
   178   pdf_set_info (pdf_file, "Title", title);
   179 }
   181 void pdf_set_subject  (pdf_file_handle pdf_file, char *subject)
   182 {
   183   pdf_set_info (pdf_file, "Subject", subject);
   184 }
   186 void pdf_set_keywords (pdf_file_handle pdf_file, char *keywords)
   187 {
   188   pdf_set_info (pdf_file, "Keywords", keywords);
   189 }
   192 pdf_page_handle pdf_new_page (pdf_file_handle pdf_file,
   193 			      double width,
   194 			      double height)
   195 {
   196   pdf_page_handle page = pdf_calloc (1, sizeof (struct pdf_page));
   198   page->pdf_file = pdf_file;
   200   page->media_box = pdf_new_obj (PT_ARRAY);
   201   pdf_add_array_elem (page->media_box, pdf_new_real (0));
   202   pdf_add_array_elem (page->media_box, pdf_new_real (0));
   203   pdf_add_array_elem (page->media_box, pdf_new_real (width));
   204   pdf_add_array_elem (page->media_box, pdf_new_real (height));
   206   page->procset = pdf_new_obj (PT_ARRAY);
   207   pdf_add_array_elem_unique (page->procset, pdf_new_name ("PDF"));
   209   page->resources = pdf_new_obj (PT_DICTIONARY);
   210   pdf_set_dict_entry (page->resources, "ProcSet", page->procset);
   212   page->page_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
   213   pdf_set_dict_entry (page->page_dict, "Type", pdf_new_name ("Page"));
   214   pdf_set_dict_entry (page->page_dict, "MediaBox", page->media_box);
   215   pdf_set_dict_entry (page->page_dict, "Resources", page->resources);
   217   if (pdf_get_integer (pdf_file->root->count) == 0)
   218     {
   219       struct pdf_obj *dest_array = pdf_new_obj (PT_ARRAY);
   220       pdf_add_array_elem (dest_array, page->page_dict);
   221       pdf_add_array_elem (dest_array, pdf_new_name ("Fit"));
   222       pdf_set_dict_entry (pdf_file->catalog, "OpenAction", dest_array);
   223     }
   225   /* $$$ currently only support a single-level pages tree */
   226   pdf_set_dict_entry (page->page_dict, "Parent", pdf_file->root->pages_dict);
   227   pdf_add_array_elem (pdf_file->root->kids, page->page_dict);
   228   pdf_set_integer (pdf_file->root->count,
   229 		   pdf_get_integer (pdf_file->root->count) + 1);
   231   page->last_XObject_name = '@';  /* first name will be "ImA" */
   233   return (page);
   234 }
   236 void pdf_close_page (pdf_page_handle pdf_page)
   237 {
   238 }
   241 void pdf_set_page_number (pdf_page_handle pdf_page, char *page_number)
   242 {
   243 }