use crate::bidi::BidiText;
use crate::shaping::{
CaretStop, FlowPoint, FontAccessError, FontFeature, FontId, LineEdges, PositionError,
PositionedGlyph, ShapeError, ShapeRequest, ShapedGlyph, ShapedRun, TextRange, Typeface,
};
use crate::unicode::{graphemes, line_breaks, script_runs, LineBreakKind, LineBreaks, Script};
use core::iter::Peekable;
use crate::buffer::SliceWriter;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct LogicalRun {
text: TextRange,
typeface: u16,
script: Script,
bidi_level: u8,
}
impl LogicalRun {
pub const fn empty() -> Self {
Self {
text: TextRange::new(0, 0),
typeface: 0,
script: Script::Common,
bidi_level: 0,
}
}
pub const fn text(self) -> TextRange {
self.text
}
pub const fn typeface_index(self) -> usize {
self.typeface as usize
}
pub const fn script(self) -> Script {
self.script
}
pub const fn bidi_level(self) -> u8 {
self.bidi_level
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum LayoutError {
NoTypeface,
TooManyTypefaces,
InsufficientRunCapacity { required: usize },
InsufficientGlyphCapacity { minimum: usize },
InsufficientLineCapacity { required: usize },
InsufficientScratchCapacity { minimum: usize },
InsufficientPositionedCapacity { minimum: usize },
InsufficientCaretCapacity { minimum: usize },
InvalidRun,
InvalidTypefaceOutput { run: usize },
InvalidWidth,
InvalidLineHeight,
CoordinateOverflow,
Font(FontAccessError),
Shape { run: usize, error: ShapeError },
}
impl From<FontAccessError> for LayoutError {
fn from(error: FontAccessError) -> Self {
Self::Font(error)
}
}
pub struct LogicalRuns<'a> {
text: TextRange,
runs: &'a [LogicalRun],
}
impl<'a> LogicalRuns<'a> {
pub fn resolve(
text: &str,
bidi: &BidiText<'_>,
typefaces: &[&dyn Typeface],
output: &'a mut [LogicalRun],
) -> Result<Self, LayoutError> {
if typefaces.is_empty() {
return Err(LayoutError::NoTypeface);
}
if typefaces.len() > u16::MAX as usize {
return Err(LayoutError::TooManyTypefaces);
}
let paragraph = bidi.text();
if paragraph.end > u32::MAX as usize {
return Err(LayoutError::InvalidRun);
}
let mut writer = LogicalRunWriter::new(output);
for bidi_run in bidi.logical_runs() {
let bidi_start = bidi_run.text.start;
let bidi_end = bidi_run.text.end;
if bidi_start > bidi_end
|| bidi_end > text.len()
|| !text.is_char_boundary(bidi_start)
|| !text.is_char_boundary(bidi_end)
{
return Err(LayoutError::InvalidRun);
}
for script_run in script_runs(&text[bidi_start..bidi_end]) {
let script_start = bidi_start + script_run.text.start;
let script_end = bidi_start + script_run.text.end;
for cluster in graphemes(&text[script_start..script_end]) {
let start = script_start + cluster.range.start;
let end = script_start + cluster.range.end;
let typeface = select_typeface(cluster.text, typefaces)?;
writer.push(LogicalRun {
text: TextRange::new(start as u32, end as u32),
typeface,
script: script_run.script,
bidi_level: bidi_run.level,
});
}
}
}
let runs = writer.finish()?;
Ok(Self {
text: TextRange::new(paragraph.start as u32, paragraph.end as u32),
runs,
})
}
pub const fn text(&self) -> TextRange {
self.text
}
pub const fn runs(&self) -> &[LogicalRun] {
self.runs
}
pub fn shape_into<'output>(
&self,
text: &str,
typefaces: &[&dyn Typeface],
features: &[FontFeature],
glyphs: &'output mut [ShapedGlyph],
runs: &'output mut [GlyphRun],
) -> Result<ShapedText<'output>, LayoutError> {
if runs.len() < self.runs.len() {
return Err(LayoutError::InsufficientRunCapacity {
required: self.runs.len(),
});
}
let mut glyph_count = 0;
for (run_index, logical) in self.runs.iter().enumerate() {
let Some(typeface) = typefaces.get(logical.typeface_index()) else {
return Err(LayoutError::InvalidRun);
};
let range = logical.text.start as usize..logical.text.end as usize;
let request = ShapeRequest::new(
text,
range,
crate::bidi::Direction::from_level(logical.bidi_level),
logical.script,
)
.with_features(features);
let count = match typeface.shape_into(&request, &mut glyphs[glyph_count..]) {
Ok(count) => count,
Err(ShapeError::InsufficientCapacity { required }) => {
return Err(LayoutError::InsufficientGlyphCapacity {
minimum: glyph_count.saturating_add(required),
});
}
Err(error) => {
return Err(LayoutError::Shape {
run: run_index,
error,
});
}
};
let end = glyph_count
.checked_add(count)
.ok_or(LayoutError::InvalidRun)?;
if end > glyphs.len()
|| end > u32::MAX as usize
|| !valid_shaped_output(&request, &glyphs[glyph_count..end])
{
return Err(LayoutError::InvalidTypefaceOutput { run: run_index });
}
runs[run_index] = GlyphRun {
text: logical.text,
glyphs: TextRange::new(glyph_count as u32, end as u32),
font_id: typeface.id(),
bidi_level: logical.bidi_level,
};
glyph_count = end;
}
Ok(ShapedText {
text: self.text,
glyphs: &glyphs[..glyph_count],
runs: &runs[..self.runs.len()],
})
}
pub fn layout_into<'output>(
&self,
text: &str,
typefaces: &[&dyn Typeface],
features: &[FontFeature],
broken: &BrokenLines<'_>,
options: LayoutOptions,
buffers: LayoutBuffers<'output>,
) -> Result<ParagraphLayout<'output>, LayoutError> {
ParagraphBuilder::new(options, buffers).layout(
LayoutInput {
text,
typefaces,
features,
logical: self.runs,
},
broken,
)
}
}
#[derive(Clone, Copy)]
struct LayoutInput<'a, 'face> {
text: &'a str,
typefaces: &'a [&'face dyn Typeface],
features: &'a [FontFeature],
logical: &'a [LogicalRun],
}
struct ParagraphBuilder<'a> {
options: LayoutOptions,
buffers: LayoutBuffers<'a>,
run_count: usize,
glyph_count: usize,
caret_count: usize,
}
impl<'output> ParagraphBuilder<'output> {
fn new(options: LayoutOptions, buffers: LayoutBuffers<'output>) -> Self {
Self {
options,
buffers,
run_count: 0,
glyph_count: 0,
caret_count: 0,
}
}
fn layout(
mut self,
input: LayoutInput<'_, '_>,
broken: &BrokenLines<'_>,
) -> Result<ParagraphLayout<'output>, LayoutError> {
self.preflight(input.logical, broken)?;
for (line_index, broken_line) in broken.lines.iter().enumerate() {
self.push_line(input, line_index, *broken_line)?;
}
Ok(self.finish(broken.lines.len()))
}
fn preflight(
&self,
logical: &[LogicalRun],
broken: &BrokenLines<'_>,
) -> Result<(), LayoutError> {
if self.options.line_height < 0 {
return Err(LayoutError::InvalidLineHeight);
}
if self.buffers.lines.len() < broken.lines.len() {
return Err(LayoutError::InsufficientLineCapacity {
required: broken.lines.len(),
});
}
let required_runs = broken.lines.iter().try_fold(0usize, |count, line| {
let line_runs = logical
.iter()
.filter(|run| intersection(run.text, line.text).is_some())
.count();
count
.checked_add(line_runs)
.ok_or(LayoutError::CoordinateOverflow)
})?;
if required_runs > u32::MAX as usize || broken.lines.len() > u32::MAX as usize {
return Err(LayoutError::CoordinateOverflow);
}
if self.buffers.runs.len() < required_runs {
return Err(LayoutError::InsufficientRunCapacity {
required: required_runs,
});
}
Ok(())
}
fn push_line(
&mut self,
input: LayoutInput<'_, '_>,
line_index: usize,
broken: BrokenLine,
) -> Result<(), LayoutError> {
let line_run_start = self.run_count;
self.append_visual_runs(input.logical, input.typefaces, broken)?;
reorder_visual_runs(&mut self.buffers.runs[line_run_start..self.run_count]);
let origin = self.line_origin(line_index)?;
let line_glyph_start = self.glyph_count;
let line_caret_start = self.caret_count;
let mut pen = origin;
for run_index in line_run_start..self.run_count {
pen = self.shape_run(input, run_index, broken, pen)?;
}
let advance = pen
.x
.checked_sub(origin.x)
.ok_or(LayoutError::CoordinateOverflow)?;
self.buffers.lines[line_index] = LayoutLine {
text: broken.text,
runs: TextRange::new(line_run_start as u32, self.run_count as u32),
glyphs: TextRange::new(line_glyph_start as u32, self.glyph_count as u32),
carets: TextRange::new(line_caret_start as u32, self.caret_count as u32),
origin,
advance,
};
Ok(())
}
fn append_visual_runs(
&mut self,
logical: &[LogicalRun],
typefaces: &[&dyn Typeface],
broken: BrokenLine,
) -> Result<(), LayoutError> {
for logical in logical {
let Some(text) = intersection(logical.text, broken.text) else {
continue;
};
let Some(typeface) = typefaces.get(logical.typeface_index()) else {
return Err(LayoutError::InvalidRun);
};
self.buffers.runs[self.run_count] = VisualRun {
text,
glyphs: TextRange::new(0, 0),
font_id: typeface.id(),
typeface_index: logical.typeface,
script: logical.script,
bidi_level: logical.bidi_level,
};
self.run_count += 1;
}
Ok(())
}
fn line_origin(&self, line_index: usize) -> Result<FlowPoint, LayoutError> {
let line = i32::try_from(line_index).map_err(|_| LayoutError::CoordinateOverflow)?;
let y = self
.options
.line_height
.checked_mul(line)
.and_then(|offset| self.options.origin.y.checked_add(offset))
.ok_or(LayoutError::CoordinateOverflow)?;
Ok(FlowPoint {
x: self.options.origin.x,
y,
})
}
fn shape_run(
&mut self,
input: LayoutInput<'_, '_>,
run_index: usize,
line: BrokenLine,
origin: FlowPoint,
) -> Result<FlowPoint, LayoutError> {
let mut visual = self.buffers.runs[run_index];
let Some(typeface) = input.typefaces.get(visual.typeface_index as usize) else {
return Err(LayoutError::InvalidRun);
};
let request = ShapeRequest::new(
input.text,
visual.text.start as usize..visual.text.end as usize,
crate::bidi::Direction::from_level(visual.bidi_level),
visual.script,
)
.with_features(input.features)
.with_line_edges(line_edges(visual.text, line.text));
let count = match typeface.shape_into(&request, self.buffers.scratch) {
Ok(count) => count,
Err(ShapeError::InsufficientCapacity { required }) => {
return Err(LayoutError::InsufficientScratchCapacity { minimum: required });
}
Err(error) => {
return Err(LayoutError::Shape {
run: run_index,
error,
});
}
};
if count > self.buffers.scratch.len()
|| !valid_shaped_output(&request, &self.buffers.scratch[..count])
{
return Err(LayoutError::InvalidTypefaceOutput { run: run_index });
}
let glyph_end = self
.glyph_count
.checked_add(count)
.ok_or(LayoutError::CoordinateOverflow)?;
if glyph_end > u32::MAX as usize {
return Err(LayoutError::CoordinateOverflow);
}
if glyph_end > self.buffers.glyphs.len() {
return Err(LayoutError::InsufficientPositionedCapacity { minimum: glyph_end });
}
let shaped = ShapedRun::new(
visual.font_id,
visual.text,
visual.bidi_level,
&self.buffers.scratch[..count],
);
let positioned = shaped
.position_into(
origin,
&mut self.buffers.glyphs[self.glyph_count..glyph_end],
)
.map_err(|error| position_error(error, self.glyph_count))?;
let end = positioned.end();
let carets = positioned
.carets_into(&mut self.buffers.carets[self.caret_count..])
.map_err(|error| caret_error(error, self.caret_count))?;
self.caret_count = self
.caret_count
.checked_add(carets.len())
.ok_or(LayoutError::CoordinateOverflow)?;
if self.caret_count > u32::MAX as usize {
return Err(LayoutError::CoordinateOverflow);
}
visual.glyphs = TextRange::new(self.glyph_count as u32, glyph_end as u32);
self.buffers.runs[run_index] = visual;
self.glyph_count = glyph_end;
Ok(end)
}
fn finish(self, line_count: usize) -> ParagraphLayout<'output> {
ParagraphLayout {
lines: &self.buffers.lines[..line_count],
runs: &self.buffers.runs[..self.run_count],
glyphs: &self.buffers.glyphs[..self.glyph_count],
carets: &self.buffers.carets[..self.caret_count],
}
}
}
fn line_edges(run: TextRange, line: TextRange) -> LineEdges {
match (run.start == line.start, run.end == line.end) {
(true, true) => LineEdges::BOTH,
(true, false) => LineEdges::START,
(false, true) => LineEdges::END,
(false, false) => LineEdges::NONE,
}
}
struct LogicalRunWriter<'a> {
output: SliceWriter<'a, LogicalRun>,
last: Option<LogicalRun>,
}
impl<'a> LogicalRunWriter<'a> {
fn new(output: &'a mut [LogicalRun]) -> Self {
Self {
output: SliceWriter::new(output),
last: None,
}
}
fn push(&mut self, run: LogicalRun) {
if let Some(previous) = self.last.as_mut() {
if previous.text.end == run.text.start
&& previous.typeface == run.typeface
&& previous.script == run.script
&& previous.bidi_level == run.bidi_level
{
previous.text.end = run.text.end;
if let Some(stored) = self.output.last_mut() {
stored.text.end = run.text.end;
}
return;
}
}
self.output.push(run);
self.last = Some(run);
}
fn finish(self) -> Result<&'a [LogicalRun], LayoutError> {
self.output
.finish()
.map_err(|required| LayoutError::InsufficientRunCapacity { required })
}
}
fn select_typeface(grapheme: &str, typefaces: &[&dyn Typeface]) -> Result<u16, FontAccessError> {
for (index, typeface) in typefaces.iter().enumerate() {
if typeface.covers(grapheme)? {
return Ok(index as u16);
}
}
Ok(0)
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GlyphRun {
text: TextRange,
glyphs: TextRange,
font_id: FontId,
bidi_level: u8,
}
impl GlyphRun {
pub const fn empty() -> Self {
Self {
text: TextRange::new(0, 0),
glyphs: TextRange::new(0, 0),
font_id: FontId::new(0),
bidi_level: 0,
}
}
pub const fn text(self) -> TextRange {
self.text
}
pub const fn glyphs(self) -> TextRange {
self.glyphs
}
pub const fn font_id(self) -> FontId {
self.font_id
}
pub const fn bidi_level(self) -> u8 {
self.bidi_level
}
}
pub struct ShapedText<'a> {
text: TextRange,
glyphs: &'a [ShapedGlyph],
runs: &'a [GlyphRun],
}
impl ShapedText<'_> {
pub const fn text(&self) -> TextRange {
self.text
}
pub const fn glyphs(&self) -> &[ShapedGlyph] {
self.glyphs
}
pub const fn runs(&self) -> &[GlyphRun] {
self.runs
}
pub fn run(&self, index: usize) -> Option<ShapedRun<'_>> {
let run = *self.runs.get(index)?;
let glyphs = self
.glyphs
.get(run.glyphs.start as usize..run.glyphs.end as usize)?;
Some(ShapedRun::new(
run.font_id,
run.text,
run.bidi_level,
glyphs,
))
}
pub fn is_safe_break(&self, offset: u32) -> bool {
if offset == self.text.start || offset == self.text.end {
return true;
}
if offset < self.text.start || offset > self.text.end {
return false;
}
!self.glyphs.iter().any(|glyph| {
glyph.cluster.start < offset && offset < glyph.cluster.end
|| glyph.cluster.start == offset && glyph.unsafe_to_break()
})
}
pub fn break_into<'output>(
&self,
text: &str,
max_width: i32,
output: &'output mut [BrokenLine],
) -> Result<BrokenLines<'output>, LayoutError> {
if max_width < 0 {
return Err(LayoutError::InvalidWidth);
}
let start = self.text.start as usize;
let end = self.text.end as usize;
if start > end
|| end > text.len()
|| !text.is_char_boundary(start)
|| !text.is_char_boundary(end)
{
return Err(LayoutError::InvalidRun);
}
let breaks = line_breaks(&text[start..end]).peekable();
let mut breaker = LineBreaker::new(self, self.text, max_width, breaks, output);
let mut glyphs = LogicalGlyphs::new(self).peekable();
while let Some(first) = glyphs.next() {
let cluster = first.cluster;
let mut advance = first.advance.x;
while let Some(next) = glyphs.peek() {
if next.cluster != cluster {
break;
}
advance = advance
.checked_add(next.advance.x)
.ok_or(LayoutError::CoordinateOverflow)?;
glyphs.next();
}
breaker.add_cluster(cluster, advance)?;
}
breaker.finish()
}
}
struct LogicalGlyphs<'a> {
shaped: &'a ShapedText<'a>,
run: usize,
glyph: usize,
}
impl<'a> LogicalGlyphs<'a> {
fn new(shaped: &'a ShapedText<'a>) -> Self {
Self {
shaped,
run: 0,
glyph: 0,
}
}
}
impl<'a> Iterator for LogicalGlyphs<'a> {
type Item = &'a ShapedGlyph;
fn next(&mut self) -> Option<Self::Item> {
loop {
let run = self.shaped.runs.get(self.run)?;
let start = run.glyphs.start as usize;
let end = run.glyphs.end as usize;
if self.glyph < end.saturating_sub(start) {
let index = if run.bidi_level & 1 == 0 {
start + self.glyph
} else {
end - self.glyph - 1
};
self.glyph += 1;
return self.shaped.glyphs.get(index);
}
self.run += 1;
self.glyph = 0;
}
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct BrokenLine {
text: TextRange,
advance: i32,
}
impl BrokenLine {
pub const fn empty() -> Self {
Self {
text: TextRange::new(0, 0),
advance: 0,
}
}
pub const fn text(self) -> TextRange {
self.text
}
pub const fn advance(self) -> i32 {
self.advance
}
}
pub struct BrokenLines<'a> {
lines: &'a [BrokenLine],
}
impl BrokenLines<'_> {
pub const fn lines(&self) -> &[BrokenLine] {
self.lines
}
}
struct LineBreaker<'shaped, 'glyphs, 'text, 'output> {
shaped: &'shaped ShapedText<'glyphs>,
paragraph: TextRange,
max_width: i32,
breaks: Peekable<LineBreaks<'text>>,
writer: LineWriter<'output>,
line_start: u32,
width: i32,
last_allowed: Option<(u32, i32)>,
}
impl<'shaped, 'glyphs, 'text, 'output> LineBreaker<'shaped, 'glyphs, 'text, 'output> {
fn new(
shaped: &'shaped ShapedText<'glyphs>,
paragraph: TextRange,
max_width: i32,
breaks: Peekable<LineBreaks<'text>>,
output: &'output mut [BrokenLine],
) -> Self {
Self {
shaped,
paragraph,
max_width,
breaks,
writer: LineWriter::new(output),
line_start: paragraph.start,
width: 0,
last_allowed: None,
}
}
fn add_cluster(&mut self, cluster: TextRange, advance: i32) -> Result<(), LayoutError> {
self.take_breaks_through(cluster.start)?;
let width_before = self.width;
self.width = self
.width
.checked_add(advance)
.ok_or(LayoutError::CoordinateOverflow)?;
if self.width > self.max_width {
if let Some((offset, width)) = self.last_allowed.take() {
self.emit(offset, width)?;
} else if width_before > 0
&& cluster.start > self.line_start
&& self.shaped.is_safe_break(cluster.start)
{
self.emit(cluster.start, width_before)?;
} else if self.line_start == cluster.start && self.shaped.is_safe_break(cluster.end) {
self.emit(cluster.end, self.width)?;
}
}
self.take_breaks_through(cluster.end)
}
fn take_breaks_through(&mut self, offset: u32) -> Result<(), LayoutError> {
while self
.breaks
.peek()
.is_some_and(|next| self.paragraph.start as usize + next.offset <= offset as usize)
{
let next = self.breaks.next().unwrap();
let global = self.paragraph.start + next.offset as u32;
if global <= self.line_start || !self.shaped.is_safe_break(global) {
continue;
}
match next.kind {
LineBreakKind::Mandatory => self.emit(global, self.width)?,
LineBreakKind::Allowed if self.width > self.max_width => {
self.emit(global, self.width)?;
}
LineBreakKind::Allowed => self.last_allowed = Some((global, self.width)),
}
}
Ok(())
}
fn emit(&mut self, end: u32, advance: i32) -> Result<(), LayoutError> {
if end <= self.line_start {
return Ok(());
}
self.writer.push(BrokenLine {
text: TextRange::new(self.line_start, end),
advance,
});
self.line_start = end;
self.width = self
.width
.checked_sub(advance)
.ok_or(LayoutError::CoordinateOverflow)?;
self.last_allowed = None;
Ok(())
}
fn finish(mut self) -> Result<BrokenLines<'output>, LayoutError> {
self.take_breaks_through(self.paragraph.end)?;
if self.line_start < self.paragraph.end {
self.emit(self.paragraph.end, self.width)?;
}
let lines = self.writer.finish()?;
Ok(BrokenLines { lines })
}
}
struct LineWriter<'a> {
output: SliceWriter<'a, BrokenLine>,
}
impl<'a> LineWriter<'a> {
fn new(output: &'a mut [BrokenLine]) -> Self {
Self {
output: SliceWriter::new(output),
}
}
fn push(&mut self, line: BrokenLine) {
self.output.push(line);
}
fn finish(self) -> Result<&'a [BrokenLine], LayoutError> {
self.output
.finish()
.map_err(|required| LayoutError::InsufficientLineCapacity { required })
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct LayoutOptions {
pub origin: FlowPoint,
pub line_height: i32,
}
impl LayoutOptions {
pub const fn new(line_height: i32) -> Self {
Self {
origin: FlowPoint { x: 0, y: 0 },
line_height,
}
}
pub const fn with_origin(mut self, origin: FlowPoint) -> Self {
self.origin = origin;
self
}
}
pub struct LayoutBuffers<'a> {
pub scratch: &'a mut [ShapedGlyph],
pub glyphs: &'a mut [PositionedGlyph],
pub runs: &'a mut [VisualRun],
pub lines: &'a mut [LayoutLine],
pub carets: &'a mut [CaretStop],
}
impl<'a> LayoutBuffers<'a> {
pub fn new(
scratch: &'a mut [ShapedGlyph],
glyphs: &'a mut [PositionedGlyph],
runs: &'a mut [VisualRun],
lines: &'a mut [LayoutLine],
carets: &'a mut [CaretStop],
) -> Self {
Self {
scratch,
glyphs,
runs,
lines,
carets,
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct VisualRun {
text: TextRange,
glyphs: TextRange,
font_id: FontId,
typeface_index: u16,
script: Script,
bidi_level: u8,
}
impl VisualRun {
pub const fn empty() -> Self {
Self {
text: TextRange::new(0, 0),
glyphs: TextRange::new(0, 0),
font_id: FontId::new(0),
typeface_index: 0,
script: Script::Common,
bidi_level: 0,
}
}
pub const fn text(self) -> TextRange {
self.text
}
pub const fn glyphs(self) -> TextRange {
self.glyphs
}
pub const fn font_id(self) -> FontId {
self.font_id
}
pub const fn bidi_level(self) -> u8 {
self.bidi_level
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct LayoutLine {
text: TextRange,
runs: TextRange,
glyphs: TextRange,
carets: TextRange,
origin: FlowPoint,
advance: i32,
}
impl LayoutLine {
pub const fn empty() -> Self {
Self {
text: TextRange::new(0, 0),
runs: TextRange::new(0, 0),
glyphs: TextRange::new(0, 0),
carets: TextRange::new(0, 0),
origin: FlowPoint { x: 0, y: 0 },
advance: 0,
}
}
pub const fn text(self) -> TextRange {
self.text
}
pub const fn runs(self) -> TextRange {
self.runs
}
pub const fn glyphs(self) -> TextRange {
self.glyphs
}
pub const fn carets(self) -> TextRange {
self.carets
}
pub const fn origin(self) -> FlowPoint {
self.origin
}
pub const fn advance(self) -> i32 {
self.advance
}
}
pub struct ParagraphLayout<'a> {
lines: &'a [LayoutLine],
runs: &'a [VisualRun],
glyphs: &'a [PositionedGlyph],
carets: &'a [CaretStop],
}
impl ParagraphLayout<'_> {
pub const fn lines(&self) -> &[LayoutLine] {
self.lines
}
pub const fn runs(&self) -> &[VisualRun] {
self.runs
}
pub const fn glyphs(&self) -> &[PositionedGlyph] {
self.glyphs
}
pub const fn carets(&self) -> &[CaretStop] {
self.carets
}
}
fn intersection(left: TextRange, right: TextRange) -> Option<TextRange> {
let start = left.start.max(right.start);
let end = left.end.min(right.end);
(start < end).then_some(TextRange::new(start, end))
}
fn valid_shaped_output(request: &ShapeRequest<'_>, glyphs: &[ShapedGlyph]) -> bool {
let mut previous = None;
glyphs.iter().all(|glyph| {
let start = glyph.cluster.start as usize;
let end = glyph.cluster.end as usize;
let ordered = match (request.direction, previous) {
(_, None) => true,
(crate::bidi::Direction::LeftToRight, Some(previous)) => {
glyph.cluster.start >= previous
}
(crate::bidi::Direction::RightToLeft, Some(previous)) => {
glyph.cluster.start <= previous
}
};
previous = Some(glyph.cluster.start);
start < end
&& start >= request.range.start
&& end <= request.range.end
&& request.text.is_char_boundary(start)
&& request.text.is_char_boundary(end)
&& ordered
})
}
fn reorder_visual_runs(runs: &mut [VisualRun]) {
let max_level = runs.iter().map(|run| run.bidi_level).max().unwrap_or(0);
let min_odd = runs
.iter()
.filter(|run| run.bidi_level & 1 == 1)
.map(|run| run.bidi_level)
.min();
let Some(min_odd) = min_odd else {
return;
};
for level in (min_odd..=max_level).rev() {
let mut start = 0;
while start < runs.len() {
if runs[start].bidi_level < level {
start += 1;
continue;
}
let mut end = start + 1;
while end < runs.len() && runs[end].bidi_level >= level {
end += 1;
}
runs[start..end].reverse();
start = end;
}
}
}
fn position_error(error: PositionError, current: usize) -> LayoutError {
match error {
PositionError::InsufficientCapacity { required } => {
LayoutError::InsufficientPositionedCapacity {
minimum: current.saturating_add(required),
}
}
PositionError::CoordinateOverflow => LayoutError::CoordinateOverflow,
}
}
fn caret_error(error: PositionError, current: usize) -> LayoutError {
match error {
PositionError::InsufficientCapacity { required } => {
LayoutError::InsufficientCaretCapacity {
minimum: current.saturating_add(required),
}
}
PositionError::CoordinateOverflow => LayoutError::CoordinateOverflow,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::bidi::{BaseDirection, BidiRun};
use crate::shaping::{FlowPoint, FontId, FontMetrics, GlyphId, GlyphSource, SimpleTypeface};
use std::prelude::v1::*;
struct Source {
key: u64,
ascii: bool,
}
impl GlyphSource for Source {
fn id(&self) -> FontId {
FontId::new(self.key)
}
fn metrics(&self) -> Result<FontMetrics, FontAccessError> {
Ok(FontMetrics::default())
}
fn glyph_for(&self, character: char) -> Result<Option<GlyphId>, FontAccessError> {
let covered = character.is_ascii() == self.ascii;
Ok(covered.then_some(GlyphId::new(character as u16)))
}
fn glyph_advance(&self, _glyph: GlyphId) -> Result<FlowPoint, FontAccessError> {
Ok(FlowPoint { x: 1, y: 0 })
}
}
struct EdgeTypeface;
impl Typeface for EdgeTypeface {
fn id(&self) -> FontId {
FontId::new(9)
}
fn metrics(&self) -> Result<FontMetrics, FontAccessError> {
Ok(FontMetrics::default())
}
fn covers(&self, _grapheme: &str) -> Result<bool, FontAccessError> {
Ok(true)
}
fn shape_into(
&self,
request: &ShapeRequest<'_>,
output: &mut [ShapedGlyph],
) -> Result<usize, ShapeError> {
let shaped = &request.text[request.range.clone()];
let required = shaped.chars().count();
if output.len() < required {
return Err(ShapeError::InsufficientCapacity { required });
}
let advance = if request.line_edges.has_start() || request.line_edges.has_end() {
2
} else {
1
};
for (index, (offset, character)) in shaped.char_indices().enumerate() {
let start = request.range.start + offset;
let end = start + character.len_utf8();
output[index] = ShapedGlyph::new(
GlyphId::new(character as u16),
TextRange::new(start as u32, end as u32),
);
output[index].advance.x = advance;
}
Ok(required)
}
}
struct InvalidTypeface;
impl Typeface for InvalidTypeface {
fn id(&self) -> FontId {
FontId::new(10)
}
fn metrics(&self) -> Result<FontMetrics, FontAccessError> {
Ok(FontMetrics::default())
}
fn covers(&self, _grapheme: &str) -> Result<bool, FontAccessError> {
Ok(true)
}
fn shape_into(
&self,
_request: &ShapeRequest<'_>,
output: &mut [ShapedGlyph],
) -> Result<usize, ShapeError> {
Ok(output.len() + 1)
}
}
fn bidi_slots<const N: usize>() -> [BidiRun; N] {
core::array::from_fn(|_| BidiRun::empty())
}
#[test]
fn splits_runs_by_script_and_fallback_face() {
let text = "ab世界cd";
let primary = SimpleTypeface::new(&Source {
key: 1,
ascii: true,
});
let fallback = SimpleTypeface::new(&Source {
key: 2,
ascii: false,
});
let typefaces: [&dyn Typeface; 2] = [&primary, &fallback];
let mut bidi_output = bidi_slots::<4>();
let bidi =
BidiText::resolve(text, 0..text.len(), BaseDirection::Auto, &mut bidi_output).unwrap();
let mut output = [LogicalRun::empty(); 4];
let runs = LogicalRuns::resolve(text, &bidi, &typefaces, &mut output).unwrap();
assert_eq!(runs.runs().len(), 3);
assert_eq!(runs.runs()[0].typeface_index(), 0);
assert_eq!(runs.runs()[1].typeface_index(), 1);
assert_eq!(runs.runs()[1].script(), Script::Han);
assert_eq!(runs.runs()[2].typeface_index(), 0);
}
#[test]
fn shapes_resolved_runs_into_caller_storage() {
let text = "ab世界";
let primary = SimpleTypeface::new(&Source {
key: 1,
ascii: true,
});
let fallback = SimpleTypeface::new(&Source {
key: 2,
ascii: false,
});
let typefaces: [&dyn Typeface; 2] = [&primary, &fallback];
let mut bidi_output = bidi_slots::<4>();
let bidi =
BidiText::resolve(text, 0..text.len(), BaseDirection::Auto, &mut bidi_output).unwrap();
let mut logical_output = [LogicalRun::empty(); 4];
let logical = LogicalRuns::resolve(text, &bidi, &typefaces, &mut logical_output).unwrap();
let mut glyph_output = [ShapedGlyph::default(); 4];
let mut run_output = [GlyphRun::empty(); 4];
let shaped = logical
.shape_into(text, &typefaces, &[], &mut glyph_output, &mut run_output)
.unwrap();
assert_eq!(shaped.glyphs().len(), 4);
assert_eq!(shaped.runs().len(), 2);
assert_eq!(shaped.run(0).unwrap().font_id(), FontId::new(1));
assert_eq!(shaped.run(1).unwrap().font_id(), FontId::new(2));
}
#[test]
fn reports_exact_run_and_minimum_glyph_capacity() {
let text = "abc";
let primary = SimpleTypeface::new(&Source {
key: 1,
ascii: true,
});
let typefaces: [&dyn Typeface; 1] = [&primary];
let mut bidi_output = bidi_slots::<1>();
let bidi =
BidiText::resolve(text, 0..text.len(), BaseDirection::Auto, &mut bidi_output).unwrap();
let mut logical_output = [LogicalRun::empty(); 1];
let logical = LogicalRuns::resolve(text, &bidi, &typefaces, &mut logical_output).unwrap();
let mut glyph_output = [ShapedGlyph::default(); 2];
let mut run_output = [GlyphRun::empty(); 1];
assert_eq!(
logical
.shape_into(text, &typefaces, &[], &mut glyph_output, &mut run_output,)
.err()
.unwrap(),
LayoutError::InsufficientGlyphCapacity { minimum: 3 }
);
}
#[test]
fn run_capacity_is_exact_when_no_slots_are_available() {
let text = "abc";
let primary = SimpleTypeface::new(&Source {
key: 1,
ascii: true,
});
let typefaces: [&dyn Typeface; 1] = [&primary];
let mut bidi_output = bidi_slots::<1>();
let bidi =
BidiText::resolve(text, 0..text.len(), BaseDirection::Auto, &mut bidi_output).unwrap();
let mut output = [];
assert_eq!(
LogicalRuns::resolve(text, &bidi, &typefaces, &mut output)
.err()
.unwrap(),
LayoutError::InsufficientRunCapacity { required: 1 }
);
}
#[test]
fn breaks_at_the_last_allowed_width() {
let text = "ab cd";
let primary = SimpleTypeface::new(&Source {
key: 1,
ascii: true,
});
let typefaces: [&dyn Typeface; 1] = [&primary];
let mut bidi_output = bidi_slots::<1>();
let bidi =
BidiText::resolve(text, 0..text.len(), BaseDirection::Auto, &mut bidi_output).unwrap();
let mut logical_output = [LogicalRun::empty(); 1];
let logical = LogicalRuns::resolve(text, &bidi, &typefaces, &mut logical_output).unwrap();
let mut glyph_output = [ShapedGlyph::default(); 5];
let mut run_output = [GlyphRun::empty(); 1];
let shaped = logical
.shape_into(text, &typefaces, &[], &mut glyph_output, &mut run_output)
.unwrap();
let mut line_output = [BrokenLine::empty(); 2];
let lines = shaped.break_into(text, 3, &mut line_output).unwrap();
assert_eq!(lines.lines().len(), 2);
assert_eq!(lines.lines()[0].text(), TextRange::new(0, 3));
assert_eq!(lines.lines()[0].advance(), 3);
assert_eq!(lines.lines()[1].text(), TextRange::new(3, 5));
assert_eq!(lines.lines()[1].advance(), 2);
let mut no_lines = [];
assert_eq!(
shaped.break_into(text, 3, &mut no_lines).err().unwrap(),
LayoutError::InsufficientLineCapacity { required: 2 }
);
}
#[test]
fn lets_unsafe_sequences_exceed_the_width() {
let text = "ab cd";
let mut glyphs = [ShapedGlyph::default(); 5];
for (index, glyph) in glyphs.iter_mut().enumerate() {
*glyph = ShapedGlyph::new(
GlyphId::new(index as u16),
TextRange::new(index as u32, index as u32 + 1),
);
glyph.advance.x = 1;
}
glyphs[3].set_unsafe_to_break(true);
let runs = [GlyphRun {
text: TextRange::new(0, 5),
glyphs: TextRange::new(0, 5),
font_id: FontId::new(1),
bidi_level: 0,
}];
let shaped = ShapedText {
text: TextRange::new(0, 5),
glyphs: &glyphs,
runs: &runs,
};
let mut line_output = [BrokenLine::empty(); 2];
let lines = shaped.break_into(text, 3, &mut line_output).unwrap();
assert_eq!(lines.lines()[0].text(), TextRange::new(0, 4));
assert_eq!(lines.lines()[0].advance(), 4);
assert_eq!(lines.lines()[1].text(), TextRange::new(4, 5));
}
#[test]
fn reshapes_line_edges_into_visual_outputs() {
let text = "ab cd";
let typefaces: [&dyn Typeface; 1] = [&EdgeTypeface];
let mut bidi_output = bidi_slots::<1>();
let bidi =
BidiText::resolve(text, 0..text.len(), BaseDirection::Auto, &mut bidi_output).unwrap();
let mut logical_output = [LogicalRun::empty(); 1];
let logical = LogicalRuns::resolve(text, &bidi, &typefaces, &mut logical_output).unwrap();
let mut initial_glyphs = [ShapedGlyph::default(); 5];
let mut initial_runs = [GlyphRun::empty(); 1];
let shaped = logical
.shape_into(
text,
&typefaces,
&[],
&mut initial_glyphs,
&mut initial_runs,
)
.unwrap();
let mut broken_output = [BrokenLine::empty(); 2];
let broken = shaped.break_into(text, 3, &mut broken_output).unwrap();
let mut scratch = [ShapedGlyph::default(); 3];
let mut glyphs = [PositionedGlyph::default(); 5];
let mut runs = [VisualRun::empty(); 2];
let mut lines = [LayoutLine::empty(); 2];
let mut carets = [CaretStop::default(); 7];
let layout = logical
.layout_into(
text,
&typefaces,
&[],
&broken,
LayoutOptions::new(10).with_origin(FlowPoint { x: 5, y: 7 }),
LayoutBuffers::new(
&mut scratch,
&mut glyphs,
&mut runs,
&mut lines,
&mut carets,
),
)
.unwrap();
assert_eq!(layout.lines().len(), 2);
assert_eq!(layout.lines()[0].advance(), 6);
assert_eq!(layout.lines()[1].advance(), 4);
assert_eq!(layout.lines()[0].origin(), FlowPoint { x: 5, y: 7 });
assert_eq!(layout.lines()[1].origin(), FlowPoint { x: 5, y: 17 });
assert_eq!(layout.runs().len(), 2);
assert_eq!(layout.runs()[0].font_id(), FontId::new(9));
assert_eq!(layout.glyphs().len(), 5);
assert_eq!(layout.carets().len(), 7);
}
#[test]
fn rejects_invalid_typeface_output_counts() {
let text = "a";
let typefaces: [&dyn Typeface; 1] = [&InvalidTypeface];
let mut bidi_output = bidi_slots::<1>();
let bidi =
BidiText::resolve(text, 0..text.len(), BaseDirection::Auto, &mut bidi_output).unwrap();
let mut logical_output = [LogicalRun::empty(); 1];
let logical = LogicalRuns::resolve(text, &bidi, &typefaces, &mut logical_output).unwrap();
let mut glyphs = [ShapedGlyph::default(); 1];
let mut runs = [GlyphRun::empty(); 1];
assert_eq!(
logical
.shape_into(text, &typefaces, &[], &mut glyphs, &mut runs)
.err()
.unwrap(),
LayoutError::InvalidTypefaceOutput { run: 0 }
);
}
#[test]
fn reorders_mixed_runs_for_rtl_lines() {
let text = "אבג abc";
let primary = SimpleTypeface::new(&Source {
key: 1,
ascii: false,
});
let fallback = SimpleTypeface::new(&Source {
key: 2,
ascii: true,
});
let typefaces: [&dyn Typeface; 2] = [&primary, &fallback];
let mut bidi_output = bidi_slots::<4>();
let bidi = BidiText::resolve(
text,
0..text.len(),
BaseDirection::RightToLeft,
&mut bidi_output,
)
.unwrap();
let mut logical_output = [LogicalRun::empty(); 4];
let logical = LogicalRuns::resolve(text, &bidi, &typefaces, &mut logical_output).unwrap();
let mut initial_glyphs = [ShapedGlyph::default(); 7];
let mut initial_runs = [GlyphRun::empty(); 4];
let shaped = logical
.shape_into(
text,
&typefaces,
&[],
&mut initial_glyphs,
&mut initial_runs,
)
.unwrap();
let mut broken_output = [BrokenLine::empty(); 1];
let broken = shaped.break_into(text, 100, &mut broken_output).unwrap();
let mut scratch = [ShapedGlyph::default(); 3];
let mut glyphs = [PositionedGlyph::default(); 7];
let mut runs = [VisualRun::empty(); 4];
let mut lines = [LayoutLine::empty(); 1];
let mut carets = [CaretStop::default(); 12];
let layout = logical
.layout_into(
text,
&typefaces,
&[],
&broken,
LayoutOptions::new(10),
LayoutBuffers::new(
&mut scratch,
&mut glyphs,
&mut runs,
&mut lines,
&mut carets,
),
)
.unwrap();
let first = layout.runs()[0].text();
assert_eq!(&text[first.start as usize..first.end as usize], "abc");
assert_eq!(layout.runs()[0].bidi_level(), 2);
}
}