-
Notifications
You must be signed in to change notification settings - Fork 1
/
fpdf.zz
3105 lines (2885 loc) · 92.8 KB
/
fpdf.zz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#hdr
#include <string>
#include <vector>
#include <map>
#include "pdf-font.h"
typedef float pdf_float_t;
#end
#define PDF_USING_ZLIB 1
#src
#define PDF_USING_ZLIB 1
#define FPDF_VERSION "1.7"
#include "sqlite3.h"
#define pdf_vsnprintf sqlite3_ivsnprintf //vsnprintf
#define pdf_snprintf sqlite3_isnprintf //snprintf
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <cmath>
#ifdef PDF_USING_ZLIB
#include "zlib.h"
#endif
//#include "duma.h"
#end
#ifdef PDF_USING_ZLIB
std::string& gzcompress( std::string &dest, const std::string &src )
{
z_stream stream;
int err;
int nExtraChunks;
size_t src_len = src.size();
size_t dest_len = 2048 + src_len;
dest.resize(dest_len);
stream.next_in = (Bytef*)&src[0];
stream.avail_in = (uInt)src_len;
stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;
stream.opaque = (voidpf)0;
//err = deflateInit(&stream, Z_DEFAULT_COMPRESSION);
err = deflateInit(&stream, Z_BEST_SPEED);
if (err != Z_OK) throw "Could not initialize zstream !";
nExtraChunks = 0;
do
{
stream.next_out = (unsigned char*)&dest[0];
stream.avail_out = dest_len;
err = deflate(&stream, Z_FINISH);
if (err == Z_STREAM_END )
break;
if (err != Z_OK)
{
inflateEnd(&stream);
throw "Could not uncompress data !";
}
nExtraChunks += 1;
}
while (stream.avail_out == 0);
dest.resize(stream.total_out);
err = deflateEnd(&stream);
if (nExtraChunks || (err != Z_OK)) throw "Could finalize zstream !";
return dest;
}
std::string& gzuncompress( std::string &dest, const std::string &src )
{
z_stream stream;
int err;
int nExtraChunks;
size_t src_len = src.size();
size_t dest_len = 2048 + src_len*5;
dest.resize(dest_len);
stream.next_in = (Bytef*)&src[0];
stream.avail_in = (uInt)src_len;
stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;
err = inflateInit(&stream);
if (err != Z_OK) throw "Could not initialize zstream !";
nExtraChunks = 0;
do
{
stream.next_out = (unsigned char*)&dest[0];
stream.avail_out = dest_len;
err = inflate(&stream, Z_FINISH);
if (err == Z_STREAM_END )
break;
if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
err = Z_DATA_ERROR;
if (err != Z_BUF_ERROR)
{
inflateEnd(&stream);
throw "Could not uncompress data !";
}
nExtraChunks += 1;
}
while (stream.avail_out == 0);
dest.resize(stream.total_out);
err = inflateEnd(&stream);
if (nExtraChunks || (err != Z_OK)) throw "Could finalize zstream !";
return dest;
}
#endif
/*******************************************************************************
* FPDF *
* *
* Version: 1.7 *
* Date: 2011-06-18 *
* Author: Olivier PLATHEY *
* C++ Port Date: 2012-03-18 *
* C++ Port Author: Domingo Alvarez Duarte *
*******************************************************************************/
const char *FPDF_FONTPATH = 0;
class FPDF
{
public:
enum e_orientation {e_orientation_none, e_orientation_portrait, e_orientation_landscape};
enum e_units {e_units_none, e_mm, e_pt, e_cm, e_in};
enum e_page_sizes {e_page_size_none, e_A3, e_A4, e_A5, e_Letter, e_Legal,
e_page_sizes_last
};
enum e_zoom_mode {e_zoom_default, e_zoom_fullpage, e_zoom_fullwidth, e_zoom_real};
enum e_layout_mode {e_layout_default, e_layout_single, e_layout_continuous, e_layout_two};
struct st_image
{
pdf_float_t w, h;
std::string cs, dp, f, parms, pal, trns, imgdata, file, data, smask;
int i, n, bpc;
}
struct st_pagesize
{
pdf_float_t w, h;
}
struct st_link
{
int from, to;
}
struct st_page_link
{
pdf_float_t x, y, w, h;
int link;
}
struct pdf_color_t
{
unsigned char r, g, b, t;
};
protected:
int m_page; // current page number
int m_n; // current object number
//starting using at 1
std::vector<int> m_offsets; // array of object offsets
std::string m_buffer; // buffer holding in-memory PDF
std::vector<std::string> m_pages; // array containing pages
int m_state; // current document state
int m_compress; // compression flag
pdf_float_t m_k; // scale factor (number of points in user unit)
e_orientation m_DefOrientation; // default orientation
e_orientation m_CurOrientation; // current orientation
st_pagesize m_DefPageSize; // default page size
st_pagesize m_CurPageSize; // current page size
std::map<int, st_pagesize> m_PageSizes; // used for pages with non default sizes or orientations
pdf_float_t m_wPt, m_hPt; // dimensions of current page in points
pdf_float_t m_w, m_h; // dimensions of current page in user unit
pdf_float_t m_angle; //rotation angle
pdf_float_t m_lMargin; // left margin
pdf_float_t m_tMargin; // top margin
pdf_float_t m_rMargin; // right margin
pdf_float_t m_bMargin; // page break margin
pdf_float_t m_cMargin; // cell margin
pdf_float_t m_x, m_y; // current position in user unit
pdf_float_t m_lasth; // height of last printed cell
pdf_float_t m_LineWidth; // line width in user unit
std::string m_fontpath; // path containing fonts
// array of core font names
typedef std::map<std::string, st_pdf_font_base*> font_map_t;
font_map_t m_fonts; // array of used fonts
std::vector<int> m_FontFiles; // array of font files
std::vector<int> m_diffs; // array of encoding differences
std::string m_FontFamily; // current font family
std::string m_FontStyle; // current font style
bool m_underline; // underlining flag
st_pdf_font_base *m_CurrentFont; // current font info
pdf_float_t m_FontSizePt; // current font size in points
pdf_float_t m_FontSize; // current font size in user unit
std::string m_DrawColor; // commands for drawing color
pdf_color_t m_DrawColor_rgb;
std::string m_FillColor; // commands for filling color
pdf_color_t m_FillColor_rgb;
std::string m_TextColor; // commands for text color
pdf_color_t m_TextColor_rgb;
bool m_ColorFlag; // indicates whether fill and text colors are different
pdf_float_t m_ws; // word spacing
typedef std::map<std::string, st_image> image_map_t;
image_map_t m_images; // array of used images
typedef std::map<int, st_page_link> link_map_t;
link_map_t m_PageLinks; // array of links in pages
std::vector<st_link> m_links; // array of internal links
bool m_AutoPageBreak; // automatic page breaking
pdf_float_t m_PageBreakTrigger; // threshold used to trigger page breaks
bool m_InHeader; // flag set when processing header
bool m_InFooter; // flag set when processing footer
e_zoom_mode m_ZoomMode; // zoom display mode
int m_CustomZoom;
int m_LayoutMode; // layout display mode
std::string m_title; // title
std::string m_subject; // subject
std::string m_author; // author
std::string m_keywords; // keywords
std::string m_creator; // creator
std::string m_AliasNbPages; // alias for total number of pages
std::string m_PDFVersion; // PDF version number
char m_scratch_buf[128];
struct st_alpha_t
{
int n;
pdf_float_t alpha;
std::string bm;
};
std::vector<st_alpha_t> m_extgstates;
pdf_float_t m_outerMargin;
pdf_float_t m_innerMargin;
pdf_float_t m_xDelta; // if double-sided, difference between outer and inner
bool m_doubleSided; // layout like books?
std::string m_javascript;
int m_n_js;
public:
/*******************************************************************************
* *
* Public methods *
* *
*******************************************************************************/
virtual ~FPDF()
{
font_map_t::iterator iter = m_fonts.begin();
font_map_t::iterator iend = m_fonts.end();
while(iter != iend)
{
delete iter->second;
++iter;
}
}
FPDF()
{
reset(e_orientation_portrait, e_mm, e_A4);
}
FPDF(e_orientation orientation, e_units unit, e_page_sizes psize)
{
reset(orientation, unit, psize);
}
FPDF(char orientation, const char *unit, const char *psize)
{
e_orientation eo = orientation == 'L' ? e_orientation_landscape : e_orientation_portrait;
e_units eu;
e_page_sizes es;
if(strcasecmp(unit, "pt")==0) eu = e_pt;
else if(strcasecmp(unit, "cm")==0) eu = e_cm;
else if(strcasecmp(unit, "in")==0) eu = e_in;
else eu = e_mm;
if(strcasecmp(psize, "a3")==0) es = e_A3;
else if(strcasecmp(psize, "a5")==0) es = e_A5;
else if(strcasecmp(psize, "letter")==0) es = e_Letter;
else if(strcasecmp(psize, "legal")==0) es = e_Legal;
else es = e_A4;
reset(eo, eu, es);
}
void reset(e_orientation orientation=e_orientation_portrait, e_units unit=e_mm, e_page_sizes psize=e_A4)
{
// Scale factor
switch(unit)
{
case e_pt:
m_k = 1;
break;
case e_mm:
m_k = 72/25.4;
break;
case e_cm:
m_k = 72/2.54;
break;
case e_in:
m_k = 72;
break;
default:
Error("Incorrect unit: %s", unit);
}
// Some checks
_dochecks();
// Initialization of properties
m_page = 0;
m_pages.clear();
m_PageSizes.clear();
m_offsets.clear();
m_fonts.clear();
m_FontFiles.clear();
m_diffs.clear();
m_images.clear();
m_PageLinks.clear();
m_links.clear();
m_extgstates.clear();
m_offsets.resize(3, 0);
m_n = 2;
m_buffer.clear();
m_buffer.reserve(1024);
//this->buffer = "";
//this->m_pages = array();
//this->PageSizes = array();
m_state = 0;
//this->fonts = array();
//this->FontFiles = array();
//this->diffs = array();
//this->images = array();
//this->links = array();
m_InHeader = false;
m_InFooter = false;
m_lasth = 0;
//this->FontFamily = "";
//this->FontStyle = "";
m_FontSizePt = 12;
m_underline = false;
m_DrawColor = "0 G";
m_DrawColor_rgb = {0,0,0,0};
m_FillColor = "0 g";
m_FillColor_rgb = {0,0,0,0};
m_TextColor = "0 g";
m_TextColor_rgb = {0,0,0,0};
m_ColorFlag = false;
m_ws = 0;
// Font path
if(FPDF_FONTPATH)
{
m_fontpath = FPDF_FONTPATH;
char clast = *m_fontpath.rbegin();
if(clast != '/' && clast != '\\')
m_fontpath += "/";
}
#if 0
else if(is_dir(dirname(__FILE__) + "/font"))
m_fontpath = dirname(__FILE__) + "/font/";
#endif
else
m_fontpath.clear();
m_CurrentFont = 0;
st_pagesize page_size;
_getpagesize(page_size, psize);
m_DefPageSize = page_size;
m_CurPageSize = page_size;
// Page orientation
switch(orientation)
{
case e_orientation_portrait:
{
m_DefOrientation = e_orientation_portrait;
m_w = page_size.w;
m_h = page_size.h;
}
break;
case e_orientation_landscape:
{
m_DefOrientation = e_orientation_landscape;
m_w = page_size.h;
m_h = page_size.w;
}
break;
default:
Error("Incorrect orientation: %d", orientation);
}
m_CurOrientation = m_DefOrientation;
m_wPt = m_w * m_k;
m_hPt = m_h * m_k;
m_angle = 0;
// Page margins (1 cm)
pdf_float_t margin = 28.35/m_k;
SetMargins(margin,margin);
// Interior cell margin (1 mm)
m_cMargin = margin/10.0;
// Line width (0.2 mm)
m_LineWidth = .567/m_k;
// Automatic page break
SetAutoPageBreak(true,2*margin);
// Default display mode
SetDisplayMode(e_zoom_default);
m_CustomZoom = 0;
// Enable compression
SetCompression(true);
// Set default PDF version number
m_PDFVersion = "1.3";
m_doubleSided=false;
m_xDelta=0;
m_innerMargin=10;
m_outerMargin=10;
m_n_js = 0;
}
void SetDoubleSided(pdf_float_t inner=7, pdf_float_t outer=13)
{
if(outer != inner)
{
m_doubleSided=true;
m_outerMargin=outer;
m_innerMargin=inner;
}
}
void SetMargins(pdf_float_t left, pdf_float_t top, pdf_float_t right=0.0f)
{
// Set left, top and right margins
m_lMargin = left;
m_tMargin = top;
m_rMargin = (right==0.0f) ? left : right;
}
void SetLeftMargin(pdf_float_t margin)
{
// Set left margin
m_lMargin = margin;
if(m_page > 0 && m_x < margin)
m_x = margin;
}
inline void SetTopMargin(pdf_float_t margin)
{
// Set top margin
m_tMargin = margin;
}
inline void SetRightMargin(pdf_float_t margin)
{
// Set right margin
m_rMargin = margin;
}
inline pdf_float_t GetLeftMargin()
{
return m_lMargin;
}
inline pdf_float_t GeRightMargin()
{
return m_rMargin;
}
void SetAutoPageBreak(bool b, pdf_float_t margin=0.0f)
{
// Set auto page break mode and triggering margin
m_AutoPageBreak = b;
m_bMargin = margin;
m_PageBreakTrigger = m_h-margin;
}
void CheckPageBreak(pdf_float_t height)
{
//If the height h would cause an overflow, add a new page immediately
if(GetY()+height > m_PageBreakTrigger)
AddPage(m_CurOrientation);
}
inline void setCustomZoom(int zoom)
{
m_CustomZoom = zoom;
}
inline int getCustomZoom()
{
return m_CustomZoom;
}
void SetDisplayMode(e_zoom_mode zoom, e_layout_mode layout=e_layout_default)
{
// Set display mode in viewer
if(zoom==e_zoom_fullpage || zoom==e_zoom_fullwidth ||
zoom==e_zoom_real || zoom==e_zoom_default)
m_ZoomMode = zoom;
else
Error("Incorrect zoom display mode %d", zoom);
if(layout==e_layout_single || layout==e_layout_continuous ||
layout==e_layout_two || layout==e_layout_default)
m_LayoutMode = layout;
else
Error("Incorrect layout display mode: %d", layout);
}
void SetCompression(bool compress)
{
// Set page compression
#ifndef PDF_USING_ZLIB
m_compress = false;
#else
m_compress = compress;
#endif
}
void SetTitle(const char *title)
{
// Title of document
m_title = title;
}
void SetSubject(const char *subject)
{
// Subject of document
m_subject = subject;
}
void SetAuthor(const char *author)
{
// Author of document
m_author = author;
}
void SetKeywords(const char *keywords)
{
// Keywords of document
m_keywords = keywords;
}
void SetCreator(const char *creator)
{
// Creator of document
m_creator = creator;
}
void AliasNbPages(const char *alias="{nb}")
{
// Define an alias for total number of pages
m_AliasNbPages = alias;
}
inline const std::string &GetAliasNbPages()
{
// Define an alias for total number of pages
return m_AliasNbPages;
}
void Error(const char *msg, ...)
{
// Fatal error
va_list args;
va_start( args, msg );
pdf_vsnprintf(m_scratch_buf, sizeof(m_scratch_buf), msg, args);
va_end( args );
std::string error_msg = "FPDF error: ";
error_msg += m_scratch_buf;
throw error_msg;
}
inline void Open()
{
// Begin document
m_state = 1;
}
void Close()
{
// Terminate document
if(m_state==3) return;
if(m_page==0) AddPage();
// Page footer
m_InFooter = true;
Footer();
m_InFooter = false;
// Close page
_endpage();
// Close document
_enddoc();
}
void AddPage(e_orientation orientation=e_orientation_none, st_pagesize *psize=0)
{
// Start a new page
if(m_state==0) Open();
std::string family = m_FontFamily;
std::string style = m_FontStyle;
if(m_underline) style += "U";
pdf_float_t fontsize = m_FontSizePt;
pdf_float_t lw = m_LineWidth;
std::string dc = m_DrawColor;
std::string fc = m_FillColor;
std::string tc = m_TextColor;
bool cf = m_ColorFlag;
if(m_page > 0)
{
// Page footer
m_InFooter = true;
Footer();
m_InFooter = false;
// Close page
_endpage();
}
// Start new page
_beginpage(orientation,psize);
// Set line cap style to square
_out("2 J");
// Set line width
m_LineWidth = lw;
_outfmt(true, "%.2f w", lw*m_k);
// Set font
if(!family.empty()) SetFont(family.c_str(),style.c_str(),fontsize);
// Set colors
m_DrawColor = dc;
if(dc != "0 G") _out(dc);
m_FillColor = fc;
if(fc != "0 g") _out(fc);
m_TextColor = tc;
m_ColorFlag = cf;
// Page header
m_InHeader = true;
Header();
m_InHeader = false;
// Restore line width
if(m_LineWidth != lw)
{
m_LineWidth = lw;
_outfmt(true, "%.2f w", lw*m_k);
}
// Restore font
if(!family.empty()) SetFont(family.c_str(),style.c_str(),fontsize);
// Restore colors
if(m_DrawColor != dc)
{
m_DrawColor = dc;
_out(dc);
}
if(m_FillColor != fc)
{
m_FillColor = fc;
_out(fc);
}
m_TextColor = tc;
m_ColorFlag = cf;
}
virtual void Header()
{
// To be implemented in your own inherited class
}
virtual void Footer()
{
// To be implemented in your own inherited class
}
inline int PageNo()
{
// Get current page number
return m_page;
}
void SetDrawColor(unsigned char r)
{
// Set color for all stroking operations
m_DrawColor_rgb = {r,r,r,0};
pdf_sprintf(m_DrawColor, "%.3f G", r/255.0);
if(m_page>0) _out(m_DrawColor);
}
void SetDrawColor(unsigned char r, unsigned char g, unsigned char b)
{
// Set color for all stroking operations
m_DrawColor_rgb = {r,g,b,0};
pdf_sprintf(m_DrawColor, "%.3f %.3f %.3f RG", r/255.0, g/255.0, b/255.0);
if(m_page>0) _out(m_DrawColor);
}
inline void SetDrawColor(pdf_color_t &color)
{
SetDrawColor(color.r, color.g, color.b);
}
inline void GetDrawColor(pdf_color_t &color)
{
color = m_DrawColor_rgb;
}
void SetFillColor(unsigned char r)
{
// Set color for all filling operations
m_FillColor_rgb = {r,r,r,0};
pdf_sprintf(m_FillColor, "%.3f g", r/255.0);
m_ColorFlag = (m_FillColor != m_TextColor);
if(m_page>0) _out(m_FillColor);
}
void SetFillColor(unsigned char r, unsigned char g, unsigned char b)
{
// Set color for all filling operations
m_FillColor_rgb = {r,g,b,0};
pdf_sprintf(m_FillColor, "%.3f %.3f %.3f rg", r/255.0, g/255.0, b/255.0);
m_ColorFlag = (m_FillColor != m_TextColor);
if(m_page>0) _out(m_FillColor);
}
inline void SetFillColor(pdf_color_t &color)
{
SetFillColor(color.r, color.g, color.b);
}
inline void GetFillColor(pdf_color_t &color)
{
color = m_FillColor_rgb;
}
void SetTextColor(unsigned char r)
{
// Set color for text
m_TextColor_rgb = {r,r,r,0};
pdf_sprintf(m_TextColor, "%.3f g", r/255.0);
m_ColorFlag = (m_FillColor != m_TextColor);
}
void SetTextColor(unsigned char r, unsigned char g, unsigned char b)
{
// Set color for text
m_TextColor_rgb = {r,g,b,0};
pdf_sprintf(m_TextColor, "%.3f %.3f %.3f rg", r/255.0, g/255.0, b/255.0);
m_ColorFlag = (m_FillColor != m_TextColor);
}
inline void SetTextColor(pdf_color_t &color)
{
SetTextColor(color.r, color.g, color.b);
}
inline void GetTextColor(pdf_color_t &color)
{
color = m_TextColor_rgb;
}
// alpha: real value from 0 (transparent) to 1 (opaque)
// bm: blend mode, one of the following:
// Normal, Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn,
// HardLight, SoftLight, Difference, Exclusion, Hue, Saturation, Color, Luminosity
void SetAlpha(pdf_float_t alpha, const char *bm=0)
{
// set alpha for stroking (CA) and non-stroking (ca) operations
st_alpha_t gs;
gs.alpha = alpha;
gs.bm = bm ? bm : "Normal";
m_extgstates.push_back(gs);
_outfmt(true, "/GS%d gs", m_extgstates.size());
}
pdf_float_t GetStringWidth(const char *s)
{
// Get width of a string in the current font
font_width_t *cw = m_CurrentFont->font.cw;
pdf_float_t w = 0.0;
if(s)
{
while(*s)
{
w += cw[(unsigned char)*s++];
}
}
return w*m_FontSize/1000.0;
}
inline pdf_float_t GetStringWidth(const std::string &s)
{
return GetStringWidth(s.c_str());
}
void SetLineWidth(pdf_float_t width)
{
// Set line width
m_LineWidth = width;
if(m_page>0) _outfmt(true, "%.2f w", width*m_k);
}
void SetDash(pdf_float_t black=-1, pdf_float_t white=-1)
{
if(black >= 0.0f && white >= 0.0f)
_outfmt(true, "[%.3f %.3f] 0 d", black*m_k, white*m_k);
else
_out("[] 0 d");
}
void Line(pdf_float_t x1, pdf_float_t y1, pdf_float_t x2, pdf_float_t y2)
{
// Draw a line
_outfmt(true, "%.2f %.2f m %.2f %.2f l S", x1*m_k,(m_h-y1)*m_k,x2*m_k,(m_h-y2)*m_k);
}
void Rect(pdf_float_t x, pdf_float_t y, pdf_float_t w, pdf_float_t h, const char *style=0)
{
// Draw a rectangle
const char *op = "S";
if(style)
{
if(strcasecmp(style, "F") == 0) op = "f";
else if( (strcasecmp(style, "FD") == 0) ||
(strcasecmp(style, "DF") == 0) ) op = "B";
}
_outfmt(true, "%.2f %.2f %.2f %.2f re %s", x*m_k,(m_h-y)*m_k,w*m_k,-h*m_k, op);
}
void AddFont(const char *afamily, const char *astyle=0, const char *afile=0)
{
// Add a TrueType, OpenType or Type1 font
std::string family, style;
if(afamily && afamily[0])
{
family = afamily;
_str_tolower(family);
}
else
{
family = m_FontFamily;
}
if(astyle && astyle[0])
{
style = astyle;
_str_tolower(style);
}
if(family=="arial") family = "helvetica";
if(style=="ib") style = "bi";
std::string fontkey = family + style;
font_map_t::iterator iter = m_fonts.find(fontkey);
if(iter != m_fonts.end()) return; //already inserted
st_pdf_font_base *font;
if(fontkey == "helveticai") font = new pdf_font_HelveticaOblique();
else if(fontkey == "helveticab") font = new pdf_font_HelveticaBold();
else if(fontkey == "helveticabi") font = new pdf_font_HelveticaBoldOblique();
else if(fontkey == "times") font = new pdf_font_Times();
else if(fontkey == "timesi") font = new pdf_font_TimesOblique();
else if(fontkey == "timesb") font = new pdf_font_TimesBold();
else if(fontkey == "timesbi") font = new pdf_font_TimesBoldOblique();
else if(fontkey == "courier") font = new pdf_font_Courier();
else if(fontkey == "courieri") font = new pdf_font_CourierOblique();
else if(fontkey == "courierb") font = new pdf_font_CourierBold();
else if(fontkey == "courierbi") font = new pdf_font_CourierBoldOblique();
else if(fontkey == "symbol") font = new pdf_font_Symbol();
else if(fontkey == "zapfdingbats") font = new pdf_font_ZapfDingbats();
else font = new pdf_font_Helvetica();
m_fonts[fontkey] = font;
font->i = m_fonts.size();
#if 0
if(m_fonts.find(fontkey) == -1) return;
st_pdf_font info = _loadfont(file);
info.i = m_fonts.size()+1;
if(!empty(info["diff"]))
{
// Search existing encodings
n = array_search(info["diff"],m_diffs);
if(!n)
{
n = count(m_diffs)+1;
m_diffs[n] = info["diff"];
}
info["diffn"] = n;
}
if(!empty(info["file"]))
{
// Embedded font
if(info["type"]=="TrueType")
m_FontFiles[info["file"]] = array("length1"=>info["originalsize"]);
else
m_FontFiles[info["file"]] = array("length1"=>info["size1"], "length2"=>info["size2"]);
}
m_fonts[fontkey] = info;
#endif
}
void SetFont(const char *afamily=0, const char *astyle=0, pdf_float_t size=0)
{
// Select a font; size given in points
std::string family, style;
if(afamily && afamily[0])
{
family = afamily;
_str_tolower(family);
if(family=="arial") family = "helvetica";
}
else
{
family = m_FontFamily;
}
if(astyle && astyle[0])
{
style = astyle;
_str_tolower(style);
}
int found = style.find("u");
if(found != std::string::npos )
{
m_underline = true;
style.erase(found, 1);
}
else
m_underline = false;
if(style=="ib") style = "bi";
if(size==0) size = m_FontSizePt;
// Test if font is already selected
if(m_FontFamily==family && m_FontStyle==style && m_FontSizePt==size)
return;
// Test if font is already loaded
std::string fontkey = family + style;
font_map_t::iterator iter = m_fonts.find(fontkey);
if(iter == m_fonts.end())
{
// Test if one of the core fonts
if(isPdfFontCore(family.c_str()))
{
if(family=="symbol" || family=="zapfdingbats") style.clear();
fontkey = family + style;
iter = m_fonts.find(fontkey);
if(iter == m_fonts.end()) AddFont(family.c_str(),style.c_str());
}
else
Error("Undefined font: %s %s", family.c_str(), style.c_str());
}
// Select it
m_FontFamily = family;
m_FontStyle = style;
m_FontSizePt = size;
m_FontSize = size/m_k;
m_CurrentFont = m_fonts[fontkey];
if(m_page>0)
_outfmt(true, "BT /F%d %.2f Tf ET", m_CurrentFont->i, m_FontSizePt);
}
void SetFontSize(pdf_float_t size)
{
// Set font size in points
if(m_FontSizePt==size) return;
m_FontSizePt = size;
m_FontSize = size/m_k;
if(m_page>0)
_outfmt(true, "BT /F%d %.2f Tf ET", m_CurrentFont->i, m_FontSizePt);
}
inline pdf_float_t GetFontSize()
{
return m_FontSizePt;
}
int AddLink()
{
// Create a new internal link
int n = m_links.size();
st_link link = {0,0};
m_links.push_back(link);
return n;
}
void SetLink(int link, pdf_float_t y=0, int page=-1)
{
// Set destination of internal link
st_link &nlink = m_links[link];
if(y==-1) nlink.to = m_y;
if(page==-1) page = m_page;
nlink.from = page;
}
void Link(pdf_float_t x, pdf_float_t y, pdf_float_t w, pdf_float_t h, int link)
{
// Put a link on the page
st_page_link pl;
pl.x = x*m_k;
pl.y = m_hPt-y*m_k;
pl.w = w*m_k;
pl.h = h*m_k;
pl.link = link;
m_PageLinks[m_page] = pl;