fpdf.cpp 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911
  1. // fpdf.cpp
  2. //
  3. #include "fpdf.h"
  4. #line 12 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  5. //#define PDF_USING_ZLIB 1
  6. #define FPDF_VERSION "1.7"
  7. //#include "sqlite3.h"
  8. //#define pdf_vsnprintf sqlite3_ivsnprintf //vsnprintf
  9. //#define pdf_snprintf sqlite3_isnprintf //snprintf
  10. #define pdf_vsnprintf vsnprintf //vsnprintf
  11. #define pdf_snprintf snprintf //snprintf
  12. #include <cstdarg>
  13. #include <cstdio>
  14. #include <cstring>
  15. #include <ctime>
  16. #include <cmath>
  17. #ifdef PDF_USING_ZLIB
  18. #include "zlib.h"
  19. #endif
  20. //#include "duma.h"
  21. #line 2252 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  22. #define M_SOF0 0xC0 // Start Of Frame N
  23. #define M_SOF1 0xC1 // N indicates which compression process
  24. #define M_SOF2 0xC2 // Only SOF0-SOF2 are now in common use
  25. #define M_SOF3 0xC3
  26. #define M_SOF5 0xC5 // NB: codes C4 and CC are NOT SOF markers
  27. #define M_SOF6 0xC6
  28. #define M_SOF7 0xC7
  29. #define M_SOF9 0xC9
  30. #define M_SOF10 0xCA
  31. #define M_SOF11 0xCB
  32. #define M_SOF13 0xCD
  33. #define M_SOF14 0xCE
  34. #define M_SOF15 0xCF
  35. #define M_SOI 0xD8
  36. #define M_EOI 0xD9 // End Of Image (end of datastream)
  37. #define M_SOS 0xDA // Start Of Scan (begins compressed data)
  38. #define M_COM 0xFE // COMment
  39. #define M_PSEUDO 0xFFD8 // pseudo marker for start of image(byte 0)
  40. #define LZZ_INLINE inline
  41. #ifdef PDF_USING_ZLIB
  42. #line 30 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  43. std::string & gzcompress (std::string & dest, std::string const & src)
  44. #line 31 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  45. {
  46. z_stream stream;
  47. int err;
  48. int nExtraChunks;
  49. size_t src_len = src.size();
  50. size_t dest_len = 2048 + src_len;
  51. dest.resize(dest_len);
  52. stream.next_in = (Bytef*)&src[0];
  53. stream.avail_in = (uInt)src_len;
  54. stream.zalloc = (alloc_func)0;
  55. stream.zfree = (free_func)0;
  56. stream.opaque = (voidpf)0;
  57. //err = deflateInit(&stream, Z_DEFAULT_COMPRESSION);
  58. err = deflateInit(&stream, Z_BEST_SPEED);
  59. if (err != Z_OK) throw "Could not initialize zstream !";
  60. nExtraChunks = 0;
  61. do
  62. {
  63. stream.next_out = (unsigned char*)&dest[0];
  64. stream.avail_out = dest_len;
  65. err = deflate(&stream, Z_FINISH);
  66. if (err == Z_STREAM_END )
  67. break;
  68. if (err != Z_OK)
  69. {
  70. deflateEnd(&stream);
  71. throw "Could not uncompress data !";
  72. }
  73. nExtraChunks += 1;
  74. }
  75. while (stream.avail_out == 0);
  76. dest.resize(stream.total_out);
  77. err = deflateEnd(&stream);
  78. if (nExtraChunks || (err != Z_OK)) throw "Could finalize zstream !";
  79. return dest;
  80. }
  81. #line 75 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  82. std::string & gzuncompress (std::string & dest, std::string const & src)
  83. #line 76 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  84. {
  85. z_stream stream;
  86. int err;
  87. int nExtraChunks;
  88. size_t src_len = src.size();
  89. size_t dest_len = 2048 + src_len*5;
  90. dest.resize(dest_len);
  91. stream.next_in = (Bytef*)&src[0];
  92. stream.avail_in = (uInt)src_len;
  93. stream.zalloc = (alloc_func)0;
  94. stream.zfree = (free_func)0;
  95. err = inflateInit(&stream);
  96. if (err != Z_OK) throw "Could not initialize zstream !";
  97. nExtraChunks = 0;
  98. do
  99. {
  100. stream.next_out = (unsigned char*)&dest[0];
  101. stream.avail_out = dest_len;
  102. err = inflate(&stream, Z_FINISH);
  103. if (err == Z_STREAM_END )
  104. break;
  105. if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
  106. err = Z_DATA_ERROR;
  107. if (err != Z_BUF_ERROR)
  108. {
  109. inflateEnd(&stream);
  110. throw "Could not uncompress data !";
  111. }
  112. nExtraChunks += 1;
  113. }
  114. while (stream.avail_out == 0);
  115. dest.resize(stream.total_out);
  116. err = inflateEnd(&stream);
  117. if (nExtraChunks || (err != Z_OK)) throw "Could finalize zstream !";
  118. return dest;
  119. }
  120. #endif
  121. #line 132 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  122. char const * FPDF_FONTPATH = 0;
  123. #line 261 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  124. FPDF::~ FPDF ()
  125. #line 262 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  126. {
  127. font_map_t::iterator iter = m_fonts.begin();
  128. font_map_t::iterator iend = m_fonts.end();
  129. while(iter != iend)
  130. {
  131. delete iter->second;
  132. ++iter;
  133. }
  134. }
  135. #line 272 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  136. FPDF::FPDF ()
  137. #line 273 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  138. {
  139. reset(e_orientation_portrait, e_mm, e_A4);
  140. }
  141. #line 277 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  142. FPDF::FPDF (e_orientation orientation, e_units unit, e_page_sizes psize)
  143. #line 278 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  144. {
  145. reset(orientation, unit, psize);
  146. }
  147. #line 282 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  148. FPDF::FPDF (char orientation, char const * unit, char const * psize)
  149. #line 283 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  150. {
  151. e_orientation eo = orientation == 'L' ? e_orientation_landscape : e_orientation_portrait;
  152. e_units eu;
  153. e_page_sizes es;
  154. if(strcasecmp(unit, "pt")==0) eu = e_pt;
  155. else if(strcasecmp(unit, "cm")==0) eu = e_cm;
  156. else if(strcasecmp(unit, "in")==0) eu = e_in;
  157. else eu = e_mm;
  158. if(strcasecmp(psize, "a3")==0) es = e_A3;
  159. else if(strcasecmp(psize, "a5")==0) es = e_A5;
  160. else if(strcasecmp(psize, "letter")==0) es = e_Letter;
  161. else if(strcasecmp(psize, "legal")==0) es = e_Legal;
  162. else es = e_A4;
  163. reset(eo, eu, es);
  164. }
  165. #line 301 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  166. void FPDF::reset (e_orientation orientation, e_units unit, e_page_sizes psize)
  167. #line 302 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  168. {
  169. // Scale factor
  170. switch(unit)
  171. {
  172. case e_pt:
  173. m_k = 1;
  174. break;
  175. case e_mm:
  176. m_k = 72/25.4;
  177. break;
  178. case e_cm:
  179. m_k = 72/2.54;
  180. break;
  181. case e_in:
  182. m_k = 72;
  183. break;
  184. default:
  185. Error("Incorrect unit: %s", unit);
  186. }
  187. // Some checks
  188. _dochecks();
  189. // Initialization of properties
  190. m_page = 0;
  191. m_pages.clear();
  192. m_PageSizes.clear();
  193. m_offsets.clear();
  194. m_fonts.clear();
  195. m_FontFiles.clear();
  196. m_diffs.clear();
  197. m_images.clear();
  198. m_PageLinks.clear();
  199. m_links.clear();
  200. m_extgstates.clear();
  201. m_offsets.resize(3, 0);
  202. m_n = 2;
  203. m_buffer.clear();
  204. m_buffer.reserve(1024);
  205. //this->buffer = "";
  206. //this->m_pages = array();
  207. //this->PageSizes = array();
  208. m_state = 0;
  209. //this->fonts = array();
  210. //this->FontFiles = array();
  211. //this->diffs = array();
  212. //this->images = array();
  213. //this->links = array();
  214. m_InHeader = false;
  215. m_InFooter = false;
  216. m_lasth = 0;
  217. //this->FontFamily = "";
  218. //this->FontStyle = "";
  219. m_FontSizePt = 12;
  220. m_underline = false;
  221. m_DrawColor = "0 G";
  222. m_DrawColor_rgb = {0,0,0,0};
  223. m_FillColor = "0 g";
  224. m_FillColor_rgb = {0,0,0,0};
  225. m_TextColor = "0 g";
  226. m_TextColor_rgb = {0,0,0,0};
  227. m_ColorFlag = false;
  228. m_ws = 0;
  229. // Font path
  230. if(FPDF_FONTPATH)
  231. {
  232. m_fontpath = FPDF_FONTPATH;
  233. char clast = *m_fontpath.rbegin();
  234. if(clast != '/' && clast != '\\')
  235. m_fontpath += "/";
  236. }
  237. #if 0
  238. else if(is_dir(dirname(__FILE__) + "/font"))
  239. m_fontpath = dirname(__FILE__) + "/font/";
  240. #endif
  241. else
  242. m_fontpath.clear();
  243. m_CurrentFont = 0;
  244. st_pagesize page_size;
  245. _getpagesize(page_size, psize);
  246. m_DefPageSize = page_size;
  247. m_CurPageSize = page_size;
  248. // Page orientation
  249. switch(orientation)
  250. {
  251. case e_orientation_portrait:
  252. {
  253. m_DefOrientation = e_orientation_portrait;
  254. m_w = page_size.w;
  255. m_h = page_size.h;
  256. }
  257. break;
  258. case e_orientation_landscape:
  259. {
  260. m_DefOrientation = e_orientation_landscape;
  261. m_w = page_size.h;
  262. m_h = page_size.w;
  263. }
  264. break;
  265. default:
  266. Error("Incorrect orientation: %d", orientation);
  267. }
  268. m_CurOrientation = m_DefOrientation;
  269. m_wPt = m_w * m_k;
  270. m_hPt = m_h * m_k;
  271. m_angle = 0;
  272. // Page margins (1 cm)
  273. pdf_float_t margin = 28.35/m_k;
  274. SetMargins(margin,margin);
  275. // Interior cell margin (1 mm)
  276. m_cMargin = margin/10.0;
  277. // Line width (0.2 mm)
  278. m_LineWidth = .567/m_k;
  279. // Automatic page break
  280. SetAutoPageBreak(true,2*margin);
  281. // Default display mode
  282. SetDisplayMode(e_zoom_default);
  283. m_CustomZoom = 0;
  284. // Enable compression
  285. SetCompression(true);
  286. // Set default PDF version number
  287. m_PDFVersion = "1.3";
  288. m_doubleSided=false;
  289. m_xDelta=0;
  290. m_innerMargin=10;
  291. m_outerMargin=10;
  292. m_n_js = 0;
  293. }
  294. #line 434 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  295. void FPDF::SetDoubleSided (pdf_float_t inner, pdf_float_t outer)
  296. #line 435 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  297. {
  298. if(outer != inner)
  299. {
  300. m_doubleSided=true;
  301. m_outerMargin=outer;
  302. m_innerMargin=inner;
  303. }
  304. }
  305. #line 444 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  306. void FPDF::SetMargins (pdf_float_t left, pdf_float_t top, pdf_float_t right)
  307. #line 445 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  308. {
  309. // Set left, top and right margins
  310. m_lMargin = left;
  311. m_tMargin = top;
  312. m_rMargin = (right==0.0f) ? left : right;
  313. }
  314. #line 452 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  315. void FPDF::SetLeftMargin (pdf_float_t margin)
  316. #line 453 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  317. {
  318. // Set left margin
  319. m_lMargin = margin;
  320. if(m_page > 0 && m_x < margin)
  321. m_x = margin;
  322. }
  323. #line 482 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  324. void FPDF::SetAutoPageBreak (bool b, pdf_float_t margin)
  325. #line 483 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  326. {
  327. // Set auto page break mode and triggering margin
  328. m_AutoPageBreak = b;
  329. m_bMargin = margin;
  330. m_PageBreakTrigger = m_h-margin;
  331. }
  332. #line 490 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  333. void FPDF::CheckPageBreak (pdf_float_t height)
  334. #line 491 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  335. {
  336. //If the height h would cause an overflow, add a new page immediately
  337. if(GetY()+height > m_PageBreakTrigger)
  338. AddPage(m_CurOrientation);
  339. }
  340. #line 507 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  341. void FPDF::SetDisplayMode (e_zoom_mode zoom, e_layout_mode layout)
  342. #line 508 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  343. {
  344. // Set display mode in viewer
  345. if(zoom==e_zoom_fullpage || zoom==e_zoom_fullwidth ||
  346. zoom==e_zoom_real || zoom==e_zoom_default)
  347. m_ZoomMode = zoom;
  348. else
  349. Error("Incorrect zoom display mode %d", zoom);
  350. if(layout==e_layout_single || layout==e_layout_continuous ||
  351. layout==e_layout_two || layout==e_layout_default)
  352. m_LayoutMode = layout;
  353. else
  354. Error("Incorrect layout display mode: %d", layout);
  355. }
  356. #line 522 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  357. void FPDF::SetCompression (bool compress)
  358. #line 523 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  359. {
  360. // Set page compression
  361. #ifndef PDF_USING_ZLIB
  362. m_compress = false;
  363. #else
  364. m_compress = compress;
  365. #endif
  366. }
  367. #line 532 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  368. void FPDF::SetTitle (char const * title)
  369. #line 533 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  370. {
  371. // Title of document
  372. m_title = title;
  373. }
  374. #line 538 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  375. void FPDF::SetSubject (char const * subject)
  376. #line 539 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  377. {
  378. // Subject of document
  379. m_subject = subject;
  380. }
  381. #line 544 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  382. void FPDF::SetAuthor (char const * author)
  383. #line 545 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  384. {
  385. // Author of document
  386. m_author = author;
  387. }
  388. #line 550 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  389. void FPDF::SetKeywords (char const * keywords)
  390. #line 551 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  391. {
  392. // Keywords of document
  393. m_keywords = keywords;
  394. }
  395. #line 556 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  396. void FPDF::SetCreator (char const * creator)
  397. #line 557 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  398. {
  399. // Creator of document
  400. m_creator = creator;
  401. }
  402. #line 562 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  403. void FPDF::AliasNbPages (char const * alias)
  404. #line 563 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  405. {
  406. // Define an alias for total number of pages
  407. m_AliasNbPages = alias;
  408. }
  409. #line 574 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  410. void FPDF::Error (char const * msg, ...)
  411. #line 575 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  412. {
  413. // Fatal error
  414. va_list args;
  415. va_start( args, msg );
  416. pdf_vsnprintf(m_scratch_buf, sizeof(m_scratch_buf), msg, args);
  417. va_end( args );
  418. std::string error_msg = "FPDF error: ";
  419. error_msg += m_scratch_buf;
  420. throw error_msg;
  421. }
  422. #line 592 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  423. void FPDF::Close ()
  424. #line 593 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  425. {
  426. // Terminate document
  427. if(m_state==3) return;
  428. if(m_page==0) AddPage();
  429. // Page footer
  430. m_InFooter = true;
  431. Footer();
  432. m_InFooter = false;
  433. // Close page
  434. _endpage();
  435. // Close document
  436. _enddoc();
  437. }
  438. #line 607 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  439. void FPDF::AddPage (e_orientation orientation, st_pagesize * psize)
  440. #line 608 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  441. {
  442. // Start a new page
  443. if(m_state==0) Open();
  444. std::string family = m_FontFamily;
  445. std::string style = m_FontStyle;
  446. if(m_underline) style += "U";
  447. pdf_float_t fontsize = m_FontSizePt;
  448. pdf_float_t lw = m_LineWidth;
  449. std::string dc = m_DrawColor;
  450. std::string fc = m_FillColor;
  451. std::string tc = m_TextColor;
  452. bool cf = m_ColorFlag;
  453. if(m_page > 0)
  454. {
  455. // Page footer
  456. m_InFooter = true;
  457. Footer();
  458. m_InFooter = false;
  459. // Close page
  460. _endpage();
  461. }
  462. // Start new page
  463. _beginpage(orientation,psize);
  464. // Set line cap style to square
  465. _out("2 J");
  466. // Set line width
  467. m_LineWidth = lw;
  468. _outfmt(true, "%.2f w", lw*m_k);
  469. // Set font
  470. if(!family.empty()) SetFont(family.c_str(),style.c_str(),fontsize);
  471. // Set colors
  472. m_DrawColor = dc;
  473. if(dc != "0 G") _out(dc);
  474. m_FillColor = fc;
  475. if(fc != "0 g") _out(fc);
  476. m_TextColor = tc;
  477. m_ColorFlag = cf;
  478. // Page header
  479. m_InHeader = true;
  480. Header();
  481. m_InHeader = false;
  482. // Restore line width
  483. if(m_LineWidth != lw)
  484. {
  485. m_LineWidth = lw;
  486. _outfmt(true, "%.2f w", lw*m_k);
  487. }
  488. // Restore font
  489. if(!family.empty()) SetFont(family.c_str(),style.c_str(),fontsize);
  490. // Restore colors
  491. if(m_DrawColor != dc)
  492. {
  493. m_DrawColor = dc;
  494. _out(dc);
  495. }
  496. if(m_FillColor != fc)
  497. {
  498. m_FillColor = fc;
  499. _out(fc);
  500. }
  501. m_TextColor = tc;
  502. m_ColorFlag = cf;
  503. }
  504. #line 672 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  505. void FPDF::Header ()
  506. #line 673 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  507. {}
  508. #line 677 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  509. void FPDF::Footer ()
  510. #line 678 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  511. {}
  512. #line 688 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  513. void FPDF::SetDrawColor (unsigned char r)
  514. #line 689 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  515. {
  516. // Set color for all stroking operations
  517. m_DrawColor_rgb = {r,r,r,0};
  518. pdf_sprintf(m_DrawColor, "%.3f G", r/255.0);
  519. if(m_page>0) _out(m_DrawColor);
  520. }
  521. #line 696 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  522. void FPDF::SetDrawColor (unsigned char r, unsigned char g, unsigned char b)
  523. #line 697 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  524. {
  525. // Set color for all stroking operations
  526. m_DrawColor_rgb = {r,g,b,0};
  527. pdf_sprintf(m_DrawColor, "%.3f %.3f %.3f RG", r/255.0, g/255.0, b/255.0);
  528. if(m_page>0) _out(m_DrawColor);
  529. }
  530. #line 712 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  531. void FPDF::SetFillColor (unsigned char r)
  532. #line 713 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  533. {
  534. // Set color for all filling operations
  535. m_FillColor_rgb = {r,r,r,0};
  536. pdf_sprintf(m_FillColor, "%.3f g", r/255.0);
  537. m_ColorFlag = (m_FillColor != m_TextColor);
  538. if(m_page>0) _out(m_FillColor);
  539. }
  540. #line 721 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  541. void FPDF::SetFillColor (unsigned char r, unsigned char g, unsigned char b)
  542. #line 722 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  543. {
  544. // Set color for all filling operations
  545. m_FillColor_rgb = {r,g,b,0};
  546. pdf_sprintf(m_FillColor, "%.3f %.3f %.3f rg", r/255.0, g/255.0, b/255.0);
  547. m_ColorFlag = (m_FillColor != m_TextColor);
  548. if(m_page>0) _out(m_FillColor);
  549. }
  550. #line 738 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  551. void FPDF::SetTextColor (unsigned char r)
  552. #line 739 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  553. {
  554. // Set color for text
  555. m_TextColor_rgb = {r,r,r,0};
  556. pdf_sprintf(m_TextColor, "%.3f g", r/255.0);
  557. m_ColorFlag = (m_FillColor != m_TextColor);
  558. }
  559. #line 746 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  560. void FPDF::SetTextColor (unsigned char r, unsigned char g, unsigned char b)
  561. #line 747 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  562. {
  563. // Set color for text
  564. m_TextColor_rgb = {r,g,b,0};
  565. pdf_sprintf(m_TextColor, "%.3f %.3f %.3f rg", r/255.0, g/255.0, b/255.0);
  566. m_ColorFlag = (m_FillColor != m_TextColor);
  567. }
  568. #line 766 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  569. void FPDF::SetAlpha (pdf_float_t alpha, char const * bm)
  570. #line 767 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  571. {
  572. // set alpha for stroking (CA) and non-stroking (ca) operations
  573. st_alpha_t gs;
  574. gs.alpha = alpha;
  575. gs.bm = bm ? bm : "Normal";
  576. m_extgstates.push_back(gs);
  577. _outfmt(true, "/GS%d gs", m_extgstates.size());
  578. }
  579. #line 776 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  580. pdf_float_t FPDF::GetStringWidth (char const * s)
  581. #line 777 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  582. {
  583. // Get width of a string in the current font
  584. font_width_t *cw = m_CurrentFont->font.cw;
  585. pdf_float_t w = 0.0;
  586. if(s)
  587. {
  588. while(*s)
  589. {
  590. w += cw[(unsigned char)*s++];
  591. }
  592. }
  593. return w*m_FontSize/1000.0;
  594. }
  595. #line 796 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  596. void FPDF::SetLineWidth (pdf_float_t width)
  597. #line 797 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  598. {
  599. // Set line width
  600. m_LineWidth = width;
  601. if(m_page>0) _outfmt(true, "%.2f w", width*m_k);
  602. }
  603. #line 803 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  604. void FPDF::SetDash (pdf_float_t black, pdf_float_t white)
  605. #line 804 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  606. {
  607. if(black >= 0.0f && white >= 0.0f)
  608. _outfmt(true, "[%.3f %.3f] 0 d", black*m_k, white*m_k);
  609. else
  610. _out("[] 0 d");
  611. }
  612. #line 811 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  613. void FPDF::Line (pdf_float_t x1, pdf_float_t y1, pdf_float_t x2, pdf_float_t y2)
  614. #line 812 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  615. {
  616. // Draw a line
  617. _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);
  618. }
  619. #line 817 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  620. void FPDF::Rect (pdf_float_t x, pdf_float_t y, pdf_float_t w, pdf_float_t h, char const * style)
  621. #line 818 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  622. {
  623. // Draw a rectangle
  624. const char *op = "S";
  625. if(style)
  626. {
  627. if(strcasecmp(style, "F") == 0) op = "f";
  628. else if( (strcasecmp(style, "FD") == 0) ||
  629. (strcasecmp(style, "DF") == 0) ) op = "B";
  630. }
  631. _outfmt(true, "%.2f %.2f %.2f %.2f re %s", x*m_k,(m_h-y)*m_k,w*m_k,-h*m_k, op);
  632. }
  633. #line 830 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  634. void FPDF::AddFont (char const * afamily, char const * astyle, char const * afile)
  635. #line 831 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  636. {
  637. // Add a TrueType, OpenType or Type1 font
  638. std::string family, style;
  639. if(afamily && afamily[0])
  640. {
  641. family = afamily;
  642. _str_tolower(family);
  643. }
  644. else
  645. {
  646. family = m_FontFamily;
  647. }
  648. if(astyle && astyle[0])
  649. {
  650. style = astyle;
  651. _str_tolower(style);
  652. }
  653. if(family=="arial") family = "helvetica";
  654. if(style=="ib") style = "bi";
  655. std::string fontkey = family + style;
  656. font_map_t::iterator iter = m_fonts.find(fontkey);
  657. if(iter != m_fonts.end()) return; //already inserted
  658. st_pdf_font_base *font;
  659. if(fontkey == "helveticai") font = new pdf_font_HelveticaOblique();
  660. else if(fontkey == "helveticab") font = new pdf_font_HelveticaBold();
  661. else if(fontkey == "helveticabi") font = new pdf_font_HelveticaBoldOblique();
  662. else if(fontkey == "times") font = new pdf_font_Times();
  663. else if(fontkey == "timesi") font = new pdf_font_TimesOblique();
  664. else if(fontkey == "timesb") font = new pdf_font_TimesBold();
  665. else if(fontkey == "timesbi") font = new pdf_font_TimesBoldOblique();
  666. else if(fontkey == "courier") font = new pdf_font_Courier();
  667. else if(fontkey == "courieri") font = new pdf_font_CourierOblique();
  668. else if(fontkey == "courierb") font = new pdf_font_CourierBold();
  669. else if(fontkey == "courierbi") font = new pdf_font_CourierBoldOblique();
  670. else if(fontkey == "symbol") font = new pdf_font_Symbol();
  671. else if(fontkey == "zapfdingbats") font = new pdf_font_ZapfDingbats();
  672. else font = new pdf_font_Helvetica();
  673. m_fonts[fontkey] = font;
  674. font->i = m_fonts.size();
  675. #if 0
  676. if(m_fonts.find(fontkey) == -1) return;
  677. st_pdf_font info = _loadfont(file);
  678. info.i = m_fonts.size()+1;
  679. if(!empty(info["diff"]))
  680. {
  681. // Search existing encodings
  682. n = array_search(info["diff"],m_diffs);
  683. if(!n)
  684. {
  685. n = count(m_diffs)+1;
  686. m_diffs[n] = info["diff"];
  687. }
  688. info["diffn"] = n;
  689. }
  690. if(!empty(info["file"]))
  691. {
  692. // Embedded font
  693. if(info["type"]=="TrueType")
  694. m_FontFiles[info["file"]] = array("length1"=>info["originalsize"]);
  695. else
  696. m_FontFiles[info["file"]] = array("length1"=>info["size1"], "length2"=>info["size2"]);
  697. }
  698. m_fonts[fontkey] = info;
  699. #endif
  700. }
  701. #line 904 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  702. void FPDF::GetFontSettings (font_settings_st & fs)
  703. #line 904 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  704. {
  705. fs.family = m_FontFamily;
  706. fs.style = m_FontStyle;
  707. fs.size = m_FontSizePt;
  708. }
  709. #line 910 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  710. void FPDF::SetFontSettings (font_settings_st & fs)
  711. #line 910 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  712. {
  713. SetFont(fs.family.c_str(), fs.style.c_str(), fs.size);
  714. }
  715. #line 914 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  716. void FPDF::SetFont (char const * afamily, char const * astyle, pdf_float_t size)
  717. #line 915 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  718. {
  719. // Select a font; size given in points
  720. std::string family, style;
  721. if(afamily && afamily[0])
  722. {
  723. family = afamily;
  724. _str_tolower(family);
  725. if(family=="arial") family = "helvetica";
  726. }
  727. else
  728. {
  729. family = m_FontFamily;
  730. }
  731. if(astyle && astyle[0])
  732. {
  733. style = astyle;
  734. _str_tolower(style);
  735. }
  736. size_t found = style.find("u");
  737. if(found != std::string::npos )
  738. {
  739. m_underline = true;
  740. style.erase(found, 1);
  741. }
  742. else
  743. m_underline = false;
  744. if(style=="ib") style = "bi";
  745. if(size==0) size = m_FontSizePt;
  746. // Test if font is already selected
  747. if(m_FontFamily==family && m_FontStyle==style && m_FontSizePt==size)
  748. return;
  749. // Test if font is already loaded
  750. std::string fontkey = family + style;
  751. font_map_t::iterator iter = m_fonts.find(fontkey);
  752. if(iter == m_fonts.end())
  753. {
  754. // Test if one of the core fonts
  755. if(isPdfFontCore(family.c_str()))
  756. {
  757. if(family=="symbol" || family=="zapfdingbats") style.clear();
  758. fontkey = family + style;
  759. iter = m_fonts.find(fontkey);
  760. if(iter == m_fonts.end()) AddFont(family.c_str(),style.c_str());
  761. }
  762. else
  763. Error("Undefined font: %s %s", family.c_str(), style.c_str());
  764. }
  765. // Select it
  766. m_FontFamily = family;
  767. m_FontStyle = style;
  768. m_FontSizePt = size;
  769. m_FontSize = size/m_k;
  770. m_CurrentFont = m_fonts[fontkey];
  771. if(m_page>0)
  772. _outfmt(true, "BT /F%d %.2f Tf ET", m_CurrentFont->i, m_FontSizePt);
  773. }
  774. #line 973 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  775. void FPDF::SetFontSize (pdf_float_t size)
  776. #line 974 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  777. {
  778. // Set font size in points
  779. if(m_FontSizePt==size) return;
  780. m_FontSizePt = size;
  781. m_FontSize = size/m_k;
  782. if(m_page>0)
  783. _outfmt(true, "BT /F%d %.2f Tf ET", m_CurrentFont->i, m_FontSizePt);
  784. }
  785. #line 988 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  786. int FPDF::AddLink ()
  787. #line 989 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  788. {
  789. // Create a new internal link
  790. int n = m_links.size();
  791. st_link link = {0,0};
  792. m_links.push_back(link);
  793. return n;
  794. }
  795. #line 997 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  796. void FPDF::SetLink (int link, pdf_float_t y, int page)
  797. #line 998 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  798. {
  799. // Set destination of internal link
  800. st_link &nlink = m_links[link];
  801. if(y==-1) nlink.to = m_y;
  802. if(page==-1) page = m_page;
  803. nlink.from = page;
  804. }
  805. #line 1006 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  806. void FPDF::Link (pdf_float_t x, pdf_float_t y, pdf_float_t w, pdf_float_t h, int link)
  807. #line 1007 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  808. {
  809. // Put a link on the page
  810. st_page_link pl;
  811. pl.x = x*m_k;
  812. pl.y = m_hPt-y*m_k;
  813. pl.w = w*m_k;
  814. pl.h = h*m_k;
  815. pl.link = link;
  816. m_PageLinks[m_page] = pl;
  817. }
  818. #line 1019 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  819. void FPDF::_TextBase (std::string & result, pdf_float_t x, pdf_float_t y, char const * txt)
  820. #line 1020 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  821. {
  822. // Output a string
  823. //because the format buffer is limited
  824. //we send the string separated from format
  825. if(m_ColorFlag)
  826. {
  827. result += "q " + m_TextColor + " ";
  828. }
  829. pdf_sprintf_append(result, "BT %.2f %.2f Td (", x, y);
  830. result += _escape(txt);
  831. result += ") Tj ET";
  832. if(m_underline && txt)
  833. {
  834. result += " " + _dounderline(x, y, txt);
  835. }
  836. if(m_ColorFlag) result += " Q";
  837. }
  838. #line 1040 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  839. void FPDF::Rotate (pdf_float_t angle, pdf_float_t x, pdf_float_t y)
  840. #line 1041 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  841. {
  842. if(x==-1) x=m_x;
  843. if(y==-1) y=m_y;
  844. if(m_angle!=0) _out("Q");
  845. m_angle=angle;
  846. if(angle!=0)
  847. {
  848. angle*=M_PI/180.0;
  849. pdf_float_t c=cos(angle);
  850. pdf_float_t s=sin(angle);
  851. pdf_float_t cx=x*m_k;
  852. pdf_float_t cy=(m_h-y)*m_k;
  853. _outfmt(true, "q %.5f %.5f %.5f %.5f %.2f %.2f cm 1 0 0 1 %.2f %.2f cm",
  854. c,s,-s,c,cx,cy,-cx,-cy);
  855. }
  856. }
  857. #line 1058 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  858. void FPDF::RotatedText (pdf_float_t x, pdf_float_t y, char const * txt, pdf_float_t angle)
  859. #line 1059 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  860. {
  861. //Text rotated around its origin
  862. Rotate(angle,x,y);
  863. Text(x,y,txt);
  864. Rotate(0);
  865. }
  866. #line 1071 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  867. void FPDF::ClippingText (pdf_float_t x, pdf_float_t y, char const * txt, bool outline)
  868. #line 1072 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  869. {
  870. int op= outline ? 5 : 7;
  871. _outfmt(false, "q BT %.2f %.2f Td %d Tr (", x*m_k, (m_h-y)*m_k, op);
  872. _out(_escape(txt), false);
  873. _out(") Tj ET");
  874. }
  875. #line 1079 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  876. void FPDF::Text (pdf_float_t x, pdf_float_t y, char const * txt)
  877. #line 1080 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  878. {
  879. std::string result;
  880. _TextBase(result, x*m_k, (m_h-y)*m_k, txt);
  881. _out(result);
  882. }
  883. #line 1086 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  884. void FPDF::TextShadow (pdf_float_t x, pdf_float_t y, char const * txt, pdf_float_t displacement)
  885. #line 1088 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  886. {
  887. pdf_color_t saved_color = m_TextColor_rgb;
  888. SetTextColor(200,200,200);
  889. Text(x+displacement, y+displacement, txt);
  890. SetTextColor(saved_color);
  891. Text(x, y, txt);
  892. }
  893. #line 1096 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  894. bool FPDF::AcceptPageBreak ()
  895. #line 1097 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  896. {
  897. // Accept automatic page break or not
  898. return m_AutoPageBreak;
  899. }
  900. #line 1102 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  901. void FPDF::Cell (pdf_float_t w, pdf_float_t h, char const * txt, char const * border, int ln, char align, bool fill, int link)
  902. #line 1104 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  903. {
  904. // Output a cell
  905. std::string s;
  906. pdf_float_t k = m_k;
  907. if(m_y+h > m_PageBreakTrigger && !m_InHeader && !m_InFooter && AcceptPageBreak())
  908. {
  909. // Automatic page break
  910. pdf_float_t x = m_x;
  911. pdf_float_t ws = m_ws;
  912. if(ws>0)
  913. {
  914. m_ws = 0;
  915. _out("0 Tw");
  916. }
  917. AddPage(m_CurOrientation, &m_CurPageSize);
  918. m_x = x + m_xDelta;
  919. if(ws>0)
  920. {
  921. m_ws = ws;
  922. _outfmt(true, "%.3f Tw", ws*k);
  923. }
  924. }
  925. if(w==0)
  926. w = m_w-m_rMargin-m_x;
  927. if(fill || (border && border[0]=='1'))
  928. {
  929. pdf_sprintf(s, "%.2f %.2f %.2f %.2f re %s ", m_x*k,(m_h-m_y)*k,w*k,-h*k,
  930. fill ? ((border && border[0]=='1') ? "B" : "f") : "S");
  931. }
  932. if(border)
  933. {
  934. pdf_float_t x = m_x;
  935. pdf_float_t y = m_y;
  936. if(strchr(border,'L'))
  937. pdf_sprintf_append(s, "%.2f %.2f m %.2f %.2f l S ",x*k,(m_h-y)*k,x*k,(m_h-(y+h))*k);
  938. if(strchr(border,'T'))
  939. pdf_sprintf_append(s, "%.2f %.2f m %.2f %.2f l S ",x*k,(m_h-y)*k,(x+w)*k,(m_h-y)*k);
  940. if(strchr(border,'R'))
  941. pdf_sprintf_append(s, "%.2f %.2f m %.2f %.2f l S ",(x+w)*k,(m_h-y)*k,(x+w)*k,(m_h-(y+h))*k);
  942. if(strchr(border,'B'))
  943. pdf_sprintf_append(s, "%.2f %.2f m %.2f %.2f l S ",x*k,(m_h-(y+h))*k,(x+w)*k,(m_h-(y+h))*k);
  944. }
  945. if(txt)
  946. {
  947. pdf_float_t dx;
  948. pdf_float_t txt_width = GetStringWidth(txt);
  949. if(align=='R')
  950. dx = w-m_cMargin-txt_width;
  951. else if(align=='C')
  952. dx = (w-txt_width)/2;
  953. else
  954. dx = m_cMargin;
  955. _TextBase(s, (m_x+dx)*k, (m_h-(m_y+.5*h+.3*m_FontSize))*k, txt);
  956. if(link)
  957. Link(m_x+dx, m_y+.5*h-.5*m_FontSize, txt_width, m_FontSize, link);
  958. }
  959. if(!s.empty()) _out(s);
  960. m_lasth = h;
  961. if(ln>0)
  962. {
  963. // Go to next line
  964. m_y += h;
  965. if(ln==1) m_x = m_lMargin;
  966. }
  967. else m_x += w;
  968. }
  969. #line 1180 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  970. void FPDF::MultiCell (pdf_float_t w, pdf_float_t h, char const * txt, char const * border, char align, bool fill)
  971. #line 1182 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  972. {
  973. // Output text with automatic or explicit line breaks
  974. font_width_t *cw = m_CurrentFont->font.cw;
  975. if(w==0) w = m_w-m_rMargin-m_x;
  976. pdf_float_t wmax = (w-2*m_cMargin)*1000.0/m_FontSize;
  977. std::string stmp, s = txt ? txt : "";
  978. _erasestrch(s, '\r');
  979. int nb = s.size();
  980. if(nb>0 && s[nb-1]=='\n') --nb;
  981. std::string b, b2;
  982. if(border)
  983. {
  984. if(border[0]=='1')
  985. {
  986. border = "LTRB";
  987. b = "LRT";
  988. b2 = "LR";
  989. }
  990. else
  991. {
  992. if(strchr(border,'L')) b2 += "L";
  993. if(strchr(border,'R')) b2 += "R";
  994. b = strchr(border,'T') ? b2+"T" : b2;
  995. }
  996. }
  997. int sep = -1;
  998. int i = 0;
  999. int j = 0;
  1000. int l = 0;
  1001. int ns = 0;
  1002. int nl = 1;
  1003. int ls = 0;
  1004. while(i<nb)
  1005. {
  1006. // Get next character
  1007. unsigned char c = (unsigned char)s[i];
  1008. if(c=='\n')
  1009. {
  1010. // Explicit line break
  1011. if(m_ws>0)
  1012. {
  1013. m_ws = 0;
  1014. _out("0 Tw");
  1015. }
  1016. stmp = s.substr(j, i-j);
  1017. Cell(w,h,stmp,b.c_str(),2,align,fill);
  1018. ++i;
  1019. sep = -1;
  1020. j = i;
  1021. l = 0;
  1022. ns = 0;
  1023. ++nl;
  1024. if(border && nl==2) b = b2;
  1025. continue;
  1026. }
  1027. if(c==' ')
  1028. {
  1029. sep = i;
  1030. ls = l;
  1031. ++ns;
  1032. }
  1033. l += cw[c];
  1034. if(l>wmax)
  1035. {
  1036. // Automatic line break
  1037. if(sep==-1)
  1038. {
  1039. if(i==j) ++i;
  1040. if(m_ws>0)
  1041. {
  1042. m_ws = 0;
  1043. _out("0 Tw");
  1044. }
  1045. stmp = s.substr(j, i-j);
  1046. Cell(w,h,stmp,b.c_str(),2,align,fill);
  1047. }
  1048. else
  1049. {
  1050. if(align=='J')
  1051. {
  1052. m_ws = (ns>1) ? (wmax-ls)/1000.0*m_FontSize/(ns-1) : 0;
  1053. //printf("%d %f %f %d %f\n", ns, m_ws, wmax, ls, m_FontSize);
  1054. _outfmt(true, "%.3f Tw",m_ws*m_k);
  1055. }
  1056. stmp = s.substr(j, sep-j);
  1057. Cell(w,h,stmp,b.c_str(),2,align,fill);
  1058. i = sep+1;
  1059. }
  1060. sep = -1;
  1061. j = i;
  1062. l = 0;
  1063. ns = 0;
  1064. ++nl;
  1065. if(border && nl==2)
  1066. b = b2;
  1067. }
  1068. else
  1069. ++i;
  1070. }
  1071. // Last chunk
  1072. if(m_ws>0)
  1073. {
  1074. m_ws = 0;
  1075. _out("0 Tw");
  1076. }
  1077. if(border && strchr(border,'B'))
  1078. b += "B";
  1079. stmp = s.substr(j, i-j);
  1080. Cell(w,h,stmp,b.c_str(),2,align,fill);
  1081. m_x = m_lMargin;
  1082. }
  1083. #line 1301 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1084. int FPDF::CalcLines (pdf_float_t w, char const * txt)
  1085. #line 1302 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1086. {
  1087. //Computes the number of lines a MultiCell of width w will take
  1088. font_width_t *cw = m_CurrentFont->font.cw;
  1089. if(w==0) w = m_w-m_rMargin-m_x;
  1090. pdf_float_t wmax = (w-2*m_cMargin)*1000.0/m_FontSize;
  1091. std::string stmp, s = txt ? txt : "";
  1092. _erasestrch(s, '\r');
  1093. int nb = s.size();
  1094. if(nb>0 && s[nb-1]=='\n') --nb;
  1095. int sep = -1;
  1096. int i = 0;
  1097. int j = 0;
  1098. int l = 0;
  1099. int nl = 1;
  1100. while(i<nb)
  1101. {
  1102. // Get next character
  1103. unsigned char c = (unsigned char)s[i];
  1104. if(c == '\n')
  1105. {
  1106. ++i;
  1107. sep = -1;
  1108. j = i;
  1109. l = 0;
  1110. ++nl;
  1111. continue;
  1112. }
  1113. if(c == ' ') sep=i;
  1114. l += cw[c];
  1115. if(l > wmax)
  1116. {
  1117. if(sep == -1)
  1118. {
  1119. if(i == j) ++i;
  1120. }
  1121. else i = sep+1;
  1122. sep = -1;
  1123. j = i;
  1124. l = 0;
  1125. ++nl;
  1126. }
  1127. else
  1128. ++i;
  1129. }
  1130. return nl;
  1131. }
  1132. #line 1351 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1133. void FPDF::MultiCellBlt (pdf_float_t w, pdf_float_t h, char const * blt, char const * txt, char const * border, char align, bool fill)
  1134. #line 1354 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1135. {
  1136. //Get bullet width including margins
  1137. pdf_float_t blt_width = GetStringWidth(blt)+m_cMargin*2;
  1138. //Save x
  1139. pdf_float_t bak_x = m_x;
  1140. //Output bullet
  1141. Cell(blt_width, h, blt,0,' ', fill);
  1142. //Output text
  1143. MultiCell(w-blt_width, h, txt, border, align, fill);
  1144. //Restore x
  1145. m_x = bak_x;
  1146. }
  1147. #line 1371 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1148. void FPDF::ClippingRect (pdf_float_t x, pdf_float_t y, pdf_float_t w, pdf_float_t h, bool outline)
  1149. #line 1372 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1150. {
  1151. char op=outline ? 'S' : 'n';
  1152. _outfmt(true, "q %.2f %.2f %.2f %.2f re W %c",
  1153. x*m_k, (m_h-y)*m_k, w*m_k,-h*m_k, op);
  1154. }
  1155. #line 1378 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1156. void FPDF::UnsetClipping ()
  1157. #line 1379 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1158. {
  1159. _out("Q");
  1160. }
  1161. #line 1383 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1162. void FPDF::ClippedCell (pdf_float_t w, pdf_float_t h, char const * txt, char const * border, int ln, char align, bool fill, int link)
  1163. #line 1385 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1164. {
  1165. if(border || fill || m_y+h>m_PageBreakTrigger)
  1166. {
  1167. Cell(w,h,0,border,0,' ',fill);
  1168. m_x-=w;
  1169. }
  1170. ClippingRect(m_x,m_y,w,h);
  1171. Cell(w,h,txt,0,ln,align,false,link);
  1172. UnsetClipping();
  1173. }
  1174. #line 1397 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1175. void FPDF::CellFit (pdf_float_t w, pdf_float_t h, char const * txt, char const * border, int ln, char align, bool fill, int link, bool scale, bool force)
  1176. #line 1400 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1177. {
  1178. //Get string width
  1179. pdf_float_t str_width=GetStringWidth(txt);
  1180. //Calculate ratio to fit cell
  1181. if(w==0.0f) w = m_w-m_rMargin-m_x;
  1182. pdf_float_t ratio = (w-m_cMargin*2)/str_width;
  1183. bool fit = (ratio < 1 || (ratio > 1 && force));
  1184. if (fit)
  1185. {
  1186. if (scale)
  1187. {
  1188. //Calculate horizontal scaling
  1189. pdf_float_t horiz_scale=ratio*100.0;
  1190. //Set horizontal scaling
  1191. _outfmt(true, "BT %.2f Tz ET", horiz_scale);
  1192. }
  1193. else
  1194. {
  1195. //Calculate character spacing in points
  1196. // TODO (mingo2#1#): UTF-8 strlen
  1197. int txt_size = txt ? strlen(txt) : 0;
  1198. pdf_float_t char_space=(w-m_cMargin*2-str_width)/std::max(txt_size-1,1)*m_k;
  1199. //Set character spacing
  1200. _outfmt(true, "BT %.2f Tc ET", char_space);
  1201. }
  1202. //Override user alignment (since text will fill up cell)
  1203. align=' ';
  1204. }
  1205. //Pass on to Cell method
  1206. Cell(w,h,txt,border,ln,align,fill,link);
  1207. //Reset character spacing/horizontal scaling
  1208. if (fit)
  1209. {
  1210. _out("BT ", false);
  1211. _out(scale ? "100 Tz" : "0 Tc", false);
  1212. _out(" ET");
  1213. }
  1214. }
  1215. #line 1516 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1216. void FPDF::Write (pdf_float_t h, char const * txt, int link)
  1217. #line 1517 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1218. {
  1219. // Output text in flowing mode
  1220. font_width_t *cw = m_CurrentFont->font.cw;
  1221. pdf_float_t w = m_w-m_rMargin-m_x;
  1222. pdf_float_t wmax = (w-2*m_cMargin)*1000.0/m_FontSize;
  1223. std::string stmp, s = txt ? txt : "";
  1224. _erasestrch(s, '\r');
  1225. int nb = s.size();
  1226. int sep = -1;
  1227. int i = 0;
  1228. int j = 0;
  1229. int l = 0;
  1230. int nl = 1;
  1231. while(i<nb)
  1232. {
  1233. // Get next character
  1234. unsigned char c = (unsigned char)s[i];
  1235. if(c=='\n')
  1236. {
  1237. // Explicit line break
  1238. stmp = s.substr(j, i-j);
  1239. Cell(w,h,stmp,0,2,' ',0,link);
  1240. i++;
  1241. sep = -1;
  1242. j = i;
  1243. l = 0;
  1244. if(nl==1)
  1245. {
  1246. m_x = m_lMargin;
  1247. w = m_w-m_rMargin-m_x;
  1248. wmax = (w-2*m_cMargin)*1000.0/m_FontSize;
  1249. }
  1250. nl++;
  1251. continue;
  1252. }
  1253. if(c==' ') sep = i;
  1254. l += cw[c];
  1255. if(l>wmax)
  1256. {
  1257. // Automatic line break
  1258. if(sep==-1)
  1259. {
  1260. if(m_x > m_lMargin)
  1261. {
  1262. // Move to next line
  1263. m_x = m_lMargin;
  1264. m_y += h;
  1265. w = m_w-m_rMargin-m_x;
  1266. wmax = (w-2*m_cMargin)*1000.0/m_FontSize;
  1267. i++;
  1268. nl++;
  1269. continue;
  1270. }
  1271. if(i==j) i++;
  1272. stmp = s.substr(j, i-j);
  1273. Cell(w,h,stmp,0,2,' ',0,link);
  1274. }
  1275. else
  1276. {
  1277. stmp = s.substr(j, sep-j);
  1278. Cell(w,h,stmp,0,2,' ',0,link);
  1279. i = sep+1;
  1280. }
  1281. sep = -1;
  1282. j = i;
  1283. l = 0;
  1284. if(nl==1)
  1285. {
  1286. m_x = m_lMargin;
  1287. w = m_w-m_rMargin-m_x;
  1288. wmax = (w-2*m_cMargin)*1000.0/m_FontSize;
  1289. }
  1290. nl++;
  1291. }
  1292. else
  1293. i++;
  1294. }
  1295. // Last chunk
  1296. if(i!=j)
  1297. {
  1298. stmp = s.substr(j, j);
  1299. Cell(l/1000.0*m_FontSize,h,stmp,0,0,' ',0,link);
  1300. }
  1301. }
  1302. #line 1602 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1303. void FPDF::Ln (pdf_float_t h)
  1304. #line 1603 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1305. {
  1306. // Line feed; default value is last cell height
  1307. m_x = m_lMargin;
  1308. if(h==0.0) m_y += m_lasth;
  1309. else m_y += h;
  1310. }
  1311. #line 1612 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1312. void FPDF::Image (char const * image_name, unsigned char const * image_blob, size_t blob_size, pdf_float_t x, pdf_float_t y, pdf_float_t w, pdf_float_t h, char const * atype, int link)
  1313. #line 1617 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1314. {
  1315. st_image info;
  1316. // Put an image on the page
  1317. if(!image_name) Error("Image name is null !");
  1318. if(m_images.find(image_name) == m_images.end())
  1319. {
  1320. // First use of this image, get info
  1321. std::string type;
  1322. if(atype) type = atype;
  1323. if(type.empty())
  1324. {
  1325. if(image_blob)
  1326. Error("Image type was not specified: %s", image_name);
  1327. //else assume image_name is a filename
  1328. const char *pos = strrchr(image_name,'.');
  1329. if(!pos)
  1330. Error("Image file has no extension and no type was specified: %s", image_name);
  1331. type = pos+1;
  1332. }
  1333. _str_tolower(type);
  1334. if(type=="jpeg") type = "jpg";
  1335. if(type == "jpg") {
  1336. if(image_blob) _parsejpg_blob(info, image_name, image_blob, blob_size);
  1337. else _parsejpg(info, image_name);
  1338. }
  1339. else if(type == "png") {
  1340. if(image_blob) _parsepng_blob(info, image_name, image_blob, blob_size);
  1341. else _parsepng(info, image_name);
  1342. }
  1343. else Error("Unsupported image type: %s", type.c_str());
  1344. info.i = m_images.size()+1;
  1345. m_images[image_name] = info;
  1346. }
  1347. else
  1348. info = m_images[image_name];
  1349. // Automatic width and height calculation if needed
  1350. if(w==0.0 && h==0.0)
  1351. {
  1352. // Put image at 96 dpi
  1353. w = -96;
  1354. h = -96;
  1355. }
  1356. else
  1357. {
  1358. if(w==0) w = h* info.w/info.h;
  1359. else if(w<0) w = - info.w*72.0/w/m_k;
  1360. if(h==0) h = w* info.h/info.w;
  1361. else if(h<0) h = - info.h*72.0/h/m_k;
  1362. }
  1363. pdf_float_t _y, _x;
  1364. // Flowing mode
  1365. if(y==-1)
  1366. {
  1367. if(m_y+h>m_PageBreakTrigger && !m_InHeader && !m_InFooter && AcceptPageBreak())
  1368. {
  1369. // Automatic page break
  1370. pdf_float_t x2 = m_x;
  1371. AddPage(m_CurOrientation, &m_CurPageSize);
  1372. m_x = x2;
  1373. }
  1374. _y = m_y;
  1375. m_y += h;
  1376. }
  1377. else _y = y;
  1378. if(x==-1) _x = m_x;
  1379. else _x = x;
  1380. _outfmt(true, "q %.2f 0 0 %.2f %.2f %.2f cm /I%d Do Q",w*m_k,h*m_k,_x*m_k,(m_h-(_y+h))*m_k,info.i);
  1381. if(link) Link(_x,_y,w,h,link);
  1382. }
  1383. #line 1689 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1384. void FPDF::Image (char const * file, pdf_float_t x, pdf_float_t y, pdf_float_t w, pdf_float_t h, char const * atype, int link)
  1385. #line 1692 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1386. {
  1387. Image(file, 0, 0, x, y, w, h, atype, link);
  1388. }
  1389. #line 1702 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1390. void FPDF::SetX (pdf_float_t x)
  1391. #line 1703 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1392. {
  1393. // Set x position
  1394. if(x>=0) m_x = x;
  1395. else m_x = m_w+x;
  1396. }
  1397. #line 1715 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1398. void FPDF::SetY (pdf_float_t y)
  1399. #line 1716 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1400. {
  1401. // Set y position and reset x
  1402. m_x = m_lMargin;
  1403. if(y>=0) m_y = y;
  1404. else m_y = m_h+y;
  1405. }
  1406. #line 1742 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1407. std::string FPDF::Output (char const * name, char dest)
  1408. #line 1743 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1409. {
  1410. // Output PDF to some destination
  1411. if(m_state<3) Close();
  1412. if(dest==' ')
  1413. {
  1414. if(!name)
  1415. {
  1416. name = "doc.pdf";
  1417. dest = 'I';
  1418. }
  1419. else
  1420. dest = 'F';
  1421. }
  1422. switch(dest)
  1423. {
  1424. #if 0
  1425. case 'I':
  1426. // Send to standard output
  1427. _checkoutput();
  1428. if(PHP_SAPI!="cli")
  1429. {
  1430. // We send to a browser
  1431. header("Content-Type: application/pdf");
  1432. header("Content-Disposition: inline; filename="".name.""");
  1433. header("Cache-Control: private, max-age=0, must-revalidate");
  1434. header("Pragma: public");
  1435. }
  1436. echo m_buffer;
  1437. break;
  1438. case 'D':
  1439. // Download file
  1440. _checkoutput();
  1441. header("Content-Type: application/x-download");
  1442. header("Content-Disposition: attachment; filename="".name.""");
  1443. header("Cache-Control: private, max-age=0, must-revalidate");
  1444. header("Pragma: public");
  1445. echo m_buffer;
  1446. break;
  1447. #endif
  1448. case 'F':
  1449. {
  1450. // Save to local file
  1451. FILE *f = fopen(name,"wb");
  1452. if(!f)
  1453. Error("Unable to create output file: %s", name);
  1454. fwrite(m_buffer.c_str(), m_buffer.size(), 1, f);
  1455. fclose(f);
  1456. }
  1457. break;
  1458. case 'S':
  1459. // Return as a string
  1460. return m_buffer;
  1461. default:
  1462. Error("Incorrect output destination: %c", dest);
  1463. }
  1464. return "";
  1465. }
  1466. #line 1801 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1467. void FPDF::RoundedRect (pdf_float_t x, pdf_float_t y, pdf_float_t w, pdf_float_t h, pdf_float_t r, char const * style)
  1468. #line 1803 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1469. {
  1470. pdf_float_t k = m_k;
  1471. pdf_float_t hp = m_h;
  1472. pdf_float_t MyArc = 4.0/3.0 * (sqrt(2) - 1);
  1473. _outfmt(true, "%.2f %.2f m",(x+r)*k,(hp-y)*k);
  1474. pdf_float_t xc = x+w-r;
  1475. pdf_float_t yc = y+r;
  1476. _outfmt(true, "%.2f %.2f l", xc*k,(hp-y)*k);
  1477. _Arc(xc + r*MyArc, yc - r, xc + r, yc - r*MyArc, xc + r, yc);
  1478. xc = x+w-r;
  1479. yc = y+h-r;
  1480. _outfmt(true, "%.2f %.2f l",(x+w)*m_k,(hp-yc)*m_k);
  1481. _Arc(xc + r, yc + r*MyArc, xc + r*MyArc, yc + r, xc, yc + r);
  1482. xc = x+r;
  1483. yc = y+h-r;
  1484. _outfmt(true, "%.2f %.2f l",xc*m_k,(hp-(y+h))*m_k);
  1485. _Arc(xc - r*MyArc, yc + r, xc - r, yc + r*MyArc, xc - r, yc);
  1486. xc = x+r;
  1487. yc = y+r;
  1488. _outfmt(true, "%.2F %.2F l",(x)*m_k,(hp-yc)*m_k);
  1489. _Arc(xc - r, yc - r*MyArc, xc - r*MyArc, yc - r, xc, yc - r);
  1490. std::string str_style = style ? style : "";
  1491. if(str_style=="F") _out("f");
  1492. else if(str_style=="FD" || str_style=="DF") _out("B");
  1493. else _out("S");
  1494. }
  1495. #line 1831 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1496. void FPDF::Circle (pdf_float_t x, pdf_float_t y, pdf_float_t r, char const * style)
  1497. #line 1832 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1498. {
  1499. Ellipse(x,y,r,r,style);
  1500. }
  1501. #line 1836 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1502. void FPDF::Ellipse (pdf_float_t x, pdf_float_t y, pdf_float_t rx, pdf_float_t ry, char const * style)
  1503. #line 1838 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1504. {
  1505. const char *op = "S";
  1506. std::string str_style = style ? style : "";
  1507. if(str_style=="F") op ="f";
  1508. else if(str_style=="FD" || str_style=="DF") op ="B";
  1509. pdf_float_t tmp = 4.0/3.0*(sqrt(2)-1);
  1510. pdf_float_t lx=tmp*rx;
  1511. pdf_float_t ly=tmp*ry;
  1512. _outfmt(true, "%.2f %.2f m %.2f %.2f %.2f %.2f %.2f %.2f c",
  1513. (x+rx)*m_k,(m_h-y)*m_k,
  1514. (x+rx)*m_k,(m_h-(y-ly))*m_k,
  1515. (x+lx)*m_k,(m_h-(y-ry))*m_k,
  1516. x*m_k,(m_h-(y-ry))*m_k);
  1517. _outfmt(true, "%.2f %.2f %.2f %.2f %.2f %.2f c",
  1518. (x-lx)*m_k,(m_h-(y-ry))*m_k,
  1519. (x-rx)*m_k,(m_h-(y-ly))*m_k,
  1520. (x-rx)*m_k,(m_h-y)*m_k);
  1521. _outfmt(true, "%.2f %.2f %.2f %.2f %.2f %.2f c",
  1522. (x-rx)*m_k,(m_h-(y+ly))*m_k,
  1523. (x-lx)*m_k,(m_h-(y+ry))*m_k,
  1524. x*m_k,(m_h-(y+ry))*m_k);
  1525. _outfmt(true, "%.2f %.2f %.2f %.2f %.2f %.2f c %s",
  1526. (x+lx)*m_k,(m_h-(y+ry))*m_k,
  1527. (x+rx)*m_k,(m_h-(y+ly))*m_k,
  1528. (x+rx)*m_k,(m_h-y)*m_k,
  1529. op);
  1530. }
  1531. #line 1866 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1532. void FPDF::IncludeJS (char const * script)
  1533. #line 1867 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1534. {
  1535. m_javascript=script;
  1536. }
  1537. #line 1878 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1538. void FPDF::_Arc (pdf_float_t x1, pdf_float_t y1, pdf_float_t x2, pdf_float_t y2, pdf_float_t x3, pdf_float_t y3)
  1539. #line 1879 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1540. {
  1541. pdf_float_t h = m_h;
  1542. _outfmt(true, "%.2f %.2f %.2f %.2f %.2f %.2f c ", x1*m_k,
  1543. (h-y1)*m_k, x2*m_k, (h-y2)*m_k, x3*m_k, (h-y3)*m_k);
  1544. }
  1545. #line 1885 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1546. std::string & FPDF::_erasestrch (std::string & str, char c)
  1547. #line 1886 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1548. {
  1549. if(!str.empty())
  1550. {
  1551. size_t size = str.size();
  1552. //int used intentionally for the case of 0nt char should be erased
  1553. for(size_t i=0; i<size; ++i)
  1554. {
  1555. if(str.at(i) == c)
  1556. {
  1557. str.erase(i, 1);
  1558. --size;
  1559. --i;
  1560. }
  1561. }
  1562. }
  1563. return str;
  1564. }
  1565. #line 1904 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1566. void FPDF::_str_tolower (std::string & str)
  1567. #line 1905 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1568. {
  1569. for(size_t i=0, size=str.size(); i < size; ++i)
  1570. {
  1571. str[i] = tolower(str[i]);
  1572. }
  1573. }
  1574. #line 1912 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1575. void FPDF::_dochecks ()
  1576. #line 1913 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1577. {}
  1578. #line 1916 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1579. void FPDF::_checkoutput ()
  1580. #line 1917 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1581. {
  1582. #if 0
  1583. if(PHP_SAPI!="cli")
  1584. {
  1585. if(headers_sent(file,line))
  1586. Error("Some data has already been output, can't send PDF file (output started at file:line)");
  1587. }
  1588. if(ob_get_length())
  1589. {
  1590. // The output buffer is not empty
  1591. if(preg_match("/^(\xEF\xBB\xBF)?\s*/",ob_get_contents()))
  1592. {
  1593. // It contains only a UTF-8 BOM and/or whitespace, let"s clean it
  1594. ob_clean();
  1595. }
  1596. else
  1597. Error("Some data has already been output, can't send PDF file");
  1598. }
  1599. #endif
  1600. }
  1601. #line 1939 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1602. FPDF::st_pagesize & FPDF::_getpagesize (st_pagesize & result, e_page_sizes size)
  1603. #line 1940 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1604. {
  1605. switch(size)
  1606. {
  1607. case e_A3:
  1608. result.w = 841.89;
  1609. result.h = 1190.55;
  1610. break;
  1611. case e_A5:
  1612. result.w = 420.94;
  1613. result.h = 595.28;
  1614. break;
  1615. case e_Letter:
  1616. result.w = 612;
  1617. result.h = 792;
  1618. break;
  1619. case e_Legal:
  1620. result.w = 612;
  1621. result.h = 1008;
  1622. break;
  1623. default:
  1624. /*case e_A4:*/
  1625. result.w = 595.28;
  1626. result.h = 841.89;
  1627. break;
  1628. }
  1629. result.w /= m_k;
  1630. result.h /=m_k;
  1631. return result;
  1632. }
  1633. #line 1971 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1634. void FPDF::_beginpage (e_orientation orientation, st_pagesize * size)
  1635. #line 1972 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1636. {
  1637. m_page++;
  1638. m_pages.resize(m_page);
  1639. m_state = 2;
  1640. m_x = m_lMargin;
  1641. m_y = m_tMargin;
  1642. m_FontFamily = "";
  1643. // Check page size and orientation
  1644. if(orientation==e_orientation_none) orientation = m_DefOrientation;
  1645. st_pagesize *psize;
  1646. if(!size) psize = &m_DefPageSize;
  1647. else psize = size;
  1648. if(orientation!=m_CurOrientation || psize->w != m_CurPageSize.w || psize->h != m_CurPageSize.h)
  1649. {
  1650. // New size or orientation
  1651. if(orientation==e_orientation_portrait)
  1652. {
  1653. m_w = psize->w;
  1654. m_h = psize->h;
  1655. }
  1656. else
  1657. {
  1658. m_w = psize->h;
  1659. m_h = psize->w;
  1660. }
  1661. m_wPt = m_w*m_k;
  1662. m_hPt = m_h*m_k;
  1663. m_PageBreakTrigger = m_h-m_bMargin;
  1664. m_CurOrientation = orientation;
  1665. m_CurPageSize = *psize;
  1666. }
  1667. if(orientation != m_DefOrientation || psize->w != m_DefPageSize.w || psize->h != m_DefPageSize.h)
  1668. {
  1669. st_pagesize ps;
  1670. ps.w = m_wPt;
  1671. ps.h = m_hPt;
  1672. m_PageSizes[m_page] = ps;
  1673. }
  1674. if ( m_doubleSided )
  1675. {
  1676. if( m_page % 2 == 0 )
  1677. {
  1678. m_xDelta=m_outerMargin - m_innerMargin;
  1679. SetLeftMargin(m_outerMargin);
  1680. SetRightMargin(m_innerMargin);
  1681. }
  1682. else
  1683. {
  1684. m_xDelta=m_innerMargin - m_outerMargin;
  1685. SetLeftMargin(m_innerMargin);
  1686. SetRightMargin(m_outerMargin);
  1687. }
  1688. m_x=m_lMargin;
  1689. m_y=m_tMargin;
  1690. }
  1691. }
  1692. #line 2051 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1693. std::string FPDF::_escape (std::string const & s)
  1694. #line 2052 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1695. {
  1696. std::string str = s;
  1697. if(!str.empty())
  1698. {
  1699. size_t size = str.size();
  1700. for(size_t i=0; i<size; ++i)
  1701. {
  1702. switch((unsigned char)str.at(i))
  1703. {
  1704. case '\\':
  1705. case '(':
  1706. case ')':
  1707. str.insert(i, "\\");
  1708. ++size;
  1709. ++i;
  1710. continue;
  1711. case '\r':
  1712. str.erase(i, 1);
  1713. --size;
  1714. continue;
  1715. //now a cheap ut82latin
  1716. case 0xE2: //euro 0xE282AC = \200
  1717. if(((unsigned char)str[i+1]) == 0x82 && ((unsigned char)str[i+2]) == 0xAC)
  1718. {
  1719. str[i] = '\\';
  1720. str[i+1] = '2';
  1721. str[i+2] = '0';
  1722. str.insert(i+3, "0");
  1723. size += 1;
  1724. i += 3;
  1725. }
  1726. continue;
  1727. case 0xC2:
  1728. str.erase(i, 1);
  1729. --size;
  1730. continue;
  1731. case 0xC3:
  1732. str.erase(i, 1);
  1733. str[i] += 64;
  1734. --size;
  1735. --i;
  1736. continue;
  1737. }
  1738. }
  1739. }
  1740. return str;
  1741. }
  1742. #line 2100 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1743. void FPDF::_textstring (std::string & result, std::string const & s)
  1744. #line 2101 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1745. {
  1746. // Format a text string
  1747. result = "(" + _escape(s) + ")";
  1748. }
  1749. #line 2141 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1750. int FPDF::substr_count (char const * str, char c)
  1751. #line 2142 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1752. {
  1753. int result = 0;
  1754. if(str)
  1755. {
  1756. while(*str)
  1757. {
  1758. if(*str++ == c) result++;
  1759. }
  1760. }
  1761. return result;
  1762. }
  1763. #line 2154 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1764. std::string FPDF::_dounderline (pdf_float_t x, pdf_float_t y, char const * txt)
  1765. #line 2155 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1766. {
  1767. // Underline text
  1768. int up = m_CurrentFont->font.up;
  1769. int ut = m_CurrentFont->font.ut;
  1770. int w = GetStringWidth(txt)+m_ws*substr_count(txt,' ');
  1771. pdf_snprintf(m_scratch_buf, sizeof(m_scratch_buf), "%.2f %.2f %.2f %.2f re f",x*m_k,(m_h-(y-up/1000.0*m_FontSize))*m_k,w*m_k,-ut/1000.0*m_FontSizePt);
  1772. return m_scratch_buf;
  1773. }
  1774. #line 2169 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1775. FPDF::blob_stream_t::~ blob_stream_t ()
  1776. #line 2169 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1777. {}
  1778. #line 2176 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1779. FPDF::blob_stream_memory_t::blob_stream_memory_t (unsigned char const * ablob, size_t asize)
  1780. #line 2176 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1781. {
  1782. blob = ablob;
  1783. size = asize;
  1784. offset = 0;
  1785. }
  1786. #line 2181 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1787. bool FPDF::blob_stream_memory_t::eof ()
  1788. #line 2181 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1789. {
  1790. return offset == size;
  1791. }
  1792. #line 2184 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1793. size_t FPDF::blob_stream_memory_t::tell ()
  1794. #line 2184 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1795. {
  1796. return offset;
  1797. }
  1798. #line 2188 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1799. size_t FPDF::blob_stream_memory_t::read (void * dest, size_t num_read)
  1800. #line 2189 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1801. {
  1802. if (offset + num_read > size)
  1803. num_read = size - offset;
  1804. memcpy ((char *)dest, blob + offset, num_read);
  1805. offset += num_read;
  1806. return num_read;
  1807. }
  1808. #line 2200 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1809. void FPDF::blob_stream_memory_t::seek (size_t to_offset, int whence)
  1810. #line 2201 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1811. {
  1812. size_t npos;
  1813. switch (whence) {
  1814. case SEEK_SET:
  1815. npos = to_offset;
  1816. break;
  1817. case SEEK_CUR:
  1818. npos = offset + to_offset;
  1819. break;
  1820. case SEEK_END:
  1821. npos = size;
  1822. break;
  1823. }
  1824. offset = npos > size ? size : npos;
  1825. }
  1826. #line 2224 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1827. FPDF::blob_stream_file_t::blob_stream_file_t (FILE * afp)
  1828. #line 2224 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1829. {
  1830. fp = afp;
  1831. }
  1832. #line 2227 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1833. FPDF::blob_stream_file_t::~ blob_stream_file_t ()
  1834. #line 2227 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1835. {
  1836. if(fp) fclose(fp);
  1837. }
  1838. #line 2230 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1839. bool FPDF::blob_stream_file_t::eof ()
  1840. #line 2230 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1841. {
  1842. return feof(fp);
  1843. }
  1844. #line 2233 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1845. size_t FPDF::blob_stream_file_t::tell ()
  1846. #line 2233 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1847. {
  1848. return ftell(fp);
  1849. }
  1850. #line 2237 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1851. size_t FPDF::blob_stream_file_t::read (void * dest, size_t num_read)
  1852. #line 2238 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1853. {
  1854. return fread(dest, 1, num_read, fp);
  1855. }
  1856. #line 2242 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1857. void FPDF::blob_stream_file_t::seek (size_t to_offset, int whence)
  1858. #line 2243 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1859. {
  1860. fseek(fp, to_offset, whence);
  1861. }
  1862. #line 2273 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1863. void FPDF::_parsejpg (st_image & info, blob_stream_t & fp, char const * image_name)
  1864. #line 2274 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  1865. {
  1866. bool isValid = false;
  1867. info.cs.clear();
  1868. info.pal.clear();
  1869. info.trns.clear();
  1870. info.data.clear();
  1871. std::string buf;
  1872. _readstream(buf,fp,3);
  1873. if (buf != "\xff\xd8\xff")
  1874. Error("Not a JPG image: %s", image_name);
  1875. // Extract info from a JPEG file
  1876. unsigned int marker = M_PSEUDO;
  1877. unsigned short length, ffRead = 1;
  1878. unsigned char bits = 0;
  1879. unsigned short height = 0;
  1880. unsigned short width = 0;
  1881. unsigned char channels = 0;
  1882. bool ready = false;
  1883. int lastMarker;
  1884. int commentCorrection;
  1885. int a;
  1886. while (!ready)
  1887. {
  1888. lastMarker = marker;
  1889. commentCorrection = 1;
  1890. a = 0;
  1891. // get marker byte, swallowing possible padding
  1892. if (lastMarker == M_COM && commentCorrection)
  1893. {
  1894. // some software does not count the length bytes of COM section
  1895. // one company doing so is very much envolved in JPEG... so we accept too
  1896. // by the way: some of those companies changed their code now...
  1897. commentCorrection = 2;
  1898. }
  1899. else
  1900. {
  1901. lastMarker = 0;
  1902. commentCorrection = 0;
  1903. }
  1904. if (ffRead)
  1905. {
  1906. a = 1; // already read 0xff in filetype detection
  1907. }
  1908. do
  1909. {
  1910. _readstream(buf,fp,1);
  1911. if (fp.eof())
  1912. {
  1913. marker = M_EOI; // we hit EOF
  1914. break;
  1915. }
  1916. marker = (unsigned char)buf[0];
  1917. if (lastMarker == M_COM && commentCorrection > 0)
  1918. {
  1919. if (marker != 0xFF)
  1920. {
  1921. marker = 0xff;
  1922. commentCorrection--;
  1923. }
  1924. else
  1925. {
  1926. lastMarker = M_PSEUDO; // stop skipping non 0xff for M_COM
  1927. }
  1928. }
  1929. if (++a > 10)
  1930. {
  1931. // who knows the maxim amount of 0xff? though 7
  1932. // but found other implementations
  1933. marker = M_EOI;
  1934. break;
  1935. }
  1936. }
  1937. while (marker == 0xff);
  1938. if (a < 2)
  1939. {
  1940. marker = M_EOI; // at least one 0xff is needed before marker code
  1941. }
  1942. if (lastMarker == M_COM && commentCorrection)
  1943. {
  1944. marker = M_EOI; // ah illegal: char after COM section not 0xFF
  1945. }
  1946. ffRead = 0;
  1947. switch (marker)
  1948. {
  1949. case M_SOF0:
  1950. case M_SOF1:
  1951. case M_SOF2:
  1952. case M_SOF3:
  1953. case M_SOF5:
  1954. case M_SOF6:
  1955. case M_SOF7:
  1956. case M_SOF9:
  1957. case M_SOF10:
  1958. case M_SOF11:
  1959. case M_SOF13:
  1960. case M_SOF14:
  1961. case M_SOF15:
  1962. // handle SOFn block
  1963. length = (unsigned short)_readshort(fp);
  1964. _readstream(buf, fp,1);
  1965. bits = (unsigned char)buf[0];
  1966. height = (unsigned short)_readshort(fp);
  1967. width = (unsigned short)_readshort(fp);
  1968. _readstream(buf, fp,1);
  1969. channels = (unsigned char)buf[0];
  1970. isValid = true;
  1971. ready = true;
  1972. break;
  1973. case M_SOS:
  1974. case M_EOI:
  1975. isValid = false;
  1976. ready = true;
  1977. default:
  1978. {
  1979. // anything else isn't interesting
  1980. long pos = (unsigned short) _readshort(fp);
  1981. pos = pos-2;
  1982. if (pos)
  1983. {
  1984. fp.seek(pos, SEEK_CUR );
  1985. }
  1986. }
  1987. break;
  1988. }
  1989. }
  1990. if (isValid)
  1991. {
  1992. if (channels == 3)
  1993. {
  1994. info.cs = "DeviceRGB";
  1995. }
  1996. else if(channels == 4)
  1997. {
  1998. info.cs = "DeviceCMYK";
  1999. }
  2000. else
  2001. {
  2002. info.cs = "DeviceGray";
  2003. }
  2004. info.bpc = bits;
  2005. //Read whole file
  2006. fp.seek(0, SEEK_END);
  2007. int fsize = fp.tell();
  2008. fp.seek(0, SEEK_SET);
  2009. _readstream(info.data, fp, fsize);
  2010. info.w = width;
  2011. info.h = height;
  2012. info.f = "DCTDecode";
  2013. return;
  2014. }
  2015. Error("Invalid JPG image: %s", image_name);
  2016. }
  2017. #line 2438 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2018. void FPDF::_parsejpg (st_image & info, char const * file_name)
  2019. #line 2439 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2020. {
  2021. // Extract info from a JPG file
  2022. FILE *fp = fopen(file_name,"rb");
  2023. if(!fp) Error("Can't open image file: %s", file_name);
  2024. blob_stream_file_t sf(fp);
  2025. _parsejpg(info, sf, file_name);
  2026. }
  2027. #line 2448 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2028. void FPDF::_parsejpg_blob (st_image & info, char const * image_name, unsigned char const * image_blob, size_t blob_size)
  2029. #line 2450 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2030. {
  2031. if(!image_name) Error("Image name is NULL!");
  2032. if(!image_blob) Error("Image blob is NULL!");
  2033. if(!blob_size) Error("Image blob size is zero!");
  2034. blob_stream_memory_t sm(image_blob, blob_size);
  2035. _parsejpg(info, sm, image_name);
  2036. }
  2037. #line 2458 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2038. void FPDF::_parsepng (st_image & info, char const * file_name)
  2039. #line 2459 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2040. {
  2041. // Extract info from a PNG file
  2042. FILE *fp = fopen(file_name,"rb");
  2043. if(!fp) Error("Can't open image file: %s", file_name);
  2044. blob_stream_file_t sf(fp);
  2045. _parsepngstream(info, sf, file_name);
  2046. }
  2047. #line 2469 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2048. void FPDF::_parsepng_blob (st_image & info, char const * image_name, unsigned char const * image_blob, size_t blob_size)
  2049. #line 2471 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2050. {
  2051. if(!image_name) Error("Image name is NULL!");
  2052. if(!image_blob) Error("Image blob is NULL!");
  2053. if(!blob_size) Error("Image blob size is zero!");
  2054. blob_stream_memory_t sm(image_blob, blob_size);
  2055. _parsepngstream(info, sm, image_name);
  2056. }
  2057. #line 2479 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2058. void FPDF::_getGrayImgColorAndalpha (std::string & color, std::string & alpha, std::string & line)
  2059. #line 2479 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2060. {
  2061. size_t size = line.size();
  2062. color.reserve(size);
  2063. alpha.reserve(size);
  2064. for(size_t i=0; i<size; i+=2){
  2065. color += line[i];
  2066. alpha += line[i+1];
  2067. }
  2068. }
  2069. #line 2489 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2070. void FPDF::_getRGBImgColorAndalpha (std::string & color, std::string & alpha, std::string & line)
  2071. #line 2489 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2072. {
  2073. size_t size = line.size();
  2074. color.reserve(size);
  2075. alpha.reserve(size/3);
  2076. for(size_t i=0; i<size; i+=4){
  2077. color.append(line.c_str()+i, 3);
  2078. alpha += line[i+3];
  2079. }
  2080. }
  2081. #line 2499 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2082. void FPDF::_parsepngstream (st_image & info, blob_stream_t & fp, char const * image_name)
  2083. #line 2500 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2084. {
  2085. std::string buf;
  2086. // Check signature
  2087. _readstream(buf, fp, 8);
  2088. if(buf != "\x89PNG\x0D\x0A\x1A\x0A")
  2089. Error("Not a PNG image: %s", image_name);
  2090. // Read header chunk
  2091. _readstream(buf, fp, 4);
  2092. _readstream(buf, fp, 4);
  2093. if(buf != "IHDR") Error("Incorrect PNG image: %s", image_name);
  2094. info.w = _readint(fp);
  2095. info.h = _readint(fp);
  2096. _readstream(buf, fp, 1);
  2097. info.bpc = buf[0];
  2098. if(info.bpc>8) Error("16-bit depth not supported: %s", image_name);
  2099. _readstream(buf, fp, 1);
  2100. int ct = buf[0];
  2101. if(ct==0 || ct==4) info.cs = "DeviceGray";
  2102. else if(ct==2 || ct==6) info.cs = "DeviceRGB";
  2103. else if(ct==3) info.cs = "Indexed";
  2104. else Error("Unknown color type: %s", image_name);
  2105. _readstream(buf, fp, 3);
  2106. if(buf[0]!=0)
  2107. Error("Unknown compression method: %s", image_name);
  2108. if(buf[1]!=0)
  2109. Error("Unknown filter method: %s", image_name);
  2110. if(buf[2]!=0)
  2111. Error("Interlacing not supported: %s", image_name);
  2112. _readstream(buf,fp,4);
  2113. pdf_sprintf(info.dp, "/Predictor 15 /Colors %d /BitsPerComponent %d /Columns %d",
  2114. (info.cs=="DeviceRGB" ? 3 : 1), (int)info.bpc, (int)info.w);
  2115. // Scan chunks looking for palette, transparency and image data
  2116. info.pal.clear();
  2117. info.trns.clear();
  2118. info.data.clear();
  2119. int n;
  2120. do
  2121. {
  2122. n = _readint(fp);
  2123. _readstream(buf, fp, 4);
  2124. if(buf=="PLTE")
  2125. {
  2126. // Read palette
  2127. _readstream(info.pal, fp,n);
  2128. _readstream(buf, fp, 4);
  2129. }
  2130. else if(buf=="tRNS")
  2131. {
  2132. // Read transparency info
  2133. _readstream(buf,fp,n);
  2134. if(ct==0)
  2135. {
  2136. info.trns.push_back(buf[1]);
  2137. }
  2138. else if(ct==2)
  2139. {
  2140. info.trns.push_back(buf[1]);
  2141. info.trns.push_back(buf[3]);
  2142. info.trns.push_back(buf[5]);
  2143. }
  2144. else
  2145. {
  2146. size_t pos = buf.find('\0');
  2147. if(pos!=std::string::npos)
  2148. info.trns.push_back(buf[pos]);
  2149. }
  2150. _readstream(buf,fp,4);
  2151. }
  2152. else if(buf=="IDAT")
  2153. {
  2154. // Read image data block
  2155. info.data += _readstream(buf,fp,n);
  2156. _readstream(buf, fp, 4);
  2157. }
  2158. else if(buf=="IEND")
  2159. break;
  2160. else
  2161. _readstream(buf,fp,n+4);
  2162. }
  2163. while(n);
  2164. if(info.cs=="Indexed" && info.pal.empty()) Error("Missing palette in %s", image_name);
  2165. info.f = "FlateDecode";
  2166. #ifdef PDF_USING_ZLIB
  2167. if(ct>=4)
  2168. {
  2169. // Extract alpha channel
  2170. std::string data;
  2171. gzuncompress(data, info.data);
  2172. std::string color, alpha, line;
  2173. if(ct==4)
  2174. {
  2175. // Gray image
  2176. int len = 2*info.w;
  2177. for(int i=0; i<info.h; i++)
  2178. {
  2179. int pos = (1+len)*i;
  2180. color.append(data[pos], 1);
  2181. alpha.append(data[pos], 1);
  2182. line.assign(data.c_str()+pos+1,len);
  2183. _getGrayImgColorAndalpha(color, alpha, line);
  2184. //color += preg_replace("/(.)./s","1",line);
  2185. //alpha += preg_replace("/.(.)/s","1",line);
  2186. }
  2187. }
  2188. else
  2189. {
  2190. // RGB image
  2191. int len = 4*info.w;
  2192. for(int i=0; i<info.h; i++)
  2193. {
  2194. int pos = (1+len)*i;
  2195. color.append(data[pos], 1);
  2196. alpha.append(data[pos], 1);
  2197. line.assign(data.c_str()+pos+1,len);
  2198. _getRGBImgColorAndalpha(color, alpha, line);
  2199. //color += preg_replace("/(.{3})./s","1",line);
  2200. //alpha += preg_replace("/.{3}(.)/s","1",line);
  2201. }
  2202. }
  2203. gzcompress(info.smask, alpha);
  2204. data.clear();
  2205. gzcompress(info.data, color);
  2206. if(m_PDFVersion < "1.4") m_PDFVersion = "1.4";
  2207. }
  2208. #endif
  2209. }
  2210. #line 2631 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2211. std::string & FPDF::_readstream (std::string & result, blob_stream_t & fp, size_t n)
  2212. #line 2632 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2213. {
  2214. // Read n bytes from stream
  2215. result.resize(n);
  2216. if(fp.read(&result[0], n) != n) Error("Can't read from stream !");
  2217. return result;
  2218. }
  2219. #line 2639 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2220. int FPDF::_readint (blob_stream_t & fp)
  2221. #line 2640 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2222. {
  2223. // Read a 4-byte integer from stream
  2224. std::string buf;
  2225. _readstream(buf, fp, 4);
  2226. int i = 1;
  2227. char *p = (char *)&i;
  2228. if (p[0] == 1) //LITTLE_ENDIAN
  2229. {
  2230. unsigned char c0 = buf[0], c1 = buf[1], c2 = buf[2], c3 = buf[3];
  2231. buf[0] = c3;
  2232. buf[1] = c2;
  2233. buf[2] = c1;
  2234. buf[3] = c0;
  2235. }
  2236. //else BIG_ENDIAN
  2237. return *((int*)&buf[0]);
  2238. }
  2239. #line 2658 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2240. int FPDF::_readshort (blob_stream_t & fp)
  2241. #line 2659 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2242. {
  2243. // Read a 4-byte integer from stream
  2244. std::string buf;
  2245. _readstream(buf, fp, 2);
  2246. short i = 1;
  2247. char *p = (char *)&i;
  2248. if (p[0] == 1) //LITTLE_ENDIAN
  2249. {
  2250. unsigned char c0 = buf[0], c1 = buf[1];
  2251. buf[0] = c1;
  2252. buf[1] = c0;
  2253. }
  2254. //else BIG_ENDIAN
  2255. return *((short*)&buf[0]);
  2256. }
  2257. #line 2675 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2258. void FPDF::_parsegif (std::string & file)
  2259. #line 2676 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2260. {
  2261. #if 0
  2262. // Extract info from a GIF file (via PNG conversion)
  2263. if(!function_exists("imagepng"))
  2264. Error("GD extension is required for GIF support");
  2265. if(!function_exists("imagecreatefromgif"))
  2266. Error("GD has no GIF read support");
  2267. im = imagecreatefromgif(file);
  2268. if(!im)
  2269. Error("Missing or incorrect image file: ".file);
  2270. imageinterlace(im,0);
  2271. f = @fopen("php://temp","rb+");
  2272. if(f)
  2273. {
  2274. // Perform conversion in memory
  2275. ob_start();
  2276. imagepng(im);
  2277. data = ob_get_clean();
  2278. imagedestroy(im);
  2279. fwrite(f,data);
  2280. fseek(fp, 0, SEEK_SET);
  2281. info = _parsepngstream(f,file);
  2282. fclose(f);
  2283. }
  2284. else
  2285. {
  2286. // Use temporary file
  2287. tmp = tempnam(".","gif");
  2288. if(!tmp)
  2289. Error("Unable to create a temporary file");
  2290. if(!imagepng(im,tmp))
  2291. Error("Error while saving to temporary file");
  2292. imagedestroy(im);
  2293. info = _parsepng(tmp);
  2294. unlink(tmp);
  2295. }
  2296. return info;
  2297. #endif
  2298. }
  2299. #line 2716 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2300. void FPDF::_newobj ()
  2301. #line 2717 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2302. {
  2303. // Begin a new object
  2304. m_n++;
  2305. m_offsets.push_back(m_buffer.size());
  2306. _outfmt(true, "%d 0 obj", m_n);
  2307. }
  2308. #line 2724 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2309. void FPDF::_putstream (std::string const & s)
  2310. #line 2725 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2311. {
  2312. _out("stream");
  2313. _out(s);
  2314. _out("endstream");
  2315. }
  2316. #line 2731 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2317. void FPDF::_out (char const * s, size_t size, bool nl)
  2318. #line 2732 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2319. {
  2320. // Add a line to the document
  2321. if(m_state==2)
  2322. {
  2323. std::string &str = m_pages[m_page-1];
  2324. str.append(s, size);
  2325. if(nl) str += "\n";
  2326. }
  2327. else
  2328. {
  2329. m_buffer.append(s, size);
  2330. if(nl) m_buffer += "\n";
  2331. }
  2332. }
  2333. #line 2747 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2334. std::string & FPDF::pdf_sprintf (std::string & s, char const * fmt, ...)
  2335. #line 2748 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2336. {
  2337. va_list args;
  2338. va_start( args, fmt );
  2339. int size = pdf_vsnprintf(m_scratch_buf, sizeof(m_scratch_buf), fmt, args);
  2340. va_end( args );
  2341. if(size < 0) Error("Too big string passed to sprintf %d", __LINE__);
  2342. s = m_scratch_buf;
  2343. return s;
  2344. }
  2345. #line 2758 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2346. void FPDF::pdf_sprintf_append (std::string & s, char const * fmt, ...)
  2347. #line 2759 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2348. {
  2349. va_list args;
  2350. va_start( args, fmt );
  2351. int size = pdf_vsnprintf(m_scratch_buf, sizeof(m_scratch_buf), fmt, args);
  2352. va_end( args );
  2353. if(size < 0) Error("Too big string passed to sprintf %d", __LINE__);
  2354. s += m_scratch_buf;
  2355. }
  2356. #line 2768 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2357. void FPDF::_outfmt (bool nl, char const * fmt, ...)
  2358. #line 2769 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2359. {
  2360. va_list args;
  2361. va_start( args, fmt );
  2362. int size = pdf_vsnprintf(m_scratch_buf, sizeof(m_scratch_buf), fmt, args);
  2363. va_end( args );
  2364. if(size < 0) Error("Too big string passed to sprintf %d", __LINE__);
  2365. _out(m_scratch_buf, size, nl);
  2366. }
  2367. #line 2778 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2368. void FPDF::_out (char const * s, bool nl)
  2369. #line 2779 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2370. {
  2371. _out(s, strlen(s), nl);
  2372. }
  2373. #line 2783 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2374. void FPDF::_out (std::string const & s, bool nl)
  2375. #line 2784 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2376. {
  2377. _out(s.c_str(), s.size(), nl);
  2378. }
  2379. #line 2788 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2380. void FPDF::_putpages ()
  2381. #line 2789 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2382. {
  2383. int nb = m_page;
  2384. if(!m_AliasNbPages.empty())
  2385. {
  2386. // Replace number of pages
  2387. std::string str;
  2388. pdf_sprintf(str, "%d", nb);
  2389. for(int n=0; n<nb; n++)
  2390. {
  2391. std::string &page = m_pages[n];
  2392. for(std::string::size_type loc = page.find(m_AliasNbPages);
  2393. loc != std::string::npos; loc = page.find(m_AliasNbPages, loc))
  2394. {
  2395. page.replace(loc, m_AliasNbPages.size(), str, 0, str.size());
  2396. }
  2397. }
  2398. }
  2399. pdf_float_t wPt, hPt;
  2400. if(m_DefOrientation==e_orientation_portrait)
  2401. {
  2402. wPt = m_DefPageSize.w*m_k;
  2403. hPt = m_DefPageSize.h*m_k;
  2404. }
  2405. else
  2406. {
  2407. wPt = m_DefPageSize.h*m_k;
  2408. hPt = m_DefPageSize.w*m_k;
  2409. }
  2410. std::string filter = (m_compress) ? "/Filter /FlateDecode " : "";
  2411. for(int n=1; n<=nb; n++)
  2412. {
  2413. // Page
  2414. _newobj();
  2415. _outfmt(true, "<<\n/Type /Page");
  2416. _outfmt(true, "/Parent 1 0 R");
  2417. if(m_PageSizes.find(n) != m_PageSizes.end())
  2418. _outfmt(true, "/MediaBox [0 0 %.2f %.2f]",m_PageSizes[n].w,m_PageSizes[n].h);
  2419. _outfmt(true, "/Resources 2 0 R");
  2420. if(m_PageLinks.find(n) != m_PageLinks.end())
  2421. {
  2422. // Links
  2423. std::string annots = "/Annots [";
  2424. #if 0
  2425. link_map_t::iterator iter = m_PageLinks[n].begin();
  2426. link_map_t::iterator iend = m_PageLinks[n].end();
  2427. for(; iter != iend; ++iter)
  2428. {
  2429. st_page_link &pl = *iter->second;
  2430. pdf_snprintf(m_scratch_buf, sizeof(m_scratch_buf), "%.2f %.2f %.2f %.2f",pl[0],pl[1],pl[0]+pl[2],pl[1]-pl[3]);
  2431. rect = m_scratch_buf;
  2432. annots += "<<\n/Type /Annot /Subtype /Link /Rect [" + rect + "] /Border [0 0 0] ";
  2433. if(is_string(pl[4]))
  2434. annots += "/A\n<<\n/S /URI /URI " + _3textstring(pl[4]) + "\n>>\n>>\n";
  2435. else
  2436. {
  2437. l = m_links[pl[4]];
  2438. int lidx = l[0];
  2439. h = m_PageSizes.find([lidx]) != -1 ? m_PageSizes[lidx].h : hPt;
  2440. pdf_sprintf_append(annots, "/Dest [%d 0 R /XYZ 0 %.2f null]\n>>\n",1+2*lidx,h-l[1]*m_k);
  2441. }
  2442. }
  2443. #endif
  2444. _out(annots + "]");
  2445. }
  2446. if(m_PDFVersion > "1.3")
  2447. _outfmt(true, "/Group\n<<\n/Type /Group /S /Transparency /CS /DeviceRGB\n>>");
  2448. _outfmt(true, "/Contents %d 0 R\n>>", m_n+1);
  2449. _out("endobj");
  2450. // Page content
  2451. std::string zbuf, &p =
  2452. #ifdef PDF_USING_ZLIB
  2453. (m_compress) ? gzcompress(zbuf, m_pages[n-1]) :
  2454. #endif
  2455. m_pages[n-1];
  2456. _newobj();
  2457. _out("<<");
  2458. _out(filter, false);
  2459. _outfmt(true, "/Length %d\n>>", p.size());
  2460. _putstream(p);
  2461. _out("endobj");
  2462. }
  2463. // Pages root
  2464. m_offsets[1] = m_buffer.size();
  2465. _out("1 0 obj");
  2466. _out("<<\n/Type /Pages");
  2467. std::string kids = "/Kids [";
  2468. for(int i=0; i<nb; i++) pdf_sprintf_append(kids, "%d 0 R ",(3+2*i));
  2469. _out(kids + "]");
  2470. _outfmt(true, "/Count %d", nb);
  2471. _outfmt(true, "/MediaBox [0 0 %.2f %.2f]", wPt, hPt);
  2472. _out(">>");
  2473. _out("endobj");
  2474. }
  2475. #line 2883 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2476. void FPDF::_putfonts ()
  2477. #line 2884 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2478. {
  2479. #if 0
  2480. int nf = m_n;
  2481. foreach(m_diffs as diff)
  2482. {
  2483. // Encodings
  2484. _newobj();
  2485. _outfmt(true, "<<\n/Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences [".diff."]\n>>");
  2486. _outfmt(true, "endobj");
  2487. }
  2488. foreach(m_FontFiles as file=>info)
  2489. {
  2490. // Font file embedding
  2491. _newobj();
  2492. m_FontFiles[file].n = m_n;
  2493. font = file_get_contents(m_fontpath + file,true);
  2494. if(!font) Error("Font file not found: ".file);
  2495. compressed = (substr(file,-2)==".z");
  2496. if(!compressed && isset(info.length2))
  2497. font = substr(font,6,info.length1).substr(font,6+info.length1+6,info.length2);
  2498. _outfmt(true, "<<\n/Length %d", strlen(font));
  2499. if(compressed) _outfmt(true, "/Filter /FlateDecode");
  2500. _outfmt(true, "/Length1 %d", info.length1);
  2501. if(isset(info["length2"]))
  2502. _outfmt(true, "/Length2 %d /Length3 0", info.length2);
  2503. _outfmt(true, ">>");
  2504. _putstream(font);
  2505. _outfmt(true, "endobj");
  2506. }
  2507. #endif
  2508. font_map_t::iterator iter = m_fonts.begin();
  2509. font_map_t::iterator iend = m_fonts.end();
  2510. for(; iter != iend; ++iter)
  2511. {
  2512. // Font objects
  2513. st_pdf_font_base &font = *iter->second;
  2514. font.n = m_n+1;
  2515. e_font_type type = font.font.type;
  2516. std::string name = font.name;
  2517. if(type==e_font_type_core)
  2518. {
  2519. // Core font
  2520. _newobj();
  2521. _out("<<\n/Type /Font");
  2522. _out("/BaseFont /" + name);
  2523. _out("/Subtype /Type1");
  2524. if(name!="Symbol" && name!="ZapfDingbats")
  2525. _out("/Encoding /WinAnsiEncoding");
  2526. _out(">>");
  2527. _out("endobj");
  2528. }
  2529. #if 0
  2530. else if(type==e_font_type_type1 || type==e_font_type_ttf)
  2531. {
  2532. // Additional Type1 or TrueType/OpenType font
  2533. _newobj();
  2534. _out("<<\n/Type /Font");
  2535. _out("/BaseFont /" + name);
  2536. _out("/Subtype /" + type);
  2537. _out("/FirstChar 32 /LastChar 255");
  2538. _outfmt(true, "/Widths %d 0 R", (m_n+1));
  2539. _outfmt(true, "/FontDescriptor %d 0 R", (m_n+2));
  2540. if(isset(font["diffn"]))
  2541. _outfmt(true, "/Encoding %d 0 R", nf+font.diffn);
  2542. else
  2543. _out("/Encoding /WinAnsiEncoding");
  2544. _out(">>");
  2545. _out("endobj");
  2546. // Widths
  2547. _newobj();
  2548. cw = font.cw;
  2549. std::string s = "[";
  2550. s.reserve(1024);
  2551. for(i=32; i<=255; i++)
  2552. pdf_sprintf_append(s, "%d ", cw[chr(i)]);
  2553. _out(s + "]");
  2554. _out("endobj");
  2555. // Descriptor
  2556. _newobj();
  2557. s = "<<\n/Type /FontDescriptor /FontName /" + name;
  2558. foreach(font["desc"] as k=>v)
  2559. s += " /".k." ".v;
  2560. if(!empty(font["file"]))
  2561. s += " /FontFile".(type=="Type1" ? "" : "2")." ".m_FontFiles[font["file"]]["n"]." 0 R";
  2562. _outfmt(true, s.">>");
  2563. _outfmt(true, "endobj");
  2564. }
  2565. else
  2566. {
  2567. // Allow for additional types
  2568. mtd = "_put".strtolower(type);
  2569. if(!method_exists(this,mtd))
  2570. Error("Unsupported font type: ".type);
  2571. mtd(font);
  2572. }
  2573. #endif
  2574. }
  2575. }
  2576. #line 2983 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2577. void FPDF::_putimages ()
  2578. #line 2984 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2579. {
  2580. for(image_map_t::iterator iter = m_images.begin(); iter != m_images.end(); ++iter)
  2581. {
  2582. st_image &image = iter->second;
  2583. _putimage(image);
  2584. image.data.clear();
  2585. image.smask.clear();
  2586. }
  2587. }
  2588. #line 2994 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2589. void FPDF::_putimage (st_image & info)
  2590. #line 2995 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2591. {
  2592. _newobj();
  2593. info.n = m_n;
  2594. _out("<<\n/Type /XObject");
  2595. _out("/Subtype /Image");
  2596. _outfmt(true, "/Width %d", (int)info.w);
  2597. _outfmt(true, "/Height %d", (int)info.h);
  2598. if(info.cs=="Indexed")
  2599. _outfmt(true, "/ColorSpace [/Indexed /DeviceRGB %d %d 0 R]", (info.pal.size())/3-1, (m_n+1));
  2600. else
  2601. {
  2602. _out("/ColorSpace /" + info.cs);
  2603. if(info.cs=="DeviceCMYK")
  2604. _out("/Decode [1 0 1 0 1 0 1 0]");
  2605. }
  2606. _outfmt(true, "/BitsPerComponent %d", info.bpc);
  2607. if(!info.f.empty())
  2608. _out("/Filter /" + info.f);
  2609. if(!info.dp.empty())
  2610. _out("/DecodeParms\n<<\n" + info.dp + "\n>>");
  2611. if(!info.trns.empty())
  2612. {
  2613. std::string trns = "/Mask [";
  2614. for(int i=0, size = info.trns.size(); i<size; i++)
  2615. {
  2616. pdf_sprintf_append(trns, "%d", info.trns[i]);
  2617. trns += " ";
  2618. pdf_sprintf_append(trns, "%d", info.trns[i]);
  2619. trns += " ";
  2620. }
  2621. _out(trns + "]");
  2622. }
  2623. if(!info.smask.empty())
  2624. _outfmt(true, "/SMask %d 0 R", (m_n+1));
  2625. _outfmt(true, "/Length %d\n>>", info.data.size());
  2626. _putstream(info.data);
  2627. _out("endobj");
  2628. // Soft mask
  2629. if(!info.smask.empty())
  2630. {
  2631. std::string dp;
  2632. pdf_sprintf(dp, "/Predictor 15 /Colors 1 /BitsPerComponent 8 /Columns %d", (int)info.w);
  2633. st_image smask = info;
  2634. smask.cs = "DeviceGray";
  2635. smask.bpc = 8;
  2636. smask.dp = dp;
  2637. smask.data = info.smask;
  2638. _putimage(smask);
  2639. }
  2640. // Palette
  2641. if(info.cs=="Indexed")
  2642. {
  2643. std::string filter = (m_compress) ? "/Filter /FlateDecode " : "";
  2644. std::string zbuf, &pal =
  2645. #ifdef PDF_USING_ZLIB
  2646. (m_compress) ? gzcompress(zbuf, info.pal) :
  2647. #endif
  2648. info.pal;
  2649. _newobj();
  2650. _outfmt(true, "<<\n%s/Length %d\n>>", filter.c_str(), pal.size());
  2651. _putstream(pal);
  2652. _out("endobj");
  2653. }
  2654. }
  2655. #line 3060 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2656. void FPDF::_putxobjectdict ()
  2657. #line 3061 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2658. {
  2659. for(image_map_t::iterator iter = m_images.begin(); iter != m_images.end(); ++iter)
  2660. _outfmt(true, "/I%d %d 0 R", iter->second.i, iter->second.n);
  2661. }
  2662. #line 3066 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2663. void FPDF::_putextgstates ()
  2664. #line 3067 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2665. {
  2666. for (size_t i = 1, size = m_extgstates.size(); i <= size; ++i)
  2667. {
  2668. _newobj();
  2669. st_alpha_t &gs = m_extgstates[i-1];
  2670. gs.n = m_n;
  2671. _out("<<\n/Type /ExtGState");
  2672. _outfmt(true, "/ca %0.3f", gs.alpha);
  2673. _outfmt(true, "/CA %0.3f", gs.alpha);
  2674. _outfmt(true, "/BM /%s", gs.bm.c_str());
  2675. _out(">>\nendobj");
  2676. }
  2677. }
  2678. #line 3081 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2679. void FPDF::_putjavascript ()
  2680. #line 3082 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2681. {
  2682. _newobj();
  2683. m_n_js=m_n;
  2684. _outfmt(true, "<<\n/Names [(EmbeddedJS) %d 0 R]\n>>\nendobj", m_n+1);
  2685. _newobj();
  2686. _out("<<\n/S /JavaScript\n/JS (", false);
  2687. _out(_escape(m_javascript), false);
  2688. _out(")\n>>\nendobj");
  2689. }
  2690. #line 3092 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2691. void FPDF::_putresourcedict ()
  2692. #line 3093 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2693. {
  2694. _out("/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]");
  2695. _out("/Font\n<<");
  2696. for(font_map_t::iterator iter = m_fonts.begin(); iter != m_fonts.end(); ++iter)
  2697. _outfmt(true, "/F%d %d 0 R", iter->second->i, iter->second->n);
  2698. _outfmt(true, ">>");
  2699. _outfmt(true, "/XObject\n<<");
  2700. _putxobjectdict();
  2701. _outfmt(true, ">>");
  2702. if(m_extgstates.size())
  2703. {
  2704. _out("/ExtGState\n<<");
  2705. for(size_t i = 1, size = m_extgstates.size(); i <= size; ++i)
  2706. {
  2707. _outfmt(true, "/GS%d %d 0 R", i, m_extgstates[i-1].n);
  2708. }
  2709. _out(">>");
  2710. }
  2711. }
  2712. #line 3113 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2713. void FPDF::_putresources ()
  2714. #line 3114 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2715. {
  2716. _putextgstates();
  2717. _putfonts();
  2718. _putimages();
  2719. // Resource dictionary
  2720. m_offsets[2] = m_buffer.size();
  2721. _outfmt(true, "2 0 obj");
  2722. _outfmt(true, "<<");
  2723. _putresourcedict();
  2724. _out(">>\nendobj");
  2725. if (!m_javascript.empty())
  2726. {
  2727. _putjavascript();
  2728. }
  2729. }
  2730. #line 3130 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2731. void FPDF::_putinfo ()
  2732. #line 3131 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2733. {
  2734. std::string str = _escape(FPDF_VERSION);
  2735. _outfmt(true, "/Producer (CppPDF %s)", str.c_str());
  2736. if(!m_title.empty())
  2737. {
  2738. str = _escape(m_title);
  2739. _out("/Title (" + str + ")");
  2740. }
  2741. if(!m_subject.empty())
  2742. {
  2743. str = _escape(m_subject);
  2744. _out("/Subject (" + str + ")");
  2745. }
  2746. if(!m_author.empty())
  2747. {
  2748. str = _escape(m_author);
  2749. _out("/Author (" + str + ")");
  2750. }
  2751. if(!m_keywords.empty())
  2752. {
  2753. str = _escape(m_keywords);
  2754. _out("/Keywords (" + str + ")");
  2755. }
  2756. if(!m_creator.empty())
  2757. {
  2758. str = _escape(m_creator);
  2759. _out("/Creator (" + str + ")");
  2760. }
  2761. time_t rawtime;
  2762. struct tm * timeinfo;
  2763. time ( &rawtime );
  2764. timeinfo = localtime ( &rawtime );
  2765. strftime (m_scratch_buf,sizeof(m_scratch_buf),"/CreationDate (D:%Y%m%d%H%M%S)", timeinfo);
  2766. _out(m_scratch_buf);
  2767. }
  2768. #line 3167 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2769. void FPDF::_putcatalog ()
  2770. #line 3168 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2771. {
  2772. _outfmt(true, "/Type /Catalog");
  2773. _outfmt(true, "/Pages 1 0 R");
  2774. if(m_CustomZoom)
  2775. _outfmt(true, "/OpenAction [3 0 R /XYZ null null %.2f]", m_CustomZoom/100.0);
  2776. else if(m_ZoomMode==e_zoom_fullpage)
  2777. _outfmt(true, "/OpenAction [3 0 R /Fit]");
  2778. else if(m_ZoomMode==e_zoom_fullwidth)
  2779. _outfmt(true, "/OpenAction [3 0 R /FitH null]");
  2780. else if(m_ZoomMode==e_zoom_real)
  2781. _outfmt(true, "/OpenAction [3 0 R /XYZ null null 1]");
  2782. if(m_LayoutMode==e_layout_single)
  2783. _outfmt(true, "/PageLayout /SinglePage");
  2784. else if(m_LayoutMode==e_layout_continuous)
  2785. _outfmt(true, "/PageLayout /OneColumn");
  2786. else if(m_LayoutMode==e_layout_two)
  2787. _outfmt(true, "/PageLayout /TwoColumnLeft");
  2788. if (!m_javascript.empty())
  2789. {
  2790. _outfmt(true, "/Names\n<<\n/JavaScript %d 0 R\n>>", m_n_js);
  2791. }
  2792. }
  2793. #line 3191 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2794. void FPDF::_putheader ()
  2795. #line 3192 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2796. {
  2797. _out("%PDF-" + m_PDFVersion);
  2798. }
  2799. #line 3196 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2800. void FPDF::_puttrailer ()
  2801. #line 3197 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2802. {
  2803. _outfmt(true, "/Size %d", (m_n+1));
  2804. _outfmt(true, "/Root %d 0 R", m_n);
  2805. _outfmt(true, "/Info %d 0 R", (m_n-1));
  2806. }
  2807. #line 3203 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2808. void FPDF::_enddoc ()
  2809. #line 3204 "/home/mingo/dev/dadbiz++/ourbiz-uk/parts/fpdf.zz"
  2810. {
  2811. if(m_extgstates.size() && (m_PDFVersion < "1.4")) m_PDFVersion = "1.4";
  2812. _putheader();
  2813. _putpages();
  2814. _putresources();
  2815. // Info
  2816. _newobj();
  2817. _out("<<");
  2818. _putinfo();
  2819. _out(">>\nendobj");
  2820. // Catalog
  2821. _newobj();
  2822. _out("<<");
  2823. _putcatalog();
  2824. _out(">>\nendobj");
  2825. //save begining of xref
  2826. int saved_startxref = m_buffer.size();
  2827. // Cross-ref
  2828. _out("xref");
  2829. _outfmt(true, "0 %d", (m_n+1));
  2830. _out("0000000000 65535 f ");
  2831. for(int i=1; i<=m_n; i++)
  2832. {
  2833. _outfmt(true, "%010d 00000 n ",m_offsets[i]);
  2834. }
  2835. // Trailer
  2836. _out("trailer\n<<");
  2837. _puttrailer();
  2838. _out(">>\nstartxref");
  2839. _outfmt(true, "%d", saved_startxref);
  2840. _out("%%EOF", false);
  2841. m_state = 3;
  2842. }
  2843. #undef LZZ_INLINE