1.1 diff -r d1c6dc4bf34a -r e8821eb2fb08 pdf.c 1.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 +++ b/pdf.c Thu Feb 20 12:16:00 2003 +0000 1.4 @@ -0,0 +1,171 @@ 1.5 +#include <stdio.h> 1.6 +#include <stdlib.h> 1.7 + 1.8 + 1.9 +#include "libpdf.h" 1.10 +#include "libpdf_util.h" 1.11 +#include "libpdf_prim.h" 1.12 +#include "libpdf_private.h" 1.13 + 1.14 + 1.15 +static void pdf_set_info (pdf_file_handle pdf_file, char *key, char *val) 1.16 +{ 1.17 + if (! pdf_file->info) 1.18 + pdf_file->info = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY)); 1.19 + 1.20 + pdf_set_dict_entry (pdf_file->info, key, pdf_new_string (val)); 1.21 +} 1.22 + 1.23 + 1.24 +void pdf_init (void) 1.25 +{ 1.26 +} 1.27 + 1.28 + 1.29 +struct pdf_pages *pdf_new_pages (pdf_file_handle pdf_file) 1.30 +{ 1.31 + struct pdf_pages *pages = pdf_calloc (sizeof (struct pdf_pages)); 1.32 + pages->kids = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_ARRAY)); 1.33 + pages->count = pdf_new_integer (0); 1.34 + pages->pages_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY)); 1.35 + pdf_set_dict_entry (pages->pages_dict, "Type", pdf_new_name ("Pages")); 1.36 + pdf_set_dict_entry (pages->pages_dict, "Kids", pages->kids); 1.37 + pdf_set_dict_entry (pages->pages_dict, "Count", pages->count); 1.38 + return (pages); 1.39 +} 1.40 + 1.41 + 1.42 +pdf_file_handle pdf_create (char *filename) 1.43 +{ 1.44 + pdf_file_handle pdf_file; 1.45 + 1.46 + pdf_file = pdf_calloc (sizeof (struct pdf_file)); 1.47 + 1.48 + pdf_file->f = fopen (filename, "wb"); 1.49 + if (! pdf_file->f) 1.50 + { 1.51 + pdf_fatal ("error opening output file\n"); 1.52 + } 1.53 + 1.54 + pdf_file->root = pdf_new_pages (pdf_file); 1.55 + 1.56 + pdf_file->catalog = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY)); 1.57 + pdf_set_dict_entry (pdf_file->catalog, "Type", pdf_new_name ("Catalog")); 1.58 + pdf_set_dict_entry (pdf_file->catalog, "Pages", pdf_file->root->pages_dict); 1.59 + /* Outlines dictionary will be created later if needed*/ 1.60 + pdf_set_dict_entry (pdf_file->catalog, "PageMode", pdf_new_name ("UseNone")); 1.61 + 1.62 + pdf_file->info = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY)); 1.63 + pdf_set_info (pdf_file, "Producer", "libpdf, Copyright 2003 Eric Smith <eric@brouhaha.com>"); 1.64 + 1.65 + pdf_file->trailer_dict = pdf_new_obj (PT_DICTIONARY); 1.66 + /* Size key will be added later */ 1.67 + pdf_set_dict_entry (pdf_file->trailer_dict, "Root", pdf_file->catalog); 1.68 + pdf_set_dict_entry (pdf_file->trailer_dict, "Info", pdf_file->info); 1.69 + 1.70 + /* write file header */ 1.71 + fprintf (pdf_file->f, "%%PDF-1.2\r\n"); 1.72 + 1.73 + return (pdf_file); 1.74 +} 1.75 + 1.76 + 1.77 +void pdf_close (pdf_file_handle pdf_file) 1.78 +{ 1.79 + /* write body */ 1.80 + pdf_write_all_ind_obj (pdf_file); 1.81 + 1.82 + /* write cross reference table and get maximum object number */ 1.83 + pdf_set_dict_entry (pdf_file->trailer_dict, "Size", pdf_new_integer (pdf_write_xref (pdf_file))); 1.84 + 1.85 + /* write trailer */ 1.86 + fprintf (pdf_file->f, "trailer\r\n"); 1.87 + pdf_write_obj (pdf_file, pdf_file->trailer_dict); 1.88 + fprintf (pdf_file->f, "startxref\r\n"); 1.89 + fprintf (pdf_file->f, "%ld\r\n", pdf_file->xref_offset); 1.90 + fprintf (pdf_file->f, "%%%%EOF\r\n"); 1.91 + 1.92 + fclose (pdf_file->f); 1.93 + /* should free stuff here */ 1.94 +} 1.95 + 1.96 + 1.97 +void pdf_set_author (pdf_file_handle pdf_file, char *author) 1.98 +{ 1.99 + pdf_set_info (pdf_file, "Author", author); 1.100 +} 1.101 + 1.102 +void pdf_set_creator (pdf_file_handle pdf_file, char *creator) 1.103 +{ 1.104 + pdf_set_info (pdf_file, "Creator", creator); 1.105 +} 1.106 + 1.107 +void pdf_set_producer (pdf_file_handle pdf_file, char *producer) 1.108 +{ 1.109 + pdf_set_info (pdf_file, "Producer", producer); 1.110 +} 1.111 + 1.112 +void pdf_set_title (pdf_file_handle pdf_file, char *title) 1.113 +{ 1.114 + pdf_set_info (pdf_file, "Title", title); 1.115 +} 1.116 + 1.117 +void pdf_set_subject (pdf_file_handle pdf_file, char *subject) 1.118 +{ 1.119 + pdf_set_info (pdf_file, "Subject", subject); 1.120 +} 1.121 + 1.122 +void pdf_set_keywords (pdf_file_handle pdf_file, char *keywords) 1.123 +{ 1.124 + pdf_set_info (pdf_file, "Keywords", keywords); 1.125 +} 1.126 + 1.127 + 1.128 +pdf_page_handle pdf_new_page (pdf_file_handle pdf_file, 1.129 + double width, 1.130 + double height) 1.131 +{ 1.132 + pdf_page_handle page = pdf_calloc (sizeof (struct pdf_page)); 1.133 + 1.134 + page->pdf_file = pdf_file; 1.135 + 1.136 + page->media_box = pdf_new_obj (PT_ARRAY); 1.137 + pdf_add_array_elem (page->media_box, pdf_new_real (0)); 1.138 + pdf_add_array_elem (page->media_box, pdf_new_real (0)); 1.139 + pdf_add_array_elem (page->media_box, pdf_new_real (width)); 1.140 + pdf_add_array_elem (page->media_box, pdf_new_real (height)); 1.141 + 1.142 + page->procset = pdf_new_obj (PT_ARRAY); 1.143 + pdf_add_array_elem (page->procset, pdf_new_name ("PDF")); 1.144 + 1.145 + page->resources = pdf_new_obj (PT_DICTIONARY); 1.146 + pdf_set_dict_entry (page->resources, "ProcSet", page->procset); 1.147 + 1.148 + page->page_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY)); 1.149 + pdf_set_dict_entry (page->page_dict, "Type", pdf_new_name ("Page")); 1.150 + pdf_set_dict_entry (page->page_dict, "MediaBox", page->media_box); 1.151 + pdf_set_dict_entry (page->page_dict, "Resources", page->resources); 1.152 + 1.153 + /* $$$ currently only support a single-level pages tree */ 1.154 + pdf_set_dict_entry (page->page_dict, "Parent", pdf_file->root->pages_dict); 1.155 + pdf_add_array_elem (pdf_file->root->kids, page->page_dict); 1.156 + pdf_set_integer (pdf_file->root->count, 1.157 + pdf_get_integer (pdf_file->root->count) + 1); 1.158 + 1.159 + page->last_XObject_name = '@'; /* first name will be "ImA" */ 1.160 + 1.161 + return (page); 1.162 +} 1.163 + 1.164 +void pdf_close_page (pdf_page_handle pdf_page) 1.165 +{ 1.166 +} 1.167 + 1.168 + 1.169 +void pdf_set_page_number (pdf_page_handle pdf_page, char *page_number) 1.170 +{ 1.171 +} 1.172 + 1.173 +void pdf_bookmark (pdf_page_handle pdf_page, int level, char *name) 1.174 +{ 1.175 +}