Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ jobs:
-o /tmp/hr-subset
/tmp/hr-subset harfrust/tests/fonts/rb_custom/PT_Sans-Caption-Web-Regular.ttf

- name: HarfBuzz C++ ownership helpers
run: |
for standard in c++11 c++17; do
for order in CORE SUBSET_FIRST SUBSET_LAST; do
library_directory=target/debug
if [ "$order" = CORE ]; then
library_directory=target/capi-default/debug
fi
c++ -std="$standard" -pedantic-errors -Wall -Wextra -Werror \
-D"HR_TEST_$order" \
-Iharfrust_capi/include \
harfrust_capi/tests/cplusplus.cc -L"$library_directory" \
-lharfrust_c -Wl,-rpath,"$PWD/$library_directory" \
-o /tmp/hr-cplusplus
/tmp/hr-cplusplus harfrust/tests/fonts/rb_custom/PT_Sans-Caption-Web-Regular.ttf
done
done

- name: Rustfmt
run: cargo fmt --all -- --check

Expand Down
12 changes: 12 additions & 0 deletions harfrust_capi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,15 @@ Enable the optional `subset` feature for Skera-backed subsetting in the same
C library, using the existing face, blob, and set handles. Include `hr-subset.h`
for the native API or `hr-hb-subset.h` for HarfBuzz spellings. See
[SUBSETTING.md](SUBSETTING.md) for building, supported flags, and validation.

## HarfBuzz C++ helpers

Include `hr-hb-cplusplus.hh` in place of HarfBuzz's `hb-cplusplus.hh` for
the C++11 `hb::shared_ptr<T>` and `hb::unique_ptr<T>` ownership helpers,
their traits, and hash specializations. They adopt an existing reference;
copying a shared pointer acquires another reference. Supported core types
are blobs, buffers, faces, fonts, font functions, maps, sets, and shape plans.

Including `hr-hb-subset.h` before or after the C++ header also enables
subset inputs and plans. Build `harfrust_capi` with `--features subset`
when using those objects. Objects without a complete HarfRust lifetime API have no vtable.
227 changes: 227 additions & 0 deletions harfrust_capi/include/hr-hb-cplusplus.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
/*
* Copyright © 2022 Behdad Esfahbod
*
* This is part of HarfBuzz, a text shaping library.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/

#ifndef HARFRUST_HB_CPLUSPLUS_HH
#define HARFRUST_HB_CPLUSPLUS_HH

/* Adapted from HarfBuzz's hb-cplusplus.hh. Ownership helpers retain the
* hb namespace; user-data adapters accommodate HarfRust's const signatures.
* Only objects with the required HarfRust lifetime API are specialized.
*/
#include "hr-hb.h"

#ifdef __cplusplus

#include <functional>
#include <utility>
#include <type_traits>

#if 0
#if !(__cplusplus >= 201103L)
#error "HarfBuzz C++ helpers require C++11"
#endif
#endif

