Skip to content

Commit

Permalink
Fix fitted column text (software-mansion#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechBarczynski committed Oct 11, 2023
1 parent ad4b85b commit c5a47e8
Showing 1 changed file with 33 additions and 67 deletions.
100 changes: 33 additions & 67 deletions compositor_render/src/transformations/text_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl TextRendererNode {
let text_dimensions = text_spec.dimensions;
let (buffer, resolution) =
Self::layout_text(text_renderer_ctx, text_spec.into(), text_dimensions);

Self {
buffer,
resolution,
Expand Down Expand Up @@ -178,7 +179,6 @@ impl TextRendererNode {
text_resolution: TextDimensions,
) -> (Buffer, Resolution) {
let font_system = &mut text_renderer_ctx.font_system.lock().unwrap();

let mut buffer = Buffer::new(
font_system,
Metrics::new(text_params.font_size, text_params.line_height),
Expand All @@ -190,51 +190,48 @@ impl TextRendererNode {
text_params.attributes.as_attrs(),
Shaping::Advanced,
);

buffer.set_wrap(font_system, text_params.wrap);

match text_resolution {
TextDimensions::Fixed { width, height } => {
buffer.set_size(font_system, width as f32, height as f32);

for line in &mut buffer.lines {
line.set_align(Some(text_params.align));
}

buffer.shape_until_scroll(font_system);
(
buffer,
Resolution {
width: width as usize,
height: height as usize,
},
)
}
let texture_size = match text_resolution {
TextDimensions::Fixed { width, height } => Resolution {
width: width as usize,
height: height as usize,
},
TextDimensions::Fitted {
max_width,
max_height,
} => Self::layout_fitted(
buffer,
font_system,
Resolution {
width: max_width as usize,
height: max_height as usize,
},
&text_params,
),
TextDimensions::FittedColumn { width, max_height } => Self::layout_fitted(
buffer,
font_system,
} => {
buffer.set_size(font_system, max_width as f32, max_height as f32);
buffer.shape_until_scroll(font_system);
Self::get_text_resolution(buffer.lines.iter(), text_params.line_height)
}
TextDimensions::FittedColumn { width, max_height } => {
buffer.set_size(font_system, width as f32, max_height as f32);
buffer.shape_until_scroll(font_system);
let text_size =
Self::get_text_resolution(buffer.lines.iter(), text_params.line_height);

Resolution {
width: width as usize,
height: max_height as usize,
},
&text_params,
),
height: text_size.height,
}
}
};

buffer.set_size(
font_system,
texture_size.width as f32,
texture_size.height as f32,
);
for line in &mut buffer.lines {
line.set_align(Some(text_params.align));
}
buffer.shape_until_scroll(font_system);

(buffer, texture_size)
}

fn get_texture_resolution<'a, I: Iterator<Item = &'a glyphon::BufferLine>>(
fn get_text_resolution<'a, I: Iterator<Item = &'a glyphon::BufferLine>>(
lines: I,
line_height: f32,
) -> Resolution {
Expand All @@ -256,35 +253,4 @@ impl TextRendererNode {
height: height as usize,
}
}

fn layout_fitted(
mut buffer: Buffer,
font_system: &mut FontSystem,
max_resolution: Resolution,
text_params: &TextParams,
) -> (Buffer, Resolution) {
// Calculate resolution
buffer.set_size(
font_system,
max_resolution.width as f32,
max_resolution.height as f32,
);

buffer.shape_until_scroll(font_system);
let resolution = Self::get_texture_resolution(buffer.lines.iter(), text_params.line_height);

// Shape text
buffer.set_size(
font_system,
resolution.width as f32,
resolution.height as f32,
);

for line in &mut buffer.lines {
line.set_align(Some(text_params.align));
}
buffer.shape_until_scroll(font_system);

(buffer, resolution)
}
}

0 comments on commit c5a47e8

Please sign in to comment.