Skip to content

Commit

Permalink
Make sure spacers gets an NA glyph
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Jan 14, 2025
1 parent 2afb95d commit dc2d3b7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/string_metrics.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "R_ext/Arith.h"
#include "cpp11/logicals.hpp"
#include "cpp11/protect.hpp"
#define R_NO_REMAP
Expand Down Expand Up @@ -211,7 +212,7 @@ list get_string_shape_c(strings string, integers id, strings path, integers inde
int n_glyphs = shaper.glyph_id.size();
for (int j = 0; j < n_glyphs; j++) {
glyph.push_back((int) shaper.glyph_cluster[j] + 1);
glyph_id.push_back((int) shaper.glyph_id[j]);
glyph_id.push_back((int) (shaper.glyph_id[j] == SPACER_CHAR ? R_NaInt : shaper.glyph_id[j]));
metric_id.push_back(pen_x.size() + 1);
string_id.push_back(shaper.string_id[j] + 1);
x_offset.push_back(double(shaper.x_pos[j]) / 64.0);
Expand Down
10 changes: 5 additions & 5 deletions src/string_shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ bool HarfBuzzShaper::add_string(const char* string, FontSettings& font_info,

if (n_chars == 0) {
// Empty run - we treat is as a 0-width spacer to capture the font ascend and descend
return add_spacer(font_info, size, 0);
return add_spacer(font_info, size, 0, EMPTY_CHAR);
}

full_string.insert(full_string.end(), utc_string, utc_string + n_chars);
Expand All @@ -83,7 +83,7 @@ bool HarfBuzzShaper::add_string(const char* string, FontSettings& font_info,
return true;
}

bool HarfBuzzShaper::add_spacer(FontSettings& font_info, double height, double width) {
bool HarfBuzzShaper::add_spacer(FontSettings& font_info, double height, double width, uint32_t filler) {
width *= 64.0 / 72.0;
int32_t ascend = height * 64.0 * cur_res / 72.0;
int32_t descend = 0;
Expand All @@ -110,7 +110,7 @@ bool HarfBuzzShaper::add_spacer(FontSettings& font_info, double height, double w
// width/x-advance giving the width
info.font_info = font_info;
info.embeddings.push_back({
{SPACER_CHAR}, // glyph_id
{filler}, // glyph_id
{0}, // glyph_cluster
{info.index}, // string_id
{int32_t(width)}, // x_advance
Expand Down Expand Up @@ -191,7 +191,7 @@ bool HarfBuzzShaper::finish_string() {
pen_x = 0;
}
}
if (iter->glyph_id[i] != SPACER_CHAR) {
if (iter->glyph_id[i] != EMPTY_CHAR) { // Avoid adding made up glyph info for empty text runs
glyph_id.push_back(iter->glyph_id[i]);
glyph_cluster.push_back(iter->glyph_cluster[i]);
fontfile.push_back(iter->fallbacks[iter->font[i]].file);
Expand Down Expand Up @@ -243,7 +243,7 @@ bool HarfBuzzShaper::finish_string() {
pen_y -= (line_ascend - previous_line_descend) * (line_width.size() == 1 ? 1 : cur_lineheight);
for (auto iter = line.begin(); iter != line.end(); ++iter) {
for (size_t i = 0; i < iter->glyph_id.size(); ++i) {
if (iter->glyph_id[i] != SPACER_CHAR) {
if (iter->glyph_id[i] != EMPTY_CHAR) { // Avoid adding made up glyph info for empty text runs
y_pos.push_back(pen_y + iter->y_offset[i]);
}
previous_line_descend = std::min(previous_line_descend, iter->descenders[i]);
Expand Down
5 changes: 3 additions & 2 deletions src/string_shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
#include "utils.h"
#include "cache_lru.h"

static const uint32_t SPACER_CHAR = 0xffffffff; // Largest possible value. Unlikely any font would use that
static const uint32_t EMPTY_CHAR = 0xffffffff; // Largest possible value. Unlikely any font would use that
static const uint32_t SPACER_CHAR = 0xffffffff - 1; // Second largest possible value. Also unlikely any font would use that

struct ShapeID {
size_t string_hash;
Expand Down Expand Up @@ -357,7 +358,7 @@ class HarfBuzzShaper {
bool add_string(const char* string, FontSettings& font_info,
double size, double tracking, bool spacer,
std::vector<int>& soft_wrap, std::vector<int>& hard_wrap);
bool add_spacer(FontSettings& font_info, double height, double width);
bool add_spacer(FontSettings& font_info, double height, double width, uint32_t filler = SPACER_CHAR);
bool finish_string();

void shape_text_run(ShapeInfo &text_run, bool ltr);
Expand Down

0 comments on commit dc2d3b7

Please sign in to comment.