Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
use cosmic_text ::{
2025-11-23 20:43:33 +01:00
fontdb , Align , Attrs , AttrsList , BidiParagraphs , Buffer , Family , FontSystem , Hinting ,
LayoutLine , Metrics , ShapeLine , Shaping , Weight , Wrap ,
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
} ;
// Test for https://github.com/pop-os/cosmic-text/issues/134
//
// Being able to get the same wrapping when feeding the measured width back into ShapeLine::layout
// as the new width limit is very useful for certain UI layout use cases.
#[ test ]
fn stable_wrap ( ) {
let font_size = 18.0 ;
let attrs = AttrsList ::new (
2025-03-31 17:03:51 +11:00
& Attrs ::new ( )
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
. family ( Family ::Name ( " FiraMono " ) )
. weight ( Weight ::MEDIUM ) ,
) ;
let mut font_system =
FontSystem ::new_with_locale_and_db ( " en-US " . into ( ) , fontdb ::Database ::new ( ) ) ;
let font = std ::fs ::read ( " fonts/FiraMono-Medium.ttf " ) . unwrap ( ) ;
font_system . db_mut ( ) . load_font_data ( font ) ;
2024-06-11 08:17:35 -06:00
let mut check_wrap = | text : & _ , wrap , align_opt , start_width_opt | {
2024-06-06 21:01:46 -06:00
let line = ShapeLine ::new ( & mut font_system , text , & attrs , Shaping ::Advanced , 8 ) ;
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
2025-11-23 20:43:33 +01:00
let layout_unbounded = line . layout (
font_size ,
start_width_opt ,
wrap ,
align_opt ,
None ,
Hinting ::Disabled ,
) ;
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
let max_width = layout_unbounded . iter ( ) . map ( | l | l . w ) . fold ( 0.0 , f32 ::max ) ;
2024-06-11 08:17:35 -06:00
let new_limit = match start_width_opt {
Some ( start_width ) = > f32 ::min ( start_width , max_width ) ,
None = > max_width ,
} ;
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
2025-11-23 20:43:33 +01:00
let layout_bounded = line . layout (
font_size ,
Some ( new_limit ) ,
wrap ,
align_opt ,
None ,
Hinting ::Disabled ,
) ;
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
let bounded_max_width = layout_bounded . iter ( ) . map ( | l | l . w ) . fold ( 0.0 , f32 ::max ) ;
// For debugging:
// dbg_layout_lines(text, &layout_unbounded);
// dbg_layout_lines(text, &layout_bounded);
assert_eq! (
( max_width , layout_unbounded . len ( ) ) ,
( bounded_max_width , layout_bounded . len ( ) ) ,
2024-06-11 08:17:35 -06:00
" Wrap \" {wrap:?} \" and align \" {align_opt:?} \" with text: \" {text} \" " ,
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
) ;
for ( u , b ) in layout_unbounded [ 1 .. ] . iter ( ) . zip ( layout_bounded [ 1 .. ] . iter ( ) ) {
2024-06-11 08:17:35 -06:00
assert_eq! (
u . w , b . w ,
" Wrap {wrap:?} and align \" {align_opt:?} \" with text: \" {text} \" " ,
) ;
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
}
} ;
let hello_sample = std ::fs ::read_to_string ( " sample/hello.txt " ) . unwrap ( ) ;
let cases = [
" (6) SomewhatBoringDisplayTransform " ,
" " ,
" " ,
" " ,
" " ,
" " ,
]
. into_iter ( )
// This has several cases where the line would wrap when the computed width was used as the
// width limit.
. chain ( BidiParagraphs ::new ( & hello_sample ) ) ;
for text in cases {
2024-06-11 08:17:35 -06:00
for wrap in [ Wrap ::None , Wrap ::Glyph , Wrap ::Word , Wrap ::WordOrGlyph ] {
for align_opt in [
None ,
Some ( Align ::Left ) ,
Some ( Align ::Right ) ,
Some ( Align ::Center ) ,
//TODO: Align::Justified
Some ( Align ::End ) ,
] {
for start_width_opt in [
None ,
Some ( f32 ::MAX ) ,
Some ( 80.0 ) ,
Some ( 198.2132 ) ,
Some ( 20.0 ) ,
Some ( 4.0 ) ,
Some ( 300.0 ) ,
] {
check_wrap ( text , wrap , align_opt , start_width_opt ) ;
let with_spaces = format! ( " {text} " ) ;
check_wrap ( & with_spaces , wrap , align_opt , start_width_opt ) ;
let with_spaces_2 = format! ( " {text} " ) ;
check_wrap ( & with_spaces_2 , wrap , align_opt , start_width_opt ) ;
}
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
}
}
}
}
2024-02-02 13:34:09 -05:00
#[ test ]
fn wrap_extra_line ( ) {
let mut font_system = FontSystem ::new ( ) ;
let metrics = Metrics ::new ( 14.0 , 20.0 ) ;
2025-11-27 23:18:30 +01:00
let mut buffer = Buffer ::new ( & mut font_system , metrics ) ;
2024-02-02 13:34:09 -05:00
let mut buffer = buffer . borrow_with ( & mut font_system ) ;
2026-02-25 23:14:56 -07:00
// Configure wrap and size, then add text
2024-02-02 13:34:09 -05:00
buffer . set_wrap ( Wrap ::Word ) ;
2024-06-12 09:04:04 -06:00
buffer . set_size ( Some ( 50.0 ) , Some ( 1000.0 ) ) ;
2026-02-25 23:14:56 -07:00
buffer . set_text ( " Lorem ipsum dolor sit amet, qui minim labore adipisicing \n \n weeewoooo minim sint cillum sint consectetur cupidatat. " , & Attrs ::new ( ) . family ( cosmic_text ::Family ::Name ( " Inter " ) ) , Shaping ::Advanced , None ) ;
2024-02-02 13:34:09 -05:00
let empty_lines = buffer . layout_runs ( ) . filter ( | x | x . line_w = = 0. ) . count ( ) ;
let overflow_lines = buffer . layout_runs ( ) . filter ( | x | x . line_w > 50. ) . count ( ) ;
assert_eq! ( empty_lines , 1 ) ;
assert_eq! ( overflow_lines , 4 ) ;
}
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
#[ allow(dead_code) ]
fn dbg_layout_lines ( text : & str , lines : & [ LayoutLine ] ) {
for line in lines {
let mut s = String ::new ( ) ;
for glyph in line . glyphs . iter ( ) {
s . push_str ( & text [ glyph . start .. glyph . end ] ) ;
}
println! ( " \" {s} \" " ) ;
}
}