enso/lib/rust/ensogl/component/text/embedded-fonts/build.rs
Mateusz Czapliński e75df61b2c
Component Group View with static header and without icons (#3373)
Add an initial version of the visual component for displaying the Component Group View. The component contains a header (for displaying the Group Name) and a list of labels (for displaying the component names).

https://www.pivotaltracker.com/story/show/181724889


#### Visuals

A screenshot from a debug scene demonstrating the component:

<img width="251" alt="Screenshot 2022-04-13 at 20 07 56" src="https://user-images.githubusercontent.com/273837/163243304-21c3ad78-4813-4368-b3bb-844d979da699.png">



Screenshots from other debug scenes (`list_view` and `text_area`), demonstrating that the other components still display correctly:

<img width="202" alt="Screenshot 2022-04-13 at 20 08 56" src="https://user-images.githubusercontent.com/273837/163243428-de9dc1c7-5a9f-45e0-9325-db60cece9768.png">


<img width="403" alt="Screenshot 2022-04-13 at 20 08 48" src="https://user-images.githubusercontent.com/273837/163243432-895061d9-5bd9-4349-8679-eb63b0f6724d.png">


A screenshot of the Node Searcher's list, showing that long entries in a ListView are now truncated, and an ellipsis character is added in place of removed characters:


<img width="651" alt="Screenshot 2022-04-13 at 20 10 16" src="https://user-images.githubusercontent.com/273837/163243664-5b671969-7aa0-4bef-8fd2-825602d85848.png">

# Important Notes
- Adding support for the text truncation feature in `ListView` required some changes in the`list_view::Entry`-related APIs.
- An embedded font was added (DejaVuSans-Bold) for use in the Component Group View debug scene, and 5 unused embedded fonts were removed.
2022-04-14 10:37:40 +00:00

111 lines
3.4 KiB
Rust

//! Downloader of fonts considered as "embedded" into the application.
use std::env;
use std::fs;
use std::io;
use std::io::Write;
use std::path;
// =====================
// === FillMapRsFile ===
// =====================
/// Generated file with code filling embedded fonts map
pub struct FillMapRsFile {
file: fs::File,
}
impl FillMapRsFile {
fn create<P: AsRef<path::Path>>(path: P) -> io::Result<FillMapRsFile> {
let mut file = fs::File::create(path)?;
writeln!(file, "{{")?;
Ok(FillMapRsFile { file })
}
fn add_font_inserting_line(&mut self, font_name: &str, font_file: &str) -> io::Result<()> {
writeln!(
self.file,
" font_data_by_name.insert(\"{font_name}\", include_bytes!(\"{font_file}\"));",
font_name = font_name,
font_file = font_file
)
}
fn close_block(&mut self) -> io::Result<()> {
writeln!(self.file, "}}")
}
}
// ===================
// === DejaVu Font ===
// ===================
mod deja_vu {
use crate::FillMapRsFile;
use enso_build_utilities::GithubRelease;
use std::path;
pub const PACKAGE: GithubRelease<&str> = GithubRelease {
project_url: "https://github.com/dejavu-fonts/dejavu-fonts/",
version: "version_2_37",
filename: "dejavu-fonts-ttf-2.37.zip",
};
pub const PACKAGE_FONTS_PREFIX: &str = "dejavu-fonts-ttf-2.37/ttf";
pub fn font_file_from_font_name(font_name: &str) -> String {
return format!("{}.ttf", font_name);
}
pub fn extract_font(package_path: &path::Path, font_name: &str) {
let font_file = font_file_from_font_name(font_name);
let font_in_package_path = format!("{}/{}", PACKAGE_FONTS_PREFIX, font_file);
let package_dir = package_path.parent().unwrap();
let output_path = package_dir.join(font_file);
let archive_file = std::fs::File::open(package_path).unwrap();
let mut archive = zip::ZipArchive::new(archive_file).unwrap();
let mut input_stream = archive.by_name(font_in_package_path.as_str()).unwrap();
let mut output_stream = std::fs::File::create(output_path).unwrap();
std::io::copy(&mut input_stream, &mut output_stream).unwrap();
}
pub const FONTS_TO_EXTRACT: &[&str] =
&["DejaVuSans", "DejaVuSans-Bold", "DejaVuSansMono", "DejaVuSansMono-Bold"];
pub fn extract_all_fonts(package_path: &path::Path) {
for font_name in FONTS_TO_EXTRACT {
extract_font(package_path, font_name);
}
}
pub fn download_and_extract_all_fonts(out_dir: &path::Path) {
let package_path = out_dir.join(PACKAGE.filename);
PACKAGE.download(out_dir);
extract_all_fonts(package_path.as_path());
}
pub fn add_entries_to_fill_map_rs(file: &mut FillMapRsFile) {
for font_name in FONTS_TO_EXTRACT {
let font_file = font_file_from_font_name(font_name);
file.add_font_inserting_line(font_name, font_file.as_str()).unwrap();
}
}
}
fn main() {
println!("cargo:rerun-if-changed=build.rs");
let out = env::var("OUT_DIR").unwrap();
let out_dir = path::Path::new(&out);
deja_vu::download_and_extract_all_fonts(out_dir);
let fill_map_rs_path = out_dir.join("fill_map.rs");
let mut fill_map_rs_file = FillMapRsFile::create(fill_map_rs_path).unwrap();
deja_vu::add_entries_to_fill_map_rs(&mut fill_map_rs_file);
fill_map_rs_file.close_block().unwrap();
}