Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement styling of fill/stroke/strokeWidth when using Font.draw() or Glyph.draw() #626

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,11 @@ Create a Path that represents the given text.
* `fontSize`: Size of the text in pixels (default: `72`).

Options is an optional object containing:
* `kerning`: if true takes kerning information into account (default: `true`)
* `kerning`: if `true`, takes kerning information into account (default: `true`)
* `features`: an object with [OpenType feature tags](https://docs.microsoft.com/en-us/typography/opentype/spec/featuretags) as keys, and a boolean value to enable each feature.
Currently only ligature features `"liga"` and `"rlig"` are supported (default: `true`).
* `hinting`: if true uses TrueType font hinting if available (default: `false`).
* `style`: An object of possible styling properties (fill, stroke, strokeWidth) applied to the resulting Path

_**Note:** there is also `Font.getPaths()` with the same arguments, which returns a list of Paths._

Expand All @@ -213,6 +214,7 @@ Options is an optional object containing:
* `features`: an object with [OpenType feature tags](https://docs.microsoft.com/en-us/typography/opentype/spec/featuretags) as keys, and a boolean value to enable each feature.
Currently only ligature features `"liga"` and `"rlig"` are supported (default: `true`).
* `hinting`: if true uses TrueType font hinting if available (default: `false`).
* `style`: An object of possible styling properties (fill, stroke, strokeWidth) applied to the resulting Path

#### `Font.drawPoints(ctx, text, x, y, fontSize, options)`
Draw the points of all glyphs in the text. On-curve points will be drawn in blue, off-curve points will be drawn in red. The arguments are the same as `Font.draw()`.
Expand Down
4 changes: 4 additions & 0 deletions src/font.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ Font.prototype.getPath = function(text, x, y, fontSize, options) {
const glyphPath = glyph.getPath(gX, gY, gFontSize, options, this);
fullPath.extend(glyphPath);
});

if ( options.style !== undefined ) {
fullPath.applyStyles(options.style);
}
return fullPath;
};

Expand Down
4 changes: 4 additions & 0 deletions src/glyph.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ Glyph.prototype.getPath = function(x, y, fontSize, options, font) {
}
}

if ( options.style !== undefined ) {
p.applyStyles(options.style);
}

return p;
};

Expand Down
18 changes: 18 additions & 0 deletions src/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,4 +658,22 @@ Path.prototype.toDOMElement = function(options, pathData) {
return newPath;
};

/**
* Apply styles to the path
* @param {object} [styles] - Styles object {fill,stroke,strokeWidth}
* @return {Path}
*/
Path.prototype.applyStyles = function(styles) {
if ( styles !== undefined ) {
const pathStyles = ['fill','stroke','strokeWidth'];
for(let s = 0; s < pathStyles.length; s++) {
const styleProp = pathStyles[s];
if ( styles[styleProp] !== undefined ) {
this[styleProp] = styles[styleProp];
}
}
}
return this;
};

export default Path;
16 changes: 16 additions & 0 deletions test/font.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ describe('font.js', function() {
});

});

describe('style handling', function() {
let font;

before(function() {
font = loadSync('./test/fonts/Roboto-Black.ttf');
});

it('should apply style options to the path', function() {
const options = {
style: { fill: '#ffaa00' }
};
const path = font.getPath('X', 0, 0, 72, options);
assert.equal(path.fill, '#ffaa00');
});
});

describe('hasChar', function() {
it('returns correct results for non-CMAP fonts', function() {
Expand Down
18 changes: 18 additions & 0 deletions test/glyph.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,24 @@ describe('glyph.js', function() {
});
});
});

describe('style handling', function() {
let font;
let glyph;

before(function() {
font = loadSync('./test/fonts/Roboto-Black.ttf');
glyph = font.charToGlyph('A');
});

it('should apply style options to the path', function() {
const options = {
style: { fill: '#ffaa00' }
};
const path = glyph.getPath(0, 0, 72, options, font);
assert.equal(path.fill, '#ffaa00');
});
});
});

describe('glyph.js on low memory mode', function() {
Expand Down
14 changes: 14 additions & 0 deletions test/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ describe('path.js', function() {
emptyPath.fill = 'black';
assert.equal(emptyPath.toDOMElement().getAttribute('fill'), undefined);
});

it('should apply styles via applyStyles()', function() {
const styles = {
fill: '#ffaa00',
stroke: '#6600aa',
strokeWidth: 5
};
emptyPath.applyStyles(styles);
assert.deepEqual({
fill: emptyPath.fill,
stroke: emptyPath.stroke,
strokeWidth: emptyPath.strokeWidth
}, styles);
});

after(() => {
delete global.document;
Expand Down