-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtext_run.cpp
74 lines (63 loc) · 1.99 KB
/
text_run.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "text_run.h"
#include "scope_guard.h"
#include <cassert>
TextRun::TextRun(Font &font,
const std::string &text,
hb_direction_t direction,
hb_script_t script,
hb_language_t language,
bool underline)
: font_(font), text_(text), direction_(direction), script_(script), language_(language), underline_(underline)
{
setDirty();
}
TextRun::~TextRun()
{
}
size_t TextRun::GetGlyphCount()
{
doLayout();
return glyphs_.size();
}
void TextRun::GetGlyph(size_t index, GlyphInfo &info)
{
doLayout();
assert(index < glyphs_.size());
info = glyphs_[index];
}
void TextRun::setDirty()
{
dirty_ = true;
}
void TextRun::doLayout()
{
if (!dirty_)
return;
glyphs_.clear();
// Create a hb_buffer.
hb_buffer_t *buf = hb_buffer_create();
auto buf_guard = scopeGuard([&buf]{ hb_buffer_destroy(buf); });
// Put text in.
hb_buffer_add_utf8(buf, text_.c_str(), -1, 0, -1);
// Set the script, language and direction of the buffer.
hb_buffer_set_direction(buf, direction_);
hb_buffer_set_script(buf, script_);
hb_buffer_set_language(buf, language_);
// Shape
hb_shape(font_.getHBFont(), buf, NULL, 0);
// Get the glyph and position information.
unsigned int glyph_count;
hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(buf, &glyph_count);
hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(buf, &glyph_count);
// Iterate over each glyph.
for (unsigned int i = 0; i < glyph_count; i++)
{
hb_codepoint_t glyphid = glyph_info[i].codepoint;
hb_position_t x_offset = glyph_pos[i].x_offset / 64;
hb_position_t y_offset = glyph_pos[i].y_offset / 64;
hb_position_t x_advance = glyph_pos[i].x_advance / 64;
hb_position_t y_advance = glyph_pos[i].y_advance / 64;
glyphs_.push_back(GlyphInfo { glyphid, x_offset, y_offset, x_advance, y_advance });
}
dirty_ = false;
}