Haru Free PDF Library
character_map.c
/*
* << Haru Free PDF Library 2.0.0 >> -- character_map.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.
*
* usage character_map <encoding-name> <low-range-from> <low-range-to>
* <high-range-from> <high-range-to>
* ex. character_map 90ms-RKSJ-V 0x80 0x
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <setjmp.h>
#include "hpdf.h"
#include "handler.h"
#include "utils.h"
void
draw_page (HPDF_Doc pdf,
HPDF_Page page,
HPDF_Font title_font,
HPDF_Font font,
HPDF_BYTE h_byte,
HPDF_BYTE l_byte)
{
const int PAGE_WIDTH = 420;
const int CELL_HEIGHT = 20;
const int CELL_WIDTH = 20;
unsigned int h_count;
int xpos, ypos;
unsigned int page_height;
l_byte = (int)(l_byte / 16) * 16;
h_count = 16 - (l_byte / 16);
page_height = 40 + 40 + (h_count + 1) * CELL_HEIGHT;
HPDF_Page_SetHeight (page, page_height);
HPDF_Page_SetWidth (page, PAGE_WIDTH);
HPDF_Page_SetFontAndSize (page, title_font, 10);
ypos = h_count + 1;
for (;;) {
int y = (ypos) * CELL_HEIGHT + 40;
HPDF_Page_MoveTo (page, 40, y);
HPDF_Page_LineTo (page, 380, y);
if (ypos < h_count) {
unsigned char buf[2];
double w;
buf[0] = 16 - ypos - 1;
if (buf[0] < 10)
buf[0] += '0';
else
buf[0] += ('A' - 10);
buf[1] = 0;
w = HPDF_Page_TextWidth (page, (char*)buf);
HPDF_Page_MoveTextPos (page, 40 + ((double)20 - w) / 2, y + 5);
HPDF_Page_ShowText (page, (char*)buf);
}
if (ypos == 0)
break;
ypos--;
}
for (xpos = 0; xpos <= 17; xpos++) {
int y = (h_count + 1) * CELL_HEIGHT + 40;
int x = xpos * CELL_WIDTH + 40;
HPDF_Page_MoveTo (page, x, 40);
HPDF_Page_LineTo (page, x, y);
if (xpos > 0 && xpos <= 16) {
unsigned char buf[2];
double w;
buf[0] = xpos - 1;
if (buf[0] < 10)
buf[0] += '0';
else
buf[0] += ('A' - 10);
buf[1] = 0;
w = HPDF_Page_TextWidth(page, (char*)buf);
HPDF_Page_MoveTextPos(page, x + ((double)20 - w) / 2,
h_count * CELL_HEIGHT + 45);
HPDF_Page_ShowText(page, (char*)buf);
}
}
HPDF_Page_SetFontAndSize (page, font, 15);
ypos = h_count;
for (;;) {
int y = (ypos - 1) * CELL_HEIGHT + 45;
for (xpos = 0; xpos < 16; xpos++) {
unsigned char buf[3];
double w;
int x = xpos * CELL_WIDTH + 40 + CELL_WIDTH;
buf[0] = h_byte;
buf[1] = (16 - ypos) * 16 + xpos;
buf[2] = 0x00;
w = HPDF_Page_TextWidth(page, (char*)buf);
if (w > 0) {
HPDF_Page_MoveTextPos(page, x + ((double)20 - w) / 2, y);
HPDF_Page_ShowText(page, (char*)buf);
}
}
if (ypos == 0)
break;
ypos--;
}
}
int
main (int argc,
char **argv)
{
unsigned int i, j;
unsigned int min_l, max_l, min_h, max_h;
char fname[256];
HPDF_UINT16 flg[256];
HPDF_Doc pdf;
HPDF_Encoder encoder;
HPDF_Font font;
strcpy (fname, argv[0]);
strcat (fname, ".pdf");
if (argc < 3) {
printf ("usage: character_map <encoding-name> <font-name>\n");
return 1;
}
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;
}
/* configure pdf-document (showing outline, compression enabled) */
encoder = HPDF_GetEncoder (pdf, argv[1]);
printf ("error: %s is not cmap-encoder\n", argv[1]);
HPDF_Free (pdf);
return 1;
}
font = HPDF_GetFont (pdf, argv[2], argv[1]);
min_l = 255;
min_h = 256;
max_l = 0;
max_h = 0;
for (i = 0; i <= 255; i++) {
flg[i] = 0;
}
for (i = 0; i <= 255; i++) {
for (j = 20; j <= 255; j++) {
unsigned char buf[3];
HPDF_UINT16 code = i * 256 + j;
HPDF_UNICODE unicode;
buf[0] = i;
buf[1] = j;
buf[2] = 0;
btype = HPDF_Encoder_GetByteType (encoder, (char*)buf, 0);
unicode = HPDF_Encoder_GetUnicode (encoder, code);
if (btype == HPDF_BYTE_TYPE_LEAD &&
unicode != 0x25A1) {
if (min_l > j)
min_l = j;
if (max_l < j)
max_l = j;
if (min_h > i)
min_h = i;
if (max_h < i)
max_h = i;
flg[i] = 1;
}
}
}
printf ("min_h=%04X max_h=%04X min_l=%04X max_l=%04X\n",
min_h, max_h, min_l, max_l);
/* create outline root. */
root = HPDF_CreateOutline (pdf, NULL, argv[1], NULL);
for (i = 0; i <= 255; i++) {
if (flg[i]) {
char buf[256];
HPDF_Page page = HPDF_AddPage (pdf);
HPDF_Font title_font = HPDF_GetFont (pdf, "Helvetica", NULL);
HPDF_Outline outline;
HPDF_snprintf (buf, 256, "0x%04X-0x%04X",
(unsigned int)(i * 256 + min_l),
(unsigned int)(i * 256 + max_l));
outline = HPDF_CreateOutline (pdf, root, buf, NULL);
draw_page (pdf, page, title_font, font, (HPDF_BYTE)i, (HPDF_BYTE)min_l);
HPDF_snprintf (buf, 256, "%s (%s) 0x%04X-0x%04X", argv[1], argv[2],
(unsigned int)(i * 256 + min_l),
(unsigned int)(i * 256 + max_l));
HPDF_Page_SetFontAndSize (page, title_font, 10);
HPDF_Page_MoveTextPos (page, 40, HPDF_Page_GetHeight (page) - 35);
HPDF_Page_ShowText (page, buf);
}
}
HPDF_SaveToFile (pdf, fname);
HPDF_Free (pdf);
return 0;
}
HPDF_STATUS HPDF_SetPagesConfiguration(HPDF_Doc pdf, HPDF_UINT page_per_pages)
Specify number of pages Pages object can own.
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_UseCNSEncodings(HPDF_Doc pdf)
Enable simplified Chinese encodings.
HPDF_Encoder HPDF_GetEncoder(HPDF_Doc pdf, const char *encoding_name)
Get encoder object handle by specified encoding name.
HPDF_ByteType HPDF_Encoder_GetByteType(HPDF_Encoder encoder, const char *text, HPDF_UINT index)
Get byte type of a text char at index position.
HPDF_STATUS HPDF_UseKREncodings(HPDF_Doc pdf)
Enable Korean encodings.
HPDF_EncoderType HPDF_Encoder_GetType(HPDF_Encoder encoder)
Get encoder object type.
HPDF_STATUS HPDF_UseJPEncodings(HPDF_Doc pdf)
Enable Japanese encodings.
HPDF_UNICODE HPDF_Encoder_GetUnicode(HPDF_Encoder encoder, HPDF_UINT16 code)
Convert a specified character code to Unicode value.
HPDF_STATUS HPDF_UseCNTEncodings(HPDF_Doc pdf)
Enable traditional Chinese encodings.
HPDF_STATUS HPDF_UseKRFonts(HPDF_Doc pdf)
Enable Korean fonts. Application can use following Korean fonts after HPDF_UseKRFonts() call:
HPDF_STATUS HPDF_UseCNTFonts(HPDF_Doc pdf)
Enable traditional Chinese fonts. Application can use following traditional Chinese fonts after HPDF_...
HPDF_Font HPDF_GetFont(HPDF_Doc pdf, const char *font_name, const char *encoding_name)
Get requested font object handle.
HPDF_STATUS HPDF_UseCNSFonts(HPDF_Doc pdf)
Enable simplified Chinese fonts. Application can use following simplified Chinese fonts after HPDF_Us...
HPDF_STATUS HPDF_UseJPFonts(HPDF_Doc pdf)
Enable Japanese fonts. Application can use following Japanese fonts after HPDF_UseJPFonts() call:
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_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_ENCODER_TYPE_DOUBLE_BYTE
Multi-byte character encoder.
Definition: hpdf_types.h:584
unsigned short HPDF_UINT16
Definition: hpdf_types.h:63
enum _HPDF_ByteType HPDF_ByteType
@ HPDF_BYTE_TYPE_LEAD
Lead byte of a double-byte character.
Definition: hpdf_types.h:595
HPDF_UINT16 HPDF_UNICODE
Definition: hpdf_types.h:100
unsigned char HPDF_BYTE
Definition: hpdf_types.h:74
@ 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
Definition: hpdf_encoder.h:115