Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
* Keep float precision for positions in buffer and glyph offsets.
Browse files Browse the repository at this point in the history
* Account for lsb_delta from fonts with hinting by adding it to glyph offset.
* Keep float precision when adding line_height which is saved to total_height/size.y.

PiperOrigin-RevId: 179851957
Change-Id: I47b658d641d430321b471602d08ef0a4bb489cb4
  • Loading branch information
Googler authored and haroonq committed Feb 9, 2018
1 parent 152db99 commit 712e922
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
8 changes: 4 additions & 4 deletions include/flatui/internal/glyph_cache.h
Expand Up @@ -170,7 +170,7 @@ class GlyphCacheEntry {
typedef std::list<GlyphCacheRow>::iterator iterator_row;

GlyphCacheEntry()
: code_point_(0), size_(0, 0), offset_(0, 0), advance_(0, 0),
: code_point_(0), size_(0, 0), offset_(0.f, 0.f), advance_(0, 0),
color_glyph_(false) {}

// Setter/Getter of code point.
Expand All @@ -183,8 +183,8 @@ class GlyphCacheEntry {
void set_size(const mathfu::vec2i &size) { size_ = size; }

// Setter/Getter of cache entry offset.
mathfu::vec2i get_offset() const { return offset_; }
void set_offset(const mathfu::vec2i &offset) { offset_ = offset; }
mathfu::vec2 get_offset() const { return offset_; }
void set_offset(const mathfu::vec2 &offset) { offset_ = offset; }

// Setter/Getter of cache entry advance.
mathfu::vec2i get_advance() const { return advance_; }
Expand Down Expand Up @@ -220,7 +220,7 @@ class GlyphCacheEntry {
mathfu::vec2i size_;

// Glyph image's offset value relative to font metrics origin.
mathfu::vec2i offset_;
mathfu::vec2 offset_;

// Glyph image's advance value relative to font metrics origin.
mathfu::vec2i advance_;
Expand Down
9 changes: 4 additions & 5 deletions src/font_buffer.cpp
Expand Up @@ -95,21 +95,20 @@ void FontBuffer::UpdateUnderline(int32_t buffer_idx, int32_t vertex_index,

void FontBuffer::AddVertices(const mathfu::vec2 &pos, int32_t base_line,
float scale, const GlyphCacheEntry &entry) {
mathfu::vec2i rounded_pos = mathfu::vec2i(pos);
mathfu::vec2 scaled_offset = mathfu::vec2(entry.get_offset()) * scale;
mathfu::vec2 scaled_offset = entry.get_offset() * scale;
mathfu::vec2 scaled_size = mathfu::vec2(entry.get_size()) * scale;
mathfu::vec2 scaled_advance = mathfu::vec2(entry.get_advance()) * scale;

auto x = rounded_pos.x + scaled_offset.x;
auto y = rounded_pos.y + base_line - scaled_offset.y;
auto x = pos.x + scaled_offset.x;
auto y = pos.y + base_line - scaled_offset.y;
auto uv = entry.get_uv();
vertices_.push_back(FontVertex(x, y, 0.0f, uv.x, uv.y));
vertices_.push_back(FontVertex(x, y + scaled_size.y, 0.0f, uv.x, uv.w));
vertices_.push_back(FontVertex(x + scaled_size.x, y, 0.0f, uv.z, uv.y));
vertices_.push_back(
FontVertex(x + scaled_size.x, y + scaled_size.y, 0.0f, uv.z, uv.w));

last_pos_ = mathfu::vec2(rounded_pos.x, rounded_pos.y);
last_pos_ = pos;
last_advance_ = last_pos_ + scaled_advance;
}

Expand Down
24 changes: 13 additions & 11 deletions src/font_manager.cpp
Expand Up @@ -527,7 +527,7 @@ FontBuffer *FontManager::FillBuffer(const char *text, uint32_t length,
// Initialize font metrics parameters.
int32_t max_line_width = 0;
// Height calculation needs to use ysize before conversion.
int32_t total_height = ysize;
float total_height = ysize;
bool first_character = true;
auto line_height = ysize * line_height_scale_;
FontMetrics initial_metrics;
Expand All @@ -543,7 +543,7 @@ FontBuffer *FontManager::FillBuffer(const char *text, uint32_t length,
// buffer.
initial_metrics = buffer->metrics();
max_line_width = buffer->get_size().x * kFreeTypeUnit;
total_height = buffer->get_size().y;
total_height = static_cast<float>(buffer->get_size().y);

if (context->current_font_size() != ysize) {
// Adjust glyph positions in current line?
Expand Down Expand Up @@ -610,7 +610,7 @@ FontBuffer *FontManager::FillBuffer(const char *text, uint32_t length,
auto rewind = 0;
auto last_line =
size.y &&
total_height + static_cast<int32_t>(line_height) > size.y;
static_cast<int32_t>(total_height + line_height) > size.y;
auto word_width = static_cast<int32_t>(
LayoutText(text + word_enum.GetCurrentWordIndex(),
word_enum.GetCurrentWordLength(), max_width / scale,
Expand Down Expand Up @@ -650,7 +650,7 @@ FontBuffer *FontManager::FillBuffer(const char *text, uint32_t length,
break;
}
// Line break.
total_height += static_cast<int32_t>(line_height);
total_height += line_height;
buffer->UpdateLine(parameters, layout_direction_, context);
pos = new_pos;

Expand Down Expand Up @@ -803,8 +803,8 @@ FontManager::ErrorType FontManager::UpdateBuffer(
// Calculate internal/external leading value and expand a buffer if
// necessary.
FontMetrics new_metrics;
if (UpdateMetrics(cache->get_offset().y, cache->get_size().y, *metrics,
&new_metrics)) {
if (UpdateMetrics(static_cast<int32_t>(cache->get_offset().y),
cache->get_size().y, *metrics, &new_metrics)) {
*metrics = new_metrics;
}

Expand Down Expand Up @@ -1570,13 +1570,15 @@ const GlyphCacheEntry *FontManager::GetCachedEntry(uint32_t code_point,
GlyphCacheEntry entry;
entry.set_code_point(code_point);
bool color_glyph = g->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA;
float bitmap_left = g->bitmap_left +
static_cast<float>(g->lsb_delta) / kFreeTypeUnit;

// Does not support SDF for color glyphs.
if (flags & (kGlyphFlagsOuterSDF | kGlyphFlagsInnerSDF) &&
g->bitmap.width && g->bitmap.rows && !color_glyph) {
// Adjust a glyph size and an offset with a padding.
entry.set_offset(vec2i(g->bitmap_left - kGlyphCachePaddingSDF,
g->bitmap_top + kGlyphCachePaddingSDF));
entry.set_offset(vec2(bitmap_left - kGlyphCachePaddingSDF,
g->bitmap_top + kGlyphCachePaddingSDF));
entry.set_size(vec2i(g->bitmap.width + kGlyphCachePaddingSDF * 2,
g->bitmap.rows + kGlyphCachePaddingSDF * 2));
entry.set_advance(vec2i(g->advance.x / kFreeTypeUnit, 0));
Expand Down Expand Up @@ -1630,13 +1632,13 @@ const GlyphCacheEntry *FontManager::GetCachedEntry(uint32_t code_point,
}

const float kEmojiBaseLine = 0.85f;
entry.set_offset(vec2i(
vec2(g->bitmap_left * glyph_scale, new_height * kEmojiBaseLine)));
entry.set_offset(vec2(bitmap_left * glyph_scale,
new_height * kEmojiBaseLine));
entry.set_size(vec2i(new_width, new_height));
entry.set_advance(vec2i(new_advance, 0));
cache = glyph_cache_->Set(out_buffer.get(), key, entry);
} else {
entry.set_offset(vec2i(g->bitmap_left, g->bitmap_top));
entry.set_offset(vec2(bitmap_left, g->bitmap_top));
entry.set_size(vec2i(g->bitmap.width, g->bitmap.rows));
entry.set_advance(vec2i(g->advance.x / kFreeTypeUnit, 0));
cache = glyph_cache_->Set(g->bitmap.buffer, key, entry);
Expand Down

0 comments on commit 712e922

Please sign in to comment.