1 /*
2  * << Haru Free PDF Library 2.0.0 >> -- grid_sheet.c
3  *
4  * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
5  *
6  * Permission to use, copy, modify, distribute and sell this software
7  * and its documentation for any purpose is hereby granted without fee,
8  * provided that the above copyright notice appear in all copies and
9  * that both that copyright notice and this permission notice appear
10  * in supporting documentation.
11  * It is provided "as is" without express or implied warranty.
12  *
13  */
14 
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <setjmp.h>
19 #include "hpdf.h"
20 #include "grid_sheet.h"
21 
22 #ifdef STAND_ALONE
23 jmp_buf env;
24 
25 #ifdef HPDF_DLL
26 void __stdcall
27 #else
28 void
29 #endif
error_handler(HPDF_STATUS error_no,HPDF_STATUS detail_no,void * user_data)30 error_handler  (HPDF_STATUS   error_no,
31                 HPDF_STATUS   detail_no,
32                 void         *user_data)
33 {
34     printf ("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no,
35                 (HPDF_UINT)detail_no);
36     longjmp(env, 1);
37 }
38 
39 
40 #endif /* STAND_ALONE */
41 
42 void
print_grid(HPDF_Doc pdf,HPDF_Page page)43 print_grid  (HPDF_Doc     pdf,
44              HPDF_Page    page)
45 {
46     HPDF_REAL height = HPDF_Page_GetHeight (page);
47     HPDF_REAL width = HPDF_Page_GetWidth (page);
48     HPDF_Font font = HPDF_GetFont (pdf, "Helvetica", NULL);
49     HPDF_UINT x, y;
50 
51     HPDF_Page_SetFontAndSize (page, font, 5);
52     HPDF_Page_SetGrayFill (page, 0.5);
53     HPDF_Page_SetGrayStroke (page, 0.8);
54 
55     /* Draw horizontal lines */
56     y = 0;
57     while (y < height) {
58         if (y % 10 == 0)
59             HPDF_Page_SetLineWidth (page, 0.5);
60         else {
61             if (HPDF_Page_GetLineWidth (page) != 0.25)
62                 HPDF_Page_SetLineWidth (page, 0.25);
63         }
64 
65         HPDF_Page_MoveTo (page, 0, y);
66         HPDF_Page_LineTo (page, width, y);
67         HPDF_Page_Stroke (page);
68 
69         if (y % 10 == 0 && y > 0) {
70             HPDF_Page_SetGrayStroke (page, 0.5);
71 
72             HPDF_Page_MoveTo (page, 0, y);
73             HPDF_Page_LineTo (page, 5, y);
74             HPDF_Page_Stroke (page);
75 
76             HPDF_Page_SetGrayStroke (page, 0.8);
77         }
78 
79         y += 5;
80     }
81 
82 
83     /* Draw virtical lines */
84     x = 0;
85     while (x < width) {
86         if (x % 10 == 0)
87             HPDF_Page_SetLineWidth (page, 0.5);
88         else {
89             if (HPDF_Page_GetLineWidth (page) != 0.25)
90                 HPDF_Page_SetLineWidth (page, 0.25);
91         }
92 
93         HPDF_Page_MoveTo (page, x, 0);
94         HPDF_Page_LineTo (page, x, height);
95         HPDF_Page_Stroke (page);
96 
97         if (x % 50 == 0 && x > 0) {
98             HPDF_Page_SetGrayStroke (page, 0.5);
99 
100             HPDF_Page_MoveTo (page, x, 0);
101             HPDF_Page_LineTo (page, x, 5);
102             HPDF_Page_Stroke (page);
103 
104             HPDF_Page_MoveTo (page, x, height);
105             HPDF_Page_LineTo (page, x, height - 5);
106             HPDF_Page_Stroke (page);
107 
108             HPDF_Page_SetGrayStroke (page, 0.8);
109         }
110 
111         x += 5;
112     }
113 
114     /* Draw horizontal text */
115     y = 0;
116     while (y < height) {
117         if (y % 10 == 0 && y > 0) {
118             char buf[12];
119 
120             HPDF_Page_BeginText (page);
121             HPDF_Page_MoveTextPos (page, 5, y - 2);
122 #ifdef __WIN32__
123             _snprintf (buf, 12, "%u", y);
124 #else
125             snprintf (buf, 12, "%u", y);
126 #endif
127             HPDF_Page_ShowText (page, buf);
128             HPDF_Page_EndText (page);
129         }
130 
131         y += 5;
132     }
133 
134 
135     /* Draw virtical text */
136     x = 0;
137     while (x < width) {
138         if (x % 50 == 0 && x > 0) {
139             char buf[12];
140 
141             HPDF_Page_BeginText (page);
142             HPDF_Page_MoveTextPos (page, x, 5);
143 #ifdef __WIN32__
144             _snprintf (buf, 12, "%u", x);
145 #else
146             snprintf (buf, 12, "%u", x);
147 #endif
148             HPDF_Page_ShowText (page, buf);
149             HPDF_Page_EndText (page);
150 
151             HPDF_Page_BeginText (page);
152             HPDF_Page_MoveTextPos (page, x, height - 10);
153             HPDF_Page_ShowText (page, buf);
154             HPDF_Page_EndText (page);
155         }
156 
157         x += 5;
158     }
159 
160     HPDF_Page_SetGrayFill (page, 0);
161     HPDF_Page_SetGrayStroke (page, 0);
162 }
163 
164 #ifdef STAND_ALONE
165 
166 int
main(int argc,char ** argv)167 main (int argc, char **argv)
168 {
169     HPDF_Doc  pdf;
170     HPDF_Page page;
171     char fname[256];
172 
173     strcpy (fname, argv[0]);
174     strcat (fname, ".pdf");
175 
176     pdf = HPDF_New (error_handler, NULL);
177     if (!pdf) {
178         printf ("error: cannot create PdfDoc object\n");
179         return 1;
180     }
181 
182     if (setjmp(env)) {
183         HPDF_Free (pdf);
184         return 1;
185     }
186 
187     /* add a new page object. */
188     page = HPDF_AddPage (pdf);
189 
190     HPDF_Page_SetHeight (page, 600);
191     HPDF_Page_SetWidth (page, 400);
192 
193     print_grid  (pdf, page);
194 
195 
196     /* save the document to a file */
197     HPDF_SaveToFile (pdf, fname);
198 
199     /* clean up */
200     HPDF_Free (pdf);
201 
202     return 0;
203 }
204 
205 #endif /* STAND_ALONE */
206 
207 
208