@@ -286,15 +286,39 @@ Gfx::Path CanvasRenderingContext2D::text_path(Utf16String const& text, float x,
286286 }
287287
288288 // Apply text align
289- // FIXME: CanvasTextAlign::Start and CanvasTextAlign::End currently do not nothing for right-to-left languages:
290- // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-textalign-start
291- // Default alignment of draw_text is left so do nothing by CanvasTextAlign::Start and CanvasTextAlign::Left
289+ // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-textalign
290+ // The direction property affects how "start" and "end" are interpreted:
291+ // - "ltr" or "inherit" (default): start=left, end=right
292+ // - "rtl": start=right, end=left
293+
294+ // Determine if we're in RTL mode
295+ bool is_rtl = drawing_state.direction == Bindings::CanvasDirection::Rtl;
296+
297+ // Center alignment is the same regardless of direction
292298 if (drawing_state.text_align == Bindings::CanvasTextAlign::Center) {
293299 transform = Gfx::AffineTransform {}.set_translation ({ -text_width / 2 , 0 }).multiply (transform);
294300 }
295- if (drawing_state.text_align == Bindings::CanvasTextAlign::End || drawing_state.text_align == Bindings::CanvasTextAlign::Right) {
301+ // Handle "start" alignment
302+ else if (drawing_state.text_align == Bindings::CanvasTextAlign::Start) {
303+ // In RTL, "start" means right-aligned (translate by full width)
304+ if (is_rtl) {
305+ transform = Gfx::AffineTransform {}.set_translation ({ -text_width, 0 }).multiply (transform);
306+ }
307+ // In LTR, "start" means left-aligned (no translation needed - default)
308+ }
309+ // Handle "end" alignment
310+ else if (drawing_state.text_align == Bindings::CanvasTextAlign::End) {
311+ // In RTL, "end" means left-aligned (no translation needed)
312+ if (!is_rtl) {
313+ // In LTR, "end" means right-aligned (translate by full width)
314+ transform = Gfx::AffineTransform {}.set_translation ({ -text_width, 0 }).multiply (transform);
315+ }
316+ }
317+ // Explicit "left" and "right" alignments ignore direction
318+ else if (drawing_state.text_align == Bindings::CanvasTextAlign::Right) {
296319 transform = Gfx::AffineTransform {}.set_translation ({ -text_width, 0 }).multiply (transform);
297320 }
321+ // Left is the default - no translation needed
298322
299323 // Apply text baseline
300324 // FIXME: Implement CanvasTextBaseline::Hanging, Bindings::CanvasTextAlign::Alphabetic and Bindings::CanvasTextAlign::Ideographic for real
0 commit comments