Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ exclude = ["benches/", "tests/"]
bitflags = "2.9"
bytemuck = { version = "1.22", features = ["extern_crate_alloc"] }
core_maths = "0.1" # only for no_std builds
read-fonts = { version = "0.36.0", default-features = false, features = ["libm"] }
# read-fonts = { git = "https://github.com/googlefonts/fontations", default-features = false, features = ["libm"] }
# read-fonts = { version = "0.36.0", default-features = false, features = ["libm"] }
read-fonts = { git = "https://github.com/googlefonts/fontations", branch = "unchecked-read", default-features = false, features = ["libm"] }
smallvec = "1.14"

[features]
Expand Down
19 changes: 12 additions & 7 deletions src/hb/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::hb::unsafe_vec::UnsafeVec;
use crate::U32Set;
use alloc::{string::String, vec::Vec};
use alloc::string::String;
use core::cmp::min;
use core::convert::TryFrom;
use read_fonts::types::{GlyphId, GlyphId16};
Expand Down Expand Up @@ -420,8 +421,8 @@ pub struct hb_buffer_t {
pub len: usize,
pub out_len: usize,

pub info: Vec<GlyphInfo>,
pub pos: Vec<GlyphPosition>,
pub info: UnsafeVec<GlyphInfo>,
pub pos: UnsafeVec<GlyphPosition>,

// Text before / after the main buffer contents.
// Always in Unicode, and ordered outward.
Expand Down Expand Up @@ -472,8 +473,8 @@ impl hb_buffer_t {
idx: 0,
len: 0,
out_len: 0,
info: Vec::new(),
pos: Vec::new(),
info: UnsafeVec::new(),
pos: UnsafeVec::new(),
have_separate_output: false,
allocated_var_bits: 0,
serial: 0,
Expand Down Expand Up @@ -770,8 +771,12 @@ impl hb_buffer_t {

if self.have_separate_output {
// Swap info and pos buffers.
let info: Vec<GlyphPosition> = bytemuck::cast_vec(core::mem::take(&mut self.info));
let pos: Vec<GlyphInfo> = bytemuck::cast_vec(core::mem::take(&mut self.pos));
let info: UnsafeVec<GlyphPosition> = UnsafeVec {
inner: bytemuck::cast_vec(core::mem::take(&mut self.info)),
};
let pos: UnsafeVec<GlyphInfo> = UnsafeVec {
inner: bytemuck::cast_vec(core::mem::take(&mut self.pos)),
};
self.pos = info;
self.info = pos;
self.have_separate_output = false;
Expand Down
1 change: 1 addition & 0 deletions src/hb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ mod text_parser;
#[rustfmt::skip]
mod ucd_table;
mod unicode;
mod unsafe_vec;

use read_fonts::types::Tag as hb_tag_t;

Expand Down
2 changes: 1 addition & 1 deletion src/hb/ot/gpos/pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl Apply for PairPosFormat2<'_> {
// Compute an offset into the 2D array of positioning records
let record_offset = (class1 as usize * record_size * self.class2_count() as usize)
+ (class2 as usize * record_size)
+ self.shape().class1_records_byte_range().start;
+ self.class1_records_byte_range().start;
let has_record2 = !format2.is_empty();
let worked1 = !format1.is_empty()
&& super::apply_value(ctx, ctx.buffer.idx, &data, record_offset, format1) == Some(true);
Expand Down
5 changes: 2 additions & 3 deletions src/hb/ot/gpos/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ impl Apply for SinglePosFormat1<'_> {
let glyph = ctx.buffer.cur(0).as_glyph();
self.coverage().ok()?.get(glyph)?;
let format = self.value_format();
let offset = self.shape().value_record_byte_range().start;
let offset = self.value_record_byte_range().start;
super::apply_value(ctx, ctx.buffer.idx, &self.offset_data(), offset, format);
ctx.buffer.idx += 1;
Some(())
Expand All @@ -19,8 +19,7 @@ impl Apply for SinglePosFormat2<'_> {
let glyph = ctx.buffer.cur(0).as_glyph();
let index = self.coverage().ok()?.get(glyph)? as usize;
let format = self.value_format();
let offset =
self.shape().value_records_byte_range().start + (format.record_byte_len() * index);
let offset = self.value_records_byte_range().start + (format.record_byte_len() * index);
super::apply_value(ctx, ctx.buffer.idx, &self.offset_data(), offset, format);
ctx.buffer.idx += 1;
Some(())
Expand Down
225 changes: 225 additions & 0 deletions src/hb/unsafe_vec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
use std::fmt;
use std::ops::{
Deref, DerefMut, Index, IndexMut, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo,
RangeToInclusive,
};

#[repr(transparent)]
pub struct UnsafeVec<T> {
pub inner: Vec<T>,
}

impl<T> UnsafeVec<T> {
#[inline]
pub fn new() -> Self {
Self { inner: Vec::new() }
}

#[inline]
pub fn with_capacity(cap: usize) -> Self {
Self {
inner: Vec::with_capacity(cap),
}
}

#[inline]
pub fn len(&self) -> usize {
self.inner.len()
}

#[inline]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}

#[inline]
pub fn push(&mut self, value: T) {
self.inner.push(value);
}

#[inline]
pub fn as_ptr(&self) -> *const T {
self.inner.as_ptr()
}

#[inline]
pub fn as_mut_ptr(&mut self) -> *mut T {
self.inner.as_mut_ptr()
}

#[inline]
pub fn into_vec(self) -> Vec<T> {
self.inner
}

#[inline]
pub fn from_vec(v: Vec<T>) -> Self {
Self { inner: v }
}
}

impl<T> Default for UnsafeVec<T> {
#[inline]
fn default() -> Self {
Self::new()
}
}

impl<T: fmt::Debug> fmt::Debug for UnsafeVec<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.inner.fmt(f)
}
}

/// Expose full Vec<T> API (resize/truncate/extend/etc.)
impl<T> Deref for UnsafeVec<T> {
type Target = Vec<T>;
#[inline]
fn deref(&self) -> &Vec<T> {
&self.inner
}
}
impl<T> DerefMut for UnsafeVec<T> {
#[inline]
fn deref_mut(&mut self) -> &mut Vec<T> {
&mut self.inner
}
}

/// Unchecked single-element indexing (UB if out of bounds)
impl<T> Index<usize> for UnsafeVec<T> {
type Output = T;
#[inline]
fn index(&self, i: usize) -> &T {
unsafe { self.inner.get_unchecked(i) }
}
}
impl<T> IndexMut<usize> for UnsafeVec<T> {
#[inline]
fn index_mut(&mut self, i: usize) -> &mut T {
unsafe { self.inner.get_unchecked_mut(i) }
}
}

/// Unchecked range indexing (UB if invalid range)
impl<T> Index<Range<usize>> for UnsafeVec<T> {
type Output = [T];
#[inline]
fn index(&self, r: Range<usize>) -> &[T] {
unsafe { std::slice::from_raw_parts(self.inner.as_ptr().add(r.start), r.end - r.start) }
}
}
impl<T> IndexMut<Range<usize>> for UnsafeVec<T> {
#[inline]
fn index_mut(&mut self, r: Range<usize>) -> &mut [T] {
unsafe {
std::slice::from_raw_parts_mut(self.inner.as_mut_ptr().add(r.start), r.end - r.start)
}
}
}

impl<T> Index<RangeFrom<usize>> for UnsafeVec<T> {
type Output = [T];
#[inline]
fn index(&self, r: RangeFrom<usize>) -> &[T] {
let len = self.inner.len();
unsafe { std::slice::from_raw_parts(self.inner.as_ptr().add(r.start), len - r.start) }
}
}
impl<T> IndexMut<RangeFrom<usize>> for UnsafeVec<T> {
#[inline]
fn index_mut(&mut self, r: RangeFrom<usize>) -> &mut [T] {
let len = self.inner.len();
unsafe {
std::slice::from_raw_parts_mut(self.inner.as_mut_ptr().add(r.start), len - r.start)
}
}
}

impl<T> Index<RangeTo<usize>> for UnsafeVec<T> {
type Output = [T];
#[inline]
fn index(&self, r: RangeTo<usize>) -> &[T] {
unsafe { std::slice::from_raw_parts(self.inner.as_ptr(), r.end) }
}
}
impl<T> IndexMut<RangeTo<usize>> for UnsafeVec<T> {
#[inline]
fn index_mut(&mut self, r: RangeTo<usize>) -> &mut [T] {
unsafe { std::slice::from_raw_parts_mut(self.inner.as_mut_ptr(), r.end) }
}
}

impl<T> Index<RangeFull> for UnsafeVec<T> {
type Output = [T];
#[inline]
fn index(&self, _: RangeFull) -> &[T] {
&self.inner
}
}
impl<T> IndexMut<RangeFull> for UnsafeVec<T> {
#[inline]
fn index_mut(&mut self, _: RangeFull) -> &mut [T] {
&mut self.inner
}
}

impl<T> Index<RangeInclusive<usize>> for UnsafeVec<T> {
type Output = [T];
#[inline]
fn index(&self, r: RangeInclusive<usize>) -> &[T] {
let start = *r.start();
let end_incl = *r.end();
unsafe { std::slice::from_raw_parts(self.inner.as_ptr().add(start), end_incl - start + 1) }
}
}
impl<T> IndexMut<RangeInclusive<usize>> for UnsafeVec<T> {
#[inline]
fn index_mut(&mut self, r: RangeInclusive<usize>) -> &mut [T] {
let start = *r.start();
let end_incl = *r.end();
unsafe {
std::slice::from_raw_parts_mut(self.inner.as_mut_ptr().add(start), end_incl - start + 1)
}
}
}

impl<T> Index<RangeToInclusive<usize>> for UnsafeVec<T> {
type Output = [T];
#[inline]
fn index(&self, r: RangeToInclusive<usize>) -> &[T] {
unsafe { std::slice::from_raw_parts(self.inner.as_ptr(), r.end + 1) }
}
}
impl<T> IndexMut<RangeToInclusive<usize>> for UnsafeVec<T> {
#[inline]
fn index_mut(&mut self, r: RangeToInclusive<usize>) -> &mut [T] {
unsafe { std::slice::from_raw_parts_mut(self.inner.as_mut_ptr(), r.end + 1) }
}
}

/// Iteration support: `for x in &v`, `for x in &mut v`, `for x in v`
impl<'a, T> IntoIterator for &'a UnsafeVec<T> {
type Item = &'a T;
type IntoIter = std::slice::Iter<'a, T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.inner.iter()
}
}
impl<'a, T> IntoIterator for &'a mut UnsafeVec<T> {
type Item = &'a mut T;
type IntoIter = std::slice::IterMut<'a, T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.inner.iter_mut()
}
}
impl<T> IntoIterator for UnsafeVec<T> {
type Item = T;
type IntoIter = std::vec::IntoIter<T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.inner.into_iter()
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A complete [harfbuzz](https://github.com/harfbuzz/harfbuzz) shaping algorithm po
#![cfg_attr(not(feature = "std"), no_std)]
// Forbidding unsafe code only applies to the lib
// examples continue to use it, so this cannot be placed into Cargo.toml
#![forbid(unsafe_code)]
//#![forbid(unsafe_code)]
#![warn(missing_docs)]

extern crate alloc;
Expand Down
Loading