1717 * 2014 Juan Pablo Gaviria, https://github.com/juanpgaviria
1818 * 2014 James Makes, https://github.com/dollaruw
1919 * 2014 Diego Casorran, https://github.com/diegocr
20+ * 2014 Steven Spungin, https://github.com/Flamenco
2021 *
2122 * Permission is hereby granted, free of charge, to any person obtaining
2223 * a copy of this software and associated documentation files (the
3940 *
4041 * Contributor(s):
4142 * siefkenj, ahwolf, rickygu, Midnith, saintclair, eaparango,
42- * kim3er, mfo, alnorth,
43+ * kim3er, mfo, alnorth, Flamenco
4344 */
4445
4546/**
@@ -198,6 +199,7 @@ var jsPDF = (function(global) {
198199 } ,
199200 API = { } ,
200201 events = new PubSub ( API ) ,
202+ lastTextWasStroke = false ,
201203
202204 /////////////////////
203205 // Private functions
@@ -228,6 +230,17 @@ var jsPDF = (function(global) {
228230 out ( objectNumber + ' 0 obj' ) ;
229231 return objectNumber ;
230232 } ,
233+ // Does not output the object. The caller must call newObjectDeferredBegin(oid) before outputing any data
234+ newObjectDeferred = function ( ) {
235+ objectNumber ++ ;
236+ offsets [ objectNumber ] = function ( ) {
237+ return content_length ;
238+ } ;
239+ return objectNumber ;
240+ } ,
241+ newObjectDeferredBegin = function ( oid ) {
242+ offsets [ oid ] = content_length ;
243+ } ,
231244 putStream = function ( str ) {
232245 out ( 'stream' ) ;
233246 out ( str ) ;
@@ -251,7 +264,10 @@ var jsPDF = (function(global) {
251264 out ( '/Parent 1 0 R' ) ;
252265 out ( '/Resources 2 0 R' ) ;
253266 out ( '/MediaBox [0 0 ' + f2 ( wPt ) + ' ' + f2 ( hPt ) + ']' ) ;
254- out ( '/Contents ' + ( objectNumber + 1 ) + ' 0 R>>' ) ;
267+ out ( '/Contents ' + ( objectNumber + 1 ) + ' 0 R' ) ;
268+ // Added for annotation plugin
269+ events . publish ( 'putPage' , { pageNumber :n , page :pages [ n ] } ) ;
270+ out ( '>>' ) ;
255271 out ( 'endobj' ) ;
256272
257273 // Page content
@@ -766,7 +782,12 @@ var jsPDF = (function(global) {
766782 out ( '0 ' + ( objectNumber + 1 ) ) ;
767783 out ( p + ' 65535 f ' ) ;
768784 for ( i = 1 ; i <= objectNumber ; i ++ ) {
769- out ( ( p + offsets [ i ] ) . slice ( - 10 ) + ' 00000 n ' ) ;
785+ var offset = offsets [ i ] ;
786+ if ( typeof offset === 'function' ) {
787+ out ( ( p + offsets [ i ] ( ) ) . slice ( - 10 ) + ' 00000 n ' ) ;
788+ } else {
789+ out ( ( p + offsets [ i ] ) . slice ( - 10 ) + ' 00000 n ' ) ;
790+ }
770791 }
771792 // Trailer
772793 out ( 'trailer' ) ;
@@ -918,6 +939,8 @@ var jsPDF = (function(global) {
918939 } ,
919940 'collections' : { } ,
920941 'newObject' : newObject ,
942+ 'newObjectDeferred' : newObjectDeferred ,
943+ 'newObjectDeferredBegin' : newObjectDeferredBegin ,
921944 'putStream' : putStream ,
922945 'events' : events ,
923946 // ratio that you use in multiplication of a given "size" number to arrive to 'point'
@@ -941,7 +964,17 @@ var jsPDF = (function(global) {
941964 'getNumberOfPages' : function ( ) {
942965 return pages . length - 1 ;
943966 } ,
944- 'pages' : pages
967+ 'pages' : pages ,
968+ 'out' : out ,
969+ 'f2' : f2 ,
970+ 'getPageInfo' : function ( pageNumberOneBased ) {
971+ var objId = ( pageNumberOneBased - 1 ) * 2 + 3 ;
972+ return { objId :objId , pageNumber :pageNumberOneBased } ;
973+ } ,
974+ 'getCurrentPageInfo' : function ( ) {
975+ var objId = ( currentPage - 1 ) * 2 + 3 ;
976+ return { objId :objId , pageNumber :currentPage } ;
977+ }
945978 } ;
946979
947980 /**
@@ -960,6 +993,47 @@ var jsPDF = (function(global) {
960993 _setPage . apply ( this , arguments ) ;
961994 return this ;
962995 } ;
996+ API . insertPage = function ( beforePage ) {
997+ this . addPage ( ) ;
998+ this . movePage ( currentPage , beforePage ) ;
999+ return this ;
1000+ } ;
1001+ API . movePage = function ( targetPage , beforePage ) {
1002+ if ( targetPage > beforePage ) {
1003+ var tmpPages = pages [ targetPage ] ;
1004+ var tmpPagedim = pagedim [ targetPage ] ;
1005+ for ( var i = targetPage ; i > beforePage ; i -- ) {
1006+ pages [ i ] = pages [ i - 1 ] ;
1007+ pagedim [ i ] = pagedim [ i - 1 ] ;
1008+ }
1009+ pages [ beforePage ] = tmpPages ;
1010+ pagedim [ beforePage ] = tmpPagedim ;
1011+ this . setPage ( beforePage ) ;
1012+ } else if ( targetPage < beforePage ) {
1013+ var tmpPages = pages [ targetPage ] ;
1014+ var tmpPagedim = pagedim [ targetPage ] ;
1015+ for ( var i = targetPage ; i < beforePage ; i ++ ) {
1016+ pages [ i ] = pages [ i + 1 ] ;
1017+ pagedim [ i ] = pagedim [ i + 1 ] ;
1018+ }
1019+ pages [ beforePage ] = tmpPages ;
1020+ pagedim [ beforePage ] = tmpPagedim ;
1021+ this . setPage ( beforePage ) ;
1022+ }
1023+ return this ;
1024+ } ;
1025+ API . deletePage = function ( targetPage ) {
1026+ for ( var i = targetPage ; i < page ; i ++ ) {
1027+ pages [ i ] = pages [ i + 1 ] ;
1028+ pagedim [ i ] = pagedim [ i + 1 ] ;
1029+ }
1030+ page -- ;
1031+ if ( currentPage > page ) {
1032+ currentPage = page ;
1033+ }
1034+ this . setPage ( currentPage ) ;
1035+ return this ;
1036+ } ;
9631037 API . setDisplayMode = function ( zoom , layout , pmode ) {
9641038 zoomMode = zoom ;
9651039 layoutMode = layout ;
@@ -1047,7 +1121,23 @@ var jsPDF = (function(global) {
10471121 flags . noBOM = true ;
10481122 if ( ! ( 'autoencode' in flags ) )
10491123 flags . autoencode = true ;
1050-
1124+
1125+ //TODO this might not work after object block changes
1126+ // It would be better to pass in a page context
1127+ var strokeOption = '' ;
1128+ if ( true === flags . stroke ) {
1129+ if ( this . lastTextWasStroke !== true ) {
1130+ strokeOption = '1 Tr\n' ;
1131+ this . lastTextWasStroke = true ;
1132+ }
1133+ }
1134+ else {
1135+ if ( this . lastTextWasStroke ) {
1136+ strokeOption = '0 Tr\n' ;
1137+ }
1138+ this . lastTextWasStroke = false ;
1139+ }
1140+
10511141 if ( text instanceof Array ) {
10521142 // we don't want to destroy original text array, so cloning it
10531143 var sa = text . concat ( ) , da = [ ] , i , len = sa . length ;
@@ -1108,6 +1198,7 @@ var jsPDF = (function(global) {
11081198 'BT\n/' +
11091199 activeFontKey + ' ' + activeFontSize + ' Tf\n' + // font face, style, size
11101200 ( activeFontSize * lineHeightProportion ) + ' TL\n' + // line spacing
1201+ strokeOption + // stroke option
11111202 textColor +
11121203 '\n' + xtra + f2 ( x * k ) + ' ' + f2 ( ( pageHeight - y ) * k ) + ' ' + mode + '\n(' +
11131204 text +
0 commit comments