Skip to content

Commit

Permalink
Merge branch 'feature/subsets' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
KonghaYao committed Nov 13, 2024
2 parents a8411d5 + b948254 commit dfefeb3
Show file tree
Hide file tree
Showing 13 changed files with 296 additions and 142 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ categories = [
"text-processing",
]
license = "MIT"
edition = "2018"
edition = "2021"
links = "harfbuzz"
include = [
"src/*",
Expand Down
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fn main() {
.flag("-std=c++11")
.warnings(false)
.include("harfbuzz/src")
.include("src/subset")
.file("harfbuzz-output.cc");

if !target.contains("windows") {
Expand Down
2 changes: 1 addition & 1 deletion harfbuzz-output.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@
#include "hb-wasm-api.cc"
#include "hb-wasm-shape.cc"

#include "src/subset/hb-glyph-to-svg.cc"
#include "hb-glyph-to-svg-path.cc"
2 changes: 1 addition & 1 deletion src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5273,7 +5273,7 @@ extern "C" {
) -> *mut ::std::os::raw::c_void;
}
extern "C" {
pub fn hb_glyph_to_svg(
pub fn hb_glyph_to_svg_path(
font: *mut hb_font_t,
glyph: hb_codepoint_t,
buf: *mut ::std::os::raw::c_char,
Expand Down
7 changes: 5 additions & 2 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ use crate::bindings::{
hb_font_get_glyph_v_origin, hb_font_get_h_extents, hb_font_get_nominal_glyph,
hb_font_get_parent, hb_font_get_ppem, hb_font_get_scale, hb_font_get_v_extents,
hb_font_get_variation_glyph, hb_font_reference, hb_font_set_funcs, hb_font_set_ppem,
hb_font_set_scale, hb_font_set_variations, hb_font_t, hb_glyph_extents_t, hb_position_t,
hb_font_set_scale, hb_font_set_variations, hb_font_t, hb_glyph_extents_t,
hb_position_t,
};
use crate::common::{HarfbuzzObject, Owned, Shared};
pub use crate::draw_funcs::DrawFuncs;
use crate::draw_funcs::DrawFuncsImpl;
use crate::face::Face;
pub use crate::font_funcs::FontFuncs;
use crate::font_funcs::FontFuncsImpl;
use crate::Variation;
use crate::{Variation};

use std::ffi::CStr;
use std::marker::PhantomData;
Expand Down Expand Up @@ -499,6 +500,7 @@ impl<'a> Font<'a> {
}
}


unsafe impl<'a> Send for Font<'a> {}
unsafe impl<'a> Sync for Font<'a> {}

Expand Down Expand Up @@ -546,4 +548,5 @@ mod test {
fn test_font_extents_layout() {
assert_memory_layout_equal::<FontExtents, hb_font_extents_t>()
}

}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ mod utils;
pub mod rusttype;

pub mod subset;
pub mod svg;

use bindings::hb_feature_t;
use bindings::hb_shape;
Expand Down
2 changes: 1 addition & 1 deletion src/subset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::bindings::{
HB_SUBSET_SETS_LAYOUT_FEATURE_TAG, HB_SUBSET_SETS_LAYOUT_SCRIPT_TAG,
};
use crate::common::{HarfbuzzObject, Owned};
use crate::Face;
use crate::{Face};
use std::marker::PhantomData;
use std::ptr::NonNull;

Expand Down
135 changes: 135 additions & 0 deletions src/subset/hb-glyph-to-svg-path.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include "hb-glyph-to-svg-path.h"
#include "hb-common.h"
#include "hb-draw.h"
#include "string.h"
#include "stdio.h"
#include "stdarg.h"

enum
{
HB_SHAPE_DONT_STOP,
HB_SHAPE_GSUB_PHASE,
HB_SHAPE_GPOS_PHASE
};

struct user_data_t
{
user_data_t(char *str_,
unsigned size_,
unsigned stop_at_ = 0,
unsigned stop_phase_ = 0)
: str(str_), size(size_), stop_at(stop_at_), stop_phase(stop_phase_)
{
}
char *str = nullptr;
unsigned size = 0;
unsigned consumed = 0;
hb_bool_t failure = false;
unsigned stop_at = 0;
unsigned stop_phase = 0;
hb_bool_t stopping = false;
unsigned current_phase = 0;
};

static void
_user_data_printf(user_data_t *data, const char *format, ...)
{
#define BUFSIZE 1000
char buf[BUFSIZE];
int len;
va_list va;

if (!data || data->failure)
return;

va_start(va, format);
len = vsnprintf(buf, BUFSIZE, format, va);
va_end(va);

if (data->consumed + len >= data->size || len < 0 || len > BUFSIZE)
{
data->failure = true;
return;
}

memcpy(data->str + data->consumed, buf, len);
data->consumed += len;
#undef BUFSIZE
}

static void
move_to(hb_draw_funcs_t *dfuncs, user_data_t *draw_data, hb_draw_state_t *,
float to_x, float to_y,
void *)
{
_user_data_printf(draw_data, "M%g,%g", (double)to_x, (double)to_y);
}

static void
line_to(hb_draw_funcs_t *dfuncs, user_data_t *draw_data, hb_draw_state_t *,
float to_x, float to_y,
void *)
{
_user_data_printf(draw_data, "L%g,%g", (double)to_x, (double)to_y);
}

static void
quadratic_to(hb_draw_funcs_t *dfuncs, user_data_t *draw_data, hb_draw_state_t *,
float control_x, float control_y,
float to_x, float to_y,
void *)
{
_user_data_printf(draw_data, "Q%g,%g %g,%g",
(double)control_x,
(double)control_y,
(double)to_x,
(double)to_y);
}

static void
cubic_to(hb_draw_funcs_t *dfuncs, user_data_t *draw_data, hb_draw_state_t *,
float control1_x, float control1_y,
float control2_x, float control2_y,
float to_x, float to_y,
void *)
{
_user_data_printf(draw_data, "C%g,%g %g,%g %g,%g",
(double)control1_x,
(double)control1_y,
(double)control2_x,
(double)control2_y,
(double)to_x,
(double)to_y);
}

static void
close_path(hb_draw_funcs_t *dfuncs, user_data_t *draw_data, hb_draw_state_t *, void *)
{
_user_data_printf(draw_data, "Z");
}

static hb_draw_funcs_t *funcs = 0;

extern "C"
{
int hb_glyph_to_svg_path(hb_font_t *font, hb_codepoint_t glyph, char *buf, unsigned buf_size)
{
if (funcs == 0) /* not the best pattern for multi-threaded apps which is not a concern here */
{
funcs = hb_draw_funcs_create(); /* will be leaked */
hb_draw_funcs_set_move_to_func(funcs, (hb_draw_move_to_func_t)move_to, nullptr, nullptr);
hb_draw_funcs_set_line_to_func(funcs, (hb_draw_line_to_func_t)line_to, nullptr, nullptr);
hb_draw_funcs_set_quadratic_to_func(funcs, (hb_draw_quadratic_to_func_t)quadratic_to, nullptr, nullptr);
hb_draw_funcs_set_cubic_to_func(funcs, (hb_draw_cubic_to_func_t)cubic_to, nullptr, nullptr);
hb_draw_funcs_set_close_path_func(funcs, (hb_draw_close_path_func_t)close_path, nullptr, nullptr);
}

user_data_t draw_data(buf, buf_size);
hb_font_draw_glyph(font, glyph, funcs, &draw_data);
if (draw_data.failure)
return -1;

buf[draw_data.consumed] = '\0';
return draw_data.consumed;
}
}
7 changes: 7 additions & 0 deletions src/subset/hb-glyph-to-svg-path.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "hb.h"
#include "hb-draw.h"

extern "C"
{
int hb_glyph_to_svg_path(hb_font_t *font, hb_codepoint_t glyph, char *buf, unsigned buf_size);
}
131 changes: 0 additions & 131 deletions src/subset/hb-glyph-to-svg.cc

This file was deleted.

4 changes: 0 additions & 4 deletions src/subset/hb-glyph-to-svg.h

This file was deleted.

Loading

0 comments on commit dfefeb3

Please sign in to comment.