Skip to content

Commit

Permalink
More cleanups, put unicode in the right place
Browse files Browse the repository at this point in the history
  • Loading branch information
CraftSpider committed Feb 7, 2024
1 parent 5214130 commit 456507d
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 51 deletions.
3 changes: 3 additions & 0 deletions crates/bridge_icu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ license = "MIT"
edition = "2018"
links = "icuuc"

[dependencies]
libc = "0.2"

[build-dependencies]
tectonic_dep_support = { path = "../dep_support", version = "0.0.0-dev.0" }

Expand Down
37 changes: 35 additions & 2 deletions crates/bridge_icu/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
// Copyright 2020 the Tectonic Project
// Licensed under the MIT License.

//! No Rust code. This crate exists to export the ICU *C* API into the
//! Cargo framework.
#![allow(nonstandard_style)]

//! This crate exists to export the ICU *C* API into the Cargo framework, as well as
//! provide bindings to other tectonic crates.
pub const UBIDI_DEFAULT_LTR: u8 = 0xFE;
pub const UBIDI_DEFAULT_RTL: u8 = 0xFF;
pub const U_ZERO_ERROR: UErrorCode = 0;

pub type UErrorCode = libc::c_int;
pub type UChar = u16;
pub type UChar32 = i32;

pub fn U_SUCCESS(code: UErrorCode) -> bool {
code <= U_ZERO_ERROR
}

#[repr(C)]
pub struct UConverter(());

extern "C" {
pub fn ucnv_open(name: *const libc::c_char, err: *mut UErrorCode) -> *mut UConverter;
pub fn ucnv_close(conv: *mut UConverter);
pub fn ucnv_toUChars(
conv: *mut UConverter,
dest: *mut UChar,
dest_capacity: i32,
src: *const libc::c_char,
src_len: i32,
p_error_code: *mut UErrorCode,
) -> i32;
pub fn ucnv_fromUChars(
conv: *mut UConverter,
dest: *mut libc::c_char,
dest_capacity: i32,
src: *const UChar,
src_len: i32,
p_error_code: *mut UErrorCode,
) -> i32;
}
2 changes: 1 addition & 1 deletion crates/xetex_layout/src/c_api/fc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::convert::TryFrom;
pub mod pat;
pub mod sys;

pub use pat::Pattern;
pub use pat::{OwnPattern, Pattern};

#[derive(Debug, PartialEq)]
pub enum FcErr {
Expand Down
30 changes: 27 additions & 3 deletions crates/xetex_layout/src/c_api/fc/pat.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{sys, FcErr};
use std::convert::TryInto;
use std::ffi::CStr;
use std::ops::Deref;
use std::ptr;
use std::ptr::NonNull;

Expand Down Expand Up @@ -96,12 +97,35 @@ impl Pattern {
self.0.as_ptr()
}

pub fn from_name(name: &CStr) -> Option<Pattern> {
let raw = unsafe { sys::FcNameParse(name.as_ptr()) };
NonNull::new(raw).map(Pattern)
pub fn as_raw(&self) -> *mut sys::FcPattern {
self.0.as_ptr()
}

pub fn get<T: PatParam>(&self, idx: usize) -> Result<T::Output<'_>, FcErr> {
T::get(self, idx)
}
}

#[derive(PartialEq, Eq, Hash)]
pub struct OwnPattern(Pattern);

impl OwnPattern {
pub fn from_name(name: &CStr) -> Option<OwnPattern> {
let raw = unsafe { sys::FcNameParse(name.as_ptr()) };
NonNull::new(raw).map(Pattern).map(OwnPattern)
}
}

impl Drop for OwnPattern {
fn drop(&mut self) {
unsafe { sys::FcPatternDestroy(self.0 .0.as_ptr()) };
}
}

