Haru Free PDF Library
line_demo.c
/*
* << Haru Free PDF Library 2.0.0 >> -- line_demo.c
*
* Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation.
* It is provided "as is" without express or implied warranty.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "hpdf.h"
#include "handler.h"
void
draw_line (HPDF_Page page,
float x,
float y,
const char *label)
{
HPDF_Page_MoveTextPos (page, x, y - 10);
HPDF_Page_ShowText (page, label);
HPDF_Page_MoveTo (page, x, y - 15);
HPDF_Page_LineTo (page, x + 220, y - 15);
}
void
draw_line2 (HPDF_Page page,
float x,
float y,
const char *label)
{
HPDF_Page_MoveTextPos (page, x, y);
HPDF_Page_ShowText (page, label);
HPDF_Page_MoveTo (page, x + 30, y - 25);
HPDF_Page_LineTo (page, x + 160, y - 25);
}
void
draw_rect (HPDF_Page page,
double x,
double y,
const char *label)
{
HPDF_Page_MoveTextPos (page, x, y - 10);
HPDF_Page_ShowText (page, label);
HPDF_Page_Rectangle(page, x, y - 40, 220, 25);
}
int main (int argc, char **argv)
{
const char* page_title = "Line Example";
HPDF_Doc pdf;
HPDF_Font font;
HPDF_Page page;
char fname[256];
const HPDF_REAL DASH_MODE1[] = {3};
const HPDF_REAL DASH_MODE2[] = {3, 7};
const HPDF_REAL DASH_MODE3[] = {8, 7, 2, 7};
double x;
double y;
double x1;
double y1;
double x2;
double y2;
double x3;
double y3;
float tw;
strcpy (fname, argv[0]);
strcat (fname, ".pdf");
pdf = HPDF_New (demo_error_handler, NULL);
if (!pdf) {
printf ("error: cannot create PdfDoc object\n");
return 1;
}
if (setjmp(env)) {
HPDF_Free (pdf);
return 1;
}
/* create default-font */
font = HPDF_GetFont (pdf, "Helvetica", NULL);
/* add a new page object. */
page = HPDF_AddPage (pdf);
/* print the lines of the page. */
HPDF_Page_Rectangle (page, 50, 50, HPDF_Page_GetWidth(page) - 100,
HPDF_Page_GetHeight (page) - 110);
/* print the title of the page (with positioning center). */
HPDF_Page_SetFontAndSize (page, font, 24);
tw = HPDF_Page_TextWidth (page, page_title);
HPDF_Page_MoveTextPos (page, (HPDF_Page_GetWidth(page) - tw) / 2,
HPDF_Page_GetHeight (page) - 50);
HPDF_Page_ShowText (page, page_title);
HPDF_Page_SetFontAndSize (page, font, 10);
/* Draw various widths of lines. */
draw_line (page, 60, 770, "line width = 0");
draw_line (page, 60, 740, "line width = 1.0");
draw_line (page, 60, 710, "line width = 2.0");
/* Line dash pattern */
HPDF_Page_SetDash (page, DASH_MODE1, 1, 1);
draw_line (page, 60, 680, "dash_ptn=[3], phase=1 -- "
"2 on, 3 off, 3 on...");
HPDF_Page_SetDash (page, DASH_MODE2, 2, 2);
draw_line (page, 60, 650, "dash_ptn=[7, 3], phase=2 -- "
"5 on 3 off, 7 on,...");
HPDF_Page_SetDash (page, DASH_MODE3, 4, 0);
draw_line (page, 60, 620, "dash_ptn=[8, 7, 2, 7], phase=0");
HPDF_Page_SetDash (page, NULL, 0, 0);
HPDF_Page_SetRGBStroke (page, 0.0, 0.5, 0.0);
/* Line Cap Style */
draw_line2 (page, 60, 570, "PDF_BUTT_END");
draw_line2 (page, 60, 505, "PDF_ROUND_END");
draw_line2 (page, 60, 440, "PDF_PROJECTING_SCUARE_END");
/* Line Join Style */
HPDF_Page_SetRGBStroke (page, 0.0, 0.0, 0.5);
HPDF_Page_MoveTo (page, 120, 300);
HPDF_Page_LineTo (page, 160, 340);
HPDF_Page_LineTo (page, 200, 300);
HPDF_Page_MoveTextPos (page, 60, 360);
HPDF_Page_ShowText (page, "PDF_MITER_JOIN");
HPDF_Page_MoveTo (page, 120, 195);
HPDF_Page_LineTo (page, 160, 235);
HPDF_Page_LineTo (page, 200, 195);
HPDF_Page_MoveTextPos (page, 60, 255);
HPDF_Page_ShowText (page, "PDF_ROUND_JOIN");
HPDF_Page_MoveTo (page, 120, 90);
HPDF_Page_LineTo (page, 160, 130);
HPDF_Page_LineTo (page, 200, 90);
HPDF_Page_MoveTextPos (page, 60, 150);
HPDF_Page_ShowText (page, "PDF_BEVEL_JOIN");
/* Draw Rectangle */
HPDF_Page_SetRGBStroke (page, 0, 0, 0);
HPDF_Page_SetRGBFill (page, 0.75, 0.0, 0.0);
draw_rect (page, 300, 770, "Stroke");
draw_rect (page, 300, 720, "Fill");
draw_rect (page, 300, 670, "Fill then Stroke");
/* Clip Rect */
HPDF_Page_GSave (page); /* Save the current graphic state */
draw_rect (page, 300, 620, "Clip Rectangle");
HPDF_Page_SetFontAndSize (page, font, 13);
HPDF_Page_MoveTextPos (page, 290, 600);
"Clip Clip Clip Clip Clip Clipi Clip Clip Clip");
"Clip Clip Clip Clip Clip Clip Clip Clip Clip");
"Clip Clip Clip Clip Clip Clip Clip Clip Clip");
/* Curve Example(CurveTo2) */
x = 330;
y = 440;
x1 = 430;
y1 = 530;
x2 = 480;
y2 = 470;
x3 = 480;
y3 = 90;
HPDF_Page_SetRGBFill (page, 0, 0, 0);
HPDF_Page_MoveTextPos (page, 300, 540);
HPDF_Page_ShowText (page, "CurveTo2(x1, y1, x2. y2)");
HPDF_Page_MoveTextPos (page, x + 5, y - 5);
HPDF_Page_ShowText (page, "Current point");
HPDF_Page_MoveTextPos (page, x1 - x, y1 - y);
HPDF_Page_ShowText (page, "(x1, y1)");
HPDF_Page_MoveTextPos (page, x2 - x1, y2 - y1);
HPDF_Page_ShowText (page, "(x2, y2)");
HPDF_Page_SetDash (page, DASH_MODE1, 1, 0);
HPDF_Page_MoveTo (page, x1, y1);
HPDF_Page_LineTo (page, x2, y2);
HPDF_Page_SetDash (page, NULL, 0, 0);
HPDF_Page_MoveTo (page, x, y);
HPDF_Page_CurveTo2 (page, x1, y1, x2, y2);
/* Curve Example(CurveTo3) */
y -= 150;
y1 -= 150;
y2 -= 150;
HPDF_Page_MoveTextPos (page, 300, 390);
HPDF_Page_ShowText (page, "CurveTo3(x1, y1, x2. y2)");
HPDF_Page_MoveTextPos (page, x + 5, y - 5);
HPDF_Page_ShowText (page, "Current point");
HPDF_Page_MoveTextPos (page, x1 - x, y1 - y);
HPDF_Page_ShowText (page, "(x1, y1)");
HPDF_Page_MoveTextPos (page, x2 - x1, y2 - y1);
HPDF_Page_ShowText (page, "(x2, y2)");
HPDF_Page_SetDash (page, DASH_MODE1, 1, 0);
HPDF_Page_MoveTo (page, x, y);
HPDF_Page_LineTo (page, x1, y1);
HPDF_Page_SetDash (page, NULL, 0, 0);
HPDF_Page_MoveTo (page, x, y);
HPDF_Page_CurveTo3 (page, x1, y1, x2, y2);
/* Curve Example(CurveTo) */
y -= 150;
y1 -= 160;
y2 -= 130;
x2 += 10;
HPDF_Page_MoveTextPos (page, 300, 240);
HPDF_Page_ShowText (page, "CurveTo(x1, y1, x2. y2, x3, y3)");
HPDF_Page_MoveTextPos (page, x + 5, y - 5);
HPDF_Page_ShowText (page, "Current point");
HPDF_Page_MoveTextPos (page, x1 - x, y1 - y);
HPDF_Page_ShowText (page, "(x1, y1)");
HPDF_Page_MoveTextPos (page, x2 - x1, y2 - y1);
HPDF_Page_ShowText (page, "(x2, y2)");
HPDF_Page_MoveTextPos (page, x3 - x2, y3 - y2);
HPDF_Page_ShowText (page, "(x3, y3)");
HPDF_Page_SetDash (page, DASH_MODE1, 1, 0);
HPDF_Page_MoveTo (page, x, y);
HPDF_Page_LineTo (page, x1, y1);
HPDF_Page_MoveTo (page, x2, y2);
HPDF_Page_LineTo (page, x3, y3);
HPDF_Page_SetDash (page, NULL, 0, 0);
HPDF_Page_MoveTo (page, x, y);
HPDF_Page_CurveTo (page, x1, y1, x2, y2, x3, y3);
/* save the document to a file */
HPDF_SaveToFile (pdf, fname);
/* clean up */
HPDF_Free (pdf);
return 0;
}
HPDF_STATUS HPDF_SaveToFile(HPDF_Doc pdf, const char *filename)
Saves the current document to file.
void HPDF_Free(HPDF_Doc pdf)
Revoke a document object and all resources.
HPDF_Doc HPDF_New(HPDF_Error_Handler user_error_fn, void *user_data)
Create an instance of a document object and initialize it.
HPDF_Font HPDF_GetFont(HPDF_Doc pdf, const char *font_name, const char *encoding_name)
Get requested font object handle.
HPDF_STATUS HPDF_Page_SetLineWidth(HPDF_Page page, HPDF_REAL line_width)
Set width of the line used to stroke paths.
HPDF_STATUS HPDF_Page_CurveTo2(HPDF_Page page, HPDF_REAL x2, HPDF_REAL y2, HPDF_REAL x3, HPDF_REAL y3)
Append Bézier curve to current path using current point and (x2, y2) and (x3, y3) as control points....
HPDF_STATUS HPDF_Page_Stroke(HPDF_Page page)
Paint current path.
HPDF_STATUS HPDF_Page_SetRGBStroke(HPDF_Page page, HPDF_REAL r, HPDF_REAL g, HPDF_REAL b)
Set stroke color (RGB).
HPDF_STATUS HPDF_Page_MoveTo(HPDF_Page page, HPDF_REAL x, HPDF_REAL y)
Start new subpath and move current point for drawing path.
HPDF_STATUS HPDF_Page_ShowTextNextLine(HPDF_Page page, const char *text)
Move current text position to the start of the next line, then put the text at the current text posit...
HPDF_STATUS HPDF_Page_SetRGBFill(HPDF_Page page, HPDF_REAL r, HPDF_REAL g, HPDF_REAL b)
Set filling color (RGB).
HPDF_STATUS HPDF_Page_CurveTo3(HPDF_Page page, HPDF_REAL x1, HPDF_REAL y1, HPDF_REAL x3, HPDF_REAL y3)
Append Bézier curve to the current path using two spesified points. The point (x1,...
HPDF_STATUS HPDF_Page_SetTextLeading(HPDF_Page page, HPDF_REAL value)
Set text leading (line spacing).
HPDF_STATUS HPDF_Page_SetLineCap(HPDF_Page page, HPDF_LineCap line_cap)
Set lines endpoints shape style.
HPDF_STATUS HPDF_Page_GSave(HPDF_Page page)
Save the page's current graphics state to the stack.
HPDF_STATUS HPDF_Page_FillStroke(HPDF_Page page)
Fill current path using the even-odd rule.
HPDF_STATUS HPDF_Page_SetFontAndSize(HPDF_Page page, HPDF_Font font, HPDF_REAL size)
Set the type of font and size leading.
HPDF_STATUS HPDF_Page_Rectangle(HPDF_Page page, HPDF_REAL x, HPDF_REAL y, HPDF_REAL width, HPDF_REAL height)
Append rectangle to the current path.
HPDF_STATUS HPDF_Page_CurveTo(HPDF_Page page, HPDF_REAL x1, HPDF_REAL y1, HPDF_REAL x2, HPDF_REAL y2, HPDF_REAL x3, HPDF_REAL y3)
Append Bézier curve to current path using control points (x1, y1) and (x2, y2) and (x3,...
HPDF_STATUS HPDF_Page_Clip(HPDF_Page page)
Modify current clipping path by intersecting it with current path using the nonzero winding number ru...
HPDF_STATUS HPDF_Page_SetLineJoin(HPDF_Page page, HPDF_LineJoin line_join)
Set line join shape style.
HPDF_STATUS HPDF_Page_Fill(HPDF_Page page)
Fill current path using non-zero winding number rule.
HPDF_STATUS HPDF_Page_SetDash(HPDF_Page page, const HPDF_REAL *dash_pattern, HPDF_UINT num_elem, HPDF_REAL phase)
Set dash pattern for lines in the page.
HPDF_STATUS HPDF_Page_LineTo(HPDF_Page page, HPDF_REAL x, HPDF_REAL y)
Append path from current point to specified point.
HPDF_STATUS HPDF_Page_ShowText(HPDF_Page page, const char *text)
Put text at the current text position on the page.
HPDF_STATUS HPDF_Page_GRestore(HPDF_Page page)
Restore graphics state which is saved by HPDF_Page_GSave().
HPDF_STATUS HPDF_Page_BeginText(HPDF_Page page)
Begin text object and set text position to (0, 0).
HPDF_STATUS HPDF_Page_EndText(HPDF_Page page)
Finish text object.
HPDF_STATUS HPDF_Page_MoveTextPos(HPDF_Page page, HPDF_REAL x, HPDF_REAL y)
Change current text position using the specified offset values.
HPDF_REAL HPDF_Page_TextWidth(HPDF_Page page, const char *text)
Get text width in current font size, character spacing and word spacing.
HPDF_REAL HPDF_Page_GetHeight(HPDF_Page page)
Get page height.
HPDF_Page HPDF_AddPage(HPDF_Doc pdf)
Create new page and add it after the last page of document.
HPDF_REAL HPDF_Page_GetWidth(HPDF_Page page)
Get page width.
@ HPDF_PROJECTING_SQUARE_END
Definition: hpdf_types.h:309
@ HPDF_BUTT_END
Definition: hpdf_types.h:307
@ HPDF_ROUND_END
Definition: hpdf_types.h:308
@ HPDF_BEVEL_JOIN
Definition: hpdf_types.h:319
@ HPDF_MITER_JOIN
Definition: hpdf_types.h:317
@ HPDF_ROUND_JOIN
Definition: hpdf_types.h:318
float HPDF_REAL
Definition: hpdf_types.h:79
Definition: hpdf_objects.h:421
Definition: hpdf_doc.h:36