mod advanced;
mod manager;
mod tools;
use core::fmt;
use crate::data::{TimeIndex, Timestamp};
use crate::format::NumberFormat;
use crate::frame::{
DrawCmd, FontId, FontStyleRef, Frame, IconInst, LabelBg, LayerId, LineDash, LineInst, RectInst,
StrokeStyle, TextAlign, TextRun,
};
use crate::geom::{Css, PointPx, Px, RectPx, Rgba, Span};
use crate::price_scale::{Normalize, PriceScale};
use crate::text::{TextMeasurer, text_width};
use crate::time_scale::TimeScale;
pub(crate) use manager::LocalChange;
pub use manager::{DrawingFactory, DrawingManager};
pub const BUILTIN_KINDS: [&str; 29] = [
"trend_line",
"ray",
"extended_line",
"horizontal_line",
"horizontal_ray",
"vertical_line",
"parallel_channel",
"rectangle",
"ellipse",
"text",
"price_range",
"date_range",
"fib_retracement",
"fib_extension",
"long_position",
"short_position",
"pitchfork",
"schiff_pitchfork",
"modified_schiff_pitchfork",
"gann_fan",
"fib_time_zones",
"xabcd_pattern",
"head_and_shoulders",
"elliott_impulse",
"elliott_correction",
"path",
"brush",
"arrow",
"callout",
];
pub(crate) fn builtin(data: &DrawingData) -> crate::error::Result<Box<dyn DrawingTool>> {
if advanced::SECOND_KINDS.contains(&data.kind.as_str()) {
advanced::build(data)
} else {
tools::builtin(data)
}
}
const DEFAULT_STEP_MS: f64 = 60_000.0;
pub(crate) const HIT_CSS: f32 = 4.0;
pub const MAX_ANCHORS: usize = 4096;
pub(crate) const FREEHAND_STEP_CSS: f32 = 3.0;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum AnchorCount {
Fixed(usize),
Open {
min: usize,
},
Freehand,
}
impl AnchorCount {
pub fn min(self) -> usize {
match self {
Self::Fixed(n) | Self::Open { min: n } => n,
Self::Freehand => 2,
}
}
pub(crate) fn max(self) -> usize {
match self {
Self::Fixed(n) => n,
Self::Open { .. } | Self::Freehand => MAX_ANCHORS,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Anchor {
pub time: Timestamp,
pub price: f64,
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DrawingStyle {
pub line: Rgba,
pub line_width: Css,
pub line_dash: LineDash,
pub fill: Rgba,
pub text: Rgba,
pub font_size: Css,
}
impl Default for DrawingStyle {
fn default() -> Self {
let blue = Rgba::hex(0x0029_62FF);
Self {
line: blue,
line_width: Css(1.0),
line_dash: LineDash::Solid,
fill: blue.with_alpha(40),
text: blue,
font_size: Css(12.0),
}
}
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Param {
Number(f64),
Bool(bool),
Text(String),
Numbers(Vec<f64>),
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DrawingData {
pub kind: String,
pub anchors: Vec<Anchor>,
pub style: DrawingStyle,
pub params: Vec<(String, Param)>,
}
impl DrawingData {
pub fn new(kind: &str, anchors: Vec<Anchor>) -> Self {
Self {
kind: kind.into(),
anchors,
style: DrawingStyle::default(),
params: Vec::new(),
}
}
pub fn param(&self, key: &str) -> Option<&Param> {
self.params.iter().find(|(k, _)| k == key).map(|(_, v)| v)
}
pub fn set(&mut self, key: &str, value: Param) {
match self.params.iter_mut().find(|(k, _)| k == key) {
Some((_, v)) => *v = value,
None => self.params.push((key.into(), value)),
}
}
pub fn number(&self, key: &str) -> Option<f64> {
match self.param(key)? {
Param::Number(x) => Some(*x),
_ => None,
}
}
pub fn text(&self, key: &str) -> Option<&str> {
match self.param(key)? {
Param::Text(s) => Some(s),
_ => None,
}
}
pub fn numbers(&self, key: &str) -> Option<&[f64]> {
match self.param(key)? {
Param::Numbers(v) => Some(v),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum HitPart {
Handle(usize),
Body,
}
pub trait DrawingTool: Send {
fn kind(&self) -> &'static str;
fn anchors(&self) -> &[Anchor];
fn anchors_mut(&mut self) -> &mut Vec<Anchor>;
fn anchor_count(&self) -> AnchorCount;
fn build(&self, ctx: &DrawingCtx<'_>, out: &mut FrameBuilder<'_>);
fn hit_test(&self, ctx: &DrawingCtx<'_>, p: PointPx) -> Option<HitPart>;
fn handles(&self, ctx: &DrawingCtx<'_>, out: &mut Vec<PointPx>);
fn style(&self) -> &DrawingStyle;
fn save(&self) -> DrawingData;
fn move_handle(&mut self, handle: usize, to: Anchor) {
if let Some(a) = self.anchors_mut().get_mut(handle) {
*a = to;
}
}
fn finish(&mut self) {}
}
pub struct DrawingCtx<'a> {
pub(crate) index: &'a TimeIndex,
pub(crate) time_scale: &'a TimeScale,
pub(crate) price_scale: &'a PriceScale,
pub(crate) norm: Normalize,
pub(crate) plot: RectPx,
pub(crate) dpr: f32,
pub(crate) labels: NumberFormat,
pub(crate) text: &'a dyn TextMeasurer,
}
impl DrawingCtx<'_> {
pub fn plot(&self) -> RectPx {
self.plot
}
pub fn dpr(&self) -> f32 {
self.dpr
}
pub fn px(&self, v: Css) -> Px {
v.to_px(self.dpr)
}
pub fn hit_tolerance(&self) -> f32 {
HIT_CSS * self.dpr
}
pub fn bar_spacing(&self) -> Px {
Px(self.time_scale.bar_spacing() as f32)
}
pub fn index_of_time(&self, time: Timestamp) -> Option<f64> {
index_of_time(self.index, time)
}
pub(crate) fn x_of_index(&self, i: f64) -> Option<f32> {
let x = f64::from(self.plot.x) + self.time_scale.index_to_x_f64(i);
x.is_finite().then_some(x.clamp(-1e7, 1e7) as f32)
}
pub fn x_of(&self, time: Timestamp) -> Option<f32> {
let i = self.index_of_time(time)?;
let x = f64::from(self.plot.x) + self.time_scale.index_to_x_f64(i);
x.is_finite().then_some(x as f32)
}
pub fn y_of(&self, price: f64) -> Option<f32> {
let (top, h) = (f64::from(self.plot.y), f64::from(self.plot.h));
let y = self.price_scale.price_to_y(self.norm.apply(price), top, h);
y.is_finite().then_some(y.clamp(-1e7, 1e7) as f32)
}
pub fn price_at(&self, y: f32) -> f64 {
let (top, h) = (f64::from(self.plot.y), f64::from(self.plot.h));
self.norm
.invert(self.price_scale.y_to_price(f64::from(y), top, h))
}
pub fn time_at(&self, x: f32) -> Option<Timestamp> {
let i = self.time_scale.x_to_index(Px(x - self.plot.x));
time_of_index(self.index, i)
}
pub fn point(&self, a: Anchor) -> Option<PointPx> {
Some(PointPx {
x: self.x_of(a.time)?.clamp(-1e7, 1e7),
y: self.y_of(a.price)?,
})
}
pub fn stroke(&self, style: &DrawingStyle) -> StrokeStyle {
StrokeStyle {
width: Px(style.line_width.to_px(self.dpr).0.round().max(1.0)),
color: style.line,
dash: style.line_dash,
}
}
pub fn font(&self, size: Css) -> FontStyleRef {
FontStyleRef {
font: FontId(0),
size: size.to_px(self.dpr).0,
}
}
pub fn text_width(&self, text: &str, font: FontStyleRef) -> f32 {
text_width(self.text, text, font)
}
pub fn line_height(&self, font: FontStyleRef) -> f32 {
self.text.line_height(font)
}
pub fn write_price(&self, out: &mut impl fmt::Write, price: f64) {
match self.labels {
f @ (NumberFormat::Fixed(_) | NumberFormat::Auto(_)) => f,
NumberFormat::Percent(_) | NumberFormat::Compact => NumberFormat::Fixed(2),
}
.write(out, price);
}
}
pub struct FrameBuilder<'a> {
pub(crate) frame: &'a mut Frame,
pub(crate) layer: LayerId,
}
impl<'a> FrameBuilder<'a> {
pub fn new(frame: &'a mut Frame, layer: LayerId) -> Self {
Self { frame, layer }
}
pub fn line(&mut self, from: PointPx, to: PointPx, stroke: StrokeStyle) {
self.frame
.push_line(self.layer, LineInst { from, to, stroke });
}
fn points(&mut self, pts: impl IntoIterator<Item = PointPx>) -> Span {
let start = self.frame.pools.points.len();
self.frame.pools.points.extend(pts);
let len = self.frame.pools.points.len() - start;
Span {
start: u32::try_from(start).unwrap_or(u32::MAX),
len: u32::try_from(len).unwrap_or(0),
}
}
pub fn polyline(&mut self, pts: impl IntoIterator<Item = PointPx>, style: StrokeStyle) {
let points = self.points(pts);
if points.len >= 2 {
self.frame
.push_cmd(self.layer, DrawCmd::Polyline { points, style });
}
}
pub fn polygon(&mut self, pts: impl IntoIterator<Item = PointPx>, fill: Rgba) {
let points = self.points(pts);
if points.len >= 3 && fill.a > 0 {
self.frame
.push_cmd(self.layer, DrawCmd::Polygon { points, fill });
}
}
pub fn rect(&mut self, rect: RectPx, color: Rgba) {
if color.a > 0 && rect.w > 0.0 && rect.h > 0.0 {
self.frame.push_rect(self.layer, RectInst { rect, color });
}
}
pub fn text(&mut self, pos: PointPx, text: &str, label: Label) {
let span = self.frame.push_str(text);
self.push_run(pos, span, label);
}
pub fn text_fmt(&mut self, pos: PointPx, args: fmt::Arguments<'_>, label: Label) {
let start = self.frame.pools.text.len();
let _ = fmt::Write::write_fmt(&mut self.frame.pools.text, args);
let len = self.frame.pools.text.len() - start;
let span = Span {
start: u32::try_from(start).unwrap_or(u32::MAX),
len: u32::try_from(len).unwrap_or(0),
};
self.push_run(pos, span, label);
}
fn push_run(&mut self, pos: PointPx, text: Span, label: Label) {
self.frame.push_text(
self.layer,
TextRun {
pos,
text,
style: label.font,
color: label.color,
align: label.align,
bg: label.bg,
},
);
}
pub fn icon(&mut self, icon: IconInst) {
self.frame.push_icon(self.layer, icon);
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Label {
pub font: FontStyleRef,
pub color: Rgba,
pub align: TextAlign,
pub bg: Option<LabelBg>,
}
pub fn distance_to_segment(p: PointPx, a: PointPx, b: PointPx) -> f32 {
let (dx, dy) = (b.x - a.x, b.y - a.y);
let len2 = dx * dx + dy * dy;
let t = if len2 > 0.0 {
(((p.x - a.x) * dx + (p.y - a.y) * dy) / len2).clamp(0.0, 1.0)
} else {
0.0
};
(p.x - (a.x + t * dx)).hypot(p.y - (a.y + t * dy))
}
pub fn clip_line(
a: PointPx,
b: PointPx,
rect: RectPx,
back: bool,
forward: bool,
) -> Option<(PointPx, PointPx)> {
let (dx, dy) = (b.x - a.x, b.y - a.y);
if dx == 0.0 && dy == 0.0 {
return Some((a, b));
}
let mut t0 = if back { f32::NEG_INFINITY } else { 0.0 };
let mut t1 = if forward { f32::INFINITY } else { 1.0 };
for (p, q) in [
(-dx, a.x - rect.x),
(dx, rect.right() - a.x),
(-dy, a.y - rect.y),
(dy, rect.bottom() - a.y),
] {
if p == 0.0 {
if q < 0.0 {
return None;
}
continue;
}
let r = q / p;
if p < 0.0 {
t0 = t0.max(r);
} else {
t1 = t1.min(r);
}
}
if t0 > t1 || !t0.is_finite() || !t1.is_finite() {
return None;
}
let at = |t: f32| PointPx {
x: a.x + t * dx,
y: a.y + t * dy,
};
Some((at(t0), at(t1)))
}
pub fn point_in_polygon(p: PointPx, pts: &[PointPx]) -> bool {
let mut inside = false;
let mut j = pts.len().wrapping_sub(1);
for (i, a) in pts.iter().enumerate() {
let Some(b) = pts.get(j) else {
return false;
};
if (a.y > p.y) != (b.y > p.y) && p.x < (b.x - a.x) * (p.y - a.y) / (b.y - a.y) + a.x {
inside = !inside;
}
j = i;
}
inside
}
fn step(index: &TimeIndex, at_end: bool) -> f64 {
let (first, end) = (index.first_index(), index.end_index());
let pair = if at_end {
(index.time_at(end - 2), index.time_at(end - 1))
} else {
(index.time_at(first), index.time_at(first + 1))
};
match pair {
(Some(a), Some(b)) if b > a => (b - a) as f64,
_ => DEFAULT_STEP_MS,
}
}
pub(crate) fn index_of_time(index: &TimeIndex, time: Timestamp) -> Option<f64> {
if index.is_empty() {
return None;
}
let (first, last) = (index.first_index(), index.end_index() - 1);
let (t0, t1) = (index.time_at(first)?, index.time_at(last)?);
if time > t1 {
return Some(last as f64 + (time - t1) as f64 / step(index, true));
}
if time < t0 {
return Some(first as f64 - (t0 - time) as f64 / step(index, false));
}
let j = index.lower_bound(time);
let tj = index.time_at(j)?;
if tj == time {
return Some(j as f64);
}
let tp = index.time_at(j - 1)?;
Some((j - 1) as f64 + (time - tp) as f64 / (tj - tp) as f64)
}
pub(crate) fn time_of_index(index: &TimeIndex, i: f64) -> Option<Timestamp> {
if index.is_empty() || !i.is_finite() {
return None;
}
let (first, last) = (index.first_index(), index.end_index() - 1);
let (t0, t1) = (index.time_at(first)?, index.time_at(last)?);
let t = if i >= last as f64 {
t1 as f64 + (i - last as f64) * step(index, true)
} else if i <= first as f64 {
t0 as f64 - (first as f64 - i) * step(index, false)
} else {
let j = i.floor() as i64;
let (a, b) = (index.time_at(j)?, index.time_at(j + 1)?);
a as f64 + (i - j as f64) * (b - a) as f64
};
Some(t.round().clamp(-9.0e18, 9.0e18) as i64)
}
pub(crate) fn crisp(v: f32, width: Px) -> f32 {
if (width.0 as i32) % 2 == 1 {
v.floor() + 0.5
} else {
v.round()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn times_map_to_indices_and_back() {
let index = TimeIndex::from_times(vec![100, 200, 400]).unwrap_or_default();
assert_eq!(index_of_time(&index, 200), Some(1.0));
assert_eq!(index_of_time(&index, 300), Some(1.5));
assert_eq!(index_of_time(&index, 800), Some(4.0));
assert_eq!(index_of_time(&index, 0), Some(-1.0));
for i in [-3.0, -0.5, 0.0, 1.25, 2.0, 7.0] {
let t = time_of_index(&index, i).unwrap_or_default();
let back = index_of_time(&index, t).unwrap_or_default();
assert!((back - i).abs() < 1e-9, "{i} -> {t} -> {back}");
}
assert_eq!(index_of_time(&TimeIndex::default(), 5), None);
}
#[test]
fn lines_are_clipped_to_the_plot() {
let r = RectPx {
x: 0.0,
y: 0.0,
w: 100.0,
h: 50.0,
};
let (a, b) = (PointPx { x: 10.0, y: 10.0 }, PointPx { x: 20.0, y: 20.0 });
let (p, q) = clip_line(a, b, r, true, true).unwrap_or((a, a));
assert_eq!(
(p, q),
(PointPx { x: 0.0, y: 0.0 }, PointPx { x: 50.0, y: 50.0 })
);
assert_eq!(clip_line(a, b, r, false, false), Some((a, b)));
let outside = PointPx { x: 200.0, y: 10.0 };
assert_eq!(
clip_line(outside, PointPx { x: 300.0, y: 10.0 }, r, false, true),
None
);
}
#[test]
fn polygons_contain_points() {
let sq = [
PointPx { x: 0.0, y: 0.0 },
PointPx { x: 10.0, y: 0.0 },
PointPx { x: 10.0, y: 10.0 },
PointPx { x: 0.0, y: 10.0 },
];
assert!(point_in_polygon(PointPx { x: 5.0, y: 5.0 }, &sq));
assert!(!point_in_polygon(PointPx { x: 15.0, y: 5.0 }, &sq));
assert!(!point_in_polygon(PointPx { x: 5.0, y: 5.0 }, &[]));
}
}