Skip to content
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
5 changes: 2 additions & 3 deletions src/graphics/stateless_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ void text_render(
pixel_x_off = trunc_pixel_x_off + 1.0f;
}

font_instance.make_glyph(uint16_t(glyphid), subpixel);
auto& gso = font_instance.get_glyph(uint16_t(glyphid), subpixel);
auto gso = font_instance.insert_or_find_glyph(uint16_t(glyphid), subpixel).get();
float x_advance = float(glyph_info[i].x_advance) / text::fixed_to_fp;

if(gso.width != 0) {
if (gso.width != 0) {
float x_offset = pixel_x_off + float(gso.bitmap_left);
float y_offset = float(-gso.bitmap_top) - float(glyph_info[i].y_offset) / text::fixed_to_fp;

Expand Down
2 changes: 1 addition & 1 deletion src/launcher/launcher_main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ void internal_text_render(std::string_view str, color_modification enabled, floa
std::vector<text::stored_glyph> glyphs;

for(unsigned int i = 0; i < glyph_count; i++) {
font_instance.make_glyph((uint16_t)glyph_info[i].codepoint, 0);
font_instance.insert_or_find_glyph((uint16_t)glyph_info[i].codepoint, 0);
glyphs.emplace_back(glyph_info[i], glyph_pos[i]);
}

Expand Down
30 changes: 15 additions & 15 deletions src/text/fonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ void font_at_size::create(FT_Library lib, FT_Byte* file_data, size_t file_size,
void font_manager::load_font(font& fnt, char const* file_data, uint32_t fz) {
fnt.file_data = std::unique_ptr<FT_Byte[]>(new FT_Byte[fz]);
fnt.file_size = fz;
memcpy(fnt.file_data.get(), file_data, fz);
std::memcpy(fnt.file_data.get(), file_data, fz);
}

float font_at_size::line_height(sys::state& state) const {
Expand All @@ -561,12 +561,9 @@ bool font::can_display(char32_t ch_in) const {
return FT_Get_Char_Index(sized_fonts.begin()->second.font_face, ch_in) != 0;
}

glyph_sub_offset& font_at_size:: get_glyph(uint16_t glyph_in, int32_t subpixel) {
return glyph_positions[(uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3)];
}
void font_at_size::make_glyph(uint16_t glyph_in, int32_t subpixel) {
if(glyph_positions.find((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3)) != glyph_positions.end())
return;
std::reference_wrapper<glyph_sub_offset> font_at_size::insert_or_find_glyph(uint16_t glyph_in, int32_t subpixel) {
if(auto const it = glyph_positions.find((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3)); it != glyph_positions.end())
return it->second;

// load all glyph metrics
if(glyph_in) {
Expand All @@ -586,17 +583,17 @@ void font_at_size::make_glyph(uint16_t glyph_in, int32_t subpixel) {
FT_Glyph g_result;
auto err = FT_Get_Glyph(font_face->glyph, &g_result);
if(err != 0) {
glyph_positions.insert_or_assign((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3), gso);
return;
auto const it = glyph_positions.insert_or_assign((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3), gso);
return it.first->second;
}

FT_Bitmap const& bitmap = ((FT_BitmapGlyphRec*)g_result)->bitmap;

assert(bitmap.rows <= 1024 && bitmap.width <= 1024);
if(bitmap.rows > 1024 || bitmap.width > 1024) { // too large to render
FT_Done_Glyph(g_result);
glyph_positions.insert_or_assign((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3), gso);
return;
auto const it = glyph_positions.insert_or_assign((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3), gso);
return it.first->second;
}
if(bitmap.width + internal_tx_line_xpos >= 1024) { // new line
internal_tx_line_xpos = 0;
Expand Down Expand Up @@ -648,8 +645,11 @@ void font_at_size::make_glyph(uint16_t glyph_in, int32_t subpixel) {
delete[] temp;
}
FT_Done_Glyph(g_result);
glyph_positions.insert_or_assign((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3), gso);

auto const it = glyph_positions.insert_or_assign((uint32_t(glyph_in) << 2) | uint32_t(subpixel & 3), gso);
return it.first->second;
}
std::abort(); //<-- should NOT call here ffs
}

stored_glyphs::stored_glyphs(sys::state& state, int32_t size, font_selection type, std::span<uint16_t> s, uint32_t details_offset, layout_details* d, uint16_t font_handle) {
Expand Down Expand Up @@ -947,7 +947,7 @@ void font_at_size::remake_cache(sys::state& state, font_selection type, stored_g

for(unsigned int j = 0; j < gcount; j++) { // Preload glyphs
total_x_advance += glyph_pos[j].x_advance / (text::fixed_to_fp * state.user_settings.ui_scale);
//make_glyph(uint16_t(glyph_info[j].codepoint));
//insert_or_find_glyph(uint16_t(glyph_info[j].codepoint));
txt.glyph_info.emplace_back(glyph_info[j], glyph_pos[j]);
}
}
Expand Down Expand Up @@ -1007,7 +1007,7 @@ void font_at_size::remake_bidiless_cache(sys::state& state, font_selection type,
hb_glyph_position_t* glyph_pos = hb_buffer_get_glyph_positions(hb_buf, &gcount);

for(unsigned int j = 0; j < gcount; j++) { // Preload glyphs
//make_glyph(uint16_t(glyph_info[j].codepoint));
//insert_or_find_glyph(uint16_t(glyph_info[j].codepoint));
txt.glyph_info.emplace_back(glyph_info[j], glyph_pos[j]);
}

Expand Down Expand Up @@ -1150,7 +1150,7 @@ float font_at_size::stateless_text_extent(float ui_scale, char const* codepoints
hb_glyph_position_t* glyph_pos = hb_buffer_get_glyph_positions(hb_buf, &glyph_count);
float x = 0.0f;
for(unsigned int i = 0; i < glyph_count; i++) {
make_glyph((uint16_t)glyph_info[i].codepoint, 0);
insert_or_find_glyph((uint16_t)glyph_info[i].codepoint, 0);
hb_codepoint_t glyphid = glyph_info[i].codepoint;
auto& gso = glyph_positions[glyphid << 2];
float x_advance = float(glyph_pos[i].x_advance) / text::fixed_to_fp;
Expand Down
3 changes: 1 addition & 2 deletions src/text/fonts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@ class font_at_size {

std::vector<uint32_t> textures;

void make_glyph(uint16_t glyph_in, int32_t subpixel);
glyph_sub_offset& get_glyph(uint16_t glyph_in, int32_t subpixel);
std::reference_wrapper<glyph_sub_offset> insert_or_find_glyph(uint16_t glyph_in, int32_t subpixel);
void reset();
void create(FT_Library lib, FT_Byte* file_data, size_t file_size, int32_t real_size);
void remake_cache(sys::state& state, font_selection type, stored_glyphs& txt, std::span<uint16_t> source, uint32_t details_offset = 0, layout_details* d = nullptr, uint16_t font_handle = 0);
Expand Down
12 changes: 4 additions & 8 deletions src/text/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1484,15 +1484,13 @@ void add_to_layout_box(sys::state& state, layout_base& dest, layout_box& box, st
auto glyphid = FT_Get_Char_Index(font_inst.font_face, 0x2026);

bool ellipsis_valid = true;
font_inst.make_glyph(uint16_t(glyphid), 0);
auto& gso = font_inst.get_glyph(uint16_t(glyphid), 0);
auto& gso = font_inst.insert_or_find_glyph(uint16_t(glyphid), 0).get();
auto width_of_ellipsis = float(gso.bitmap_left + gso.width) / state.user_settings.ui_scale;

if(width_of_ellipsis <= 0 || glyphid == 0) {
ellipsis_valid = false;
auto dot_g = FT_Get_Char_Index(font_inst.font_face, '.');
font_inst.make_glyph(uint16_t(dot_g), 0);
auto& gso2 = font_inst.get_glyph(uint16_t(dot_g), 0);
auto& gso2 = font_inst.insert_or_find_glyph(uint16_t(dot_g), 0).get();
width_of_ellipsis = float(gso2.bitmap_left + gso2.width) * 3.0f / state.user_settings.ui_scale;
}
if(state.user_settings.use_classic_fonts) {
Expand Down Expand Up @@ -1612,15 +1610,13 @@ void add_to_layout_box(sys::state& state, layout_base& dest, layout_box& box, st
auto glyphid = FT_Get_Char_Index(font_inst.font_face, 0x2026);

bool ellipsis_valid = true;
font_inst.make_glyph(uint16_t(glyphid), 0);
auto& gso = font_inst.get_glyph(uint16_t(glyphid), 0);
auto& gso = font_inst.insert_or_find_glyph(uint16_t(glyphid), 0).get();
auto width_of_ellipsis = float(gso.bitmap_left + gso.width) / state.user_settings.ui_scale;

if(width_of_ellipsis <= 0 || glyphid == 0) {
ellipsis_valid = false;
auto dot_g = FT_Get_Char_Index(font_inst.font_face, '.');
font_inst.make_glyph(uint16_t(dot_g), 0);
auto& gso2 = font_inst.get_glyph(uint16_t(dot_g), 0);
auto& gso2 = font_inst.insert_or_find_glyph(uint16_t(dot_g), 0).get();
width_of_ellipsis = float(gso2.bitmap_left + gso2.width) * 3.0f / state.user_settings.ui_scale;
}
if(state.user_settings.use_classic_fonts) {
Expand Down
Loading