Skip to content

Commit

Permalink
[ttf] fixed a bug where advance_width/left_side_bearings are not retr…
Browse files Browse the repository at this point in the history
…ieved correctly
  • Loading branch information
harrand committed Nov 16, 2023
1 parent c70511c commit b561fb1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
8 changes: 8 additions & 0 deletions demo/ren/tz_text_rendering_demo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ add_demo(
SOURCE_FILES
main.cpp
)

add_text(
TARGET tz_text_rendering_demo
INPUT_DIR ${PROJECT_SOURCE_DIR}
OUTPUT_DIR ${PROJECT_BINARY_DIR}
TEXT_FILES
demo/ren/tz_text_rendering_demo/res/ProggyClean.ttf
)
6 changes: 6 additions & 0 deletions demo/ren/tz_text_rendering_demo/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#include "tz/tz.hpp"
#include "tz/gl/device.hpp"
#include "tz/io/ttf.hpp"

#include "tz/core/imported_text.hpp"
#include ImportedTextHeader(ProggyClean, ttf)

int main()
{
tz::initialise({.name = "tz_text_rendering_demo"});
{
tz::io::ttf ttf = tz::io::ttf::from_memory(ImportedTextData(ProggyClean, ttf));

tz::begin_frame();
tz::gl::get_device().render();
tz::end_frame();
Expand Down
20 changes: 17 additions & 3 deletions src/tz/io/ttf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,21 @@ namespace tz::io
char c = ttf_alphabet[i];
auto index = this->cmap.glyph_index_map[c | 0] | 0;
auto glyfd = this->glyf.glyfs[index];
auto hmtxd = this->hmtx.hmetrics[index];
// its possible index > hmetrics.length
// spec:The table uses a longHorMetric record to give the advance width and left side bearing of a glyph. Records are indexed by glyph ID. As an optimization, the number of records can be less than the number of glyphs, in which case the advance width value of the last record applies to all remaining glyph IDs
// If numberOfHMetrics is less than the total number of glyphs, then the hMetrics array is followed by an array for the left side bearing values of the remaining glyphs.
std::size_t advance_width, left_side_bearing;
if(std::cmp_greater_equal(index, this->hmtx.hmetrics.size()))
{
advance_width = hmtx.hmetrics.back().advance_width;
left_side_bearing = hmtx.left_side_bearings[index - hmtx.hmetrics.size()];
}
else
{
auto hmtxd = this->hmtx.hmetrics[index];
advance_width = hmtxd.advance_width;
left_side_bearing = hmtxd.left_side_bearing;
}

ttf_glyph_shape_info shape;
tz::assert(glyfd.x_coords.size() == glyfd.y_coords.size());
Expand All @@ -644,8 +658,8 @@ namespace tz::io
{
.position = static_cast<tz::vec2i>(tz::vector<std::int16_t, 2>{glyfd.xmin, glyfd.ymin}),
.dimensions = static_cast<tz::vec2ui>(tz::vector<int, 2>{glyfd.xmax - glyfd.xmin, glyfd.ymax - glyfd.ymin}),
.left_side_bearing = static_cast<int>(hmtxd.left_side_bearing),
.right_side_bearing = static_cast<int>(hmtxd.advance_width - hmtxd.left_side_bearing - (glyfd.xmax - glyfd.xmin))
.left_side_bearing = static_cast<int>(left_side_bearing),
.right_side_bearing = static_cast<int>(advance_width - left_side_bearing - (glyfd.xmax - glyfd.xmin))
},
.shape = shape
};
Expand Down

0 comments on commit b561fb1

Please sign in to comment.