+ R.registerFont = function (font) {
+ if (!font.face) {
+ return;
+ }
+ this.fonts = this.fonts || {};
+ if (this.fonts[font.face["font-family"]]) {
+ this.fonts[font.face["font-family"]].push(font);
+ } else {
+ this.fonts[font.face["font-family"]] = [font];
+ }
+ font.face["units-per-em"] = parseInt(font.face["units-per-em"], 10);
+
+ for (var glyph in font.glyphs) {
+ var path = font.glyphs[glyph];
+ if (path.d) {
+ path.d = "M" + path.d.replace(/[mlcxtrv]/g, function (command) {
+ return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
+ }) + "z";
+ }
+ }
+ };
+ paper.getFont = function (family, weight, style, stretch) {
+ stretch = stretch || "normal";
+ style = style || "normal";
+ weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
+ var font = R.fonts[family];
+ if (!font) {
+ var name = new RegExp("(^|\\s)" + family.replace(/[^\w\d\s+!~.:_-]/g, "") + "(\\s|$)", "i");
+ for (var fontName in R.fonts) {
+ if (name.test(fontName)) {
+ font = R.fonts[fontName];
+ break;
+ }
+ }
+ }
+ var thefont;
+ if (font) {
+ for (var i = 0, ii = font.length; i < ii; i++) {
+ thefont = font[i];
+ if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
+ break;
+ }
+ }
+ }
+ return thefont;
+ };
+
+ paper.print = function (x, y, string, font, size) {
+ var out = this.set(),
+ letters = (string + "").split(""),
+ shift = 0,
+ path = "",
+ scale;
+ if (font) {
+ scale = (size || 16) / font.face["units-per-em"];
+ for (var i = 0, ii = letters.length; i < ii; i++) {
+ var prev = i && font.glyphs[letters[i - 1]] || {},
+ curr = font.glyphs[letters[i]];
+ shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
+ curr && curr.d && out.push(this.path({fill: "#000", stroke: "none"}, curr.d).translate(shift, 0));
+ }
+ out.scale(scale, scale, 0, y).translate(x, (size || 16) / 2);
+ }
+ return out;
+ };
+
+