Skip to content

Commit

Permalink
fix: font fallback for harfbuzz
Browse files Browse the repository at this point in the history
close #191
  • Loading branch information
Aloxaf committed Oct 14, 2022
1 parent 2ed9199 commit f71e1a0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "silicon"
version = "0.5.0"
version = "0.5.1"
description = "Create beautiful image of your code"
authors = ["Aloxaf <aloxafx@gmail.com>"]
categories = ["command-line-utilities"]
Expand Down
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@ cargo install silicon

### AUR

Silicon is available on AUR (Thanks to @radmen).
Silicon is available in the official repository:

You can install it with any AUR helpers you like.

eg.
```bash
pikaur -S silicon
pacman -S silicon
```

### Homebrew
Expand All @@ -61,18 +58,18 @@ brew install silicon
```bash
sudo apt install expat
sudo apt install libxml2-dev
sudo apt install pkg-config libasound2-dev libssl-dev cmake libfreetype6-dev libexpat1-dev libxcb-composite0-dev
sudo apt install pkg-config libasound2-dev libssl-dev cmake libfreetype6-dev libexpat1-dev libxcb-composite0-dev libharfbuzz-dev
```

### Fedora
```bash
sudo dnf install cmake expat-devel libxcb-devel freetype-devel libxml2-devel
sudo dnf install cmake expat-devel libxcb-devel freetype-devel libxml2-devel harfbuzz
```

### Arch Linux

```bash
sudo pacman -S --needed pkgconf freetype2 fontconfig libxcb xclip
sudo pacman -S --needed pkgconf freetype2 fontconfig libxcb xclip harfbuzz
```

## Examples
Expand Down
38 changes: 26 additions & 12 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use pathfinder_geometry::transform2d::Transform2F;
use std::collections::HashMap;
use std::sync::Arc;
use syntect::highlighting;
use log::trace;

/// Font style
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
Expand Down Expand Up @@ -194,7 +195,6 @@ impl FontCollection {
Ok(Self(fonts))
}

#[cfg(target_os = "windows")]
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);
Expand Down Expand Up @@ -235,20 +235,34 @@ impl FontCollection {
Ok(glyph_ids)
}

#[cfg(not(target_os = "windows"))]
fn split_by_font(&self, text: &str, style: FontStyle) -> Vec<(&ImageFont, &Font, String)> {
let mut result: Vec<(&ImageFont, &Font, String)> = vec![];
for c in text.chars() {
if let Some((_, imfont, font)) = self.glyph_for_char(c, style) {
if result.is_empty() || !std::ptr::eq(result.last().unwrap().0, imfont) {
result.push((imfont, font, String::new()));
}
if std::ptr::eq(result.last().unwrap().0, imfont) {
result.last_mut().unwrap().2.push(c);
}
}
}
trace!("{:#?}", &result);
result
}

#[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();

let imfont = self.0.get(0).unwrap();
let font = imfont.get_by_style(style);
let mut hb_font = HBFont::new(font);
// apply font features especially ligature with a shape engine
let shaped_glyphs = self.shape_text(&mut hb_font, text).unwrap();

let glyphs = shaped_glyphs
.iter()
.map(|id| {
let mut glyphs = Vec::with_capacity(text.len());
for (imfont, font, text) in self.split_by_font(text, style) {
let mut hb_font = HBFont::new(font);
// apply font features especially ligature with a shape engine
let shaped_glyphs = self.shape_text(&mut hb_font, &text).unwrap();
glyphs.extend(shaped_glyphs.iter().map(|id| {
let raster_rect = font
.raster_bounds(
*id,
Expand All @@ -268,8 +282,8 @@ impl FontCollection {
raster_rect,
position,
}
})
.collect();
}))
}

(glyphs, delta_x)
}
Expand Down

0 comments on commit f71e1a0

Please sign in to comment.