From d984cf76e72bbe300c220a62cd7bf0f13f738fc2 Mon Sep 17 00:00:00 2001 From: kerryliu Date: Sun, 14 Sep 2014 09:42:26 -0700 Subject: [PATCH] add unit tests for new textalign support, update readme --- README.md | 1 + canvas2svg.js | 1 + jasmine-tests/spec/canvas2svgspec.js | 63 ++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/README.md b/README.md index aae7258..8c4158a 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ var svg = ctx.getSvg(); Updates ========== +- v1.0.5 fixes for #5 and #6 (with contributions from KoKuToru) - v1.0.4 generate ids that start with a letter - v1.0.3 fixed #4 where largeArcFlag was set incorrectly in some cases - v1.0.2 Split up rgba values set in fill/stroke to allow illustrator import support. diff --git a/canvas2svg.js b/canvas2svg.js index 303c46a..e8e4f33 100644 --- a/canvas2svg.js +++ b/canvas2svg.js @@ -60,6 +60,7 @@ //helper function to map canvas-textAlign to svg-textAnchor function getTextAnchor(textAlign) { + //TODO: support rtl languages var mapping = {"left":"start", "right":"end", "center":"middle", "start":"start", "end":"end"}; return mapping[textAlign] || mapping.start; } diff --git a/jasmine-tests/spec/canvas2svgspec.js b/jasmine-tests/spec/canvas2svgspec.js index b338f93..00674c9 100755 --- a/jasmine-tests/spec/canvas2svgspec.js +++ b/jasmine-tests/spec/canvas2svgspec.js @@ -165,4 +165,67 @@ describe("canvas2svg", function() { }); }); + describe("supports text align", function() { + + it("not specifying a value defaults to 'start'", function() { + + var ctx = new C2S(); + ctx.font = "normal 36px Times"; + ctx.fillStyle = "#000000"; + ctx.fillText("A Text Example", 0, 50); + var svg = ctx.getSvg(); + expect(svg.querySelector("text").getAttribute("text-anchor")).toBe("start"); + + }); + + it("assuming ltr, left maps to 'start'", function() { + + var ctx = new C2S(); + ctx.textAlign = "left"; + ctx.font = "normal 36px Times"; + ctx.fillStyle = "#000000"; + ctx.fillText("A Text Example", 0, 50); + var svg = ctx.getSvg(); + expect(svg.querySelector("text").getAttribute("text-anchor")).toBe("start"); + + }); + + it("assuming ltr, right maps to 'end'", function() { + + var ctx = new C2S(); + ctx.textAlign = "right"; + ctx.font = "normal 36px Times"; + ctx.fillStyle = "#000000"; + ctx.fillText("A Text Example", 0, 50); + var svg = ctx.getSvg(); + expect(svg.querySelector("text").getAttribute("text-anchor")).toBe("end"); + + }); + + it("center maps to 'middle'", function() { + + var ctx = new C2S(); + ctx.textAlign = "center"; + ctx.font = "normal 36px Times"; + ctx.fillStyle = "#000000"; + ctx.fillText("A Text Example", 0, 50); + var svg = ctx.getSvg(); + expect(svg.querySelector("text").getAttribute("text-anchor")).toBe("middle"); + + }); + + it("stores the proper values on save and restore", function() { + var ctx = new C2S(); + ctx.textAlign = "center"; + expect(ctx.textAlign).toBe("center"); + ctx.save(); + expect(ctx.textAlign).toBe("center"); + ctx.textAlign = "right"; + expect(ctx.textAlign).toBe("right"); + ctx.restore(); + expect(ctx.textAlign).toBe("center"); + }); + + }); + });