@@ -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);
@@ -762,6 +763,27 @@ Context2d::AddPage(const Napi::CallbackInfo& info) {
762
763
cairo_pdf_surface_set_size (canvas ()->surface (), width, height);
763
764
}
764
765
766
+ /*
767
+ * Get text direction.
768
+ */
769
+ Napi::Value
770
+ Context2d::GetDirection (const Napi::CallbackInfo& info) {
771
+ return Napi::String::New (env, state->direction );
772
+ }
773
+
774
+ /*
775
+ * Set text direction.
776
+ */
777
+ void
778
+ Context2d::SetDirection (const Napi::CallbackInfo& info, const Napi::Value& value) {
779
+ if (!value.IsString ()) return ;
780
+
781
+ std::string dir = value.As <Napi::String>();
782
+ if (dir != " ltr" && dir != " rtl" ) return ;
783
+
784
+ state->direction = dir;
785
+ }
786
+
765
787
/*
766
788
* Put image data.
767
789
*
@@ -2451,6 +2473,9 @@ Context2d::paintText(const Napi::CallbackInfo&info, bool stroke) {
2451
2473
pango_layout_set_text (layout, str.c_str (), -1 );
2452
2474
pango_cairo_update_layout (context (), layout);
2453
2475
2476
+ PangoDirection pango_dir = state->direction == " ltr" ? PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL;
2477
+ pango_context_set_base_dir (pango_layout_get_context (_layout), pango_dir);
2478
+
2454
2479
if (argsNum == 3 ) {
2455
2480
scaled_by = get_text_scale (layout, args[2 ]);
2456
2481
cairo_save (context ());
@@ -2522,18 +2547,26 @@ inline double getBaselineAdjustment(PangoLayout* layout, short baseline) {
2522
2547
void
2523
2548
Context2d::setTextPath (double x, double y) {
2524
2549
PangoRectangle logical_rect;
2550
+ text_align_t alignment = state->textAlignment ;
2525
2551
2526
- switch (state->textAlignment ) {
2552
+ // Convert start/end to left/right based on direction
2553
+ if (alignment == TEXT_ALIGNMENT_START) {
2554
+ alignment = (state->direction == " rtl" ) ? TEXT_ALIGNMENT_RIGHT : TEXT_ALIGNMENT_LEFT;
2555
+ } else if (alignment == TEXT_ALIGNMENT_END) {
2556
+ alignment = (state->direction == " rtl" ) ? TEXT_ALIGNMENT_LEFT : TEXT_ALIGNMENT_RIGHT;
2557
+ }
2558
+
2559
+ switch (alignment) {
2527
2560
case TEXT_ALIGNMENT_CENTER:
2528
2561
pango_layout_get_pixel_extents (_layout, NULL , &logical_rect);
2529
2562
x -= logical_rect.width / 2 ;
2530
2563
break ;
2531
- case TEXT_ALIGNMENT_END:
2532
2564
case TEXT_ALIGNMENT_RIGHT:
2533
2565
pango_layout_get_pixel_extents (_layout, NULL , &logical_rect);
2534
2566
x -= logical_rect.width ;
2535
2567
break ;
2536
- default : ;
2568
+ default : // TEXT_ALIGNMENT_LEFT
2569
+ break ;
2537
2570
}
2538
2571
2539
2572
y -= getBaselineAdjustment (_layout, state->textBaseline );
@@ -2687,13 +2720,12 @@ Napi::Value
2687
2720
Context2d::GetTextAlign (const Napi::CallbackInfo& info) {
2688
2721
const char * align;
2689
2722
switch (state->textAlignment ) {
2690
- default :
2691
- // TODO the default is supposed to be "start"
2692
2723
case TEXT_ALIGNMENT_LEFT: align = " left" ; break ;
2693
- case TEXT_ALIGNMENT_START: align = " start" ; break ;
2694
- case TEXT_ALIGNMENT_CENTER: align = " center" ; break ;
2695
2724
case TEXT_ALIGNMENT_RIGHT: align = " right" ; break ;
2725
+ case TEXT_ALIGNMENT_CENTER: align = " center" ; break ;
2726
+ case TEXT_ALIGNMENT_START: align = " start" ; break ;
2696
2727
case TEXT_ALIGNMENT_END: align = " end" ; break ;
2728
+ default : align = " start" ;
2697
2729
}
2698
2730
return Napi::String::New (env, align);
2699
2731
}
0 commit comments