ImFontConfig: Added RasterizerMultiply option to alter the brightness of individual fonts at rasterization time, which may help increasing readability for some. Added RasterizerFlags for custom rasterizer flags (aka imgui_freetype)

This commit is contained in:
omar 2017-08-26 14:11:56 +08:00
parent 304de0ee1b
commit f72ca6d22c
3 changed files with 33 additions and 0 deletions

View File

@ -1330,6 +1330,8 @@ struct ImFontConfig
ImVec2 GlyphOffset; // 0, 0 // Offset all glyphs from this font input. ImVec2 GlyphOffset; // 0, 0 // Offset all glyphs from this font input.
const ImWchar* GlyphRanges; // NULL // Pointer to a user-provided list of Unicode range (2 value per range, values are inclusive, zero-terminated list). THE ARRAY DATA NEEDS TO PERSIST AS LONG AS THE FONT IS ALIVE. const ImWchar* GlyphRanges; // NULL // Pointer to a user-provided list of Unicode range (2 value per range, values are inclusive, zero-terminated list). THE ARRAY DATA NEEDS TO PERSIST AS LONG AS THE FONT IS ALIVE.
bool MergeMode; // false // Merge into previous ImFont, so you can combine multiple inputs font into one ImFont (e.g. ASCII font + icons + Japanese glyphs). You may want to use GlyphOffset.y when merge font of different heights. bool MergeMode; // false // Merge into previous ImFont, so you can combine multiple inputs font into one ImFont (e.g. ASCII font + icons + Japanese glyphs). You may want to use GlyphOffset.y when merge font of different heights.
unsigned int RasterizerFlags; // 0x00 // Settings for custom font rasterizer (e.g. ImGuiFreeType). Leave as zero if you aren't using one.
float RasterizerMultiply; // 1.0f // Brighten (>1.0f) or darken (<1.0f) font output. Brightening small fonts may be a good workaround to make them more readable.
// [Internal] // [Internal]
char Name[32]; // Name (strictly to ease debugging) char Name[32]; // Name (strictly to ease debugging)

View File

@ -1051,6 +1051,8 @@ ImFontConfig::ImFontConfig()
GlyphOffset = ImVec2(0.0f, 0.0f); GlyphOffset = ImVec2(0.0f, 0.0f);
GlyphRanges = NULL; GlyphRanges = NULL;
MergeMode = false; MergeMode = false;
RasterizerFlags = 0x00;
RasterizerMultiply = 1.0f;
memset(Name, 0, sizeof(Name)); memset(Name, 0, sizeof(Name));
DstFont = NULL; DstFont = NULL;
} }
@ -1340,6 +1342,23 @@ bool ImFontAtlas::Build()
return ImFontAtlasBuildWithStbTruetype(this); return ImFontAtlasBuildWithStbTruetype(this);
} }
void ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], float in_brighten_factor)
{
for (unsigned int i = 0; i < 256; i++)
{
unsigned int value = (unsigned int)(i * in_brighten_factor);
out_table[i] = value > 255 ? 255 : (value & 0xFF);
}
}
void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride)
{
unsigned char* data = pixels + x + y * stride;
for (int j = h; j > 0; j--, data += stride)
for (int i = 0; i < w; i++)
data[i] = table[data[i]];
}
bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas)
{ {
IM_ASSERT(atlas->ConfigData.Size > 0); IM_ASSERT(atlas->ConfigData.Size > 0);
@ -1382,6 +1401,7 @@ bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas)
{ {
stbtt_fontinfo FontInfo; stbtt_fontinfo FontInfo;
stbrp_rect* Rects; stbrp_rect* Rects;
int RectsCount;
stbtt_pack_range* Ranges; stbtt_pack_range* Ranges;
int RangesCount; int RangesCount;
}; };
@ -1434,6 +1454,7 @@ bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas)
// Pack // Pack
tmp.Rects = buf_rects + buf_rects_n; tmp.Rects = buf_rects + buf_rects_n;
tmp.RectsCount = font_glyphs_count;
buf_rects_n += font_glyphs_count; buf_rects_n += font_glyphs_count;
stbtt_PackSetOversampling(&spc, cfg.OversampleH, cfg.OversampleV); stbtt_PackSetOversampling(&spc, cfg.OversampleH, cfg.OversampleV);
int n = stbtt_PackFontRangesGatherRects(&spc, &tmp.FontInfo, tmp.Ranges, tmp.RangesCount, tmp.Rects); int n = stbtt_PackFontRangesGatherRects(&spc, &tmp.FontInfo, tmp.Ranges, tmp.RangesCount, tmp.Rects);
@ -1463,6 +1484,14 @@ bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas)
ImFontTempBuildData& tmp = tmp_array[input_i]; ImFontTempBuildData& tmp = tmp_array[input_i];
stbtt_PackSetOversampling(&spc, cfg.OversampleH, cfg.OversampleV); stbtt_PackSetOversampling(&spc, cfg.OversampleH, cfg.OversampleV);
stbtt_PackFontRangesRenderIntoRects(&spc, &tmp.FontInfo, tmp.Ranges, tmp.RangesCount, tmp.Rects); stbtt_PackFontRangesRenderIntoRects(&spc, &tmp.FontInfo, tmp.Ranges, tmp.RangesCount, tmp.Rects);
if (cfg.RasterizerMultiply != 1.0f)
{
unsigned char multiply_table[256];
ImFontAtlasBuildMultiplyCalcLookupTable(multiply_table, cfg.RasterizerMultiply);
for (const stbrp_rect* r = tmp.Rects; r != tmp.Rects + tmp.RectsCount; r++)
if (r->was_packed)
ImFontAtlasBuildMultiplyRectAlpha8(multiply_table, spc.pixels, r->x, r->y, r->w, r->h, spc.stride_in_bytes);
}
tmp.Rects = NULL; tmp.Rects = NULL;
} }

View File

@ -816,6 +816,8 @@ IMGUI_API void ImFontAtlasBuildRegisterDefaultCustomRects(ImFontAtl
IMGUI_API void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent); IMGUI_API void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent);
IMGUI_API void ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* spc); IMGUI_API void ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* spc);
IMGUI_API void ImFontAtlasBuildRenderDefaultTexData(ImFontAtlas* atlas); IMGUI_API void ImFontAtlasBuildRenderDefaultTexData(ImFontAtlas* atlas);
IMGUI_API void ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], float in_multiply_factor);
IMGUI_API void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride);
#ifdef __clang__ #ifdef __clang__
#pragma clang diagnostic pop #pragma clang diagnostic pop