//! General retained graphics resources for source paint: images, fonts, prepared //! text, geometric exclusions and numerical functions. No application behaviour. use crate::{ document::{RenderCommand, parse_color}, geometry::{Point, Rect}, render::{ image::Image, text_flow::{Cursor, Prepared}, }, text::{TextFamily, TextStyle}, }; use endor_core::{ ContextNativeFunction, NativeCallable, NativeContext, NativeResult, ObjectPayload, ObjectRef, ObjectStore, Value, Vm, VmError, }; use std::{any::Any, sync::Arc}; fn invalid(message: &'static str) -> VmError { VmError::InvalidArgument(message) } fn object(v: &Value) -> Result { if let Value::Object(o) = v { Ok(*o) } else { Err(invalid("expected Object")) } } fn number(v: &Value) -> Result { let Value::Number(v) = v else { return Err(invalid("expected Number")); }; let n = v.to_f64()?; if n.abs() > 1_000_000. { return Err(invalid("graphics number exceeds bounds")); } Ok(n as f32) } fn text(v: &Value) -> Result<&str, VmError> { if let Value::Text(s) = v { Ok(s) } else { Err(invalid("expected String")) } } fn n(v: f32) -> Value { Value::Number((v as f64).into()) } #[derive(Debug)] pub(crate) struct ImageResource(pub Arc); #[derive(Debug)] struct FontResource(TextFamily); #[derive(Debug)] struct TextResource { prepared: Arc, line: f32, } #[derive(Debug)] struct Exclusion { circle: Option<(Point, f32)>, polygon: Vec, padding: f32, } macro_rules! resource { ($t:ty,$name:literal) => { impl ObjectPayload for $t { fn as_any(&self) -> &dyn Any { self } fn kind_name(&self) -> &'static str { $name } fn allows_attributes(&self) -> bool { true } fn reference_assignment_only(&self) -> bool { true } } }; } resource!(ImageResource, "image-resource"); resource!(FontResource, "font-resource"); resource!(TextResource, "prepared-text"); resource!(Exclusion, "layout-exclusion"); fn attach(cx: &mut NativeContext<'_>, o: ObjectRef, desktop: ObjectRef) -> Result<(), VmError> { crate::ui_bridge::attach_desktop_type(cx, o, "Object", desktop) } fn file(path: &str, limit: u64) -> Result, VmError> { use std::io::Read; if path.len() > 8192 { return Err(invalid("resource path too long")); } let f = std::fs::File::open(path).map_err(|_| invalid("resource file cannot be opened"))?; let meta = f .metadata() .map_err(|_| invalid("resource metadata unavailable"))?; if !meta.is_file() || meta.len() > limit { return Err(invalid("resource must be a bounded regular file")); } let mut data = Vec::new(); f.take(limit + 1) .read_to_end(&mut data) .map_err(|_| invalid("resource read failed"))?; if data.len() as u64 > limit { return Err(invalid("resource exceeds byte limit")); } Ok(data) } fn field(s: &ObjectStore, o: ObjectRef, key: &str) -> Result { number(&s.get_attr(o, key)?) } pub(crate) fn decode_image(s: &ObjectStore, c: ObjectRef) -> Result { let resource = object(&s.get_attr(c, "image")?)?; let image = s .payload::(resource, "ImageResource")? .0 .clone(); let tint = match s.get_attr(c, "tint") { Ok(Value::Text(v)) => Some(parse_color(&v).map_err(|_| invalid("invalid image tint"))?), Ok(Value::Nil) | Err(VmError::AttributeNotFound(_)) => None, _ => return Err(invalid("invalid image tint")), }; let c = RenderCommand::Image { x: field(s, c, "x")?, y: field(s, c, "y")?, width: field(s, c, "width")?, height: field(s, c, "height")?, offset_x: field(s, c, "offsetX")?, offset_y: field(s, c, "offsetY")?, angle: field(s, c, "angle")?, opacity: field(s, c, "opacity")?, tint, image, }; c.validate() .map_err(|_| invalid("invalid image geometry"))?; Ok(c) } fn command( cx: &mut NativeContext<'_>, desktop: ObjectRef, image: ObjectRef, origin: Point, rect: Rect, angle: f32, opacity: f32, ) -> Result { let c = crate::native_visual::record( cx, desktop, "PaintCommand", vec![ ("kind", Value::Text("image".into())), ("image", Value::Object(image)), ("x", n(origin.x)), ("y", n(origin.y)), ("width", n(rect.width)), ("height", n(rect.height)), ("offsetX", n(rect.x)), ("offsetY", n(rect.y)), ("angle", n(angle)), ("opacity", n(opacity)), ], )?; Ok(c) } #[derive(Debug, Clone, Copy)] enum Op { LoadImage, LoadFont, ReadText, Slice, Stringify, Floor, Now, Prepare, Sprite, ImageExclusion, Circle, Rectangle, Flow, Float, Sin, Cos, Sqrt, Atan2, Abs, Min, Max, Clamp, } #[derive(Debug)] struct Function { op: Op, desktop: ObjectRef, } impl NativeCallable for Function { fn arity(&self) -> Option { use Op::*; Some(endor_core::CallArity::exact(match self.op { Now => 0, LoadImage | ReadText | Float | Sin | Cos | Sqrt | Abs | Stringify | Floor => 1, LoadFont | Atan2 | Min | Max | ImageExclusion => 2, Circle | Clamp | Slice => 3, Rectangle => 4, Prepare => 6, Flow => 7, Sprite => 9, })) } fn invoke(&self, cx: &mut NativeContext<'_>, args: &[Value]) -> Result { use Op::*; let desktop = self.desktop; let result = match self.op { Now => { if cx.store().is_read_only() { return Err(invalid( "sample the clock in an event/task and store it in the model", )); } static START: std::sync::OnceLock = std::sync::OnceLock::new(); Value::Number( START .get_or_init(std::time::Instant::now) .elapsed() .as_secs_f64() .into(), ) } LoadImage => { if cx.store().is_read_only() { return Err(invalid("load resources outside paint/layout")); } let image = Image::png(&file(text(&args[0])?, 4_194_304)?) .map_err(|_| invalid("invalid or oversized PNG"))?; let (w, h) = (image.width, image.height); let resource = cx.allocate(ImageResource(image))?; attach(cx, resource, desktop)?; cx.set_attr(resource, "width", n(w as f32))?; cx.set_attr(resource, "height", n(h as f32))?; Value::Object(resource) } LoadFont => { if cx.store().is_read_only() { return Err(invalid("load resources outside paint/layout")); } let family = text(&args[1])?; if family.len() > 128 { return Err(invalid("font family name too long")); } let font = crate::text::register_font(file(text(&args[0])?, 4_194_304)?, family) .map_err(|_| invalid("invalid font family or registry budget"))?; let resource = cx.allocate(FontResource(font))?; attach(cx, resource, desktop)?; Value::Object(resource) } ReadText => { if cx.store().is_read_only() { return Err(invalid("load resources outside paint/layout")); } Value::Text( String::from_utf8(file(text(&args[0])?, 16_384)?) .map_err(|_| invalid("resource is not UTF-8"))?, ) } Slice => { let value = text(&args[0])?; let start = number(&args[1])?; let end = number(&args[2])?; if start < 0. || end < start || start.fract() != 0. || end.fract() != 0. { return Err(invalid("invalid text slice")); } let lo = value .char_indices() .nth(start as usize) .map_or(value.len(), |(i, _)| i); let hi = value .char_indices() .nth(end as usize) .map_or(value.len(), |(i, _)| i); Value::Text(value[lo..hi].into()) } Stringify => Value::Text(match &args[0] { Value::Text(s) => s.clone(), Value::Number(v) => v.to_string(), Value::Bool(v) => v.to_string(), Value::Nil => "nil".into(), _ => return Err(invalid("string expects scalar value")), }), Prepare => { let value = text(&args[0])?; if value.len() > 16_384 { return Err(invalid("prepared text exceeds 16 KiB")); } let family = cx .store() .payload::(object(&args[1])?, "FontResource")? .0; let size = number(&args[2])?; let line = number(&args[3])?; let scale = number(&args[5])?; if !(8. ..=96.).contains(&size) || !(size..=192.).contains(&line) || !(0.5..=4.).contains(&scale) { return Err(invalid("invalid text metrics")); } let color = parse_color(text(&args[4])?).map_err(|_| invalid("invalid text color"))?; let prepared = Prepared::new( value, TextStyle { font_size: size, line_height: line, family, }, scale, color, ); let prepared = prepared.map_err(invalid)?; let resource = cx.allocate(TextResource { prepared: Arc::new(prepared), line, })?; attach(cx, resource, desktop)?; Value::Object(resource) } Sprite => { let image = object(&args[0])?; cx.store() .payload::(image, "ImageResource")?; let x = number(&args[1])?; let y = number(&args[2])?; let w = number(&args[3])?; let h = number(&args[4])?; let a = number(&args[5])?; let ax = number(&args[6])?; let ay = number(&args[7])?; let opacity = number(&args[8])?; let c = command( cx, desktop, image, Point::new(x, y), Rect::new(-w * ax, -h * ay, w, h), a, opacity, )?; decode_image(cx.store(), c)?; Value::Object(c) } ImageExclusion => { let RenderCommand::Image { x, y, width, height, offset_x, offset_y, angle, image, .. } = decode_image(cx.store(), object(&args[0])?)? else { unreachable!() }; let padding = number(&args[1])?; if !(0. ..=1000.).contains(&padding) { return Err(invalid("invalid exclusion padding")); } let (sin, cos) = angle.sin_cos(); let polygon = image .outline .iter() .map(|p| { let px = offset_x + p.x / image.width as f32 * width; let py = offset_y + p.y / image.height as f32 * height; Point::new(x + cos * px - sin * py, y + sin * px + cos * py) }) .collect(); let e = cx.allocate(Exclusion { circle: None, polygon, padding, })?; attach(cx, e, desktop)?; Value::Object(e) } Circle | Rectangle => { let x = number(&args[0])?; let y = number(&args[1])?; let size = number(&args[2])?; if size < 0. { return Err(invalid("negative exclusion extent")); } let e = if matches!(self.op, Circle) { Exclusion { circle: Some((Point::new(x, y), size)), polygon: vec![], padding: 0., } } else { let h = number(&args[3])?; if h < 0. { return Err(invalid("negative exclusion height")); } Exclusion { circle: None, polygon: vec![ Point::new(x, y), Point::new(x + size, y), Point::new(x + size, y + h), Point::new(x, y + h), ], padding: 0., } }; let e = cx.allocate(e)?; attach(cx, e, desktop)?; Value::Object(e) } Flow => flow(cx, desktop, args)?, _ => { let real = |v: &Value| -> Result { if let Value::Number(n) = v { n.to_f64() } else { Err(invalid("math expects Number")) } }; let x = real(&args[0])?; if matches!(self.op, Floor) { if x.abs() > 1_000_000_000. { return Err(invalid("floor result exceeds integer conversion budget")); } return Ok(NativeResult::Return(Value::Number( (x.floor() as i64).into(), ))); } let v = match self.op { Float => x, Sin => x.sin(), Cos => x.cos(), Sqrt => x.sqrt(), Atan2 => x.atan2(real(&args[1])?), Abs => x.abs(), Min => x.min(real(&args[1])?), Max => x.max(real(&args[1])?), Clamp => { let lo = real(&args[1])?; let hi = real(&args[2])?; if lo > hi { return Err(invalid("clamp minimum exceeds maximum")); } x.clamp(lo, hi) } _ => unreachable!(), }; if !v.is_finite() { return Err(invalid("non-finite math result")); } Value::Number(v.into()) } }; Ok(NativeResult::Return(result)) } } fn ranges(left: f32, right: f32, y: f32, line: f32, exclusions: &[&Exclusion]) -> Vec<(f32, f32)> { let mut blocked = Vec::new(); for e in exclusions { if let Some((p, r)) = e.circle { let gap = (y - p.y).max(p.y - y - line).max(0.); if gap < r { let dx = (r * r - gap * gap).sqrt(); blocked.push((p.x - dx, p.x + dx)); } } let (top, bottom) = (y - e.padding, y + line + e.padding); let mut xs = Vec::new(); for i in 0..e.polygon.len() { let a = e.polygon[i]; let b = e.polygon[(i + 1) % e.polygon.len()]; if a.y >= top && a.y <= bottom { xs.push(a.x); } for edge in [top, bottom] { if (a.y < edge && b.y > edge) || (b.y < edge && a.y > edge) { xs.push(a.x + (b.x - a.x) * (edge - a.y) / (b.y - a.y)); } } } if !xs.is_empty() { blocked.push(( xs.iter().copied().fold(f32::INFINITY, f32::min) - e.padding, xs.iter().copied().fold(f32::NEG_INFINITY, f32::max) + e.padding, )); } } blocked.sort_by(|a, b| a.0.total_cmp(&b.0)); let mut x = left; let mut ranges = Vec::new(); for (lo, hi) in blocked { if hi <= left || lo >= right { continue; } if lo > x { ranges.push((x, lo.min(right))); } x = x.max(hi).min(right); } if x < right { ranges.push((x, right)); } ranges.into_iter().filter(|(l, r)| r - l >= 8.).collect() } fn flow(cx: &mut NativeContext<'_>, desktop: ObjectRef, args: &[Value]) -> Result { let resource = object(&args[0])?; let text = cx .store() .payload::(resource, "PreparedText")?; let prepared = text.prepared.clone(); let line = text.line; let x = number(&args[1])?; let y = number(&args[2])?; let width = number(&args[3])?; let height = number(&args[4])?; let letters = number(&args[6])? != 0.; if width < 0. || height < 0. || height / line > 1024. { return Err(invalid("text flow bounds exceed budget")); } let items = crate::object_list::items(cx.store(), object(&args[5])?)?; if items.len() > 512 { return Err(invalid("text flow exceeds 512 exclusions")); } let exclusions = items .iter() .map(|&o| cx.store().payload::(o, "Exclusion")) .collect::, _>>()?; let mut placements = Vec::new(); let mut cursor = Cursor::default(); let mut top = y; while top + line <= y + height && cursor.word < prepared.words.len() { let intervals = ranges(x, x + width, top, line, &exclusions); prepared.row(&mut cursor, &intervals, top, &mut placements); top += line; } let mut commands = Vec::new(); for p in placements { let word = &prepared.words[p.word]; let inks = if let Some(i) = p.letter { std::slice::from_ref(&word.letters[i]) } else if letters { &word.letters[..] } else { std::slice::from_ref(&word.ink) }; let mut px = p.x; for ink in inks { let image = cx.allocate(ImageResource(ink.image.clone()))?; attach(cx, image, desktop)?; commands.push(command( cx, desktop, image, Point::new(px, p.y), ink.rect, 0., 1., )?); px += ink.width; if commands.len() > 10_000 { return Err(invalid("text flow command budget exceeded")); } } } let commands = crate::object_list::collection(cx, commands, desktop)?; Ok(Value::Object(crate::native_visual::record( cx, desktop, "DisplayList", vec![("commands", Value::Object(commands))], )?)) } pub(crate) fn install( vm: &mut Vm, roots: &mut Vec, ui: ObjectRef, desktop: ObjectRef, ) -> Result<(), VmError> { let graphics = vm.new_object()?; roots.push(graphics); attach_vm(vm, graphics, "Object", desktop)?; let math = vm.new_object()?; roots.push(math); attach_vm(vm, math, "Object", desktop)?; for (owner, name, op) in [ (graphics, "loadImage", Op::LoadImage), (graphics, "loadFont", Op::LoadFont), (graphics, "readText", Op::ReadText), (graphics, "textSlice", Op::Slice), (graphics, "string", Op::Stringify), (math, "floor", Op::Floor), (math, "now", Op::Now), (graphics, "prepareText", Op::Prepare), (graphics, "sprite", Op::Sprite), (graphics, "imageExclusion", Op::ImageExclusion), (graphics, "circleExclusion", Op::Circle), (graphics, "rectExclusion", Op::Rectangle), (graphics, "flowText", Op::Flow), (math, "float", Op::Float), (math, "sin", Op::Sin), (math, "cos", Op::Cos), (math, "sqrt", Op::Sqrt), (math, "atan2", Op::Atan2), (math, "abs", Op::Abs), (math, "min", Op::Min), (math, "max", Op::Max), (math, "clamp", Op::Clamp), ] { let f = vm.allocate(ContextNativeFunction::new( name, Function { op, desktop }, vec![desktop], ))?; roots.push(f); attach_vm(vm, f, "Function", desktop)?; vm.set_attr(owner, name, Value::Object(f))?; } vm.set_attr(math, "pi", Value::Number(std::f64::consts::PI.into()))?; vm.set_attr(graphics, "math", Value::Object(math))?; vm.set_attr(ui, "graphics", Value::Object(graphics))?; Ok(()) } fn attach_vm(vm: &mut Vm, o: ObjectRef, name: &str, desktop: ObjectRef) -> Result<(), VmError> { let t = crate::ui_bridge::desktop_type(vm.object_store(), name, desktop)?; vm.attach_type(o, t) } #[derive(Debug)] pub(crate) struct PointerDispatch { pub owner: ObjectRef, pub mount: ObjectRef, pub desktop: ObjectRef, pub kind: String, pub point: Point, } impl NativeCallable for PointerDispatch { fn arity(&self) -> Option { Some(endor_core::CallArity::exact(0)) } fn invoke(&self, cx: &mut NativeContext<'_>, _: &[Value]) -> Result { let data = cx .store() .payload::(self.mount, "Mount")?; let g = &data.geometry; if !data.mounted || !g.pointer_events { return Err(invalid( "pointer target is detached or has no pointer subscriptions", )); } let local = g .view_point(self.point, false) .map(|p| g.view.unwrap_or_default().inverse(p)) .ok_or(invalid("pointer cannot be projected"))?; let (x, y) = (local.x - g.x, local.y - g.y); let definition = crate::ui_bridge::desktop_type(cx.store(), "Event", self.desktop)?; let event = crate::event_dispatch::new_event(cx, definition, &self.kind)?; cx.set_attr(event, "x", n(x))?; cx.set_attr(event, "y", n(y))?; crate::event_dispatch::dispatch(cx, self.owner, event, Some(self.mount), self.desktop) } }