Skip to content

Commit

Permalink
Make sure old API still works during transition period
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Nov 12, 2020
1 parent 7d97df6 commit d52574e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
51 changes: 22 additions & 29 deletions inst/include/textshaping.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,6 @@
#include <vector>
#include <string>

// Calculate the width of a string based on a fontfile, index, size, and
// resolution. Writes it to width, and returns 0 if successful
static inline int ts_string_width(const char* string, FontSettings font_info,
double size, double res, int include_bearing,
double* width) {
static int (*p_ts_string_width)(const char*, FontSettings, double, double, int, double*) = NULL;
if (p_ts_string_width == NULL) {
p_ts_string_width = (int (*)(const char*, FontSettings, double, double, int, double*)) R_GetCCallable("textshaping", "ts_string_width");
}
return p_ts_string_width(string, font_info, size, res, include_bearing, width);
}

// Calculate glyph positions and id in the font file for a string based on a
// fontfile, index, size, and resolution, and writes it to the x and y arrays.
// Returns 0 if successful.
static inline int ts_string_shape(const char* string, FontSettings font_info,
double size, double res, double* x, double* y,
int* id, int* cluster, int* n_glyphs,
unsigned int max_length) {
Rf_warning("Please update your device for the latest version of textshaping");
return 1;
}

namespace textshaping {
struct Point {
double x;
Expand All @@ -48,8 +25,8 @@ struct Point {
// Calculate the width of a string based on a fontfile, index, size, and
// resolution. Writes it to width, and returns 0 if successful
static inline int string_width(const char* string, FontSettings font_info,
double size, double res, int include_bearing,
double* width) {
double size, double res, int include_bearing,
double* width) {
static int (*p_ts_string_width)(const char*, FontSettings, double, double, int, double*) = NULL;
if (p_ts_string_width == NULL) {
p_ts_string_width = (int (*)(const char*, FontSettings, double, double, int, double*)) R_GetCCallable("textshaping", "ts_string_width");
Expand All @@ -61,15 +38,31 @@ static inline int string_width(const char* string, FontSettings font_info,
// fontfile, index, size, and resolution, and writes it to the x and y arrays.
// Returns 0 if successful.
static inline int string_shape(const char* string, FontSettings font_info,
double size, double res, std::vector<Point>& loc,
std::vector<uint32_t>& id, std::vector<int>& cluster,
std::vector<unsigned int>& font, std::vector<FontSettings>& fallbacks) {
double size, double res, std::vector<Point>& loc,
std::vector<uint32_t>& id, std::vector<int>& cluster,
std::vector<unsigned int>& font, std::vector<FontSettings>& fallbacks) {
static int (*p_ts_string_shape)(const char*, FontSettings, double, double, std::vector<Point>&, std::vector<uint32_t>&, std::vector<int>&, std::vector<unsigned int>&, std::vector<FontSettings>&) = NULL;
if (p_ts_string_shape == NULL) {
p_ts_string_shape = (int (*)(const char*, FontSettings, double, double, std::vector<Point>&, std::vector<uint32_t>&, std::vector<int>&, std::vector<unsigned int>&, std::vector<FontSettings>&)) R_GetCCallable("textshaping", "ts_string_shape");
p_ts_string_shape = (int (*)(const char*, FontSettings, double, double, std::vector<Point>&, std::vector<uint32_t>&, std::vector<int>&, std::vector<unsigned int>&, std::vector<FontSettings>&)) R_GetCCallable("textshaping", "ts_string_shape_new");
}
return p_ts_string_shape(string, font_info, size, res, loc, id, cluster, font, fallbacks);
}
};


// Old API ---------------------------------------------------------------------
static inline int ts_string_width(const char* string, FontSettings font_info,
double size, double res, int include_bearing,
double* width) {
return textshaping::string_width(string, font_info, size, res, include_bearing, width);
}
static inline int ts_string_shape(const char* string, FontSettings font_info,
double size, double res, double* x, double* y,
int* id, int* cluster, int* n_glyphs,
unsigned int max_length) {
static int (*p_ts_string_shape)(const char*, FontSettings, double, double, double*, double*, int*, int*, unsigned int) = NULL;
if (p_ts_string_shape == NULL) {
p_ts_string_shape = (int (*)(const char*, FontSettings, double, double, double*, double*, int*, int*, unsigned int)) R_GetCCallable("textshaping", "ts_string_shape");
}
return p_ts_string_shape(string, font_info, size, res, x, y, id, n_glyphs, max_length);
}
27 changes: 26 additions & 1 deletion src/string_metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,35 @@ int ts_string_shape(const char* string, FontSettings font_info, double size,
END_CPP11_NO_RETURN
return 0;
}
int ts_string_shape_old(const char* string, FontSettings font_info, double size,
double res, double* x, double* y, int* id, int* n_glyphs,
unsigned int max_length) {
int result = 0;
BEGIN_CPP11
std::vector<Point> _loc;
std::vector<uint32_t> _id;
std::vector<int> _cluster;
std::vector<unsigned int> _font;
std::vector<FontSettings> _fallbacks;
result = ts_string_shape(string, font_info, size, res, _loc, _id, _cluster, _font, _fallbacks);

if (result == 0) {
*n_glyphs = max_length > _loc.size() ? _loc.size() : max_length;
for (int i = 0; i < *n_glyphs; ++i) {
x[i] = _loc[i].x;
y[i] = _loc[i].y;
id[i] = (int) _id[i];
}
}

END_CPP11_NO_RETURN
return result;
}

#endif

void export_string_metrics(DllInfo* dll) {
R_RegisterCCallable("textshaping", "ts_string_width", (DL_FUNC)ts_string_width);
R_RegisterCCallable("textshaping", "ts_string_shape", (DL_FUNC)ts_string_shape);
R_RegisterCCallable("textshaping", "ts_string_shape_new", (DL_FUNC)ts_string_shape);
R_RegisterCCallable("textshaping", "ts_string_shape", (DL_FUNC)ts_string_shape_old);
}
3 changes: 3 additions & 0 deletions src/string_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@ int ts_string_shape(const char* string, FontSettings font_info, double size,
double res, std::vector<Point>& loc, std::vector<uint32_t>& id,
std::vector<int>& cluster, std::vector<unsigned int>& font,
std::vector<FontSettings>& fallbacks);
int ts_string_shape_old(const char* string, FontSettings font_info, double size,
double res, double* x, double* y, int* id, int* n_glyphs,
unsigned int max_length);
[[cpp11::init]]
void export_string_metrics(DllInfo* dll);

0 comments on commit d52574e

Please sign in to comment.