From c5b4a406af4c71c2a87af20f33419bdd164c512f Mon Sep 17 00:00:00 2001 From: Christian Parpart Date: Sun, 18 Sep 2022 22:24:27 +0200 Subject: [PATCH] [text_shaper] open_shaper: Some code refactorizations. --- src/text_shaper/open_shaper.cpp | 39 +++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/text_shaper/open_shaper.cpp b/src/text_shaper/open_shaper.cpp index b3a43f1304..0c72fa88fd 100644 --- a/src/text_shaper/open_shaper.cpp +++ b/src/text_shaper/open_shaper.cpp @@ -486,10 +486,8 @@ struct open_shaper::Private // {{{ if (auto const ec = FT_Init_FreeType(&ft_); ec != FT_Err_Ok) throw runtime_error { "freetype: Failed to initialize. "s + ftErrorStr(ec) }; -#if defined(FT_LCD_FILTER_DEFAULT) if (auto const ec = FT_Library_SetLcdFilter(ft_, FT_LCD_FILTER_DEFAULT); ec != FT_Err_Ok) errorlog()("freetype: Failed to set LCD filter. {}", ftErrorStr(ec)); -#endif } bool tryShapeWithFallback(font_key _font, @@ -747,10 +745,13 @@ optional open_shaper::rasterize(glyph_key _glyph, render_mode if (!FT_HAS_COLOR(ftFace)) { if (FT_Render_Glyph(ftFace->glyph, ftRenderMode(_mode)) != FT_Err_Ok) + { + RasterizerLog()("Failed to rasterize glyph {}.", _glyph); return nullopt; + } } - rasterized_glyph output {}; + auto output = rasterized_glyph {}; output.bitmapSize.width = crispy::Width::cast_from(ftFace->glyph->bitmap.width); output.bitmapSize.height = crispy::Height::cast_from(ftFace->glyph->bitmap.rows); output.position.x = ftFace->glyph->bitmap_left; @@ -798,18 +799,34 @@ optional open_shaper::rasterize(glyph_key _glyph, render_mode break; } case FT_PIXEL_MODE_LCD: { - auto const width = static_cast(ftFace->glyph->bitmap.width); - auto const height = static_cast(ftFace->glyph->bitmap.rows); + auto const& ftBitmap = ftFace->glyph->bitmap; + // RasterizerLog()("Rasterizing using pixel mode: {}, rows={}, width={}, pitch={}, mode={}", + // "lcd", + // ftBitmap.rows, + // ftBitmap.width, + // ftBitmap.pitch, + // ftBitmap.pixel_mode); output.format = bitmap_format::rgb; // LCD - output.bitmap.resize(width * height); + output.bitmap.resize(static_cast(ftBitmap.width) * static_cast(ftBitmap.rows)); output.bitmapSize.width /= crispy::Width(3); - auto const pitch = static_cast(ftFace->glyph->bitmap.pitch); - auto const s = ftFace->glyph->bitmap.buffer; - for (auto const i: iota(0u, ftFace->glyph->bitmap.rows)) - for (auto const j: iota(0u, ftFace->glyph->bitmap.width)) - output.bitmap[i * width + j] = s[i * pitch + j]; + auto s = ftBitmap.buffer; + auto t = output.bitmap.data(); + if (ftBitmap.width == static_cast(std::abs(ftBitmap.pitch))) + { + std::copy_n(s, ftBitmap.width * ftBitmap.rows, t); + } + else + { + for (auto const _: iota(0u, ftBitmap.rows)) + { + crispy::ignore_unused(_); + std::copy_n(s, ftBitmap.width, t); + s += ftBitmap.pitch; + t += ftBitmap.width; + } + } break; } case FT_PIXEL_MODE_BGRA: {