Skip to content
This repository was archived by the owner on Dec 28, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions build-utilities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,34 @@
use std::{fs, path};
use std::io::ErrorKind;

/// Download the release package from github
///
/// The project_url should be a project's main page on github.
pub fn github_download(
project_url : &str,
version : &str,
filename : &str,
destination_dir : &path::Path
) {
let url = format!(
"{project}/releases/download/{version}/{filename}",
project = project_url,
version = version,
filename = filename
);
/// A structure describing a concrete release package on github.
pub struct GithubRelease<Str:AsRef<str>> {
pub project_url : Str,
pub version : Str,
pub filename : Str,
}

let destination_file = path::Path::new(destination_dir)
.join(filename);
impl<Str:AsRef<str>> GithubRelease<Str> {
/// Download the release package from github
///
/// The project_url should be a project's main page on github.
pub fn download(&self, destination_dir:&path::Path) {
let url = format!(
"{project}/releases/download/{version}/{filename}",
project = self.project_url.as_ref(),
version = self.version.as_ref(),
filename = self.filename.as_ref());
let destination_dir_str = destination_dir.to_str().unwrap();
let destination_file = destination_dir.join(self.filename.as_ref());

let rm_error = fs::remove_file(&destination_file).err();
let fatal_rm_error =
rm_error.filter(|err| err.kind() != ErrorKind::NotFound);
fatal_rm_error.unwrap_none();
Self::remove_old_file(&destination_file);
download_lp::download(url.as_str(),destination_dir_str).unwrap();
}

download_lp::download(
url.as_str(),
destination_dir.to_str().unwrap()
).unwrap();
fn remove_old_file(file:&path::Path) {
let result = fs::remove_file(&file);
let error = result.err();
let fatal_error = error.filter(|err| err.kind() != ErrorKind::NotFound);
fatal_error.unwrap_none();
}
}
1 change: 1 addition & 0 deletions examples/03-text/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
package-lock.json
3 changes: 1 addition & 2 deletions lib/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ failure = { version = "0.1.5" }
derive_more = { version = "0.15.0" }
shrinkwraprs = { version = "0.2.1" }
itertools = { version = "0.8" }
nalgebra = { version = "0.18.1" }
nalgebra = { version = "0.19.0" }
bit_field = { version = "0.10.0" }
paste = { version = "0.1.6" }
enum_dispatch = { version = "= 0.1.3" } # https://gitlab.com/antonok/enum_dispatch/issues/10
typenum = { version = "1.11.2" }
rustc-hash = { version = "1.0.1" }
console_error_panic_hook = { version = "0.1.6" }
num_enum = { version = "0.4.2" }
smallvec = { version = "1.0.0" }

[dependencies.web-sys]
version = "0.3.4"
Expand Down
91 changes: 38 additions & 53 deletions lib/core/embedded-fonts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ pub struct FillMapRsFile {
}

impl FillMapRsFile {
fn create<P: AsRef<path::Path>>(path : P) -> io::Result<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,
" fonts_by_name.insert(\"{}\", include_bytes!(\"{}\"));",
font_name,
font_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
)
}

