Skip to content

Commit

Permalink
remove Metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
tigregalis committed Oct 2, 2023
1 parent ba2a1e8 commit 6dc9792
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 79 deletions.
6 changes: 2 additions & 4 deletions examples/rich-text/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use cosmic_text::{
Action, Attrs, AttrsList, Buffer, BufferLine, Color, Edit, Editor, Family, FontSystem,
LineHeight, Metrics, Shaping, Style, SwashCache, Weight,
LineHeight, Shaping, Style, SwashCache, Weight,
};
use orbclient::{EventOption, Renderer, Window, WindowFlag};
use std::{
Expand Down Expand Up @@ -36,9 +36,7 @@ fn main() {
)
.unwrap();

let mut editor = Editor::new(Buffer::new_empty(
Metrics::new(32.0, 44.0).scale(display_scale),
));
let mut editor = Editor::new(Buffer::new_empty());

let mut editor = editor.borrow_with(&mut font_system);

Expand Down
78 changes: 3 additions & 75 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use alloc::{
string::{String, ToString},
vec::Vec,
};
use core::fmt;
use unicode_segmentation::UnicodeSegmentation;

use crate::{
Expand Down Expand Up @@ -275,45 +274,12 @@ impl<'b> Iterator for LayoutRunIter<'b> {

impl<'b> ExactSizeIterator for LayoutRunIter<'b> {}

/// Metrics of text
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Metrics {
/// Font size in pixels
pub font_size_: f32,
/// Line height in pixels
pub line_height_: f32,
}

impl Metrics {
pub fn new(font_size: f32, line_height: f32) -> Self {
Self {
font_size_: font_size,
// TODO: remove this (not hardcoded)
line_height_: font_size * 1.2,
}
}

pub fn scale(self, scale: f32) -> Self {
Self {
font_size_: self.font_size_ * scale,
line_height_: self.line_height_ * scale,
}
}
}

impl fmt::Display for Metrics {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}px / {}px", self.font_size_, self.line_height_)
}
}

/// A buffer of text that is shaped and laid out
#[derive(Debug)]
pub struct Buffer {
/// [BufferLine]s (or paragraphs) of text in the buffer
pub lines: Vec<BufferLine>,
line_heights: Vec<f32>,
metrics: Metrics,
width: f32,
height: f32,
scroll: i32,
Expand All @@ -333,16 +299,10 @@ impl Buffer {
/// for example by calling [`Buffer::set_text`].
///
/// If you have a [`FontSystem`] in scope, you should use [`Buffer::new`] instead.
///
/// # Panics
///
/// Will panic if `metrics.line_height` is zero.
pub fn new_empty(metrics: Metrics) -> Self {
assert_ne!(metrics.line_height_, 0.0, "line height cannot be 0");
pub fn new_empty() -> Self {
Self {
lines: Vec::new(),
line_heights: Vec::new(),
metrics,
width: 0.0,
height: 0.0,
scroll: 0,
Expand All @@ -353,12 +313,8 @@ impl Buffer {
}

/// Create a new [`Buffer`] with the provided [`FontSystem`] and [`Metrics`]
///
/// # Panics
///
/// Will panic if `metrics.line_height` is zero.
pub fn new(font_system: &mut FontSystem, metrics: Metrics) -> Self {
let mut buffer = Self::new_empty(metrics);
pub fn new(font_system: &mut FontSystem) -> Self {
let mut buffer = Self::new_empty();
buffer.set_text(font_system, "", Attrs::new(), Shaping::Advanced);
buffer
}
Expand Down Expand Up @@ -605,25 +561,6 @@ impl Buffer {
}
}

/// Get the current [`Metrics`]
pub fn metrics(&self) -> Metrics {
self.metrics
}

/// Set the current [`Metrics`]
///
/// # Panics
///
/// Will panic if `metrics.font_size` is zero.
pub fn set_metrics(&mut self, font_system: &mut FontSystem, metrics: Metrics) {
if metrics != self.metrics {
assert_ne!(metrics.font_size_, 0.0, "font size cannot be 0");
self.metrics = metrics;
self.relayout(font_system);
self.shape_until_scroll(font_system);
}
}

/// Get the current [`Wrap`]
pub fn wrap(&self) -> Wrap {
self.wrap
Expand Down Expand Up @@ -1052,15 +989,6 @@ impl<'a> BorrowedWithFontSystem<'a, Buffer> {
self.inner.line_layout(self.font_system, line_i)
}

/// Set the current [`Metrics`]
///
/// # Panics
///
/// Will panic if `metrics.font_size` is zero.
pub fn set_metrics(&mut self, metrics: Metrics) {
self.inner.set_metrics(self.font_system, metrics);
}

/// Set the current [`Wrap`]
pub fn set_wrap(&mut self, wrap: Wrap) {
self.inner.set_wrap(self.font_system, wrap);
Expand Down

0 comments on commit 6dc9792

Please sign in to comment.