Haru Free PDF Library
transforms.c
Transformation helper functions

This program show usage of helper functions for coordinate system transformations. It creates transformation matrices for translate, scale, rotate and skew. For this purpose it uses HPDF_Page_Translate(), HPDF_Page_Scale(), HPDF_Page_Rotate() and HPDF_Page_Skew() respectively.

Internally all these functions use HPDF_Page_Concat() for transformation matrix manipulation.

/*
* << Haru Free PDF Library 2.4.4 >> -- transforms.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 <setjmp.h>
#include "hpdf.h"
#include "handler.h"
const HPDF_REAL rect_height = 50*HPDF_MM;
const HPDF_REAL rect_width = 50*HPDF_MM;
const HPDF_REAL line_width = 0.2*HPDF_MM;
const HPDF_REAL font_size = 14;
const char *captions[] = {
"Translate",
"Scale",
"Rotate",
"Skew",
"Combined"
};
/* Draw original rectangle */
void draw_rect (HPDF_Page page)
{
HPDF_Page_SetLineWidth (page, line_width);
HPDF_Page_SetRGBStroke (page, 0, 0, 0);
HPDF_Page_SetDash (page, NULL, 0, 0);
HPDF_Page_Rectangle (page, 0, 0, rect_width, rect_height);
}
/* Draw rectangle after transformation */
void draw_transformed (HPDF_Page page)
{
HPDF_Page_SetLineWidth (page, line_width);
HPDF_Page_SetRGBStroke (page, 1.0, 0, 1.0);
const HPDF_REAL dash[] = {7};
HPDF_Page_SetDash (page, dash, 1, 1);
HPDF_Page_Rectangle (page, 0, 0, rect_width, rect_height);
}
/* Draw transformation caption */
void draw_caption (HPDF_Page page, int index)
{
HPDF_Page_TextOut (page, 0, -1.5*font_size, captions[index]);
}
int
main (int argc, char **argv)
{
HPDF_Doc pdf;
HPDF_Page page;
HPDF_Font font;
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 */
/* create default-font */
font = HPDF_GetFont (pdf, "Helvetica", NULL);
HPDF_Page_SetFontAndSize (page, font, font_size);
/* calculate rectangles positions */
HPDF_REAL page_height = HPDF_Page_GetHeight (page);
HPDF_REAL x[] = {20*HPDF_MM,
90*HPDF_MM,
20*HPDF_MM,
90*HPDF_MM,
160*HPDF_MM};
HPDF_REAL y[] = {page_height - rect_height - 20*HPDF_MM,
page_height - rect_height - 20*HPDF_MM,
page_height - rect_height - 90*HPDF_MM,
page_height - rect_height - 90*HPDF_MM,
page_height - rect_height - 90*HPDF_MM};
/* translate transformation */
HPDF_Page_Translate (page, x[0], y[0]);
draw_rect (page);
draw_transformed (page);
draw_caption (page, 0);
/* scale transformation */
HPDF_Page_Translate (page, x[1], y[1]);
draw_rect (page);
HPDF_Page_Scale (page, 0.8, 1.2);
draw_transformed (page);
draw_caption (page, 1);
/* rotate transformation */
HPDF_Page_Translate (page, x[2], y[2]);
draw_rect (page);
draw_transformed (page);
draw_caption (page, 2);
/* skew transformation */
HPDF_Page_Translate (page, x[3], y[3]);
draw_rect (page);
HPDF_Page_Skew (page, HPDF_PI/20, HPDF_PI/20);
draw_transformed (page);
draw_caption (page, 3);
/* combined transformation */
HPDF_Page_Translate (page, x[4], y[4]);
draw_rect (page);
HPDF_Page_Skew (page, HPDF_PI/20, HPDF_PI/20);
HPDF_Page_Scale (page, 0.8, 1.2);
draw_transformed (page);
draw_caption (page, 4);
/* 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_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_Skew(HPDF_Page page, HPDF_REAL a, HPDF_REAL b)
Concatenate the page's transformation matrix with skew matrix.
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_TextOut(HPDF_Page page, HPDF_REAL xpos, HPDF_REAL ypos, const char *text)
Put text to the specified position.
HPDF_STATUS HPDF_Page_Scale(HPDF_Page page, HPDF_REAL sx, HPDF_REAL sy)
Concatenate the page's transformation matrix with scale matrix.
HPDF_STATUS HPDF_Page_GSave(HPDF_Page page)
Save the page's current graphics state to the stack.
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_Translate(HPDF_Page page, HPDF_REAL dx, HPDF_REAL dy)
Concatenate the page's transformation matrix with translation matrix.
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_GRestore(HPDF_Page page)
Restore graphics state which is saved by HPDF_Page_GSave().
HPDF_STATUS HPDF_Page_Rotate(HPDF_Page page, HPDF_REAL a)
Concatenate the page's transformation matrix with rotate matrix.
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_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.
#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_LANDSCAPE
Landscape orientation (longest size horizontal)
Definition: hpdf_types.h:577
Definition: hpdf_objects.h:421
Definition: hpdf_doc.h:36