Expand All @@ -37,38 +35,34 @@ impl FillMapRsFile {
// ====================

mod deja_vu {
use std::path;
use basegl_build_utilities::github_download;
use crate::FillMapRsFile;

pub const PACKAGE_NAME : &str = "dejavu-fonts-ttf-2.37.zip";
pub const PACKAGE_VERSION : &str = "version_2_37";
pub const PROJECT_URL : &str =
"https://github.com/dejavu-fonts/dejavu-fonts/";
use std::path;
use basegl_build_utilities::GithubRelease;

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 const PACKAGE_FONTS_PREFIX : &str = "dejavu-fonts-ttf-2.37/ttf";

pub fn font_file_from_font_name(font_name : &str) -> String {
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_package_path = format!("{}/{}",
PACKAGE_FONTS_PREFIX,
font_file
);

let mut archive = zip::ZipArchive::new(
std::fs::File::open(package_path).unwrap()
).unwrap();
let mut input = archive.by_name(
font_package_path.as_str()
).unwrap();
let mut output = std::fs::File::create(
package_path.parent().unwrap().join(font_file)
).unwrap();
std::io::copy(&mut input, &mut output).unwrap();
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] = &[
Expand All @@ -79,8 +73,7 @@ mod deja_vu {
"DejaVuSansMono-Oblique",
"DejaVuSansCondensed",
"DejaVuSerif",
"DejaVuSerifCondensed",
];
"DejaVuSerifCondensed" ];

pub fn extract_all_fonts(package_path : &path::Path) {
for font_name in FONTS_TO_EXTRACT {
Expand All @@ -89,35 +82,27 @@ mod deja_vu {
}

pub fn download_and_extract_all_fonts(out_dir : &path::Path) {
github_download(
PROJECT_URL,
PACKAGE_VERSION,
PACKAGE_NAME,
&out_dir
);

let package_path = out_dir.join(PACKAGE_NAME);
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) {
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();

file.add_font_inserting_line(font_name,font_file.as_str()).unwrap();
}
}
}

fn main() {
let out = env::var("OUT_DIR").unwrap();
let out_dir = path::Path::new(&out);
let fill_map_rs_path = out_dir.join("fill_map.rs");
let out = env::var("OUT_DIR").unwrap();
let out_dir = path::Path::new(&out);
let fill_map_rs_path = out_dir.join("fill_map.rs");

let mut fill_map_rs_file =
FillMapRsFile::create(fill_map_rs_path).unwrap();
let mut fill_map_rs_file = FillMapRsFile::create(fill_map_rs_path).unwrap();

deja_vu::download_and_extract_all_fonts(out_dir);
deja_vu::add_entries_to_fill_map_rs(&mut fill_map_rs_file);
Expand Down
14 changes: 6 additions & 8 deletions lib/core/embedded-fonts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use basegl_prelude::*;
///
/// For list of embedded fonts, see FONTS_TO_EXTRACT constant in `build.rs`
pub struct EmbeddedFonts {
pub font_data_by_name: HashMap<&'static str, &'static [u8]>
pub font_data_by_name: HashMap<&'static str,&'static [u8]>
}

impl EmbeddedFonts {
Expand All @@ -16,23 +16,21 @@ impl EmbeddedFonts {
/// For list of embedded fonts, see `FONTS_TO_EXTRACT` constant in
/// `build.rs`
pub fn create_and_fill() -> EmbeddedFonts {
let mut fonts_by_name : HashMap<&'static str, &'static [u8]>
= HashMap::new();
let mut font_data_by_name = HashMap::<&'static str,&'static [u8]>::new();
include!(concat!(env!("OUT_DIR"), "/fill_map.rs"));
EmbeddedFonts {
font_data_by_name: fonts_by_name
}
EmbeddedFonts{font_data_by_name}
}
}

#[cfg(test)]
mod test {
use crate::EmbeddedFonts;
use crate::*;

#[test]
fn loading_embedded_fonts() {
let fonts = EmbeddedFonts::create_and_fill();
let fonts = EmbeddedFonts::create_and_fill();
let example_font = fonts.font_data_by_name.get("DejaVuSans").unwrap();

assert_eq!(0x00, example_font[0]);
assert_eq!(0x01, example_font[1]);
assert_eq!(0x1d, example_font[example_font.len()-1]);
Expand Down
2 changes: 1 addition & 1 deletion lib/core/msdf-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
wasm-bindgen = "0.2.53"
js-sys = "0.3.30"
vector2d = "2.2.0"
nalgebra = "0.19.0"
basegl-prelude = { version = "0.1.0", path="../../prelude" }

[dev-dependencies]
Expand Down
35 changes: 20 additions & 15 deletions lib/core/msdf-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use basegl_build_utilities::github_download;

mod msdfgen_wasm {
use crate::github_download;
use std::{path, fs};
use basegl_build_utilities::GithubRelease;

use std::{path,fs};
use std::io::Write;

pub const VERSION : &str = "v1.0.1";
pub const FILENAME : &str = "msdfgen_wasm.js";
pub const PROJECT_URL : &str = "https://github.com/luna/msdfgen-wasm";
pub const PACKAGE : GithubRelease<&str> = GithubRelease {
project_url : "https://github.com/luna/msdfgen-wasm",
version : "v1.1",
filename : "msdfgen_wasm.js"
};

pub const FILENAME : &str = PACKAGE.filename;

pub fn download() {
github_download(
PROJECT_URL,
VERSION,
FILENAME,
path::Path::new(".") // Note [Downloading to src dir]
)
PACKAGE.download(path::Path::new(".")) // Note [Downloading to src dir]
}

/* Note [Downloading to src dir]
Expand All @@ -30,8 +29,10 @@ mod msdfgen_wasm {
* remember to remove msdfgen_wasm.js entry from .gitignore
*/

const PATCH_LINE : &str = "; export { ccall, getValue, \
_msdfgen_maxMSDFSize, _msdfgen_generateMSDF, _msdfgen_freeFont, \
const PATCH_LINE : &str = "; export { ccall, getValue, _msdfgen_getKerning,\
_msdfgen_generateAutoframedMSDF, _msdfgen_result_getMSDFData,\
_msdfgen_result_getAdvance, _msdfgen_result_getTranslation,\
_msdfgen_result_getScale, _msdfgen_freeResult, _msdfgen_freeFont,\
addInitializationCb, isInitialized }";

/// Patches downloaded msdfgen_wasm.js file
Expand All @@ -40,7 +41,11 @@ mod msdfgen_wasm {
/// be explicitly exported. Examples works without this line perfectly.
pub fn patch_for_wasm_bindgen_test() {
let path = path::Path::new(FILENAME);
let mut file = fs::OpenOptions::new().append(true).open(path).unwrap();

let mut open_options = fs::OpenOptions::new();
open_options.append(true);

let mut file = open_options.open(path).unwrap();
file.write(PATCH_LINE.as_bytes()).unwrap();
}
}
Expand Down
Loading