Haru Free PDF Library
clip_hatch2.c
Clipping path demo program

This program create hatch pattern using lines, page transform (rotation) and clipping path.

Clipping path is created using HPDF_Page_Rectangle() function. In order to make clipping work properly, HPDF_Page_EndPath() function must be called right after HPDF_Page_Eoclip().

In this example clipping path is complex, constructed as intersections of two rectangles.

/*
* << Haru Free PDF Library 2.4.4 >> -- minimal.c
*
* Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
* Copyright (c) 2023 Dmitry Solomennikov
*
* 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 <stdio.h>
#include <string.h>
#include <math.h>
#include <setjmp.h>
#include "hpdf.h"
#include "handler.h"
void
hatch (HPDF_Doc pdf,
HPDF_Page page)
{
HPDF_REAL height = HPDF_Page_GetHeight (page);
HPDF_REAL width = HPDF_Page_GetWidth (page);
HPDF_REAL x = 0.0;
/* start clipping */
/* prepare clipping path (rectangle with a hole) */
HPDF_Page_Rectangle (page, 0, 0, width, 15 * HPDF_MM);
HPDF_Page_Rectangle (page, 5*HPDF_MM, 5*HPDF_MM, width-10*HPDF_MM, 5 * HPDF_MM);
/* use created path as clipping path (even-odd rule) */
/* Finish path.
This action instructs viewer that path is created and can be used as clipping path.
It is required since it is possible to create complicated clipping path.
For example, we can add another rectangle and its union will be used as clipping path.
*/
/* start hatch drawing */
HPDF_Page_SetRGBStrokeHex (page, 0x55, 0x77, 0x22);
HPDF_REAL angle1 = -45;
HPDF_REAL rad1 = angle1 / 180 * HPDF_PI;
/* rotate hatch */
HPDF_Page_Concat (page, cos(rad1), sin(rad1), -sin(rad1), cos(rad1), -15*HPDF_MM, 0);
while (x < width) {
HPDF_Page_MoveTo (page, x, 0);
HPDF_Page_LineTo (page, x, height);
x += HPDF_MM;
}
/* end of hatch drawing */
/* end of clipping */
}
int
main (int argc, char **argv)
{
HPDF_Doc pdf;
HPDF_Page page;
char fname[256];
strcpy (fname, argv[0]);
strcat (fname, ".pdf");
/* create document object*/
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;
}
/* add a new page object. */
page = HPDF_AddPage (pdf);
/* set up added page */
hatch (pdf, page);
/* save 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_STATUS HPDF_Page_EndPath(HPDF_Page page)
Finish path object without filling or painting.
HPDF_STATUS HPDF_Page_SetRGBStrokeHex(HPDF_Page page, HPDF_UINT8 r, HPDF_UINT8 g, HPDF_UINT8 b)
Set stroke color (RGB) using HPDF_UINT8 values.
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_Stroke(HPDF_Page page)
Paint current path.
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_Eoclip(HPDF_Page page)
Modifies the current clipping path by intersecting it with current path using the even-odd rule....
HPDF_STATUS HPDF_Page_GSave(HPDF_Page page)
Save the page's current graphics state to the stack.
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_Concat(HPDF_Page page, HPDF_REAL a, HPDF_REAL b, HPDF_REAL c, HPDF_REAL d, HPDF_REAL x, HPDF_REAL y)
Concatenate the page's transformation matrix and specified matrix.
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_GRestore(HPDF_Page page)
Restore graphics state which is saved by HPDF_Page_GSave().
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_STATUS HPDF_Page_SetSize(HPDF_Page page, HPDF_PageSizes size, HPDF_PageDirection direction)
Change page size and direction to a predefined ones.
HPDF_REAL HPDF_Page_GetWidth(HPDF_Page page)
Get page width.
#define HPDF_MM
Predefined value for calculations with millimeters. Equals to 72.0/25.4 (72 points pre inch per mm).
Definition: hpdf_consts.h:178
#define HPDF_PI
Definition: hpdf_consts.h:32
@ HPDF_PAGE_SIZE_A4
ISO 216 "A4" page size (210.0mm x 297.0mm)
Definition: hpdf_page_sizes.h:137
float HPDF_REAL
Definition: hpdf_types.h:79
@ HPDF_PAGE_PORTRAIT
Portrait orientation (longest size vertical)
Definition: hpdf_types.h:575
Definition: hpdf_objects.h:421
Definition: hpdf_doc.h:36