diff --git a/engine/src/text_layout_engine.rs b/engine/src/text_layout_engine.rs index 9fa8e1d5e..e49c8b4df 100644 --- a/engine/src/text_layout_engine.rs +++ b/engine/src/text_layout_engine.rs @@ -4,6 +4,7 @@ use crate::node::NativeWord; use crate::xetex_font_info::{GlyphBBox, XeTeXFontInst}; //use crate::xetex_font_manager::PlatformFontRef; +use crate::cmd::XetexExtCmd; use crate::xetex_font_info::GlyphID; use crate::xetex_layout_interface::FixedPoint; use crate::xetex_layout_interface::XeTeXLayoutEngine; @@ -200,6 +201,8 @@ pub(crate) trait TextLayoutEngine { /// getFontFilename fn font_filename(&self, index: &mut u32) -> String; + //unsafe fn print_font_name(&self, c: i32, arg1: i32, arg2: i32); + /// getFontInst //fn font_instance(&self) -> &XeTeXFontInst; @@ -210,6 +213,27 @@ pub(crate) trait TextLayoutEngine { // XXX: make a single struct for make_font_def to consume, of all the required values + unsafe fn get_font_metrics(&self) -> (Scaled, Scaled, Scaled, Scaled, Scaled); + + /// ot_font_get, aat_font_get + unsafe fn poorly_named_getter(&self, what: XetexExtCmd) -> i32; + + /// ot_font_get_1, aat_font_get_1 + unsafe fn poorly_named_getter_1(&self, what: XetexExtCmd, param1: i32) -> i32; + + /// ot_font_get_2, aat_font_get_2 + unsafe fn poorly_named_getter_2(&self, what: XetexExtCmd, param1: i32, param2: i32) -> i32; + + unsafe fn poorly_named_getter_3( + &self, + what: XetexExtCmd, + param1: i32, + param2: i32, + param3: i32, + ) -> i32; + + unsafe fn get_flags(&self, font_number: usize) -> u16; + /// getExtendFactor fn extend_factor(&self) -> f64; /// getPointSize @@ -247,15 +271,15 @@ pub(crate) trait TextLayoutEngine { /// This is used for 'fallback in case lacks an OS/2 table', and also for adding accents /// (get_native_char_sidebearings). /// Although the shaping engine should probably be doing the latter, not xetex0! - unsafe fn map_char_to_glyph(&self, chr: char) -> u32; + fn map_char_to_glyph(&self, chr: char) -> u32; /// getFontCharRange /// Another candidate for using XeTeXFontInst directly - unsafe fn font_char_range(&self, reqFirst: libc::c_int) -> libc::c_int; + fn font_char_range(&self, reqFirst: i32) -> i32; /// mapGlyphToIndex /// Should use engine.font directly - unsafe fn map_glyph_to_index(&self, glyphName: *const libc::c_char) -> i32; + fn map_glyph_to_index(&self, glyph_name: &str) -> i32; // Provided methods, override if using stuff diff --git a/engine/src/xetex_aatfont.rs b/engine/src/xetex_aatfont.rs index 7d725d535..67fd0ae09 100644 --- a/engine/src/xetex_aatfont.rs +++ b/engine/src/xetex_aatfont.rs @@ -4,6 +4,7 @@ use super::xetex_layout_interface::GlyphBBox; use crate::text_layout_engine::{LayoutRequest, NodeLayout, TextLayoutEngine}; use crate::xetex_font_info::GlyphID; +use std::ffi::CString; use crate::cf_prelude::*; @@ -257,6 +258,53 @@ impl TextLayoutEngine for AATLayoutEngine { fn font_filename(&self, index: &mut u32) -> String { unsafe { getFileNameFromCTFont(self.ct_font(), index) } } + /*unsafe fn print_font_name(&self, c: i32, arg1: i32, arg2: i32) { + if self.usingGraphite() { + aat_print_font_name(c, arg1, arg2); + } + }*/ + + unsafe fn get_font_metrics(&self) -> (Scaled, Scaled, Scaled, Scaled, Scaled) { + aat_get_font_metrics(self.attributes) + } + + unsafe fn get_flags(&self, _font_number: usize) -> u16 { + if !CFDictionaryGetValue( + self.attributes, + kCTVerticalFormsAttributeName as *const libc::c_void, + ) + .is_null() + { + 0x100 + } else { + 0 + } + } + + /// ot_font_get, aat_font_get + unsafe fn poorly_named_getter(&self, what: XetexExtCmd) -> i32 { + aat_font_get(what, self.attributes) + } + + /// ot_font_get_1, aat_font_get_1 + unsafe fn poorly_named_getter_1(&self, what: XetexExtCmd, param1: i32) -> i32 { + aat_font_get_1(what, self.attributes, param1) + } + + /// ot_font_get_2, aat_font_get_2 + unsafe fn poorly_named_getter_2(&self, what: XetexExtCmd, param1: i32, param2: i32) -> i32 { + aat_font_get_2(what, self.attributes, param1, param2) + } + + unsafe fn poorly_named_getter_3( + &self, + what: XetexExtCmd, + param1: i32, + param2: i32, + param3: i32, + ) -> i32 { + unimplemented!() + } /// getExtendFactor fn extend_factor(&self) -> f64 { @@ -355,20 +403,21 @@ impl TextLayoutEngine for AATLayoutEngine { } /// mapCharToGlyph - unsafe fn map_char_to_glyph(&self, chr: char) -> u32 { - MapCharToGlyph_AAT(self.attributes, chr as _) as u32 + fn map_char_to_glyph(&self, chr: char) -> u32 { + unsafe { MapCharToGlyph_AAT(self.attributes, chr as _) as u32 } } /// getFontCharRange /// Another candidate for using XeTeXFontInst directly - unsafe fn font_char_range(&self, reqFirst: libc::c_int) -> libc::c_int { - GetFontCharRange_AAT(self.attributes, reqFirst) + fn font_char_range(&self, reqFirst: i32) -> i32 { + unsafe { GetFontCharRange_AAT(self.attributes, reqFirst) } } /// mapGlyphToIndex /// Should use engine.font directly - unsafe fn map_glyph_to_index(&self, glyphName: *const libc::c_char) -> i32 { - MapGlyphToIndex_AAT(self.attributes, glyphName) + fn map_glyph_to_index(&self, glyph_name: &str) -> i32 { + let name = CString::new(glyph_name).unwrap(); + unsafe { MapGlyphToIndex_AAT(self.attributes, name.as_ptr()) } } } diff --git a/engine/src/xetex_ext.rs b/engine/src/xetex_ext.rs index 41cd067c1..f6dec722b 100644 --- a/engine/src/xetex_ext.rs +++ b/engine/src/xetex_ext.rs @@ -4,6 +4,7 @@ use crate::c_pointer_to_str; use crate::t_print_nl; use std::ffi::CString; +use crate::cmd::XetexExtCmd; use crate::node::{Glyph, NativeWord}; use crate::strstartswith; use crate::stub_icu as icu; @@ -927,74 +928,7 @@ pub(crate) unsafe fn ot_get_font_metrics( }; (ascent, descent, xheight, capheight, slant) } -pub(crate) unsafe fn ot_font_get(what: i32, engine: &XeTeXLayoutEngine) -> i32 { - let fontInst = engine.get_font(); - match what { - 1 => return countGlyphs(fontInst) as i32, - 8 => { - /* ie Graphite features */ - return countGraphiteFeatures(engine) as i32; - } - 16 => return countScripts(fontInst) as i32, - _ => {} - } - 0 -} -pub(crate) unsafe fn ot_font_get_1(what: i32, engine: &XeTeXLayoutEngine, param: i32) -> i32 { - let fontInst = engine.get_font(); - match what { - 17 => return countLanguages(fontInst, param as hb_tag_t) as i32, - 19 => return getIndScript(fontInst, param as u32) as i32, - 9 => { - /* for graphite fonts...*/ - return getGraphiteFeatureCode(engine, param as u32) as i32; - } - 11 => return 1, - 12 => return countGraphiteFeatureSettings(engine, param as u32) as i32, - _ => {} - } - 0 -} -pub(crate) unsafe fn ot_font_get_2( - what: i32, - engine: &XeTeXLayoutEngine, - param1: i32, - param2: i32, -) -> i32 { - let fontInst = engine.get_font(); - match what { - 20 => return getIndLanguage(fontInst, param1 as hb_tag_t, param2 as u32) as i32, - 18 => return countFeatures(fontInst, param1 as hb_tag_t, param2 as hb_tag_t) as i32, - 13 => { - /* for graphite fonts */ - return getGraphiteFeatureSettingCode(engine, param1 as u32, param2 as u32) as i32; - } - 15 => { - return (getGraphiteFeatureDefaultSetting(engine, param1 as u32) == param2 as u32) - as i32 - } - _ => {} - } /* to guarantee enough space in the buffer */ - 0 -} -pub(crate) unsafe fn ot_font_get_3( - what: i32, - engine: &XeTeXLayoutEngine, - param1: i32, - param2: i32, - param3: i32, -) -> i32 { - let fontInst = engine.get_font(); - match what { - 21 => getIndFeature( - fontInst, - param1 as hb_tag_t, - param2 as hb_tag_t, - param3 as u32, - ) as i32, - _ => 0, - } -} + pub(crate) unsafe fn gr_get_font_name( what: i32, engine: &XeTeXLayoutEngine, @@ -1015,20 +949,26 @@ pub(crate) unsafe fn gr_get_font_name( String::new() } } -pub(crate) unsafe fn gr_font_get_named(name: &str, what: i32, engine: &XeTeXLayoutEngine) -> i32 { +pub(crate) unsafe fn gr_font_get_named( + name: &str, + what: XetexExtCmd, + engine: &XeTeXLayoutEngine, +) -> i32 { match what { - 10 => findGraphiteFeatureNamed(engine, name.as_bytes()) as _, + XetexExtCmd::FindFeatureByName => findGraphiteFeatureNamed(engine, name.as_bytes()) as _, _ => -1, } } pub(crate) unsafe fn gr_font_get_named_1( name: &str, - what: i32, + what: XetexExtCmd, engine: &XeTeXLayoutEngine, param: i32, ) -> i32 { match what { - 14 => findGraphiteFeatureSettingNamed(engine, param as u32, name.as_bytes()) as _, + XetexExtCmd::FindSelectorByName => { + findGraphiteFeatureSettingNamed(engine, param as u32, name.as_bytes()) as _ + } _ => -1, } } @@ -1062,7 +1002,7 @@ pub(crate) unsafe fn makeXDVGlyphArrayData(p: &NativeWord) -> Vec { } pub(crate) unsafe fn make_font_def(f: usize) -> Vec { // XXX: seems like a good idea to make a struct FontDef - let mut flags: u16 = 0_u16; + let mut flags; #[allow(unused_assignments)] let rgba; let size; @@ -1073,41 +1013,17 @@ pub(crate) unsafe fn make_font_def(f: usize) -> Vec { let slant; #[allow(unused_assignments)] let mut embolden: f32 = 0.; - match &FONT_LAYOUT_ENGINE[f] { - #[cfg(target_os = "macos")] - Some(Aat(engine)) => { - filename = engine.font_filename(&mut index); - assert!(!filename.is_empty()); - if !CFDictionaryGetValue( - engine.attributes, - kCTVerticalFormsAttributeName as *const libc::c_void, - ) - .is_null() - { - flags = (flags as i32 | 0x100) as u16 - } - rgba = engine.rgb_value(); - extend = engine.extend_factor(); - slant = engine.slant_factor(); - embolden = engine.embolden_factor(); - size = Scaled::from(engine.point_size()); - } - Some(Otgr(engine)) => { - /* fontRef = */ - //getFontRef(engine); - filename = engine.font_filename(&mut index); - assert!(!filename.is_empty()); - rgba = engine.rgb_value(); - if FONT_FLAGS[f] as i32 & 0x2 != 0 { - flags = (flags as i32 | 0x100) as u16 - } - extend = engine.extend_factor(); - slant = engine.slant_factor(); - embolden = engine.embolden_factor(); - size = Scaled::from(engine.point_size() as f64) - } - _ => panic!("Not native font"), - } + let engine = FONT_LAYOUT_ENGINE[f] + .as_ref() + .unwrap_or_else(|| panic!("Not native font")); + filename = engine.font_filename(&mut index); + assert!(!filename.is_empty()); + rgba = engine.rgb_value(); + flags = engine.get_flags(f); + extend = engine.extend_factor(); + slant = engine.slant_factor(); + embolden = engine.embolden_factor(); + size = Scaled::from(engine.point_size() as f64); /* parameters after internal font ID: // size[4] // flags[2] @@ -1196,17 +1112,11 @@ unsafe fn snap_zone(value: &mut Scaled, snap_value: Scaled, fuzz: Scaled) { pub(crate) unsafe fn get_native_char_height_depth(font: usize, ch: char) -> (Scaled, Scaled) { use crate::xetex_consts::{QUAD_CODE, X_HEIGHT_CODE}; const CAP_HEIGHT: i32 = 8; - let (ht, dp) = match &FONT_LAYOUT_ENGINE[font] { - #[cfg(target_os = "macos")] - Some(Aat(engine)) => { - let gid = engine.map_char_to_glyph(ch) as i32; - engine.glyph_height_depth(gid as u32).unwrap() - } - Some(Otgr(engine)) => { - let gid = engine.map_char_to_glyph(ch) as i32; - engine.glyph_height_depth(gid as u32).unwrap() - } - _ => panic!("bad native font flag in `get_native_char_height_depth`"), + let (ht, dp) = if let Some(engine) = &FONT_LAYOUT_ENGINE[font] { + let gid = engine.map_char_to_glyph(ch) as i32; + engine.glyph_height_depth(gid as u32).unwrap() + } else { + panic!("bad native font flag in `get_native_char_height_depth`"); }; let mut height = (ht as f64).into(); let mut depth = (dp as f64).into(); @@ -1237,17 +1147,8 @@ pub(crate) unsafe fn getnativechardp(f: usize, c: char) -> Scaled { get_native_char_height_depth(f, c).1 } pub(crate) unsafe fn get_native_char_sidebearings(font: &NativeFont, ch: char) -> (Scaled, Scaled) { - let (l, r) = match font { - #[cfg(target_os = "macos")] - Aat(engine) => { - let gid = engine.map_char_to_glyph(ch) as i32; - engine.glyph_sidebearings(gid as u32).unwrap() - } - Otgr(engine) => { - let gid = engine.map_char_to_glyph(ch) as i32; - engine.glyph_sidebearings(gid as u32).unwrap() - } - }; + let gid = font.map_char_to_glyph(ch) as i32; + let (l, r) = font.glyph_sidebearings(gid as u32).unwrap(); ((l as f64).into(), (r as f64).into()) } @@ -1255,23 +1156,14 @@ pub(crate) unsafe fn get_glyph_bounds(font: usize, edge: i32, gid: i32) -> Scale GlyphEdge::from_int(edge) .map(|edge| { /* edge codes 1,2,3,4 => L T R B */ - let (a, b) = match &FONT_LAYOUT_ENGINE[font] { - #[cfg(target_os = "macos")] - Some(Aat(engine)) => { - if edge.is_side() { - engine.glyph_sidebearings(gid as u32).unwrap() - } else { - engine.glyph_height_depth(gid as u32).unwrap() - } - } - Some(Otgr(engine)) => { - if edge.is_side() { - engine.glyph_sidebearings(gid as u32).unwrap() - } else { - engine.glyph_height_depth(gid as u32).unwrap() - } + let (a, b) = if let Some(engine) = &FONT_LAYOUT_ENGINE[font] { + if edge.is_side() { + engine.glyph_sidebearings(gid as u32).unwrap() + } else { + engine.glyph_height_depth(gid as u32).unwrap() } - _ => abort!("bad native font flag in `get_glyph_bounds`"), + } else { + abort!("bad native font flag in `get_glyph_bounds`"); }; edge.pick_from(&(a, b)) }) @@ -1289,17 +1181,11 @@ pub(crate) unsafe fn getnativecharic(f: &NativeFont, letter_space: Scaled, c: ch } /* single-purpose metrics accessors */ pub(crate) unsafe fn getnativecharwd(f: usize, c: char) -> Scaled { - match &FONT_LAYOUT_ENGINE[f] { - #[cfg(target_os = "macos")] - 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() - } - 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() - } - _ => panic!("bad native font flag in `get_native_char_wd`"), + if let Some(engine) = &FONT_LAYOUT_ENGINE[f] { + let gid = engine.map_char_to_glyph(c) as i32; + (engine.get_glyph_width_from_engine(gid as u32) as f64).into() + } else { + panic!("bad native font flag in `get_native_char_wd`"); } } pub(crate) unsafe fn real_get_native_glyph(node: &NativeWord, index: u32) -> u16 { @@ -1365,14 +1251,10 @@ pub(crate) unsafe fn store_justified_native_glyphs(node: &mut NativeWord) { pub(crate) unsafe fn measure_native_node(node: &mut NativeWord, use_glyph_metrics: bool) { let f = node.font() as usize; let request = LayoutRequest::from_node(node, false); - let layout = match &mut FONT_LAYOUT_ENGINE[f] { - #[cfg(target_os = "macos")] - Some(Aat(engine)) => { - /* we're using this font in AAT mode, so font_layout_engine[f] is actually a CFDictionaryRef */ - engine.layout_text(request) - } - Some(Otgr(engine)) => engine.layout_text(request), - _ => panic!("bad native font flag in `measure_native_node`"), + let layout = if let Some(engine) = &mut FONT_LAYOUT_ENGINE[f] { + engine.layout_text(request) + } else { + panic!("bad native font flag in `measure_native_node`"); }; layout.write_node(node); if !use_glyph_metrics || node.glyph_count() == 0 { @@ -1391,12 +1273,8 @@ pub(crate) unsafe fn measure_native_node(node: &mut NativeWord, use_glyph_metric let y_0 = f32::from(-locations[i].y); let bbox = getCachedGlyphBBox(f as u16, glyph_ids[i]).unwrap_or_else(|| { let bbox = match &FONT_LAYOUT_ENGINE[f] { - #[cfg(target_os = "macos")] - 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]) + Some(engine) => engine + .glyph_bbox(glyph_ids[i] as u32) .unwrap_or(GlyphBBox::zero()), _ => GlyphBBox::zero(), }; @@ -1421,23 +1299,14 @@ pub(crate) unsafe fn real_get_native_italic_correction(node: &NativeWord) -> Sca let n = node.glyph_count() as usize; if n > 0 { let glyph_ids = node.glyph_ids(); - match &FONT_LAYOUT_ENGINE[f] { - #[cfg(target_os = "macos")] - Some(Aat(engine)) => { - Scaled::from( - engine - .glyph_ital_correction(glyph_ids[n - 1] as u32) - .unwrap() as f64, - ) + FONT_LETTER_SPACE[f] - } - Some(Otgr(engine)) => { - Scaled::from( - engine - .glyph_ital_correction(glyph_ids[n - 1] as u32) - .unwrap() as f64, - ) + FONT_LETTER_SPACE[f] - } - _ => Scaled::ZERO, + if let Some(engine) = &FONT_LAYOUT_ENGINE[f] { + Scaled::from( + engine + .glyph_ital_correction(glyph_ids[n - 1] as u32) + .unwrap() as f64, + ) + FONT_LETTER_SPACE[f] + } else { + Scaled::ZERO } } else { Scaled::ZERO @@ -1446,38 +1315,25 @@ pub(crate) unsafe fn real_get_native_italic_correction(node: &NativeWord) -> Sca pub(crate) unsafe fn real_get_native_glyph_italic_correction(node: &Glyph) -> Scaled { let gid: u16 = node.glyph(); let f = node.font() as usize; - match &FONT_LAYOUT_ENGINE[f] { - #[cfg(target_os = "macos")] - 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 */ - } + if let Some(engine) = &FONT_LAYOUT_ENGINE[f] { + (engine.glyph_ital_correction(gid as u32).unwrap() as f64).into() + } else { + Scaled::ZERO + /* can't actually happen */ } } pub(crate) unsafe fn measure_native_glyph(node: &mut Glyph, use_glyph_metrics: bool) { let gid = node.glyph(); let f = node.font() as usize; - let (ht, dp) = match &FONT_LAYOUT_ENGINE[f] { - #[cfg(target_os = "macos")] - 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.) - } - } - 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() - } else { - (0., 0.) - } + let (ht, dp) = if let Some(engine) = &FONT_LAYOUT_ENGINE[f] { + 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.) } - _ => panic!("bad native font flag in `measure_native_glyph`"), + } else { + panic!("bad native font flag in `measure_native_glyph`"); }; if use_glyph_metrics { node.set_height((ht as f64).into()); @@ -1487,29 +1343,6 @@ pub(crate) unsafe fn measure_native_glyph(node: &mut Glyph, use_glyph_metrics: b node.set_depth(Scaled(DEPTH_BASE[f])); }; } -pub(crate) unsafe fn map_char_to_glyph(font: &NativeFont, ch: char) -> i32 { - match font { - #[cfg(target_os = "macos")] - Aat(engine) => engine.map_char_to_glyph(ch) as i32, - Otgr(engine) => engine.map_char_to_glyph(ch) as i32, - } -} -pub(crate) unsafe fn map_glyph_to_index(font: &NativeFont, name: &str) -> i32 { - let name = CString::new(name).unwrap(); - match font { - #[cfg(target_os = "macos")] - Aat(engine) => engine.map_glyph_to_index(name.as_ptr()), - Otgr(engine) => engine.map_glyph_to_index(name.as_ptr()), - } -} -pub(crate) unsafe fn get_font_char_range(font: usize, first: i32) -> i32 { - match &mut FONT_LAYOUT_ENGINE[font] { - #[cfg(target_os = "macos")] - 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\'`"), - } -} pub(crate) unsafe fn real_get_native_word_cp(node: &NativeWord, side: Side) -> i32 { let glyph_ids = node.glyph_ids(); diff --git a/engine/src/xetex_layout_interface.rs b/engine/src/xetex_layout_interface.rs index af5a78448..83b1c3fa0 100644 --- a/engine/src/xetex_layout_interface.rs +++ b/engine/src/xetex_layout_interface.rs @@ -32,6 +32,7 @@ authorization from the copyright holders. \****************************************************************************/ #![allow(non_camel_case_types, non_snake_case, non_upper_case_globals)] +use crate::cmd::XetexExtCmd; use crate::stub_icu as icu; use crate::text_layout_engine::{LayoutRequest, NodeLayout, TextLayoutEngine}; use crate::xetex_font_manager::{FindFont, ShaperRequest}; @@ -327,7 +328,15 @@ impl TextLayoutEngine for XeTeXLayoutEngine { &self.font } */ - // getGlyphWidth + unsafe fn get_flags(&self, font_number: usize) -> u16 { + if crate::xetex_ini::FONT_FLAGS[font_number] as i32 & 0x2 != 0 { + 0x100 + } else { + 0 + } + } + + /// getGlyphWidth unsafe fn glyph_width(&self, gid: u32) -> f64 { self.font.get_glyph_width(gid as GlyphID) as f64 } @@ -341,6 +350,87 @@ impl TextLayoutEngine for XeTeXLayoutEngine { fn extend_factor(&self) -> f64 { self.extend as f64 } + unsafe fn get_font_metrics(&self) -> (Scaled, Scaled, Scaled, Scaled, Scaled) { + crate::xetex_ext::ot_get_font_metrics(self) + } + + /// ot_font_get, aat_font_get + unsafe fn poorly_named_getter(&self, what: XetexExtCmd) -> i32 { + match what { + XetexExtCmd::CountGlyphs => return countGlyphs(&self.font) as i32, + XetexExtCmd::CountFeatures => { + /* ie Graphite features */ + return countGraphiteFeatures(self) as i32; + } + XetexExtCmd::OTCountScripts => return countScripts(&self.font) as i32, + _ => {} + } + 0 + } + + /// ot_font_get_1, aat_font_get_1 + unsafe fn poorly_named_getter_1(&self, what: XetexExtCmd, param: i32) -> i32 { + match what { + XetexExtCmd::OTCountLanguages => { + return countLanguages(&self.font, param as hb_tag_t) as i32 + } + XetexExtCmd::OTScript => return getIndScript(&self.font, param as u32) as i32, + XetexExtCmd::FeatureCode => { + /* for graphite fonts...*/ + return getGraphiteFeatureCode(self, param as u32) as i32; + } + XetexExtCmd::IsExclusiveFeature => return 1, + XetexExtCmd::CountSelectors => { + return countGraphiteFeatureSettings(self, param as u32) as i32 + } + _ => {} + } + 0 + } + + /// ot_font_get_2, aat_font_get_2 + unsafe fn poorly_named_getter_2(&self, what: XetexExtCmd, param1: i32, param2: i32) -> i32 { + match what { + XetexExtCmd::OTLanguage => { + return getIndLanguage(&self.font, param1 as hb_tag_t, param2 as u32) as i32 + } + XetexExtCmd::OTCountFeatures => { + return countFeatures(&self.font, param1 as hb_tag_t, param2 as hb_tag_t) as i32 + } + XetexExtCmd::SelectorCode => { + /* for graphite fonts */ + return getGraphiteFeatureSettingCode(self, param1 as u32, param2 as u32) as i32; + } + XetexExtCmd::IsDefaultSelector => { + return (getGraphiteFeatureDefaultSetting(self, param1 as u32) == param2 as u32) + as i32 + } + _ => {} + } /* to guarantee enough space in the buffer */ + 0 + } + + unsafe fn poorly_named_getter_3( + &self, + what: XetexExtCmd, + param1: i32, + param2: i32, + param3: i32, + ) -> i32 { + match what { + XetexExtCmd::OTFeature => { + return getIndFeature( + &self.font, + param1 as hb_tag_t, + param2 as hb_tag_t, + param3 as u32, + ) as i32 + } + _ => {} + } + 0 + } + /// getPointSize fn point_size(&self) -> f64 { self.font.get_point_size() as f64 @@ -350,6 +440,24 @@ impl TextLayoutEngine for XeTeXLayoutEngine { (self.font.get_ascent(), self.font.get_descent()) } + /*/// gr_print_font_name + unsafe fn print_font_name(&self, what: i32, arg1: i32, arg2: i32) { + if self.usingGraphite() { + let mut name: *mut i8 = 0 as *mut i8; + match what { + 8 => name = getGraphiteFeatureLabel(self, param1 as u32), + 9 => name = getGraphiteFeatureSettingLabel(self, param1 as u32, param2 as u32), + _ => {} + } + if !name.is_null() { + print_c_string(name); + gr_label_destroy(name as *mut libc::c_void); + }; + } + // Not sure why non-graphite font names aren't printed + // Originally in xetex0.c. + }*/ + /// getGlyphName unsafe fn glyph_name(&self, gid: GlyphID) -> String { self.font.get_glyph_name(gid) @@ -415,23 +523,24 @@ impl TextLayoutEngine for XeTeXLayoutEngine { } /// mapCharToGlyph - unsafe fn map_char_to_glyph(&self, chr: char) -> u32 { - self.font.map_char_to_glyph(chr as _) as u32 + fn map_char_to_glyph(&self, chr: char) -> u32 { + unsafe { self.font.map_char_to_glyph(chr as _) as u32 } } /// getFontCharRange /// Another candidate for using XeTeXFontInst directly - unsafe fn font_char_range(&self, reqFirst: i32) -> i32 { + fn font_char_range(&self, reqFirst: i32) -> i32 { if reqFirst != 0 { - self.font.get_first_char_code() + unsafe { self.font.get_first_char_code() } } else { - self.font.get_last_char_code() + unsafe { self.font.get_last_char_code() } } } /// mapGlyphToIndex - unsafe fn map_glyph_to_index(&self, glyphName: *const i8) -> i32 { - self.font.map_glyph_to_index(glyphName) as i32 + fn map_glyph_to_index(&self, glyph_name: &str) -> i32 { + let name = CString::new(glyph_name).unwrap(); + unsafe { self.font.map_glyph_to_index(name.as_ptr()) as i32 } } fn using_graphite(&self) -> bool { diff --git a/engine/src/xetex_xetex0.rs b/engine/src/xetex_xetex0.rs index d6dcdca73..592296cbc 100644 --- a/engine/src/xetex_xetex0.rs +++ b/engine/src/xetex_xetex0.rs @@ -19,11 +19,10 @@ use crate::xetex_aatfont as aat; use crate::xetex_consts::*; use crate::xetex_errors::{confusion, error, fatal_error, overflow, pdf_error, Confuse}; use crate::xetex_ext::{ - apply_mapping, apply_tfm_font_mapping, get_encoding_mode_and_info, get_font_char_range, - get_glyph_bounds, get_native_char_height_depth, get_native_char_sidebearings, getnativechardp, - getnativecharht, getnativecharic, getnativecharwd, gr_font_get_named, gr_font_get_named_1, - gr_get_font_name, linebreak_next, linebreak_start, map_char_to_glyph, map_glyph_to_index, - ot_font_get, ot_font_get_1, ot_font_get_2, ot_font_get_3, + apply_mapping, apply_tfm_font_mapping, get_encoding_mode_and_info, get_glyph_bounds, + get_native_char_height_depth, get_native_char_sidebearings, getnativechardp, getnativecharht, + getnativecharic, getnativecharwd, gr_font_get_named, gr_font_get_named_1, gr_get_font_name, + linebreak_next, linebreak_start, }; use crate::xetex_ini::{ _xeq_level_array, active_width, adjust_tail, after_token, align_ptr, align_state, arith_error, @@ -5378,10 +5377,10 @@ pub(crate) unsafe fn scan_glyph_number(input: &mut input_state_t, f: &NativeFont if scan_keyword(input, "/") { let (file, ..) = scan_file_name(input); let fullname = file.to_string(); - map_glyph_to_index(f, &fullname) + f.map_glyph_to_index(&fullname) } else if scan_keyword(input, "u") { let val = scan_char_num(input); - map_char_to_glyph(f, std::char::from_u32(val as u32).unwrap()) + f.map_char_to_glyph(std::char::from_u32(val as u32).unwrap()) as i32 } else { scan_int(input) } @@ -6266,9 +6265,7 @@ unsafe fn restart_scan_something_internal( LastItemCode::XetexCountGlyphs => { let n = scan_font_ident(input); match &FONT_LAYOUT_ENGINE[n as usize] { - #[cfg(target_os = "macos")] - Some(Aat(e)) => aat::aat_font_get(m.into(), e.attributes), - Some(Otgr(e)) => ot_font_get((m as i32) - 14, e), + Some(e) => e.poorly_named_getter(m.into()), _ => 0, } } @@ -6276,9 +6273,9 @@ unsafe fn restart_scan_something_internal( let n = scan_font_ident(input); match &FONT_LAYOUT_ENGINE[n as usize] { #[cfg(target_os = "macos")] - Some(Aat(e)) => aat::aat_font_get(m.into(), e.attributes), + Some(Aat(e)) => e.poorly_named_getter(m.into()), Some(Otgr(e)) if e.using_graphite() => { - ot_font_get((m as i32) - 14, e) + e.poorly_named_getter(m.into()) } _ => 0, } @@ -6299,11 +6296,11 @@ unsafe fn restart_scan_something_internal( #[cfg(target_os = "macos")] Some(Aat(e)) => { let k = scan_int(input); - aat::aat_font_get_1(m.into(), e.attributes, k) + e.poorly_named_getter_1(m.into(), k) } Some(Otgr(e)) if e.using_graphite() => { let k = scan_int(input); - ot_font_get_1((m as i32) - 14, e, k) + e.poorly_named_getter_1(m.into(), k) } _ => { not_aat_gr_font_error(Cmd::LastItem, m as i32, n as usize); @@ -6318,12 +6315,12 @@ unsafe fn restart_scan_something_internal( Some(Aat(e)) => { let k = scan_int(input); let val = scan_int(input); - aat::aat_font_get_2(m.into(), e.attributes, k, val) + e.poorly_named_getter_2(m.into(), k, val) } Some(Otgr(e)) if e.using_graphite() => { let k = scan_int(input); let val = scan_int(input); - ot_font_get_2((m as i32) - 14, e, k, val) + e.poorly_named_getter_2(m.into(), k, val) } _ => { not_aat_gr_font_error(Cmd::LastItem, m as i32, n as usize); @@ -6355,7 +6352,7 @@ unsafe fn restart_scan_something_internal( } Some(Otgr(e)) if e.using_graphite() => { let name = scan_file_name(input).0.to_string(); - gr_font_get_named(&name, (m as i32) - 14, e) + gr_font_get_named(&name, m.into(), e) } _ => { not_aat_gr_font_error(Cmd::LastItem, m as i32, n as usize); @@ -6375,7 +6372,7 @@ unsafe fn restart_scan_something_internal( Some(Otgr(e)) if e.using_graphite() => { let k = scan_int(input); let name = scan_file_name(input).0.to_string(); - gr_font_get_named_1(&name, (m as i32) - 14, e, k) + gr_font_get_named_1(&name, m.into(), e, k) } _ => { not_aat_gr_font_error(Cmd::LastItem, m as i32, n as usize); @@ -6387,7 +6384,7 @@ unsafe fn restart_scan_something_internal( let n = scan_font_ident(input); match &FONT_LAYOUT_ENGINE[n as usize] { Some(Otgr(e)) if e.using_open_type() => { - ot_font_get((m as i32) - 14, e) + e.poorly_named_getter(m.into()) } _ => 0, } @@ -6397,7 +6394,7 @@ unsafe fn restart_scan_something_internal( match &FONT_LAYOUT_ENGINE[n as usize] { Some(Otgr(e)) if e.using_open_type() => { let val = scan_int(input); - ot_font_get_1((m as i32) - 14, e, val) + e.poorly_named_getter_1(m.into(), val) } _ => { not_ot_font_error(Cmd::LastItem, m as i32, n as usize); @@ -6411,7 +6408,7 @@ unsafe fn restart_scan_something_internal( Some(Otgr(e)) if e.using_open_type() => { let k = scan_int(input); let val = scan_int(input); - ot_font_get_2((m as i32) - 14, e, k, val) + e.poorly_named_getter_2(m.into(), k, val) } _ => { not_ot_font_error(Cmd::LastItem, m as i32, n as usize); @@ -6426,7 +6423,7 @@ unsafe fn restart_scan_something_internal( let k = scan_int(input); let kk = scan_int(input); let val = scan_int(input); - ot_font_get_3((m as i32) - 14, e, k, kk, val) + e.poorly_named_getter_3(m.into(), k, kk, val) } _ => { not_ot_font_error(Cmd::LastItem, m as i32, n as usize); @@ -6437,7 +6434,7 @@ unsafe fn restart_scan_something_internal( LastItemCode::XetexMapCharToGlyph => { if let Some(nf) = &FONT_LAYOUT_ENGINE[EQTB[CUR_FONT_LOC].val as usize] { let n = scan_int(input); - map_char_to_glyph(nf, std::char::from_u32(n as u32).unwrap()) + nf.map_char_to_glyph(std::char::from_u32(n as u32).unwrap()) as i32 } else { not_native_font_error( Cmd::LastItem, @@ -6450,7 +6447,7 @@ unsafe fn restart_scan_something_internal( LastItemCode::XetexGlyphIndex => { if let Some(nf) = &FONT_LAYOUT_ENGINE[EQTB[CUR_FONT_LOC].val as usize] { let name = scan_file_name(input).0.to_string(); - map_glyph_to_index(nf, &name) + nf.map_glyph_to_index(&name) } else { not_native_font_error( Cmd::LastItem, @@ -6472,11 +6469,8 @@ unsafe fn restart_scan_something_internal( } LastItemCode::XetexFirstChar | LastItemCode::XetexLastChar => { let n = scan_font_ident(input); - if let Some(_) = &FONT_LAYOUT_ENGINE[n as usize] { - get_font_char_range( - n as usize, - (m == LastItemCode::XetexFirstChar) as i32, - ) + if let Some(engine) = &FONT_LAYOUT_ENGINE[n as usize] { + engine.font_char_range((m == LastItemCode::XetexFirstChar) as i32) } else if m == LastItemCode::XetexFirstChar { FONT_BC[n as usize] as i32 } else { @@ -8026,9 +8020,7 @@ pub(crate) unsafe fn conv_toks(input: &mut input_state_t, chr: i32, cs: i32) { } } ConvertCode::XetexGlyphName => match &FONT_LAYOUT_ENGINE[fnt as usize] { - #[cfg(target_os = "macos")] - Some(Aat(engine)) => engine.glyph_name(arg1 as u16), - Some(Otgr(engine)) => engine.glyph_name(arg1 as u16), + Some(engine) => engine.glyph_name(arg1 as u16), _ => panic!("bad native font flag in `print_glyph_name`"), }, ConvertCode::LeftMarginKern => { @@ -8708,7 +8700,7 @@ pub(crate) unsafe fn conditional(input: &mut input_state_t, cmd: Cmd, chr: i32) let n = scan_font_ident(input) as usize; let val = scan_usv_num(input); b = if let Some(nf) = &FONT_LAYOUT_ENGINE[n] { - map_char_to_glyph(nf, std::char::from_u32(val as u32).unwrap()) > 0 + nf.map_char_to_glyph(std::char::from_u32(val as u32).unwrap()) as i32 > 0 } else if FONT_BC[n] as i32 <= val && FONT_EC[n] as i32 >= val { FONT_CHARACTER_INFO(n, effective_char(true, n, val as u16) as usize).s3 > 0 } else { @@ -9235,7 +9227,7 @@ pub(crate) unsafe fn new_native_character(f: internal_font_number, c: char) -> N for chr in std::char::decode_utf16(mapped_text.iter().cloned()) { let chr = chr.unwrap(); - if map_char_to_glyph(nf, chr) == 0 { + if nf.map_char_to_glyph(chr) == 0 { char_warning(f, chr); } } @@ -9243,7 +9235,7 @@ pub(crate) unsafe fn new_native_character(f: internal_font_number, c: char) -> N p = new_native_word_node(f, mapped_text.len() as _); p.text_mut().copy_from_slice(&mapped_text[..]); } else { - if get_int_par(IntPar::tracing_lost_chars) > 0 && map_char_to_glyph(nf, c) == 0 { + if get_int_par(IntPar::tracing_lost_chars) > 0 && nf.map_char_to_glyph(c) == 0 { char_warning(f, c); } p = NativeWord::from(get_node(NATIVE_NODE_SIZE + 1)); @@ -14605,7 +14597,7 @@ pub(crate) unsafe fn main_control(input: &mut input_state_t) { if get_int_par(IntPar::tracing_lost_chars) > 0 { for c in std::char::decode_utf16(native_text.iter().cloned()) { let c = c.unwrap(); - if map_char_to_glyph(nf, c) == 0 { + if nf.map_char_to_glyph(c) == 0 { char_warning(main_f, c); } }