Haru Free PDF Library
matrix.c
Matrix transformations combined demo program.

This program shows usage of matrix multiplication. It allows to produce some delayed and combined calculations on matrices.

Please pay attention, matrix combination operations and page operations are in reverse order to each other.

/*
* << Haru Free PDF Library 2.4.4 >> -- matrix.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 "hpdf_utils.h"
#include "handler.h"
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 */
/* draw line using page transformation */
HPDF_Page_Scale (page, 0.5, 0.5);
HPDF_Page_Rotate (page, -35*HPDF_PI/180);
HPDF_Page_MoveTo (page, 0, 0);
/* draw line using matrix calculation */
HPDF_TransMatrix r = IDENTITY_MATRIX;
r = HPDF_Matrix_RotateDeg (r, -45);
r = HPDF_Matrix_Scale (r, 0.5, 0.5);
HPDF_Page_Concat (page, r.a, r.b, r.c, r.d, r.x, r.y);
HPDF_Page_MoveTo (page, 0, 0);
HPDF_Page_LineTo (page, 0, 50*HPDF_MM);
/* draw center rectangles */
HPDF_Page_Rectangle (page, 25*HPDF_MM-2.5, 25*HPDF_MM-2.5, 5, 5);
HPDF_Page_Rectangle (page, 25*HPDF_MM-2.5, 50*HPDF_MM-2.5, 5, 5);
/* 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_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_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_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_Translate(HPDF_Page page, HPDF_REAL dx, HPDF_REAL dy)
Concatenate the page's transformation matrix with translation 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_STATUS HPDF_Page_Rotate(HPDF_Page page, HPDF_REAL a)
Concatenate the page's transformation matrix with rotate matrix.
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
@ HPDF_PAGE_LANDSCAPE
Landscape orientation (longest size horizontal)
Definition: hpdf_types.h:577
HPDF_TransMatrix HPDF_Matrix_Translate(HPDF_TransMatrix m, HPDF_REAL dx, HPDF_REAL dy)
HPDF_TransMatrix HPDF_Matrix_RotateDeg(HPDF_TransMatrix m, HPDF_REAL degrees)
HPDF_TransMatrix HPDF_Matrix_Scale(HPDF_TransMatrix m, HPDF_REAL sx, HPDF_REAL sy)
Definition: hpdf_objects.h:421
Definition: hpdf_doc.h:36
Definition: hpdf_types.h:240
HPDF_REAL b
Definition: hpdf_types.h:242
HPDF_REAL y
Definition: hpdf_types.h:246
HPDF_REAL a
Definition: hpdf_types.h:241
HPDF_REAL d
Definition: hpdf_types.h:244
HPDF_REAL c
Definition: hpdf_types.h:243
HPDF_REAL x
Definition: hpdf_types.h:245