Add image support

This commit is contained in:
Isaac Mills 2024-04-19 10:47:11 -04:00
parent ee9ff8e2d6
commit dfd8e5d86c
Signed by: fnmain
GPG key ID: B67D7410F33A0F61

View file

@ -42,7 +42,7 @@ pub struct Indent {
pub indent: f32,
}
pub const INDENT_AMOUNT: f32 = 64.0;
pub const INDENT_AMOUNT: f32 = 32.0;
impl<'a, 'b, T: Iterator<Item = Event<'a>>> Iterator for JotdownIntoBuffer<'a, 'b, T> {
type Item = (&'a str, Attrs<'static>);
@ -120,6 +120,7 @@ impl<'a, 'b, T: Iterator<Item = Event<'a>>> Iterator for JotdownIntoBuffer<'a, '
modifier: Some(kind),
}),
Container::Image(ref url, _) => {
self.added = true;
self.image_url = Some(url.clone());
self.top_level_container.get_or_insert(container);
}
@ -136,7 +137,7 @@ impl<'a, 'b, T: Iterator<Item = Event<'a>>> Iterator for JotdownIntoBuffer<'a, '
Container::List { .. } => {
self.indent.pop();
}
Container::Heading { .. } | Container::Paragraph => {
Container::Heading { .. } | Container::Paragraph | Container::Image(_, _) => {
if self.added {
return None;
}
@ -229,25 +230,24 @@ pub fn resolve_paragraphs<'a>(
let split = url.0.rsplit_once('.').unwrap();
let hi_image = format!("{}/{}_hi.{}", base_uri, split.0, split.1);
buffer.image_urls = Some((image, hi_image));
buffer.relative_bounds = Rect::from_min_size(
Pos2::new(buffer.indent.indent, size.y + margin_top),
image_size,
);
log::info!("{:?}", buffer.image_urls);
buffer.relative_bounds =
Rect::from_min_size(Pos2::new(buffer.indent.indent, size.y), image_size);
const IMAGE_PADDING: f32 = 8.0;
if let Some(last_size) = last_image_size.as_mut() {
let ls = *last_size;
last_size.x += image_size.x + IMAGE_PADDING;
if last_size.x > viewbox.x {
size.y += last_size.y + margin_top;
size.y += last_size.y;
last_size.x = image_size.x + IMAGE_PADDING;
last_size.y = image_size.y + margin_top;
last_size.y = image_size.y + margin;
buffer.relative_bounds = Rect::from_min_size(
Pos2::new(buffer.indent.indent, size.y + margin_top),
Pos2::new(buffer.indent.indent, size.y),
image_size,
);
} else {
last_size.y = last_size.y.max(image_size.y + margin_top);
last_size.y = last_size.y.max(image_size.y + margin);
buffer.relative_bounds =
buffer.relative_bounds.translate(Vec2::new(ls.x, 0.0));
}
@ -255,19 +255,22 @@ pub fn resolve_paragraphs<'a>(
if image_size.x > viewbox.x {
let max_size = Vec2::new(viewbox.x, image_size.y);
let new_size = scale_to_fit(image_size, max_size, true);
buffer.relative_bounds = Rect::from_min_size(
Pos2::new(buffer.indent.indent, size.y + margin_top),
new_size,
);
size.y += new_size.y + margin_top;
buffer.relative_bounds =
Rect::from_min_size(Pos2::new(buffer.indent.indent, size.y), new_size);
size.y += new_size.y
} else {
last_image_size = Some(image_size + Vec2::new(IMAGE_PADDING, margin_top));
last_image_size = Some(image_size + Vec2::new(IMAGE_PADDING, margin));
}
}
[Some(buffer), None]
} else if let Some(mut list_kind) = buffer.indent.modifier {
if let Some(image_size) = last_image_size {
size.y += image_size.y;
buffer.relative_bounds = buffer
.relative_bounds
.translate(Vec2::new(0.0, image_size.y));
size.x = size.x.max(image_size.x);
last_image_size = None;
}
if let ListKind::Ordered { start, .. } = &mut list_kind {
@ -297,7 +300,7 @@ pub fn resolve_paragraphs<'a>(
let list_buffer_metrics = buffer.metrics;
let indent = (buffer_indent) - (INDENT_AMOUNT * factor);
[
let res = [
Some(buffer),
Some(ResolvedJotdownItem {
indent: Indent {
@ -314,21 +317,31 @@ pub fn resolve_paragraphs<'a>(
url_map: None,
image_urls: None,
}),
]
];
size.y += buffer_size.y + (margin_top + margin);
last_margin = margin;
first = false;
size.x = size.x.max(buffer_size.x + buffer_indent);
res
} else {
if let Some(image_size) = last_image_size {
size.y += image_size.y;
buffer.relative_bounds = buffer
.relative_bounds
.translate(Vec2::new(0.0, image_size.y));
size.x = size.x.max(image_size.x);
last_image_size = None;
}
in_list = false;
size.y += buffer_size.y + (margin_top + margin);
last_margin = margin;
first = false;
size.x = size.x.max(buffer_size.x + buffer_indent);
[Some(buffer), None]
};
size.y += buffer_size.y + (margin_top + margin);
last_margin = margin;
first = false;
size.x = size.x.max(buffer_size.x + buffer_indent);
result_buffers
})
.filter_map(|p| p)