Merge pull request #42 from Janpot/fix-font

Fix __parseFont to not crash
This commit is contained in:
clintjd 2016-04-21 11:59:26 -07:00
commit db62162a47
2 changed files with 14 additions and 1 deletions

View File

@ -880,7 +880,7 @@
* @private
*/
ctx.prototype.__parseFont = function() {
var regex = /^\s*(?=(?:(?:[-a-z]+\s*){0,2}(italic|oblique))?)(?=(?:(?:[-a-z]+\s*){0,2}(small-caps))?)(?=(?:(?:[-a-z]+\s*){0,2}(bold(?:er)?|lighter|[1-9]00))?)(?:(?:normal|\1|\2|\3)\s*){0,3}((?:xx?-)?(?:small|large)|medium|smaller|larger|[.\d]+(?:\%|in|[cem]m|ex|p[ctx]))(?:\s*\/\s*(normal|[.\d]+(?:\%|in|[cem]m|ex|p[ctx])))?\s*([-,\"\sa-z]+?)\s*$/i;
var regex = /^\s*(?=(?:(?:[-a-z]+\s*){0,2}(italic|oblique))?)(?=(?:(?:[-a-z]+\s*){0,2}(small-caps))?)(?=(?:(?:[-a-z]+\s*){0,2}(bold(?:er)?|lighter|[1-9]00))?)(?:(?:normal|\1|\2|\3)\s*){0,3}((?:xx?-)?(?:small|large)|medium|smaller|larger|[.\d]+(?:\%|in|[cem]m|ex|p[ctx]))(?:\s*\/\s*(normal|[.\d]+(?:\%|in|[cem]m|ex|p[ctx])))?\s*([-,\'\"\sa-z]+?)\s*$/i;
var fontPart = regex.exec( this.font );
var data = {
style : fontPart[1] || 'normal',

View File

@ -297,6 +297,19 @@ describe('canvas2svg', function() {
});
describe("supports fonts", function () {
it("doesn't crash when using a font", function () {
var ctx = new C2S();
ctx.font = "normal 12px 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif";
ctx.fillText("A Text Example", 0, 50);
var svg = ctx.getSvg();
expect(svg.querySelector("text").getAttribute("font-family")).to.equal("\'Helvetica Neue\', \'Helvetica\', \'Arial\', sans-serif");
expect(svg.querySelector("text").getAttribute("font-size")).to.equal("12px");
expect(svg.querySelector("text").getAttribute("font-weight")).to.equal("normal");
expect(svg.querySelector("text").getAttribute("font-style")).to.equal("normal");
});
});
describe("supports globalOpacity", function() {
it("set stroke-opacity when stroking and set fill-opacity when filling",function() {
var ctx = new C2S();