Skip to content

Commit

Permalink
Switch fontawesome from font files to embedded SVG
Browse files Browse the repository at this point in the history
Fixes rust-lang#790

The rationale for embedding the SVG files directly in the binary:

* avoid file I/O occurring in your Tera Filter, which would likely
  have complex knock-on effects when combined with `async`
* avoid complex error handling in your Tera Filter, which is the
  inevitable outcome of file I/O
* avoid having to carefully construct filesystem paths, with all
  the terrible possible outcomes and failure modes that come with
  that (using the infrastructure that the existing static file
  server uses is not an option, because it's not an HTTP request)
* I'm guessing it's faster to pull a string out of the binary's
  text section than it is to open a file for every request, though
  I have done no benchmarking to justify this wild speculation

The rationale for doing it in its own crate:

* we're not using any of docs.rs's file handling infrastructure
  anyway, so all the other stuff in its build.rs was just getting
  in the way while I coded up its codegen script
* it should be useful in other Rust web projects, regardless of
  the web framework they're using
* let's not force rustc to parse a 1.3MiB source file every time
  you change docs.rs; this seems like prime "separate translation
  unit" material
  • Loading branch information
notriddle authored and Joshua Nelson committed Sep 14, 2020
1 parent cc11a32 commit f2c5f66
Show file tree
Hide file tree
Showing 1,652 changed files with 1,836 additions and 7,546 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ once_cell = { version = "1.4.0", features = ["parking_lot"] }
base64 = "0.12.1"
strum = { version = "0.18.0", features = ["derive"] }
lol_html = "0.2"
font-awesome-as-a-crate = { path = "crates/font-awesome-as-a-crate" }

# Async
tokio = { version = "0.2.22", features = ["rt-threaded"] }
Expand Down
11 changes: 1 addition & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,9 @@ cargo run -- queue add <CRATE> <VERSION>

### Updating vendored sources

The instructions & links for updating Font Awesome can be found [on their website](https://fontawesome.com/how-to-use/on-the-web/using-with/sass). Similarly, Pure-CSS also [explains on theirs](https://purecss.io/start/).
The instructions & links for updating Font Awesome can be found [on their website](https://fontawesome.com/how-to-use/on-the-web/advanced/svg-sprites). Similarly, Pure-CSS also [explains on theirs](https://purecss.io/start/).

When updating Font Awesome, make sure to change `$fa-font-path` in `scss/_variables.scss` (it should be at the top of the file) to `../-/static`. This will point font awesome at the correct path from which to request font and icon resources.
<!--
TODO: Whenever scss modules are avaliable, use [scss modules](https://sass-lang.com/documentation/at-rules/use#configuration)
instead of manually editing the `_variables.scss` file. Something like this should work:
```scss
@use "fontawesome" with (
$fa-font-path: "../-/static"
);
```
-->

### Contact

Expand Down
6 changes: 1 addition & 5 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ fn main() {
println!("cargo:rerun-if-changed=templates/menu.js");
println!("cargo:rerun-if-changed=templates/index.js");
println!("cargo:rerun-if-changed=vendor/");
println!("cargo:rerun-if-changed=vendor/fontawesome/scss/_variables.scss");
// TODO: are these right?
println!("cargo:rerun-if-changed=.git/HEAD");
println!("cargo:rerun-if-changed=.git/index");
Expand Down Expand Up @@ -91,10 +90,7 @@ fn compile_sass() -> Result<(), Box<dyn Error>> {
compile_sass_file(
"vendored",
"vendored",
&[
concat!(env!("CARGO_MANIFEST_DIR"), "/vendor/fontawesome/scss").to_owned(),
concat!(env!("CARGO_MANIFEST_DIR"), "/vendor/pure-css/css").to_owned(),
],
&[concat!(env!("CARGO_MANIFEST_DIR"), "/vendor/pure-css/css").to_owned()],
)?;

Ok(())
Expand Down
2 changes: 2 additions & 0 deletions crates/font-awesome-as-a-crate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
10 changes: 10 additions & 0 deletions crates/font-awesome-as-a-crate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "font-awesome-as-a-crate"
version = "0.1.2"
# https://github.com/FortAwesome/Font-Awesome/blob/master/composer.json
authors = ["Michael Howell <[email protected]>", "Travis Chase", "Dave Gandy", "Rob Madole", "Jory Raphael", "Geremia Taglialatela", "Brian Talbot", "Mike Wilkerson", "Fonticons Inc <[email protected]>"]
edition = "2018"
license = "CC-BY-4.0 AND MIT"
description = "Font Awesome Free, packaged as a crate"
repository = "https://github.com/rust-lang/docs.rs"

5 changes: 5 additions & 0 deletions crates/font-awesome-as-a-crate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# [Font Awesome Free](https://fontawesome.com/how-to-use/on-the-web/setup/hosting-font-awesome-yourself) SVG files as a crate

This is not officially supported by Fonticons, Inc.
If you have problems, [contact us](https://github.com/rust-lang/docs.rs), not them.

54 changes: 54 additions & 0 deletions crates/font-awesome-as-a-crate/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::{
env,
fs::{read_dir, File},
io::{Read, Write},
path::Path,
};

fn main() {
println!("cargo:rustc-cfg=font_awesome_out_dir");
write_fontawesome_sprite();
}

fn write_fontawesome_sprite() {
let dest_path = Path::new(&env::var("OUT_DIR").unwrap()).join("fontawesome.rs");
let mut dest_file = File::create(&dest_path).unwrap();
dest_file
.write_all(b"fn fontawesome_svg(dir:&str,file:&str)->&'static str{match(dir,file){")
.expect("fontawesome fn write");
for dirname in &["brands", "regular", "solid"] {
let dir = read_dir(Path::new("fontawesome-free-5.14.0-web/svgs").join(dirname)).unwrap();
let mut data = String::new();
for file in dir {
let file = file.expect("fontawesome directory access");
let filename = file
.file_name()
.into_string()
.expect("fontawesome filenames are unicode");
let mut file = File::open(file.path()).expect("fontawesome file access");
data.clear();
file.read_to_string(&mut data)
.expect("fontawesome file read");
// if this assert goes off, add more hashes here and in the format! below
assert!(
data.find("###").is_none(),
"file {} breaks raw string",
filename,
);
dest_file
.write_all(
format!(
r####"("{dirname}","{filename}")=>r###"{data}"###,"####,
data = data,
dirname = dirname,
filename = filename.replace(".svg", ""),
)
.as_bytes(),
)
.expect("write fontawesome file");
}
}
dest_file
.write_all(b"_=>\"\"}}")
.expect("fontawesome fn write");
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The svgs folder was downloaded from https://fontawesome.com/how-to-use/on-the-web/setup/hosting-font-awesome-yourself
The rest of fontawesome was just deleted; this means that the font license and MIT license are not applicable, just the copyright license on the icon files themselves.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit f2c5f66

Please sign in to comment.