1 /*
2  * tumble: build a PDF file from image files
3  *
4  * PDF routines
5  * Copyright 2003, 2017 Eric Smith <spacewar@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.  Note that permission is
10  * not granted to redistribute this program under the terms of any
11  * other version of the General Public License.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
21  */
22 
23 
24 #include <stdbool.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 
31 #include "bitblt.h"
32 #include "pdf.h"
33 #include "pdf_util.h"
34 #include "pdf_prim.h"
35 #include "pdf_private.h"
36 
37 
38 
pdf_write_text_content_callback(pdf_file_handle pdf_file,struct pdf_obj * stream,void * app_data)39 static void pdf_write_text_content_callback (pdf_file_handle pdf_file,
40 					     struct pdf_obj *stream,
41 					     void *app_data)
42 {
43   pdf_stream_printf (pdf_file, stream,
44 		     "BT /F1 24 Tf 100 100 Td (Hello World) Tj ET\r\n");
45 }
46 
47 
pdf_create_font(pdf_page_handle pdf_page)48 static struct pdf_obj *pdf_create_font (pdf_page_handle pdf_page)
49 {
50   struct pdf_obj *font = pdf_new_ind_ref (pdf_page->pdf_file,
51 					  pdf_new_obj (PT_DICTIONARY));
52   pdf_set_dict_entry (font, "Type", pdf_new_name ("Font"));
53   pdf_set_dict_entry (font, "Subtype", pdf_new_name ("Type1"));
54   pdf_set_dict_entry (font, "Name", pdf_new_name ("F1"));
55   pdf_set_dict_entry (font, "BaseFont", pdf_new_name ("Helvetica"));
56   pdf_set_dict_entry (font, "Encoding", pdf_new_name ("MacRomanEncoding"));
57 
58   return (font);
59 }
60 
pdf_write_text(pdf_page_handle pdf_page)61 void pdf_write_text (pdf_page_handle pdf_page)
62 {
63   struct pdf_obj *font;
64   struct pdf_obj *font_dict;
65   struct pdf_obj *content_stream;
66 
67   font = pdf_create_font (pdf_page);
68 
69   font_dict = pdf_new_ind_ref (pdf_page->pdf_file,
70 			       pdf_new_obj (PT_DICTIONARY));
71 
72   pdf_set_dict_entry (font_dict, "F1", font);
73 
74   pdf_set_dict_entry (pdf_page->resources, "Font", font_dict);
75 
76   pdf_add_array_elem_unique (pdf_page->procset, pdf_new_name ("Text"));
77 
78   content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
79 				    pdf_new_stream (pdf_page->pdf_file,
80 						    pdf_new_obj (PT_DICTIONARY),
81 						    & pdf_write_text_content_callback,
82 						    NULL));
83 
84   pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream);
85 
86   pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
87 }
88 
89