Allow dynamic sizing

This commit is contained in:
Isaac Mills 2024-04-10 13:18:11 -04:00
parent c82248831b
commit 7f245acf66
Signed by: fnmain
GPG key ID: B67D7410F33A0F61

View file

@ -8,8 +8,6 @@ use range_map::RangeMap;
pub struct JotdownBufferIter<'a, T: Iterator<Item = Event<'a>>> { pub struct JotdownBufferIter<'a, T: Iterator<Item = Event<'a>>> {
djot: T, djot: T,
width: f32,
metrics: Metrics,
indent: Vec<Indent>, indent: Vec<Indent>,
} }
@ -86,10 +84,7 @@ impl<'a, 'b, T: Iterator<Item = Event<'a>>> Iterator for JotdownIntoBuffer<'a, '
} }
Event::Start(container, _) => match container { Event::Start(container, _) => match container {
Container::Heading { level, .. } => { Container::Heading { level, .. } => {
self.metrics = Metrics::new( self.metrics = Metrics::new(4.0 - level as f32, 4.0 - level as f32);
self.metrics.font_size * (4.0 - level as f32),
self.metrics.line_height * (4.0 - level as f32),
);
} }
Container::Emphasis => self.attrs = self.attrs.style(Style::Italic), Container::Emphasis => self.attrs = self.attrs.style(Style::Italic),
Container::Strong => self.attrs = self.attrs.weight(Weight::BOLD), Container::Strong => self.attrs = self.attrs.weight(Weight::BOLD),
@ -141,12 +136,22 @@ pub struct JotdownItem<'a> {
pub metrics: Metrics, pub metrics: Metrics,
pub image_url: Option<Cow<'a, str>>, pub image_url: Option<Cow<'a, str>>,
pub url_map: Option<RangeMap<usize, &'a str>>, pub url_map: Option<RangeMap<usize, &'a str>>,
pub width: f32,
} }
impl<'a> JotdownItem<'a> { impl<'a> JotdownItem<'a> {
pub fn make_buffer(&self, font_system: &mut FontSystem) -> Buffer { pub fn make_buffer(
let mut buffer = Buffer::new(font_system, self.metrics); &self,
font_system: &mut FontSystem,
width: f32,
metrics: Metrics,
) -> Buffer {
let mut buffer = Buffer::new(
font_system,
Metrics::new(
metrics.font_size * self.metrics.font_size,
metrics.line_height * self.metrics.line_height,
),
);
buffer.set_rich_text( buffer.set_rich_text(
font_system, font_system,
self.buffer.iter().cloned(), self.buffer.iter().cloned(),
@ -155,7 +160,7 @@ impl<'a> JotdownItem<'a> {
); );
buffer.set_wrap(font_system, cosmic_text::Wrap::WordOrGlyph); buffer.set_wrap(font_system, cosmic_text::Wrap::WordOrGlyph);
buffer.set_size(font_system, self.width - self.indent.indent, f32::MAX); buffer.set_size(font_system, width - self.indent.indent, f32::MAX);
buffer.shape_until_scroll(font_system, false); buffer.shape_until_scroll(font_system, false);
buffer buffer
} }
@ -168,9 +173,9 @@ impl<'a, T: Iterator<Item = Event<'a>>> Iterator for JotdownBufferIter<'a, T> {
let mut jot = JotdownIntoBuffer { let mut jot = JotdownIntoBuffer {
djot: &mut self.djot, djot: &mut self.djot,
attrs: Attrs::new().family(Family::SansSerif), attrs: Attrs::new().family(Family::SansSerif),
metrics: self.metrics,
indent: &mut self.indent, indent: &mut self.indent,
image_url: None, image_url: None,
metrics: Metrics::new(1.0, 1.0),
added: false, added: false,
link_start: 0, link_start: 0,
location: 0, location: 0,
@ -196,7 +201,6 @@ impl<'a, T: Iterator<Item = Event<'a>>> Iterator for JotdownBufferIter<'a, T> {
buffer, buffer,
image_url, image_url,
metrics, metrics,
width: self.width,
}); });
} }
} }
@ -204,13 +208,9 @@ impl<'a, T: Iterator<Item = Event<'a>>> Iterator for JotdownBufferIter<'a, T> {
pub fn jotdown_into_buffers<'a, T: Iterator<Item = Event<'a>>>( pub fn jotdown_into_buffers<'a, T: Iterator<Item = Event<'a>>>(
djot: T, djot: T,
metrics: Metrics,
width: f32,
) -> JotdownBufferIter<'a, T> { ) -> JotdownBufferIter<'a, T> {
JotdownBufferIter { JotdownBufferIter {
djot, djot,
width,
metrics,
indent: Vec::new(), indent: Vec::new(),
} }
} }