Skip to content

Truly independent web browser

LibGfx+LibWeb: Turn the glyphs of sideways-lr text counterclockwise

sideshowbarker5b814abb3700
diff --git a/Libraries/LibCompositing/Rust/build.rs b/Libraries/LibCompositing/Rust/build.rs
index e47f9ac50ba..3c2264aa6e8 100644
--- a/Libraries/LibCompositing/Rust/build.rs
+++ b/Libraries/LibCompositing/Rust/build.rs
@@ -569,6 +569,7 @@ fn main() -> Result<(), Box<dyn Error>> {
("CompositingAndBlendingOperator", "Gfx::CompositingAndBlendingOperator"),
("ColorFilterType", "Gfx::ColorFilterType"),
("Orientation", "Gfx::Orientation"),
+ ("GlyphRunOrientation", "Gfx::GlyphRunOrientation"),
("MaskKind", "Gfx::MaskKind"),
("ShouldAntiAlias", "Gfx::ShouldAntiAlias"),
("CssPixels", "Web::CSSPixels"),
diff --git a/Libraries/LibCompositing/Rust/src/display_list/commands.rs b/Libraries/LibCompositing/Rust/src/display_list/commands.rs
index 1faa1404b81..10f016626d1 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/commands.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/commands.rs
@@ -785,7 +785,7 @@ pub struct DrawGlyphRun {
pub translation: FloatPoint,
pub scale: f32,
pub color: Color,
- pub orientation: Orientation,
+ pub orientation: GlyphRunOrientation,
pub font_smoothing: u8,
}
display_list_record_fields!(DrawGlyphRun {
@@ -1200,7 +1200,7 @@ pub struct PaintTextShadow {
pub rect: IntRect,
pub translation: FloatPoint,
pub scale: f32,
- pub orientation: Orientation,
+ pub orientation: GlyphRunOrientation,
pub font_smoothing: u8,
}
display_list_record_fields!(PaintTextShadow {
diff --git a/Libraries/LibCompositing/Rust/src/display_list/ffi_bytes.rs b/Libraries/LibCompositing/Rust/src/display_list/ffi_bytes.rs
index 400b0102d75..836abb5d90c 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/ffi_bytes.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/ffi_bytes.rs
@@ -7,9 +7,10 @@
use crate::css_pixels::{FfiCssPixelPoint, FfiCssPixelRect};
use libgfx_rust::{
AffineTransform, CapStyle, Color, ColorFilterType, CompositingAndBlendingOperator, CornerRadii, CornerRadius,
- FloatMatrix4x4, FloatPoint, FloatRect, FloatSize, GradientInterpolationMethod, GradientInterpolationType,
- HueInterpolationMethod, IntPoint, IntRect, IntSize, InterpolationColorSpace, JoinStyle, LineStyle, MaskKind,
- Orientation, PolarColorSpace, RectangularColorSpace, ScalingMode, ShouldAntiAlias, WindingRule,
+ FloatMatrix4x4, FloatPoint, FloatRect, FloatSize, GlyphRunOrientation, GradientInterpolationMethod,
+ GradientInterpolationType, HueInterpolationMethod, IntPoint, IntRect, IntSize, InterpolationColorSpace, JoinStyle,
+ LineStyle, MaskKind, Orientation, PolarColorSpace, RectangularColorSpace, ScalingMode, ShouldAntiAlias,
+ WindingRule,
};
pub trait FfiBytes: Sized {
@@ -125,6 +126,7 @@ ffi_enum_bytes!(ScalingMode as i32);
ffi_enum_bytes!(CompositingAndBlendingOperator as i32);
ffi_enum_bytes!(MaskKind as i32);
ffi_enum_bytes!(Orientation as i32);
+ffi_enum_bytes!(GlyphRunOrientation as i32);
ffi_enum_bytes!(ShouldAntiAlias as u8);
ffi_enum_bytes!(InterpolationColorSpace as i32);
ffi_enum_bytes!(RectangularColorSpace as u8);
diff --git a/Libraries/LibCompositing/Rust/src/display_list/record_fields.rs b/Libraries/LibCompositing/Rust/src/display_list/record_fields.rs
index 6e6453939e6..893e1716a13 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/record_fields.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/record_fields.rs
@@ -161,6 +161,7 @@ record_field_enum!(
LineStyle,
MaskKind,
Orientation,
+ GlyphRunOrientation,
ScalingMode,
ShouldAntiAlias,
WindingRule,
diff --git a/Libraries/LibCompositing/Rust/src/display_list/validate.rs b/Libraries/LibCompositing/Rust/src/display_list/validate.rs
index 5e26de7f376..987d6e28ccd 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/validate.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/validate.rs
@@ -12,9 +12,9 @@
use super::builder::{COMMAND_ALIGNMENT, HEADER_SIZE, bounding_rect_computed_from_payload, read_header};
use super::commands::*;
use libgfx_rust::{
- CapStyle, Color, CompositingAndBlendingOperator, GradientInterpolationMethod, GradientInterpolationType,
- HueInterpolationMethod, IntRect, InterpolationColorSpace, JoinStyle, LineStyle, MaskKind, Orientation,
- PolarColorSpace, RectangularColorSpace, ScalingMode, ShouldAntiAlias, WindingRule,
+ CapStyle, Color, CompositingAndBlendingOperator, GlyphRunOrientation, GradientInterpolationMethod,
+ GradientInterpolationType, HueInterpolationMethod, IntRect, InterpolationColorSpace, JoinStyle, LineStyle,
+ MaskKind, Orientation, PolarColorSpace, RectangularColorSpace, ScalingMode, ShouldAntiAlias, WindingRule,
};
use std::mem::{align_of, offset_of, size_of};
@@ -67,6 +67,7 @@ tape_enum!(InterpolationColorSpace as i32 { LinearRGB, SRGB });
tape_enum!(CapStyle as i32 { Butt, Round, Square });
tape_enum!(JoinStyle as i32 { Miter, Round, Bevel });
tape_enum!(Orientation as i32 { Horizontal, Vertical });
+tape_enum!(GlyphRunOrientation as i32 { Horizontal, Vertical, VerticalCounterclockwise });
tape_enum!(GradientInterpolationType as u8 { Rectangular, Polar });
tape_enum!(RectangularColorSpace as u8 {
Srgb,
@@ -387,7 +388,7 @@ fn validate_command_fields(
return Err("Glyph run stroke width must be finite and nonnegative");
}
p.array_span_at::<DisplayListGlyph>(offset_of!(DrawGlyphRun, glyphs))?;
- p.enum_at::<Orientation>(offset_of!(DrawGlyphRun, orientation))?;
+ p.enum_at::<GlyphRunOrientation>(offset_of!(DrawGlyphRun, orientation))?;
}
T::FillRect => {
p.enum_at::<CompositingAndBlendingOperator>(offset_of!(FillRect, compositing_and_blending_operator))?;
@@ -485,7 +486,7 @@ fn validate_command_fields(
T::PaintTextShadow => {
p.array_span_at::<DisplayListGlyph>(offset_of!(PaintTextShadow, glyphs))?;
p.array_span_at::<TextShadowLayer>(offset_of!(PaintTextShadow, layers))?;
- p.enum_at::<Orientation>(offset_of!(PaintTextShadow, orientation))?;
+ p.enum_at::<GlyphRunOrientation>(offset_of!(PaintTextShadow, orientation))?;
}
T::PaintPathBoxShadow => {
p.span_at(offset_of!(PaintPathBoxShadow, shape_path_data))?;
diff --git a/Libraries/LibGfx/Orientation.h b/Libraries/LibGfx/Orientation.h
index 6929ad666ef..969b13562b6 100644
--- a/Libraries/LibGfx/Orientation.h
+++ b/Libraries/LibGfx/Orientation.h
@@ -13,6 +13,14 @@ enum class Orientation {
Vertical
};
+// How a glyph run is set along its line: as horizontal text, or turned a quarter turn clockwise to run down a vertical
+// line, or a quarter turn counterclockwise to run up one.
+enum class GlyphRunOrientation {
+ Horizontal,
+ Vertical,
+ VerticalCounterclockwise,
+};
+
}
using Gfx::Orientation;
diff --git a/Libraries/LibGfx/Rust/src/geometry.rs b/Libraries/LibGfx/Rust/src/geometry.rs
index e54c818b577..0a03fa90841 100644
--- a/Libraries/LibGfx/Rust/src/geometry.rs
+++ b/Libraries/LibGfx/Rust/src/geometry.rs
@@ -81,6 +81,17 @@ pub enum Orientation {
Vertical,
}
+/// How a glyph run is set along its line: as horizontal text, or turned a quarter turn clockwise to run down a vertical
+/// line, or a quarter turn counterclockwise to run up one.
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+#[repr(i32)]
+pub enum GlyphRunOrientation {
+ #[default]
+ Horizontal,
+ Vertical,
+ VerticalCounterclockwise,
+}
+
impl FloatPoint {
pub fn primary_offset_for_orientation(self, orientation: Orientation) -> f32 {
match orientation {
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_content.rs b/Libraries/LibWeb/Rust/src/layout/inline_content.rs
index d51ba865c43..d558c97e96d 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_content.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_content.rs
@@ -1030,9 +1030,9 @@ impl<'content> PhysicalPlane<'content> {
// NB: Glyphs that run along a vertical line are turned a quarter turn clockwise, so their line-over side
// faces the physical right side. That is the block-start side of the frame of vertical-rl, but the
// block-end side of the frame of vertical-lr, where each line is laid out mirrored in the block axis.
+ // The glyphs of sideways-lr are turned counterclockwise instead, so they face its line-over side on the
+ // left, its block-start side, and run upwards like its inline axis.
// https://drafts.csswg.org/css-writing-modes-4/#line-orientation
- // FIXME: Turn the glyphs of sideways-lr a quarter turn counterclockwise instead, so that they face its
- // line-over side on the left and run upwards like its inline axis.
if !self.lines_are_mirrored {
return block_offset;
}
diff --git a/Libraries/LibWeb/Rust/src/layout/query/inline_cursor.rs b/Libraries/LibWeb/Rust/src/layout/query/inline_cursor.rs
index db6660c0f6f..27bb2a6594b 100644
--- a/Libraries/LibWeb/Rust/src/layout/query/inline_cursor.rs
+++ b/Libraries/LibWeb/Rust/src/layout/query/inline_cursor.rs
@@ -257,6 +257,8 @@ impl<'a, A: PaintRead> InlineCursor<'a, A> {
let coordinate = CssPixels::nearest_value_for_f32(coordinate);
if text_fragment::is_horizontal(fragment) {
rect.x = origin.x + coordinate;
+ } else if text_fragment::runs_upwards(fragment) {
+ rect.y = origin.bottom() - coordinate - rect.height;
} else {
rect.y = origin.y + coordinate;
}
@@ -341,7 +343,10 @@ impl<'a, A: PaintRead> InlineCursor<'a, A> {
// The line box is unpositioned; move its block-axis extent with the atomic box and its inline ancestors.
rect.x += item_rect.x - fragment_rect.x + fragment.relpos_delta.x;
rect.y += item_rect.y - fragment_rect.y + fragment.relpos_delta.y;
- let at_end = after != arena.node_style_if_live(node)?.inline_axis_is_reverse();
+ // NB: The box's start side comes from the way its line runs and the box's direction, not from the box's own
+ // writing mode, which can turn its inline axis across the line.
+ let rtl = arena.node_style_if_live(node)?.direction() == crate::css::css_enums::direction::RTL;
+ let at_end = after != (rtl != text_fragment::runs_upwards(fragment));
let thickness = CssPixels::from_integer(1);
if text_fragment::is_horizontal(fragment) {
rect.x = if at_end { item_rect.right() } else { item_rect.x };
@@ -422,6 +427,14 @@ impl<'a, A: PaintRead> InlineCursor<'a, A> {
.is_none_or(|record| record.writing_mode == writing_mode::HORIZONTAL_TB)
}
+ /// Whether the inline axis of `line` runs upwards, as in sideways-lr.
+ fn line_runs_upwards(&self, line: InlineLine) -> bool {
+ let side = self.arena.committed_side_data(line.root);
+ side.lines()
+ .get(line.index as usize)
+ .is_some_and(|record| record.writing_mode == writing_mode::SIDEWAYS_LR)
+ }
+
/// The middle of `line` along the block axis.
pub(crate) fn line_block_coordinate(&self, line: InlineLine) -> CssPixels {
let side = self.arena.committed_side_data(line.root);
@@ -700,8 +713,26 @@ impl<'a, A: PaintRead> InlineCursor<'a, A> {
let arena = self.arena;
let side = arena.committed_side_data(line.root);
let horizontal = self.line_is_horizontal(line);
- let x = if horizontal { point.x } else { point.y };
- let inline_start = |rect: CssPixelRect| if horizontal { rect.x } else { rect.y };
+ // NB: Positions along the line are measured in the direction of its inline axis, so the inline axis of a line
+ // that runs upwards is measured from the bottom: negated.
+ let upwards = self.line_runs_upwards(line);
+ let zero = CssPixels::from_raw(0);
+ let x = if horizontal {
+ point.x
+ } else if upwards {
+ zero - point.y
+ } else {
+ point.y
+ };
+ let inline_start = |rect: CssPixelRect| {
+ if horizontal {
+ rect.x
+ } else if upwards {
+ zero - rect.bottom()
+ } else {
+ rect.y
+ }
+ };
let mut chosen = None;
for fragment in self.fragments_on_line(&side, line, accepts) {
let rect = text_fragment::absolute_rect(arena, fragment);
@@ -713,7 +744,15 @@ impl<'a, A: PaintRead> InlineCursor<'a, A> {
return self.position_before_forced_break(line);
};
// INTEROP: A point in the gap between two atomic inlines goes to the one after it.
- let inline_end = |rect: CssPixelRect| if horizontal { rect.right() } else { rect.bottom() };
+ let inline_end = |rect: CssPixelRect| {
+ if horizontal {
+ rect.right()
+ } else if upwards {
+ zero - rect.y
+ } else {
+ rect.bottom()
+ }
+ };
if fragment.is_atomic_inline
&& x >= inline_end(rect)
&& let Some(next) = self
@@ -730,11 +769,10 @@ impl<'a, A: PaintRead> InlineCursor<'a, A> {
&& !last.is_atomic_inline
{
let last_rect = text_fragment::absolute_rect(arena, last);
- let past_line_end = match (horizontal, last.direction == crate::css::css_enums::direction::LTR) {
- (true, true) => x >= last_rect.right(),
- (true, false) => x < last_rect.x,
- (false, true) => x >= last_rect.bottom(),
- (false, false) => x < last_rect.y,
+ let past_line_end = if last.direction == crate::css::css_enums::direction::LTR {
+ x >= inline_end(last_rect)
+ } else {
+ x < inline_start(last_rect)
};
if past_line_end {
let collapsible = matches!(
@@ -752,11 +790,14 @@ impl<'a, A: PaintRead> InlineCursor<'a, A> {
if fragment.is_atomic_inline {
return Some(self.position_for_point_at_atomic_inline(fragment.layout_node, line, point, accepts));
}
+ // NB: A point in front of the fragment's inline start is taken to be at it.
let mut point_in_fragment = point;
if horizontal {
- point_in_fragment.x = rect.x.max(x);
+ point_in_fragment.x = rect.x.max(point.x);
+ } else if upwards {
+ point_in_fragment.y = rect.bottom().min(point.y);
} else {
- point_in_fragment.y = rect.y.max(x);
+ point_in_fragment.y = rect.y.max(point.y);
}
let offset = text_fragment::index_in_node_for_point(arena, fragment, point_in_fragment);
Some(self.text_position_in_fragment(fragment, offset))
@@ -833,33 +874,54 @@ impl<'a, A: PaintRead> InlineCursor<'a, A> {
let side = arena.committed_side_data(line.root);
let mut fragments = self.fragments_on_line(&side, line, accepts);
let horizontal = self.line_is_horizontal(line);
- let rect = |fragment: &&FragmentRecord| text_fragment::absolute_rect(arena, fragment);
+ // NB: The left and right of a line are its line-left and line-right sides, so a line that runs upwards has
+ // its left end at the bottom.
+ // https://drafts.csswg.org/css-writing-modes-4/#line-left
+ let upwards = self.line_runs_upwards(line);
+ let zero = CssPixels::from_raw(0);
+ let line_left = |fragment: &&FragmentRecord| {
+ let rect = text_fragment::absolute_rect(arena, fragment);
+ if horizontal {
+ rect.x
+ } else if upwards {
+ zero - rect.bottom()
+ } else {
+ rect.y
+ }
+ };
+ let line_right = |fragment: &&FragmentRecord| {
+ let rect = text_fragment::absolute_rect(arena, fragment);
+ if horizontal {
+ rect.right()
+ } else if upwards {
+ zero - rect.y
+ } else {
+ rect.bottom()
+ }
+ };
let fragment = match edge {
LineEdge::Start => fragments.next(),
LineEdge::End => fragments.last(),
- LineEdge::Left => fragments.min_by_key(|fragment| {
- let rect = rect(fragment);
- if horizontal { rect.x } else { rect.y }
- }),
- LineEdge::Right => fragments.max_by_key(|fragment| {
- let rect = rect(fragment);
- if horizontal { rect.right() } else { rect.bottom() }
- }),
+ LineEdge::Left => fragments.min_by_key(line_left),
+ LineEdge::Right => fragments.max_by_key(line_right),
};
let Some(fragment) = fragment else {
return self.position_before_forced_break(line);
};
if fragment.is_atomic_inline {
- let reverse = arena
+ // NB: A box's line-left side is its inline-start side whenever its direction is ltr. That's so even in
+ // sideways-lr, whose inline axis runs upwards from the bottom of the line.
+ // https://drafts.csswg.org/css-writing-modes-4/#line-left
+ let rtl = arena
.node_style_if_live(fragment.layout_node)
- .is_some_and(|style| style.inline_axis_is_reverse());
+ .is_some_and(|style| style.direction() == crate::css::css_enums::direction::RTL);
return Some(CaretPosition::BesideAtomicInline {
node: fragment.layout_node,
after: match edge {
LineEdge::Start => false,
LineEdge::End => true,
- LineEdge::Left => reverse,
- LineEdge::Right => !reverse,
+ LineEdge::Left => rtl,
+ LineEdge::Right => !rtl,
},
});
}
diff --git a/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rs b/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rs
index eea0c83ec75..d28969e04d8 100644
--- a/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rs
+++ b/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rs
@@ -1202,7 +1202,7 @@ impl DisplayListRecorder {
stroke_width: f32,
rect: IntRect,
scale: f64,
- orientation: Orientation,
+ orientation: GlyphRunOrientation,
glyph_bounding_rect: IntRect,
force_dark_role: ForceDarkRole,
) {
@@ -1284,7 +1284,7 @@ impl DisplayListRecorder {
translation: FloatPoint,
run: GlyphRunForRecording<impl IntoIterator<Item = DisplayListGlyph>>,
glyph_run_scale: f64,
- orientation: Orientation,
+ orientation: GlyphRunOrientation,
force_dark_role: ForceDarkRole,
) {
let mut payload = CommandPayloadBuilder::new::<PaintTextShadow>(&self.builder);
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/inspector_overlay.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/inspector_overlay.rs
index 24bf5829f5f..e40136ff6c4 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/inspector_overlay.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/inspector_overlay.rs
@@ -19,7 +19,7 @@ use crate::painting::host::{FfiFlexOverlayInput, FfiGridOverlayInput};
use crate::painting::paintable_geometry;
use crate::painting::record::PaintRecorder;
use crate::painting::record::inputs::{InspectorHighlight, OverlayLabelFonts};
-use libgfx_rust::{Color, FloatPoint, IntPoint, IntRect, LineStyle, Orientation};
+use libgfx_rust::{Color, FloatPoint, GlyphRunOrientation, IntPoint, IntRect, LineStyle};
pub(crate) fn record_inspector_overlays<O: Observer>(recorder: &mut PaintRecorder<'_, O>) {
let inputs = recorder.inputs;
@@ -536,7 +536,7 @@ fn draw_label<O: Observer>(recorder: &mut PaintRecorder<'_, O>, label: &OverlayL
0.0,
rect,
1.0,
- Orientation::Horizontal,
+ GlyphRunOrientation::Horizontal,
glyph_bounding_rect,
ForceDarkRole::None,
);
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/math.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/math.rs
index e8783500db2..226fa0c2353 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/math.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/math.rs
@@ -14,7 +14,7 @@ use crate::painting::display_list::recorder::GlyphRunForRecording;
use crate::painting::force_dark::ForceDarkRole;
use crate::painting::paintable_geometry;
use crate::painting::record::PaintRecorder;
-use libgfx_rust::{Color, FloatPoint, Orientation};
+use libgfx_rust::{Color, FloatPoint, GlyphRunOrientation};
/// Paints what MathML layout drew for a box: its fraction bar, its radical symbol and overbar, or the glyphs of its
/// stretched operator.
@@ -95,7 +95,7 @@ pub(crate) fn paint_foreground<O: Observer>(
width,
ink_rect,
scale,
- Orientation::Horizontal,
+ GlyphRunOrientation::Horizontal,
ink_rect,
ForceDarkRole::Foreground,
);
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs
index 56f281e6a38..e50c6f1421a 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs
@@ -724,7 +724,7 @@ fn paint_color_glyph_runs<O: Observer>(
0.0,
device_rect,
f64::from(device_scale),
- libgfx_rust::Orientation::Horizontal,
+ libgfx_rust::GlyphRunOrientation::Horizontal,
device_rect,
ForceDarkRole::Svg,
);
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs
index 20ca4586a25..d6ad8fdd79c 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs
@@ -19,7 +19,7 @@ use crate::painting::record::inputs::CaretTarget;
use crate::painting::selection::{HighlightPseudoElement, SelectionRange};
use crate::painting::text_fragment::{self, SelectionOffsets};
use libgfx_rust::path::Path;
-use libgfx_rust::{Color, FloatPoint, IntPoint, IntRect, Orientation, WindingRule};
+use libgfx_rust::{Color, FloatPoint, GlyphRunOrientation, IntPoint, IntRect, WindingRule};
use std::collections::HashSet;
use std::sync::Arc;
@@ -464,7 +464,7 @@ fn compute_render_spans<O: Observer>(
pub(crate) struct GlyphRunEmission {
pub baseline_start: FloatPoint,
- pub orientation: Orientation,
+ pub orientation: GlyphRunOrientation,
pub glyph_bounding_rect: IntRect,
}
@@ -472,15 +472,51 @@ pub(crate) struct GlyphRunEmission {
/// width and turning it a quarter turn clockwise about the rect's top-left corner, so the blob's
/// bounds follow the same mapping.
fn vertical_glyph_bounds(horizontal_bounds: IntRect, fragment_device_rect: IntRect) -> IntRect {
+ let (bounds, fragment) = (WideRect::of(horizontal_bounds), WideRect::of(fragment_device_rect));
IntRect::new(
- fragment_device_rect.x + fragment_device_rect.width
- - (horizontal_bounds.y + horizontal_bounds.height - fragment_device_rect.y),
- fragment_device_rect.y + (horizontal_bounds.x - fragment_device_rect.x),
+ saturated(fragment.x + fragment.width - (bounds.y + bounds.height - fragment.y)),
+ saturated(fragment.y + (bounds.x - fragment.x)),
horizontal_bounds.height,
horizontal_bounds.width,
)
}
+/// The player draws a counterclockwise run by turning the horizontal blob a quarter turn counterclockwise, so the
+/// blob's start lands on the bottom-left corner of the fragment rect, and its bounds follow the same mapping.
+fn counterclockwise_glyph_bounds(horizontal_bounds: IntRect, fragment_device_rect: IntRect) -> IntRect {
+ let (bounds, fragment) = (WideRect::of(horizontal_bounds), WideRect::of(fragment_device_rect));
+ IntRect::new(
+ saturated(fragment.x + (bounds.y - fragment.y)),
+ saturated(fragment.y + fragment.height - (bounds.x + bounds.width - fragment.x)),
+ horizontal_bounds.height,
+ horizontal_bounds.width,
+ )
+}
+
+/// A device rect with room to add its coordinates to its sizes: Glyph bounds come from float coordinates that saturate
+/// at the ends of the i32 range, so their sums can go past it.
+struct WideRect {
+ x: i64,
+ y: i64,
+ width: i64,
+ height: i64,
+}
+
+impl WideRect {
+ fn of(rect: IntRect) -> Self {
+ Self {
+ x: rect.x.into(),
+ y: rect.y.into(),
+ width: rect.width.into(),
+ height: rect.height.into(),
+ }
+ }
+}
+
+fn saturated(value: i64) -> i32 {
+ value.clamp(i32::MIN.into(), i32::MAX.into()) as i32
+}
+
pub(crate) fn glyph_run_emission(
fragment: &crate::painting::paintable_data::FragmentRecord,
run: &crate::painting::paintable_data::GlyphRunRecord,
@@ -493,10 +529,12 @@ pub(crate) fn glyph_run_emission(
x: (fragment_absolute_rect.x.to_float() as f64 * scale) as f32,
y: ((fragment_absolute_rect.y.to_float() + fragment.baseline.to_float()) as f64 * scale) as f32,
};
- let orientation = if fragment.writing_mode == crate::css::css_enums::writing_mode::HORIZONTAL_TB {
- Orientation::Horizontal
- } else {
- Orientation::Vertical
+ // https://drafts.csswg.org/css-writing-modes-4/#valdef-writing-mode-sideways-lr
+ // "... and all text is typeset sideways, with the glyphs' tops facing left (the line-over side)."
+ let orientation = match fragment.writing_mode {
+ crate::css::css_enums::writing_mode::HORIZONTAL_TB => GlyphRunOrientation::Horizontal,
+ crate::css::css_enums::writing_mode::SIDEWAYS_LR => GlyphRunOrientation::VerticalCounterclockwise,
+ _ => GlyphRunOrientation::Vertical,
};
let horizontal_bounds = IntRect::new(
(bounds.x * scale as f32 + baseline_start.x).round_ties_even() as i32,
@@ -505,8 +543,11 @@ pub(crate) fn glyph_run_emission(
(bounds.height * scale as f32).round_ties_even() as i32,
);
let glyph_bounding_rect = match orientation {
- Orientation::Horizontal => horizontal_bounds,
- Orientation::Vertical => vertical_glyph_bounds(horizontal_bounds, fragment_device_rect),
+ GlyphRunOrientation::Horizontal => horizontal_bounds,
+ GlyphRunOrientation::Vertical => vertical_glyph_bounds(horizontal_bounds, fragment_device_rect),
+ GlyphRunOrientation::VerticalCounterclockwise => {
+ counterclockwise_glyph_bounds(horizontal_bounds, fragment_device_rect)
+ }
};
GlyphRunEmission {
baseline_start,
@@ -983,4 +1024,13 @@ mod tests {
// right, four pixels short of the fragment's right edge.
assert_eq!(vertical, IntRect::new(100, 52, 24, 40));
}
+
+ #[test]
+ fn turned_glyph_bounds_saturate_rather_than_overflow() {
+ let fragment = IntRect::new(16, 16, 20, 60);
+ // A run so long that its width saturated when its float bounds were taken to device pixels.
+ let horizontal = IntRect::new(16, 16, i32::MAX, 24);
+ assert_eq!(vertical_glyph_bounds(horizontal, fragment).height, i32::MAX);
+ assert_eq!(counterclockwise_glyph_bounds(horizontal, fragment).y, i32::MIN + 77);
+ }
}
diff --git a/Libraries/LibWeb/Rust/src/painting/text_fragment.rs b/Libraries/LibWeb/Rust/src/painting/text_fragment.rs
index 6ea19c550e3..45453e91559 100644
--- a/Libraries/LibWeb/Rust/src/painting/text_fragment.rs
+++ b/Libraries/LibWeb/Rust/src/painting/text_fragment.rs
@@ -80,6 +80,12 @@ pub(crate) fn is_horizontal(fragment: &FragmentRecord) -> bool {
fragment.writing_mode == crate::css::css_enums::writing_mode::HORIZONTAL_TB
}
+/// Whether the inline axis of a vertical fragment runs upwards, as in sideways-lr, whose glyphs are turned a quarter
+/// turn counterclockwise: Its text starts at the bottom of the fragment, and the line-over side is on the left.
+pub(crate) fn runs_upwards(fragment: &FragmentRecord) -> bool {
+ fragment.writing_mode == crate::css::css_enums::writing_mode::SIDEWAYS_LR
+}
+
fn primary_size(rect: CssPixelRect, horizontal: bool) -> CssPixels {
if horizontal { rect.width } else { rect.height }
}
@@ -330,6 +336,9 @@ fn rect_for_selection_offsets_of_fragment_at(
if horizontal {
rect.x += pixel_offset;
rect.width = pixel_width;
+ } else if runs_upwards(fragment) {
+ rect.y = rect.bottom() - pixel_offset - pixel_width;
+ rect.height = pixel_width;
} else {
rect.y += pixel_offset;
rect.height = pixel_width;
@@ -362,6 +371,13 @@ pub(crate) fn glyph_rect_of_fragment_at(
pixel(glyph.width),
pixel(glyph.height),
)
+ } else if runs_upwards(fragment) {
+ CssPixelRect::new(
+ rect.x + fragment.baseline + pixel(glyph.y),
+ rect.bottom() - pixel(glyph.x + glyph.width),
+ pixel(glyph.height),
+ pixel(glyph.width),
+ )
} else {
CssPixelRect::new(
rect.right() - fragment.baseline - pixel(glyph.y + glyph.height),
@@ -417,6 +433,9 @@ pub(crate) fn rects_for_selection_offsets(
if horizontal {
piece.x = origin.x + offset;
piece.width = size;
+ } else if runs_upwards(fragment) {
+ piece.y = origin.bottom() - offset - size;
+ piece.height = size;
} else {
piece.y = origin.y + offset;
piece.height = size;
@@ -474,6 +493,8 @@ pub(crate) fn index_in_node_for_point(
let rect = absolute_rect(layout_arena, fragment);
let x = if is_horizontal(fragment) {
(position.x - rect.x).to_float()
+ } else if runs_upwards(fragment) {
+ (rect.bottom() - position.y).to_float()
} else {
(position.y - rect.y).to_float()
};
diff --git a/Services/Compositor/DisplayListPlayerSkia.cpp b/Services/Compositor/DisplayListPlayerSkia.cpp
index ce34fcdb85f..4902bda7b05 100644
--- a/Services/Compositor/DisplayListPlayerSkia.cpp
+++ b/Services/Compositor/DisplayListPlayerSkia.cpp
@@ -297,33 +297,38 @@ void DisplayListPlayerSkia::paint_scrollbar(PresentedScrollbar const& scrollbar)
paint_scrollbar_into_surface(surface(), scrollbar);
}
-static SkMatrix vertical_glyph_run_rotation(Gfx::IntRect const& rect)
-{
+// The turn that sets a run laid out as horizontal text, from the top-left corner of rect, along a vertical line.
+static SkMatrix vertical_glyph_run_rotation(Gfx::IntRect const& rect, Gfx::GlyphRunOrientation orientation)
+{
+ if (orientation == Gfx::GlyphRunOrientation::VerticalCounterclockwise) {
+ // A quarter turn counterclockwise about the top-left corner, then down by the rect's height: The run starts at
+ // the bottom-left corner and runs upwards, with the tops of its glyphs facing left.
+ auto rotation = SkMatrix::Translate(0, rect.height());
+ rotation.preRotate(-90, rect.top_left().x(), rect.top_left().y());
+ return rotation;
+ }
auto rotation = SkMatrix::Translate(rect.width(), 0);
rotation.preRotate(90, rect.top_left().x(), rect.top_left().y());
return rotation;
}
-static void draw_glyph_run(SkCanvas& canvas, SkTextBlob const& blob, Gfx::IntRect const& rect, Gfx::FloatPoint translation, Gfx::Orientation orientation, SkPaint const& paint)
+static void draw_glyph_run(SkCanvas& canvas, SkTextBlob const& blob, Gfx::IntRect const& rect, Gfx::FloatPoint translation, Gfx::GlyphRunOrientation orientation, SkPaint const& paint)
{
- switch (orientation) {
- case Gfx::Orientation::Horizontal:
- canvas.drawTextBlob(&blob, translation.x(), translation.y(), paint);
- break;
- case Gfx::Orientation::Vertical:
- canvas.save();
- canvas.concat(vertical_glyph_run_rotation(rect));
+ if (orientation == Gfx::GlyphRunOrientation::Horizontal) {
canvas.drawTextBlob(&blob, translation.x(), translation.y(), paint);
- canvas.restore();
- break;
+ return;
}
+ canvas.save();
+ canvas.concat(vertical_glyph_run_rotation(rect, orientation));
+ canvas.drawTextBlob(&blob, translation.x(), translation.y(), paint);
+ canvas.restore();
}
-static SkRect glyph_run_bounds_including_antialiasing(SkTextBlob const& blob, Gfx::IntRect const& rect, Gfx::FloatPoint translation, Gfx::Orientation orientation)
+static SkRect glyph_run_bounds_including_antialiasing(SkTextBlob const& blob, Gfx::IntRect const& rect, Gfx::FloatPoint translation, Gfx::GlyphRunOrientation orientation)
{
auto bounds = blob.bounds().makeOffset(translation.x(), translation.y());
- if (orientation == Gfx::Orientation::Vertical)
- bounds = vertical_glyph_run_rotation(rect).mapRect(bounds);
+ if (orientation != Gfx::GlyphRunOrientation::Horizontal)
+ bounds = vertical_glyph_run_rotation(rect, orientation).mapRect(bounds);
bounds.outset(1, 1);
return bounds;
}
@@ -339,7 +344,7 @@ void DisplayListPlayerSkia::play_command(DrawGlyphRun const& command)
bool uses_lcd_text = !opaque_background.isEmpty()
&& m_surfaces.size() == 1
&& command.stroke_width == 0
- && command.orientation == Gfx::Orientation::Horizontal
+ && command.orientation == Gfx::GlyphRunOrientation::Horizontal
&& canvas.getTopProps().pixelGeometry() != kUnknown_SkPixelGeometry
&& canvas.getLocalToDeviceAs3x3().isTranslate();
if (uses_lcd_text) {
diff --git a/Services/Compositor/Rust/src/overlays.rs b/Services/Compositor/Rust/src/overlays.rs
index 098246ad10c..f4d90a15c4d 100644
--- a/Services/Compositor/Rust/src/overlays.rs
+++ b/Services/Compositor/Rust/src/overlays.rs
@@ -25,8 +25,8 @@ use libcompositing_rust::time::MonotonicTime;
use libcompositing_rust::visual_context::{TransformData, TransformDataRole, VisualContextTree};
use libgfx_rust::path::Path;
use libgfx_rust::{
- CapStyle, Color, CompositingAndBlendingOperator, FloatMatrix4x4, FloatPoint, FloatRect, IntRect, IntSize,
- JoinStyle, Orientation, ShouldAntiAlias, WindingRule, enclosing_int_rect,
+ CapStyle, Color, CompositingAndBlendingOperator, FloatMatrix4x4, FloatPoint, FloatRect, GlyphRunOrientation,
+ IntRect, IntSize, JoinStyle, ShouldAntiAlias, WindingRule, enclosing_int_rect,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -552,7 +552,7 @@ impl OverlayRecorder<'_> {
translation: FloatPoint::default(),
scale,
color,
- orientation: Orientation::Horizontal,
+ orientation: GlyphRunOrientation::Horizontal,
font_smoothing: 0,
},
&inline_data,
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-writing-modes/reference/sideways-lr-main-axis-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-writing-modes/reference/sideways-lr-main-axis-ref.html
new file mode 100644
index 00000000000..00999acf88d
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-writing-modes/reference/sideways-lr-main-axis-ref.html
@@ -0,0 +1,61 @@
+<!DOCTYPE html>
+<title>Reference: sideways-lr flexbox main axis progresses in correct direction</title>
+<style>
+ .container {
+ font-size: 0
+ }
+ .item {
+ width: 20px;
+ height: 20px;
+ }
+
+ .item:nth-child(1) { background-color: lime; }
+ .item:nth-child(2) { background-color: limegreen; }
+ .item:nth-child(3) { background-color: green; }
+
+ .container.reverse .item:nth-child(1) { background-color: green; }
+ .container.reverse .item:nth-child(2) { background-color: limegreen; }
+ .container.reverse .item:nth-child(3) { background-color: lime; }
+
+ .container.row .item { display: inline-block; }
+</style>
+<div class="container reverse">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+</div>
+<div class="container">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+</div>
+<div class="container">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+</div>
+<div class="container reverse">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+</div>
+<div class="container row">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+</div>
+<div class="container row">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+</div>
+<div class="container row reverse">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+</div>
+<div class="container row reverse">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+</div>
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-writing-modes/sideways-inline-001-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-writing-modes/sideways-inline-001-ref.html
new file mode 100644
index 00000000000..c5de07f9f85
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-writing-modes/sideways-inline-001-ref.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<title>Reference for Sideways Inline Layout: Basic Case</title>
+<meta charset=utf-8>
+<link rel="stylesheet" type="text/css" href="../../fonts/ahem.css">
+<style>
+body > div {
+ /* styling */
+ border: solid silver;
+ font: 30px/1 Ahem;
+ color: blue;
+ float: left;
+
+ /* test */
+ padding: 2px 4px 6px 8px;
+ margin: 2px 4px 6px 8px;
+}
+div > div {
+ transform: rotate(180deg);
+}
+</style>
+
+<div style="writing-mode: vertical-lr"><div>ApÉx</div></div>
+
+<div style="writing-mode: vertical-rl">ApÉx</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-writing-modes/sideways-inline-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-writing-modes/sideways-inline-001.html
new file mode 100644
index 00000000000..9fd628e2c66
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-writing-modes/sideways-inline-001.html
@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<title>Sideways Inline Layout: Basic Case</title>
+<meta charset=utf-8>
+<link rel="help" href="https://www.w3.org/TR/css-writing-modes-4/#block-flow">
+
+<link rel="match" href="../../../../expected/wpt-import/css/css-writing-modes/sideways-inline-001-ref.html">
+
+<link rel="stylesheet" type="text/css" href="../../fonts/ahem.css">
+<style>
+div {
+ /* styling */
+ border: solid silver;
+ font: 30px/1 Ahem;
+ color: blue;
+ float: left;
+
+ /* test */
+ padding: 2px 4px 6px 8px;
+ margin: 2px 4px 6px 8px;
+}
+</style>
+
+<div style="writing-mode: sideways-lr">ApÉx</div>
+
+<div style="writing-mode: sideways-rl">ApÉx</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-writing-modes/sideways-lr-main-axis.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-writing-modes/sideways-lr-main-axis.html
new file mode 100644
index 00000000000..7df1cddb383
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-writing-modes/sideways-lr-main-axis.html
@@ -0,0 +1,57 @@
+<!DOCTYPE html>
+<title>sideways-lr flexbox main axis progresses in correct direction</title>
+<link rel="author" title="Tim Nguyen" href="https://github.com/nt1m">
+<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#box-model">
+<link rel="match" href="../../../../expected/wpt-import/css/css-writing-modes/reference/sideways-lr-main-axis-ref.html">
+<style>
+ .container {
+ display: flex;
+ }
+ .item {
+ width: 20px;
+ height: 20px;
+ }
+ .item:nth-child(1) { background-color: lime; }
+ .item:nth-child(2) { background-color: limegreen; }
+ .item:nth-child(3) { background-color: green; }
+</style>
+<div class="container" style="writing-mode: sideways-lr; flex-direction: row;">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+</div>
+<div class="container" style="writing-mode: sideways-lr; direction: rtl; flex-direction: row;">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+</div>
+<div class="container" style="writing-mode: sideways-lr; flex-direction: row-reverse;">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+</div>
+<div class="container" style="writing-mode: sideways-lr; direction: rtl; flex-direction: row-reverse;">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+</div>
+<div class="container" style="writing-mode: sideways-lr; flex-direction: column;">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+</div>
+<div class="container" style="writing-mode: sideways-lr; direction: rtl; flex-direction: column;">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+</div>
+<div class="container" style="writing-mode: sideways-lr; flex-direction: column-reverse;">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+</div>
+<div class="container" style="writing-mode: sideways-lr; direction: rtl; flex-direction: column-reverse;">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+</div>
diff --git a/Tests/LibWeb/Text/expected/css/sideways-lr-text-geometry-runs-upwards.txt b/Tests/LibWeb/Text/expected/css/sideways-lr-text-geometry-runs-upwards.txt
new file mode 100644
index 00000000000..49ea106aa7d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/sideways-lr-text-geometry-runs-upwards.txt
@@ -0,0 +1,16 @@
+first character below the last: true
+first character at the bottom of the text: true
+point at the bottom hits offset: 0
+point in the first span hits: "abc" at offset 0
+left end of line: "abc" at offset 0
+right end of line: "def" at offset 3
+left end of line of images: offset 0
+right end of line of images: offset 2
+left end of line of rtl-images: offset 2
+right end of line of rtl-images: offset 0
+left end of line of horizontal-images: offset 0, caret on the left side
+right end of line of horizontal-images: offset 2, caret on the right side
+left end of line of sideways-images: offset 0, caret on the left side
+right end of line of sideways-images: offset 2, caret on the right side
+left end of line of rtl-sideways-images: offset 2, caret on the left side
+right end of line of rtl-sideways-images: offset 0, caret on the right side
diff --git a/Tests/LibWeb/Text/input/css/sideways-lr-text-geometry-runs-upwards.html b/Tests/LibWeb/Text/input/css/sideways-lr-text-geometry-runs-upwards.html
new file mode 100644
index 00000000000..dfdae340b46
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/sideways-lr-text-geometry-runs-upwards.html
@@ -0,0 +1,81 @@
+<!DOCTYPE html>
+<style>
+ div { writing-mode: sideways-lr; font: 20px/1 serif; height: 200px; }
+ img { width: 20px; height: 20px; }
+ .sideways img { writing-mode: sideways-lr; }
+</style>
+<div id="text">abcdef</div>
+<div id="spans"><span id="first">abc</span><span id="second">def</span></div>
+<div id="editable" contenteditable>abc<span>def</span></div>
+<div id="images" contenteditable><img><img></div>
+<div id="rtl-images" dir="rtl" contenteditable><img><img></div>
+<p id="horizontal-images" contenteditable><img><img></p>
+<p id="sideways-images" class="sideways" contenteditable><img><img></p>
+<p id="rtl-sideways-images" class="sideways" dir="rtl" contenteditable><img><img></p>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ // The text of sideways-lr runs upwards from the bottom of its line, so its first character is lowest.
+ const node = text.firstChild;
+ const rect_of = (start, end) => {
+ const range = document.createRange();
+ range.setStart(node, start);
+ range.setEnd(node, end);
+ return range.getBoundingClientRect();
+ };
+ const first = rect_of(0, 1);
+ const last = rect_of(5, 6);
+ println(`first character below the last: ${first.top > last.top}`);
+ const whole = rect_of(0, 6);
+ println(`first character at the bottom of the text: ${Math.abs(first.bottom - whole.bottom) < 1}`);
+ const position = document.caretPositionFromPoint(whole.left + whole.width / 2, whole.bottom - 2);
+ println(`point at the bottom hits offset: ${position.offset}`);
+
+ // Of two spans in a line, the first one is the lower one, so a point in it hits its text.
+ const first_rect = document.getElementById("first").getBoundingClientRect();
+ const hit = document.caretPositionFromPoint(first_rect.left + first_rect.width / 2, first_rect.bottom - 2);
+ println(`point in the first span hits: "${hit.offsetNode.data}" at offset ${hit.offset}`);
+
+ // The left end of a line of sideways-lr text is its line-left side, at the bottom, where the text starts.
+ if (internals.sendTextEditingCommand) {
+ const editable = document.getElementById("editable");
+ const selection = getSelection();
+ editable.focus();
+ for (const edge of ["Left", "Right"]) {
+ selection.collapse(editable.firstChild, 1);
+ internals.sendTextEditingCommand(editable, `MoveTo${edge}EndOfLine`);
+ println(`${edge.toLowerCase()} end of line: "${selection.focusNode.data}" at offset ${selection.focusOffset}`);
+ }
+
+ // The line-left side of a box is its inline-start side when the box is ltr, and its inline-end side when
+ // it's rtl.
+ for (const container of [document.getElementById("images"), document.getElementById("rtl-images")]) {
+ container.focus();
+ for (const edge of ["Left", "Right"]) {
+ selection.collapse(container, 1);
+ internals.sendTextEditingCommand(container, `MoveTo${edge}EndOfLine`);
+ println(`${edge.toLowerCase()} end of line of ${container.id}: offset ${selection.focusOffset}`);
+ }
+ }
+
+ // In a horizontal line, the caret goes on the left or right side of the boxes, whatever their own writing
+ // mode.
+ for (const id of ["horizontal-images", "sideways-images", "rtl-sideways-images"]) {
+ const container = document.getElementById(id);
+ const boxes = [...container.children].map(child => child.getBoundingClientRect());
+ const left = Math.min(...boxes.map(box => box.left));
+ const right = Math.max(...boxes.map(box => box.right));
+ container.focus();
+ for (const edge of ["Left", "Right"]) {
+ selection.collapse(container, 1);
+ internals.sendTextEditingCommand(container, `MoveTo${edge}EndOfLine`);
+ const caret = internals.currentCaretRect();
+ const at = x => Math.abs(caret.left - x) <= 1;
+ const side = at(left) ? "left" : at(right) ? "right" : "inside";
+ const offset = selection.focusOffset;
+ println(`${edge.toLowerCase()} end of line of ${id}: offset ${offset}, caret on the ${side} side`);
+ }
+ }
+ }
+ });
+</script>