Skip to content

Commit 8d5a615

Browse files
authored
Merge pull request #336 from burrbull/mngr
oxidize font manager
2 parents 96c0c62 + d525703 commit 8d5a615

17 files changed

+719
-915
lines changed

Cargo.lock

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dpx/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ euclid = "0.20"
1919
indexmap = "1.3.0"
2020
png = "0.16"
2121
itoa = "0.4.6"
22-
once_cell = "1.4.1"
22+
once_cell = "1.5.2"
2323
arrayvec = "0.5"
2424

2525
[features]

engine/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ enumn = "0.1.2"
4343
derive_more = "0.99.7"
4444
md5 = "0.7"
4545
fontconfig_sys = { package = "yeslogic-fontconfig-sys", version = "2.11.1" }
46+
once_cell = "1.5.2"
4647

4748
[target.'cfg(target_os = "macos")'.dependencies]
4849
core-foundation = "0.9.1"

engine/src/lib.rs

-5
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,6 @@ mod core_memory {
126126
}
127127
new_mem
128128
}
129-
pub(crate) unsafe fn strdup(s: &str) -> *mut i8 {
130-
let new_string = xmalloc((s.len() + 1) as size_t) as *mut i8;
131-
let s = std::ffi::CString::new(s).unwrap();
132-
libc::strcpy(new_string, s.as_ptr())
133-
}
134129

