Cancelation in the middle of getting text shape

This commit is contained in:
Filip Sykala - NTB T15p 2022-09-23 14:07:17 +02:00
parent 00db58c823
commit 072c1752b1
4 changed files with 14 additions and 8 deletions

View File

@ -652,8 +652,9 @@ std::optional<Emboss::Glyph> Emboss::letter2glyph(const FontFile &font,
}
ExPolygons Emboss::text2shapes(FontFileWithCache &font_with_cache,
const char * text,
const FontProp &font_prop)
const char *text,
const FontProp &font_prop,
std::function<bool()> was_canceled)
{
assert(font_with_cache.has_value());
std::optional<stbtt_fontinfo> font_info_opt;
@ -688,7 +689,11 @@ ExPolygons Emboss::text2shapes(FontFileWithCache &font_with_cache,
if (wc == '\r') continue;
int unicode = static_cast<int>(wc);
const Glyph* glyph_ptr = Private::get_glyph(unicode, font, font_prop, cache, font_info_opt);
// check cancelation only before unknown symbol - loading of symbol could be timeconsuming on slow computer and dificult fonts
auto it = cache.find(unicode);
if (it == cache.end() && was_canceled != nullptr && was_canceled()) return {};
const Glyph *glyph_ptr = (it != cache.end())? &it->second :
Private::get_glyph(unicode, font, font_prop, cache, font_info_opt);
if (glyph_ptr == nullptr) continue;
// move glyph to cursor position

View File

@ -152,8 +152,9 @@ public:
/// <param name="font">Define fonts + cache, which could extend</param>
/// <param name="text">Characters to convert</param>
/// <param name="font_prop">User defined property of the font</param>
/// <param name="was_canceled">Way to interupt processing</param>
/// <returns>Inner polygon cw(outer ccw)</returns>
static ExPolygons text2shapes(FontFileWithCache &font, const char *text, const FontProp &font_prop);
static ExPolygons text2shapes(FontFileWithCache &font, const char *text, const FontProp &font_prop, std::function<bool()> was_canceled = nullptr);
/// <summary>
/// Fix intersections and self intersections in polygons glyph shape

View File

@ -45,8 +45,8 @@ void CreateFontImageJob::process(Ctl &ctl)
text = text.substr(0, enter_pos);
}
ExPolygons shapes = Emboss::text2shapes(font_file_with_cache,
text.c_str(), fp);
std::function<bool()> was_canceled = [&ctl]() -> bool { return ctl.was_canceled(); };
ExPolygons shapes = Emboss::text2shapes(font_file_with_cache, text.c_str(), fp, was_canceled);
// normalize height of font
BoundingBox bounding_box;
for (ExPolygon &shape : shapes)

View File

@ -521,7 +521,7 @@ TriangleMesh priv::try_create_mesh(const EmbossDataBase &input, Emboss::FontFile
assert(font.has_value());
if (!font.has_value()) return {};
ExPolygons shapes = Emboss::text2shapes(font, text, prop);
ExPolygons shapes = Emboss::text2shapes(font, text, prop, was_canceled);
if (shapes.empty()) return {};
if (was_canceled()) return {};