27void SPdfDoc::ErrorHandler(HPDF_STATUS error_no, HPDF_STATUS detail_no,
void *user_data)
29 MMechostr(MSKRUNTIME,
"SPdfDoc ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no, (HPDF_UINT)detail_no);
33 HPDF_Error_Reset (&doc->mDocument->error);
34 throw std::exception();
37SPdfDoc::SPdfDoc(std::string title, std::string creator, std::string author,
unsigned int dpiResolution)
39 mDocument = HPDF_New(SPdfDoc::ErrorHandler,
this);
40 mCharset =
"StandardEncoding";
42 HPDF_SetInfoAttr(mDocument, HPDF_INFO_TITLE, title.c_str());
43 HPDF_SetInfoAttr(mDocument, HPDF_INFO_CREATOR, creator.c_str());
44 HPDF_SetInfoAttr(mDocument, HPDF_INFO_AUTHOR, author.c_str());
45 HPDF_SetCompressionMode(mDocument, HPDF_COMP_ALL);
55float SPdfDoc::MmtoDpi(
float value)
57 return value / 25.4f * 72.0f;
60float SPdfDoc::DpiToMm(
float value)
62 return value / 72.0f * 25.4f;
65float SPdfDoc::DpiScale(
float value)
67 return (value / 72.0f) * (float)mDpi;
70void SPdfDoc::AlignCoords(
int flags,
float pageWidth,
float pageHeight,
float eltWidth,
float eltHeight,
float &x,
float &y)
75 if (flags & EM_CENTER)
76 x = x + (pageWidth / 2) - (eltWidth / 2);
79 y = pageHeight - y - eltHeight;
81 if (flags & EM_MIDDLE)
82 y = (pageHeight / 2) - (eltHeight / 2) - y;
85float SPdfDoc::GetTextWidth(HPDF_Page page, std::string text, std::string fontname,
float fontsize)
90 nfont = HPDF_GetFont(mDocument, fontname.c_str(), mCharset.c_str());
92 catch (std::exception &)
98 nfont = HPDF_GetFont(mDocument,
"Helvetica", mCharset.c_str());
100 HPDF_Page_SetFontAndSize(page, nfont, MmtoDpi(fontsize));
101 return DpiToMm(HPDF_Page_TextWidth(page, text.c_str()));
104HPDF_Page SPdfDoc::AddPage(HPDF_PageSizes sizes, HPDF_PageDirection direction)
106 HPDF_Page page = HPDF_AddPage(mDocument);
107 HPDF_Page_SetSize(page, sizes, direction);
108 HPDF_Page_Concat(page, 72.0f / (
float)mDpi, 0, 0, 72.0f / (
float)mDpi, 0, 0);
112HPDF_Page SPdfDoc::GetPageByIndex(HPDF_UINT index)
114 return HPDF_GetPageByIndex(mDocument, index);
117float SPdfDoc::GetPageWidth(HPDF_Page page)
119 return DpiToMm(HPDF_Page_GetWidth(page));
122float SPdfDoc::GetPageHeight(HPDF_Page page)
124 return DpiToMm(HPDF_Page_GetHeight(page));
127void SPdfDoc::SetCharset(std::string charset)
132void SPdfDoc::SetPageMode(HPDF_PageMode mode)
134 HPDF_SetPageMode(mDocument, mode);
137HPDF_PageMode SPdfDoc::GetPageMode()
139 return HPDF_GetPageMode(mDocument);
142void SPdfDoc::SetPageLayout(HPDF_PageLayout layout)
144 HPDF_SetPageLayout(mDocument, layout);
147HPDF_PageLayout SPdfDoc::GetPageLayout()
149 return HPDF_GetPageLayout(mDocument);
152void SPdfDoc::AddPageLabel(HPDF_UINT pagenum, HPDF_PageNumStyle style, HPDF_UINT firstpage, std::string prefix)
154 HPDF_AddPageLabel(mDocument, pagenum, style, firstpage, prefix.c_str());
157std::string SPdfDoc::LoadFontTTF(std::string fontpath)
159 std::string fontname;
162 fontname = HPDF_LoadTTFontFromFile(mDocument, fontpath.c_str(), HPDF_TRUE);
164 catch(std::exception&)
166 fontname = HPDF_LoadTTFontFromFile2(mDocument, fontpath.c_str(), 0, HPDF_TRUE);
171void SPdfDoc::AddPageText(HPDF_Page page, std::string text,
float posx,
float posy,
int color, std::string fontname,
float fontsize,
int align,
bool relative)
176 nfont = HPDF_GetFont(mDocument, fontname.c_str(), mCharset.c_str());
178 catch (std::exception &)
184 nfont = HPDF_GetFont(mDocument,
"Helvetica", mCharset.c_str());
186 HPDF_RGBColor ccolor = ConvertColor(color);
188 HPDF_Page_BeginText(page);
189 HPDF_Page_SetFontAndSize(page, nfont, fontsize);
190 HPDF_Page_SetRGBFill(page, ccolor.r, ccolor.g, ccolor.b);
192 float width = HPDF_Page_TextWidth(page, text.c_str());
193 AlignCoords(align, GetPageWidth(page), GetPageHeight(page), width, fontsize, posx, posy);
196 HPDF_Page_SetFontAndSize(page, nfont, DpiScale(MmtoDpi(fontsize)));
199 HPDF_Page_ShowText(page, text.c_str());
200 HPDF_Page_MoveTextPos(page, (HPDF_REAL)DpiScale(MmtoDpi(posx)), (HPDF_REAL)DpiScale(MmtoDpi(posy)));
204 HPDF_Page_TextOut(page, (HPDF_REAL)DpiScale(MmtoDpi(posx)), (HPDF_REAL)DpiScale(MmtoDpi(posy)), text.c_str());
207 HPDF_Page_EndText(page);
210void SPdfDoc::AddPageBitmap(HPDF_Page page, PtrObjBitmap bitmap,
float posx,
float posy,
float width,
float height,
int align)
212 HPDF_Image image = ConvertBitmap(bitmap);
214 AlignCoords(align, GetPageWidth(page), GetPageHeight(page), width, height, posx, posy);
215 HPDF_Page_DrawImage(page, image, (HPDF_REAL)DpiScale(MmtoDpi(posx)), (HPDF_REAL)DpiScale(MmtoDpi(posy)), (HPDF_REAL)DpiScale(MmtoDpi(width)), (HPDF_REAL)DpiScale(MmtoDpi(height)));
218void SPdfDoc::AddPageImage(HPDF_Page page, std::string path,
float posx,
float posy,
float width,
float height,
int align)
220 HPDF_Image image = 0;
225 image = HPDF_LoadJpegImageFromFile(mDocument, path.c_str());
227 catch (std::exception &)
232 image = HPDF_LoadPngImageFromFile(mDocument, path.c_str());
235 width = (float)HPDF_Image_GetWidth(image);
238 height = (float)HPDF_Image_GetHeight(image);
240 AlignCoords(align, GetPageWidth(page), GetPageHeight(page), width, height, posx, posy);
241 HPDF_Page_DrawImage(page, image, (HPDF_REAL)DpiScale(MmtoDpi(posx)), (HPDF_REAL)DpiScale(MmtoDpi(posy)), (HPDF_REAL)DpiScale(MmtoDpi(width)), (HPDF_REAL)DpiScale(MmtoDpi(height)));
244void SPdfDoc::AddPageRectangle(HPDF_Page page,
float posx,
float posy,
float width,
float height,
float lineWith,
int lineColor,
int fillColor,
bool filled,
int align)
246 HPDF_RGBColor fcolor = ConvertColor(fillColor);
247 HPDF_Page_SetRGBFill(page, fcolor.r, fcolor.g, fcolor.b);
248 HPDF_RGBColor ccolor = ConvertColor(lineColor);
250 AlignCoords(align, GetPageWidth(page), GetPageHeight(page), width, height, posx, posy);
251 HPDF_Page_SetLineWidth(page, (HPDF_REAL)DpiScale(MmtoDpi(lineWith)));
252 HPDF_Page_Rectangle(page, (HPDF_REAL)DpiScale(MmtoDpi(posx)), (HPDF_REAL)DpiScale(MmtoDpi(posy)), (HPDF_REAL)DpiScale(MmtoDpi(width)), (HPDF_REAL)DpiScale(MmtoDpi(height)));
255 HPDF_Page_Fill(page);
259 HPDF_Page_SetRGBStroke(page, ccolor.r, ccolor.g, ccolor.b);
260 HPDF_Page_Rectangle(page, (HPDF_REAL)DpiScale(MmtoDpi(posx)), (HPDF_REAL)DpiScale(MmtoDpi(posy)), (HPDF_REAL)DpiScale(MmtoDpi(width)), (HPDF_REAL)DpiScale(MmtoDpi(height)));
261 HPDF_Page_Stroke(page);
265void SPdfDoc::AddPageEllipse(HPDF_Page page,
float posx,
float posy,
float xradius,
float yradius,
float lineWith,
int lineColor,
int fillColor,
bool filled,
int align)
267 HPDF_RGBColor fcolor = ConvertColor(fillColor);
268 HPDF_Page_SetRGBFill(page, fcolor.r, fcolor.g, fcolor.b);
269 HPDF_RGBColor ccolor = ConvertColor(lineColor);
271 AlignCoords(align, GetPageWidth(page), GetPageHeight(page), 0, 0, posx, posy);
272 HPDF_Page_SetLineWidth(page, (HPDF_REAL)DpiScale(MmtoDpi(lineWith)));
273 HPDF_Page_Ellipse(page, (HPDF_REAL)DpiScale(MmtoDpi(posx)), (HPDF_REAL)DpiScale(MmtoDpi(posy)), (HPDF_REAL)DpiScale(MmtoDpi(xradius)), (HPDF_REAL)DpiScale(MmtoDpi(yradius)));
276 HPDF_Page_Fill(page);
280 HPDF_Page_SetRGBStroke(page, ccolor.r, ccolor.g, ccolor.b);
281 HPDF_Page_Ellipse(page, (HPDF_REAL)DpiScale(MmtoDpi(posx)), (HPDF_REAL)DpiScale(MmtoDpi(posy)), (HPDF_REAL)DpiScale(MmtoDpi(xradius)), (HPDF_REAL)DpiScale(MmtoDpi(yradius)));
282 HPDF_Page_Stroke(page);
286void SPdfDoc::AddPageLine(HPDF_Page page,
float posx,
float posy,
float dstx,
float dsty,
float lineWith,
int lineColor)
288 HPDF_RGBColor ccolor = ConvertColor(lineColor);
289 HPDF_Page_SetLineWidth(page, (HPDF_REAL)DpiScale(MmtoDpi(lineWith)));
291 HPDF_Page_SetRGBStroke(page, ccolor.r, ccolor.g, ccolor.b);
292 HPDF_Page_MoveTo(page, DpiScale(MmtoDpi(posx)), DpiScale(MmtoDpi(GetPageHeight(page) - posy)));
293 HPDF_Page_LineTo(page, DpiScale(MmtoDpi(dstx)), DpiScale(MmtoDpi(GetPageHeight(page) - dsty)));
294 HPDF_Page_Stroke(page);
298void SPdfDoc::SaveToFile(std::string path)
300 HPDF_SaveToFile(mDocument, path.c_str());
303HPDF_Image SPdfDoc::ConvertBitmap(PtrObjBitmap scolBitmap)
308 HPDF_ColorSpace color = (scolBitmap->BytesPP == 3) ? HPDF_CS_DEVICE_RGB : HPDF_CS_DEVICE_GRAY;
309 HPDF_Image cimage = 0;
312 int sbpl = scolBitmap->BPL;
313 int dbpl = scolBitmap->TailleW * scolBitmap->BytesPP;
320 int size = (
sizeof(
unsigned char*) * scolBitmap->BytesPP);
321 unsigned char* buff = (
unsigned char*)malloc(scolBitmap->TailleW * scolBitmap->TailleH * size);
323 for (
long y=0; y<scolBitmap->TailleH; y++)
325 for (
long x=0; x<scolBitmap->TailleW; x++)
327 unsigned long srcByte = (x * scolBitmap->BytesPP) + (sbpl * y);
328 unsigned long destByte = (x * scolBitmap->BytesPP) + (dbpl * y);
330 buff[destByte] = scolBitmap->bits[srcByte + 2];
331 buff[destByte + 1] = scolBitmap->bits[srcByte + 1];
332 buff[destByte + 2] = scolBitmap->bits[srcByte];
336 cimage = HPDF_LoadRawImageFromMem(mDocument, buff, scolBitmap->TailleW, scolBitmap->TailleH, color, scolBitmap->BPP / scolBitmap->BytesPP);
343HPDF_RGBColor SPdfDoc::ConvertColor(
int color)
345 float b = ((color>>16) & 0x000000ff) / 255.0f;
346 float g = ((color>>8) & 0x000000ff) / 255.0f;
347 float r = ((color) & 0x000000ff) / 255.0f;
349 HPDF_RGBColor ccolor;
SPdfDoc(std::string title, std::string creator, std::string author, unsigned int dpiResolution=72)