12
12
* The goal is to provide a way for current canvas implementations to print directly to a PDF.
13
13
*/
14
14
15
- function context ( ) {
16
- this . fillStyle = '#000000' ;
17
- this . strokeStyle = '#000000' ;
18
- this . font = "12pt Times" ;
19
- this . textBaseline = 'alphabetic' ; //top,bottom,middle,ideographic,alphabetic,hanging
20
- this . lineWidth = 1 ;
21
- this . lineJoin = 'miter' ; //round, bevel, miter
22
- this . lineCap = 'butt' ; //butt, round, square
23
- //TODO miter limit //default 10
24
-
25
- this . copy = function ( ctx ) {
26
- this . fillStyle = ctx . fillStyle ;
27
- this . strokeStyle = ctx . strokeStyle ;
28
- this . font = ctx . font ;
29
- this . lineWidth = ctx . lineWidth ;
30
- this . lineJoin = ctx . lineJoin ;
31
- this . lineCap = ctx . lineCap ;
32
- this . textBaseline = ctx . textBaseline ;
33
- } ;
34
- }
35
-
36
15
( function ( jsPDFAPI ) {
37
16
'use strict' ;
38
17
@@ -46,7 +25,7 @@ function context() {
46
25
}
47
26
] ) ;
48
27
49
- jsPDF . API . context2d = {
28
+ jsPDFAPI . context2d = {
50
29
51
30
f2 : function ( number ) {
52
31
return number . toFixed ( 2 ) ;
@@ -68,6 +47,7 @@ function context() {
68
47
} ,
69
48
70
49
save : function ( ) {
50
+ this . ctx . _fontSize = this . pdf . internal . getFontSize ( ) ;
71
51
var ctx = new context ( ) ;
72
52
ctx . copy ( this . ctx ) ;
73
53
this . ctxStack . push ( this . ctx ) ;
@@ -79,17 +59,20 @@ function context() {
79
59
this . setFillStyle ( this . ctx . fillStyle ) ;
80
60
this . setStrokeStyle ( this . ctx . strokeStyle ) ;
81
61
this . setFont ( this . ctx . font ) ;
62
+ this . pdf . setFontSize ( this . ctx . _fontSize ) ;
82
63
this . setLineCap ( this . ctx . lineCap ) ;
83
64
this . setLineWidth ( this . ctx . lineWidth ) ;
84
65
this . setLineJoin ( this . ctx . lineJoin ) ;
85
66
} ,
86
67
87
68
beginPath : function ( ) {
88
- path = [ ] ;
69
+ this . path = [ ] ;
89
70
} ,
90
71
91
- endPath : function ( ) {
92
- //TODO implement
72
+ closePath : function ( ) {
73
+ this . path . push ( {
74
+ type : 'close'
75
+ } ) ;
93
76
} ,
94
77
95
78
setFillStyle : function ( style ) {
@@ -191,10 +174,12 @@ function context() {
191
174
192
175
drawImage : function ( img , x , y , w , h ) {
193
176
var format ;
194
- if ( / d a t a : i m a g e \/ p n g .* / i. test ( img ) ) {
195
- format = 'png' ;
177
+ var rx = / d a t a : i m a g e \/ ( \w + ) .* / i;
178
+ var m = rx . exec ( img ) ;
179
+ if ( m != null ) {
180
+ format = m [ 1 ]
196
181
} else {
197
- format = ' jpeg' ;
182
+ format = " jpeg" ;
198
183
}
199
184
this . pdf . addImage ( img , format , x , y , w , h ) ;
200
185
} ,
@@ -208,6 +193,10 @@ function context() {
208
193
switch ( pt . type ) {
209
194
case 'mt' :
210
195
start = pt ;
196
+ if ( typeof start != 'undefined' ) {
197
+ this . pdf . lines ( deltas , start . x , start . y , null , 's' ) ;
198
+ deltas = [ ] ;
199
+ }
211
200
break ;
212
201
case 'lt' :
213
202
var delta = [
@@ -226,7 +215,9 @@ function context() {
226
215
var pt = this . path [ i ] ;
227
216
switch ( pt . type ) {
228
217
case 'arc' :
229
- this . internal . arc ( pt . x , pt . y , pt . radius , pt . startAngle , pt . endAngle , pt . anticlockwise , 's' ) ;
218
+ var start = pt . startAngle * 360 / ( 2 * Math . PI ) ;
219
+ var end = pt . endAngle * 360 / ( 2 * Math . PI ) ;
220
+ this . internal . arc ( pt . x , pt . y , pt . radius , start , end , pt . anticlockwise , 's' ) ;
230
221
break ;
231
222
}
232
223
}
@@ -243,6 +234,10 @@ function context() {
243
234
switch ( pt . type ) {
244
235
case 'mt' :
245
236
start = pt ;
237
+ if ( typeof start != 'undefined' ) {
238
+ this . pdf . lines ( deltas , start . x , start . y , null , 'f' ) ;
239
+ deltas = [ ] ;
240
+ }
246
241
break ;
247
242
case 'lt' :
248
243
var delta = [
@@ -261,7 +256,12 @@ function context() {
261
256
var pt = this . path [ i ] ;
262
257
switch ( pt . type ) {
263
258
case 'arc' :
264
- this . internal . arc ( pt . x , pt . y , pt . radius , pt . startAngle , pt . endAngle , pt . anticlockwise , 'f' ) ;
259
+ var start = pt . startAngle * 360 / ( 2 * Math . PI ) ;
260
+ var end = pt . endAngle * 360 / ( 2 * Math . PI ) ;
261
+ this . internal . arc ( pt . x , pt . y , pt . radius , start , end , pt . anticlockwise , 'f' ) ;
262
+ break ;
263
+ case 'close' :
264
+ this . pdf . internal . out ( 'h' ) ;
265
265
break ;
266
266
}
267
267
}
@@ -291,7 +291,7 @@ function context() {
291
291
}
292
292
} ;
293
293
294
- var c2d = jsPDF . API . context2d ;
294
+ var c2d = jsPDFAPI . context2d ;
295
295
296
296
c2d . internal = { } ;
297
297
@@ -309,13 +309,21 @@ function context() {
309
309
310
310
for ( var i = 0 ; i < curves . length ; i ++ ) {
311
311
var curve = curves [ i ] ;
312
- this . pdf . internal . out ( [
313
- f2 ( ( curve . x1 + xc ) * k ) , f2 ( ( pageHeight - ( curve . y1 + yc ) ) * k ) , 'm' , f2 ( ( curve . x2 + xc ) * k ) , f2 ( ( pageHeight - ( curve . y2 + yc ) ) * k ) , f2 ( ( curve . x3 + xc ) * k ) , f2 ( ( pageHeight - ( curve . y3 + yc ) ) * k ) , f2 ( ( curve . x4 + xc ) * k ) , f2 ( ( pageHeight - ( curve . y4 + yc ) ) * k ) , 'c'
314
- ] . join ( ' ' ) ) ;
312
+ if ( i == 0 ) {
313
+ this . pdf . internal . out ( [
314
+ f2 ( ( curve . x1 + xc ) * k ) , f2 ( ( pageHeight - ( curve . y1 + yc ) ) * k ) , 'm' , f2 ( ( curve . x2 + xc ) * k ) , f2 ( ( pageHeight - ( curve . y2 + yc ) ) * k ) , f2 ( ( curve . x3 + xc ) * k ) , f2 ( ( pageHeight - ( curve . y3 + yc ) ) * k ) , f2 ( ( curve . x4 + xc ) * k ) , f2 ( ( pageHeight - ( curve . y4 + yc ) ) * k ) , 'c'
315
+ ] . join ( ' ' ) ) ;
315
316
316
- if ( style !== null ) {
317
- this . pdf . internal . out ( this . pdf . internal . getStyle ( style ) ) ;
317
+ } else {
318
+ this . pdf . internal . out ( [
319
+ f2 ( ( curve . x2 + xc ) * k ) , f2 ( ( pageHeight - ( curve . y2 + yc ) ) * k ) , f2 ( ( curve . x3 + xc ) * k ) , f2 ( ( pageHeight - ( curve . y3 + yc ) ) * k ) , f2 ( ( curve . x4 + xc ) * k ) , f2 ( ( pageHeight - ( curve . y4 + yc ) ) * k ) , 'c'
320
+ ] . join ( ' ' ) ) ;
318
321
}
322
+ //f2((curve.x1 + xc) * k), f2((pageHeight - (curve.y1 + yc)) * k), 'm', f2((curve.x2 + xc) * k), f2((pageHeight - (curve.y2 + yc)) * k), f2((curve.x3 + xc) * k), f2((pageHeight - (curve.y3 + yc)) * k), f2((curve.x4 + xc) * k), f2((pageHeight - (curve.y4 + yc)) * k), 'c'
323
+ }
324
+
325
+ if ( style !== null ) {
326
+ this . pdf . internal . out ( this . pdf . internal . getStyle ( style ) ) ;
319
327
}
320
328
}
321
329
@@ -415,3 +423,25 @@ function context() {
415
423
416
424
return this ;
417
425
} ) ( jsPDF . API ) ;
426
+
427
+ function context ( ) {
428
+ this . fillStyle = '#000000' ;
429
+ this . strokeStyle = '#000000' ;
430
+ this . font = "12pt times" ;
431
+ this . textBaseline = 'alphabetic' ; //top,bottom,middle,ideographic,alphabetic,hanging
432
+ this . lineWidth = 1 ;
433
+ this . lineJoin = 'miter' ; //round, bevel, miter
434
+ this . lineCap = 'butt' ; //butt, round, square
435
+ //TODO miter limit //default 10
436
+
437
+ this . copy = function ( ctx ) {
438
+ this . fillStyle = ctx . fillStyle ;
439
+ this . strokeStyle = ctx . strokeStyle ;
440
+ this . font = ctx . font ;
441
+ this . lineWidth = ctx . lineWidth ;
442
+ this . lineJoin = ctx . lineJoin ;
443
+ this . lineCap = ctx . lineCap ;
444
+ this . textBaseline = ctx . textBaseline ;
445
+ this . _fontSize = ctx . _fontSize ;
446
+ } ;
447
+ }
0 commit comments