Haru Free PDF Library
text_demo.c
Text demo

This demo app shows various ways to create text using libHaru:

/*
* << Haru Free PDF Library 2.0.0 >> -- text_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 <math.h>
#include "hpdf.h"
#include "grid_sheet.h"
#include "handler.h"
#include "utils.h"
void
show_stripe_pattern (HPDF_Page page,
{
HPDF_UINT iy = 0;
while (iy < 50) {
HPDF_Page_SetRGBStroke (page, 0.0, 0.0, 0.5);
HPDF_Page_MoveTo (page, x, y + iy);
HPDF_Page_LineTo (page, x + HPDF_Page_TextWidth (page, "ABCabc123"),
y + iy);
iy += 3;
}
}
void
show_description (HPDF_Page page,
const char *text)
{
float fsize = HPDF_Page_GetCurrentFontSize (page);
HPDF_Page_SetRGBFill (page, 0, 0, 0);
HPDF_Page_SetFontAndSize (page, font, 10);
HPDF_Page_TextOut (page, x, y - 12, text);
HPDF_Page_SetFontAndSize (page, font, fsize);
HPDF_Page_SetRGBFill (page, c.r, c.g, c.b);
}
int main (int argc, char **argv)
{
const char *page_title = "Text Demo";
HPDF_Doc pdf;
HPDF_Font font;
HPDF_Page page;
char fname[256];
const char* samp_text = "abcdefgABCDEFG123!#$%&+-@?";
const char* samp_text2 = "The quick brown fox jumps over the lazy dog.";
float tw;
float fsize;
int i;
int len;
float angle1;
float angle2;
float rad1;
float rad2;
float ypos;
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;
}
/* set compression mode */
/* create default-font */
font = HPDF_GetFont (pdf, "Helvetica", NULL);
/* add a new page object. */
page = HPDF_AddPage (pdf);
/* draw grid to the page */
print_grid (pdf, page);
/* print the lines of the page.
HPDF_Page_SetLineWidth (page, 1);
HPDF_Page_Rectangle (page, 50, 50, HPDF_Page_GetWidth(page) - 100,
HPDF_Page_GetHeight (page) - 110);
HPDF_Page_Stroke (page);
*/
/* print the title of the page (with positioning center). */
HPDF_Page_SetFontAndSize (page, font, 24);
tw = HPDF_Page_TextWidth (page, page_title);
HPDF_Page_TextOut (page, (HPDF_Page_GetWidth(page) - tw) / 2,
HPDF_Page_GetHeight (page) - 50, page_title);
/*
* font size
*/
fsize = 8;
while (fsize < 60) {
char buf[50];
int len;
/* set style and size of font. */
HPDF_Page_SetFontAndSize(page, font, fsize);
/* set the position of the text. */
HPDF_Page_MoveTextPos (page, 0, -5 - fsize);
/* measure the number of characters which included in the page. */
strcpy(buf, samp_text);
len = HPDF_Page_MeasureText (page, samp_text,
HPDF_Page_GetWidth(page) - 120, HPDF_FALSE, NULL);
/* truncate the text. */
buf[len] = 0x00;
HPDF_Page_ShowText (page, buf);
/* print the description. */
HPDF_Page_MoveTextPos (page, 0, -10);
HPDF_Page_SetFontAndSize(page, font, 8);
HPDF_snprintf(buf, 50, "Fontsize=%.0f", fsize);
HPDF_Page_ShowText (page, buf);
fsize *= 1.5;
}
/*
* font color
*/
HPDF_Page_SetFontAndSize(page, font, 8);
HPDF_Page_MoveTextPos (page, 0, -30);
HPDF_Page_ShowText (page, "Font color");
HPDF_Page_SetFontAndSize (page, font, 18);
HPDF_Page_MoveTextPos (page, 0, -20);
len = strlen (samp_text);
for (i = 0; i < len; i++) {
char buf[2];
float r = (float)i / (float)len;
float g = 1 - ((float)i / (float)len);
buf[0] = samp_text[i];
buf[1] = 0x00;
HPDF_Page_SetRGBFill (page, r, g, 0.0);
HPDF_Page_ShowText (page, buf);
}
HPDF_Page_MoveTextPos (page, 0, -25);
for (i = 0; i < len; i++) {
char buf[2];
float r = (float)i / (float)len;
float b = 1 - ((float)i / (float)len);
buf[0] = samp_text[i];
buf[1] = 0x00;
HPDF_Page_SetRGBFill (page, r, 0.0, b);
HPDF_Page_ShowText (page, buf);
}
HPDF_Page_MoveTextPos (page, 0, -25);
for (i = 0; i < len; i++) {
char buf[2];
float b = (float)i / (float)len;
float g = 1 - ((float)i / (float)len);
buf[0] = samp_text[i];
buf[1] = 0x00;
HPDF_Page_SetRGBFill (page, 0.0, g, b);
HPDF_Page_ShowText (page, buf);
}
ypos = 450;
/*
* Font rendering mode
*/
HPDF_Page_SetFontAndSize(page, font, 32);
HPDF_Page_SetRGBFill (page, 0.5, 0.5, 0.0);
/* PDF_FILL */
show_description (page, 60, ypos,
"RenderingMode=PDF_FILL");
HPDF_Page_TextOut (page, 60, ypos, "ABCabc123");
/* PDF_STROKE */
show_description (page, 60, ypos - 50,
"RenderingMode=PDF_STROKE");
HPDF_Page_TextOut (page, 60, ypos - 50, "ABCabc123");
/* PDF_FILL_THEN_STROKE */
show_description (page, 60, ypos - 100,
"RenderingMode=PDF_FILL_THEN_STROKE");
HPDF_Page_TextOut (page, 60, ypos - 100, "ABCabc123");
/* PDF_FILL_CLIPPING */
show_description (page, 60, ypos - 150,
"RenderingMode=PDF_FILL_CLIPPING");
HPDF_Page_TextOut (page, 60, ypos - 150, "ABCabc123");
show_stripe_pattern (page, 60, ypos - 150);
/* PDF_STROKE_CLIPPING */
show_description (page, 60, ypos - 200,
"RenderingMode=PDF_STROKE_CLIPPING");
HPDF_Page_TextOut (page, 60, ypos - 200, "ABCabc123");
show_stripe_pattern (page, 60, ypos - 200);
/* PDF_FILL_STROKE_CLIPPING */
show_description (page, 60, ypos - 250,
"RenderingMode=PDF_FILL_STROKE_CLIPPING");
HPDF_Page_TextOut (page, 60, ypos - 250, "ABCabc123");
show_stripe_pattern (page, 60, ypos - 250);
/* Reset text attributes */
HPDF_Page_SetRGBFill (page, 0, 0, 0);
HPDF_Page_SetFontAndSize(page, font, 30);
/*
* Rotating text
*/
angle1 = 30; /* A rotation of 30 degrees. */
rad1 = angle1 / 180 * HPDF_PI; /* Calculate the radian value. */
show_description (page, 320, ypos - 60, "Rotating text");
HPDF_Page_SetTextMatrix (page, cos(rad1), sin(rad1), -sin(rad1), cos(rad1),
330, ypos - 60);
HPDF_Page_ShowText (page, "ABCabc123");
/*
* Skewing text.
*/
show_description (page, 320, ypos - 120, "Skewing text");
angle1 = 10;
angle2 = 20;
rad1 = angle1 / 180 * HPDF_PI;
rad2 = angle2 / 180 * HPDF_PI;
HPDF_Page_SetTextMatrix (page, 1, tan(rad1), tan(rad2), 1, 320, ypos - 120);
HPDF_Page_ShowText (page, "ABCabc123");
/*
* scaling text (X direction)
*/
show_description (page, 320, ypos - 175, "Scaling text (X direction)");
HPDF_Page_SetTextMatrix (page, 1.5, 0, 0, 1, 320, ypos - 175);
HPDF_Page_ShowText (page, "ABCabc12");
/*
* scaling text (Y direction)
*/
show_description (page, 320, ypos - 250, "Scaling text (Y direction)");
HPDF_Page_SetTextMatrix (page, 1, 0, 0, 2, 320, ypos - 250);
HPDF_Page_ShowText (page, "ABCabc123");
/*
* char spacing, word spacing
*/
show_description (page, 60, 140, "char-spacing 0");
show_description (page, 60, 100, "char-spacing 1.5");
show_description (page, 60, 60, "char-spacing 1.5, word-spacing 2.5");
HPDF_Page_SetFontAndSize (page, font, 20);
HPDF_Page_SetRGBFill (page, 0.1, 0.3, 0.1);
/* char-spacing 0 */
HPDF_Page_TextOut (page, 60, 140, samp_text2);
/* char-spacing 1.5 */
HPDF_Page_TextOut (page, 60, 100, samp_text2);
/* char-spacing 1.5, word-spacing 3.5 */
HPDF_Page_TextOut (page, 60, 60, samp_text2);
/* 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_SetCharSpace(HPDF_Page page, HPDF_REAL value)
Set text character spacing.
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_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_SetRGBFill(HPDF_Page page, HPDF_REAL r, HPDF_REAL g, HPDF_REAL b)
Set filling 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_SetWordSpace(HPDF_Page page, HPDF_REAL value)
Set text word spacing.
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_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_SetTextRenderingMode(HPDF_Page page, HPDF_TextRenderingMode mode)
Set text rendering mode.
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_UINT HPDF_Page_MeasureText(HPDF_Page page, const char *text, HPDF_REAL width, HPDF_BOOL wordwrap, HPDF_REAL *real_width)
Calculate byte length which can be included within the specified width.
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_Font HPDF_Page_GetCurrentFont(HPDF_Page page)
Get page current font handle.
HPDF_REAL HPDF_Page_GetCurrentFontSize(HPDF_Page page)
Get page current font size.
HPDF_RGBColor HPDF_Page_GetRGBFill(HPDF_Page page)
Get page current filling color value (RGB).
HPDF_STATUS HPDF_SetCompressionMode(HPDF_Doc pdf, HPDF_UINT mode)
Set compression mode.
HPDF_STATUS HPDF_Page_SetTextMatrix(HPDF_Page page, HPDF_REAL a, HPDF_REAL b, HPDF_REAL c, HPDF_REAL d, HPDF_REAL x, HPDF_REAL y)
#define HPDF_COMP_ALL
Definition: hpdf_consts.h:82
#define HPDF_FALSE
Definition: hpdf_consts.h:27
#define HPDF_PI
Definition: hpdf_consts.h:32
unsigned int HPDF_UINT
Definition: hpdf_types.h:45
float HPDF_REAL
Definition: hpdf_types.h:79
@ HPDF_FILL
Definition: hpdf_types.h:327
@ HPDF_STROKE_CLIPPING
Definition: hpdf_types.h:332
@ HPDF_FILL_STROKE_CLIPPING
Definition: hpdf_types.h:333
@ HPDF_FILL_THEN_STROKE
Definition: hpdf_types.h:329
@ HPDF_STROKE
Definition: hpdf_types.h:328
@ HPDF_FILL_CLIPPING
Definition: hpdf_types.h:331
Definition: hpdf_objects.h:421
Definition: hpdf_doc.h:36
Definition: hpdf_types.h:287
HPDF_REAL r
Definition: hpdf_types.h:288
HPDF_REAL b
Definition: hpdf_types.h:290
HPDF_REAL g
Definition: hpdf_types.h:289