Skip to content

Commit

Permalink
Add test_string_info()
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Jun 7, 2018
1 parent 34f9e53 commit 2906666
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions R/RcppExports.R
Expand Up @@ -25,3 +25,7 @@ test_string_width <- function(string, font_size, font_file) {
.Call(`_vdiffr_test_string_width`, string, font_size, font_file)
}

test_string_info <- function(string, font_size, font_file) {
.Call(`_vdiffr_test_string_info`, string, font_size, font_file)
}

14 changes: 14 additions & 0 deletions src/RcppExports.cpp
Expand Up @@ -85,6 +85,19 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// test_string_info
SEXP test_string_info(SEXP string, SEXP font_size, SEXP font_file);
RcppExport SEXP _vdiffr_test_string_info(SEXP stringSEXP, SEXP font_sizeSEXP, SEXP font_fileSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< SEXP >::type string(stringSEXP);
Rcpp::traits::input_parameter< SEXP >::type font_size(font_sizeSEXP);
Rcpp::traits::input_parameter< SEXP >::type font_file(font_fileSEXP);
rcpp_result_gen = Rcpp::wrap(test_string_info(string, font_size, font_file));
return rcpp_result_gen;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"_vdiffr_compare_files", (DL_FUNC) &_vdiffr_compare_files, 2},
Expand All @@ -93,6 +106,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_vdiffr_get_svg_content", (DL_FUNC) &_vdiffr_get_svg_content, 1},
{"_vdiffr_library_load", (DL_FUNC) &_vdiffr_library_load, 0},
{"_vdiffr_test_string_width", (DL_FUNC) &_vdiffr_test_string_width, 3},
{"_vdiffr_test_string_info", (DL_FUNC) &_vdiffr_test_string_info, 3},
{NULL, NULL, 0}
};

Expand Down
32 changes: 32 additions & 0 deletions src/utils.cpp
Expand Up @@ -49,3 +49,35 @@ SEXP test_string_width(SEXP string, SEXP font_size, SEXP font_file) {
UNPROTECT(n_protect);
return info;
}


const char* string_info_names[] = {
"width", "height", "ascent", "descent", ""
};

// [[Rcpp::export]]
SEXP test_string_info(SEXP string, SEXP font_size, SEXP font_file) {
int n_protect = validate_string_info_inputs(&string, &font_size, &font_file);

const char* text = Rf_translateCharUTF8(STRING_ELT(string, 0));
const char* font_path = CHAR(STRING_ELT(font_file, 0));
double size = REAL(font_size)[0];

struct fthb_string_info metrics;
int error = fthb_calc_string_info(text, font_path, size, &metrics);
if (error) {
Rf_errorcall(R_NilValue, "Couldn't compute textbox metrics: %d", error);
}

SEXP info = PROTECT(Rf_mkNamed(REALSXP, string_info_names));
++n_protect;
double* info_ptr = REAL(info);

*info_ptr = metrics.width;
*(++info_ptr) = metrics.height;
*(++info_ptr) = metrics.ascent;
*(++info_ptr) = metrics.descent;

UNPROTECT(n_protect);
return info;
}

0 comments on commit 2906666

Please sign in to comment.