pdf.c

Thu, 13 Mar 2003 07:59:10 +0000

author
eric
date
Thu, 13 Mar 2003 07:59:10 +0000
changeset 121
e50c7f76f2f6
parent 118
c22e1c0a64fd
child 124
ba64dfca82e9
permissions
-rw-r--r--

don't use page mode USE_OUTLINES if there are no outline entries.

     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.8 2003/03/12 22:56:57 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"
    38 #include "pdf_name_tree.h"
    41 static void pdf_set_info (pdf_file_handle pdf_file, char *key, char *val)
    42 {
    43   if (! pdf_file->info)
    44     pdf_file->info = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    46   pdf_set_dict_entry (pdf_file->info, key, pdf_new_string (val));
    47 }
    50 void pdf_init (void)
    51 {
    52 }
    55 struct pdf_pages *pdf_new_pages (pdf_file_handle pdf_file)
    56 {
    57   struct pdf_pages *pages = pdf_calloc (1, sizeof (struct pdf_pages));
    58   pages->kids = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_ARRAY));
    59   pages->count = pdf_new_integer (0);
    60   pages->pages_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    61   pdf_set_dict_entry (pages->pages_dict, "Type", pdf_new_name ("Pages"));
    62   pdf_set_dict_entry (pages->pages_dict, "Kids", pages->kids);
    63   pdf_set_dict_entry (pages->pages_dict, "Count", pages->count);
    64   return (pages);
    65 }
    68 pdf_file_handle pdf_create (char *filename, int page_mode)
    69 {
    70   pdf_file_handle pdf_file;
    71   char *page_mode_string;
    73   switch (page_mode)
    74     {
    75     case PDF_PAGE_MODE_USE_NONE:      page_mode_string = "UseNone";     break;
    76     case PDF_PAGE_MODE_USE_OUTLINES:  page_mode_string = "UseOutlines"; break;
    77     case PDF_PAGE_MODE_USE_THUMBS:    page_mode_string = "UseThumbs";   break;
    78     default:
    79       pdf_fatal ("invalid page mode\n");
    80     }
    82   pdf_file = pdf_calloc (1, sizeof (struct pdf_file));
    84   pdf_file->f = fopen (filename, "wb");
    85   if (! pdf_file->f)
    86     {
    87       pdf_fatal ("error opening output file\n");
    88     }
    90   pdf_file->root = pdf_new_pages (pdf_file);
    92   pdf_file->catalog = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
    93   pdf_set_dict_entry (pdf_file->catalog, "Type", pdf_new_name ("Catalog"));
    94   pdf_set_dict_entry (pdf_file->catalog, "Pages", pdf_file->root->pages_dict);
    95   /* Outlines dictionary will be created later if needed */
    96   pdf_set_dict_entry (pdf_file->catalog,
    97 		      "PageMode",
    98 		      pdf_new_name (page_mode_string));
   100   pdf_file->info    = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
   101   pdf_set_info (pdf_file, "Producer", "t2p by Eric Smith <eric@brouhaha.com>");
   103   pdf_file->trailer_dict = pdf_new_obj (PT_DICTIONARY);
   104   /* Size key will be added later */
   105   pdf_set_dict_entry (pdf_file->trailer_dict, "Root", pdf_file->catalog);
   106   pdf_set_dict_entry (pdf_file->trailer_dict, "Info", pdf_file->info);
   108   /* write file header */
   109   fprintf (pdf_file->f, "%%PDF-1.2\r\n");
   111   return (pdf_file);
   112 }
   115 void pdf_close (pdf_file_handle pdf_file)
   116 {
   117   /* finalize all data structures */
   118   pdf_finalize_name_trees (pdf_file);
   120   /* write body */
   121   pdf_write_all_ind_obj (pdf_file);
   123   /* write cross reference table and get maximum object number */
   124   pdf_set_dict_entry (pdf_file->trailer_dict, "Size", pdf_new_integer (pdf_write_xref (pdf_file)));
   126   /* write trailer */
   127   fprintf (pdf_file->f, "trailer\r\n");
   128   pdf_write_obj (pdf_file, pdf_file->trailer_dict);
   129   fprintf (pdf_file->f, "startxref\r\n");
   130   fprintf (pdf_file->f, "%ld\r\n", pdf_file->xref_offset);
   131   fprintf (pdf_file->f, "%%%%EOF\r\n");
   133   fclose (pdf_file->f);
   134   /* should free stuff here */
   135 }
   138 void pdf_set_author   (pdf_file_handle pdf_file, char *author)
   139 {
   140   pdf_set_info (pdf_file, "Author", author);
   141 }
   143 void pdf_set_creator  (pdf_file_handle pdf_file, char *creator)
   144 {
   145   pdf_set_info (pdf_file, "Creator", creator);
   146 }
   148 void pdf_set_producer  (pdf_file_handle pdf_file, char *producer)
   149 {
   150   pdf_set_info (pdf_file, "Producer", producer);
   151 }
   153 void pdf_set_title    (pdf_file_handle pdf_file, char *title)
   154 {
   155   pdf_set_info (pdf_file, "Title", title);
   156 }
   158 void pdf_set_subject  (pdf_file_handle pdf_file, char *subject)
   159 {
   160   pdf_set_info (pdf_file, "Subject", subject);
   161 }
   163 void pdf_set_keywords (pdf_file_handle pdf_file, char *keywords)
   164 {
   165   pdf_set_info (pdf_file, "Keywords", keywords);
   166 }
   169 pdf_page_handle pdf_new_page (pdf_file_handle pdf_file,
   170 			      double width,
   171 			      double height)
   172 {
   173   pdf_page_handle page = pdf_calloc (1, sizeof (struct pdf_page));
   175   page->pdf_file = pdf_file;
   177   page->media_box = pdf_new_obj (PT_ARRAY);
   178   pdf_add_array_elem (page->media_box, pdf_new_real (0));
   179   pdf_add_array_elem (page->media_box, pdf_new_real (0));
   180   pdf_add_array_elem (page->media_box, pdf_new_real (width));
   181   pdf_add_array_elem (page->media_box, pdf_new_real (height));
   183   page->procset = pdf_new_obj (PT_ARRAY);
   184   pdf_add_array_elem_unique (page->procset, pdf_new_name ("PDF"));
   186   page->resources = pdf_new_obj (PT_DICTIONARY);
   187   pdf_set_dict_entry (page->resources, "ProcSet", page->procset);
   189   page->page_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY));
   190   pdf_set_dict_entry (page->page_dict, "Type", pdf_new_name ("Page"));
   191   pdf_set_dict_entry (page->page_dict, "MediaBox", page->media_box);
   192   pdf_set_dict_entry (page->page_dict, "Resources", page->resources);
   194   /* $$$ currently only support a single-level pages tree */
   195   pdf_set_dict_entry (page->page_dict, "Parent", pdf_file->root->pages_dict);
   196   pdf_add_array_elem (pdf_file->root->kids, page->page_dict);
   197   pdf_set_integer (pdf_file->root->count,
   198 		   pdf_get_integer (pdf_file->root->count) + 1);
   200   page->last_XObject_name = '@';  /* first name will be "ImA" */
   202   return (page);
   203 }
   205 void pdf_close_page (pdf_page_handle pdf_page)
   206 {
   207 }
   210 void pdf_set_page_number (pdf_page_handle pdf_page, char *page_number)
   211 {
   212 }
   214 void pdf_bookmark (pdf_page_handle pdf_page, int level, char *name)
   215 {
   216 }