Update to allow non-buffer return

This commit is contained in:
Isaac Mills 2024-04-10 13:06:58 -04:00
parent 75467e2ddf
commit c82248831b
Signed by: fnmain
GPG key ID: B67D7410F33A0F61

View file

@ -6,9 +6,8 @@ use jotdown::{Container, Event, ListKind};
pub use jotdown;
use range_map::RangeMap;
pub struct JotdownBufferIter<'a, 'b, T: Iterator<Item = Event<'a>>> {
pub struct JotdownBufferIter<'a, T: Iterator<Item = Event<'a>>> {
djot: T,
font_system: &'b mut FontSystem,
width: f32,
metrics: Metrics,
indent: Vec<Indent>,
@ -16,7 +15,7 @@ pub struct JotdownBufferIter<'a, 'b, T: Iterator<Item = Event<'a>>> {
struct JotdownIntoBuffer<'a, 'b, T: Iterator<Item = Event<'a>>> {
djot: &'b mut T,
attrs: Attrs<'a>,
attrs: Attrs<'static>,
metrics: Metrics,
indent: &'b mut Vec<Indent>,
image_url: Option<Cow<'a, str>>,
@ -35,7 +34,7 @@ pub struct Indent {
pub const INDENT_AMOUNT: f32 = 18.0;
impl<'a, 'b, T: Iterator<Item = Event<'a>>> Iterator for JotdownIntoBuffer<'a, 'b, T> {
type Item = (&'a str, Attrs<'a>);
type Item = (&'a str, Attrs<'static>);
fn next(&mut self) -> Option<Self::Item> {
while let Some(event) = self.djot.next() {
@ -138,17 +137,34 @@ impl<'a, 'b, T: Iterator<Item = Event<'a>>> Iterator for JotdownIntoBuffer<'a, '
pub struct JotdownItem<'a> {
pub indent: Indent,
pub buffer: Buffer,
pub buffer: Vec<(&'a str, Attrs<'static>)>,
pub metrics: Metrics,
pub image_url: Option<Cow<'a, str>>,
pub url_map: Option<RangeMap<usize, &'a str>>,
pub width: f32,
}
impl<'a, 'b, T: Iterator<Item = Event<'a>>> Iterator for JotdownBufferIter<'a, 'b, T> {
impl<'a> JotdownItem<'a> {
pub fn make_buffer(&self, font_system: &mut FontSystem) -> Buffer {
let mut buffer = Buffer::new(font_system, self.metrics);
buffer.set_rich_text(
font_system,
self.buffer.iter().cloned(),
Attrs::new().family(Family::SansSerif),
Shaping::Advanced,
);
buffer.set_wrap(font_system, cosmic_text::Wrap::WordOrGlyph);
buffer.set_size(font_system, self.width - self.indent.indent, f32::MAX);
buffer.shape_until_scroll(font_system, false);
buffer
}
}
impl<'a, T: Iterator<Item = Event<'a>>> Iterator for JotdownBufferIter<'a, T> {
type Item = JotdownItem<'a>;
fn next(&mut self) -> Option<Self::Item> {
let mut buffer = Buffer::new(&mut self.font_system, self.metrics);
let mut jot = JotdownIntoBuffer {
djot: &mut self.djot,
attrs: Attrs::new().family(Family::SansSerif),
@ -161,25 +177,15 @@ impl<'a, 'b, T: Iterator<Item = Event<'a>>> Iterator for JotdownBufferIter<'a, '
urls: Vec::new(),
};
buffer.set_rich_text(
&mut self.font_system,
&mut jot,
Attrs::new().family(Family::SansSerif),
Shaping::Advanced,
);
buffer.set_wrap(&mut self.font_system, cosmic_text::Wrap::WordOrGlyph);
buffer.set_metrics(&mut self.font_system, jot.metrics);
let buffer: Vec<(&str, Attrs<'static>)> = (&mut jot).collect();
let image_url = jot.image_url;
let urls = jot.urls;
let added = jot.added;
let metrics = jot.metrics;
let indent = self.indent.last().copied().unwrap_or_default();
buffer.set_size(&mut self.font_system, self.width - indent.indent, f32::MAX);
if !added {
return None;
} else {
buffer.shape_until_scroll(&mut self.font_system, true);
return Some(JotdownItem {
indent,
url_map: if urls.is_empty() {
@ -189,20 +195,20 @@ impl<'a, 'b, T: Iterator<Item = Event<'a>>> Iterator for JotdownBufferIter<'a, '
},
buffer,
image_url,
metrics,
width: self.width,
});
}
}
}
pub fn jotdown_into_buffers<'a, 'b, T: Iterator<Item = Event<'a>>>(
pub fn jotdown_into_buffers<'a, T: Iterator<Item = Event<'a>>>(
djot: T,
font_system: &'b mut FontSystem,
metrics: Metrics,
width: f32,
) -> JotdownBufferIter<'a, 'b, T> {
) -> JotdownBufferIter<'a, T> {
JotdownBufferIter {
djot,
font_system,
width,
metrics,
indent: Vec::new(),