Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lang is a string, not a char array. The size are not fixed, and not w… #12

Open
wants to merge 5 commits into
base: fix-set-correct-shapes-size
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/alfons/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ Font::Font(const Properties& properties)

auto Font::addFace(std::shared_ptr<FontFace> _face, hb_language_t _lang) -> bool {

if (!_face) { return false; }

if (_lang == HB_LANGUAGE_INVALID) {
m_faces.push_back(_face);
return true;
}

for (auto& face : m_fontFaceMap[_lang]) {
if (face == _face) {
log("Won't add font twice. %s", _lang);
return false;
}
if (face == _face) { return false; }
}

m_fontFaceMap[_lang].push_back(_face);
Expand Down
4 changes: 4 additions & 0 deletions src/alfons/fontFace.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ class FontFace {
return m_languages;
}

Descriptor& descriptor() {
return m_descriptor;
}

protected:

FreetypeHelper& m_ft;
Expand Down
14 changes: 5 additions & 9 deletions src/alfons/fontManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ void FontManager::unload(Font& font) {
}

void FontManager::unload() {
// layoutCache.clear();

for (auto& face : m_faces) {
face->unload();
Expand All @@ -170,14 +169,11 @@ void FontManager::unload() {
std::shared_ptr<FontFace> FontManager::addFontFace(const FontFace::Descriptor& descriptor,
float baseSize) {

// FontFace::Key key(descriptor, baseSize);
// for (const auto& face : faces) {
// if (face.)
// }
// auto it = faces.find(key);
// if (it != faces.end()) {
// return it->second.faceId;
// }
if (m_maxFontId == std::numeric_limits<uint16_t>::max()) {
LOGE("addFontFace failed: Reached maximum FontFace ID");
return nullptr;
}

auto face = std::make_shared<FontFace>(m_ftHelper, m_maxFontId++, descriptor, baseSize);

m_faces.push_back(face);
Expand Down
4 changes: 4 additions & 0 deletions src/alfons/inputSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class InputSource {

bool hasSourceCallback() { return m_data && bool(m_data->loadSource); }

void clearData() {
m_data->buffer.clear();
}

bool resolveSource() {
if (!m_data || !bool(m_data->loadSource)) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/alfons/langHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace alfons {
const std::string DEFAULT_LANGUAGES = "en:zh-cn"; // GIVING PRIORITY (BY DEFAULT) TO CHINESE OVER JAPANESE

struct HBScriptForLang {
const char lang[7];
const char *lang;
hb_script_t scripts[3];
};

Expand Down
4 changes: 2 additions & 2 deletions src/alfons/lineLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ enum class Alignment {

struct Shape {
// Reference to face within a Font.
uint8_t face;
uint16_t face;
union {
uint8_t flags;
struct {
Expand All @@ -59,7 +59,7 @@ struct Shape {

Shape() : face(0), flags(0), advance(0), codepoint(0) {}

Shape(uint8_t faceID, uint32_t codepoint, glm::vec2 offset,
Shape(uint16_t faceID, uint32_t codepoint, glm::vec2 offset,
float advance, uint8_t flags)
: face(faceID),
flags(flags),
Expand Down
36 changes: 24 additions & 12 deletions src/alfons/textShaper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ struct TextLine {

// Input
UnicodeString* text;
size_t offset;
hb_language_t langHint;
hb_direction_t overallDirection;

Expand All @@ -72,14 +73,15 @@ struct TextLine {
// Result
std::vector<TextRun> runs;

void set(UnicodeString& _input,
void set(UnicodeString& _input, size_t _offset,
hb_language_t _langHint = HB_LANGUAGE_INVALID,
hb_direction_t _overallDirection = HB_DIRECTION_INVALID) {
runs.clear();
directionItems.clear();
scriptLangItems.clear();

text = &_input;
offset = _offset;
langHint = _langHint;
overallDirection = _overallDirection;
}
Expand Down Expand Up @@ -293,7 +295,7 @@ TextShaper::~TextShaper() {
}

bool TextShaper::processRun(const FontFace& _face, const TextRun& _run,
FontFace::Metrics& _lineMetrics) {
size_t lineBreakOffset, FontFace::Metrics& _lineMetrics) {

hb_shape(_face.hbFont(), m_hbBuffer, NULL, 0);

Expand All @@ -317,7 +319,7 @@ bool TextShaper::processRun(const FontFace& _face, const TextRun& _run,
}

if (codepoint == 0) {
if (m_linebreaks[clusterId] != LINEBREAK_MUSTBREAK) {
if (!m_glyphAdded[id] && m_linebreaks[lineBreakOffset + clusterId] != LINEBREAK_MUSTBREAK) {
missingGlyphs = true;
}
continue;
Expand Down Expand Up @@ -345,12 +347,12 @@ bool TextShaper::processRun(const FontFace& _face, const TextRun& _run,
addedGlyphs = true;
m_glyphAdded[id] = 1;

uint8_t breakmode = m_linebreaks[clusterId];
uint8_t breakmode = m_linebreaks[lineBreakOffset + clusterId];

uint8_t flags = 1 | // cluster start
//((breakmode == LINEBREAK_MUSTBREAK) ? 2 : 0) | // must break
((breakmode == 1) ? 4 : 0) | // can break
((breakmode == 2) ? 8 : 0) | // no break
uint8_t flags = 1 | // cluster start
((breakmode == LINEBREAK_MUSTBREAK) ? 2 : 0) |
((breakmode == LINEBREAK_ALLOWBREAK) ? 4 : 0) |
((breakmode == LINEBREAK_NOBREAK) ? 8 : 0) |
(_face.isSpace(codepoint) ? 16 : 0);

m_shapes[id] = Shape(_face.id(), codepoint, offset, advance, flags);
Expand Down Expand Up @@ -411,7 +413,7 @@ bool TextShaper::shape(std::shared_ptr<Font>& _font, const TextLine& _line,
hb_buffer_set_language(m_hbBuffer, run.language);
}

if (processRun(*face, run, _layout.metrics())) {
if (processRun(*face, run, _line.offset, _layout.metrics())) {
missingGlyphs = false;
break;
}
Expand All @@ -436,6 +438,9 @@ bool TextShaper::shape(std::shared_ptr<Font>& _font, const TextLine& _line,

if (shapes.empty()) { return false; }

// Last char on line: Must break at end of line
shapes.back().mustBreak = true;

_layout.addShapes(shapes);

return true;
Expand Down Expand Up @@ -468,16 +473,23 @@ LineLayout TextShaper::shapeICU(std::shared_ptr<Font>& _font, const UnicodeStrin
int start = 0;

for (int pos = 0; pos < numChars; pos++) {
if (m_linebreaks[pos] != 0) { continue; }
// Last char is always MUSTBREAK
if (m_linebreaks[pos] != LINEBREAK_MUSTBREAK) {
continue;
}

// Remove linebreak after final char, this interferes with RTL text.
if (pos == numChars - 1) {
m_linebreaks[pos] = LINEBREAK_NOBREAK;
}

auto cur = _text.tempSubStringBetween(start, pos+1);

line.set(cur, _langHint, _direction);
line.set(cur, start, _langHint, _direction);
m_itemizer->processLine(line);

shape(_font, line, line.runs, layout);
start = pos + 1;

}

return layout;
Expand Down
2 changes: 1 addition & 1 deletion src/alfons/textShaper.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class TextShaper {

bool shape(std::shared_ptr<Font>& font, TextLine& line, LineLayout& layout);

bool processRun(const FontFace& face, const TextRun& run, FontFace::Metrics& _lineMetrics);
bool processRun(const FontFace& face, const TextRun& run, size_t lineBreakOffset, FontFace::Metrics& _lineMetrics);

LangHelper m_langHelper;
std::unique_ptr<TextItemizer> m_itemizer;
Expand Down