135130
#[inline]
136131
pub(crate) unsafe fn mfree(ptr: *mut libc::c_void) -> *mut libc::c_void {

engine/src/node.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2277,8 +2277,8 @@ pub(crate) mod math {
22772277
self.val.chr = c;
22782278
self.typ = MathCell::MathTextChar;
22792279
}*/
2280-
pub(crate) unsafe fn fetch(&mut self) {
2281-
crate::xetex_math::fetch(self);
2280+
pub(crate) unsafe fn fetch(&mut self) -> (usize, char) {
2281+
crate::xetex_math::fetch(self)
22822282
}
22832283
}
22842284
// ---------------

engine/src/tfm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ pub(crate) unsafe fn load_native_font(name: &str, s: Scaled) -> Result<usize, Na
673673
FONT_MAPPING[FONT_PTR] = ptr::null_mut();
674674
FONT_LETTER_SPACE[FONT_PTR] = loaded_font_letter_space;
675675
/* "measure the width of the space character and set up font parameters" */
676-
let p = new_native_character(FONT_PTR, ' ' as i32);
676+
let p = new_native_character(FONT_PTR, ' ');
677677
let s = p.width() + loaded_font_letter_space;
678678
p.free();
679679

engine/src/xetex_aatfont.rs

+41-49
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ use crate::xetex_ini::{
2121
loaded_font_flags, loaded_font_letter_space, FONT_LAYOUT_ENGINE, FONT_LETTER_SPACE,
2222
};
2323
use crate::xetex_xetex0::font_feature_warning;
24-
use libc::{free, strlen};
24+
use libc::free;
2525
pub(crate) type Boolean = libc::c_uchar;
26-
use crate::xetex_output::print_chr;
2726

2827
use crate::xetex_scaledmath::Scaled;
2928
type Fract = i32;
@@ -103,7 +102,7 @@ pub(crate) unsafe fn do_aat_layout(node: &mut NativeWord, justify: bool) {
103102
let mut glyph_info: *mut libc::c_void = ptr::null_mut();
104103
let mut locations: *mut FixedPoint = ptr::null_mut();
105104
let mut width: CGFloat = 0.;
106-
let mut typesetter;
105+
let typesetter;
107106
let mut line;
108107
let f = node.font() as libc::c_uint;
109108
if let Font::Native(Aat(attributes)) = &FONT_LAYOUT_ENGINE[f as usize] {
@@ -149,36 +148,38 @@ pub(crate) unsafe fn do_aat_layout(node: &mut NativeWord, justify: bool) {
149148
width = 0i32 as CGFloat;
150149
for i in 0..runCount {
151150
let run = CFArrayGetValueAtIndex(glyphRuns, i) as CTRunRef;
152-
let count = CTRunGetGlyphCount(run);
151+
let count = CTRunGetGlyphCount(run) as usize;
153152
let runAttributes = CTRunGetAttributes(run);
154153
let vertical = CFDictionaryGetValue(
155154
runAttributes,
156155
kCTVerticalFormsAttributeName as *const libc::c_void,
157156
) as CFBooleanRef;
158157
// TODO(jjgod): Avoid unnecessary allocation with CTRunGetFoosPtr().
159-
let glyphs =
160-
xmalloc((count as usize).wrapping_mul(::std::mem::size_of::<CGGlyph>()) as _)
161-
as *mut CGGlyph;
162-
let positions =
163-
xmalloc((count as usize).wrapping_mul(::std::mem::size_of::<CGPoint>()) as _)
164-
as *mut CGPoint;
165-
let advances =
166-
xmalloc((count as usize).wrapping_mul(::std::mem::size_of::<CGSize>()) as _)
167-
as *mut CGSize;
158+
let mut glyphs = vec![0 as CGGlyph; count];
159+
let mut positions = vec![CGPoint::new(0., 0.); count];
160+
let mut advances = vec![CGSize::new(0., 0.); count];
168161
let runWidth = CTRunGetTypographicBounds(
169162
run,
170163
CFRangeMake(0i32 as CFIndex, 0i32 as CFIndex),
171164
ptr::null_mut(),
172165
ptr::null_mut(),
173166
ptr::null_mut(),
174167
);
175-
CTRunGetGlyphs(run, CFRangeMake(0i32 as CFIndex, 0i32 as CFIndex), glyphs);
168+
CTRunGetGlyphs(
169+
run,
170+
CFRangeMake(0i32 as CFIndex, 0i32 as CFIndex),
171+
glyphs.as_mut_ptr(),
172+
);
176173
CTRunGetPositions(
177174
run,
178175
CFRangeMake(0i32 as CFIndex, 0i32 as CFIndex),
179-
positions,
176+
positions.as_mut_ptr(),
177+
);
178+
CTRunGetAdvances(
179+
run,
180+
CFRangeMake(0i32 as CFIndex, 0i32 as CFIndex),
181+
advances.as_mut_ptr(),
180182
);
181-
CTRunGetAdvances(run, CFRangeMake(0i32 as CFIndex, 0i32 as CFIndex), advances);
182183
for j in 0..count {
183184
// XXX Core Text has that font cascading thing that will do
184185
// font substitution for missing glyphs, which we do not want
@@ -193,28 +194,25 @@ pub(crate) unsafe fn do_aat_layout(node: &mut NativeWord, justify: bool) {
193194
{
194195
*glyphIDs.offset(totalGlyphCount as isize) = 0i32 as u16
195196
} else {
196-
*glyphIDs.offset(totalGlyphCount as isize) = *glyphs.offset(j as isize)
197+
*glyphIDs.offset(totalGlyphCount as isize) = glyphs[j]
197198
}
198199
// Swap X and Y when doing vertical layout
199200
if vertical == kCFBooleanTrue {
200201
(*locations.offset(totalGlyphCount as isize)).x =
201-
-FixedPStoTeXPoints((*positions.offset(j as isize)).y);
202+
-FixedPStoTeXPoints(positions[j].y);
202203
(*locations.offset(totalGlyphCount as isize)).y =
203-
FixedPStoTeXPoints((*positions.offset(j as isize)).x)
204+
FixedPStoTeXPoints(positions[j].x)
204205
} else {
205206
(*locations.offset(totalGlyphCount as isize)).x =
206-
FixedPStoTeXPoints((*positions.offset(j as isize)).x);
207+
FixedPStoTeXPoints(positions[j].x);
207208
(*locations.offset(totalGlyphCount as isize)).y =
208-
-FixedPStoTeXPoints((*positions.offset(j as isize)).y)
209+
-FixedPStoTeXPoints(positions[j].y)
209210
}
210211
*glyphAdvances.offset(totalGlyphCount as isize) =
211-
Scaled((*advances.offset(j as isize)).width as i32);
212+
Scaled(advances[j].width as i32);
212213
totalGlyphCount += 1;
213214
}
214215
width += FixedPStoTeXPoints(runWidth).0 as f64;
215-
free(glyphs as *mut libc::c_void);
216-
free(positions as *mut libc::c_void);
217-
free(advances as *mut libc::c_void);
218216
}
219217
}
220218
} else {
@@ -373,32 +371,22 @@ pub(crate) unsafe fn GetGlyphItalCorr_AAT(attributes: CFDictionaryRef, mut gid:
373371
}
374372
return 0i32 as f64;
375373
}
376-
unsafe fn mapCharToGlyphFromCTFont(font: CTFontRef, mut ch: u32) -> libc::c_int {
374+
unsafe fn mapCharToGlyphFromCTFont(font: CTFontRef, ch: char) -> libc::c_int {
377375
let mut glyphs: [CGGlyph; 2] = [0i32 as CGGlyph, 0];
378-
let mut txt: [UniChar; 2] = [0; 2];
379-
let mut len: libc::c_int = 1i32;
380-
if ch > 0xffffi32 as libc::c_uint {
381-
ch = (ch as libc::c_uint).wrapping_sub(0x10000i32 as libc::c_uint) as u32;
382-
txt[0] = (0xd800i32 as libc::c_uint).wrapping_add(ch.wrapping_div(1024i32 as libc::c_uint))
383-
as UniChar;
384-
txt[1] = (0xdc00i32 as libc::c_uint).wrapping_add(ch.wrapping_rem(1024i32 as libc::c_uint))
385-
as UniChar;
386-
len = 2i32
387-
} else {
388-
txt[0] = ch as UniChar
389-
}
376+
let mut txt = [0; 2];
377+
let result = ch.encode_utf16(&mut txt);
390378
if CTFontGetGlyphsForCharacters(
391379
font,
392-
txt.as_mut_ptr() as *const UniChar,
380+
result.as_mut_ptr() as *const UniChar,
393381
glyphs.as_mut_ptr(),
394-
len as CFIndex,
382+
result.len() as CFIndex,
395383
) {
396384
return glyphs[0] as libc::c_int;
397385
}
398386
return 0i32;
399387
}
400388

401-
pub(crate) unsafe fn MapCharToGlyph_AAT(attributes: CFDictionaryRef, ch: u32) -> libc::c_int {
389+
pub(crate) unsafe fn MapCharToGlyph_AAT(attributes: CFDictionaryRef, ch: char) -> libc::c_int {
402390
let font = font_from_attributes(attributes);
403391
mapCharToGlyphFromCTFont(font, ch)
404392
}
@@ -454,17 +442,21 @@ pub(crate) unsafe fn GetFontCharRange_AAT(
454442
reqFirst: libc::c_int,
455443
) -> libc::c_int {
456444
if reqFirst != 0 {
457-
let mut ch: libc::c_int = 0i32;
458-
while MapCharToGlyph_AAT(attributes, ch as u32) == 0i32 && ch < 0x10ffffi32 {
459-
ch += 1
445+
for ch in 0..=0x10ffff {
446+
match std::char::from_u32(ch) {
447+
Some(c) if MapCharToGlyph_AAT(attributes, c) != 0 => return ch as i32,
448+
_ => {}
449+
}
460450
}
461-
return ch;
451+
return 0x10ffff;
462452
} else {
463-
let mut ch_0: libc::c_int = 0x10ffffi32;
464-
while MapCharToGlyph_AAT(attributes, ch_0 as u32) == 0i32 && ch_0 > 0i32 {
465-
ch_0 -= 1
453+
for ch in (0..=0x10ffff).rev() {
454+
match std::char::from_u32(ch) {
455+
Some(c) if MapCharToGlyph_AAT(attributes, c) != 0 => return ch as i32,
456+
_ => {}
457+
}
466458
}
467-
return ch_0;
459+
return 0;
468460
};
469461
}
470462

engine/src/xetex_ext.rs

+19-25
Original file line numberDiff line numberDiff line change
@@ -915,10 +915,7 @@ pub(crate) unsafe fn find_native_font(uname: &str, mut scaled_size: Scaled) -> O
915915
let fontRef = findFontByName(&nameString, &mut varString, Fix2D(scaled_size));
916916
if !fontRef.is_null() {
917917
/* update name_of_font to the full name of the font, for error messages during font loading */
918-
let fullName: *const i8 = getFullName(fontRef);
919-
name_of_font = std::ffi::CStr::from_ptr(fullName)
920-
.to_string_lossy()
921-
.to_string();
918+
name_of_font = getFullName(fontRef);
922919
if scaled_size < Scaled::ZERO {
923920
if let Some(font) = createFont(fontRef, scaled_size) {
924921
let dsize_0 = D2Fix(getDesignSize(&font));
@@ -985,7 +982,7 @@ pub(crate) unsafe fn ot_get_font_metrics(
985982
let mut xheight = D2Fix(d as f64);
986983
/* fallback in case the font does not have OS/2 table */
987984
if xheight == Scaled::ZERO {
988-
let glyphID = engine.map_char_to_glyph('x' as i32 as u32) as i32;
985+
let glyphID = engine.map_char_to_glyph('x') as i32;
989986
if glyphID != 0i32 {
990987
let (a, _) = engine.get_glyph_height_depth(glyphID as u32);
991988
xheight = D2Fix(a as f64)
@@ -995,7 +992,7 @@ pub(crate) unsafe fn ot_get_font_metrics(
995992
}
996993
}
997994
if capheight == Scaled::ZERO {
998-
let glyphID_0 = engine.map_char_to_glyph('X' as i32 as u32) as i32;
995+
let glyphID_0 = engine.map_char_to_glyph('X') as i32;
999996
if glyphID_0 != 0i32 {
1000997
let (a, _) = engine.get_glyph_height_depth(glyphID_0 as u32);
1001998
capheight = D2Fix(a as f64)
@@ -1292,20 +1289,20 @@ unsafe fn snap_zone(value: &mut Scaled, snap_value: Scaled, fuzz: Scaled) {
12921289
*value = snap_value
12931290
};
12941291
}
1295-
pub(crate) unsafe fn get_native_char_height_depth(font: usize, ch: i32) -> (Scaled, Scaled) {
1292+
pub(crate) unsafe fn get_native_char_height_depth(font: usize, ch: char) -> (Scaled, Scaled) {
12961293
use crate::xetex_consts::{QUAD_CODE, X_HEIGHT_CODE};
12971294
const CAP_HEIGHT: i32 = 8;
12981295
let (ht, dp) = match &FONT_LAYOUT_ENGINE[font] {
12991296
#[cfg(target_os = "macos")]
13001297
Font::Native(Aat(attributes)) => {
13011298
let mut ht: f32 = 0.0f64 as f32;
13021299
let mut dp: f32 = 0.0f64 as f32;
1303-
let gid: libc::c_int = aat::MapCharToGlyph_AAT(*attributes, ch as u32);
1300+
let gid: libc::c_int = aat::MapCharToGlyph_AAT(*attributes, ch);
13041301
aat::GetGlyphHeightDepth_AAT(*attributes, gid as u16, &mut ht, &mut dp);
13051302
(ht, dp)
13061303
}
13071304
Font::Native(Otgr(engine)) => {
1308-
let gid = engine.map_char_to_glyph(ch as u32) as i32;
1305+
let gid = engine.map_char_to_glyph(ch) as i32;
13091306
engine.get_glyph_height_depth(gid as u32)
13101307
}
13111308
_ => panic!("bad native font flag in `get_native_char_height_depth`"),
@@ -1332,24 +1329,24 @@ pub(crate) unsafe fn get_native_char_height_depth(font: usize, ch: i32) -> (Scal
13321329
);
13331330
(height, depth)
13341331
}
1335-
pub(crate) unsafe fn getnativecharht(f: usize, c: i32) -> Scaled {
1332+
pub(crate) unsafe fn getnativecharht(f: usize, c: char) -> Scaled {
13361333
get_native_char_height_depth(f, c).0
13371334
}
1338-
pub(crate) unsafe fn getnativechardp(f: usize, c: i32) -> Scaled {
1335+
pub(crate) unsafe fn getnativechardp(f: usize, c: char) -> Scaled {
13391336
get_native_char_height_depth(f, c).1
13401337
}
1341-
pub(crate) unsafe fn get_native_char_sidebearings(font: &NativeFont, ch: i32) -> (Scaled, Scaled) {
1338+
pub(crate) unsafe fn get_native_char_sidebearings(font: &NativeFont, ch: char) -> (Scaled, Scaled) {
13421339
let (l, r) = match font {
13431340
#[cfg(target_os = "macos")]
13441341
Aat(attributes) => {
13451342
let mut l: f32 = 0.;
13461343
let mut r: f32 = 0.;
1347-
let gid = aat::MapCharToGlyph_AAT(*attributes, ch as u32);
1344+
let gid = aat::MapCharToGlyph_AAT(*attributes, ch);
13481345
aat::GetGlyphSidebearings_AAT(*attributes, gid as u16, &mut l, &mut r);
13491346
(l, r)
13501347
}
13511348
Otgr(engine) => {
1352-
let gid = engine.map_char_to_glyph(ch as u32) as i32;
1349+
let gid = engine.map_char_to_glyph(ch) as i32;
13531350
engine.get_glyph_sidebearings(gid as u32)
13541351
}
13551352
};
@@ -1380,7 +1377,7 @@ pub(crate) unsafe fn get_glyph_bounds(font: usize, edge: i32, gid: i32) -> Scale
13801377
};
13811378
D2Fix((if edge <= 2i32 { a } else { b }) as f64)
13821379
}
1383-
pub(crate) unsafe fn getnativecharic(f: &NativeFont, letter_space: Scaled, c: i32) -> Scaled {
1380+
pub(crate) unsafe fn getnativecharic(f: &NativeFont, letter_space: Scaled, c: char) -> Scaled {
13841381
let (_, rsb) = get_native_char_sidebearings(f, c);
13851382
if rsb < Scaled::ZERO {
13861383
letter_space - rsb
@@ -1389,15 +1386,15 @@ pub(crate) unsafe fn getnativecharic(f: &NativeFont, letter_space: Scaled, c: i3
13891386
}
13901387
}
13911388
/* single-purpose metrics accessors */
1392-
pub(crate) unsafe fn getnativecharwd(f: usize, c: i32) -> Scaled {
1389+
pub(crate) unsafe fn getnativecharwd(f: usize, c: char) -> Scaled {
13931390
match &FONT_LAYOUT_ENGINE[f] {
13941391
#[cfg(target_os = "macos")]
13951392
Font::Native(Aat(attributes)) => {
1396-
let gid = aat::MapCharToGlyph_AAT(*attributes, c as u32);
1393+
let gid = aat::MapCharToGlyph_AAT(*attributes, c);
13971394
D2Fix(aat::GetGlyphWidth_AAT(*attributes, gid as u16))
13981395
}
13991396
Font::Native(Otgr(engine)) => {
1400-
let gid = engine.map_char_to_glyph(c as u32) as i32;
1397+
let gid = engine.map_char_to_glyph(c) as i32;
14011398
D2Fix(engine.get_glyph_width_from_engine(gid as u32) as f64)
14021399
}
14031400
_ => panic!("bad native font flag in `get_native_char_wd`"),
@@ -1431,7 +1428,7 @@ pub(crate) unsafe fn store_justified_native_glyphs(node: &mut NativeWord) {
14311428
/* apply justification to spaces (or if there are none, distribute it to all glyphs as a last resort) */
14321429
let glyph_count = node.glyph_count() as usize;
14331430
let mut spaceCount: i32 = 0i32;
1434-
let spaceGlyph: i32 = map_char_to_glyph(nf, ' ' as i32);
1431+
let spaceGlyph: i32 = map_char_to_glyph(nf, ' ');
14351432
for i in 0..glyph_count {
14361433
if node.glyph_ids()[i] as i32 == spaceGlyph {
14371434
spaceCount += 1
@@ -1701,14 +1698,11 @@ pub(crate) unsafe fn measure_native_glyph(node: &mut Glyph, use_glyph_metrics: b
17011698
node.set_depth(Scaled(DEPTH_BASE[f]));
17021699
};
17031700
}
1704-
pub(crate) unsafe fn map_char_to_glyph(font: &NativeFont, ch: i32) -> i32 {
1705-
if ch > 0x10ffff || ch >= 0xd800 && ch <= 0xdfff {
1706-
return 0i32;
1707-
}
1701+
pub(crate) unsafe fn map_char_to_glyph(font: &NativeFont, ch: char) -> i32 {
17081702
match font {
17091703
#[cfg(target_os = "macos")]
1710-
Aat(engine) => aat::MapCharToGlyph_AAT(*engine, ch as u32),
1711-
Otgr(engine) => engine.map_char_to_glyph(ch as u32) as i32,
1704+
Aat(engine) => aat::MapCharToGlyph_AAT(*engine, ch),
1705+
Otgr(engine) => engine.map_char_to_glyph(ch) as i32,
17121706
}
17131707
}
17141708
pub(crate) unsafe fn map_glyph_to_index(font: &NativeFont, name: &str) -> i32 {

0 commit comments

Comments
 (0)