Skip to content

Commit

Permalink
Font -> Option<NativeFont>
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Feb 7, 2021
1 parent 1ee9b30 commit 75db98b
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 225 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ derive_more = "0.99.7"
md5 = "0.7"
fontconfig_sys = { package = "yeslogic-fontconfig-sys", version = "2.11.1" }
once_cell = "1.5.2"
enum_dispatch = "0.3.5"

[target.'cfg(target_os = "macos")'.dependencies]
core-foundation = "0.9.1"
Expand Down
6 changes: 2 additions & 4 deletions engine/src/fmt_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,7 @@ pub(crate) unsafe fn store_fmt_file() {
Esc(&PoolString::from(yhash[FONT_ID_BASE + k - hash_offset].s1).to_string())
);

if matches!(&FONT_LAYOUT_ENGINE[k], crate::xetex_ext::Font::Native(_))
|| !(FONT_MAPPING[k]).is_null()
{
if matches!(&FONT_LAYOUT_ENGINE[k], Some(_)) || !(FONT_MAPPING[k]).is_null() {
t_print!(
"{:#}",
FileName {
Expand Down Expand Up @@ -854,7 +852,7 @@ pub(crate) unsafe fn load_fmt_file() -> bool {

FONT_MAPPING = vec![ptr::null_mut(); FONT_MAX + 1];
for _ in 0..FONT_MAX + 1 {
FONT_LAYOUT_ENGINE.push(crate::xetex_ext::Font::None);
FONT_LAYOUT_ENGINE.push(None);
}
FONT_FLAGS = vec![0; FONT_MAX + 1];
FONT_LETTER_SPACE = vec![Scaled::ZERO; FONT_MAX + 1];
Expand Down
19 changes: 19 additions & 0 deletions engine/src/text_layout_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::xetex_font_info::{GlyphBBox, XeTeXFontInst};
//use crate::xetex_font_manager::PlatformFontRef;
use crate::xetex_font_info::GlyphID;
use crate::xetex_layout_interface::FixedPoint;
use crate::xetex_layout_interface::XeTeXLayoutEngine;
use crate::xetex_scaledmath::Scaled;
use harfbuzz_sys::hb_tag_t;

Expand Down Expand Up @@ -174,6 +175,24 @@ impl GlyphEdge {
}
}

#[enum_dispatch::enum_dispatch]
pub(crate) enum NativeFont {
#[cfg(target_os = "macos")]
Aat(crate::xetex_aatfont::AATLayoutEngine),
Otgr(XeTeXLayoutEngine),
}

impl NativeFont {
pub(crate) fn flag(&self) -> u32 {
match self {
#[cfg(target_os = "macos")]
Self::Aat(_) => 0xFFFF,
Self::Otgr(_) => 0xFFFE,
}
}
}

#[enum_dispatch::enum_dispatch(NativeFont)]
pub(crate) trait TextLayoutEngine {
/// The most important trait method. Lay out some text and return its size.
unsafe fn layout_text(&mut self, request: LayoutRequest) -> NodeLayout;
Expand Down
10 changes: 5 additions & 5 deletions engine/src/tfm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bridge::TTInputFormat;

use crate::help;
use crate::text_layout_engine::TextLayoutEngine;
use crate::text_layout_engine::{NativeFont::*, TextLayoutEngine};
use std::ptr;

use crate::{t_eprint, t_print, t_print_nl};
Expand Down Expand Up @@ -50,9 +50,9 @@ use crate::xetex_ini::{
};
use crate::xetex_stringpool::{pool_ptr, str_pool};

use crate::xetex_ext::ot_get_font_metrics;
use crate::xetex_ext::{check_for_tfm_font_mapping, load_tfm_font_mapping};
use crate::xetex_ext::{find_native_font, NativeFont::*};
use crate::xetex_ext::{
check_for_tfm_font_mapping, find_native_font, load_tfm_font_mapping, ot_get_font_metrics,
};

use super::xetex_io::tt_xetex_open_input;
use crate::xetex_consts::get_int_par;
Expand Down Expand Up @@ -661,7 +661,7 @@ pub(crate) unsafe fn load_native_font(name: &str, s: Scaled) -> Result<usize, Na
HYPHEN_CHAR[FONT_PTR] = get_int_par(IntPar::default_hyphen_char);
SKEW_CHAR[FONT_PTR] = get_int_par(IntPar::default_skew_char);
PARAM_BASE[FONT_PTR] = fmem_ptr - 1;
FONT_LAYOUT_ENGINE[FONT_PTR] = crate::xetex_ext::Font::Native(font_engine);
FONT_LAYOUT_ENGINE[FONT_PTR] = Some(font_engine);
FONT_MAPPING[FONT_PTR] = ptr::null_mut();
FONT_LETTER_SPACE[FONT_PTR] = loaded_font_letter_space;
/* "measure the width of the space character and set up font parameters" */
Expand Down
10 changes: 9 additions & 1 deletion engine/src/xetex_aatfont.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ pub struct AATLayoutEngine {
pub(crate) attributes: CFDictionaryRef,
}

impl Drop for AATLayoutEngine {
fn drop(&mut self) {
unsafe {
CFRelease(self.attributes as CFDictionaryRef as CFTypeRef);
}
}
}

impl AATLayoutEngine {
unsafe fn ct_font(&self) -> CTFontRef {
font_from_attributes(self.attributes)
Expand Down Expand Up @@ -741,7 +749,7 @@ unsafe fn getLastResort() -> CFStringRef {
return kLastResort;
}

use crate::xetex_ext::{NativeFont, NativeFont::*};
use crate::text_layout_engine::{NativeFont, NativeFont::*};

pub(crate) unsafe fn loadAATfont(
mut descriptor: CTFontDescriptorRef,
Expand Down
88 changes: 25 additions & 63 deletions engine/src/xetex_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,7 @@ pub(crate) use crate::xetex_scaledmath::Scaled;
use crate::xetex_layout_interface::*;
use harfbuzz_sys::{hb_feature_t, hb_tag_from_string, hb_tag_t};

pub(crate) enum Font {
None,
Native(NativeFont),
}

pub(crate) enum NativeFont {
#[cfg(target_os = "macos")]
Aat(crate::xetex_aatfont::AATLayoutEngine),
Otgr(Box<XeTeXLayoutEngine>),
}
impl Drop for NativeFont {
fn drop(&mut self) {
#[cfg(target_os = "macos")]
match self {
Self::Aat(engine) => unsafe {
CFRelease(engine.attributes as CFDictionaryRef as CFTypeRef)
},
_ => {}
}
}
}

impl NativeFont {
pub(crate) fn flag(&self) -> u32 {
match self {
#[cfg(target_os = "macos")]
Self::Aat(_) => 0xFFFF,
Self::Otgr(_) => 0xFFFE,
}
}
}

use NativeFont::*;
use crate::text_layout_engine::{NativeFont, NativeFont::*};

/* 16.16 version number */

Expand Down Expand Up @@ -168,7 +136,7 @@ pub(crate) unsafe fn linebreak_start(f: usize, localeStrNum: i32, text: &[u16])
let mut status: icu::UErrorCode = icu::U_ZERO_ERROR;
let locale = gettexstring(localeStrNum);
match &FONT_LAYOUT_ENGINE[f] {
Font::Native(Otgr(engine)) if locale == "G" => {
Some(Otgr(engine)) if locale == "G" => {
if initGraphiteBreaking(engine, text) {
/* user asked for Graphite line breaking and the font supports it */
return;
Expand Down Expand Up @@ -1107,7 +1075,7 @@ pub(crate) unsafe fn make_font_def(f: usize) -> Vec<u8> {
let mut embolden: f32 = 0.;
match &FONT_LAYOUT_ENGINE[f] {
#[cfg(target_os = "macos")]
Font::Native(Aat(engine)) => {
Some(Aat(engine)) => {
filename = engine.font_filename(&mut index);
assert!(!filename.is_empty());
if !CFDictionaryGetValue(
Expand All @@ -1124,7 +1092,7 @@ pub(crate) unsafe fn make_font_def(f: usize) -> Vec<u8> {
embolden = engine.embolden_factor();
size = Scaled::from(engine.point_size());
}
Font::Native(Otgr(engine)) => {
Some(Otgr(engine)) => {
/* fontRef = */
//getFontRef(engine);
filename = engine.font_filename(&mut index);
Expand Down Expand Up @@ -1230,11 +1198,11 @@ pub(crate) unsafe fn get_native_char_height_depth(font: usize, ch: char) -> (Sca
const CAP_HEIGHT: i32 = 8;
let (ht, dp) = match &FONT_LAYOUT_ENGINE[font] {
#[cfg(target_os = "macos")]
Font::Native(Aat(engine)) => {
Some(Aat(engine)) => {
let gid = engine.map_char_to_glyph(ch) as i32;
engine.glyph_height_depth(gid as u32).unwrap()
}
Font::Native(Otgr(engine)) => {
Some(Otgr(engine)) => {
let gid = engine.map_char_to_glyph(ch) as i32;
engine.glyph_height_depth(gid as u32).unwrap()
}
Expand Down Expand Up @@ -1289,14 +1257,14 @@ pub(crate) unsafe fn get_glyph_bounds(font: usize, edge: i32, gid: i32) -> Scale
/* edge codes 1,2,3,4 => L T R B */
let (a, b) = match &FONT_LAYOUT_ENGINE[font] {
#[cfg(target_os = "macos")]
Font::Native(Aat(engine)) => {
Some(Aat(engine)) => {
if edge.is_side() {
engine.glyph_sidebearings(gid as u32).unwrap()
} else {
engine.glyph_height_depth(gid as u32).unwrap()
}
}
Font::Native(Otgr(engine)) => {
Some(Otgr(engine)) => {
if edge.is_side() {
engine.glyph_sidebearings(gid as u32).unwrap()
} else {
Expand All @@ -1323,11 +1291,11 @@ pub(crate) unsafe fn getnativecharic(f: &NativeFont, letter_space: Scaled, c: ch
pub(crate) unsafe fn getnativecharwd(f: usize, c: char) -> Scaled {
match &FONT_LAYOUT_ENGINE[f] {
#[cfg(target_os = "macos")]
Font::Native(Aat(engine)) => {
Some(Aat(engine)) => {
let gid = engine.map_char_to_glyph(c) as i32;
(engine.get_glyph_width_from_engine(gid as u32) as f64).into()
}
Font::Native(Otgr(engine)) => {
Some(Otgr(engine)) => {
let gid = engine.map_char_to_glyph(c) as i32;
(engine.get_glyph_width_from_engine(gid as u32) as f64).into()
}
Expand All @@ -1346,14 +1314,14 @@ pub(crate) unsafe fn store_justified_native_glyphs(node: &mut NativeWord) {
let f = node.font() as usize;
match &mut FONT_LAYOUT_ENGINE[f] {
#[cfg(target_os = "macos")]
Font::Native(Aat(engine)) => {
Some(Aat(engine)) => {
/* separate Mac-only codepath for AAT fonts */
let request = LayoutRequest::from_node(node, true);
let layout = engine.layout_text(request);
layout.write_node(node);
return;
}
Font::Native(Otgr(engine)) => {
Some(Otgr(engine)) => {
/* save desired width */
let savedWidth = node.width();
node.set_metrics(false);
Expand Down Expand Up @@ -1399,11 +1367,11 @@ pub(crate) unsafe fn measure_native_node(node: &mut NativeWord, use_glyph_metric
let request = LayoutRequest::from_node(node, false);
let layout = match &mut FONT_LAYOUT_ENGINE[f] {
#[cfg(target_os = "macos")]
Font::Native(Aat(engine)) => {
Some(Aat(engine)) => {
/* we're using this font in AAT mode, so font_layout_engine[f] is actually a CFDictionaryRef */
engine.layout_text(request)
}
Font::Native(Otgr(engine)) => engine.layout_text(request),
Some(Otgr(engine)) => engine.layout_text(request),
_ => panic!("bad native font flag in `measure_native_node`"),
};
layout.write_node(node);
Expand All @@ -1424,11 +1392,9 @@ pub(crate) unsafe fn measure_native_node(node: &mut NativeWord, use_glyph_metric
let bbox = getCachedGlyphBBox(f as u16, glyph_ids[i]).unwrap_or_else(|| {
let bbox = match &FONT_LAYOUT_ENGINE[f] {
#[cfg(target_os = "macos")]
Font::Native(Aat(engine)) => {
aat::GetGlyphBBox_AAT(engine.attributes, glyph_ids[i])
.unwrap_or(GlyphBBox::zero())
}
Font::Native(Otgr(engine)) => engine
Some(Aat(engine)) => aat::GetGlyphBBox_AAT(engine.attributes, glyph_ids[i])
.unwrap_or(GlyphBBox::zero()),
Some(Otgr(engine)) => engine
.font
.get_glyph_bounds(glyph_ids[i])
.unwrap_or(GlyphBBox::zero()),
Expand Down Expand Up @@ -1457,14 +1423,14 @@ pub(crate) unsafe fn real_get_native_italic_correction(node: &NativeWord) -> Sca
let glyph_ids = node.glyph_ids();
match &FONT_LAYOUT_ENGINE[f] {
#[cfg(target_os = "macos")]
Font::Native(Aat(engine)) => {
Some(Aat(engine)) => {
Scaled::from(
engine
.glyph_ital_correction(glyph_ids[n - 1] as u32)
.unwrap() as f64,
) + FONT_LETTER_SPACE[f]
}
Font::Native(Otgr(engine)) => {
Some(Otgr(engine)) => {
Scaled::from(
engine
.glyph_ital_correction(glyph_ids[n - 1] as u32)
Expand All @@ -1482,12 +1448,8 @@ pub(crate) unsafe fn real_get_native_glyph_italic_correction(node: &Glyph) -> Sc
let f = node.font() as usize;
match &FONT_LAYOUT_ENGINE[f] {
#[cfg(target_os = "macos")]
Font::Native(Aat(engine)) => {
(engine.glyph_ital_correction(gid as u32).unwrap() as f64).into()
}
Font::Native(Otgr(engine)) => {
(engine.glyph_ital_correction(gid as u32).unwrap() as f64).into()
}
Some(Aat(engine)) => (engine.glyph_ital_correction(gid as u32).unwrap() as f64).into(),
Some(Otgr(engine)) => (engine.glyph_ital_correction(gid as u32).unwrap() as f64).into(),
_ => {
Scaled::ZERO
/* can't actually happen */
Expand All @@ -1499,15 +1461,15 @@ pub(crate) unsafe fn measure_native_glyph(node: &mut Glyph, use_glyph_metrics: b
let f = node.font() as usize;
let (ht, dp) = match &FONT_LAYOUT_ENGINE[f] {
#[cfg(target_os = "macos")]
Font::Native(Aat(engine)) => {
Some(Aat(engine)) => {
node.set_width((engine.glyph_width(gid as u32)).into());
if use_glyph_metrics {
engine.glyph_height_depth(gid as u32).unwrap()
} else {
(0., 0.)
}
}
Font::Native(Otgr(engine)) => {
Some(Otgr(engine)) => {
node.set_width((engine.glyph_width(gid as u32)).into());
if use_glyph_metrics {
engine.glyph_height_depth(gid as u32).unwrap()
Expand Down Expand Up @@ -1543,8 +1505,8 @@ pub(crate) unsafe fn map_glyph_to_index(font: &NativeFont, name: &str) -> i32 {
pub(crate) unsafe fn get_font_char_range(font: usize, first: i32) -> i32 {
match &mut FONT_LAYOUT_ENGINE[font] {
#[cfg(target_os = "macos")]
Font::Native(Aat(engine)) => engine.font_char_range(first),
Font::Native(Otgr(engine)) => engine.font_char_range(first),
Some(Aat(engine)) => engine.font_char_range(first),
Some(Otgr(engine)) => engine.font_char_range(first),
_ => panic!("bad native font flag in `get_font_char_range\'`"),
}
}
Expand Down
9 changes: 4 additions & 5 deletions engine/src/xetex_ini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,9 +722,8 @@ pub(crate) static mut FONT_BCHAR: Vec<nine_bits> = Vec::new();
#[no_mangle]
pub(crate) static mut FONT_FALSE_BCHAR: Vec<nine_bits> = Vec::new();

use crate::xetex_ext::Font;

pub(crate) static mut FONT_LAYOUT_ENGINE: Vec<Font> = Vec::new();
use crate::text_layout_engine::NativeFont;
pub(crate) static mut FONT_LAYOUT_ENGINE: Vec<Option<NativeFont>> = Vec::new();
#[no_mangle]
pub(crate) static mut FONT_MAPPING: Vec<*mut libc::c_void> = Vec::new();
#[no_mangle]
Expand Down Expand Up @@ -2038,7 +2037,7 @@ pub(crate) unsafe fn prefixed_command(
} else { SKEW_CHAR[f] = val }
}
_ => {
let p = if let Font::Native(nf) = &FONT_LAYOUT_ENGINE[f] {
let p = if let Some(nf) = &FONT_LAYOUT_ENGINE[f] {
scan_glyph_number(input, nf)
} else { scan_char_num(input) };
scan_optional_equals(input);
Expand Down Expand Up @@ -4216,7 +4215,7 @@ pub(crate) unsafe fn tt_run_engine(dump_name: &str, input_file_name: &str) -> TT
FONT_MAPPING = vec![ptr::null_mut(); FONT_MAX + 1];
FONT_LAYOUT_ENGINE.clear();
for _ in 0..FONT_MAX + 1 {
FONT_LAYOUT_ENGINE.push(Font::None);
FONT_LAYOUT_ENGINE.push(None);
}
FONT_FLAGS = vec![0; FONT_MAX + 1];
FONT_LETTER_SPACE = vec![Scaled::ZERO; FONT_MAX + 1];
Expand Down
Loading

0 comments on commit 75db98b

Please sign in to comment.