@@ -161,7 +161,8 @@ Context2d::Initialize(Napi::Env& env, Napi::Object& exports) {
161
161
InstanceAccessor<&Context2d::GetStrokeStyle, &Context2d::SetStrokeStyle>(" strokeStyle" , napi_default_jsproperty),
162
162
InstanceAccessor<&Context2d::GetFont, &Context2d::SetFont>(" font" , napi_default_jsproperty),
163
163
InstanceAccessor<&Context2d::GetTextBaseline, &Context2d::SetTextBaseline>(" textBaseline" , napi_default_jsproperty),
164
- InstanceAccessor<&Context2d::GetTextAlign, &Context2d::SetTextAlign>(" textAlign" , napi_default_jsproperty)
164
+ InstanceAccessor<&Context2d::GetTextAlign, &Context2d::SetTextAlign>(" textAlign" , napi_default_jsproperty),
165
+ InstanceAccessor<&Context2d::GetDirection, &Context2d::SetDirection>(" direction" , napi_default_jsproperty)
165
166
});
166
167
167
168
exports.Set (" CanvasRenderingContext2d" , ctor);
@@ -230,6 +231,8 @@ Context2d::Context2d(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Context2
230
231
pango_context_set_round_glyph_positions (pango_layout_get_context (_layout), FALSE );
231
232
#endif
232
233
234
+ pango_layout_set_auto_dir (_layout, FALSE );
235
+
233
236
states.emplace ();
234
237
state = &states.top ();
235
238
pango_layout_set_font_description (_layout, state->fontDescription );
@@ -762,6 +765,27 @@ Context2d::AddPage(const Napi::CallbackInfo& info) {
762
765
cairo_pdf_surface_set_size (canvas ()->surface (), width, height);
763
766
}
764
767
768
+ /*
769
+ * Get text direction.
770
+ */
771
+ Napi::Value
772
+ Context2d::GetDirection (const Napi::CallbackInfo& info) {
773
+ return Napi::String::New (env, state->direction );
774
+ }
775
+
776
+ /*
777
+ * Set text direction.
778
+ */
779
+ void
780
+ Context2d::SetDirection (const Napi::CallbackInfo& info, const Napi::Value& value) {
781
+ if (!value.IsString ()) return ;
782
+
783
+ std::string dir = value.As <Napi::String>();
784
+ if (dir != " ltr" && dir != " rtl" ) return ;
785
+
786
+ state->direction = dir;
787
+ }
788
+
765
789
/*
766
790
* Put image data.
767
791
*
@@ -2451,6 +2475,9 @@ Context2d::paintText(const Napi::CallbackInfo&info, bool stroke) {
2451
2475
pango_layout_set_text (layout, str.c_str (), -1 );
2452
2476
pango_cairo_update_layout (context (), layout);
2453
2477
2478
+ PangoDirection pango_dir = state->direction == " ltr" ? PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL;
2479
+ pango_context_set_base_dir (pango_layout_get_context (_layout), pango_dir);
2480
+
2454
2481
if (argsNum == 3 ) {
2455
2482
scaled_by = get_text_scale (layout, args[2 ]);
2456
2483
cairo_save (context ());
@@ -2522,18 +2549,26 @@ inline double getBaselineAdjustment(PangoLayout* layout, short baseline) {
2522
2549
void
2523
2550
Context2d::setTextPath (double x, double y) {
2524
2551
PangoRectangle logical_rect;
2552
+ text_align_t alignment = state->textAlignment ;
2525
2553
2526
- switch (state->textAlignment ) {
2554
+ // Convert start/end to left/right based on direction
2555
+ if (alignment == TEXT_ALIGNMENT_START) {
2556
+ alignment = (state->direction == " rtl" ) ? TEXT_ALIGNMENT_RIGHT : TEXT_ALIGNMENT_LEFT;
2557
+ } else if (alignment == TEXT_ALIGNMENT_END) {
2558
+ alignment = (state->direction == " rtl" ) ? TEXT_ALIGNMENT_LEFT : TEXT_ALIGNMENT_RIGHT;
2559
+ }
2560
+
2561
+ switch (alignment) {
2527
2562
case TEXT_ALIGNMENT_CENTER:
2528
2563
pango_layout_get_pixel_extents (_layout, NULL , &logical_rect);
2529
2564
x -= logical_rect.width / 2 ;
2530
2565
break ;
2531
- case TEXT_ALIGNMENT_END:
2532
2566
case TEXT_ALIGNMENT_RIGHT:
2533
2567
pango_layout_get_pixel_extents (_layout, NULL , &logical_rect);
2534
2568
x -= logical_rect.width ;
2535
2569
break ;
2536
- default : ;
2570
+ default : // TEXT_ALIGNMENT_LEFT
2571
+ break ;
2537
2572
}
2538
2573
2539
2574
y -= getBaselineAdjustment (_layout, state->textBaseline );
@@ -2687,13 +2722,12 @@ Napi::Value
2687
2722
Context2d::GetTextAlign (const Napi::CallbackInfo& info) {
2688
2723
const char * align;
2689
2724
switch (state->textAlignment ) {
2690
- default :
2691
- // TODO the default is supposed to be "start"
2692
2725
case TEXT_ALIGNMENT_LEFT: align = " left" ; break ;
2693
2726
case TEXT_ALIGNMENT_START: align = " start" ; break ;
2694
2727
case TEXT_ALIGNMENT_CENTER: align = " center" ; break ;
2695
2728
case TEXT_ALIGNMENT_RIGHT: align = " right" ; break ;
2696
2729
case TEXT_ALIGNMENT_END: align = " end" ; break ;
2730
+ default : align = " start" ;
2697
2731
}
2698
2732
return Napi::String::New (env, align);
2699
2733
}
0 commit comments