impl Deref for OwnPattern {
type Target = Pattern;

fn deref(&self) -> &Self::Target {
&self.0
}
}
2 changes: 1 addition & 1 deletion crates/xetex_layout/src/c_api/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub struct NameCollection {
pub trait FontManagerBackend {
unsafe fn initialize(&mut self);
unsafe fn terminate(&mut self);
unsafe fn get_platform_font_desc<'a>(&'a self, font: &'a PlatformFontRef) -> &'a CStr;
fn get_platform_font_desc<'a>(&'a self, font: &'a PlatformFontRef) -> &'a CStr;
unsafe fn get_op_size_rec_and_style_flags(&self, font: &mut Font);
unsafe fn search_for_host_platform_fonts(&mut self, maps: &mut FontMaps, name: &CStr);
unsafe fn read_names(&self, font: PlatformFontRef) -> NameCollection;
Expand Down
15 changes: 7 additions & 8 deletions crates/xetex_layout/src/c_api/manager/fc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ use crate::c_api::fc::sys::{
FC_FULLNAME, FC_INDEX, FC_SLANT, FC_STYLE, FC_WEIGHT, FC_WIDTH,
};
use crate::c_api::font::XeTeXFontBase;
use crate::c_api::unicode::{
ucnv_close, ucnv_fromUChars, ucnv_open, ucnv_toUChars, UConverter, U_SUCCESS, U_ZERO_ERROR,
};
use crate::c_api::{fc, Fixed, PlatformFontRef, RsFix2D};
use std::cell::{Cell, RefCell};
use std::ffi::{CStr, CString};
Expand All @@ -18,6 +15,9 @@ use tectonic_bridge_freetype2::{
FT_IS_SFNT, TT_MAC_ID_ROMAN, TT_OS2, TT_PLATFORM_APPLE_UNICODE, TT_PLATFORM_MACINTOSH,
TT_PLATFORM_MICROSOFT,
};
use tectonic_bridge_icu::{
ucnv_close, ucnv_fromUChars, ucnv_open, ucnv_toUChars, UConverter, U_SUCCESS, U_ZERO_ERROR,
};

pub const FONT_FAMILY_NAME: libc::c_ushort = 1;
pub const FONT_STYLE_NAME: libc::c_ushort = 2;
Expand Down Expand Up @@ -126,7 +126,7 @@ impl FontManagerBackend for FcBackend {
panic!("cannot read font names");
}

let pat = FcNameParse(c!(":outline=true"));
let pat = fc::OwnPattern::from_name(cstr!(":outline=true")).unwrap();
let os = FcObjectSetBuild(
FC_FAMILY,
FC_STYLE,
Expand All @@ -139,9 +139,8 @@ impl FontManagerBackend for FcBackend {
FC_FONTFORMAT,
ptr::null::<()>(),
);
self.all_fonts = FcFontList(FcConfigGetCurrent(), pat, os);
self.all_fonts = FcFontList(FcConfigGetCurrent(), pat.as_raw(), os);
FcObjectSetDestroy(os);
FcPatternDestroy(pat);
self.cached_all = false;
}

Expand All @@ -165,11 +164,11 @@ impl FontManagerBackend for FcBackend {
}
}

unsafe fn get_platform_font_desc<'a>(&'a self, font: &'a PlatformFontRef) -> &'a CStr {
fn get_platform_font_desc<'a>(&'a self, font: &'a PlatformFontRef) -> &'a CStr {
if let Ok(str) = font.get::<fc::pat::File>(0) {
str
} else {
CStr::from_ptr(c!("[unknown]"))
cstr!("[unknown]")
}
}

Expand Down
34 changes: 0 additions & 34 deletions crates/xetex_layout/src/c_api/unicode.rs

This file was deleted.

11 changes: 9 additions & 2 deletions crates/xetex_layout/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ mod linkage {
use tectonic_bridge_icu as clipyrenamehack5;
}

macro_rules! cstr {
($lit:literal) => {
// SAFETY: C string passed to from_ptr guaranteed to end with a null due to concat!
unsafe {
::std::ffi::CStr::from_ptr(concat!($lit, "\0") as *const str as *const ::libc::c_char)
}
};
}

macro_rules! c {
($lit:literal) => {
concat!($lit, "\0") as *const str as *const libc::c_char
Expand All @@ -47,8 +56,6 @@ mod c_api {
mod fc;
mod font;
mod manager;
/// cbindgen:ignore
mod unicode;

pub(crate) struct SyncPtr<T>(*mut T);
unsafe impl<T> Send for SyncPtr<T> {}
Expand Down

0 comments on commit 456507d

Please sign in to comment.