Skip to content

Commit

Permalink
Update canvas2svg.js
Browse files Browse the repository at this point in the history
Added textBaseline support.
  • Loading branch information
KoKuToru committed Sep 14, 2014
1 parent 9d15a47 commit 51a1860
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion canvas2svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@
lookup["\\xa0"] = ' ';
return lookup;
}

//helper function to shift y-coordinates by textBaseline
function getTextBaselineOffset(textBaseline, fontSize) {
var fontEmSize = parseFloat(fontSize); //support em ?
fontEmSize = fontEmSize / 1.25; //just a guess
var fontSize = fontEmSize / 1.25; //just a guess
var offset;
if (textBaseline == "top") {
offset = fontEmSize;
} else if (textBaseline == "bottom") {
offset = fontSize - fontEmSize;
} else if (textBaseline == "middle") {
offset = fontSize/2;
} else if (textBaseline == "hanging") {
offset = fontSize;
} else {
//alphabetic normal:
offset = 0;
}
return offset;
}

// Unpack entities lookup where the numbers are in radix 32 to reduce the size
// entity mapping courtesy of tinymce
Expand Down Expand Up @@ -746,7 +767,7 @@
"font-weight" : font.weight,
"text-decoration" : font.decoration,
"x" : x,
"y" : y
"y" : y + getTextBaselineOffset(this.textBaseline, font.size)
}, true);

textElement.appendChild(document.createTextNode(text));
Expand Down

0 comments on commit 51a1860

Please sign in to comment.