Skip to content

Commit

Permalink
feat(studio): instant font preview
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiphoseer committed Jan 21, 2025
1 parent a5f0500 commit bd75ad0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 59 deletions.
133 changes: 75 additions & 58 deletions crates/sdo-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,44 +485,51 @@ impl Handle {
}

async fn show_staged(&mut self, name: &str) -> Result<(), JsValue> {
let heading = self.document.create_element("h2")?;
heading.set_text_content(Some(name));
self.output.append_child(&heading)?;

let file = self.input_file(name)?;
let data = js_file_data(&file).await?.to_vec();

if let Ok((_, four_cc)) = four_cc(&data) {
match four_cc {
FourCC::SDOC => {
let sdoc = self.parse_sdoc(&data)?;
self.fc.reset();
let dfci = self.fc.load(&self.fs, &sdoc.cset).await;
let pd = match dfci.print_driver(None) {
Some(pd) => pd,
None => {
// FIXME: pick the "best" format?
log::warn!(
"Could not auto-select a font format, some fonts are not available"
);
FontKind::Printer(PrinterKind::Needle24)
}
};
let images = sdoc
.hcim
.as_ref()
.map(|hcim| hcim.decode_images())
.unwrap_or_default();

self.active = Some(ActiveDocument {
sdoc: sdoc.into_owned(),
di: DocumentInfo::new(dfci, images),
pd,
name: name.to_owned(),
});
let (_, four_cc) =
four_cc(&data).map_err(|_| JsError::new("File has less than 4 bytes"))?;

if four_cc == FourCC::SDOC {
let heading = self.document.create_element("h2")?;
heading.set_text_content(Some(name));
self.output.append_child(&heading)?;

let sdoc = self.parse_sdoc(&data)?;
self.fc.reset();
let dfci = self.fc.load(&self.fs, &sdoc.cset).await;
let pd = match dfci.print_driver(None) {
Some(pd) => pd,
None => {
// FIXME: pick the "best" format?
log::warn!("Could not auto-select a font format, some fonts are not available");
FontKind::Printer(PrinterKind::Needle24)
}
_ => warn!("Unknown format: {}", four_cc),
}
};
let images = sdoc
.hcim
.as_ref()
.map(|hcim| hcim.decode_images())
.unwrap_or_default();

self.active = Some(ActiveDocument {
sdoc: sdoc.into_owned(),
di: DocumentInfo::new(dfci, images),
pd,
name: name.to_owned(),
});
} else if let Some(font_kind) = Option::<FontKind>::from(four_cc) {
self.show_font(font_kind, name, &data).await?;
} else {
warn!("Unknown format: {}", four_cc);
let heading = self.document.create_element("h2")?;
heading.set_text_content(Some(name));
self.output.append_child(&heading)?;
let p = self.document.create_element("p")?;
p.append_with_str_1("Unknown format: ")?;
p.append_with_str_1(&four_cc.as_bstr().to_string())?;
self.output.append_child(&p)?;
}
Ok(())
}
Expand Down Expand Up @@ -583,37 +590,47 @@ impl Handle {
Ok(())
}

async fn show_font(
&mut self,
font_kind: FontKind,
name: &str,
data: &[u8],
) -> Result<(), JsValue> {
let h2 = self.document.create_element("h2")?;
h2.set_text_content(Some(name));
h2.append_with_str_1(" ")?;

let small = self.document.create_element("small")?;
small
.class_list()
.add_2("text-secondary", "d-inline-block")?;
small.set_text_content(Some(font_kind.file_format_name()));
h2.append_child(&small)?;

self.output.append_child(&h2)?;

match font_kind {
FontKind::Editor => {
let eset = self.parse_eset(data)?;
self.show_eset(&eset)?;
}
FontKind::Printer(_) => {
let pset = self.parse_pset(data)?;
self.show_pset(&pset)?;
}
}
Ok(())
}

async fn show_chset(&mut self, name: &str) -> Result<(), JsValue> {
let chsets = self.fs.chset_dir().await?;
let file_handle = js_directory_get_file_handle(&chsets, name).await?;
let file = fs_file_handle_get_file(&file_handle).await?;
let arr = js_file_data(&file).await?;
let four_cc = js_four_cc(&arr).ok_or(js_sys::Error::new("No four-cc: file too short"))?;
if let Some(font_kind) = Option::<FontKind>::from(four_cc) {
let _data = arr.to_vec();
let h2 = self.document.create_element("h2")?;
h2.set_text_content(Some(name));
h2.append_with_str_1(" ")?;

let small = self.document.create_element("small")?;
small
.class_list()
.add_2("text-secondary", "d-inline-block")?;
small.set_text_content(Some(font_kind.file_format_name()));
h2.append_child(&small)?;

self.output.append_child(&h2)?;

match font_kind {
FontKind::Editor => {
let eset = self.parse_eset(&_data)?;
self.show_eset(&eset)?;
}
FontKind::Printer(_) => {
let pset = self.parse_pset(&_data)?;
self.show_pset(&pset)?;
}
}
let data = arr.to_vec();
self.show_font(font_kind, name, &data).await?;
}
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/signum/src/chsets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl FromStr for FontKind {

fn from_str(input: &str) -> Result<Self, Self::Err> {
match input {
"P09" => Ok(Self::Printer(PrinterKind::Needle9)),
"P9" | "P09" => Ok(Self::Printer(PrinterKind::Needle9)),
"E24" => Ok(Self::Editor),
"P24" => Ok(Self::Printer(PrinterKind::Needle24)),
"L30" => Ok(Self::Printer(PrinterKind::Laser30)),
Expand Down

0 comments on commit bd75ad0

Please sign in to comment.