v1.2.1: text measuring support
several malvim features added, font data caching, taskbar title overflow fix, new background
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ming-wm-lib"
|
||||
version = "0.2.1"
|
||||
version = "0.2.2"
|
||||
repository = "https://github.com/stjet/ming-wm"
|
||||
description = "library for building windows for ming-wm in rust"
|
||||
readme = "README.md"
|
||||
|
||||
107
ming-wm-lib/src/fonts.rs
Normal file
107
ming-wm-lib/src/fonts.rs
Normal file
@@ -0,0 +1,107 @@
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::dirs;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct FontCharInfo {
|
||||
pub c: char,
|
||||
pub data: Vec<Vec<u8>>,
|
||||
pub top_offset: u8,
|
||||
pub height: usize,
|
||||
pub width: usize,
|
||||
}
|
||||
|
||||
fn get_font_char(dir: &str, c: char) -> Option<FontCharInfo> {
|
||||
let c = if c == '/' { '𐘋' } else if c == '\\' { '𐚆' } else if c == '.' { '𐘅' } else { c };
|
||||
if let Ok(mut file) = File::open(dir.to_string() + "/" + &c.to_string() + ".alpha") {
|
||||
let mut ch: Vec<Vec<u8>> = Vec::new();
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents).unwrap();
|
||||
let lines: Vec<&str> = contents.split("\n").collect();
|
||||
for ln in 1..lines.len() {
|
||||
//.unwrap_or(0) is important because zeroes are just empty
|
||||
ch.push(lines[ln].replace(":", ",,,,").replace(";", ",,,").replace(".", ",,").split(",").map(|n| n.parse().unwrap_or(0)).collect());
|
||||
}
|
||||
return Some(FontCharInfo {
|
||||
c,
|
||||
top_offset: lines[0].parse().unwrap(),
|
||||
height: lines.len() - 1,
|
||||
width: ch[0].len(),
|
||||
data: ch,
|
||||
});
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn get_font_char_from_fonts(fonts: &[String], c: char) -> FontCharInfo {
|
||||
for font in fonts {
|
||||
let p = dirs::exe_dir(Some(&("ming_bmps/".to_string() + &font))).to_string_lossy().to_string();
|
||||
if let Some(font_char) = get_font_char(&p, c) {
|
||||
return font_char;
|
||||
}
|
||||
}
|
||||
let p = dirs::exe_dir(Some(&("ming_bmps/".to_string() + &fonts[0]))).to_string_lossy().to_string();
|
||||
//so a ? char should be in every font. otherwise will just return blank
|
||||
get_font_char(&p, '?').unwrap_or(FontCharInfo {
|
||||
c: '?',
|
||||
data: vec![vec![0]],
|
||||
top_offset: 0,
|
||||
height: 1,
|
||||
width: 1,
|
||||
})
|
||||
}
|
||||
|
||||
pub struct MeasureInfo {
|
||||
pub height: usize,
|
||||
pub width: usize,
|
||||
}
|
||||
|
||||
/// Doesn't take into account `horiz_spacing`, which defaults to 1 per character
|
||||
pub fn measure_text(fonts: &[String], text: String) -> MeasureInfo {
|
||||
let mut height = 0;
|
||||
let mut width = 0;
|
||||
for c in text.chars() {
|
||||
let i = get_font_char_from_fonts(fonts, c);
|
||||
let c_height = i.top_offset as usize + i.height;
|
||||
if c_height > height {
|
||||
height = c_height;
|
||||
}
|
||||
width += i.width;
|
||||
}
|
||||
MeasureInfo {
|
||||
height,
|
||||
width,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct CachedFontCharGetter {
|
||||
cache: HashMap<char, FontCharInfo>,
|
||||
cache_size: usize, //# of items cached
|
||||
pub max_cache_size: usize,
|
||||
}
|
||||
|
||||
impl CachedFontCharGetter {
|
||||
pub fn new(max_cache_size: usize) -> Self {
|
||||
let mut s: Self = Default::default();
|
||||
s.max_cache_size = max_cache_size;
|
||||
s
|
||||
}
|
||||
|
||||
pub fn get(&mut self, fonts: &[String], c: char) -> FontCharInfo {
|
||||
if let Some(cached) = self.cache.get(&c) {
|
||||
cached.clone()
|
||||
} else {
|
||||
let got = get_font_char_from_fonts(fonts, c);
|
||||
if self.cache_size == self.max_cache_size {
|
||||
self.cache_size = 0;
|
||||
self.cache = HashMap::new();
|
||||
}
|
||||
self.cache.insert(c, got.clone());
|
||||
self.cache_size += 1;
|
||||
got
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ pub mod serialize;
|
||||
pub mod messages;
|
||||
pub mod ipc;
|
||||
pub mod components;
|
||||
pub mod fonts;
|
||||
pub mod dirs;
|
||||
pub mod utils;
|
||||
pub mod logging;
|
||||
|
||||
@@ -223,3 +223,4 @@ pub fn get_all_files(dir: PathBuf) -> Vec<PathBuf> {
|
||||
}
|
||||
files
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user