Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configurable tab width (DirectWrite and Pango) #442

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion piet-cairo/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::rc::Rc;

use glib::translate::{from_glib_full, ToGlibPtr};

use pango::{AttrList, FontMapExt};
use pango::{AttrList, FontMapExt, TabAlign, TabArray};
use pango_sys::pango_attr_insert_hyphens_new;
use pangocairo::FontMap;

Expand Down Expand Up @@ -49,9 +49,12 @@ pub struct CairoTextLayout {

pub struct CairoTextLayoutBuilder {
text: Rc<dyn TextStorage>,

defaults: util::LayoutDefaults,
attributes: Vec<AttributeWithRange>,
last_range_start_pos: usize,

tab_width: Option<f64>,
width_constraint: f64,
pango_layout: PangoLayout,
}
Expand Down Expand Up @@ -193,6 +196,7 @@ impl Text for CairoText {
defaults: util::LayoutDefaults::default(),
attributes: Vec::new(),
last_range_start_pos: 0,
tab_width: None,
width_constraint: f64::INFINITY,
pango_layout,
}
Expand All @@ -213,6 +217,11 @@ impl TextLayoutBuilder for CairoTextLayoutBuilder {
self
}

fn tab_width(mut self, width: f64) -> Self {
self.tab_width = Some(width);
self
}

fn alignment(self, alignment: TextAlignment) -> Self {
/*
* NOTE: Pango has `auto_dir` enabled by default. This means that
Expand Down Expand Up @@ -277,6 +286,13 @@ impl TextLayoutBuilder for CairoTextLayoutBuilder {
}

fn build(self) -> Result<Self::Out, Error> {
if let Some(tab_width) = self.tab_width {
let tab_width = (tab_width * PANGO_SCALE) as i32;
let mut array = TabArray::new(1, false);
array.set_tab(0, TabAlign::Left, tab_width);
self.pango_layout.set_tabs(Some(&array));
}

let pango_attributes = AttrList::new();
let add_attribute = |attribute| {
if let Some(attribute) = attribute {
Expand Down
4 changes: 4 additions & 0 deletions piet-coregraphics/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,10 @@ impl TextLayoutBuilder for CoreGraphicsTextLayoutBuilder {
self
}

fn tab_width(self, _width: f64) -> Self {
self
}

fn alignment(mut self, alignment: piet::TextAlignment) -> Self {
self.alignment = alignment;
self
Expand Down
6 changes: 6 additions & 0 deletions piet-direct2d/src/dwrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ impl TextLayout {
}
}

pub(crate) fn set_incremental_tab_stop(&self, distance: f32) {
unsafe {
self.0.SetIncrementalTabStop(distance);
}
}

/// Set the weight for a range of this layout. `start` and `len` are in utf16.
pub(crate) fn set_weight(&mut self, range: Utf16Range, weight: FontWeight) {
let weight = weight.to_raw() as DWRITE_FONT_WEIGHT;
Expand Down
11 changes: 11 additions & 0 deletions piet-direct2d/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub struct D2DTextLayout {
pub struct D2DTextLayoutBuilder {
text: Rc<dyn TextStorage>,
layout: Result<dwrite::TextLayout, Error>,
tab_width: Option<f32>,
len_utf16: usize,
loaded_fonts: D2DLoadedFonts,
default_font: FontFamily,
Expand Down Expand Up @@ -161,6 +162,7 @@ impl Text for D2DText {
D2DTextLayoutBuilder {
layout,
text,
tab_width: None,
len_utf16: wide_str.len(),
colors: Vec::new(),
loaded_fonts: self.loaded_fonts.clone(),
Expand All @@ -186,6 +188,11 @@ impl TextLayoutBuilder for D2DTextLayoutBuilder {
self
}

fn tab_width(mut self, width: f64) -> Self {
self.tab_width = Some(width as f32);
self
}

fn alignment(mut self, alignment: TextAlignment) -> Self {
if let Ok(layout) = self.layout.as_mut() {
layout.set_alignment(alignment);
Expand Down Expand Up @@ -229,6 +236,10 @@ impl TextLayoutBuilder for D2DTextLayoutBuilder {
let (default_line_height, default_baseline) = self.get_default_line_height_and_baseline();
let layout = self.layout?;

if let Some(tab_width) = self.tab_width {
layout.set_incremental_tab_stop(tab_width);
}

let mut layout = D2DTextLayout {
text: self.text,
colors: self.colors.into(),
Expand Down
4 changes: 4 additions & 0 deletions piet-svg/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ impl piet::TextLayoutBuilder for TextLayoutBuilder {
self
}

fn tab_width(self, _width: f64) -> Self {
self
}

fn alignment(self, _alignment: piet::TextAlignment) -> Self {
self
}
Expand Down
4 changes: 4 additions & 0 deletions piet-web/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ impl TextLayoutBuilder for WebTextLayoutBuilder {
self
}

fn tab_width(self, _width: f64) -> Self {
self
}

fn alignment(self, _alignment: piet::TextAlignment) -> Self {
web_sys::console::log_1(&"TextLayout alignment unsupported on web".into());
self
Expand Down
4 changes: 4 additions & 0 deletions piet/src/null_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ impl TextLayoutBuilder for NullTextLayoutBuilder {
self
}

fn tab_width(self, _width: f64) -> Self {
self
}

fn alignment(self, _alignment: crate::TextAlignment) -> Self {
self
}
Expand Down
6 changes: 6 additions & 0 deletions piet/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ pub trait TextLayoutBuilder: Sized {
/// default behaviour.
fn max_width(self, width: f64) -> Self;

/// Set the base tabulator width.
///
/// The width specified here controls the max width for tab characters
/// in the final layout.
fn tab_width(self, width: f64) -> Self;

/// Set the [`TextAlignment`] to be used for this layout.
///
/// [`TextAlignment`]: enum.TextAlignment.html
Expand Down