Myo Scol plugin
Loading...
Searching...
No Matches
sPdf.cpp
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2013 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24
25#include "sPdf.h"
26
27void SPdfDoc::ErrorHandler(HPDF_STATUS error_no, HPDF_STATUS detail_no, void *user_data)
28{
29 MMechostr(MSKRUNTIME, "SPdfDoc ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no, (HPDF_UINT)detail_no);
30 SPdfDoc* doc = static_cast<SPdfDoc*>(user_data);
31
32 //reset error status
33 HPDF_Error_Reset (&doc->mDocument->error);
34 throw std::exception();
35}
36
37SPdfDoc::SPdfDoc(std::string title, std::string creator, std::string author, unsigned int dpiResolution)
38{
39 mDocument = HPDF_New(SPdfDoc::ErrorHandler, this);
40 mCharset = "StandardEncoding";
41
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);
46 mDpi = dpiResolution;
47}
48
50{
51 if (mDocument)
52 HPDF_Free(mDocument);
53}
54
55float SPdfDoc::MmtoDpi(float value)
56{
57 return value / 25.4f * 72.0f;
58}
59
60float SPdfDoc::DpiToMm(float value)
61{
62 return value / 72.0f * 25.4f;
63}
64
65float SPdfDoc::DpiScale(float value)
66{
67 return (value / 72.0f) * (float)mDpi;
68}
69
70void SPdfDoc::AlignCoords(int flags, float pageWidth, float pageHeight, float eltWidth, float eltHeight, float &x, float &y)
71{
72 if (flags & EM_RIGHT)
73 x = pageWidth - x;
74
75 if (flags & EM_CENTER)
76 x = x + (pageWidth / 2) - (eltWidth / 2);
77
78 if (flags & EM_TOP)
79 y = pageHeight - y - eltHeight;
80
81 if (flags & EM_MIDDLE)
82 y = (pageHeight / 2) - (eltHeight / 2) - y;
83}
84
85float SPdfDoc::GetTextWidth(HPDF_Page page, std::string text, std::string fontname, float fontsize)
86{
87 HPDF_Font nfont = 0;
88 try
89 {
90 nfont = HPDF_GetFont(mDocument, fontname.c_str(), mCharset.c_str());
91 }
92 catch (std::exception &)
93 {
94 }
95
96 // font still found define a default one
97 if (nfont == 0)
98 nfont = HPDF_GetFont(mDocument, "Helvetica", mCharset.c_str());
99
100 HPDF_Page_SetFontAndSize(page, nfont, MmtoDpi(fontsize));
101 return DpiToMm(HPDF_Page_TextWidth(page, text.c_str()));
102}
103
104HPDF_Page SPdfDoc::AddPage(HPDF_PageSizes sizes, HPDF_PageDirection direction)
105{
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);
109 return page;
110}
111
112HPDF_Page SPdfDoc::GetPageByIndex(HPDF_UINT index)
113{
114 return HPDF_GetPageByIndex(mDocument, index);
115}
116
117float SPdfDoc::GetPageWidth(HPDF_Page page)
118{
119 return DpiToMm(HPDF_Page_GetWidth(page));
120}
121
122float SPdfDoc::GetPageHeight(HPDF_Page page)
123{
124 return DpiToMm(HPDF_Page_GetHeight(page));
125}
126
127void SPdfDoc::SetCharset(std::string charset)
128{
129 mCharset = charset;
130}
131
132void SPdfDoc::SetPageMode(HPDF_PageMode mode)
133{
134 HPDF_SetPageMode(mDocument, mode);
135}
136
137HPDF_PageMode SPdfDoc::GetPageMode()
138{
139 return HPDF_GetPageMode(mDocument);
140}
141
142void SPdfDoc::SetPageLayout(HPDF_PageLayout layout)
143{
144 HPDF_SetPageLayout(mDocument, layout);
145}
146
147HPDF_PageLayout SPdfDoc::GetPageLayout()
148{
149 return HPDF_GetPageLayout(mDocument);
150}
151
152void SPdfDoc::AddPageLabel(HPDF_UINT pagenum, HPDF_PageNumStyle style, HPDF_UINT firstpage, std::string prefix)
153{
154 HPDF_AddPageLabel(mDocument, pagenum, style, firstpage, prefix.c_str());
155}
156
157std::string SPdfDoc::LoadFontTTF(std::string fontpath)
158{
159 std::string fontname;
160 try
161 {
162 fontname = HPDF_LoadTTFontFromFile(mDocument, fontpath.c_str(), HPDF_TRUE);
163 }
164 catch(std::exception&)
165 {
166 fontname = HPDF_LoadTTFontFromFile2(mDocument, fontpath.c_str(), 0, HPDF_TRUE);
167 }
168 return fontname;
169}
170
171void SPdfDoc::AddPageText(HPDF_Page page, std::string text, float posx, float posy, int color, std::string fontname, float fontsize, int align, bool relative)
172{
173 HPDF_Font nfont = 0;
174 try
175 {
176 nfont = HPDF_GetFont(mDocument, fontname.c_str(), mCharset.c_str());
177 }
178 catch (std::exception &)
179 {
180 }
181
182 // font still found define a default one
183 if (nfont == 0)
184 nfont = HPDF_GetFont(mDocument, "Helvetica", mCharset.c_str());
185
186 HPDF_RGBColor ccolor = ConvertColor(color);
187
188 HPDF_Page_BeginText(page);
189 HPDF_Page_SetFontAndSize(page, nfont, fontsize);
190 HPDF_Page_SetRGBFill(page, ccolor.r, ccolor.g, ccolor.b);
191
192 float width = HPDF_Page_TextWidth(page, text.c_str());
193 AlignCoords(align, GetPageWidth(page), GetPageHeight(page), width, fontsize, posx, posy);
194
195 // now with transformed fontsize
196 HPDF_Page_SetFontAndSize(page, nfont, DpiScale(MmtoDpi(fontsize)));
197 if (relative)
198 {
199 HPDF_Page_ShowText(page, text.c_str());
200 HPDF_Page_MoveTextPos(page, (HPDF_REAL)DpiScale(MmtoDpi(posx)), (HPDF_REAL)DpiScale(MmtoDpi(posy)));
201 }
202 else
203 {
204 HPDF_Page_TextOut(page, (HPDF_REAL)DpiScale(MmtoDpi(posx)), (HPDF_REAL)DpiScale(MmtoDpi(posy)), text.c_str());
205 }
206
207 HPDF_Page_EndText(page);
208}
209
210void SPdfDoc::AddPageBitmap(HPDF_Page page, PtrObjBitmap bitmap, float posx, float posy, float width, float height, int align)
211{
212 HPDF_Image image = ConvertBitmap(bitmap);
213
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)));
216}
217
218void SPdfDoc::AddPageImage(HPDF_Page page, std::string path, float posx, float posy, float width, float height, int align)
219{
220 HPDF_Image image = 0;
221
222 // try to load jpg or png file
223 try
224 {
225 image = HPDF_LoadJpegImageFromFile(mDocument, path.c_str());
226 }
227 catch (std::exception &)
228 {
229 }
230
231 if (image == 0)
232 image = HPDF_LoadPngImageFromFile(mDocument, path.c_str());
233
234 if (width == 0)
235 width = (float)HPDF_Image_GetWidth(image);
236
237 if (height == 0)
238 height = (float)HPDF_Image_GetHeight(image);
239
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)));
242}
243
244void SPdfDoc::AddPageRectangle(HPDF_Page page, float posx, float posy, float width, float height, float lineWith, int lineColor, int fillColor, bool filled, int align)
245{
246 HPDF_RGBColor fcolor = ConvertColor(fillColor);
247 HPDF_Page_SetRGBFill(page, fcolor.r, fcolor.g, fcolor.b);
248 HPDF_RGBColor ccolor = ConvertColor(lineColor);
249
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)));
253
254 if (filled)
255 HPDF_Page_Fill(page);
256
257 if (lineWith > 0)
258 {
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);
262 }
263}
264
265void SPdfDoc::AddPageEllipse(HPDF_Page page, float posx, float posy, float xradius, float yradius, float lineWith, int lineColor, int fillColor, bool filled, int align)
266{
267 HPDF_RGBColor fcolor = ConvertColor(fillColor);
268 HPDF_Page_SetRGBFill(page, fcolor.r, fcolor.g, fcolor.b);
269 HPDF_RGBColor ccolor = ConvertColor(lineColor);
270
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)));
274
275 if (filled)
276 HPDF_Page_Fill(page);
277
278 if (lineWith > 0.0f)
279 {
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);
283 }
284}
285
286void SPdfDoc::AddPageLine(HPDF_Page page, float posx, float posy, float dstx, float dsty, float lineWith, int lineColor)
287{
288 HPDF_RGBColor ccolor = ConvertColor(lineColor);
289 HPDF_Page_SetLineWidth(page, (HPDF_REAL)DpiScale(MmtoDpi(lineWith)));
290
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);
295}
296
297
298void SPdfDoc::SaveToFile(std::string path)
299{
300 HPDF_SaveToFile(mDocument, path.c_str());
301}
302
303HPDF_Image SPdfDoc::ConvertBitmap(PtrObjBitmap scolBitmap)
304{
305 if (scolBitmap == 0)
306 return 0;
307
308 HPDF_ColorSpace color = (scolBitmap->BytesPP == 3) ? HPDF_CS_DEVICE_RGB : HPDF_CS_DEVICE_GRAY;
309 HPDF_Image cimage = 0;
310
311 //Scol bitmap are 32bits aligned
312 int sbpl = scolBitmap->BPL;
313 int dbpl = scolBitmap->TailleW * scolBitmap->BytesPP;
314
315 // have to switch RGB / BGR and re-align.
316 /*if (sbpl == dbpl)
317 cimage = HPDF_LoadRawImageFromMem(mDocument, scolBitmap->bits, scolBitmap->TailleW, scolBitmap->TailleH, color, scolBitmap->BPP / scolBitmap->BytesPP);
318 else*/
319 {
320 int size = (sizeof(unsigned char*) * scolBitmap->BytesPP);
321 unsigned char* buff = (unsigned char*)malloc(scolBitmap->TailleW * scolBitmap->TailleH * size);
322
323 for (long y=0; y<scolBitmap->TailleH; y++)
324 {
325 for (long x=0; x<scolBitmap->TailleW; x++)
326 {
327 unsigned long srcByte = (x * scolBitmap->BytesPP) + (sbpl * y);
328 unsigned long destByte = (x * scolBitmap->BytesPP) + (dbpl * y);
329
330 buff[destByte] = scolBitmap->bits[srcByte + 2]; // revert R / B
331 buff[destByte + 1] = scolBitmap->bits[srcByte + 1];
332 buff[destByte + 2] = scolBitmap->bits[srcByte];
333 }
334 }
335
336 cimage = HPDF_LoadRawImageFromMem(mDocument, buff, scolBitmap->TailleW, scolBitmap->TailleH, color, scolBitmap->BPP / scolBitmap->BytesPP);
337 free(buff);
338 }
339
340 return cimage;
341}
342
343HPDF_RGBColor SPdfDoc::ConvertColor(int color)
344{
345 float b = ((color>>16) & 0x000000ff) / 255.0f;
346 float g = ((color>>8) & 0x000000ff) / 255.0f;
347 float r = ((color) & 0x000000ff) / 255.0f;
348
349 HPDF_RGBColor ccolor;
350 ccolor.r = r;
351 ccolor.g = g;
352 ccolor.b = b;
353
354 return ccolor;
355}
Definition sPdf.h:36
SPdfDoc(std::string title, std::string creator, std::string author, unsigned int dpiResolution=72)
Definition sPdf.cpp:37
~SPdfDoc()
Definition sPdf.cpp:49