Restructure api functions

This commit is contained in:
meehl committed 2026-08-29 19:08:49 +02:00
1 parent fe96786aca
commit 078269653e
5 files changed
+484 -440

No files matched your search

+38 -5
View File
@@ -6,9 +6,7 @@ use crate::{
clipboard::{copy, paste},
compression::{deflate, inflate},
console::{console_clear, console_execute, console_print_table, console_printf},
general::{
exit, get_time, open_url, render_init, restart, strip_escapes, take_screenshot,
},
general::{exit, get_time, open_url, render_init, restart, take_screenshot},
image_handle::register_image_handle_api,
input::{
get_cursor_pos, is_key_down, key_as_str, mousebutton_as_str, set_cursor_pos,
@@ -19,8 +17,13 @@ use crate::{
get_runtime_path, get_script_path, get_user_path, get_work_dir, make_dir,
remove_dir, set_work_dir,
},
rendering::{
draw_image, draw_image_quad, get_async_count, get_draw_color, get_draw_layer,
set_blend_mode, set_clear_color, set_draw_color, set_draw_layer, set_viewport,
},
search_handle::new_search_handle,
subscript::{abort_subscript, is_subscript_running, launch_subscript},
text::{draw_string, get_index_at_cur, get_string_width, strip_escapes},
window::{
get_dpi_scale_override, get_screen_scale, get_screen_size, set_dpi_scale_override,
set_foreground, set_window_title,
@@ -34,6 +37,7 @@ use winit::keyboard::SmolStr;
mod callback;
mod clipboard;
mod color;
mod compression;
mod console;
mod general;
@@ -44,6 +48,7 @@ mod paths;
mod rendering;
mod search_handle;
mod subscript;
mod text;
mod window;
/// Register functions that can be called from Lua
@@ -72,7 +77,6 @@ pub fn register_globals(lua: &Lua) -> LuaResult<()> {
// general
globals.set("GetTime", lua.create_function(get_time)?)?;
globals.set("StripEscapes", lua.create_function(strip_escapes)?)?;
globals.set("Exit", lua.create_function(exit)?)?;
globals.set("Restart", lua.create_function(restart)?)?;
globals.set("OpenURL", lua.create_function(open_url)?)?;
@@ -130,7 +134,36 @@ pub fn register_globals(lua: &Lua) -> LuaResult<()> {
globals.set("AbortSubScript", lua.create_function(abort_subscript)?)?;
// rendering
rendering::register_globals(lua)?;
// NOTE: unfortunately, mlua's conversion of function arguments adds a lot of
// overhead. this is very noticeable for the draw functions which can be called
// thousands of times per frame. C functions are used to get raw access to
// the lua stack without the overhead.
unsafe { globals.set("SetDrawColor", lua.create_c_function(set_draw_color)?)? };
unsafe { globals.set("GetDrawColor", lua.create_c_function(get_draw_color)?)? };
unsafe { globals.set("SetViewport", lua.create_c_function(set_viewport)?)? };
unsafe {
globals.set("SetDrawLayer", lua.create_c_function(set_draw_layer)?)?;
}
unsafe { globals.set("DrawImage", lua.create_c_function(draw_image)?)? };
unsafe { globals.set("DrawImageQuad", lua.create_c_function(draw_image_quad)?)? };
globals.set("GetDrawLayer", lua.create_function(get_draw_layer)?)?;
globals.set("SetBlendMode", lua.create_function(set_blend_mode)?)?;
globals.set("GetAsyncCount", lua.create_function(get_async_count)?)?;
globals.set("SetClearColor", lua.create_function(set_clear_color)?)?;
// text
// see comment above about why C functions are used for these
unsafe {
globals.set("DrawString", lua.create_c_function(draw_string)?)?;
}
unsafe {
globals.set("DrawStringWidth", lua.create_c_function(get_string_width)?)?;
}
globals.set(
"DrawStringCursorIndex",
lua.create_function_mut(get_index_at_cur)?,
)?;
globals.set("StripEscapes", lua.create_function(strip_escapes)?)?;
Ok(())
}
+41
View File
@@ -0,0 +1,41 @@
use crate::color::Srgba;
#[inline]
fn hex_byte(a: u8, b: u8) -> u8 {
(hex_digit(a) << 4) | hex_digit(b)
}
#[inline]
fn hex_digit(c: u8) -> u8 {
match c {
b'0'..=b'9' => c - b'0',
b'a'..=b'f' => c - b'a' + 10,
b'A'..=b'F' => c - b'A' + 10,
_ => unreachable!("invalid hex digit"),
}
}
impl Srgba {
pub fn from_escape_code(escape_str: &str) -> Self {
let bytes = escape_str.as_bytes();
match bytes {
[b'^', b'0'] => Self::from_rgb(0, 0, 0),
[b'^', b'1'] => Self::from_rgb(255, 0, 0),
[b'^', b'2'] => Self::from_rgb(0, 255, 0),
[b'^', b'3'] => Self::from_rgb(0, 0, 255),
[b'^', b'4'] => Self::from_rgb(255, 255, 0),
[b'^', b'5'] => Self::from_rgb(255, 0, 255),
[b'^', b'6'] => Self::from_rgb(0, 255, 255),
[b'^', b'7'] => Self::from_rgb(255, 255, 255),
[b'^', b'8'] => Self::from_rgb(178, 178, 178),
[b'^', b'9'] => Self::from_rgb(102, 102, 102),
[b'^', b'x' | b'X', r1, r2, g1, g2, b1, b2] => {
Self::from_rgb(hex_byte(*r1, *r2), hex_byte(*g1, *g2), hex_byte(*b1, *b2))
}
_ => Self::WHITE,
}
}
}
+1 -5
View File
@@ -1,4 +1,4 @@
use crate::pob::{Context, api::rendering::PoBString};
use crate::pob::Context;
use mlua::{IntoLuaMulti, Lua, MultiValue, Result as LuaResult, Variadic};
use std::time::{SystemTime, UNIX_EPOCH};
@@ -9,10 +9,6 @@ pub fn get_time(_l: &Lua, _: ()) -> LuaResult<u128> {
.as_millis())
}
pub fn strip_escapes(_: &Lua, text: String) -> LuaResult<String> {
Ok(PoBString(&text).strip_escapes())
}
pub fn exit(l: &Lua, exit_msg: Option<String>) -> LuaResult<()> {
if let Some(exit_msg) = exit_msg {
println!("{exit_msg}");
+14 -430
View File
@@ -1,7 +1,5 @@
use crate::{
color::Srgba,
dpi::LogicalPoint,
fonts::{Alignment, FontStyle, LayoutJob, LayoutToLogical},
math::{Point, Quad, Rect, Size},
pob::{Context, api::image_handle::image_handle_texture_id},
};
@@ -10,48 +8,6 @@ use mlua::{
Lua, Result as LuaResult,
ffi::{self},
};
use parley::{FontFamily, FontFamilyName};
use regex::Regex;
use std::{borrow::Cow, sync::LazyLock};
pub fn register_globals(lua: &Lua) -> LuaResult<()> {
let globals = lua.globals();
// unused functions
let get_draw_layer = |_: &Lua, ()| -> LuaResult<()> { unimplemented!() };
let set_blend_mode = |_: &Lua, ()| -> LuaResult<()> { unimplemented!() };
let get_async_count = |_: &Lua, ()| -> LuaResult<()> { unimplemented!() };
let set_clear_color = |_: &Lua, ()| -> LuaResult<()> { unimplemented!() };
globals.set("GetDrawLayer", lua.create_function(get_draw_layer)?)?;
globals.set("SetBlendMode", lua.create_function(set_blend_mode)?)?;
globals.set("GetAsyncCount", lua.create_function(get_async_count)?)?;
globals.set("SetClearColor", lua.create_function(set_clear_color)?)?;
// rendering functions
// NOTE: unfortunately, mlua's conversion of function arguments adds a lot of
// overhead. this is very noticeable for the draw functions which can be called
// thousands of times per frame. C functions are used to get raw access to
// the lua stack without the overhead.
unsafe { globals.set("SetDrawColor", lua.create_c_function(set_draw_color)?)? };
unsafe { globals.set("GetDrawColor", lua.create_c_function(get_draw_color)?)? };
unsafe { globals.set("SetViewport", lua.create_c_function(set_viewport)?)? };
unsafe {
globals.set("SetDrawLayer", lua.create_c_function(set_draw_layer)?)?;
}
unsafe { globals.set("DrawImage", lua.create_c_function(draw_image)?)? };
unsafe { globals.set("DrawImageQuad", lua.create_c_function(draw_image_quad)?)? };
unsafe {
globals.set("DrawString", lua.create_c_function(draw_string)?)?;
}
unsafe {
globals.set("DrawStringWidth", lua.create_c_function(get_string_width)?)?;
}
globals.set(
"DrawStringCursorIndex",
lua.create_function_mut(get_index_at_cur)?,
)?;
Ok(())
}
macro_rules! str_from_stack {
($s:ident, $i:expr) => {
@@ -76,7 +32,7 @@ macro_rules! i32_from_stack {
};
}
unsafe extern "C-unwind" fn set_draw_color(state: *mut ffi::lua_State) -> c_int {
pub unsafe extern "C-unwind" fn set_draw_color(state: *mut ffi::lua_State) -> c_int {
//profiling::scope!("set_draw_color");
let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) };
let mut ctx = lua_instance.app_data_mut::<Context>().unwrap();
@@ -112,7 +68,7 @@ unsafe extern "C-unwind" fn set_draw_color(state: *mut ffi::lua_State) -> c_int
0
}
unsafe extern "C-unwind" fn get_draw_color(state: *mut ffi::lua_State) -> c_int {
pub unsafe extern "C-unwind" fn get_draw_color(state: *mut ffi::lua_State) -> c_int {
//profiling::scope!("get_draw_color");
let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) };
let ctx = lua_instance.app_data_ref::<Context>().unwrap();
@@ -126,7 +82,7 @@ unsafe extern "C-unwind" fn get_draw_color(state: *mut ffi::lua_State) -> c_int
4
}
unsafe extern "C-unwind" fn set_viewport(state: *mut ffi::lua_State) -> c_int {
pub unsafe extern "C-unwind" fn set_viewport(state: *mut ffi::lua_State) -> c_int {
//profiling::scope!("set_viewport");
let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) };
let mut ctx = lua_instance.app_data_mut::<Context>().unwrap();
@@ -148,7 +104,7 @@ unsafe extern "C-unwind" fn set_viewport(state: *mut ffi::lua_State) -> c_int {
0
}
unsafe extern "C-unwind" fn set_draw_layer(state: *mut ffi::lua_State) -> c_int {
pub unsafe extern "C-unwind" fn set_draw_layer(state: *mut ffi::lua_State) -> c_int {
//profiling::scope!("set_draw_layer");
let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) };
let mut ctx = lua_instance.app_data_mut::<Context>().unwrap();
@@ -182,7 +138,7 @@ unsafe extern "C-unwind" fn set_draw_layer(state: *mut ffi::lua_State) -> c_int
0
}
unsafe extern "C-unwind" fn draw_image(state: *mut ffi::lua_State) -> c_int {
pub unsafe extern "C-unwind" fn draw_image(state: *mut ffi::lua_State) -> c_int {
//profiling::scope!("draw_image");
let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) };
let mut ctx = lua_instance.app_data_mut::<Context>().unwrap();
@@ -231,7 +187,7 @@ unsafe extern "C-unwind" fn draw_image(state: *mut ffi::lua_State) -> c_int {
0
}
unsafe extern "C-unwind" fn draw_image_quad(state: *mut ffi::lua_State) -> c_int {
pub unsafe extern "C-unwind" fn draw_image_quad(state: *mut ffi::lua_State) -> c_int {
//profiling::scope!("draw_image_quad");
let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) };
let mut ctx = lua_instance.app_data_mut::<Context>().unwrap();
@@ -299,390 +255,18 @@ unsafe extern "C-unwind" fn draw_image_quad(state: *mut ffi::lua_State) -> c_int
0
}
unsafe extern "C-unwind" fn draw_string(state: *mut ffi::lua_State) -> c_int {
//profiling::scope!("draw_string");
let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) };
let mut ctx = lua_instance.app_data_mut::<Context>().unwrap();
let nargs = unsafe { ffi::lua_gettop(state) };
let x = f32_from_stack!(state, -nargs);
let y = f32_from_stack!(state, -nargs + 1);
let alignment = str_from_stack!(state, -nargs + 2);
let line_height = i32_from_stack!(state, -nargs + 3);
let font_type = str_from_stack!(state, -nargs + 4);
let text = str_from_stack!(state, -nargs + 5);
let Ok(alignment) = alignment.parse::<PoBTextAlignment>() else {
panic!("Invalid alignment");
};
let Ok(font_type) = font_type.parse::<PoBFontType>() else {
panic!("Invalid font type");
};
let mut position = LogicalPoint::new(x, y);
let mut is_absolute_position = false;
// the position needs to be adjusted for some alignments to match PoBs behavior
let screen_size = ctx.window_state.logical_size();
let halign = match alignment {
PoBTextAlignment::Left => Alignment::Min,
PoBTextAlignment::Center => {
position.x += screen_size.width / 2.0;
is_absolute_position = true;
Alignment::Center
}
PoBTextAlignment::Right => {
position.x = screen_size.width - position.x;
is_absolute_position = true;
Alignment::Max
}
PoBTextAlignment::CenterX => Alignment::Center,
PoBTextAlignment::RightX => Alignment::Max,
};
let current_draw_color = ctx.recorder.get_draw_color();
let job = build_layout_job(
text,
current_draw_color,
font_type,
line_height,
Some(halign),
);
// NOTE: color escape codes modify the current draw color.
// set current draw color to color of last segment to match PoB's behavior
if let Some(last_segment) = job.segments.last() {
ctx.recorder.set_draw_color(last_segment.color);
}
let scale_factor = ctx.window_state.scale_factor();
let layout = ctx.fonts.borrow_mut().layout(job, scale_factor);
// apply offset to account for anchor position
let position = position + layout.anchor_offset(halign);
// align layout anchor with the physical pixel grid to reduce blurriness
let position = (position * scale_factor).round() / scale_factor;
for glyph in &layout.glyphs {
ctx.recorder.draw_glyph(
LayoutToLogical::new(position.x, position.y).transform_box(&glyph.rect),
glyph.uv,
glyph.color,
glyph.layer_idx,
is_absolute_position,
);
}
0
pub fn get_draw_layer(_: &Lua, _: ()) -> LuaResult<()> {
unimplemented!()
}
unsafe extern "C-unwind" fn get_string_width(state: *mut ffi::lua_State) -> c_int {
//profiling::scope!("get_string_width");
let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) };
let ctx = lua_instance.app_data_ref::<Context>().unwrap();
let nargs = unsafe { ffi::lua_gettop(state) };
let line_height = i32_from_stack!(state, -nargs);
let font_type = str_from_stack!(state, -nargs + 1);
let text = str_from_stack!(state, -nargs + 2);
let Ok(font_type) = font_type.parse::<PoBFontType>() else {
panic!("Invalid font type");
};
let job = build_layout_job(text, Srgba::WHITE, font_type, line_height, None);
let layout = ctx
.fonts
.borrow_mut()
.layout(job, ctx.window_state.scale_factor());
let width = layout.width();
unsafe { ffi::lua_pushnumber(state, width as f64) };
1
pub fn set_blend_mode(_: &Lua, _: ()) -> LuaResult<()> {
unimplemented!()
}
fn get_index_at_cur(
l: &Lua,
(line_height, font_type, text, cur_x, cur_y): (i32, String, String, f32, f32),
) -> LuaResult<usize> {
//profiling::scope!("get_char_index_at_cur");
let ctx = l.app_data_ref::<Context>().unwrap();
let font_type = font_type.parse::<PoBFontType>()?;
let job = build_layout_job(&text, Srgba::WHITE, font_type, line_height, None);
let layout = ctx
.fonts
.borrow_mut()
.layout(job, ctx.window_state.scale_factor());
let index_stripped = layout.cursor_index_at(Point::new(cur_x, cur_y));
// build_layout_job() strips all color escape strings from the original string. The
// resulting [`LayoutJob`] is then passed to get_text_index_at_cursor() which returns an
// index into the **stripped* string.
// But PoB expects an index into the **original, unstripped** text. Therefore we need to add
// the length of all color escapes up until the cursor position to return the right value.
//
// TODO: avoid matching and iterating over string twice
let mut color_escapes_total_length = 0;
for capture in ESCAPE_STR_REGEX.find_iter(&text) {
if capture.start() - color_escapes_total_length > index_stripped {
break;
}
color_escapes_total_length += capture.len();
}
// add length of color escapes and convert to lua's 1-based indexing
Ok(index_stripped + color_escapes_total_length + 1)
pub fn get_async_count(_: &Lua, _: ()) -> LuaResult<()> {
unimplemented!()
}
pub static ESCAPE_STR_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\^(?<idx>[0-9])|\^[xX](?<hex>[0-9A-Fa-f]{6})").unwrap());
fn build_layout_job<'a>(
text: &'a str,
current_color: Srgba,
font_type: PoBFontType,
line_height: i32,
alignment: Option<Alignment>,
) -> LayoutJob<'a> {
let mut font_weight = None;
let mut font_style = FontStyle::default();
let font_family = match font_type {
PoBFontType::Fixed => FontFamily::Single(FontFamilyName::Named(Cow::Borrowed(
"Bitstream Vera Sans Mono",
))),
PoBFontType::Var => {
FontFamily::Single(FontFamilyName::Named(Cow::Borrowed("Liberation Sans")))
}
PoBFontType::VarBold => {
font_weight = Some(700.0);
FontFamily::Single(FontFamilyName::Named(Cow::Borrowed("Liberation Sans")))
}
PoBFontType::Fontin => FontFamily::Single(FontFamilyName::Named(Cow::Borrowed("Fontin"))),
PoBFontType::FontinItalic => {
FontFamily::Single(FontFamilyName::Named(Cow::Borrowed("Fontin")))
}
PoBFontType::FontinSmallcaps => {
FontFamily::Single(FontFamilyName::Named(Cow::Borrowed("Fontin SmallCaps")))
}
PoBFontType::FontinSmallcapsItalic => {
font_style = FontStyle::Italic;
// use regular Smallcaps with "faux italics"
FontFamily::Single(FontFamilyName::Named(Cow::Borrowed("Fontin SmallCaps")))
}
};
// NOTE: This is just an approximation and was chosen based on how it looks.
//
// PoB uses pre-rendered font atlases of discrete sizes and selects the appropriate
// atlas based on the provided height. Rusty-PoB dynamically renders fonts to a
// cached font atlas to support the selection of arbitrary sizes.
//
// TODO: font size in some dropdowns is too small, e.g. socket group selection in
// 'Calcs' tab
let font_size = (line_height - 2).max(1) as f32;
let mut job = LayoutJob::new(
font_family,
font_size,
line_height as f32,
alignment,
font_weight,
font_style,
);
for (color, segment) in PoBString(text) {
job.append(segment, color.unwrap_or(current_color));
}
job
}
// PoB strings can contain escape codes that affect the color of subsequent text
pub struct PoBString<'a>(pub &'a str);
impl<'a> PoBString<'a> {
pub fn strip_escapes(&self) -> String {
ESCAPE_STR_REGEX.replace_all(self.0, "").to_string()
}
}
type ColoredSegment<'a> = (Option<Srgba>, &'a str);
impl<'a> IntoIterator for PoBString<'a> {
type Item = ColoredSegment<'a>;
type IntoIter = PoBStringSegmentIterator<'a>;
fn into_iter(self) -> Self::IntoIter {
PoBStringSegmentIterator::new(self.0)
}
}
// Iterates over colored segments
pub struct PoBStringSegmentIterator<'a> {
haystack: &'a str,
captures: std::iter::Peekable<regex::CaptureMatches<'static, 'a>>,
is_first: bool,
is_done: bool,
}
impl<'a> PoBStringSegmentIterator<'a> {
fn new(haystack: &'a str) -> Self {
let captures = ESCAPE_STR_REGEX.captures_iter(haystack).peekable();
Self {
haystack,
captures,
is_first: true,
is_done: false,
}
}
}
impl<'a> Iterator for PoBStringSegmentIterator<'a> {
type Item = ColoredSegment<'a>;
fn next(&mut self) -> Option<Self::Item> {
let is_first = core::mem::replace(&mut self.is_first, false);
match self.captures.peek() {
Some(capture) => {
let code_start = capture.get(0).unwrap().start();
let code_end = capture.get(0).unwrap().end();
// string didn't start with an escape code.
// return text up to first code without color.
if is_first && code_start > 0 {
return Some((None, &self.haystack[..code_start]));
}
let escape_str = capture.get(0).unwrap().as_str();
let color = Some(Srgba::from_escape_code(escape_str));
let _ = self.captures.next(); // pop current code to peek the next one
if let Some(next_code) = self.captures.peek() {
// found another escape code. return text up the next code
let next_code_start = next_code.get(0).unwrap().start();
Some((color, &self.haystack[code_end..next_code_start]))
} else {
// no additional escape codes found. return rest of string
self.is_done = true;
Some((color, &self.haystack[code_end..]))
}
}
None => {
if self.is_done {
None
} else {
// string doesn't contain any escape codes.
// return entire string without color
self.is_done = true;
Some((None, self.haystack))
}
}
}
}
}
// PoB's alignment argument is weird!
// It controls:
// - the alignment within the text box
// - the anchor point of the box
// - the relative/absolute positioning of the box
#[derive(Clone, Copy, Debug)]
enum PoBTextAlignment {
// alignment: left | anchor: top-left corner | position: relative to viewport
Left,
// alignment: center | anchor: top-center | position: relative to center of screen
Center,
// alignment: right | anchor: top-right | position: relative to right edge of screen
Right,
// alignment: center | anchor: top-center | position: relative to viewport
CenterX,
// alignment: right | anchor: top-right corner | position: relative to viewport
RightX,
}
impl std::str::FromStr for PoBTextAlignment {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"LEFT" => Ok(Self::Left),
"CENTER" => Ok(Self::Center),
"RIGHT" => Ok(Self::Right),
"CENTER_X" => Ok(Self::CenterX),
"RIGHT_X" => Ok(Self::RightX),
_ => Err(anyhow::anyhow!("'{s}' is not a valid TextFontType variant")),
}
}
}
#[derive(Clone, Copy, Debug)]
enum PoBFontType {
Fixed,
Var,
VarBold,
FontinSmallcaps,
FontinSmallcapsItalic,
Fontin,
FontinItalic,
}
impl std::str::FromStr for PoBFontType {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"FIXED" => Ok(Self::Fixed),
"VAR" => Ok(Self::Var),
"VAR BOLD" => Ok(Self::VarBold),
"FONTIN SC" => Ok(Self::FontinSmallcaps),
"FONTIN SC ITALIC" => Ok(Self::FontinSmallcapsItalic),
"FONTIN" => Ok(Self::Fontin),
"FONTIN ITALIC" => Ok(Self::FontinItalic),
_ => Err(anyhow::anyhow!("'{s}' is not a valid TextFontType variant")),
}
}
}
#[inline]
fn hex_byte(a: u8, b: u8) -> u8 {
(hex_digit(a) << 4) | hex_digit(b)
}
#[inline]
fn hex_digit(c: u8) -> u8 {
match c {
b'0'..=b'9' => c - b'0',
b'a'..=b'f' => c - b'a' + 10,
b'A'..=b'F' => c - b'A' + 10,
_ => unreachable!("invalid hex digit"),
}
}
impl Srgba {
fn from_escape_code(escape_str: &str) -> Srgba {
let bytes = escape_str.as_bytes();
match bytes {
[b'^', b'0'] => Self::from_rgb(0, 0, 0),
[b'^', b'1'] => Self::from_rgb(255, 0, 0),
[b'^', b'2'] => Self::from_rgb(0, 255, 0),
[b'^', b'3'] => Self::from_rgb(0, 0, 255),
[b'^', b'4'] => Self::from_rgb(255, 255, 0),
[b'^', b'5'] => Self::from_rgb(255, 0, 255),
[b'^', b'6'] => Self::from_rgb(0, 255, 255),
[b'^', b'7'] => Self::from_rgb(255, 255, 255),
[b'^', b'8'] => Self::from_rgb(178, 178, 178),
[b'^', b'9'] => Self::from_rgb(102, 102, 102),
[b'^', b'x' | b'X', r1, r2, g1, g2, b1, b2] => {
Self::from_rgb(hex_byte(*r1, *r2), hex_byte(*g1, *g2), hex_byte(*b1, *b2))
}
_ => Self::WHITE,
}
}
pub fn set_clear_color(_: &Lua, _: ()) -> LuaResult<()> {
unimplemented!()
}
+390
View File
@@ -0,0 +1,390 @@
use crate::{
color::Srgba,
dpi::LogicalPoint,
fonts::{Alignment, FontStyle, LayoutJob, LayoutToLogical},
math::Point,
pob::Context,
};
use core::ffi::c_int;
use mlua::{
Lua, Result as LuaResult,
ffi::{self},
};
use parley::{FontFamily, FontFamilyName};
use regex::Regex;
use std::{borrow::Cow, sync::LazyLock};
macro_rules! str_from_stack {
($s:ident, $i:expr) => {
unsafe {
let mut size = 0;
let data = ffi::luaL_checklstring($s, $i, &mut size);
let bytes = std::slice::from_raw_parts(data as *const u8, size);
std::str::from_utf8_unchecked(bytes)
}
};
}
macro_rules! f32_from_stack {
($s:ident, $i:expr) => {
unsafe { ffi::luaL_checknumber($s, $i) } as f32
};
}
macro_rules! i32_from_stack {
($s:ident, $i:expr) => {
unsafe { ffi::luaL_checkinteger($s, $i) } as i32
};
}
pub fn strip_escapes(_: &Lua, text: String) -> LuaResult<String> {
Ok(PoBString(&text).strip_escapes())
}
pub unsafe extern "C-unwind" fn draw_string(state: *mut ffi::lua_State) -> c_int {
//profiling::scope!("draw_string");
let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) };
let mut ctx = lua_instance.app_data_mut::<Context>().unwrap();
let nargs = unsafe { ffi::lua_gettop(state) };
let x = f32_from_stack!(state, -nargs);
let y = f32_from_stack!(state, -nargs + 1);
let alignment = str_from_stack!(state, -nargs + 2);
let line_height = i32_from_stack!(state, -nargs + 3);
let font_type = str_from_stack!(state, -nargs + 4);
let text = str_from_stack!(state, -nargs + 5);
let Ok(alignment) = alignment.parse::<PoBTextAlignment>() else {
panic!("Invalid alignment");
};
let Ok(font_type) = font_type.parse::<PoBFontType>() else {
panic!("Invalid font type");
};
let mut position = LogicalPoint::new(x, y);
let mut is_absolute_position = false;
// the position needs to be adjusted for some alignments to match PoBs behavior
let screen_size = ctx.window_state.logical_size();
let halign = match alignment {
PoBTextAlignment::Left => Alignment::Min,
PoBTextAlignment::Center => {
position.x += screen_size.width / 2.0;
is_absolute_position = true;
Alignment::Center
}
PoBTextAlignment::Right => {
position.x = screen_size.width - position.x;
is_absolute_position = true;
Alignment::Max
}
PoBTextAlignment::CenterX => Alignment::Center,
PoBTextAlignment::RightX => Alignment::Max,
};
let current_draw_color = ctx.recorder.get_draw_color();
let job = build_layout_job(
text,
current_draw_color,
font_type,
line_height,
Some(halign),
);
// NOTE: color escape codes modify the current draw color.
// set current draw color to color of last segment to match PoB's behavior
if let Some(last_segment) = job.segments.last() {
ctx.recorder.set_draw_color(last_segment.color);
}
let scale_factor = ctx.window_state.scale_factor();
let layout = ctx.fonts.borrow_mut().layout(job, scale_factor);
// apply offset to account for anchor position
let position = position + layout.anchor_offset(halign);
// align layout anchor with the physical pixel grid to reduce blurriness
let position = (position * scale_factor).round() / scale_factor;
for glyph in &layout.glyphs {
ctx.recorder.draw_glyph(
LayoutToLogical::new(position.x, position.y).transform_box(&glyph.rect),
glyph.uv,
glyph.color,
glyph.layer_idx,
is_absolute_position,
);
}
0
}
pub unsafe extern "C-unwind" fn get_string_width(state: *mut ffi::lua_State) -> c_int {
//profiling::scope!("get_string_width");
let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) };
let ctx = lua_instance.app_data_ref::<Context>().unwrap();
let nargs = unsafe { ffi::lua_gettop(state) };
let line_height = i32_from_stack!(state, -nargs);
let font_type = str_from_stack!(state, -nargs + 1);
let text = str_from_stack!(state, -nargs + 2);
let Ok(font_type) = font_type.parse::<PoBFontType>() else {
panic!("Invalid font type");
};
let job = build_layout_job(text, Srgba::WHITE, font_type, line_height, None);
let layout = ctx
.fonts
.borrow_mut()
.layout(job, ctx.window_state.scale_factor());
let width = layout.width();
unsafe { ffi::lua_pushnumber(state, width as f64) };
1
}
pub fn get_index_at_cur(
l: &Lua,
(line_height, font_type, text, cur_x, cur_y): (i32, String, String, f32, f32),
) -> LuaResult<usize> {
//profiling::scope!("get_char_index_at_cur");
let ctx = l.app_data_ref::<Context>().unwrap();
let font_type = font_type.parse::<PoBFontType>()?;
let job = build_layout_job(&text, Srgba::WHITE, font_type, line_height, None);
let layout = ctx
.fonts
.borrow_mut()
.layout(job, ctx.window_state.scale_factor());
let index_stripped = layout.cursor_index_at(Point::new(cur_x, cur_y));
// build_layout_job() strips all color escape strings from the original string. The
// resulting [`LayoutJob`] is then passed to get_text_index_at_cursor() which returns an
// index into the **stripped* string.
// But PoB expects an index into the **original, unstripped** text. Therefore we need to add
// the length of all color escapes up until the cursor position to return the right value.
//
// TODO: avoid matching and iterating over string twice
let mut color_escapes_total_length = 0;
for capture in ESCAPE_STR_REGEX.find_iter(&text) {
if capture.start() - color_escapes_total_length > index_stripped {
break;
}
color_escapes_total_length += capture.len();
}
// add length of color escapes and convert to lua's 1-based indexing
Ok(index_stripped + color_escapes_total_length + 1)
}
pub static ESCAPE_STR_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\^(?<idx>[0-9])|\^[xX](?<hex>[0-9A-Fa-f]{6})").unwrap());
fn build_layout_job<'a>(
text: &'a str,
current_color: Srgba,
font_type: PoBFontType,
line_height: i32,
alignment: Option<Alignment>,
) -> LayoutJob<'a> {
let mut font_weight = None;
let mut font_style = FontStyle::default();
let font_family = match font_type {
PoBFontType::Fixed => FontFamily::Single(FontFamilyName::Named(Cow::Borrowed(
"Bitstream Vera Sans Mono",
))),
PoBFontType::Var => {
FontFamily::Single(FontFamilyName::Named(Cow::Borrowed("Liberation Sans")))
}
PoBFontType::VarBold => {
font_weight = Some(700.0);
FontFamily::Single(FontFamilyName::Named(Cow::Borrowed("Liberation Sans")))
}
PoBFontType::Fontin => FontFamily::Single(FontFamilyName::Named(Cow::Borrowed("Fontin"))),
PoBFontType::FontinItalic => {
FontFamily::Single(FontFamilyName::Named(Cow::Borrowed("Fontin")))
}
PoBFontType::FontinSmallcaps => {
FontFamily::Single(FontFamilyName::Named(Cow::Borrowed("Fontin SmallCaps")))
}
PoBFontType::FontinSmallcapsItalic => {
font_style = FontStyle::Italic;
// use regular Smallcaps with "faux italics"
FontFamily::Single(FontFamilyName::Named(Cow::Borrowed("Fontin SmallCaps")))
}
};
// NOTE: This is just an approximation and was chosen based on how it looks.
//
// PoB uses pre-rendered font atlases of discrete sizes and selects the appropriate
// atlas based on the provided height. Rusty-PoB dynamically renders fonts to a
// cached font atlas to support the selection of arbitrary sizes.
//
// TODO: font size in some dropdowns is too small, e.g. socket group selection in
// 'Calcs' tab
let font_size = (line_height - 2).max(1) as f32;
let mut job = LayoutJob::new(
font_family,
font_size,
line_height as f32,
alignment,
font_weight,
font_style,
);
for (color, segment) in PoBString(text) {
job.append(segment, color.unwrap_or(current_color));
}
job
}
// PoB strings can contain escape codes that affect the color of subsequent text
pub struct PoBString<'a>(pub &'a str);
impl<'a> PoBString<'a> {
pub fn strip_escapes(&self) -> String {
ESCAPE_STR_REGEX.replace_all(self.0, "").to_string()
}
}
type ColoredSegment<'a> = (Option<Srgba>, &'a str);
impl<'a> IntoIterator for PoBString<'a> {
type Item = ColoredSegment<'a>;
type IntoIter = PoBStringSegmentIterator<'a>;
fn into_iter(self) -> Self::IntoIter {
PoBStringSegmentIterator::new(self.0)
}
}
// Iterates over colored segments
pub struct PoBStringSegmentIterator<'a> {
haystack: &'a str,
captures: std::iter::Peekable<regex::CaptureMatches<'static, 'a>>,
is_first: bool,
is_done: bool,
}
impl<'a> PoBStringSegmentIterator<'a> {
fn new(haystack: &'a str) -> Self {
let captures = ESCAPE_STR_REGEX.captures_iter(haystack).peekable();
Self {
haystack,
captures,
is_first: true,
is_done: false,
}
}
}
impl<'a> Iterator for PoBStringSegmentIterator<'a> {
type Item = ColoredSegment<'a>;
fn next(&mut self) -> Option<Self::Item> {
let is_first = core::mem::replace(&mut self.is_first, false);
match self.captures.peek() {
Some(capture) => {
let code_start = capture.get(0).unwrap().start();
let code_end = capture.get(0).unwrap().end();
// string didn't start with an escape code.
// return text up to first code without color.
if is_first && code_start > 0 {
return Some((None, &self.haystack[..code_start]));
}
let escape_str = capture.get(0).unwrap().as_str();
let color = Some(Srgba::from_escape_code(escape_str));
let _ = self.captures.next(); // pop current code to peek the next one
if let Some(next_code) = self.captures.peek() {
// found another escape code. return text up the next code
let next_code_start = next_code.get(0).unwrap().start();
Some((color, &self.haystack[code_end..next_code_start]))
} else {
// no additional escape codes found. return rest of string
self.is_done = true;
Some((color, &self.haystack[code_end..]))
}
}
None => {
if self.is_done {
None
} else {
// string doesn't contain any escape codes.
// return entire string without color
self.is_done = true;
Some((None, self.haystack))
}
}
}
}
}
// PoB's alignment argument is weird!
// It controls:
// - the alignment within the text box
// - the anchor point of the box
// - the relative/absolute positioning of the box
#[derive(Clone, Copy, Debug)]
enum PoBTextAlignment {
// alignment: left | anchor: top-left corner | position: relative to viewport
Left,
// alignment: center | anchor: top-center | position: relative to center of screen
Center,
// alignment: right | anchor: top-right | position: relative to right edge of screen
Right,
// alignment: center | anchor: top-center | position: relative to viewport
CenterX,
// alignment: right | anchor: top-right corner | position: relative to viewport
RightX,
}
impl std::str::FromStr for PoBTextAlignment {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"LEFT" => Ok(Self::Left),
"CENTER" => Ok(Self::Center),
"RIGHT" => Ok(Self::Right),
"CENTER_X" => Ok(Self::CenterX),
"RIGHT_X" => Ok(Self::RightX),
_ => Err(anyhow::anyhow!("'{s}' is not a valid TextFontType variant")),
}
}
}
#[derive(Clone, Copy, Debug)]
enum PoBFontType {
Fixed,
Var,
VarBold,
FontinSmallcaps,
FontinSmallcapsItalic,
Fontin,
FontinItalic,
}
impl std::str::FromStr for PoBFontType {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"FIXED" => Ok(Self::Fixed),
"VAR" => Ok(Self::Var),
"VAR BOLD" => Ok(Self::VarBold),
"FONTIN SC" => Ok(Self::FontinSmallcaps),
"FONTIN SC ITALIC" => Ok(Self::FontinSmallcapsItalic),
"FONTIN" => Ok(Self::Fontin),
"FONTIN ITALIC" => Ok(Self::FontinItalic),
_ => Err(anyhow::anyhow!("'{s}' is not a valid TextFontType variant")),
}
}
}