11( AcroForm = function ( jsPDFAPI ) {
22 'use strict' ;
33
4+ AcroForm . scale = function ( x ) {
5+ return ( x * ( acroformPlugin . internal . scaleFactor / 1 ) ) ; // 1 = (96 / 72)
6+ } ;
7+ AcroForm . antiScale = function ( x ) {
8+ return ( ( 1 / acroformPlugin . internal . scaleFactor ) * x ) ;
9+ } ;
10+
411 var acroformPlugin = {
512 fields : [ ] ,
613 xForms : [ ] ,
119126 }
120127
121128 var fieldArray = fieldArray || this . acroformPlugin . acroFormDictionaryRoot . Kids ;
129+
122130 for ( var i in fieldArray ) {
123131 var key = i ;
124132 var form = fieldArray [ i ] ;
133+
134+ var oldRect = form . Rect ;
135+
136+ if ( form . Rect ) {
137+ form . Rect = AcroForm . internal . calculateCoordinates . call ( this , form . Rect ) ;
138+ }
139+
125140 // Start Writing the Object
126141 this . internal . newObjectDeferredBegin ( form . objId ) ;
127142
130145
131146 content += ( "<<\n" + form . getContent ( ) ) ;
132147
148+ form . Rect = oldRect ;
149+
133150 if ( form . hasAppearanceStream && ! form . appearanceStreamContent ) {
134151 // Calculate Appearance
135152 var appearance = AcroForm . internal . calculateAppearanceStream . call ( this , form ) ;
197214 var content = "" ;
198215 content += ( form . getString ( ) ) ;
199216 this . internal . out ( content ) ;
217+
218+ delete fieldArray [ key ] ;
200219 }
201220 } ;
202221
@@ -379,8 +398,8 @@ AcroForm.internal = {};
379398
380399AcroForm . createFormXObject = function ( formObject ) {
381400 var xobj = new AcroForm . FormXObject ;
382- var height = formObject . Rect [ 3 ] - formObject . Rect [ 1 ] || 0 ;
383- var width = formObject . Rect [ 2 ] - formObject . Rect [ 0 ] || 0 ;
401+ var height = AcroForm . Appearance . internal . getHeight ( formObject ) || 0 ;
402+ var width = AcroForm . Appearance . internal . getWidth ( formObject ) || 0 ;
384403 xobj . BBox = [ 0 , 0 , width , height ] ;
385404 return xobj ;
386405} ;
@@ -663,6 +682,7 @@ AcroForm.Appearance.internal = {
663682 var width = AcroForm . Appearance . internal . getWidth ( formObject ) ;
664683 var height = AcroForm . Appearance . internal . getHeight ( formObject ) ;
665684 var a = min ( width , height ) ;
685+ var crossSize = a ;
666686 var borderPadding = 2 ; // The Padding in px
667687
668688
@@ -689,10 +709,10 @@ AcroForm.Appearance.internal = {
689709 } ,
690710} ;
691711AcroForm . Appearance . internal . getWidth = function ( formObject ) {
692- return formObject . Rect [ 2 ] - formObject . Rect [ 0 ] || 0 ;
712+ return formObject . Rect [ 2 ] ; //(formObject.Rect[2] - formObject.Rect[0]) || 0;
693713} ;
694714AcroForm . Appearance . internal . getHeight = function ( formObject ) {
695- return formObject . Rect [ 3 ] - formObject . Rect [ 1 ] || 0 ;
715+ return formObject . Rect [ 3 ] ; //(formObject.Rect[1] - formObject.Rect[3]) || 0;
696716} ;
697717
698718// ##########################
@@ -802,6 +822,10 @@ AcroForm.PDFObject.prototype.getContent = function () {
802822 var key = keys [ i ] ;
803823 var value = fieldObject [ key ] ;
804824
825+ /*if (key == 'Rect' && value) {
826+ value = AcroForm.internal.calculateCoordinates.call(jsPDF.API.acroformPlugin.internal, value);
827+ }*/
828+
805829 if ( value ) {
806830 if ( Array . isArray ( value ) ) {
807831 content += '/' + key + ' ' + AcroForm . internal . arrayToPdfArray ( value ) + "\n" ;
@@ -901,8 +925,8 @@ AcroForm.Field = function () {
901925 return ;
902926 }
903927 var tmp = _Rect ;
904- var calculatedRes = AcroForm . internal . calculateCoordinates ( _Rect ) ;
905- return calculatedRes
928+ // var calculatedRes = AcroForm.internal.calculateCoordinates(_Rect); // do later!
929+ return tmp
906930 } ,
907931 set : function ( val ) {
908932 _Rect = val ;
@@ -1343,9 +1367,9 @@ AcroForm.internal.calculateX = function (formObject, text, font, maxFontSize) {
13431367 var borderPadding = 2 ;
13441368
13451369
1346- var height = formObject . Rect [ 3 ] - formObject . Rect [ 1 ] || 0 ;
1370+ var height = AcroForm . Appearance . internal . getHeight ( formObject ) || 0 ;
13471371 height = ( height < 0 ) ? - height : height ;
1348- var width = formObject . Rect [ 2 ] - formObject . Rect [ 0 ] || 0 ;
1372+ var width = AcroForm . Appearance . internal . getWidth ( formObject ) || 0 ;
13491373 width = ( width < 0 ) ? - width : width ;
13501374
13511375 var isSmallerThanWidth = function ( i , lastLine , fontSize ) {
@@ -1367,7 +1391,7 @@ AcroForm.internal.calculateX = function (formObject, text, font, maxFontSize) {
13671391 var textHeight = AcroForm . internal . calculateFontSpace ( "3" , fontSize + "px" , font ) . height ;
13681392 var startY = ( formObject . multiline ) ? height - fontSize : ( height - textHeight ) / 2 ;
13691393 startY += lineSpacing ;
1370- var startX = borderPadding ;
1394+ var startX = - borderPadding ;
13711395
13721396 var lastX = startX , lastY = startY ;
13731397 var firstWordInLine = 0 , lastWordInLine = 0 ;
@@ -1526,20 +1550,45 @@ AcroForm.internal.calculateAppearanceStream = function (formObject) {
15261550 */
15271551AcroForm . internal . calculateCoordinates = function ( x , y , w , h ) {
15281552 var coordinates = { } ;
1529- if ( Array . isArray ( x ) ) { // in case, the parameters are sent in as an array
1530- /*coordinates.lowerLeft_X = x[0] | 0;
1531- coordinates.lowerLeft_Y = x[1] | 0;
1532- coordinates.upperRight_X = x[2] | 0;
1533- coordinates.upperRight_Y = x[3] | 0;*/
1534- coordinates . lowerLeft_X = x [ 0 ] | 0 ;
1535- coordinates . lowerLeft_Y = x [ 1 ] | 0 ;
1536- coordinates . upperRight_X = x [ 0 ] + x [ 2 ] | 0 ;
1537- coordinates . upperRight_Y = x [ 1 ] + x [ 3 ] | 0 ;
1553+
1554+ if ( this . internal ) {
1555+ function mmtopx ( x ) {
1556+ return ( x * this . internal . scaleFactor ) ;
1557+ }
1558+
1559+ if ( Array . isArray ( x ) ) {
1560+ x [ 0 ] = AcroForm . scale ( x [ 0 ] ) ;
1561+ x [ 1 ] = AcroForm . scale ( x [ 1 ] ) ;
1562+ x [ 2 ] = AcroForm . scale ( x [ 2 ] ) ;
1563+ x [ 3 ] = AcroForm . scale ( x [ 3 ] ) ;
1564+
1565+ coordinates . lowerLeft_X = x [ 0 ] | 0 ;
1566+ coordinates . lowerLeft_Y = ( mmtopx . call ( this , this . internal . pageSize . height ) - x [ 3 ] - x [ 1 ] ) | 0 ;
1567+ coordinates . upperRight_X = x [ 0 ] + x [ 2 ] | 0 ;
1568+ coordinates . upperRight_Y = ( mmtopx . call ( this , this . internal . pageSize . height ) - x [ 1 ] ) | 0 ;
1569+ } else {
1570+ x = AcroForm . scale ( x ) ;
1571+ y = AcroForm . scale ( y ) ;
1572+ w = AcroForm . scale ( w ) ;
1573+ h = AcroForm . scale ( h ) ;
1574+ coordinates . lowerLeft_X = x | 0 ;
1575+ coordinates . lowerLeft_Y = this . internal . pageSize . height - y | 0 ;
1576+ coordinates . upperRight_X = x + w | 0 ;
1577+ coordinates . upperRight_Y = this . internal . pageSize . height - y + h | 0 ;
1578+ }
15381579 } else {
1539- coordinates . lowerLeft_X = x | 0 ;
1540- coordinates . lowerLeft_Y = y | 0 ;
1541- coordinates . upperRight_X = x + w | 0 ;
1542- coordinates . upperRight_Y = y + h | 0 ;
1580+ // old method, that is fallback, if we can't get the pageheight, the coordinate-system starts from lower left
1581+ if ( Array . isArray ( x ) ) {
1582+ coordinates . lowerLeft_X = x [ 0 ] | 0 ;
1583+ coordinates . lowerLeft_Y = x [ 1 ] | 0 ;
1584+ coordinates . upperRight_X = x [ 0 ] + x [ 2 ] | 0 ;
1585+ coordinates . upperRight_Y = x [ 1 ] + x [ 3 ] | 0 ;
1586+ } else {
1587+ coordinates . lowerLeft_X = x | 0 ;
1588+ coordinates . lowerLeft_Y = y | 0 ;
1589+ coordinates . upperRight_X = x + w | 0 ;
1590+ coordinates . upperRight_Y = y + h | 0 ;
1591+ }
15431592 }
15441593
15451594 return [ coordinates . lowerLeft_X , coordinates . lowerLeft_Y , coordinates . upperRight_X , coordinates . upperRight_Y ] ;
0 commit comments