Haru Free PDF Library
ttfont_demo.c
/*
* << Haru Free PDF Library 2.0.0 >> -- ttfont_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"
int main (int argc, char **argv)
{
const char* SAMP_TXT = "The quick brown fox jumps over the lazy dog.";
HPDF_Doc pdf;
char fname[256];
HPDF_Page page;
HPDF_Font title_font;
HPDF_Font detail_font;
const char *detail_font_name;
HPDF_BOOL embed;
HPDF_REAL page_height;
HPDF_REAL page_width;
if (argc < 2) {
printf("usage: ttfont_demo [path to font file] "
"-E(embedding font).\n");
return 1;
}
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;
}
/* Add a new page object. */
page = HPDF_AddPage (pdf);
title_font = HPDF_GetFont (pdf, "Helvetica", NULL);
if (argc > 2 && memcmp(argv[2], "-E", 2) == 0)
embed = HPDF_TRUE;
else
embed = HPDF_FALSE;
detail_font_name = HPDF_LoadTTFontFromFile (pdf, argv[1], embed);
detail_font = HPDF_GetFont (pdf, detail_font_name, NULL);
HPDF_Page_SetFontAndSize (page, title_font, 10);
/* Move the position of the text to top of the page. */
HPDF_Page_MoveTextPos(page, 10, 190);
HPDF_Page_ShowText (page, detail_font_name);
if (embed)
HPDF_Page_ShowText (page, "(Embedded Subset)");
HPDF_Page_SetFontAndSize (page, detail_font, 15);
HPDF_Page_MoveTextPos (page, 10, -20);
HPDF_Page_ShowText (page, "abcdefghijklmnopqrstuvwxyz");
HPDF_Page_MoveTextPos (page, 0, -20);
HPDF_Page_ShowText (page, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
HPDF_Page_MoveTextPos (page, 0, -20);
HPDF_Page_ShowText (page, "1234567890");
HPDF_Page_MoveTextPos (page, 0, -20);
HPDF_Page_SetFontAndSize (page, detail_font, 10);
HPDF_Page_ShowText (page, SAMP_TXT);
HPDF_Page_MoveTextPos (page, 0, -18);
HPDF_Page_SetFontAndSize (page, detail_font, 16);
HPDF_Page_ShowText (page, SAMP_TXT);
HPDF_Page_MoveTextPos (page, 0, -27);
HPDF_Page_SetFontAndSize (page, detail_font, 23);
HPDF_Page_ShowText (page, SAMP_TXT);
HPDF_Page_MoveTextPos (page, 0, -36);
HPDF_Page_SetFontAndSize (page, detail_font, 30);
HPDF_Page_ShowText (page, SAMP_TXT);
HPDF_Page_MoveTextPos (page, 0, -36);
pw = HPDF_Page_TextWidth (page, SAMP_TXT);
page_height = 210;
page_width = pw + 40;
HPDF_Page_SetWidth (page, page_width);
HPDF_Page_SetHeight (page, page_height);
/* Finish to print text. */
HPDF_Page_MoveTo (page, 10, page_height - 25);
HPDF_Page_LineTo (page, page_width - 10, page_height - 25);
HPDF_Page_MoveTo (page, 10, page_height - 85);
HPDF_Page_LineTo (page, page_width - 10, page_height - 85);
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.
const char * HPDF_LoadTTFontFromFile(HPDF_Doc pdf, const char *filename, HPDF_BOOL embedding)
Load TrueType font from external .ttf file and register it in the document object.
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_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_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_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_Page HPDF_AddPage(HPDF_Doc pdf)
Create new page and add it after the last page of document.
HPDF_STATUS HPDF_Page_SetWidth(HPDF_Page page, HPDF_REAL value)
Change page width.
HPDF_STATUS HPDF_Page_SetHeight(HPDF_Page page, HPDF_REAL value)
Change page height.
#define HPDF_TRUE
Definition: hpdf_consts.h:26
#define HPDF_FALSE
Definition: hpdf_consts.h:27
signed int HPDF_BOOL
Definition: hpdf_types.h:89
float HPDF_REAL
Definition: hpdf_types.h:79
Definition: hpdf_objects.h:421
Definition: hpdf_doc.h:36