feat: introduce harfbuzz feature (#209)

This commit is contained in:
Aloxaf 2022-10-14 15:45:11 +08:00 committed by GitHub
parent 7039029222
commit 447c456385
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 18 deletions

View File

@ -60,6 +60,34 @@ jobs:
test-windows: test-windows:
name: x86_64-pc-windows-msvc name: x86_64-pc-windows-msvc
runs-on: windows-latest runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v1
with:
fetch-depth: 1
- name: Install rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
- name: Cargo check
uses: actions-rs/cargo@v1
with:
command: check
args: --no-default-features --features=bin
- name: Cargo test
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features --features=bin -- --nocapture
test-macos:
name: x86_64-apple-darwin
runs-on: macos-latest
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v1 uses: actions/checkout@v1

View File

@ -95,7 +95,7 @@ jobs:
- uses: actions-rs/cargo@v1 - uses: actions-rs/cargo@v1
with: with:
command: build command: build
args: --release args: --release --no-default-features --features=bin
- id: get_name - id: get_name
shell: bash shell: bash
run: | run: |

View File

@ -10,10 +10,11 @@ license = "MIT"
edition = "2018" edition = "2018"
[features] [features]
# fearures required for silicon as a application # bin fearure is required for silicon as a application
# disable it when using as a library # disable it when using as a library
default = ["bin"] default = ["bin", "harfbuzz"]
bin = ["structopt", "env_logger", "anyhow", "shell-words"] bin = ["structopt", "env_logger", "anyhow", "shell-words"]
harfbuzz = ["harfbuzz-sys", "font-kit/loader-freetype-default", "font-kit/source-fontconfig-default"]
[dependencies] [dependencies]
dirs = "4.0" dirs = "4.0"
@ -26,6 +27,8 @@ log = "0.4.11"
lazy_static = "1.4.0" lazy_static = "1.4.0"
shell-words = { version = "1.0.0", optional = true } shell-words = { version = "1.0.0", optional = true }
rayon = "1.5.1" rayon = "1.5.1"
font-kit = "0.11"
harfbuzz-sys = { version = "0.5.0", optional = true }
[dependencies.image] [dependencies.image]
version = "0.24" version = "0.24"
@ -53,13 +56,6 @@ default-features = false
features = ["termcolor", "atty", "humantime"] features = ["termcolor", "atty", "humantime"]
optional = true optional = true
[dependencies.font-kit]
version= "0.11"
features= ["loader-freetype-default"]
[target.'cfg(not(target_os = "windows"))'.dependencies]
harfbuzz-sys = "0.5.0"
[target.'cfg(target_os = "macos")'.dependencies] [target.'cfg(target_os = "macos")'.dependencies]
pasteboard = "0.1.3" pasteboard = "0.1.3"

View File

@ -36,6 +36,8 @@ It's not as beautiful as Carbon...
cargo install silicon cargo install silicon
``` ```
NOTE: harfbuzz feature is enabled by default. If you are using Windows, I suggest you disable it to get it build easier.
### AUR ### AUR
Silicon is available in the official repository: Silicon is available in the official repository:

View File

@ -12,7 +12,7 @@
//! font.draw_text_mut(&mut image, Rgb([255, 0, 0]), 0, 0, FontStyle::REGULAR, "Hello, world"); //! font.draw_text_mut(&mut image, Rgb([255, 0, 0]), 0, 0, FontStyle::REGULAR, "Hello, world");
//! ``` //! ```
use crate::error::FontError; use crate::error::FontError;
#[cfg(not(target_os = "windows"))] #[cfg(feature = "harfbuzz")]
use crate::hb_wrapper::{feature_from_tag, HBBuffer, HBFont}; use crate::hb_wrapper::{feature_from_tag, HBBuffer, HBFont};
use anyhow::Result; use anyhow::Result;
use conv::ValueInto; use conv::ValueInto;
@ -24,7 +24,6 @@ use font_kit::source::SystemSource;
use image::{GenericImage, Pixel}; use image::{GenericImage, Pixel};
use imageproc::definitions::Clamp; use imageproc::definitions::Clamp;
use imageproc::pixelops::weighted_sum; use imageproc::pixelops::weighted_sum;
use log::trace;
use pathfinder_geometry::transform2d::Transform2F; use pathfinder_geometry::transform2d::Transform2F;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
@ -215,7 +214,7 @@ impl FontCollection {
.unwrap() .unwrap()
} }
#[cfg(not(target_os = "windows"))] #[cfg(feature = "harfbuzz")]
fn shape_text(&self, font: &mut HBFont, text: &str) -> Result<Vec<u32>> { fn shape_text(&self, font: &mut HBFont, text: &str) -> Result<Vec<u32>> {
// feature tags // feature tags
let features = vec![ let features = vec![
@ -235,7 +234,7 @@ impl FontCollection {
Ok(glyph_ids) Ok(glyph_ids)
} }
#[cfg(not(target_os = "windows"))] #[cfg(feature = "harfbuzz")]
fn split_by_font(&self, text: &str, style: FontStyle) -> Vec<(&ImageFont, &Font, String)> { fn split_by_font(&self, text: &str, style: FontStyle) -> Vec<(&ImageFont, &Font, String)> {
let mut result: Vec<(&ImageFont, &Font, String)> = vec![]; let mut result: Vec<(&ImageFont, &Font, String)> = vec![];
for c in text.chars() { for c in text.chars() {
@ -248,11 +247,11 @@ impl FontCollection {
} }
} }
} }
trace!("{:#?}", &result); log::trace!("{:#?}", &result);
result result
} }
#[cfg(not(target_os = "windows"))] #[cfg(feature = "harfbuzz")]
fn layout(&self, text: &str, style: FontStyle) -> (Vec<PositionedGlyph>, u32) { fn layout(&self, text: &str, style: FontStyle) -> (Vec<PositionedGlyph>, u32) {
let mut delta_x = 0; let mut delta_x = 0;
let height = self.get_font_height(); let height = self.get_font_height();
@ -288,7 +287,7 @@ impl FontCollection {
(glyphs, delta_x) (glyphs, delta_x)
} }
#[cfg(target_os = "windows")] #[cfg(not(feature = "harfbuzz"))]
fn layout(&self, text: &str, style: FontStyle) -> (Vec<PositionedGlyph>, u32) { fn layout(&self, text: &str, style: FontStyle) -> (Vec<PositionedGlyph>, u32) {
let mut delta_x = 0; let mut delta_x = 0;
let height = self.get_font_height(); let height = self.get_font_height();

View File

@ -43,6 +43,6 @@ pub mod directories;
pub mod error; pub mod error;
pub mod font; pub mod font;
pub mod formatter; pub mod formatter;
#[cfg(not(target_os = "windows"))] #[cfg(feature = "harfbuzz")]
pub mod hb_wrapper; pub mod hb_wrapper;
pub mod utils; pub mod utils;