Skip to content

Commit

Permalink
fix: missing registerFromPath implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Jul 12, 2021
1 parent 7df9ca6 commit 8bac515
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions __test__/draw.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const png = PNG()

const fontIosevka = readFileSync(join(__dirname, 'fonts', 'iosevka-slab-regular.ttf'))
const fontSourceSerifPro = readFileSync(join(__dirname, 'fonts', 'SourceSerifPro-Regular.ttf'))
const fontOSRS = readFileSync(join(__dirname, 'fonts', 'osrs-font-compact.otf'))
const fontOSRSPath = join(__dirname, 'fonts', 'osrs-font-compact.otf')

console.assert(GlobalFonts.register(fontIosevka), 'Register Iosevka font failed')
console.assert(GlobalFonts.register(fontSourceSerifPro), 'Register SourceSerifPro font failed')
Expand Down Expand Up @@ -395,7 +395,7 @@ test('fillText', async (t) => {
})

test('fillText-AA', async (t) => {
GlobalFonts.register(fontOSRS)
GlobalFonts.registerFromPath(fontOSRSPath)
const { ctx, canvas } = t.context
ctx.imageSmoothingEnabled = false
ctx.font = '16px OSRSFontCompact'
Expand Down
8 changes: 8 additions & 0 deletions skia-c/skia_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,14 @@ extern "C"
return fp->registerTypeface(typeface);
}

size_t skiac_typeface_font_provider_register_from_file(skiac_typeface_font_provider *c_typeface_font_provider, skiac_font_mgr *c_font_mgr, const char *font_path)
{
auto fp = reinterpret_cast<TypefaceFontProvider *>(c_typeface_font_provider);
auto font_mgr = reinterpret_cast<SkFontMgr *>(c_font_mgr);
auto typeface = font_mgr->makeFromFile(font_path);
return fp->registerTypeface(typeface);
}

void skiac_typeface_font_provider_ref(skiac_typeface_font_provider *c_typeface_font_provider)
{
reinterpret_cast<TypefaceFontProvider *>(c_typeface_font_provider)->ref();
Expand Down
1 change: 1 addition & 0 deletions skia-c/skia_c.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ extern "C"
// TypefaceFontProvider
skiac_typeface_font_provider *skiac_typeface_font_provider_create();
size_t skiac_typeface_font_provider_register(skiac_typeface_font_provider *c_typeface_font_provider, skiac_font_mgr *c_font_mgr, uint8_t *font, size_t length);
size_t skiac_typeface_font_provider_register_from_file(skiac_typeface_font_provider *c_typeface_font_provider, skiac_font_mgr *c_font_mgr, const char *font_path);
void skiac_typeface_font_provider_ref(skiac_typeface_font_provider *c_typeface_font_provider);
void skiac_typeface_font_provider_unref(skiac_typeface_font_provider *c_typeface_font_provider);

Expand Down
10 changes: 10 additions & 0 deletions src/global_fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ fn register(ctx: CallContext) -> Result<JsBoolean> {
ctx.env.get_boolean(register_result)
}

#[js_function(1)]
fn register_from_path(ctx: CallContext) -> Result<JsBoolean> {
let this = ctx.this_unchecked::<JsObject>();
let typeface_font_provider = ctx.env.unwrap::<TypefaceFontProvider>(&this)?;
let font_path = ctx.get::<JsString>(0)?.into_utf8()?;
let register_result = typeface_font_provider.register_from_path(font_path.as_str()?);
ctx.env.get_boolean(register_result)
}

#[js_function]
fn get_families(ctx: CallContext) -> Result<JsObject> {
let this = ctx.this_unchecked::<JsObject>();
Expand All @@ -31,6 +40,7 @@ impl TypefaceFontProvider {
global_fonts_constructor,
&[
Property::new(env, "_register")?.with_method(register),
Property::new(env, "_registerFromPath")?.with_method(register_from_path),
Property::new(env, "_families")?
.with_getter(get_families)
.with_property_attributes(PropertyAttributes::Enumerable),
Expand Down
19 changes: 19 additions & 0 deletions src/sk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use crate::font::FontStyle;
use crate::image::ImageData;

mod ffi {
use std::os::raw::c_char;

use super::SkiaString;

#[repr(C)]
Expand Down Expand Up @@ -627,6 +629,12 @@ mod ffi {
length: usize,
) -> usize;

pub fn skiac_typeface_font_provider_register_from_file(
c_typeface_font_provider: *mut skiac_typeface_font_provider,
c_font_mgr: *mut skiac_font_mgr,
font_path: *const c_char,
) -> usize;

pub fn skiac_typeface_font_provider_ref(
c_typeface_font_provider: *mut skiac_typeface_font_provider,
);
Expand Down Expand Up @@ -2774,6 +2782,17 @@ impl TypefaceFontProvider {
}
}

#[inline]
pub fn register_from_path(&mut self, font_path: &str) -> bool {
if let Ok(fp) = CString::new(font_path) {
unsafe {
ffi::skiac_typeface_font_provider_register_from_file(self.0, self.1, fp.as_ptr()) > 0
}
} else {
false
}
}

#[inline]
pub fn get_families(&self) -> Vec<String> {
let mut names = Vec::new();
Expand Down

0 comments on commit 8bac515

Please sign in to comment.