Merge pull request #2795 from Codex-/obs_text_antialias_option
obs-text, text-freetype2: Add Enable Antialiasing optionmaster
commit
4929de408d
|
@ -36,3 +36,4 @@ Transform.None="None"
|
|||
Transform.Uppercase="Uppercase"
|
||||
Transform.Lowercase="Lowercase"
|
||||
Transform.Startcase="Start Case"
|
||||
Antialiasing="Enable Antialiasing"
|
||||
|
|
|
@ -69,6 +69,7 @@ using namespace Gdiplus;
|
|||
#define S_EXTENTS_CX "extents_cx"
|
||||
#define S_EXTENTS_CY "extents_cy"
|
||||
#define S_TRANSFORM "transform"
|
||||
#define S_ANTIALIASING "antialiasing"
|
||||
|
||||
#define S_ALIGN_LEFT "left"
|
||||
#define S_ALIGN_CENTER "center"
|
||||
|
@ -83,6 +84,9 @@ using namespace Gdiplus;
|
|||
#define S_TRANSFORM_LOWERCASE 2
|
||||
#define S_TRANSFORM_STARTCASE 3
|
||||
|
||||
#define S_ANTIALIASING_NONE 0
|
||||
#define S_ANTIALIASING_STANDARD 1
|
||||
|
||||
#define T_(v) obs_module_text(v)
|
||||
#define T_FONT T_("Font")
|
||||
#define T_USE_FILE T_("ReadFromFile")
|
||||
|
@ -110,6 +114,7 @@ using namespace Gdiplus;
|
|||
#define T_EXTENTS_CX T_("Width")
|
||||
#define T_EXTENTS_CY T_("Height")
|
||||
#define T_TRANSFORM T_("Transform")
|
||||
#define T_ANTIALIASING T_("Antialiasing")
|
||||
|
||||
#define T_FILTER_TEXT_FILES T_("Filter.TextFiles")
|
||||
#define T_FILTER_ALL_FILES T_("Filter.AllFiles")
|
||||
|
@ -242,6 +247,7 @@ struct TextSource {
|
|||
bool italic = false;
|
||||
bool underline = false;
|
||||
bool strikeout = false;
|
||||
bool antialiasing = true;
|
||||
bool vertical = false;
|
||||
|
||||
bool use_outline = false;
|
||||
|
@ -288,6 +294,7 @@ struct TextSource {
|
|||
void RenderText();
|
||||
void LoadFileText();
|
||||
void TransformText();
|
||||
void SetAntiAliasing(Graphics &graphics_bitmap);
|
||||
|
||||
const char *GetMainString(const char *str);
|
||||
|
||||
|
@ -575,9 +582,8 @@ void TextSource::RenderText()
|
|||
warn_stat("graphics_bitmap.Clear");
|
||||
}
|
||||
|
||||
graphics_bitmap.SetTextRenderingHint(TextRenderingHintAntiAlias);
|
||||
graphics_bitmap.SetCompositingMode(CompositingModeSourceOver);
|
||||
graphics_bitmap.SetSmoothingMode(SmoothingModeAntiAlias);
|
||||
SetAntiAliasing(graphics_bitmap);
|
||||
|
||||
if (!text.empty()) {
|
||||
if (use_outline) {
|
||||
|
@ -684,6 +690,19 @@ void TextSource::TransformText()
|
|||
}
|
||||
}
|
||||
|
||||
void TextSource::SetAntiAliasing(Graphics &graphics_bitmap)
|
||||
{
|
||||
if (!antialiasing) {
|
||||
graphics_bitmap.SetTextRenderingHint(
|
||||
TextRenderingHintSingleBitPerPixel);
|
||||
graphics_bitmap.SetSmoothingMode(SmoothingModeNone);
|
||||
return;
|
||||
}
|
||||
|
||||
graphics_bitmap.SetTextRenderingHint(TextRenderingHintAntiAlias);
|
||||
graphics_bitmap.SetSmoothingMode(SmoothingModeAntiAlias);
|
||||
}
|
||||
|
||||
#define obs_data_get_uint32 (uint32_t) obs_data_get_int
|
||||
|
||||
inline void TextSource::Update(obs_data_t *s)
|
||||
|
@ -712,6 +731,7 @@ inline void TextSource::Update(obs_data_t *s)
|
|||
uint32_t n_extents_cx = obs_data_get_uint32(s, S_EXTENTS_CX);
|
||||
uint32_t n_extents_cy = obs_data_get_uint32(s, S_EXTENTS_CY);
|
||||
int new_text_transform = (int)obs_data_get_int(s, S_TRANSFORM);
|
||||
bool new_antialiasing = obs_data_get_bool(s, S_ANTIALIASING);
|
||||
|
||||
const char *font_face = obs_data_get_string(font_obj, "face");
|
||||
int font_size = (int)obs_data_get_int(font_obj, "size");
|
||||
|
@ -763,6 +783,7 @@ inline void TextSource::Update(obs_data_t *s)
|
|||
extents_cx = n_extents_cx;
|
||||
extents_cy = n_extents_cy;
|
||||
text_transform = new_text_transform;
|
||||
antialiasing = new_antialiasing;
|
||||
|
||||
if (!gradient) {
|
||||
color2 = color;
|
||||
|
@ -965,6 +986,8 @@ static obs_properties_t *get_properties(void *data)
|
|||
obs_properties_add_path(props, S_FILE, T_FILE, OBS_PATH_FILE,
|
||||
filter.c_str(), path.c_str());
|
||||
|
||||
obs_properties_add_bool(props, S_ANTIALIASING, T_ANTIALIASING);
|
||||
|
||||
p = obs_properties_add_list(props, S_TRANSFORM, T_TRANSFORM,
|
||||
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
|
||||
obs_property_list_add_int(p, T_TRANSFORM_NONE, S_TRANSFORM_NONE);
|
||||
|
@ -976,6 +999,7 @@ static obs_properties_t *get_properties(void *data)
|
|||
S_TRANSFORM_STARTCASE);
|
||||
|
||||
obs_properties_add_bool(props, S_VERTICAL, T_VERTICAL);
|
||||
|
||||
obs_properties_add_color(props, S_COLOR, T_COLOR);
|
||||
p = obs_properties_add_int_slider(props, S_OPACITY, T_OPACITY, 0, 100,
|
||||
1);
|
||||
|
@ -1059,6 +1083,7 @@ static void defaults(obs_data_t *settings, int ver)
|
|||
obs_data_set_default_int(settings, S_EXTENTS_CX, 100);
|
||||
obs_data_set_default_int(settings, S_EXTENTS_CY, 100);
|
||||
obs_data_set_default_int(settings, S_TRANSFORM, S_TRANSFORM_NONE);
|
||||
obs_data_set_default_bool(settings, S_ANTIALIASING, true);
|
||||
|
||||
obs_data_release(font_obj);
|
||||
};
|
||||
|
|
|
@ -12,3 +12,4 @@ DropShadow="Drop Shadow"
|
|||
ReadFromFile="Read from file"
|
||||
CustomWidth="Custom text width"
|
||||
WordWrap="Word Wrap"
|
||||
Antialiasing="Enable Antialiasing"
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/******************************************************************************
|
||||
/******************************************************************************
|
||||
Copyright (C) 2014 by Nibbles
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
|
@ -156,6 +156,9 @@ static obs_properties_t *ft2_source_properties(void *unused)
|
|||
obs_properties_add_bool(props, "from_file",
|
||||
obs_module_text("ReadFromFile"));
|
||||
|
||||
obs_properties_add_bool(props, "antialiasing",
|
||||
obs_module_text("Antialiasing"));
|
||||
|
||||
obs_properties_add_bool(props, "log_mode",
|
||||
obs_module_text("ChatLogMode"));
|
||||
|
||||
|
@ -365,6 +368,8 @@ static void ft2_source_update(void *data, obs_data_t *settings)
|
|||
if (ft2_lib == NULL)
|
||||
goto error;
|
||||
|
||||
const size_t texbuf_size = (size_t)texbuf_w * (size_t)texbuf_h;
|
||||
|
||||
if (srcdata->draw_effect == NULL) {
|
||||
char *effect_file = NULL;
|
||||
char *error_string = NULL;
|
||||
|
@ -386,6 +391,16 @@ static void ft2_source_update(void *data, obs_data_t *settings)
|
|||
if (srcdata->font_size != font_size || srcdata->from_file != from_file)
|
||||
vbuf_needs_update = true;
|
||||
|
||||
const bool new_aa_setting = obs_data_get_bool(settings, "antialiasing");
|
||||
const bool aa_changed = srcdata->antialiasing != new_aa_setting;
|
||||
if (aa_changed) {
|
||||
srcdata->antialiasing = new_aa_setting;
|
||||
if (srcdata->texbuf != NULL) {
|
||||
memset(srcdata->texbuf, 0, texbuf_size);
|
||||
}
|
||||
cache_standard_glyphs(srcdata);
|
||||
}
|
||||
|
||||
srcdata->file_load_failed = false;
|
||||
srcdata->from_file = from_file;
|
||||
|
||||
|
@ -422,7 +437,7 @@ static void ft2_source_update(void *data, obs_data_t *settings)
|
|||
bfree(srcdata->texbuf);
|
||||
srcdata->texbuf = NULL;
|
||||
}
|
||||
srcdata->texbuf = bzalloc(texbuf_w * texbuf_h);
|
||||
srcdata->texbuf = bzalloc(texbuf_size);
|
||||
|
||||
if (srcdata->font_face)
|
||||
cache_standard_glyphs(srcdata);
|
||||
|
@ -505,6 +520,8 @@ static void *ft2_source_create(obs_data_t *settings, obs_source_t *source,
|
|||
obs_data_set_default_int(font_obj, "size", font_size);
|
||||
obs_data_set_default_obj(settings, "font", font_obj);
|
||||
|
||||
obs_data_set_default_bool(settings, "antialiasing", true);
|
||||
|
||||
obs_data_set_default_int(settings, "log_lines", 6);
|
||||
|
||||
obs_data_set_default_int(settings, "color1", 0xFFFFFFFF);
|
||||
|
|
|
@ -37,6 +37,7 @@ struct ft2_source {
|
|||
|
||||
bool file_load_failed;
|
||||
bool from_file;
|
||||
bool antialiasing;
|
||||
char *text_file;
|
||||
wchar_t *text;
|
||||
time_t m_timestamp;
|
||||
|
|
|
@ -240,39 +240,110 @@ void cache_standard_glyphs(struct ft2_source *srcdata)
|
|||
L"!@#$%^&*()-_=+,<.>/?\\|[]{}`~ \'\"\0");
|
||||
}
|
||||
|
||||
#define glyph_pos x + (y * slot->bitmap.pitch)
|
||||
#define buf_pos (dx + x) + ((dy + y) * texbuf_w)
|
||||
FT_Render_Mode get_render_mode(struct ft2_source *srcdata)
|
||||
{
|
||||
return srcdata->antialiasing ? FT_RENDER_MODE_NORMAL
|
||||
: FT_RENDER_MODE_MONO;
|
||||
}
|
||||
|
||||
void load_glyph(struct ft2_source *srcdata, const FT_UInt glyph_index,
|
||||
const FT_Render_Mode render_mode)
|
||||
{
|
||||
const FT_Int32 load_mode = render_mode == FT_RENDER_MODE_MONO
|
||||
? FT_LOAD_TARGET_MONO
|
||||
: FT_LOAD_DEFAULT;
|
||||
FT_Load_Glyph(srcdata->font_face, glyph_index, load_mode);
|
||||
}
|
||||
|
||||
struct glyph_info *init_glyph(FT_GlyphSlot slot, const uint32_t dx,
|
||||
const uint32_t dy, const uint32_t g_w,
|
||||
const uint32_t g_h)
|
||||
{
|
||||
struct glyph_info *glyph = bzalloc(sizeof(struct glyph_info));
|
||||
glyph->u = (float)dx / (float)texbuf_w;
|
||||
glyph->u2 = (float)(dx + g_w) / (float)texbuf_w;
|
||||
glyph->v = (float)dy / (float)texbuf_h;
|
||||
glyph->v2 = (float)(dy + g_h) / (float)texbuf_h;
|
||||
glyph->w = g_w;
|
||||
glyph->h = g_h;
|
||||
glyph->yoff = slot->bitmap_top;
|
||||
glyph->xoff = slot->bitmap_left;
|
||||
glyph->xadv = slot->advance.x >> 6;
|
||||
|
||||
return glyph;
|
||||
}
|
||||
|
||||
uint8_t get_pixel_value(const unsigned char *buf_row,
|
||||
FT_Render_Mode render_mode, const uint32_t x)
|
||||
{
|
||||
if (render_mode == FT_RENDER_MODE_NORMAL) {
|
||||
return buf_row[x];
|
||||
}
|
||||
|
||||
const uint32_t byte_index = x / 8;
|
||||
const uint8_t bit_index = x % 8;
|
||||
const bool pixel_set = (buf_row[byte_index] >> (7 - bit_index)) & 1;
|
||||
return pixel_set ? 255 : 0;
|
||||
}
|
||||
|
||||
void rasterize(struct ft2_source *srcdata, FT_GlyphSlot slot,
|
||||
const FT_Render_Mode render_mode, const uint32_t dx,
|
||||
const uint32_t dy)
|
||||
{
|
||||
/**
|
||||
* The pitch's absolute value is the number of bytes taken by one bitmap
|
||||
* row, including padding.
|
||||
*
|
||||
* Source: https://www.freetype.org/freetype2/docs/reference/ft2-basic_types.html
|
||||
*/
|
||||
const int pitch = abs(slot->bitmap.pitch);
|
||||
|
||||
for (uint32_t y = 0; y < slot->bitmap.rows; y++) {
|
||||
const uint32_t row_start = y * pitch;
|
||||
const uint32_t row = (dy + y) * texbuf_w;
|
||||
|
||||
for (uint32_t x = 0; x < slot->bitmap.width; x++) {
|
||||
const uint32_t row_pixel_position = dx + x;
|
||||
const uint8_t pixel_value =
|
||||
get_pixel_value(&slot->bitmap.buffer[row_start],
|
||||
render_mode, x);
|
||||
srcdata->texbuf[row_pixel_position + row] = pixel_value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void cache_glyphs(struct ft2_source *srcdata, wchar_t *cache_glyphs)
|
||||
{
|
||||
FT_GlyphSlot slot;
|
||||
FT_UInt glyph_index = 0;
|
||||
|
||||
if (!srcdata->font_face || !cache_glyphs)
|
||||
return;
|
||||
|
||||
slot = srcdata->font_face->glyph;
|
||||
FT_GlyphSlot slot = srcdata->font_face->glyph;
|
||||
|
||||
uint32_t dx = srcdata->texbuf_x, dy = srcdata->texbuf_y;
|
||||
uint32_t dx = srcdata->texbuf_x;
|
||||
uint32_t dy = srcdata->texbuf_y;
|
||||
|
||||
int32_t cached_glyphs = 0;
|
||||
size_t len = wcslen(cache_glyphs);
|
||||
const size_t len = wcslen(cache_glyphs);
|
||||
|
||||
const FT_Render_Mode render_mode = get_render_mode(srcdata);
|
||||
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
glyph_index =
|
||||
const FT_UInt glyph_index =
|
||||
FT_Get_Char_Index(srcdata->font_face, cache_glyphs[i]);
|
||||
|
||||
if (src_glyph != NULL)
|
||||
goto skip_glyph;
|
||||
if (src_glyph != NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
FT_Load_Glyph(srcdata->font_face, glyph_index, FT_LOAD_DEFAULT);
|
||||
FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
|
||||
load_glyph(srcdata, glyph_index, render_mode);
|
||||
FT_Render_Glyph(slot, render_mode);
|
||||
|
||||
uint32_t g_w = slot->bitmap.width;
|
||||
uint32_t g_h = slot->bitmap.rows;
|
||||
const uint32_t g_w = slot->bitmap.width;
|
||||
const uint32_t g_h = slot->bitmap.rows;
|
||||
|
||||
if (srcdata->max_h < g_h)
|
||||
if (srcdata->max_h < g_h) {
|
||||
srcdata->max_h = g_h;
|
||||
}
|
||||
|
||||
if (dx + g_w >= texbuf_w) {
|
||||
dx = 0;
|
||||
|
@ -285,22 +356,8 @@ void cache_glyphs(struct ft2_source *srcdata, wchar_t *cache_glyphs)
|
|||
break;
|
||||
}
|
||||
|
||||
src_glyph = bzalloc(sizeof(struct glyph_info));
|
||||
src_glyph->u = (float)dx / (float)texbuf_w;
|
||||
src_glyph->u2 = (float)(dx + g_w) / (float)texbuf_w;
|
||||
src_glyph->v = (float)dy / (float)texbuf_h;
|
||||
src_glyph->v2 = (float)(dy + g_h) / (float)texbuf_h;
|
||||
src_glyph->w = g_w;
|
||||
src_glyph->h = g_h;
|
||||
src_glyph->yoff = slot->bitmap_top;
|
||||
src_glyph->xoff = slot->bitmap_left;
|
||||
src_glyph->xadv = slot->advance.x >> 6;
|
||||
|
||||
for (uint32_t y = 0; y < g_h; y++) {
|
||||
for (uint32_t x = 0; x < g_w; x++)
|
||||
srcdata->texbuf[buf_pos] =
|
||||
slot->bitmap.buffer[glyph_pos];
|
||||
}
|
||||
src_glyph = init_glyph(slot, dx, dy, g_w, g_h);
|
||||
rasterize(srcdata, slot, render_mode, dx, dy);
|
||||
|
||||
dx += (g_w + 1);
|
||||
if (dx >= texbuf_w) {
|
||||
|
@ -309,7 +366,6 @@ void cache_glyphs(struct ft2_source *srcdata, wchar_t *cache_glyphs)
|
|||
}
|
||||
|
||||
cached_glyphs++;
|
||||
skip_glyph:;
|
||||
}
|
||||
|
||||
srcdata->texbuf_x = dx;
|
||||
|
@ -320,8 +376,7 @@ void cache_glyphs(struct ft2_source *srcdata, wchar_t *cache_glyphs)
|
|||
obs_enter_graphics();
|
||||
|
||||
if (srcdata->tex != NULL) {
|
||||
gs_texture_t *tmp_texture = NULL;
|
||||
tmp_texture = srcdata->tex;
|
||||
gs_texture_t *tmp_texture = srcdata->tex;
|
||||
srcdata->tex = NULL;
|
||||
gs_texture_destroy(tmp_texture);
|
||||
}
|
||||
|
@ -497,18 +552,18 @@ void read_from_end(struct ft2_source *srcdata, const char *filename)
|
|||
|
||||
uint32_t get_ft2_text_width(wchar_t *text, struct ft2_source *srcdata)
|
||||
{
|
||||
FT_GlyphSlot slot = srcdata->font_face->glyph;
|
||||
FT_UInt glyph_index = 0;
|
||||
uint32_t w = 0, max_w = 0;
|
||||
size_t len;
|
||||
|
||||
if (!text)
|
||||
if (!text) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
len = wcslen(text);
|
||||
FT_GlyphSlot slot = srcdata->font_face->glyph;
|
||||
uint32_t w = 0, max_w = 0;
|
||||
const size_t len = wcslen(text);
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
glyph_index = FT_Get_Char_Index(srcdata->font_face, text[i]);
|
||||
FT_Load_Glyph(srcdata->font_face, glyph_index, FT_LOAD_DEFAULT);
|
||||
const FT_UInt glyph_index =
|
||||
FT_Get_Char_Index(srcdata->font_face, text[i]);
|
||||
|
||||
load_glyph(srcdata, glyph_index, get_render_mode(srcdata));
|
||||
|
||||
if (text[i] == L'\n')
|
||||
w = 0;
|
||||
|
|
Loading…
Reference in New Issue