Skip to content

Commit

Permalink
feat: rework loader, font mod rename to family
Browse files Browse the repository at this point in the history
  • Loading branch information
7sDream committed Nov 12, 2023
1 parent 9c99a27 commit a61feb8
Show file tree
Hide file tree
Showing 20 changed files with 306 additions and 352 deletions.
27 changes: 9 additions & 18 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ httparse = "1"
crossterm = "0.27"
fontdb = "0.16"
ab_glyph = "0.2"
owned_ttf_parser = "0.20.0"
thiserror = "1.0.50"
owned_ttf_parser = "0.20"
thiserror = "1"

[dependencies.clap]
version = "4"
Expand Down
1 change: 0 additions & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ reorder_modules = false
use_field_init_shorthand = true
use_small_heuristics = "Max"
use_try_shorthand = true
where_single_line = true
wrap_comments = true
4 changes: 2 additions & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub struct Args {
#[arg(short, long)]
pub preview: bool,

/// Enable Terminal UI mode
/// enable this mode will disable the --preview/-p and ignore --verbose/-v arg
/// Enable Terminal UI mode.
/// enable this mode will disable the --preview/-p and ignore --verbose/-v option
#[arg(short, long)]
pub tui: bool,

Expand Down
60 changes: 60 additions & 0 deletions src/family.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// FontFor: find fonts which can show a specified character
// Copyright (C) 2019 - 2020 7sDream <[email protected]> and contributors
//
// This file is part of FontFor.
//
// FontFor is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use std::collections::HashMap;

use super::loader::FaceInfo;

pub struct Family<'a> {
pub name: &'a str,
pub faces: Vec<&'a FaceInfo>,
pub default_name_width: usize,
}

impl<'a> Family<'a> {
pub fn new(name: &'a str) -> Self {
Self { name, faces: vec![], default_name_width: name.len() }
}

pub fn styles_count(&self) -> usize {
self.faces.len()
}

pub fn add_face(&mut self, face: &'a FaceInfo) {
self.faces.push(face);
}
}

pub fn group_by_family_sort_by_name(faces: &Vec<FaceInfo>) -> Vec<Family<'_>> {
let mut families = HashMap::new();

faces.iter().for_each(|face| {
let family = &face.family;
families.entry(family).or_insert_with(|| Family::new(family)).add_face(face);
});

let mut families: Vec<Family<'_>> = families.into_values().collect();

families.sort_by_key(|f| f.name);

for family in &mut families {
family.faces.sort_unstable_by(|a, b| a.name.cmp(&b.name))
}

return families;
}
117 changes: 0 additions & 117 deletions src/font.rs

This file was deleted.

29 changes: 0 additions & 29 deletions src/loader/charset.rs

This file was deleted.

51 changes: 51 additions & 0 deletions src/loader/cmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// FontFor: find fonts which can show a specified character
// Copyright (C) 2019 - 2020 7sDream <[email protected]> and contributors
//
// This file is part of FontFor.
//
// FontFor is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use owned_ttf_parser::{cmap, GlyphId, RawFace};

use super::{
error::{BROKEN_CMAP_TABLE, CMAP_TAG, MISSING_CMAP_TABLE},
Result,
};

#[derive(Debug)]
pub struct CMapTable<'a> {
subtables: Vec<cmap::Subtable<'a>>,
}

impl<'a> CMapTable<'a> {
pub fn parse(rf: RawFace<'a>) -> Result<Self> {
let cmap_data = rf.table(CMAP_TAG).ok_or(MISSING_CMAP_TABLE)?;
let table = cmap::Table::parse(cmap_data).ok_or(BROKEN_CMAP_TABLE)?;

let mut subtables = vec![];

for i in 0..table.subtables.len() {
let subtable = table.subtables.get(i).ok_or(BROKEN_CMAP_TABLE)?;
if subtable.is_unicode() {
subtables.push(subtable)
}
}

Ok(Self { subtables })
}

pub fn glyph_index(&self, c: char) -> Option<GlyphId> {
self.subtables.iter().filter_map(|subtable| subtable.glyph_index(c as u32)).next()
}
}
26 changes: 26 additions & 0 deletions src/loader/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use owned_ttf_parser::{FaceParsingError, Tag};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
#[error("Font face has no family name")]
MissingFamilyName,
#[error("Font face has no {0} table")]
MissingRequiredTable(Tag),
#[error("Parse {0} table of this font failed")]
ParseTableFailed(Tag),
#[error("Parse font face failed: {0}")]
RawFontParseFailed(
#[source]
#[from]
FaceParsingError,
),
}

pub const NAME_TAG: Tag = Tag::from_bytes(b"name");
pub const MISSING_NAME_TABLE: Error = Error::MissingRequiredTable(NAME_TAG);
pub const BROKEN_NAME_TABLE: Error = Error::ParseTableFailed(NAME_TAG);

pub const CMAP_TAG: Tag = Tag::from_bytes(b"cmap");
pub const MISSING_CMAP_TABLE: Error = Error::MissingRequiredTable(CMAP_TAG);
pub const BROKEN_CMAP_TABLE: Error = Error::ParseTableFailed(CMAP_TAG);
Loading

0 comments on commit a61feb8

Please sign in to comment.