Skip to content

Commit 6ce963d

Browse files
mapmeldchearon
andauthored
Set pango language through ctx.lang (#2526)
Co-authored-by: Caleb Hearon <caleb@chearon.net>
1 parent d548caa commit 6ce963d

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
99
==================
1010
### Changed
1111
### Added
12+
* Added `ctx.lang` to set the ISO language code for text
1213
### Fixed
1314

1415
3.1.2

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ export class CanvasRenderingContext2D {
291291
textAlign: CanvasTextAlign;
292292
canvas: Canvas;
293293
direction: 'ltr' | 'rtl';
294+
lang: string;
294295
}
295296

296297
export class CanvasGradient {

src/CanvasRenderingContext2d.cc

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ constexpr double twoPi = M_PI * 2.;
4343
#define PANGO_LAYOUT_GET_METRICS(LAYOUT) pango_context_get_metrics( \
4444
pango_layout_get_context(LAYOUT), \
4545
pango_layout_get_font_description(LAYOUT), \
46-
pango_context_get_language(pango_layout_get_context(LAYOUT)))
46+
pango_language_from_string(state->lang.c_str()))
4747

4848
inline static bool checkArgs(const Napi::CallbackInfo&info, double *args, int argsNum, int offset = 0){
4949
Napi::Env env = info.Env();
@@ -162,7 +162,8 @@ Context2d::Initialize(Napi::Env& env, Napi::Object& exports) {
162162
InstanceAccessor<&Context2d::GetFont, &Context2d::SetFont>("font", napi_default_jsproperty),
163163
InstanceAccessor<&Context2d::GetTextBaseline, &Context2d::SetTextBaseline>("textBaseline", napi_default_jsproperty),
164164
InstanceAccessor<&Context2d::GetTextAlign, &Context2d::SetTextAlign>("textAlign", napi_default_jsproperty),
165-
InstanceAccessor<&Context2d::GetDirection, &Context2d::SetDirection>("direction", napi_default_jsproperty)
165+
InstanceAccessor<&Context2d::GetDirection, &Context2d::SetDirection>("direction", napi_default_jsproperty),
166+
InstanceAccessor<&Context2d::GetLanguage, &Context2d::SetLanguage>("lang", napi_default_jsproperty)
166167
});
167168

168169
exports.Set("CanvasRenderingContext2d", ctor);
@@ -786,6 +787,25 @@ Context2d::SetDirection(const Napi::CallbackInfo& info, const Napi::Value& value
786787
state->direction = dir;
787788
}
788789

790+
/*
791+
* Get language.
792+
*/
793+
Napi::Value
794+
Context2d::GetLanguage(const Napi::CallbackInfo& info) {
795+
return Napi::String::New(env, state->lang);
796+
}
797+
798+
/*
799+
* Set language.
800+
*/
801+
void
802+
Context2d::SetLanguage(const Napi::CallbackInfo& info, const Napi::Value& value) {
803+
if (!value.IsString()) return;
804+
805+
std::string lang = value.As<Napi::String>();
806+
state->lang = lang;
807+
}
808+
789809
/*
790810
* Put image data.
791811
*
@@ -2490,6 +2510,9 @@ Context2d::paintText(const Napi::CallbackInfo& info, bool stroke) {
24902510

24912511
checkFonts();
24922512
pango_layout_set_text(layout, str.c_str(), -1);
2513+
if (state->lang != "") {
2514+
pango_context_set_language(pango_layout_get_context(_layout), pango_language_from_string(state->lang.c_str()));
2515+
}
24932516
pango_cairo_update_layout(context(), layout);
24942517

24952518
PangoDirection pango_dir = state->direction == "ltr" ? PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL;
@@ -2802,6 +2825,9 @@ Context2d::MeasureText(const Napi::CallbackInfo& info) {
28022825

28032826
checkFonts();
28042827
pango_layout_set_text(layout, str.Utf8Value().c_str(), -1);
2828+
if (state->lang != "") {
2829+
pango_context_set_language(pango_layout_get_context(_layout), pango_language_from_string(state->lang.c_str()));
2830+
}
28052831
pango_cairo_update_layout(ctx, layout);
28062832

28072833
// Normally you could use pango_layout_get_pixel_extents and be done, or use

src/CanvasRenderingContext2d.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct canvas_state_t {
3636
canvas_draw_mode_t textDrawingMode = TEXT_DRAW_PATHS;
3737
bool imageSmoothingEnabled = true;
3838
std::string direction = "ltr";
39+
std::string lang = "";
3940

4041
canvas_state_t() {
4142
fontDescription = pango_font_description_from_string("sans");
@@ -61,6 +62,7 @@ struct canvas_state_t {
6162
fontDescription = pango_font_description_copy(other.fontDescription);
6263
font = other.font;
6364
imageSmoothingEnabled = other.imageSmoothingEnabled;
65+
lang = other.lang;
6466
}
6567

6668
~canvas_state_t() {
@@ -157,6 +159,7 @@ class Context2d : public Napi::ObjectWrap<Context2d> {
157159
Napi::Value GetFont(const Napi::CallbackInfo& info);
158160
Napi::Value GetTextBaseline(const Napi::CallbackInfo& info);
159161
Napi::Value GetTextAlign(const Napi::CallbackInfo& info);
162+
Napi::Value GetLanguage(const Napi::CallbackInfo& info);
160163
void SetPatternQuality(const Napi::CallbackInfo& info, const Napi::Value& value);
161164
void SetImageSmoothingEnabled(const Napi::CallbackInfo& info, const Napi::Value& value);
162165
void SetGlobalCompositeOperation(const Napi::CallbackInfo& info, const Napi::Value& value);
@@ -179,6 +182,7 @@ class Context2d : public Napi::ObjectWrap<Context2d> {
179182
void SetFont(const Napi::CallbackInfo& info, const Napi::Value& value);
180183
void SetTextBaseline(const Napi::CallbackInfo& info, const Napi::Value& value);
181184
void SetTextAlign(const Napi::CallbackInfo& info, const Napi::Value& value);
185+
void SetLanguage(const Napi::CallbackInfo& info, const Napi::Value& value);
182186
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 16, 0)
183187
void BeginTag(const Napi::CallbackInfo& info);
184188
void EndTag(const Napi::CallbackInfo& info);

test/canvas.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2490,7 +2490,8 @@ describe('Canvas', function () {
24902490
['patternQuality', 'best'],
24912491
// ['quality', 'best'], // doesn't do anything, TODO remove
24922492
['textDrawingMode', 'glyph'],
2493-
['antialias', 'gray']
2493+
['antialias', 'gray'],
2494+
['lang', 'eu']
24942495
]
24952496

24962497
for (const [k, v] of state) {

0 commit comments

Comments
 (0)