From 18b977b81a9c47f904848e09427027a6acb0eca0 Mon Sep 17 00:00:00 2001
From: 7sDream <7822577+7sDream@users.noreply.github.com>
Date: Wed, 15 Nov 2023 16:35:58 +0800
Subject: [PATCH] feat(loader): support custom font paths (#65)
Add -I/--include option to add custom font paths.
Fixes #62.
---
CHANGELOG.md | 1 +
src/args.rs | 7 +++++++
src/loader/face_info.rs | 14 +++++++------
src/loader/mod.rs | 35 +++++++++++++++++++++++++-------
src/main.rs | 3 ++-
src/preview/terminal/ui/state.rs | 6 +++---
6 files changed, 49 insertions(+), 17 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6cebbb9..115825c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,7 @@
- Remove dependency of fontconfig and freetype lib
- Add `-vv` option to show font file location and face index
- ASCII mode render result now not narrow (Issue [#61](https://github.com/7sDream/fontfor/issues/61), fixed by PR [#63](https://github.com/7sDream/fontfor/pull/63))
+- Support custom font paths (Issue [#62](https://github.com/7sDream/fontfor/issues/62))
- Now release contains ia32/x64/arm64 binary for Windows
- Now release contains x64/arm64 binary for macOS
- Now release contains x64/arm64/armhf binary Linux
diff --git a/src/args.rs b/src/args.rs
index ac8d48c..573ab89 100644
--- a/src/args.rs
+++ b/src/args.rs
@@ -16,6 +16,8 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
+use std::path::PathBuf;
+
use clap::Parser;
use super::one_char::OneChar;
@@ -36,6 +38,11 @@ pub struct Args {
#[arg(short, long)]
pub tui: bool,
+ /// Also load fonts in a custom path.
+ /// This arg can be provided multiple times.
+ #[arg(short = 'I', long = "include", name = "PATH", action = clap::ArgAction::Append)]
+ pub custom_font_paths: Vec,
+
/// The character
#[arg(name = "CHAR")]
pub char: OneChar,
diff --git a/src/loader/face_info.rs b/src/loader/face_info.rs
index a3219d4..f5fd363 100644
--- a/src/loader/face_info.rs
+++ b/src/loader/face_info.rs
@@ -18,16 +18,18 @@
use std::{borrow::Cow, path::Path};
+use fontdb::Source;
use ttf_parser::{
name::{name_id, Table as NameTable},
- GlyphId, Language, RawFace,
+ Language, RawFace,
};
use super::{
cmap::CMapTable,
error::{BROKEN_NAME_TABLE, MISSING_NAME_TABLE, NAME_TAG},
- Error, Result, DATABASE,
+ Error, Result,
};
+use crate::loader::database;
/// FaceInfo contains basic font face info like family and name,
/// and pre-located glyph id for target character.
@@ -40,12 +42,12 @@ pub struct FaceInfo {
pub path: &'static Path,
pub index: u32,
- pub gid: GlyphId,
+ pub gid: u16,
}
impl FaceInfo {
pub fn parse_if_contains(face: &'static fontdb::FaceInfo, c: char) -> Result