pdf.c

Fri, 14 Mar 2003 08:56:38 +0000

author
eric
date
Fri, 14 Mar 2003 08:56:38 +0000
changeset 132
ccf1c28a2940
parent 131
4b8c80d77f76
child 133
76c197fe2eeb
permissions
-rw-r--r--

remove debug output.

     1 /*
     2  * tumble: build a PDF file from image files
     3  *
     4  * PDF routines
     5  * $Id: pdf.c,v 1.12 2003/03/14 00:24:37 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));
   101   pdf_set_dict_entry (pdf_file->catalog, "PageLayout", pdf_new_name ("SinglePage"));
   103   pdf_file->info    = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
   104   pdf_set_info (pdf_file, "Producer", "tumble by Eric Smith -- http://tumble.brouhaha.com/");
   106   pdf_file->trailer_dict = pdf_new_obj (PT_DICTIONARY);
   107   /* Size key will be added later */
   108   pdf_set_dict_entry (pdf_file->trailer_dict, "Root", pdf_file->catalog);
   109   pdf_set_dict_entry (pdf_file->trailer_dict, "Info", pdf_file->info);
   111   /* write file header */
   112   fprintf (pdf_file->f, "%%PDF-1.3\r\n");
   114   /* write comment containing 8-bit chars as a hint that the file is binary */
   115   /* PDF 1.4 spec, section 3.4.1 */
   116   fprintf (pdf_file->f, "%%\342\343\317\323\r\n");
   118   return (pdf_file);
   119 }
   122 void pdf_close (pdf_file_handle pdf_file)
   123 {
   124   /* finalize trees, object numbers aren't allocated until this step */
   125   pdf_finalize_name_trees (pdf_file);
   127   /* add the page label number tree, if it exists, to the catalog */
   128   if (pdf_file->page_label_tree)
   129     pdf_set_dict_entry (pdf_file->catalog,
   130 			"PageLabels",
   131 			pdf_file->page_label_tree->root->dict);
   133   /* write body */
   134   pdf_write_all_ind_obj (pdf_file);
   136   /* write cross reference table and get maximum object number */
   137   pdf_set_dict_entry (pdf_file->trailer_dict, "Size", pdf_new_integer (pdf_write_xref (pdf_file)));
   139   /* write trailer */
   140   fprintf (pdf_file->f, "trailer\r\n");
   141   pdf_write_obj (pdf_file, pdf_file->trailer_dict);
   142   fprintf (pdf_file->f, "startxref\r\n");
   143   fprintf (pdf_file->f, "%ld\r\n", pdf_file->xref_offset);
   144   fprintf (pdf_file->f, "%%%%EOF\r\n");
   146   fclose (pdf_file->f);
   147   /* should free stuff here */
   148 }
   151 void pdf_set_author   (pdf_file_handle pdf_file, char *author)
   152 {
   153   pdf_set_info (pdf_file, "Author", author);
   154 }
   156 void pdf_set_creator  (pdf_file_handle pdf_file, char *creator)
   157 {
   158   pdf_set_info (pdf_file, "Creator", creator);
   159 }
   161 void pdf_set_producer  (pdf_file_handle pdf_file, char *producer)
   162 {
   163   pdf_set_info (pdf_file, "Producer", producer);
   164 }
   166 void pdf_set_title    (pdf_file_handle pdf_file, char *title)
   167 {
   168   pdf_set_info (pdf_file, "Title", title);
   169 }
   171 void pdf_set_subject  (pdf_file_handle pdf_file, char *subject)
   172 {
   173   pdf_set_info (pdf_file, "Subject", subject);
   174 }
   176 void pdf_set_keywords (pdf_file_handle pdf_file, char *keywords)
   177 {
   178   pdf_set_info (pdf_file, "Keywords", keywords);
   179 }
   182 pdf_page_handle pdf_new_page (pdf_file_handle pdf_file,
   183 			      double width,
   184 			      double height)
   185 {
   186   pdf_page_handle page = pdf_calloc (1, sizeof (struct pdf_page));
   188   page->pdf_file = pdf_file;
   190   page->media_box = pdf_new_obj (PT_ARRAY);
   191   pdf_add_array_elem (page->media_box, pdf_new_real (0));
   192   pdf_add_array_elem (page->media_box, pdf_new_real (0));
   193   pdf_add_array_elem (page->media_box, pdf_new_real (width));
   194   pdf_add_array_elem (page->media_box, pdf_new_real (height));
   196   page->procset = pdf_new_obj (PT_ARRAY);
   197   pdf_add_array_elem_unique (page->procset, pdf_new_name ("PDF"));
   199   page->resources = pdf_new_obj (PT_DICTIONARY);
   200   pdf_set_dict_entry (page->resources, "ProcSet", page->procset);
   202   page->page_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
   203   pdf_set_dict_entry (page->page_dict, "Type", pdf_new_name ("Page"));
   204   pdf_set_dict_entry (page->page_dict, "MediaBox", page->media_box);
   205   pdf_set_dict_entry (page->page_dict, "Resources", page->resources);
   207   if (pdf_get_integer (pdf_file->root->count) == 0)
   208     {
   209       struct pdf_obj *dest_array = pdf_new_obj (PT_ARRAY);
   210       pdf_add_array_elem (dest_array, page->page_dict);
   211       pdf_add_array_elem (dest_array, pdf_new_name ("Fit"));
   212       pdf_set_dict_entry (pdf_file->catalog, "OpenAction", dest_array);
   213     }
   215   /* $$$ currently only support a single-level pages tree */
   216   pdf_set_dict_entry (page->page_dict, "Parent", pdf_file->root->pages_dict);
   217   pdf_add_array_elem (pdf_file->root->kids, page->page_dict);
   218   pdf_set_integer (pdf_file->root->count,
   219 		   pdf_get_integer (pdf_file->root->count) + 1);
   221   page->last_XObject_name = '@';  /* first name will be "ImA" */
   223   return (page);
   224 }
   226 void pdf_close_page (pdf_page_handle pdf_page)
   227 {
   228 }
   231 void pdf_set_page_number (pdf_page_handle pdf_page, char *page_number)
   232 {
   233 }