-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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<'_>> { | ||
Check failure on line 43 in src/family.rs GitHub Actions / clippywriting `&Vec` instead of `&[_]` involves a new object where a slice will do
Check failure on line 43 in src/family.rs GitHub Actions / clippywriting `&Vec` instead of `&[_]` involves a new object where a slice will do
|
||
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; | ||
Check failure on line 59 in src/family.rs GitHub Actions / clippyunneeded `return` statement
Check failure on line 59 in src/family.rs GitHub Actions / clippyunneeded `return` statement
|
||
} |
This file was deleted.
This file was deleted.
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() | ||
} | ||
} |
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); |