Skip to content

Commit 3c9349d

Browse files
songhahaha66da-liii
authored andcommitted
!456 [206_3] 重构emoji缓存机制
1 parent 980aa0e commit 3c9349d

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

devel/206_3.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 206_3: 优化emoji渲染时缓存机制
2+
3+
## 2025/07/27
4+
5+
### What
6+
7+
重构缓存机制:
8+
9+
1. 添加了glyphID -> picture的缓存
10+
11+
### Why
12+
13+
当前Mogan输入emoji有卡顿现象
14+
15+
### How
16+
17+
修改了
18+
19+
1. qtrender.cpp

src/Plugins/Qt/qt_renderer.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ CONCRETE_NULL_CODE (qt_pixmap);
8484
static hashmap<basic_character, qt_image> character_image;
8585
// image cache
8686
static hashmap<string, qt_pixmap> images;
87+
// emoji cache: glyphID -> (size -> picture)
88+
static hashmap<int, hashmap<int, picture>> emoji_cache;
8789

8890
/*
8991
** hash contents must be removed because
@@ -94,6 +96,7 @@ void
9496
del_obj_qt_renderer (void) {
9597
character_image= hashmap<basic_character, qt_image> ();
9698
images = hashmap<string, qt_pixmap> ();
99+
emoji_cache = hashmap<int, hashmap<int, picture>> ();
97100
}
98101

99102
/******************************************************************************
@@ -698,20 +701,35 @@ qt_renderer_rep::draw_emoji (int char_code, font_glyphs fn, SI x, SI y) {
698701
return;
699702
}
700703

701-
// Retrieve SVG data for the glyph
702-
string svg_data= tt_font->face->svg_table->get_svg_from_glyphid (glyph_id);
703-
if (N (svg_data) == 0) {
704-
return;
705-
}
706-
707704
// Calculate emoji size
708705
SI xo, yo;
709706
glyph pre_gl= fn->get (char_code);
710707
glyph gl = shrink (pre_gl, std_shrinkf, std_shrinkf, xo, yo);
711708
int w= gl->width, h= gl->height;
712709

713-
// Create picture from SVG data
714-
picture emoji_picture= svg_string_to_picture (svg_data, w, h);
710+
// check cache first
711+
picture emoji_picture;
712+
if (emoji_cache->contains (glyph_id)) {
713+
hashmap<int, picture> size_cache= emoji_cache[glyph_id];
714+
if (size_cache->contains (w)) {
715+
emoji_picture= size_cache[w];
716+
}
717+
}
718+
719+
// cache miss: get SVG data and create picture
720+
if (is_nil (emoji_picture)) {
721+
string svg_data= tt_font->face->svg_table->get_svg_from_glyphid (glyph_id);
722+
if (N (svg_data) == 0) {
723+
return;
724+
}
725+
emoji_picture= svg_string_to_picture (svg_data, w, h);
726+
if (!is_nil (emoji_picture)) {
727+
if (!emoji_cache->contains (glyph_id)) {
728+
emoji_cache (glyph_id)= hashmap<int, picture> ();
729+
}
730+
emoji_cache[glyph_id](w)= emoji_picture;
731+
}
732+
}
715733
if (is_nil (emoji_picture)) {
716734
return;
717735
}

0 commit comments

Comments
 (0)