Skip to content

Commit

Permalink
Write HTML and copy font files to output directory
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncozens committed May 17, 2024
1 parent 822c2f4 commit ac03b00
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 40 deletions.
81 changes: 57 additions & 24 deletions src/bin/diffenator3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use diffenator3::{
ttj::{jsondiff::Substantial, table_diff},
};
use serde_json::Map;
use std::{error::Error, path::PathBuf};
use std::{
error::Error,
path::{Path, PathBuf},
};

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
Expand Down Expand Up @@ -51,6 +54,10 @@ struct Cli {
#[clap(long = "html")]
html: bool,

/// Output directory for HTML
#[clap(long = "output", default_value = "out", requires = "html")]
output: String,

/// Location in design space, in the form axis=123,other=456
#[clap(long = "location")]
location: Option<String>,
Expand Down Expand Up @@ -117,12 +124,12 @@ fn main() {

let mut font_a = DFont::new(&font_binary_a);
let mut font_b = DFont::new(&font_binary_b);
if let Some(loc) = cli.location {
let _hack = font_a.set_location(&loc);
let _hack = font_b.set_location(&loc);
} else if let Some(inst) = cli.instance {
font_a.set_instance(&inst).expect("Couldn't find instance");
font_b.set_instance(&inst).expect("Couldn't find instance");
if let Some(ref loc) = cli.location {
let _hack = font_a.set_location(loc);
let _hack = font_b.set_location(loc);
} else if let Some(ref inst) = cli.instance {
font_a.set_instance(inst).expect("Couldn't find instance");
font_b.set_instance(inst).expect("Couldn't find instance");
}
let mut diff = Map::new();
if cli.tables {
Expand All @@ -142,23 +149,7 @@ fn main() {
diff.insert("words".into(), word_diff);
}
if cli.html {
let font_face_old = CSSFontFace::new(cli.font1.to_str().unwrap(), "old", &font_a);
let font_face_new = CSSFontFace::new(cli.font2.to_str().unwrap(), "new", &font_b);
let font_style_old = CSSFontStyle::new(&font_a, Some("old"));
let font_style_new = CSSFontStyle::new(&font_b, Some("new"));
let value = serde_json::to_value(&diff).unwrap_or_else(|e| {
die("serializing diff", e);
});
let html = render_output(
&value,
font_face_old,
font_face_new,
font_style_old,
font_style_new,
)
.unwrap_or_else(|err| die("rendering HTML", err));
println!("{}", html);
std::process::exit(0);
do_html(&cli, &font_a, &font_b, diff);
}
if cli.json {
println!("{}", serde_json::to_string_pretty(&diff).expect("foo"));
Expand Down Expand Up @@ -220,3 +211,45 @@ fn main() {
}
}
}

fn do_html(cli: &Cli, font_a: &DFont, font_b: &DFont, diff: Map<String, serde_json::Value>) -> ! {
// Make output directory
let output_dir = Path::new(&cli.output);
if !output_dir.exists() {
std::fs::create_dir(output_dir).expect("Couldn't create output directory");
}

// Copy old font to output/old-<existing name>
let old_font = output_dir.join(format!(
"old-{}",
cli.font1.file_name().unwrap().to_str().unwrap()
));
std::fs::copy(&cli.font1, &old_font).expect("Couldn't copy old font");
let new_font = output_dir.join(format!(
"new-{}",
cli.font2.file_name().unwrap().to_str().unwrap()
));
std::fs::copy(&cli.font2, &new_font).expect("Couldn't copy new font");

let font_face_old = CSSFontFace::new(&old_font, "old", font_a);
let font_face_new = CSSFontFace::new(&new_font, "new", font_b);
let font_style_old = CSSFontStyle::new(font_a, Some("old"));
let font_style_new = CSSFontStyle::new(font_b, Some("new"));
let value = serde_json::to_value(&diff).unwrap_or_else(|e| {
die("serializing diff", e);
});
let html = render_output(
&value,
font_face_old,
font_face_new,
font_style_old,
font_style_new,
)
.unwrap_or_else(|err| die("rendering HTML", err));

// Write output
let output_file = output_dir.join("diffenator.html");
println!("Writing output to {}", output_file.to_str().unwrap());
std::fs::write(output_file, html).expect("Couldn't write output file");
std::process::exit(0);
}
15 changes: 10 additions & 5 deletions src/html.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{collections::HashMap, path::Path};

use lazy_static::lazy_static;
use serde::Serialize;
Expand All @@ -20,7 +20,7 @@ pub struct CSSFontFace {
}

impl CSSFontFace {
pub fn new(filename: &str, suffix: &str, dfont: &DFont) -> Self {
pub fn new(filename: &Path, suffix: &str, dfont: &DFont) -> Self {
let familyname = suffix.to_string() + " " + &dfont.family_name();
let cssfamilyname = familyname.clone();
let class_name = cssfamilyname.replace(' ', "-");
Expand All @@ -30,7 +30,12 @@ impl CSSFontFace {

CSSFontFace {
suffix: suffix.to_string(),
filename: filename.to_string(),
filename: filename
.file_name()
.unwrap()
.to_str()
.unwrap_or_default()
.to_string(),
familyname,
cssfamilyname,
class_name,
Expand Down Expand Up @@ -93,7 +98,7 @@ lazy_static! {
match Tera::new("templates/*") {
Ok(t) => t,
Err(e) => {
println!("Parsing error(s): {}", e);
eprintln!("Parsing error(s): {}", e);
::std::process::exit(1);
}
}
Expand Down Expand Up @@ -121,7 +126,7 @@ pub fn render_output(
"font_styles_old": [font_style_old],
"font_styles_new": [font_style_new],
"font_styles": [],
"pt_size": 20,
"pt_size": 40,
"include_ui": true,
}))?,
)
Expand Down
14 changes: 7 additions & 7 deletions templates/WordDiff.partial.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<div class="cell-word" style="font-feature-settings: {% for f in ot_features %} '{{ f }}' 1 {% endfor %}" lang="{{ lang }}">
<div class="cell-word" style="font-feature-settings: {% for f in diff.ot_features %} '{{ f }}' 1 {% endfor %}" lang="{{ diff.lang }}">
<div class="tooltip">
{{ string }}<br>
{{ diff.word }}<br>
<span class="tooltiptext">
<div class="tooltipleft">
Before:<br>
{{ hb_a }}
feats={{ ot_features }}
{{ diff.buffer_a }}
feats={{ diff.ot_features }}
</div>
<div class="tooltipright">
After:<br>
{{ hb_b }}
feats={{ ot_features }}
{{ diff.buffer_b }}
feats={{ diff.ot_features }}
</div>
<div>
changed pixels: {{ changed_pixels }}%
changed pixels: {{ diff.percent }}%
</div>
</span>
</div>
Expand Down
8 changes: 4 additions & 4 deletions templates/diffenator.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@
<div class="box">
<div class="box-title">Misshapen {{ script }} words</div>
<div class="box-text {{ font_class.class_name }}" style="font-size: {{ pt_size }}pt">
{% for word in words %}
{{ word }}
{% for diff in words %}
{% include "WordDiff.partial.html" %}
{% endfor %}
</div>
</div>
Expand All @@ -100,8 +100,8 @@
<div class="box">
<div class="box-title">Misshapen user strings</div>
<div class="box-text {{ font_class.class_name }}" style="font-size: {{ pt_size }}pt">
{% for word in diff.strings %}
{{ word }}
{% for diff in diff.strings %}
{% include "WordDiff.partial.html" %}
{% endfor %}
</div>
</div>
Expand Down

0 comments on commit ac03b00

Please sign in to comment.