Haru Free PDF Library
encoding_list.c
/*
* << Haru Free PDF Library 2.0.0 >> -- encoding_list.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 <setjmp.h>
#include "hpdf.h"
#include "handler.h"
#include "utils.h"
static const int PAGE_WIDTH = 420;
static const int PAGE_HEIGHT = 400;
static const int CELL_WIDTH = 20;
static const int CELL_HEIGHT = 20;
void
draw_graph (HPDF_Page page);
void
draw_fonts (HPDF_Page page);
void
draw_graph (HPDF_Page page)
{
char buf[50];
int i;
/* Draw 16 X 15 cells */
/* Draw vertical lines. */
for (i = 0; i <= 17; i++) {
int x = i * CELL_WIDTH + 40;
HPDF_Page_MoveTo (page, x, PAGE_HEIGHT - 60);
HPDF_Page_LineTo (page, x, 40);
if (i > 0 && i <= 16) {
HPDF_Page_MoveTextPos (page, x + 5, PAGE_HEIGHT - 75);
HPDF_snprintf(buf, 5, "%X", i - 1);
HPDF_Page_ShowText (page, buf);
}
}
/* Draw horizontal lines. */
for (i = 0; i <= 15; i++) {
int y = i * CELL_HEIGHT + 40;
HPDF_Page_MoveTo (page, 40, y);
HPDF_Page_LineTo (page, PAGE_WIDTH - 40, y);
if (i < 14) {
HPDF_Page_MoveTextPos (page, 45, y + 5);
HPDF_snprintf(buf, 5, "%X", 15 - i);
HPDF_Page_ShowText (page, buf);
}
}
}
void
draw_fonts (HPDF_Page page)
{
int i;
int j;
/* Draw all character from 0x20 to 0xFF to the canvas. */
for (i = 1; i < 17; i++) {
for (j = 1; j < 17; j++) {
unsigned char buf[2];
int y = PAGE_HEIGHT - 55 - ((i - 1) * CELL_HEIGHT);
int x = j * CELL_WIDTH + 50;
buf[1] = 0x00;
buf[0] = (i - 1) * 16 + (j - 1);
if (buf[0] >= 32) {
double d;
d = x - HPDF_Page_TextWidth (page, (char*)buf) / 2;
HPDF_Page_TextOut (page, d, y, (char*)buf);
}
}
}
}
int main (int argc, char **argv)
{
HPDF_Doc pdf;
char fname[256];
HPDF_Font font;
const char *font_name;
int i = 0;
const char *encodings[] = {
"StandardEncoding",
"MacRomanEncoding",
"WinAnsiEncoding",
"ISO8859-2",
"ISO8859-3",
"ISO8859-4",
"ISO8859-5",
"ISO8859-9",
"ISO8859-10",
"ISO8859-13",
"ISO8859-14",
"ISO8859-15",
"ISO8859-16",
"CP1250",
"CP1251",
"CP1252",
"CP1254",
"CP1257",
"KOI8-R",
"Symbol-Set",
"ZapfDingbats-Set",
NULL
};
pdf = HPDF_NewEx (demo_error_handler, NULL, NULL, 0, NULL);
if (!pdf) {
printf ("error: cannot create PdfDoc object\n");
return 1;
}
if (setjmp(env)) {
HPDF_Free (pdf);
return 1;
}
strcpy (fname, argv[0]);
strcat (fname, ".pdf");
/* set compression mode */
/* Set page mode to use outlines. */
/* get default font */
font = HPDF_GetFont (pdf, "Helvetica", NULL);
/* load font object */
#ifdef __WIN32__
font_name = HPDF_LoadType1FontFromFile (pdf, "type1\\a010013l.afm",
"type1\\a010013l.pfb");
#else
font_name = HPDF_LoadType1FontFromFile (pdf, "type1/a010013l.afm",
"type1/a010013l.pfb");
#endif
/* create outline root. */
root = HPDF_CreateOutline (pdf, NULL, "Encoding list", NULL);
while (encodings[i]) {
HPDF_Page page = HPDF_AddPage (pdf);
HPDF_Outline outline;
HPDF_Font font2;
HPDF_Page_SetWidth (page, PAGE_WIDTH);
HPDF_Page_SetHeight (page, PAGE_HEIGHT);
outline = HPDF_CreateOutline (pdf, root, encodings[i], NULL);
/* HPDF_Destination_SetFitB(dst); */
HPDF_Page_SetFontAndSize (page, font, 15);
draw_graph (page);
HPDF_Page_SetFontAndSize (page, font, 20);
HPDF_Page_MoveTextPos (page, 40, PAGE_HEIGHT - 50);
HPDF_Page_ShowText (page, encodings[i]);
HPDF_Page_ShowText (page, " Encoding");
if (strcmp (encodings[i], "Symbol-Set") == 0)
font2 = HPDF_GetFont (pdf, "Symbol", NULL);
else if (strcmp (encodings[i], "ZapfDingbats-Set") == 0)
font2 = HPDF_GetFont (pdf, "ZapfDingbats", NULL);
else
font2 = HPDF_GetFont (pdf, font_name, encodings[i]);
HPDF_Page_SetFontAndSize (page, font2, 14);
draw_fonts (page);
i++;
}
/* 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_NewEx(HPDF_Error_Handler user_error_fn, HPDF_Alloc_Func user_alloc_fn, HPDF_Free_Func user_free_fn, HPDF_UINT mem_pool_buf_size, 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.
const char * HPDF_LoadType1FontFromFile(HPDF_Doc pdf, const char *afm_filename, const char *data_filename)
Load Type1 font from external file and register it in the document object.
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_TextOut(HPDF_Page page, HPDF_REAL xpos, HPDF_REAL ypos, const char *text)
Put text to the specified position.
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_Outline HPDF_CreateOutline(HPDF_Doc pdf, HPDF_Outline parent, const char *title, HPDF_Encoder encoder)
Create new outline object.
HPDF_STATUS HPDF_Outline_SetOpened(HPDF_Outline outline, HPDF_BOOL opened)
Set whether this outline node is opened when the outline is displayed for the first time.
HPDF_STATUS HPDF_Outline_SetDestination(HPDF_Outline outline, HPDF_Destination dst)
Set target destination object to jump to when outline object is clicked.
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_STATUS HPDF_SetPageMode(HPDF_Doc pdf, HPDF_PageMode mode)
Set document display mode.
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.
HPDF_STATUS HPDF_SetCompressionMode(HPDF_Doc pdf, HPDF_UINT mode)
Set compression mode.
#define HPDF_COMP_ALL
Definition: hpdf_consts.h:82
#define HPDF_TRUE
Definition: hpdf_consts.h:26
@ HPDF_PAGE_MODE_USE_OUTLINE
Definition: hpdf_types.h:362
Definition: hpdf_objects.h:333
Definition: hpdf_objects.h:421
Definition: hpdf_doc.h:36