Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
snb2013 committed Sep 26, 2013
1 parent 096107d commit df30be9
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 164 deletions.
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ function svg2ttf(svgString, options) {
_.forEach(glyphs, function (glyph) {

//SVG transformations
var svgContours = svg.pathParse(glyph.d).toAbsolute();
svgContours = svg.smoothToGenericCurves(svgContours);
svgContours = svg.cubicToQuad(svgContours, 0.3);
var sfntContours = svg.toSfntCoutours(svgContours);
var svgPath = svg.pathParse(glyph.d)
.abs()
.unshort()
.iterate(svg.cubicToQuad);
var sfntContours = svg.toSfntCoutours(svgPath);

// Add contours to SFNT font
glyph.contours = _.map(sfntContours, function (sfntContour) {
Expand All @@ -117,7 +118,8 @@ function svg2ttf(svgString, options) {
});
});

return sfnt.toTTF(font);
var ttf = sfnt.toTTF(font);
return ttf;
}

module.exports = svg2ttf;
22 changes: 20 additions & 2 deletions lib/svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var _ = require('lodash');
var DOMParser = require('xmldom').DOMParser;
var math = require('./math');

// supports multibyte characters
function getUnicode(character) {
Expand Down Expand Up @@ -92,8 +93,25 @@ function load(str) {
return font;
}

function cubicToQuad(segment, index, x, y) {
if (segment[0] === 'C') {
var quadCurves = math.bezierCubicToQuad(
new math.Point(x, y),
new math.Point(segment[1], segment[2]),
new math.Point(segment[3], segment[4]),
new math.Point(segment[5], segment[6]),
0.3
);

var res = [];
_.forEach(quadCurves, function(curve) {
res.push(['Q', curve[1].x, curve[1].y, curve[2].x, curve[2].y]);
});
return res;
}
}

module.exports.load = load;
module.exports.pathParse = require("./svg/svgpath");
module.exports.smoothToGenericCurves = require("./svg/smooth_to_generic_curves");
module.exports.cubicToQuad = require("./svg/cubic_to_quad");
module.exports.cubicToQuad = cubicToQuad;
module.exports.toSfntCoutours = require("./svg/to_sfnt_contours");
37 changes: 0 additions & 37 deletions lib/svg/cubic_to_quad.js

This file was deleted.

38 changes: 0 additions & 38 deletions lib/svg/smooth_to_generic_curves.js

This file was deleted.

Loading

0 comments on commit df30be9

Please sign in to comment.