Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: windows build #208

Merged
merged 7 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: don't use harfbuzz on windows
  • Loading branch information
Aloxaf committed Oct 14, 2022
commit 4fe6fcb4b44f7a736b12ddca4fc87c088a5395b4
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:
vcpkgGitCommitId: '6ca56aeb457f033d344a7106cb3f9f1abf8f4e98'

- name: Install dependencies
run: $VCPKG_ROOT/vcpkg install --recurse glib:x64-windows fontconfig:x64-windows freetype:x64-windows cairo:x64-windows harfbuzz[core,glib]:x64-windows
run: $VCPKG_ROOT/vcpkg install --recurse glib:x64-windows fontconfig:x64-windows freetype:x64-windows cairo:x64-windows
shell: bash

- name: Cargo check
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ optional = true
version= "0.11"
features= ["loader-freetype-default"]

[dependencies.harfbuzz-sys]
version="0.5.0"
[target.'cfg(not(target_os = "windows"))'.dependencies]
harfbuzz-sys = "0.5.0"

[target.'cfg(target_os = "macos")'.dependencies]
pasteboard = "0.1.3"
Expand Down
50 changes: 50 additions & 0 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//! font.draw_text_mut(&mut image, Rgb([255, 0, 0]), 0, 0, FontStyle::REGULAR, "Hello, world");
//! ```
use crate::error::FontError;
#[cfg(not(target_os = "windows"))]
use crate::hb_wrapper::{feature_from_tag, HBBuffer, HBFont};
use anyhow::Result;
use conv::ValueInto;
Expand Down Expand Up @@ -193,6 +194,17 @@ impl FontCollection {
Ok(Self(fonts))
}

fn glyph_for_char(&self, c: char, style: FontStyle) -> Option<(u32, &ImageFont, &Font)> {
for font in &self.0 {
let result = font.get_by_style(style);
if let Some(id) = result.glyph_for_char(c) {
return Some((id, font, result));
}
}
eprintln!("[warning] No font found for character `{}`", c);
None
}

/// get max height of all the fonts
pub fn get_font_height(&self) -> u32 {
self.0
Expand All @@ -202,6 +214,7 @@ impl FontCollection {
.unwrap()
}

#[cfg(not(target_os = "windows"))]
fn shape_text(&self, font: &mut HBFont, text: &str) -> Result<Vec<u32>> {
// feature tags
let features = vec![
Expand All @@ -221,6 +234,7 @@ impl FontCollection {
Ok(glyph_ids)
}

#[cfg(not(target_os = "windows"))]
fn layout(&self, text: &str, style: FontStyle) -> (Vec<PositionedGlyph>, u32) {
let mut delta_x = 0;
let height = self.get_font_height();
Expand Down Expand Up @@ -259,6 +273,42 @@ impl FontCollection {
(glyphs, delta_x)
}

#[cfg(target_os = "windows")]
fn layout(&self, text: &str, style: FontStyle) -> (Vec<PositionedGlyph>, u32) {
let mut delta_x = 0;
let height = self.get_font_height();

let glyphs = text
.chars()
.filter_map(|c| {
self.glyph_for_char(c, style).map(|(id, imfont, font)| {
let raster_rect = font
.raster_bounds(
id,
imfont.size,
Transform2F::default(),
HintingOptions::None,
RasterizationOptions::GrayscaleAa,
)
.unwrap();
let position =
Vector2I::new(delta_x as i32, height as i32) + raster_rect.origin();
delta_x += Self::get_glyph_width(font, id, imfont.size);

PositionedGlyph {
id,
font: font.clone(),
size: imfont.size,
raster_rect,
position,
}
})
})
.collect();

(glyphs, delta_x)
}

/// Get the width of the given glyph
fn get_glyph_width(font: &Font, id: u32, size: f32) -> u32 {
let metrics = font.metrics();
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ pub mod directories;
pub mod error;
pub mod font;
pub mod formatter;
#[cfg(not(target_os = "windows"))]
pub mod hb_wrapper;
pub mod utils;