2 * << Haru Free PDF Library 2.0.0 >> -- line_demo.c
4 * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
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.
28 error_handler (HPDF_STATUS error_no,
29 HPDF_STATUS detail_no,
32 printf ("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no,
33 (HPDF_UINT)detail_no);
38 draw_line (HPDF_Page page,
43 HPDF_Page_BeginText (page);
44 HPDF_Page_MoveTextPos (page, x, y - 10);
45 HPDF_Page_ShowText (page, label);
46 HPDF_Page_EndText (page);
48 HPDF_Page_MoveTo (page, x, y - 15);
49 HPDF_Page_LineTo (page, x + 220, y - 15);
50 HPDF_Page_Stroke (page);
54 draw_line2 (HPDF_Page page,
59 HPDF_Page_BeginText (page);
60 HPDF_Page_MoveTextPos (page, x, y);
61 HPDF_Page_ShowText (page, label);
62 HPDF_Page_EndText (page);
64 HPDF_Page_MoveTo (page, x + 30, y - 25);
65 HPDF_Page_LineTo (page, x + 160, y - 25);
66 HPDF_Page_Stroke (page);
70 draw_rect (HPDF_Page page,
75 HPDF_Page_BeginText (page);
76 HPDF_Page_MoveTextPos (page, x, y - 10);
77 HPDF_Page_ShowText (page, label);
78 HPDF_Page_EndText (page);
80 HPDF_Page_Rectangle(page, x, y - 40, 220, 25);
83 int main (int argc, char **argv)
85 const char* page_title = "Line Example";
92 const HPDF_UINT16 DASH_MODE1[] = {3};
93 const HPDF_UINT16 DASH_MODE2[] = {3, 7};
94 const HPDF_UINT16 DASH_MODE3[] = {8, 7, 2, 7};
107 strcpy (fname, argv[0]);
108 strcat (fname, ".pdf");
110 pdf = HPDF_New (error_handler, NULL);
112 printf ("error: cannot create PdfDoc object\n");
121 /* create default-font */
122 font = HPDF_GetFont (pdf, "Helvetica", NULL);
124 /* add a new page object. */
125 page = HPDF_AddPage (pdf);
127 /* print the lines of the page. */
128 HPDF_Page_SetLineWidth (page, 1);
129 HPDF_Page_Rectangle (page, 50, 50, HPDF_Page_GetWidth(page) - 100,
130 HPDF_Page_GetHeight (page) - 110);
131 HPDF_Page_Stroke (page);
133 /* print the title of the page (with positioning center). */
134 HPDF_Page_SetFontAndSize (page, font, 24);
135 tw = HPDF_Page_TextWidth (page, page_title);
136 HPDF_Page_BeginText (page);
137 HPDF_Page_MoveTextPos (page, (HPDF_Page_GetWidth(page) - tw) / 2,
138 HPDF_Page_GetHeight (page) - 50);
139 HPDF_Page_ShowText (page, page_title);
140 HPDF_Page_EndText (page);
142 HPDF_Page_SetFontAndSize (page, font, 10);
144 /* Draw verious widths of lines. */
145 HPDF_Page_SetLineWidth (page, 0);
146 draw_line (page, 60, 770, "line width = 0");
148 HPDF_Page_SetLineWidth (page, 1.0);
149 draw_line (page, 60, 740, "line width = 1.0");
151 HPDF_Page_SetLineWidth (page, 2.0);
152 draw_line (page, 60, 710, "line width = 2.0");
154 /* Line dash pattern */
155 HPDF_Page_SetLineWidth (page, 1.0);
157 HPDF_Page_SetDash (page, DASH_MODE1, 1, 1);
158 draw_line (page, 60, 680, "dash_ptn=[3], phase=1 -- "
159 "2 on, 3 off, 3 on...");
161 HPDF_Page_SetDash (page, DASH_MODE2, 2, 2);
162 draw_line (page, 60, 650, "dash_ptn=[7, 3], phase=2 -- "
163 "5 on 3 off, 7 on,...");
165 HPDF_Page_SetDash (page, DASH_MODE3, 4, 0);
166 draw_line (page, 60, 620, "dash_ptn=[8, 7, 2, 7], phase=0");
168 HPDF_Page_SetDash (page, NULL, 0, 0);
170 HPDF_Page_SetLineWidth (page, 30);
171 HPDF_Page_SetRGBStroke (page, 0.0, 0.5, 0.0);
174 HPDF_Page_SetLineCap (page, HPDF_BUTT_END);
175 draw_line2 (page, 60, 570, "PDF_BUTT_END");
177 HPDF_Page_SetLineCap (page, HPDF_ROUND_END);
178 draw_line2 (page, 60, 505, "PDF_ROUND_END");
180 HPDF_Page_SetLineCap (page, HPDF_PROJECTING_SCUARE_END);
181 draw_line2 (page, 60, 440, "PDF_PROJECTING_SCUARE_END");
183 /* Line Join Style */
184 HPDF_Page_SetLineWidth (page, 30);
185 HPDF_Page_SetRGBStroke (page, 0.0, 0.0, 0.5);
187 HPDF_Page_SetLineJoin (page, HPDF_MITER_JOIN);
188 HPDF_Page_MoveTo (page, 120, 300);
189 HPDF_Page_LineTo (page, 160, 340);
190 HPDF_Page_LineTo (page, 200, 300);
191 HPDF_Page_Stroke (page);
193 HPDF_Page_BeginText (page);
194 HPDF_Page_MoveTextPos (page, 60, 360);
195 HPDF_Page_ShowText (page, "PDF_MITER_JOIN");
196 HPDF_Page_EndText (page);
198 HPDF_Page_SetLineJoin (page, HPDF_ROUND_JOIN);
199 HPDF_Page_MoveTo (page, 120, 195);
200 HPDF_Page_LineTo (page, 160, 235);
201 HPDF_Page_LineTo (page, 200, 195);
202 HPDF_Page_Stroke (page);
204 HPDF_Page_BeginText (page);
205 HPDF_Page_MoveTextPos (page, 60, 255);
206 HPDF_Page_ShowText (page, "PDF_ROUND_JOIN");
207 HPDF_Page_EndText (page);
209 HPDF_Page_SetLineJoin (page, HPDF_BEVEL_JOIN);
210 HPDF_Page_MoveTo (page, 120, 90);
211 HPDF_Page_LineTo (page, 160, 130);
212 HPDF_Page_LineTo (page, 200, 90);
213 HPDF_Page_Stroke (page);
215 HPDF_Page_BeginText (page);
216 HPDF_Page_MoveTextPos (page, 60, 150);
217 HPDF_Page_ShowText (page, "PDF_BEVEL_JOIN");
218 HPDF_Page_EndText (page);
221 HPDF_Page_SetLineWidth (page, 2);
222 HPDF_Page_SetRGBStroke (page, 0, 0, 0);
223 HPDF_Page_SetRGBFill (page, 0.75, 0.0, 0.0);
225 draw_rect (page, 300, 770, "Stroke");
226 HPDF_Page_Stroke (page);
228 draw_rect (page, 300, 720, "Fill");
229 HPDF_Page_Fill (page);
231 draw_rect (page, 300, 670, "Fill then Stroke");
232 HPDF_Page_FillStroke (page);
235 HPDF_Page_GSave (page); /* Save the current graphic state */
236 draw_rect (page, 300, 620, "Clip Rectangle");
237 HPDF_Page_Clip (page);
238 HPDF_Page_Stroke (page);
239 HPDF_Page_SetFontAndSize (page, font, 13);
241 HPDF_Page_BeginText (page);
242 HPDF_Page_MoveTextPos (page, 290, 600);
243 HPDF_Page_SetTextLeading (page, 12);
244 HPDF_Page_ShowText (page,
245 "Clip Clip Clip Clip Clip Clipi Clip Clip Clip");
246 HPDF_Page_ShowTextNextLine (page,
247 "Clip Clip Clip Clip Clip Clip Clip Clip Clip");
248 HPDF_Page_ShowTextNextLine (page,
249 "Clip Clip Clip Clip Clip Clip Clip Clip Clip");
250 HPDF_Page_EndText (page);
251 HPDF_Page_GRestore (page);
253 /* Curve Example(CurveTo2) */
263 HPDF_Page_SetRGBFill (page, 0, 0, 0);
265 HPDF_Page_BeginText (page);
266 HPDF_Page_MoveTextPos (page, 300, 540);
267 HPDF_Page_ShowText (page, "CurveTo2(x1, y1, x2. y2)");
268 HPDF_Page_EndText (page);
270 HPDF_Page_BeginText (page);
271 HPDF_Page_MoveTextPos (page, x + 5, y - 5);
272 HPDF_Page_ShowText (page, "Current point");
273 HPDF_Page_MoveTextPos (page, x1 - x, y1 - y);
274 HPDF_Page_ShowText (page, "(x1, y1)");
275 HPDF_Page_MoveTextPos (page, x2 - x1, y2 - y1);
276 HPDF_Page_ShowText (page, "(x2, y2)");
277 HPDF_Page_EndText (page);
279 HPDF_Page_SetDash (page, DASH_MODE1, 1, 0);
281 HPDF_Page_SetLineWidth (page, 0.5);
282 HPDF_Page_MoveTo (page, x1, y1);
283 HPDF_Page_LineTo (page, x2, y2);
284 HPDF_Page_Stroke (page);
286 HPDF_Page_SetDash (page, NULL, 0, 0);
288 HPDF_Page_SetLineWidth (page, 1.5);
290 HPDF_Page_MoveTo (page, x, y);
291 HPDF_Page_CurveTo2 (page, x1, y1, x2, y2);
292 HPDF_Page_Stroke (page);
294 /* Curve Example(CurveTo3) */
299 HPDF_Page_BeginText (page);
300 HPDF_Page_MoveTextPos (page, 300, 390);
301 HPDF_Page_ShowText (page, "CurveTo3(x1, y1, x2. y2)");
302 HPDF_Page_EndText (page);
304 HPDF_Page_BeginText (page);
305 HPDF_Page_MoveTextPos (page, x + 5, y - 5);
306 HPDF_Page_ShowText (page, "Current point");
307 HPDF_Page_MoveTextPos (page, x1 - x, y1 - y);
308 HPDF_Page_ShowText (page, "(x1, y1)");
309 HPDF_Page_MoveTextPos (page, x2 - x1, y2 - y1);
310 HPDF_Page_ShowText (page, "(x2, y2)");
311 HPDF_Page_EndText (page);
313 HPDF_Page_SetDash (page, DASH_MODE1, 1, 0);
315 HPDF_Page_SetLineWidth (page, 0.5);
316 HPDF_Page_MoveTo (page, x, y);
317 HPDF_Page_LineTo (page, x1, y1);
318 HPDF_Page_Stroke (page);
320 HPDF_Page_SetDash (page, NULL, 0, 0);
322 HPDF_Page_SetLineWidth (page, 1.5);
323 HPDF_Page_MoveTo (page, x, y);
324 HPDF_Page_CurveTo3 (page, x1, y1, x2, y2);
325 HPDF_Page_Stroke (page);
327 /* Curve Example(CurveTo) */
333 HPDF_Page_BeginText (page);
334 HPDF_Page_MoveTextPos (page, 300, 240);
335 HPDF_Page_ShowText (page, "CurveTo(x1, y1, x2. y2, x3, y3)");
336 HPDF_Page_EndText (page);
338 HPDF_Page_BeginText (page);
339 HPDF_Page_MoveTextPos (page, x + 5, y - 5);
340 HPDF_Page_ShowText (page, "Current point");
341 HPDF_Page_MoveTextPos (page, x1 - x, y1 - y);
342 HPDF_Page_ShowText (page, "(x1, y1)");
343 HPDF_Page_MoveTextPos (page, x2 - x1, y2 - y1);
344 HPDF_Page_ShowText (page, "(x2, y2)");
345 HPDF_Page_MoveTextPos (page, x3 - x2, y3 - y2);
346 HPDF_Page_ShowText (page, "(x3, y3)");
347 HPDF_Page_EndText (page);
349 HPDF_Page_SetDash (page, DASH_MODE1, 1, 0);
351 HPDF_Page_SetLineWidth (page, 0.5);
352 HPDF_Page_MoveTo (page, x, y);
353 HPDF_Page_LineTo (page, x1, y1);
354 HPDF_Page_Stroke (page);
355 HPDF_Page_MoveTo (page, x2, y2);
356 HPDF_Page_LineTo (page, x3, y3);
357 HPDF_Page_Stroke (page);
359 HPDF_Page_SetDash (page, NULL, 0, 0);
361 HPDF_Page_SetLineWidth (page, 1.5);
362 HPDF_Page_MoveTo (page, x, y);
363 HPDF_Page_CurveTo (page, x1, y1, x2, y2, x3, y3);
364 HPDF_Page_Stroke (page);
366 /* save the document to a file */
367 HPDF_SaveToFile (pdf, fname);