Skip to content

Commit

Permalink
Fix no_std build
Browse files Browse the repository at this point in the history
This fixes the `no_std` build and also makes sure it's tested properly
in the CI workflow.
  • Loading branch information
CryZe authored and jackpot51 committed Feb 12, 2024
1 parent 0cb6eba commit 8582173
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ self_cell = "1.0.1"
swash = { version = "0.1.12", optional = true }
syntect = { version = "5.1.0", optional = true }
sys-locale = { version = "0.3.1", optional = true }
ttf-parser = "0.20.0"
ttf-parser = { version = "0.20.0", default-features = false }
unicode-linebreak = "0.1.5"
unicode-script = "0.5.5"
unicode-segmentation = "1.10.1"
Expand All @@ -44,6 +44,7 @@ std = [
"fontdb/std",
"rustybuzz/std",
"sys-locale",
"ttf-parser/std",
"unicode-bidi/std",
]
vi = ["modit", "syntect", "cosmic_undo_2"]
Expand Down
6 changes: 5 additions & 1 deletion ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ cargo fmt --check
echo Build with default features
build

echo Install target for no_std build
# This is necessary because Rust otherwise may silently use std regardless.
rustup target add thumbv8m.main-none-eabihf

echo Build with only no_std feature
build --no-default-features --features no_std
build --no-default-features --features no_std --target thumbv8m.main-none-eabihf

echo Build with only std feature
build --no-default-features --features std
Expand Down
2 changes: 0 additions & 2 deletions src/font/fallback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

use alloc::collections::BTreeSet;
use alloc::sync::Arc;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use fontdb::Family;
use unicode_script::Script;

Expand Down
5 changes: 3 additions & 2 deletions src/glyph_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ pub enum SubpixelBin {

impl SubpixelBin {
pub fn new(pos: f32) -> (i32, Self) {
let (fract, truncf) = libm::modff(pos);
let trunc = truncf as i32;
let trunc = pos as i32;
let fract = pos - trunc as f32;

if pos.is_sign_negative() {
if fract > -0.125 {
(trunc, Self::Zero)
Expand Down
4 changes: 2 additions & 2 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::fmt::Display;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use crate::{CacheKey, CacheKeyFlags, Color};
use crate::{math, CacheKey, CacheKeyFlags, Color};

/// A laid out glyph
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -75,7 +75,7 @@ impl LayoutGlyph {
self.font_size * scale,
(
(self.x + x_offset) * scale + offset.0,
libm::truncf((self.y - y_offset) * scale + offset.1), // Hinting in Y axis
math::truncf((self.y - y_offset) * scale + offset.1), // Hinting in Y axis
),
self.cache_key_flags,
);
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ pub use self::swash::*;
#[cfg(feature = "swash")]
mod swash;

mod math;

type BuildHasher = core::hash::BuildHasherDefault<rustc_hash::FxHasher>;

#[cfg(feature = "std")]
Expand Down
14 changes: 14 additions & 0 deletions src/math.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#[cfg(not(feature = "std"))]
pub use libm::{roundf, truncf};

#[cfg(feature = "std")]
#[inline]
pub fn roundf(x: f32) -> f32 {
x.round()
}

#[cfg(feature = "std")]
#[inline]
pub fn truncf(x: f32) -> f32 {
x.trunc()
}
5 changes: 3 additions & 2 deletions src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use unicode_segmentation::UnicodeSegmentation;

use crate::fallback::FontFallbackIter;
use crate::{
Align, AttrsList, CacheKeyFlags, Color, Font, FontSystem, LayoutGlyph, LayoutLine,
math, Align, AttrsList, CacheKeyFlags, Color, Font, FontSystem, LayoutGlyph, LayoutLine,
ShapePlanCache, Wrap,
};

Expand Down Expand Up @@ -1394,7 +1394,8 @@ impl ShapeLine {
if glyph_em_width != match_em_width =>
{
let glyph_to_match_factor = glyph_em_width / match_em_width;
let glyph_font_size = glyph_to_match_factor.round().max(1.0)
let glyph_font_size = math::roundf(glyph_to_match_factor)
.max(1.0)
/ glyph_to_match_factor
* font_size;
log::trace!("Adjusted glyph font size ({font_size} => {glyph_font_size})");
Expand Down

0 comments on commit 8582173

Please sign in to comment.