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

Render small-caps font variant #1611

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Binary file added examples/crimsonFont/Crimson-Bold.ttf
Binary file not shown.
Binary file added examples/crimsonFont/Crimson-BoldItalic.ttf
Binary file not shown.
Binary file added examples/crimsonFont/Crimson-Italic.ttf
Binary file not shown.
Binary file added examples/crimsonFont/Crimson-Roman.ttf
Binary file not shown.
Binary file added examples/crimsonFont/Crimson-Semibold.ttf
Binary file not shown.
Binary file added examples/crimsonFont/Crimson-SemiboldItalic.ttf
Binary file not shown.
13 changes: 13 additions & 0 deletions src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2499,6 +2499,7 @@ NAN_GETTER(Context2d::GetFont) {

/*
* Set font:
* - variant
* - weight
* - style
* - size
Expand All @@ -2523,6 +2524,7 @@ NAN_SETTER(Context2d::SetFont) {
if (mparsed->IsUndefined()) return;
Local<Object> font = Nan::To<Object>(mparsed).ToLocalChecked();

Nan::Utf8String variant(Nan::Get(font, Nan::New("variant").ToLocalChecked()).ToLocalChecked());
Nan::Utf8String weight(Nan::Get(font, Nan::New("weight").ToLocalChecked()).ToLocalChecked());
Nan::Utf8String style(Nan::Get(font, Nan::New("style").ToLocalChecked()).ToLocalChecked());
double size = Nan::To<double>(Nan::Get(font, Nan::New("size").ToLocalChecked()).ToLocalChecked()).FromMaybe(0);
Expand All @@ -2547,6 +2549,17 @@ NAN_SETTER(Context2d::SetFont) {
context->state->fontDescription = sys_desc;
pango_layout_set_font_description(context->_layout, sys_desc);

PangoAttribute *features;
if (strlen(*variant) > 0 && strcmp("small-caps", *variant)==0) {
features = pango_attr_font_features_new("smcp 1, onum 1");
}else{
samizdatco marked this conversation as resolved.
Show resolved Hide resolved
features = pango_attr_font_features_new("smcp 0, onum 0");
}
PangoAttrList *attrList = pango_attr_list_new();
pango_attr_list_insert(attrList, features);
pango_layout_set_attributes(context->_layout, attrList);
pango_attr_list_unref(attrList);

context->_font.Reset(value);
}

Expand Down
20 changes: 20 additions & 0 deletions test/canvas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,26 @@ describe('Canvas', function () {
assert.equal(ctx.font, '15px Arial, sans-serif')
});

it('Context2d#font=small-caps', function () {
registerFont('./examples/crimsonFont/Crimson-Bold.ttf', {family: 'Crimson'})
let canvas = createCanvas(200,200),
ctx = canvas.getContext('2d');

// here is where the dot will appear above a lower-case 'i' (and be missing for small-caps)
let offsetX = 15, offsetY = -115;

ctx.font = "180px Crimson";
ctx.fillText('i', 20, 180);
let lcDottedI = ctx.getImageData(20+offsetX, 180+offsetY, 20,20).data;
assert.equal(lcDottedI.some(p => p > 0), true);

ctx.font = "small-caps 180px Crimson";
ctx.fillText('i', 80, 180);
let scEmptySpace = ctx.getImageData(80+offsetX, 180+offsetY, 20,20).data;
assert.equal(scEmptySpace.every(p => p == 0), true);
});


it('Context2d#lineWidth=', function () {
var canvas = createCanvas(200, 200)
, ctx = canvas.getContext('2d');
Expand Down