namespace hb {


template <typename T>
struct vtable;

template <typename T>
struct shared_ptr
{
using element_type = T;

using v = vtable<T>;

explicit shared_ptr (T *p = nullptr) : p (p) {}
shared_ptr (const shared_ptr &o) : p (v::reference (o.p)) {}
shared_ptr (shared_ptr &&o) noexcept : p (o.p) { o.p = nullptr; }
shared_ptr& operator = (const shared_ptr &o) { if (p != o.p) { destroy (); p = o.p; reference (); } return *this; }
shared_ptr& operator = (shared_ptr &&o) noexcept { v::destroy (p); p = o.p; o.p = nullptr; return *this; }
~shared_ptr () { v::destroy (p); p = nullptr; }

T* get() const { return p; }

void swap (shared_ptr &o) noexcept { std::swap (p, o.p); }
friend void swap (shared_ptr &a, shared_ptr &b) noexcept { std::swap (a.p, b.p); }

operator T * () const { return p; }
T& operator * () const { return *get (); }
T* operator -> () const { return get (); }
operator bool () const { return p; }
bool operator == (const shared_ptr &o) const { return p == o.p; }
bool operator != (const shared_ptr &o) const { return p != o.p; }

static T* get_empty() { return v::get_empty (); }
T* reference() { return v::reference (p); }
void destroy() { v::destroy (p); }
void set_user_data (hb_user_data_key_t *key,
void *value,
hb_destroy_func_t destroy,
hb_bool_t replace) { v::set_user_data (p, key, value, destroy, replace); }
void * get_user_data (hb_user_data_key_t *key) { return v::get_user_data (p, key); }

private:
T *p;
};

template<typename T> struct is_shared_ptr : std::false_type {};
template<typename T> struct is_shared_ptr<shared_ptr<T>> : std::true_type {};

template <typename T>
struct unique_ptr
{
using element_type = T;

using v = vtable<T>;

explicit unique_ptr (T *p = nullptr) : p (p) {}
unique_ptr (const unique_ptr &o) = delete;
unique_ptr (unique_ptr &&o) noexcept : p (o.p) { o.p = nullptr; }
unique_ptr& operator = (const unique_ptr &o) = delete;
unique_ptr& operator = (unique_ptr &&o) noexcept { v::destroy (p); p = o.p; o.p = nullptr; return *this; }
~unique_ptr () { v::destroy (p); p = nullptr; }

T* get() const { return p; }
T* release () { T* v = p; p = nullptr; return v; }

void swap (unique_ptr &o) noexcept { std::swap (p, o.p); }
friend void swap (unique_ptr &a, unique_ptr &b) noexcept { std::swap (a.p, b.p); }

operator T * () const { return p; }
T& operator * () const { return *get (); }
T* operator -> () const { return get (); }
operator bool () { return p; }

private:
T *p;
};

template<typename T> struct is_unique_ptr : std::false_type {};
template<typename T> struct is_unique_ptr<unique_ptr<T>> : std::true_type {};

template <typename T,
T * (*_get_empty) (void),
T * (*_reference) (T *),
void (*_destroy) (T *),
hb_bool_t (*_set_user_data) (T *,
hb_user_data_key_t *,
void *,
hb_destroy_func_t,
hb_bool_t),
void * (*_get_user_data) (const T *,
hb_user_data_key_t *)>
struct vtable_t
{
static constexpr auto get_empty = _get_empty;
static constexpr auto reference = _reference;
static constexpr auto destroy = _destroy;
static constexpr auto set_user_data = _set_user_data;
static constexpr auto get_user_data = _get_user_data;
};

/* HarfRust's key parameters are const, while several core user-data getters
* take a mutable object. Preserve the HarfBuzz vtable_t signature using these
* forwarding functions; they never modify the key or object themselves. */
template <typename T>
struct vtable_user_data;

#define HB_DEFINE_VTABLE(name, empty) \
template<> \
struct vtable_user_data<hb_##name##_t> \
{ \
static hb_bool_t set (hb_##name##_t *p, hb_user_data_key_t *key, \
void *value, hb_destroy_func_t destroy, \
hb_bool_t replace) \
{ return hb_##name##_set_user_data (p, key, value, destroy, replace); } \
static void * get (const hb_##name##_t *p, hb_user_data_key_t *key) \
{ return hb_##name##_get_user_data (const_cast<hb_##name##_t *> (p), key); } \
}; \
template<> \
struct vtable<hb_##name##_t> \
: vtable_t<hb_##name##_t, \
empty, \
&hb_##name##_reference, \
&hb_##name##_destroy, \
&vtable_user_data<hb_##name##_t>::set, \
&vtable_user_data<hb_##name##_t>::get> {}

HB_DEFINE_VTABLE (buffer, &hb_buffer_get_empty);
HB_DEFINE_VTABLE (blob, &hb_blob_get_empty);
HB_DEFINE_VTABLE (face, &hb_face_get_empty);
HB_DEFINE_VTABLE (font, &hb_font_get_empty);
HB_DEFINE_VTABLE (font_funcs, &hb_font_funcs_get_empty);
HB_DEFINE_VTABLE (map, &hb_map_get_empty);
HB_DEFINE_VTABLE (set, &hb_set_get_empty);
HB_DEFINE_VTABLE (shape_plan, &hb_shape_plan_get_empty);


#ifdef HARFRUST_HB_SUBSET_H

HB_DEFINE_VTABLE (subset_input, nullptr);
HB_DEFINE_VTABLE (subset_plan, nullptr);

#endif


/* Left available for the subset header's paired block when it is included
* after this header. */


} // namespace hb

/* Workaround for GCC < 7, see:
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480
* https://stackoverflow.com/a/25594741 */
namespace std {


template<typename T>
struct hash<hb::shared_ptr<T>>
{
std::size_t operator()(const hb::shared_ptr<T>& v) const noexcept
{
std::size_t h = std::hash<decltype (v.get ())>{}(v.get ());
return h;
}
};

template<typename T>
struct hash<hb::unique_ptr<T>>
{
std::size_t operator()(const hb::unique_ptr<T>& v) const noexcept
{
std::size_t h = std::hash<decltype (v.get ())>{}(v.get ());
return h;
}
};


} // namespace std

#endif /* __cplusplus */

#endif /* HARFRUST_HB_CPLUSPLUS_HH */
7 changes: 7 additions & 0 deletions harfrust_capi/include/hr-hb-subset.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,11 @@
#define HB_SUBSET_SETS_NO_SUBSET_TABLE_TAG HR_SUBSET_SETS_NO_SUBSET_TABLE_TAG
#define HB_SUBSET_SETS_UNICODE HR_SUBSET_SETS_UNICODE

#if defined(__cplusplus) && defined(HARFRUST_HB_CPLUSPLUS_HH)
namespace hb {
HB_DEFINE_VTABLE (subset_input, nullptr);
HB_DEFINE_VTABLE (subset_plan, nullptr);
} // namespace hb
#endif

#endif /* HARFRUST_HB_SUBSET_H */
Loading
Loading