Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5465544eb2 | ||
|
|
75c88af778 | ||
|
|
4db36c1382 | ||
|
|
9b925feb90 |
No files matched your search
+12
-1
@@ -2,6 +2,16 @@
|
|||||||
|
|
||||||
## [Unreleased] - ReleaseDate
|
## [Unreleased] - ReleaseDate
|
||||||
|
|
||||||
|
## [0.3.2] - 2026-10-22
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- Refactor surface format selection
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- Render tab characters as 4 spaces to match SimpleGraphic (#53)
|
||||||
|
|
||||||
## [0.3.1] - 2026-10-21
|
## [0.3.1] - 2026-10-21
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
@@ -236,8 +246,9 @@
|
|||||||
- First release
|
- First release
|
||||||
|
|
||||||
<!-- next-url -->
|
<!-- next-url -->
|
||||||
[Unreleased]: https://github.com/meehl/rusty-path-of-building/compare/v0.3.1...HEAD
|
[Unreleased]: https://github.com/meehl/rusty-path-of-building/compare/v0.3.2...HEAD
|
||||||
|
|
||||||
|
[0.3.2]: https://github.com/meehl/rusty-path-of-building/compare/v0.3.1...v0.3.2
|
||||||
[0.3.1]: https://github.com/meehl/rusty-path-of-building/compare/v0.3.0...v0.3.1
|
[0.3.1]: https://github.com/meehl/rusty-path-of-building/compare/v0.3.0...v0.3.1
|
||||||
[0.3.0]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.18...v0.3.0
|
[0.3.0]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.18...v0.3.0
|
||||||
[0.2.18]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.17...v0.2.18
|
[0.2.18]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.17...v0.2.18
|
||||||
|
|||||||
Generated
+1
-1
@@ -3502,7 +3502,7 @@ checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rusty-path-of-building"
|
name = "rusty-path-of-building"
|
||||||
version = "0.3.1"
|
version = "0.3.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ahash",
|
"ahash",
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@ authors = ["meehl"]
|
|||||||
description = "Cross-platform runtime for Path of Building"
|
description = "Cross-platform runtime for Path of Building"
|
||||||
repository = "https://github.com/meehl/rusty-path-of-building"
|
repository = "https://github.com/meehl/rusty-path-of-building"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
version = "0.3.1"
|
version = "0.3.2"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
|
|||||||
+33
-9
@@ -91,16 +91,40 @@ impl GraphicsContext {
|
|||||||
|
|
||||||
let surface_caps = surface.get_capabilities(&adapter);
|
let surface_caps = surface.get_capabilities(&adapter);
|
||||||
|
|
||||||
// NOTE: PoB incorrectly performs mixing and blending in sRGB space.
|
log::debug!(
|
||||||
// To get a similar visual outcome, we need to do the same.
|
"surface format capabilities: {:#?}",
|
||||||
// Select a non-sRGB format so that no automatic linear -> sRGB conversion
|
surface_caps.format_capabilities
|
||||||
// is performed.
|
);
|
||||||
let surface_format = surface_caps
|
|
||||||
.formats
|
// NOTE: PoB blends in non-linear space, so we want a plain UNORM format with
|
||||||
|
// no automatic linear -> non-linear conversion to get the same visual result.
|
||||||
|
const PREFERRED: [wgpu::TextureFormat; 2] = [
|
||||||
|
wgpu::TextureFormat::Bgra8Unorm,
|
||||||
|
wgpu::TextureFormat::Rgba8Unorm,
|
||||||
|
];
|
||||||
|
|
||||||
|
let surface_format = PREFERRED
|
||||||
.iter()
|
.iter()
|
||||||
.find(|f| !f.is_srgb() && f.required_features().is_empty())
|
|
||||||
.copied()
|
.copied()
|
||||||
.unwrap_or(surface_caps.formats[0]);
|
.find(|f| surface_caps.formats.contains(f))
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
// fallback: any non-sRGB format that isn't floating point
|
||||||
|
surface_caps
|
||||||
|
.formats
|
||||||
|
.iter()
|
||||||
|
.copied()
|
||||||
|
.find(|f| {
|
||||||
|
!f.is_srgb()
|
||||||
|
&& !matches!(
|
||||||
|
f,
|
||||||
|
wgpu::TextureFormat::Rgba16Float | wgpu::TextureFormat::Rgba32Float
|
||||||
|
)
|
||||||
|
&& f.required_features().is_empty()
|
||||||
|
})
|
||||||
|
.unwrap_or(surface_caps.formats[0])
|
||||||
|
});
|
||||||
|
|
||||||
|
log::debug!("selected format: {:?}", surface_format);
|
||||||
|
|
||||||
let config = wgpu::SurfaceConfiguration {
|
let config = wgpu::SurfaceConfiguration {
|
||||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
||||||
@@ -111,7 +135,7 @@ impl GraphicsContext {
|
|||||||
alpha_mode: surface_caps.alpha_modes[0],
|
alpha_mode: surface_caps.alpha_modes[0],
|
||||||
view_formats: vec![],
|
view_formats: vec![],
|
||||||
desired_maximum_frame_latency: 2,
|
desired_maximum_frame_latency: 2,
|
||||||
color_space: wgpu::SurfaceColorSpace::Auto,
|
color_space: wgpu::SurfaceColorSpace::Srgb,
|
||||||
};
|
};
|
||||||
|
|
||||||
let (blit_texture, blit_texture_view) =
|
let (blit_texture, blit_texture_view) =
|
||||||
|
|||||||
+190
-34
@@ -160,11 +160,10 @@ pub fn get_index_at_cur(
|
|||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
.layout(job, ctx.window_state.scale_factor());
|
.layout(job, ctx.window_state.scale_factor());
|
||||||
|
|
||||||
// `cursor_index_at` returns an offset into the **stripped** text, but PoB expects on offset
|
// `cursor_index_at` returns an offset into the layout text, but PoB expects an offset into
|
||||||
// into the original, **unstripped** text. A `StrippedToOriginalOffsetMap` is used to do the
|
// the original text. A `LayoutTextToOriginalOffsetMap` handles the conversion.
|
||||||
// conversion.
|
let layout_text_index = layout.cursor_index_at(Point::new(cur_x, cur_y));
|
||||||
let stripped_index = layout.cursor_index_at(Point::new(cur_x, cur_y));
|
let original_index = offset_map.to_original(layout_text_index);
|
||||||
let original_index = offset_map.to_original(stripped_index);
|
|
||||||
|
|
||||||
// convert to lua's 1-based indexing
|
// convert to lua's 1-based indexing
|
||||||
Ok(original_index + 1)
|
Ok(original_index + 1)
|
||||||
@@ -240,44 +239,106 @@ fn build_layout_job<'a>(
|
|||||||
original_start: _,
|
original_start: _,
|
||||||
} in PoBString(text)
|
} in PoBString(text)
|
||||||
{
|
{
|
||||||
job.append(text, color.unwrap_or(current_color));
|
let color = color.unwrap_or(current_color);
|
||||||
|
for (_, piece) in split_tabs(text) {
|
||||||
|
job.append(piece.layout_text(), color);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
job
|
job
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Maps byte offset in stripped text back to offset in original text.
|
/// Tabs are replaced with four spaces to match SimpleGraphic's behavior.
|
||||||
#[derive(Debug, Default, Clone)]
|
const TAB_REPLACEMENT: &str = " ";
|
||||||
struct StrippedToOriginalOffsetMap {
|
|
||||||
segments: Vec<(usize, usize)>,
|
/// A piece of text with tabs split out.
|
||||||
|
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
||||||
|
enum TextPiece<'a> {
|
||||||
|
Text(&'a str),
|
||||||
|
Tab,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StrippedToOriginalOffsetMap {
|
impl<'a> TextPiece<'a> {
|
||||||
fn push_segment(&mut self, stripped_start: usize, original_start: usize) {
|
/// Returns the text that is passed to the layout
|
||||||
self.segments.push((stripped_start, original_start));
|
fn layout_text(self) -> &'a str {
|
||||||
|
match self {
|
||||||
|
Self::Text(text) => text,
|
||||||
|
Self::Tab => TAB_REPLACEMENT,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Splits `text` at tab characters. Yields each piece alongside its byte offset into `text`.
|
||||||
|
fn split_tabs(text: &str) -> impl Iterator<Item = (usize, TextPiece<'_>)> {
|
||||||
|
let mut offset = 0;
|
||||||
|
text.split('\t').enumerate().flat_map(move |(i, piece)| {
|
||||||
|
// every piece after the first one is preceded by a tab
|
||||||
|
let tab = (i > 0).then(|| (offset - 1, TextPiece::Tab));
|
||||||
|
let text_piece = (offset, TextPiece::Text(piece));
|
||||||
|
offset += piece.len() + 1;
|
||||||
|
tab.into_iter().chain(std::iter::once(text_piece))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
struct OffsetSegment {
|
||||||
|
layout_text_start: usize,
|
||||||
|
original_start: usize,
|
||||||
|
is_tab: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Maps a byte offset in the layout text back to an offset in the original text.
|
||||||
|
///
|
||||||
|
/// The layout text is the text passed to `parley`, i.e. without color escapes and with tabs
|
||||||
|
/// replaced by spaces.
|
||||||
|
#[derive(Debug, Default, Clone)]
|
||||||
|
struct LayoutTextToOriginalOffsetMap {
|
||||||
|
segments: Vec<OffsetSegment>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LayoutTextToOriginalOffsetMap {
|
||||||
|
fn push_segment(&mut self, layout_text_start: usize, original_start: usize) {
|
||||||
|
self.segments.push(OffsetSegment {
|
||||||
|
layout_text_start,
|
||||||
|
original_start,
|
||||||
|
is_tab: false,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_original(&self, stripped_index: usize) -> usize {
|
fn push_tab(&mut self, layout_text_start: usize, original_start: usize) {
|
||||||
|
self.segments.push(OffsetSegment {
|
||||||
|
layout_text_start,
|
||||||
|
original_start,
|
||||||
|
is_tab: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_original(&self, layout_text_index: usize) -> usize {
|
||||||
assert!(!self.segments.is_empty());
|
assert!(!self.segments.is_empty());
|
||||||
let i = self
|
let i = self
|
||||||
.segments
|
.segments
|
||||||
.partition_point(|&(stripped_start, _)| stripped_start <= stripped_index);
|
.partition_point(|seg| seg.layout_text_start <= layout_text_index);
|
||||||
let (stripped_start, original_start) = self.segments[i - 1];
|
let seg = self.segments[i - 1];
|
||||||
original_start + (stripped_index - stripped_start)
|
if seg.is_tab {
|
||||||
|
// map any cursor position within a tab to the start of it like SG
|
||||||
|
seg.original_start
|
||||||
|
} else {
|
||||||
|
seg.original_start + (layout_text_index - seg.layout_text_start)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a `StrippedToOriginalOffsetMap` alongside the `LayoutJob`.
|
/// Returns a `LayoutTextToOriginalOffsetMap` alongside the `LayoutJob`.
|
||||||
fn build_layout_job_with_offsets<'a>(
|
fn build_layout_job_with_offsets<'a>(
|
||||||
text: &'a str,
|
text: &'a str,
|
||||||
current_color: Srgba,
|
current_color: Srgba,
|
||||||
font_type: PoBFontType,
|
font_type: PoBFontType,
|
||||||
line_height: i32,
|
line_height: i32,
|
||||||
alignment: Option<Alignment>,
|
alignment: Option<Alignment>,
|
||||||
) -> (LayoutJob<'a>, StrippedToOriginalOffsetMap) {
|
) -> (LayoutJob<'a>, LayoutTextToOriginalOffsetMap) {
|
||||||
let mut job = new_layout_job(font_type, line_height, alignment);
|
let mut job = new_layout_job(font_type, line_height, alignment);
|
||||||
let mut offset_map = StrippedToOriginalOffsetMap::default();
|
let mut offset_map = LayoutTextToOriginalOffsetMap::default();
|
||||||
let mut stripped_offset = 0;
|
let mut layout_text_offset = 0;
|
||||||
|
|
||||||
for ColoredSegment {
|
for ColoredSegment {
|
||||||
color,
|
color,
|
||||||
@@ -285,9 +346,18 @@ fn build_layout_job_with_offsets<'a>(
|
|||||||
original_start,
|
original_start,
|
||||||
} in PoBString(text)
|
} in PoBString(text)
|
||||||
{
|
{
|
||||||
offset_map.push_segment(stripped_offset, original_start);
|
let color = color.unwrap_or(current_color);
|
||||||
job.append(text, color.unwrap_or(current_color));
|
for (offset, piece) in split_tabs(text) {
|
||||||
stripped_offset += text.len();
|
match piece {
|
||||||
|
TextPiece::Text(_) => {
|
||||||
|
offset_map.push_segment(layout_text_offset, original_start + offset);
|
||||||
|
}
|
||||||
|
TextPiece::Tab => offset_map.push_tab(layout_text_offset, original_start + offset),
|
||||||
|
}
|
||||||
|
let layout_text = piece.layout_text();
|
||||||
|
job.append(layout_text, color);
|
||||||
|
layout_text_offset += layout_text.len();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
(job, offset_map)
|
(job, offset_map)
|
||||||
@@ -317,7 +387,7 @@ impl<'a> IntoIterator for PoBString<'a> {
|
|||||||
pub struct ColoredSegment<'a> {
|
pub struct ColoredSegment<'a> {
|
||||||
pub color: Option<Srgba>,
|
pub color: Option<Srgba>,
|
||||||
pub text: &'a str,
|
pub text: &'a str,
|
||||||
/// The byte offset of `text` within the **original**, unstripped string.
|
/// The byte offset of `text` within the **original** string.
|
||||||
pub original_start: usize,
|
pub original_start: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -525,7 +595,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn cursor_offset_empty() {
|
fn cursor_offset_empty() {
|
||||||
let mut offset_map = StrippedToOriginalOffsetMap::default();
|
let mut offset_map = LayoutTextToOriginalOffsetMap::default();
|
||||||
for seg in PoBString("") {
|
for seg in PoBString("") {
|
||||||
offset_map.push_segment(0, seg.original_start);
|
offset_map.push_segment(0, seg.original_start);
|
||||||
}
|
}
|
||||||
@@ -534,11 +604,11 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn cursor_offset_no_escape() {
|
fn cursor_offset_no_escape() {
|
||||||
let mut offset_map = StrippedToOriginalOffsetMap::default();
|
let mut offset_map = LayoutTextToOriginalOffsetMap::default();
|
||||||
let mut stripped_offset = 0;
|
let mut layout_text_offset = 0;
|
||||||
for seg in PoBString("Hello, World!") {
|
for seg in PoBString("Hello, World!") {
|
||||||
offset_map.push_segment(stripped_offset, seg.original_start);
|
offset_map.push_segment(layout_text_offset, seg.original_start);
|
||||||
stripped_offset += seg.text.len();
|
layout_text_offset += seg.text.len();
|
||||||
}
|
}
|
||||||
assert_eq!(offset_map.to_original(0), 0);
|
assert_eq!(offset_map.to_original(0), 0);
|
||||||
assert_eq!(offset_map.to_original(3), 3);
|
assert_eq!(offset_map.to_original(3), 3);
|
||||||
@@ -547,15 +617,101 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn cursor_offset() {
|
fn cursor_offset() {
|
||||||
let mut offset_map = StrippedToOriginalOffsetMap::default();
|
let mut offset_map = LayoutTextToOriginalOffsetMap::default();
|
||||||
let mut stripped_offset = 0;
|
let mut layout_text_offset = 0;
|
||||||
for seg in PoBString("^1Hello^xFF0000World^3!") {
|
for seg in PoBString("^1Hello^xFF0000World^3!") {
|
||||||
offset_map.push_segment(stripped_offset, seg.original_start);
|
offset_map.push_segment(layout_text_offset, seg.original_start);
|
||||||
stripped_offset += seg.text.len();
|
layout_text_offset += seg.text.len();
|
||||||
}
|
}
|
||||||
assert_eq!(offset_map.to_original(0), 2);
|
assert_eq!(offset_map.to_original(0), 2);
|
||||||
assert_eq!(offset_map.to_original(3), 5);
|
assert_eq!(offset_map.to_original(3), 5);
|
||||||
assert_eq!(offset_map.to_original(6), 16);
|
assert_eq!(offset_map.to_original(6), 16);
|
||||||
assert_eq!(offset_map.to_original(11), 23);
|
assert_eq!(offset_map.to_original(11), 23);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn split_tabs_no_tabs() {
|
||||||
|
assert_eq!(
|
||||||
|
split_tabs("Hello").collect::<Vec<_>>(),
|
||||||
|
vec![(0, TextPiece::Text("Hello"))]
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
split_tabs("").collect::<Vec<_>>(),
|
||||||
|
vec![(0, TextPiece::Text(""))]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn split_tabs_with_tabs() {
|
||||||
|
assert_eq!(
|
||||||
|
split_tabs("a\tbc\t\td\t").collect::<Vec<_>>(),
|
||||||
|
vec![
|
||||||
|
(0, TextPiece::Text("a")),
|
||||||
|
(1, TextPiece::Tab),
|
||||||
|
(2, TextPiece::Text("bc")),
|
||||||
|
(4, TextPiece::Tab),
|
||||||
|
(5, TextPiece::Text("")),
|
||||||
|
(5, TextPiece::Tab),
|
||||||
|
(6, TextPiece::Text("d")),
|
||||||
|
(7, TextPiece::Tab),
|
||||||
|
(8, TextPiece::Text("")),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn layout_text(job: &LayoutJob) -> String {
|
||||||
|
job.segments.iter().map(|s| s.text).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tabs_replaced_with_spaces() {
|
||||||
|
let job = build_layout_job("^1a\tb^2\tc", Srgba::WHITE, PoBFontType::Var, 16, None);
|
||||||
|
assert_eq!(layout_text(&job), "a b c");
|
||||||
|
|
||||||
|
// tabs keep the color of the segment they're in
|
||||||
|
let red = Srgba::from_rgb(255, 0, 0);
|
||||||
|
let green = Srgba::from_rgb(0, 255, 0);
|
||||||
|
let colors: Vec<_> = job.segments.iter().map(|s| s.color).collect();
|
||||||
|
assert_eq!(colors, vec![red, red, red, green, green, green]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn cursor_offset_with_tabs() {
|
||||||
|
let (job, offset_map) =
|
||||||
|
build_layout_job_with_offsets("^1a\tb\t", Srgba::WHITE, PoBFontType::Var, 16, None);
|
||||||
|
assert_eq!(layout_text(&job), "a b ");
|
||||||
|
|
||||||
|
// 'a'
|
||||||
|
assert_eq!(offset_map.to_original(0), 2);
|
||||||
|
// first tab: any position within the tab maps to the tab start
|
||||||
|
assert_eq!(offset_map.to_original(1), 3);
|
||||||
|
assert_eq!(offset_map.to_original(2), 3);
|
||||||
|
assert_eq!(offset_map.to_original(4), 3);
|
||||||
|
// 'b'
|
||||||
|
assert_eq!(offset_map.to_original(5), 4);
|
||||||
|
// trailing tab: any position within the tab maps to the tab start
|
||||||
|
assert_eq!(offset_map.to_original(6), 5);
|
||||||
|
assert_eq!(offset_map.to_original(9), 5);
|
||||||
|
// end of text is after the trailing tab
|
||||||
|
assert_eq!(offset_map.to_original(10), 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn cursor_offset_with_consecutive_tabs() {
|
||||||
|
let (job, offset_map) =
|
||||||
|
build_layout_job_with_offsets("a\t\tb", Srgba::WHITE, PoBFontType::Var, 16, None);
|
||||||
|
assert_eq!(layout_text(&job), "a b");
|
||||||
|
assert_eq!(offset_map.to_original(0), 0);
|
||||||
|
|
||||||
|
// first tab
|
||||||
|
for layout_text_index in 1..=4 {
|
||||||
|
assert_eq!(offset_map.to_original(layout_text_index), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// second tab, which starts immediately after the first one
|
||||||
|
for layout_text_index in 5..=8 {
|
||||||
|
assert_eq!(offset_map.to_original(layout_text_index), 2);
|
||||||
|
}
|
||||||
|
assert_eq!(offset_map.to_original(9), 3);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user