|
76 | 76 | }, |
77 | 77 |
|
78 | 78 | setFillStyle : function(style) { |
79 | | - if (style.charAt(0) != '#') { |
80 | | - style = this.internal.colorNameToHex(style); |
81 | | - if (!style) { |
82 | | - style = '#000000'; |
| 79 | + |
| 80 | + // get the decimal values of r, g, and b; |
| 81 | + var r, g, b, a; |
| 82 | + |
| 83 | + var m = this.internal.rxRgb.exec(style); |
| 84 | + if (m != null) { |
| 85 | + r = parseInt(m[1]); |
| 86 | + g = parseInt(m[2]); |
| 87 | + b = parseInt(m[3]); |
| 88 | + } else { |
| 89 | + m = this.internal.rxRgba.exec(style); |
| 90 | + if (m != null) { |
| 91 | + r = parseInt(m[1]); |
| 92 | + g = parseInt(m[2]); |
| 93 | + b = parseInt(m[3]); |
| 94 | + a = parseInt(m[4]); |
| 95 | + } else { |
| 96 | + if (style.charAt(0) != '#') { |
| 97 | + style = this.internal.colorNameToHex(style); |
| 98 | + if (!style) { |
| 99 | + style = '#000000'; |
| 100 | + } |
| 101 | + } else { |
| 102 | + } |
| 103 | + this.ctx.fillStyle = style; |
| 104 | + |
| 105 | + if (style.length === 4) { |
| 106 | + r = this.ctx.fillStyle.substring(1, 2); |
| 107 | + r += r; |
| 108 | + g = this.ctx.fillStyle.substring(2, 3); |
| 109 | + g += g; |
| 110 | + b = this.ctx.fillStyle.substring(3, 4); |
| 111 | + b += b; |
| 112 | + } else { |
| 113 | + r = this.ctx.fillStyle.substring(1, 3); |
| 114 | + g = this.ctx.fillStyle.substring(3, 5); |
| 115 | + b = this.ctx.fillStyle.substring(5, 7); |
| 116 | + } |
| 117 | + r = parseInt(r, 16); |
| 118 | + g = parseInt(g, 16); |
| 119 | + b = parseInt(b, 16); |
83 | 120 | } |
84 | 121 | } |
85 | | - this.ctx.fillStyle = style; |
86 | | - var r = this.ctx.fillStyle.substring(1, 3); |
87 | | - r = parseInt(r, 16); |
88 | | - var g = this.ctx.fillStyle.substring(3, 5); |
89 | | - g = parseInt(g, 16); |
90 | | - var b = this.ctx.fillStyle.substring(5, 7); |
91 | | - b = parseInt(b, 16); |
92 | | - this.pdf.setFillColor(r, g, b); |
93 | | - this.pdf.setTextColor(r, g, b); |
| 122 | + this.pdf.setFillColor(r, g, b, { |
| 123 | + a : a |
| 124 | + }); |
| 125 | + this.pdf.setTextColor(r, g, b, { |
| 126 | + a : a |
| 127 | + }); |
94 | 128 | }, |
95 | 129 |
|
96 | 130 | setStrokeStyle : function(style) { |
|
122 | 156 |
|
123 | 157 | setFont : function(font) { |
124 | 158 | this.ctx.font = font; |
125 | | - var rx = /(\d+)pt\s+(\w+)\s*(\w+)?/; |
126 | | - var m = rx.exec(font); |
127 | | - var size = m[1]; |
128 | | - var name = m[2]; |
129 | | - var style = m[3]; |
130 | | - if (!style) { |
131 | | - style = 'normal'; |
| 159 | + |
| 160 | + var rx = /\s*(\w+)\s+(\w+)\s+(\w+)\s+(\d+)(px|pt|em)\s+(\w+)/; |
| 161 | + m = rx.exec(font); |
| 162 | + if (m != null) { |
| 163 | + var fontStyle = m[1]; |
| 164 | + var fontVariant = m[2]; |
| 165 | + var fontWeight = m[3]; |
| 166 | + var fontSize = m[4]; |
| 167 | + var fontSizeUnit = m[5]; |
| 168 | + var fontFamily = m[6]; |
| 169 | + |
| 170 | + if ('px' === fontSizeUnit) { |
| 171 | + fontSize = parseInt(fontSize); |
| 172 | + //fontSize = fontSize * 1.25; |
| 173 | + } else if ('em' === fontSizeUnit) { |
| 174 | + fontSize = parseInt(fontSize) * this.pdf.getFontSize(); |
| 175 | + } else { |
| 176 | + fontSize = parseInt(fontSize); |
| 177 | + } |
| 178 | + |
| 179 | + this.pdf.setFontSize(fontSize); |
| 180 | + |
| 181 | + if (fontWeight === 'bold' || fontWeight === '700') { |
| 182 | + this.pdf.setFontStyle('bold'); |
| 183 | + } else { |
| 184 | + if (fontStyle === 'italic') { |
| 185 | + this.pdf.setFontStyle('italic'); |
| 186 | + } else { |
| 187 | + this.pdf.setFontStyle('normal'); |
| 188 | + } |
| 189 | + } |
| 190 | + // I am disabling changing the font until the font API is more robust |
| 191 | + name = "times"; |
| 192 | + this.pdf.setFont(name, style); |
| 193 | + |
| 194 | + } else { |
| 195 | + var rx = /(\d+)(pt|px|em)\s+(\w+)\s*(\w+)?/; |
| 196 | + var m = rx.exec(font); |
| 197 | + if (m != null) { |
| 198 | + var size = m[1]; |
| 199 | + var unit = m[2]; |
| 200 | + var name = m[3]; |
| 201 | + var style = m[4]; |
| 202 | + if (!style) { |
| 203 | + style = 'normal'; |
| 204 | + } |
| 205 | + if ('em' === fontSizeUnit) { |
| 206 | + size = parseInt(fontSize) * this.pdf.getFontSize(); |
| 207 | + }else{ |
| 208 | + size = parseInt(size); |
| 209 | + } |
| 210 | + this.pdf.setFontSize(size); |
| 211 | + // I am disabling changing the font until the font API is more robust |
| 212 | + name = 'times'; |
| 213 | + this.pdf.setFont(name, style); |
| 214 | + } |
132 | 215 | } |
133 | | - this.pdf.setFontSize(size); |
134 | | - this.pdf.setFont(name, style); |
135 | 216 | }, |
136 | 217 |
|
137 | 218 | setTextBaseline : function(baseline) { |
|
176 | 257 | }, |
177 | 258 |
|
178 | 259 | arc : function(x,y,radius,startAngle,endAngle,anticlockwise) { |
| 260 | + y = y - this.pdf._runningPageHeight; |
179 | 261 | var obj = { |
180 | 262 | type : 'arc', |
181 | 263 | x : x, |
|
290 | 372 | this.path = []; |
291 | 373 | }, |
292 | 374 |
|
| 375 | + clip : function() { |
| 376 | + //TODO not implemented |
| 377 | + }, |
| 378 | + |
| 379 | + translate : function(x,y) { |
| 380 | + this.ctx._translate = { |
| 381 | + x : x, |
| 382 | + y : y |
| 383 | + }; |
| 384 | + //TODO use translate in other drawing methods. |
| 385 | + }, |
| 386 | + measureText : function(text) { |
| 387 | + return { |
| 388 | + getWidth : function() { |
| 389 | + 'use strict'; |
| 390 | + var fontSize = pdf.internal.getFontSize(); |
| 391 | + var txtWidth = pdf.getStringUnitWidth(text) * fontSize / pdf.internal.scaleFactor; |
| 392 | + return txtWidth; |
| 393 | + } |
| 394 | + } |
| 395 | + }, |
293 | 396 | _getBaseline : function(y) { |
294 | 397 | var height = parseInt(this.pdf.internal.getFontSize()); |
295 | 398 | //TODO Get descent from font descriptor |
|
315 | 418 |
|
316 | 419 | var c2d = jsPDFAPI.context2d; |
317 | 420 |
|
| 421 | + // accessor methods |
| 422 | + Object.defineProperty(c2d, 'fillStyle', { |
| 423 | + set : function(value) { |
| 424 | + this.setFillStyle(value); |
| 425 | + }, |
| 426 | + get : function() { |
| 427 | + return this.ctx.fillStyle; |
| 428 | + } |
| 429 | + }); |
| 430 | + Object.defineProperty(c2d, 'textBaseline', { |
| 431 | + set : function(value) { |
| 432 | + this.setTextBaseline(value); |
| 433 | + }, |
| 434 | + get : function() { |
| 435 | + return this.getTextBaseline(); |
| 436 | + } |
| 437 | + }); |
| 438 | + Object.defineProperty(c2d, 'font', { |
| 439 | + set : function(value) { |
| 440 | + this.setFont(value); |
| 441 | + }, |
| 442 | + get : function() { |
| 443 | + return this.getFont(); |
| 444 | + } |
| 445 | + }); |
| 446 | + |
318 | 447 | c2d.internal = {}; |
319 | 448 |
|
| 449 | + c2d.internal.rxRgb = /rgb\s*\(\s*(\d+),\s*(\d+),\s*(\d+\s*)\)/; |
| 450 | + c2d.internal.rxRgba = /rgba\s*\(\s*(\d+),\s*(\d+),\s*(\d+),\s*(\d+)\s*\)/; |
| 451 | + |
320 | 452 | // http://hansmuller-flex.blogspot.com/2011/10/more-about-approximating-circular-arcs.html |
321 | 453 | c2d.internal.arc = function(xc,yc,r,a1,a2,anticlockwise,style) { |
322 | 454 |
|
|
601 | 733 | this.lineWidth = 1; |
602 | 734 | this.lineJoin = 'miter'; //round, bevel, miter |
603 | 735 | this.lineCap = 'butt'; //butt, round, square |
| 736 | + this._translate = { |
| 737 | + x : 0, |
| 738 | + y : 0 |
| 739 | + }; |
604 | 740 | //TODO miter limit //default 10 |
605 | 741 |
|
606 | 742 | this.copy = function(ctx) { |
|
612 | 748 | this.lineCap = ctx.lineCap; |
613 | 749 | this.textBaseline = ctx.textBaseline; |
614 | 750 | this._fontSize = ctx._fontSize; |
| 751 | + this._translate = { |
| 752 | + x : ctx._translate.x, |
| 753 | + y : ctx._translate.y |
| 754 | + }; |
615 | 755 | }; |
616 | 756 | } |
617 | 757 |
|
|
0 commit comments