29 #include <hpdf_error.h>
34 static void error_handler(HPDF_STATUS error_no, HPDF_STATUS detail_no,
void *user_data)
38 case HPDF_ARRAY_COUNT_ERR :
40 case HPDF_ARRAY_ITEM_NOT_FOUND :
42 case HPDF_ARRAY_ITEM_UNEXPECTED_TYPE :
44 case HPDF_BINARY_LENGTH_ERR :
46 case HPDF_CANNOT_GET_PALLET :
48 case HPDF_DICT_COUNT_ERR :
50 case HPDF_DICT_ITEM_NOT_FOUND :
52 case HPDF_DICT_ITEM_UNEXPECTED_TYPE :
54 case HPDF_DICT_STREAM_LENGTH_NOT_FOUND :
56 case HPDF_DOC_ENCRYPTDICT_NOT_FOUND :
57 throw ExceptionRuntime(
"HPDF_SetEncryptMode() or HPDF_SetPermission() called before password set.");
58 case HPDF_DOC_INVALID_OBJECT :
60 case HPDF_DUPLICATE_REGISTRATION :
62 case HPDF_EXCEED_JWW_CODE_NUM_LIMIT :
63 throw ExceptionRuntime(
"Cannot register a character to the Japanese word wrap characters list.");
64 case HPDF_ENCRYPT_INVALID_PASSWORD :
65 throw ExceptionRuntime(
"1. Tried to set the owner password to NULL. 2. Owner and user password are the same.");
66 case HPDF_ERR_UNKNOWN_CLASS :
68 case HPDF_EXCEED_GSTATE_LIMIT :
70 case HPDF_FAILD_TO_ALLOC_MEM :
72 case HPDF_FILE_IO_ERROR :
74 case HPDF_FILE_OPEN_ERROR :
76 case HPDF_FONT_EXISTS :
78 case HPDF_FONT_INVALID_WIDTHS_TABLE :
79 throw ExceptionRuntime(
"1. Font-file format is invalid. 2. Internal error. Data consistency was lost.");
80 case HPDF_INVALID_AFM_HEADER :
82 case HPDF_INVALID_ANNOTATION :
84 case HPDF_INVALID_BIT_PER_COMPONENT :
85 throw ExceptionRuntime(
"Bit-per-component of a image which was set as mask-image is invalid.");
86 case HPDF_INVALID_CHAR_MATRICS_DATA :
88 case HPDF_INVALID_COLOR_SPACE :
89 throw ExceptionRuntime(
"1. Invalid color_space parameter of HPDF_LoadRawImage. 2. Color-space of a image which was set as mask-image is invalid. 3. Invoked function invalid in present color-space.");
90 case HPDF_INVALID_COMPRESSION_MODE :
91 throw ExceptionRuntime(
"Invalid value set when invoking HPDF_SetCommpressionMode().");
92 case HPDF_INVALID_DATE_TIME :
94 case HPDF_INVALID_DESTINATION :
96 case HPDF_INVALID_DOCUMENT :
98 case HPDF_INVALID_DOCUMENT_STATE :
99 throw ExceptionRuntime(
"Function invalid in the present state was invoked.");
100 case HPDF_INVALID_ENCODER :
102 case HPDF_INVALID_ENCODER_TYPE :
104 case HPDF_INVALID_ENCODING_NAME :
106 case HPDF_INVALID_ENCRYPT_KEY_LEN :
108 case HPDF_INVALID_FONTDEF_DATA :
109 throw ExceptionRuntime(
"1. An invalid font handle was set. 2. Unsupported font format.");
110 case HPDF_INVALID_FONTDEF_TYPE :
112 case HPDF_INVALID_FONT_NAME :
114 case HPDF_INVALID_IMAGE :
116 case HPDF_INVALID_JPEG_DATA :
118 case HPDF_INVALID_N_DATA :
120 case HPDF_INVALID_OBJECT :
121 throw ExceptionRuntime(
"1. An invalid object is set. 2. Internal error. Data consistency was lost.");
122 case HPDF_INVALID_OBJ_ID :
124 case HPDF_INVALID_OPERATION :
125 throw ExceptionRuntime(
"Invoked HPDF_Image_SetColorMask() against the image-object which was set a mask-image.");
126 case HPDF_INVALID_OUTLINE :
128 case HPDF_INVALID_PAGE :
130 case HPDF_INVALID_PAGES :
131 throw ExceptionRuntime(
"An invalid pages-handle was specified. (internal error)");
132 case HPDF_INVALID_PARAMETER :
134 case HPDF_INVALID_PNG_IMAGE :
136 case HPDF_INVALID_STREAM :
138 case HPDF_MISSING_FILE_NAME_ENTRY :
139 throw ExceptionRuntime(
"Internal error. _FILE_NAME entry for delayed loading is missing.");
140 case HPDF_INVALID_TTC_FILE :
142 case HPDF_INVALID_TTC_INDEX :
144 case HPDF_INVALID_WX_DATA :
146 case HPDF_ITEM_NOT_FOUND :
148 case HPDF_LIBPNG_ERROR :
150 case HPDF_NAME_INVALID_VALUE :
152 case HPDF_NAME_OUT_OF_RANGE :
154 case HPDF_PAGES_MISSING_KIDS_ENTRY :
156 case HPDF_PAGE_CANNOT_FIND_OBJECT :
158 case HPDF_PAGE_CANNOT_GET_ROOT_PAGES :
160 case HPDF_PAGE_CANNOT_RESTORE_GSTATE :
162 case HPDF_PAGE_CANNOT_SET_PARENT :
164 case HPDF_PAGE_FONT_NOT_FOUND :
166 case HPDF_PAGE_INVALID_FONT :
168 case HPDF_PAGE_INVALID_FONT_SIZE :
170 case HPDF_PAGE_INVALID_GMODE :
172 case HPDF_PAGE_INVALID_INDEX :
174 case HPDF_PAGE_INVALID_ROTATE_VALUE :
176 case HPDF_PAGE_INVALID_SIZE :
178 case HPDF_PAGE_INVALID_XOBJECT :
180 case HPDF_PAGE_OUT_OF_RANGE :
182 case HPDF_REAL_OUT_OF_RANGE :
184 case HPDF_STREAM_EOF :
186 case HPDF_STREAM_READLN_CONTINUE :
188 case HPDF_STRING_OUT_OF_RANGE :
190 case HPDF_THIS_FUNC_WAS_SKIPPED :
192 case HPDF_TTF_CANNOT_EMBEDDING_FONT :
194 case HPDF_TTF_INVALID_CMAP :
195 throw ExceptionRuntime(
"Unsupported ttf format. (cannot find unicode cmap)");
196 case HPDF_TTF_INVALID_FOMAT :
198 case HPDF_TTF_MISSING_TABLE :
199 throw ExceptionRuntime(
"Unsupported ttf format. (cannot find a necessary table)");
200 case HPDF_UNSUPPORTED_FONT_TYPE :
202 case HPDF_UNSUPPORTED_FUNC :
203 throw ExceptionRuntime(
"1. Library not configured to use PNGLIB. 2. Internal error. Data consistency was lost.");
204 case HPDF_UNSUPPORTED_JPEG_FORMAT :
206 case HPDF_UNSUPPORTED_TYPE1_FONT :
208 case HPDF_XREF_COUNT_ERR :
210 case HPDF_ZLIB_ERROR :
212 case HPDF_INVALID_PAGE_INDEX :
214 case HPDF_INVALID_URI :
224 case HPDF_ANNOT_INVALID_ICON :
226 case HPDF_ANNOT_INVALID_BORDER_STYLE :
228 case HPDF_PAGE_INVALID_DIRECTION :
230 case HPDF_INVALID_FONT :
235 using namespace crn::PDF;
241 doc(HPDF_New(error_handler, NULL)),
244 HPDF_SetCompressionMode(HPDF_Doc(doc), HPDF_COMP_ALL);
245 SetCreator(
"libcrn");
253 doc(HPDF_New(error_handler, NULL)),
256 HPDF_SetCompressionMode(HPDF_Doc(doc), HPDF_COMP_ALL);
263 HPDF_Free(HPDF_Doc(doc));
271 void Doc::Save(
const Path &file_name)
273 HPDF_SaveToFile(HPDF_Doc(doc), file_name.
CStr());
284 case Attributes::Layout::ONE_PAGE:
285 HPDF_SetPageLayout(HPDF_Doc(doc), HPDF_PAGE_LAYOUT_SINGLE);
287 case Attributes::Layout::CONTINUOUS:
288 HPDF_SetPageLayout(HPDF_Doc(doc), HPDF_PAGE_LAYOUT_ONE_COLUMN);
290 case Attributes::Layout::OPEN_LEFT:
291 HPDF_SetPageLayout(HPDF_Doc(doc), HPDF_PAGE_LAYOUT_TWO_COLUMN_LEFT);
293 case Attributes::Layout::OPEN_RIGHT:
294 HPDF_SetPageLayout(HPDF_Doc(doc), HPDF_PAGE_LAYOUT_TWO_COLUMN_RIGHT);
304 HPDF_PageLayout l = HPDF_GetPageLayout(HPDF_Doc(doc));
307 case HPDF_PAGE_LAYOUT_SINGLE:
308 return Attributes::Layout::ONE_PAGE;
309 case HPDF_PAGE_LAYOUT_ONE_COLUMN:
310 return Attributes::Layout::CONTINUOUS;
311 case HPDF_PAGE_LAYOUT_TWO_COLUMN_LEFT:
312 return Attributes::Layout::OPEN_LEFT;
314 return Attributes::Layout::OPEN_RIGHT;
322 Page Doc::GetCurrentPage()
324 return Page(HPDF_GetCurrentPage(HPDF_Doc(doc)));
333 return Page(HPDF_AddPage(HPDF_Doc(doc)));
341 Page Doc::InsertPage(
const Page& target)
343 return Page(HPDF_InsertPage(HPDF_Doc(doc), HPDF_Page(target.page)));
353 return Image(HPDF_LoadJpegImageFromFile(HPDF_Doc(doc), fname.
CStr()));
362 Image Doc::AddPNG(
const Path &fname,
bool delay)
366 return Image(HPDF_LoadPngImageFromFile2(HPDF_Doc(doc), fname.
CStr()));
370 return Image(HPDF_LoadPngImageFromFile(HPDF_Doc(doc), fname.
CStr()));
380 HPDF_SetInfoAttr(HPDF_Doc(doc), HPDF_INFO_AUTHOR, s.
CStr());
389 HPDF_SetInfoAttr(HPDF_Doc(doc), HPDF_INFO_CREATOR, s.
CStr());
398 HPDF_SetInfoAttr(HPDF_Doc(doc), HPDF_INFO_TITLE, s.
CStr());
407 HPDF_SetInfoAttr(HPDF_Doc(doc), HPDF_INFO_SUBJECT, s.
CStr());
416 HPDF_SetInfoAttr(HPDF_Doc(doc), HPDF_INFO_KEYWORDS, s.
CStr());
429 HPDF_SetPassword(HPDF_Doc(doc), admin.
CStr(), user.
CStr());
430 HPDF_SetEncryptionMode(HPDF_Doc(doc), HPDF_ENCRYPT_R3, 16);
439 void Doc::SetPermissions(
bool can_copy,
bool can_print)
442 throw ExceptionLogic(
"Cannot set permissions of a document that has no admin password.");
443 HPDF_UINT perm = HPDF_ENABLE_READ|HPDF_ENABLE_EDIT_ALL|HPDF_ENABLE_EDIT;
445 perm |= HPDF_ENABLE_COPY;
447 perm |= HPDF_ENABLE_PRINT;
448 HPDF_SetPermission(HPDF_Doc(doc), perm);
455 void Doc::SetAttributes(
const Attributes &attr)
457 SetPageLayout(attr.
layout);
460 SetTitle(attr.
title);
473 PDF::Image::Image(
void *obj):
482 size_t PDF::Image::GetWidth()
const
484 return HPDF_Image_GetWidth(HPDF_Image(img));
491 size_t PDF::Image::GetHeight()
const
493 return HPDF_Image_GetHeight(HPDF_Image(img));
499 Page::Page(
void *obj):
505 double Page::GetWidth()
507 return HPDF_Page_GetWidth(HPDF_Page(page));
511 double Page::GetHeight()
513 return HPDF_Page_GetHeight(HPDF_Page(page));
520 void Page::SetWidth(
double w)
522 HPDF_Page_SetWidth(HPDF_Page(page), HPDF_REAL(w));
529 void Page::SetHeight(
double h)
531 HPDF_Page_SetHeight(HPDF_Page(page), HPDF_REAL(h));
540 HPDF_Page_SetRGBStroke(HPDF_Page(page), HPDF_REAL(color.
r / 255.0), HPDF_REAL(color.
g / 255.0), HPDF_REAL(color.
b / 255.0));
549 HPDF_Page_SetRGBFill(HPDF_Page(page), HPDF_REAL(color.
r / 255.0), HPDF_REAL(color.
g / 255.0), HPDF_REAL(color.
b / 255.0));
558 void Page::DrawImage(
Image &img,
const Rect &bbox)
560 HPDF_Page_DrawImage(HPDF_Page(page), HPDF_Image(img.img), HPDF_REAL(bbox.
GetLeft()), HPDF_REAL(GetHeight()) - HPDF_REAL(bbox.
GetBottom()), HPDF_REAL(bbox.
GetWidth()), HPDF_REAL(bbox.
GetHeight()));
565 void Page::DrawLine(
double x1,
double y1,
double x2,
double y2)
567 HPDF_Page_MoveTo(HPDF_Page(page), HPDF_REAL(x1), HPDF_REAL(GetHeight() - y1));
568 HPDF_Page_LineTo(HPDF_Page(page), HPDF_REAL(x2), HPDF_REAL(GetHeight() - y2));
569 HPDF_Page_Stroke(HPDF_Page(page));
577 void Page::DrawRect(
const Rect &rect,
bool fill)
579 HPDF_Page_Rectangle(HPDF_Page(page), HPDF_REAL(rect.
GetLeft()), HPDF_REAL(GetHeight() - rect.
GetBottom()), HPDF_REAL(rect.
GetWidth()), HPDF_REAL(rect.
GetHeight()));
581 HPDF_Page_FillStroke(HPDF_Page(page));
583 HPDF_Page_Stroke(HPDF_Page(page));
586 #endif // CRN_USING_HARU
Abstract class for images.
int GetBottom() const
Returns the bottommost coordinate.
Unintialized object error.
bool IsNotEmpty() const noexcept
Checks if the string is not empty.
int GetLeft() const
Returns the leftmost coordinate.
const char * CStr() const noexcept
Conversion to UTF8 cstring.
bool IsEmpty() const noexcept
Checks if the string is empty.
A convenience class for file paths.
int GetHeight() const
Returns the height of the rectangle.
StringUTF8 owner_password
int GetWidth() const
Returns the width of the rectangle.
Attributes to create a PDF export of a document.
A character string class.