mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 06:40:06 -04:00
Choose your own font and size (#1154)
* Refactor text layout: don't need &Fonts in all functions * Replace indexing in Fonts with member function * Wrap Fonts in a Mutex * Remove mutex for Font::glyph_info_cache * Remove RwLock around Font::characters * Put FontsImpl and GalleyCache behind the same Mutex * Round font sizes to whole pixels before deduplicating them * Make TextStyle !Copy * Implement user-named TextStyle:s * round font size earlier * Cache fonts based on family and size * Move TextStyle into egui and Style * Remove body_text_style * Query graphics about max texture size and use that as font atlas size * Recreate texture atlas when it is getting full
This commit is contained in:
@@ -88,7 +88,7 @@ impl EasyMarkEditor {
|
||||
|
||||
let response = if self.highlight_editor {
|
||||
let mut layouter = |ui: &egui::Ui, easymark: &str, wrap_width: f32| {
|
||||
let mut layout_job = highlighter.highlight(ui.visuals(), easymark);
|
||||
let mut layout_job = highlighter.highlight(ui.style(), easymark);
|
||||
layout_job.wrap_width = wrap_width;
|
||||
ui.fonts().layout_job(layout_job)
|
||||
};
|
||||
@@ -96,7 +96,7 @@ impl EasyMarkEditor {
|
||||
ui.add(
|
||||
egui::TextEdit::multiline(code)
|
||||
.desired_width(f32::INFINITY)
|
||||
.text_style(egui::TextStyle::Monospace) // for cursor height
|
||||
.font(egui::TextStyle::Monospace) // for cursor height
|
||||
.layouter(&mut layouter),
|
||||
)
|
||||
} else {
|
||||
|
||||
@@ -5,23 +5,23 @@ use crate::easy_mark::easy_mark_parser;
|
||||
/// In practice, the highlighter is fast enough not to need any caching.
|
||||
#[derive(Default)]
|
||||
pub struct MemoizedEasymarkHighlighter {
|
||||
visuals: egui::Visuals,
|
||||
style: egui::Style,
|
||||
code: String,
|
||||
output: egui::text::LayoutJob,
|
||||
}
|
||||
|
||||
impl MemoizedEasymarkHighlighter {
|
||||
pub fn highlight(&mut self, visuals: &egui::Visuals, code: &str) -> egui::text::LayoutJob {
|
||||
if (&self.visuals, self.code.as_str()) != (visuals, code) {
|
||||
self.visuals = visuals.clone();
|
||||
pub fn highlight(&mut self, egui_style: &egui::Style, code: &str) -> egui::text::LayoutJob {
|
||||
if (&self.style, self.code.as_str()) != (egui_style, code) {
|
||||
self.style = egui_style.clone();
|
||||
self.code = code.to_owned();
|
||||
self.output = highlight_easymark(visuals, code);
|
||||
self.output = highlight_easymark(egui_style, code);
|
||||
}
|
||||
self.output.clone()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn highlight_easymark(visuals: &egui::Visuals, mut text: &str) -> egui::text::LayoutJob {
|
||||
pub fn highlight_easymark(egui_style: &egui::Style, mut text: &str) -> egui::text::LayoutJob {
|
||||
let mut job = egui::text::LayoutJob::default();
|
||||
let mut style = easy_mark_parser::Style::default();
|
||||
let mut start_of_line = true;
|
||||
@@ -33,7 +33,7 @@ pub fn highlight_easymark(visuals: &egui::Visuals, mut text: &str) -> egui::text
|
||||
&text[..end],
|
||||
0.0,
|
||||
format_from_style(
|
||||
visuals,
|
||||
egui_style,
|
||||
&easy_mark_parser::Style {
|
||||
code: true,
|
||||
..Default::default()
|
||||
@@ -50,7 +50,7 @@ pub fn highlight_easymark(visuals: &egui::Visuals, mut text: &str) -> egui::text
|
||||
let end = text[1..]
|
||||
.find(&['`', '\n'][..])
|
||||
.map_or_else(|| text.len(), |i| i + 2);
|
||||
job.append(&text[..end], 0.0, format_from_style(visuals, &style));
|
||||
job.append(&text[..end], 0.0, format_from_style(egui_style, &style));
|
||||
text = &text[end..];
|
||||
style.code = false;
|
||||
continue;
|
||||
@@ -77,7 +77,7 @@ pub fn highlight_easymark(visuals: &egui::Visuals, mut text: &str) -> egui::text
|
||||
skip = 1;
|
||||
if style.strong {
|
||||
// Include the character that i ending ths style:
|
||||
job.append(&text[..skip], 0.0, format_from_style(visuals, &style));
|
||||
job.append(&text[..skip], 0.0, format_from_style(egui_style, &style));
|
||||
text = &text[skip..];
|
||||
skip = 0;
|
||||
}
|
||||
@@ -86,7 +86,7 @@ pub fn highlight_easymark(visuals: &egui::Visuals, mut text: &str) -> egui::text
|
||||
skip = 1;
|
||||
if style.small {
|
||||
// Include the character that i ending ths style:
|
||||
job.append(&text[..skip], 0.0, format_from_style(visuals, &style));
|
||||
job.append(&text[..skip], 0.0, format_from_style(egui_style, &style));
|
||||
text = &text[skip..];
|
||||
skip = 0;
|
||||
}
|
||||
@@ -95,7 +95,7 @@ pub fn highlight_easymark(visuals: &egui::Visuals, mut text: &str) -> egui::text
|
||||
skip = 1;
|
||||
if style.raised {
|
||||
// Include the character that i ending ths style:
|
||||
job.append(&text[..skip], 0.0, format_from_style(visuals, &style));
|
||||
job.append(&text[..skip], 0.0, format_from_style(egui_style, &style));
|
||||
text = &text[skip..];
|
||||
skip = 0;
|
||||
}
|
||||
@@ -114,12 +114,16 @@ pub fn highlight_easymark(visuals: &egui::Visuals, mut text: &str) -> egui::text
|
||||
.map_or_else(|| text.len(), |i| (skip + i).max(1));
|
||||
|
||||
if line_end <= end {
|
||||
job.append(&text[..line_end], 0.0, format_from_style(visuals, &style));
|
||||
job.append(
|
||||
&text[..line_end],
|
||||
0.0,
|
||||
format_from_style(egui_style, &style),
|
||||
);
|
||||
text = &text[line_end..];
|
||||
start_of_line = true;
|
||||
style = Default::default();
|
||||
} else {
|
||||
job.append(&text[..end], 0.0, format_from_style(visuals, &style));
|
||||
job.append(&text[..end], 0.0, format_from_style(egui_style, &style));
|
||||
text = &text[end..];
|
||||
start_of_line = false;
|
||||
}
|
||||
@@ -129,17 +133,17 @@ pub fn highlight_easymark(visuals: &egui::Visuals, mut text: &str) -> egui::text
|
||||
}
|
||||
|
||||
fn format_from_style(
|
||||
visuals: &egui::Visuals,
|
||||
egui_style: &egui::Style,
|
||||
emark_style: &easy_mark_parser::Style,
|
||||
) -> egui::text::TextFormat {
|
||||
use egui::{Align, Color32, Stroke, TextStyle};
|
||||
|
||||
let color = if emark_style.strong || emark_style.heading {
|
||||
visuals.strong_text_color()
|
||||
egui_style.visuals.strong_text_color()
|
||||
} else if emark_style.quoted {
|
||||
visuals.weak_text_color()
|
||||
egui_style.visuals.weak_text_color()
|
||||
} else {
|
||||
visuals.text_color()
|
||||
egui_style.visuals.text_color()
|
||||
};
|
||||
|
||||
let text_style = if emark_style.heading {
|
||||
@@ -153,7 +157,7 @@ fn format_from_style(
|
||||
};
|
||||
|
||||
let background = if emark_style.code {
|
||||
visuals.code_bg_color
|
||||
egui_style.visuals.code_bg_color
|
||||
} else {
|
||||
Color32::TRANSPARENT
|
||||
};
|
||||
@@ -177,7 +181,7 @@ fn format_from_style(
|
||||
};
|
||||
|
||||
egui::text::TextFormat {
|
||||
style: text_style,
|
||||
font_id: text_style.resolve(egui_style),
|
||||
color,
|
||||
background,
|
||||
italics: emark_style.italics,
|
||||
|
||||
@@ -18,7 +18,7 @@ pub fn easy_mark_it<'em>(ui: &mut Ui, items: impl Iterator<Item = easy_mark::Ite
|
||||
|
||||
ui.allocate_ui_with_layout(initial_size, layout, |ui| {
|
||||
ui.spacing_mut().item_spacing.x = 0.0;
|
||||
let row_height = (*ui.fonts())[TextStyle::Body].row_height();
|
||||
let row_height = ui.text_style_height(&TextStyle::Body);
|
||||
ui.set_row_height(row_height);
|
||||
|
||||
for item in items {
|
||||
@@ -28,7 +28,7 @@ pub fn easy_mark_it<'em>(ui: &mut Ui, items: impl Iterator<Item = easy_mark::Ite
|
||||
}
|
||||
|
||||
pub fn item_ui(ui: &mut Ui, item: easy_mark::Item<'_>) {
|
||||
let row_height = ui.fonts()[TextStyle::Body].row_height();
|
||||
let row_height = ui.text_style_height(&TextStyle::Body);
|
||||
let one_indent = row_height / 2.0;
|
||||
|
||||
match item {
|
||||
@@ -134,7 +134,7 @@ fn rich_text_from_style(text: &str, style: &easy_mark::Style) -> RichText {
|
||||
}
|
||||
|
||||
fn bullet_point(ui: &mut Ui, width: f32) -> Response {
|
||||
let row_height = ui.fonts()[TextStyle::Body].row_height();
|
||||
let row_height = ui.text_style_height(&TextStyle::Body);
|
||||
let (rect, response) = ui.allocate_exact_size(vec2(width, row_height), Sense::hover());
|
||||
ui.painter().circle_filled(
|
||||
rect.center(),
|
||||
@@ -145,7 +145,8 @@ fn bullet_point(ui: &mut Ui, width: f32) -> Response {
|
||||
}
|
||||
|
||||
fn numbered_point(ui: &mut Ui, width: f32, number: &str) -> Response {
|
||||
let row_height = ui.fonts()[TextStyle::Body].row_height();
|
||||
let font_id = TextStyle::Body.resolve(ui.style());
|
||||
let row_height = ui.fonts().row_height(&font_id);
|
||||
let (rect, response) = ui.allocate_exact_size(vec2(width, row_height), Sense::hover());
|
||||
let text = format!("{}.", number);
|
||||
let text_color = ui.visuals().strong_text_color();
|
||||
@@ -153,7 +154,7 @@ fn numbered_point(ui: &mut Ui, width: f32, number: &str) -> Response {
|
||||
rect.right_center(),
|
||||
Align2::RIGHT_CENTER,
|
||||
text,
|
||||
TextStyle::Body,
|
||||
font_id,
|
||||
text_color,
|
||||
);
|
||||
response
|
||||
|
||||
Reference in New Issue
Block a user