Use f32 instead of i32 for lengths
This allows users to use logical coordinates instead of physical ones.
This commit is contained in:
parent
f08bea22ed
commit
4320ae6329
15 changed files with 203 additions and 155 deletions
|
|
@ -14,7 +14,7 @@ use cosmic::{
|
|||
use cosmic_text::{
|
||||
Align, Attrs, AttrsList, Buffer, Edit, FontSystem, Metrics, SyntaxEditor, SyntaxSystem, Wrap,
|
||||
};
|
||||
use std::{env, fs, path::PathBuf, sync::Mutex};
|
||||
use std::{env, fmt, fs, path::PathBuf, sync::Mutex};
|
||||
|
||||
use self::text::text;
|
||||
mod text;
|
||||
|
|
@ -27,14 +27,52 @@ lazy_static::lazy_static! {
|
|||
static ref SYNTAX_SYSTEM: SyntaxSystem = SyntaxSystem::new();
|
||||
}
|
||||
|
||||
static FONT_SIZES: &'static [Metrics] = &[
|
||||
Metrics::new(10, 14), // Caption
|
||||
Metrics::new(14, 20), // Body
|
||||
Metrics::new(20, 28), // Title 4
|
||||
Metrics::new(24, 32), // Title 3
|
||||
Metrics::new(28, 36), // Title 2
|
||||
Metrics::new(32, 44), // Title 1
|
||||
];
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum FontSize {
|
||||
Caption,
|
||||
Body,
|
||||
Title4,
|
||||
Title3,
|
||||
Title2,
|
||||
Title1,
|
||||
}
|
||||
|
||||
impl FontSize {
|
||||
pub fn all() -> &'static [Self] {
|
||||
&[
|
||||
Self::Caption,
|
||||
Self::Body,
|
||||
Self::Title4,
|
||||
Self::Title3,
|
||||
Self::Title2,
|
||||
Self::Title1,
|
||||
]
|
||||
}
|
||||
|
||||
pub fn to_metrics(self) -> Metrics {
|
||||
match self {
|
||||
Self::Caption => Metrics::new(10.0, 14.0), // Caption
|
||||
Self::Body => Metrics::new(14.0, 20.0), // Body
|
||||
Self::Title4 => Metrics::new(20.0, 28.0), // Title 4
|
||||
Self::Title3 => Metrics::new(24.0, 32.0), // Title 3
|
||||
Self::Title2 => Metrics::new(28.0, 36.0), // Title 2
|
||||
Self::Title1 => Metrics::new(32.0, 44.0), // Title 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for FontSize {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::Caption => write!(f, "Caption"),
|
||||
Self::Body => write!(f, "Body"),
|
||||
Self::Title4 => write!(f, "Title 4"),
|
||||
Self::Title3 => write!(f, "Title 3"),
|
||||
Self::Title2 => write!(f, "Title 2"),
|
||||
Self::Title1 => write!(f, "Title 1"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static WRAP_MODE: &'static [Wrap] = &[Wrap::None, Wrap::Glyph, Wrap::Word];
|
||||
|
||||
|
|
@ -50,6 +88,7 @@ pub struct Window {
|
|||
theme: Theme,
|
||||
path_opt: Option<PathBuf>,
|
||||
attrs: Attrs<'static>,
|
||||
font_size: FontSize,
|
||||
#[cfg(not(feature = "vi"))]
|
||||
editor: Mutex<SyntaxEditor<'static>>,
|
||||
#[cfg(feature = "vi")]
|
||||
|
|
@ -64,7 +103,7 @@ pub enum Message {
|
|||
Bold(bool),
|
||||
Italic(bool),
|
||||
Monospaced(bool),
|
||||
MetricsChanged(Metrics),
|
||||
FontSizeChanged(FontSize),
|
||||
WrapChanged(Wrap),
|
||||
AlignmentChanged(Align),
|
||||
ThemeChanged(&'static str),
|
||||
|
|
@ -98,7 +137,7 @@ impl Application for Window {
|
|||
.family(cosmic_text::Family::Monospace);
|
||||
|
||||
let mut editor = SyntaxEditor::new(
|
||||
Buffer::new(&FONT_SYSTEM, FONT_SIZES[1 /* Body */]),
|
||||
Buffer::new(&FONT_SYSTEM, FontSize::Body.to_metrics()),
|
||||
&SYNTAX_SYSTEM,
|
||||
"base16-eighties.dark",
|
||||
)
|
||||
|
|
@ -111,6 +150,7 @@ impl Application for Window {
|
|||
|
||||
let mut window = Window {
|
||||
theme: Theme::Dark,
|
||||
font_size: FontSize::Body,
|
||||
path_opt: None,
|
||||
attrs,
|
||||
editor: Mutex::new(editor),
|
||||
|
|
@ -195,9 +235,11 @@ impl Application for Window {
|
|||
let mut editor = self.editor.lock().unwrap();
|
||||
update_attrs(&mut *editor, self.attrs);
|
||||
}
|
||||
Message::MetricsChanged(metrics) => {
|
||||
Message::FontSizeChanged(font_size) => {
|
||||
self.font_size = font_size;
|
||||
|
||||
let mut editor = self.editor.lock().unwrap();
|
||||
editor.buffer_mut().set_metrics(metrics);
|
||||
editor.buffer_mut().set_metrics(font_size.to_metrics());
|
||||
}
|
||||
Message::WrapChanged(wrap) => {
|
||||
let mut editor = self.editor.lock().unwrap();
|
||||
|
|
@ -245,9 +287,9 @@ impl Application for Window {
|
|||
let font_size_picker = {
|
||||
let editor = self.editor.lock().unwrap();
|
||||
pick_list(
|
||||
FONT_SIZES,
|
||||
Some(editor.buffer().metrics()),
|
||||
Message::MetricsChanged,
|
||||
FontSize::all(),
|
||||
Some(self.font_size),
|
||||
Message::FontSizeChanged,
|
||||
)
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ impl Text {
|
|||
|
||||
let text = Self {
|
||||
line,
|
||||
metrics: Metrics::new(14, 20),
|
||||
metrics: Metrics::new(14.0, 20.0),
|
||||
};
|
||||
|
||||
log::debug!("Text::new in {:?}", instant.elapsed());
|
||||
|
|
@ -99,17 +99,17 @@ where
|
|||
//TODO: can we cache this?
|
||||
let layout_lines = shape.layout(
|
||||
self.metrics.font_size,
|
||||
limits.max().width as i32,
|
||||
limits.max().width,
|
||||
self.line.wrap(),
|
||||
self.line.align(),
|
||||
);
|
||||
|
||||
let mut width = 0;
|
||||
let mut height = 0;
|
||||
let mut width = 0.0f32;
|
||||
let mut height = 0.0f32;
|
||||
|
||||
for layout_line in layout_lines {
|
||||
for glyph in layout_line.glyphs.iter() {
|
||||
width = cmp::max(width, (glyph.x + glyph.w) as i32 + 1);
|
||||
width = width.max((glyph.x + glyph.w) + 1.0);
|
||||
}
|
||||
height += self.metrics.line_height;
|
||||
}
|
||||
|
|
@ -156,8 +156,8 @@ where
|
|||
cmp::max(0, cmp::min(255, (appearance.text_color.a * 255.0) as i32)) as u8,
|
||||
);
|
||||
|
||||
let layout_w = layout.bounds().width as i32;
|
||||
let layout_h = layout.bounds().height as i32;
|
||||
let layout_w = layout.bounds().width;
|
||||
let layout_h = layout.bounds().height;
|
||||
|
||||
let shape = self.line.shape_opt().as_ref().unwrap();
|
||||
|
||||
|
|
@ -185,8 +185,8 @@ where
|
|||
|
||||
cache.with_pixels(cache_key, glyph_color, |pixel_x, pixel_y, color| {
|
||||
let x = x_int + pixel_x;
|
||||
let y = line_y + y_int + pixel_y;
|
||||
draw_pixel(&mut pixels, layout_w, layout_h, x, y, color);
|
||||
let y = line_y as i32 + y_int + pixel_y;
|
||||
draw_pixel(&mut pixels, layout_w as i32, layout_h as i32, x, y, color);
|
||||
});
|
||||
}
|
||||
line_y += self.metrics.line_height;
|
||||
|
|
|
|||
|
|
@ -160,10 +160,8 @@ where
|
|||
|
||||
let mut editor = self.editor.lock().unwrap();
|
||||
|
||||
let view_w = cmp::min(viewport.width as i32, layout.bounds().width as i32)
|
||||
- self.padding.horizontal() as i32;
|
||||
let view_h = cmp::min(viewport.height as i32, layout.bounds().height as i32)
|
||||
- self.padding.vertical() as i32;
|
||||
let view_w = viewport.width.min(layout.bounds().width) - self.padding.horizontal() as f32;
|
||||
let view_h = viewport.height.min(layout.bounds().height) - self.padding.vertical() as f32;
|
||||
editor.buffer_mut().set_size(view_w, view_h);
|
||||
|
||||
editor.shape_as_needed();
|
||||
|
|
@ -203,7 +201,7 @@ where
|
|||
),
|
||||
);
|
||||
} else {
|
||||
text::draw_pixel(&mut pixels, view_w, view_h, x, y, color);
|
||||
text::draw_pixel(&mut pixels, view_w as i32, view_h as i32, x, y, color);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue