Skip to content

Commit 74aaebc

Browse files
committed
allow css color names for setStrokeStyle and setFillStyle
annotation plugin should use current page (not last page) move c2d context object into plugin scope added missing comma in object definition added c2d test cases
1 parent 6ab14fc commit 74aaebc

File tree

4 files changed

+214
-28
lines changed

4 files changed

+214
-28
lines changed

jspdf.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -966,10 +966,14 @@ var jsPDF = (function(global) {
966966
},
967967
'pages' : pages,
968968
'out' : out,
969-
'f2' : f2
969+
'f2' : f2,
970970
'getPageInfo' : function(pageNumberOneBased){
971971
var objId = (pageNumberOneBased - 1) * 2 + 3;
972-
return {objId:objId};
972+
return {objId:objId, pageNumber:pageNumberOneBased};
973+
},
974+
'getCurrentPageInfo' : function(){
975+
var objId = (currentPage - 1) * 2 + 3;
976+
return {objId:objId, pageNumber:currentPage};
973977
}
974978
};
975979

jspdf.plugin.annotations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
*/
154154
jsPDFAPI.link = function(x,y,w,h,options) {
155155
'use strict';
156-
this.annotationPlugin.annotations[this.internal.getNumberOfPages()].push({
156+
this.annotationPlugin.annotations[this.internal.getCurrentPageInfo().pageNumber].push({
157157
x : x,
158158
y : y,
159159
w : w,

jspdf.plugin.context2d.js

Lines changed: 189 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@
7676
},
7777

7878
setFillStyle : function(style) {
79+
if (style.charAt(0) != '#') {
80+
style = this.internal.colorNameToHex(style);
81+
if (!style) {
82+
style = '#000000';
83+
}
84+
}
7985
this.ctx.fillStyle = style;
8086
var r = this.ctx.fillStyle.substring(1, 3);
8187
r = parseInt(r, 16);
@@ -88,6 +94,12 @@
8894
},
8995

9096
setStrokeStyle : function(style) {
97+
if (style.charAt(0) != '#') {
98+
style = this.internal.colorNameToHex(style);
99+
if (!style) {
100+
style = '#000000';
101+
}
102+
}
91103
this.ctx.strokeStyle = style;
92104
var r = this.ctx.strokeStyle.substring(1, 3);
93105
r = parseInt(r, 16);
@@ -177,9 +189,10 @@
177189
var rx = /data:image\/(\w+).*/i;
178190
var m = rx.exec(img);
179191
if (m != null) {
180-
format = m[1]
192+
format = m[1];
181193
} else {
182-
format = "jpeg";
194+
//format = "jpeg";
195+
format = "png";
183196
}
184197
this.pdf.addImage(img, format, x, y, w, h);
185198
},
@@ -271,7 +284,7 @@
271284

272285
_getBaseline : function(y) {
273286
var height = parseInt(this.pdf.internal.getFontSize());
274-
//TODO Get this from font
287+
//TODO Get descent from font descriptor
275288
var descent = height * .25;
276289
switch (this.ctx.textBaseline) {
277290
case 'bottom':
@@ -283,6 +296,7 @@
283296
case 'middle':
284297
return y + height / 2 - descent;
285298
case 'ideographic':
299+
//TODO not implemented
286300
return y;
287301
case 'alphabetic':
288302
default:
@@ -421,27 +435,177 @@
421435
};
422436
}
423437

424-
return this;
425-
})(jsPDF.API);
438+
c2d.internal.colorNameToHex = function(color) {
439+
var colors = {
440+
"aliceblue" : "#f0f8ff",
441+
"antiquewhite" : "#faebd7",
442+
"aqua" : "#00ffff",
443+
"aquamarine" : "#7fffd4",
444+
"azure" : "#f0ffff",
445+
"beige" : "#f5f5dc",
446+
"bisque" : "#ffe4c4",
447+
"black" : "#000000",
448+
"blanchedalmond" : "#ffebcd",
449+
"blue" : "#0000ff",
450+
"blueviolet" : "#8a2be2",
451+
"brown" : "#a52a2a",
452+
"burlywood" : "#deb887",
453+
"cadetblue" : "#5f9ea0",
454+
"chartreuse" : "#7fff00",
455+
"chocolate" : "#d2691e",
456+
"coral" : "#ff7f50",
457+
"cornflowerblue" : "#6495ed",
458+
"cornsilk" : "#fff8dc",
459+
"crimson" : "#dc143c",
460+
"cyan" : "#00ffff",
461+
"darkblue" : "#00008b",
462+
"darkcyan" : "#008b8b",
463+
"darkgoldenrod" : "#b8860b",
464+
"darkgray" : "#a9a9a9",
465+
"darkgreen" : "#006400",
466+
"darkkhaki" : "#bdb76b",
467+
"darkmagenta" : "#8b008b",
468+
"darkolivegreen" : "#556b2f",
469+
"darkorange" : "#ff8c00",
470+
"darkorchid" : "#9932cc",
471+
"darkred" : "#8b0000",
472+
"darksalmon" : "#e9967a",
473+
"darkseagreen" : "#8fbc8f",
474+
"darkslateblue" : "#483d8b",
475+
"darkslategray" : "#2f4f4f",
476+
"darkturquoise" : "#00ced1",
477+
"darkviolet" : "#9400d3",
478+
"deeppink" : "#ff1493",
479+
"deepskyblue" : "#00bfff",
480+
"dimgray" : "#696969",
481+
"dodgerblue" : "#1e90ff",
482+
"firebrick" : "#b22222",
483+
"floralwhite" : "#fffaf0",
484+
"forestgreen" : "#228b22",
485+
"fuchsia" : "#ff00ff",
486+
"gainsboro" : "#dcdcdc",
487+
"ghostwhite" : "#f8f8ff",
488+
"gold" : "#ffd700",
489+
"goldenrod" : "#daa520",
490+
"gray" : "#808080",
491+
"green" : "#008000",
492+
"greenyellow" : "#adff2f",
493+
"honeydew" : "#f0fff0",
494+
"hotpink" : "#ff69b4",
495+
"indianred " : "#cd5c5c",
496+
"indigo" : "#4b0082",
497+
"ivory" : "#fffff0",
498+
"khaki" : "#f0e68c",
499+
"lavender" : "#e6e6fa",
500+
"lavenderblush" : "#fff0f5",
501+
"lawngreen" : "#7cfc00",
502+
"lemonchiffon" : "#fffacd",
503+
"lightblue" : "#add8e6",
504+
"lightcoral" : "#f08080",
505+
"lightcyan" : "#e0ffff",
506+
"lightgoldenrodyellow" : "#fafad2",
507+
"lightgrey" : "#d3d3d3",
508+
"lightgreen" : "#90ee90",
509+
"lightpink" : "#ffb6c1",
510+
"lightsalmon" : "#ffa07a",
511+
"lightseagreen" : "#20b2aa",
512+
"lightskyblue" : "#87cefa",
513+
"lightslategray" : "#778899",
514+
"lightsteelblue" : "#b0c4de",
515+
"lightyellow" : "#ffffe0",
516+
"lime" : "#00ff00",
517+
"limegreen" : "#32cd32",
518+
"linen" : "#faf0e6",
519+
"magenta" : "#ff00ff",
520+
"maroon" : "#800000",
521+
"mediumaquamarine" : "#66cdaa",
522+
"mediumblue" : "#0000cd",
523+
"mediumorchid" : "#ba55d3",
524+
"mediumpurple" : "#9370d8",
525+
"mediumseagreen" : "#3cb371",
526+
"mediumslateblue" : "#7b68ee",
527+
"mediumspringgreen" : "#00fa9a",
528+
"mediumturquoise" : "#48d1cc",
529+
"mediumvioletred" : "#c71585",
530+
"midnightblue" : "#191970",
531+
"mintcream" : "#f5fffa",
532+
"mistyrose" : "#ffe4e1",
533+
"moccasin" : "#ffe4b5",
534+
"navajowhite" : "#ffdead",
535+
"navy" : "#000080",
536+
"oldlace" : "#fdf5e6",
537+
"olive" : "#808000",
538+
"olivedrab" : "#6b8e23",
539+
"orange" : "#ffa500",
540+
"orangered" : "#ff4500",
541+
"orchid" : "#da70d6",
542+
"palegoldenrod" : "#eee8aa",
543+
"palegreen" : "#98fb98",
544+
"paleturquoise" : "#afeeee",
545+
"palevioletred" : "#d87093",
546+
"papayawhip" : "#ffefd5",
547+
"peachpuff" : "#ffdab9",
548+
"peru" : "#cd853f",
549+
"pink" : "#ffc0cb",
550+
"plum" : "#dda0dd",
551+
"powderblue" : "#b0e0e6",
552+
"purple" : "#800080",
553+
"red" : "#ff0000",
554+
"rosybrown" : "#bc8f8f",
555+
"royalblue" : "#4169e1",
556+
"saddlebrown" : "#8b4513",
557+
"salmon" : "#fa8072",
558+
"sandybrown" : "#f4a460",
559+
"seagreen" : "#2e8b57",
560+
"seashell" : "#fff5ee",
561+
"sienna" : "#a0522d",
562+
"silver" : "#c0c0c0",
563+
"skyblue" : "#87ceeb",
564+
"slateblue" : "#6a5acd",
565+
"slategray" : "#708090",
566+
"snow" : "#fffafa",
567+
"springgreen" : "#00ff7f",
568+
"steelblue" : "#4682b4",
569+
"tan" : "#d2b48c",
570+
"teal" : "#008080",
571+
"thistle" : "#d8bfd8",
572+
"tomato" : "#ff6347",
573+
"turquoise" : "#40e0d0",
574+
"violet" : "#ee82ee",
575+
"wheat" : "#f5deb3",
576+
"white" : "#ffffff",
577+
"whitesmoke" : "#f5f5f5",
578+
"yellow" : "#ffff00",
579+
"yellowgreen" : "#9acd32"
580+
};
426581

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;
582+
if (typeof colors[color.toLowerCase()] != 'undefined')
583+
return colors[color.toLowerCase()];
584+
585+
return false;
446586
};
447-
}
587+
588+
function context() {
589+
this.fillStyle = '#000000';
590+
this.strokeStyle = '#000000';
591+
this.font = "12pt times";
592+
this.textBaseline = 'alphabetic'; //top,bottom,middle,ideographic,alphabetic,hanging
593+
this.lineWidth = 1;
594+
this.lineJoin = 'miter'; //round, bevel, miter
595+
this.lineCap = 'butt'; //butt, round, square
596+
//TODO miter limit //default 10
597+
598+
this.copy = function(ctx) {
599+
this.fillStyle = ctx.fillStyle;
600+
this.strokeStyle = ctx.strokeStyle;
601+
this.font = ctx.font;
602+
this.lineWidth = ctx.lineWidth;
603+
this.lineJoin = ctx.lineJoin;
604+
this.lineCap = ctx.lineCap;
605+
this.textBaseline = ctx.textBaseline;
606+
this._fontSize = ctx._fontSize;
607+
};
608+
}
609+
610+
return this;
611+
})(jsPDF.API);

test/test_context2d.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,24 @@
7070

7171
ctx.restore();
7272

73+
// CSS Color Names
74+
ctx.save();
75+
ctx.fillText("Testing CSS color names", 20, y + textHeight);
76+
y += textHeight + pad;
77+
78+
ctx.setFillStyle('red');
79+
ctx.fillText("Red", 20, y + textHeight);
80+
y += textHeight + pad;
81+
82+
ctx.setFillStyle('green');
83+
ctx.fillText("Green", 20, y + textHeight);
84+
y += textHeight + pad;
85+
86+
ctx.setStrokeStyle('blue');
87+
ctx.strokeText("Blue", 20, y + textHeight);
88+
y += textHeight + pad;
89+
ctx.restore();
90+
7391
//
7492
// Text baseline
7593
//

0 commit comments

Comments
 (0)