Commit f5ee8c17 authored by David C. Lonie's avatar David C. Lonie
Browse files

Improve appearance of small-sized mathtext.

The alpha mask returned by Matplotlib for mathtext renderings
appears very dim when font size is small.

This patch uses the same trick in MatplotlibMathTextUtilities
as FreeTypeTools, which doesn't blend RGB values when the
background alpha == 0. This gives a bolder appearance to the
text, like it was before the text background color options
were added.

Also clamped the tprop opacity setters to [0., 1.] and updated
a baseline that had small mathtext.
parent 20f38014
......@@ -46,7 +46,7 @@ public:
// Description:
// Set/Get the text's opacity. 1.0 is totally opaque and 0.0 is completely
// transparent.
vtkSetMacro(Opacity,double);
vtkSetClampMacro(Opacity, double, 0., 1.);
vtkGetMacro(Opacity,double);
// Description:
......@@ -57,7 +57,7 @@ public:
// Description:
// The background opacity. 1.0 is totally opaque and 0.0 is completely
// transparent.
vtkSetMacro(BackgroundOpacity, double);
vtkSetClampMacro(BackgroundOpacity, double, 0., 1.);
vtkGetMacro(BackgroundOpacity, double);
// Description:
......
......@@ -1846,7 +1846,8 @@ bool vtkFreeTypeTools::RenderCharacter(CharType character, int &x, int &y,
iMetaData->imageIncrements[0];
unsigned char *glyphPtrRow = bitmap->buffer;
unsigned char *glyphPtr;
float tpropAlpha = iMetaData->rgba[3] / 255.0;
const unsigned char *fgRGB = iMetaData->rgba;
const float fgA = iMetaData->rgba[3] / 255.f;
for (int j = 0; j < static_cast<int>(bitmap->rows); ++j)
{
......@@ -1857,41 +1858,42 @@ bool vtkFreeTypeTools::RenderCharacter(CharType character, int &x, int &y,
if (*glyphPtr == 0)
{
ptr += 4;
++glyphPtr;
}
else if (ptr[3] > 0)
{
// This is a pixel we've drawn before since it has non-zero alpha.
// We must therefore blend the colors.
float t_alpha = tpropAlpha * (*glyphPtr / 255.0);
float t_1_m_alpha = 1.0 - t_alpha;
float data_alpha = ptr[3] / 255.0;
const float val = *glyphPtr / 255.f;
const float bgA = ptr[3] / 255.0;
float blendR(t_1_m_alpha * ptr[0] + t_alpha * iMetaData->rgba[0]);
float blendG(t_1_m_alpha * ptr[1] + t_alpha * iMetaData->rgba[1]);
float blendB(t_1_m_alpha * ptr[2] + t_alpha * iMetaData->rgba[2]);
const float fg_blend = fgA * val;
const float bg_blend = 1.f - fg_blend;
float r(bg_blend * ptr[0] + fg_blend * fgRGB[0]);
float g(bg_blend * ptr[1] + fg_blend * fgRGB[1]);
float b(bg_blend * ptr[2] + fg_blend * fgRGB[2]);
float a(255 * (fg_blend + bgA * bg_blend));
// Figure out the color.
ptr[0] = static_cast<unsigned char>(blendR);
ptr[1] = static_cast<unsigned char>(blendG);
ptr[2] = static_cast<unsigned char>(blendB);
ptr[3] = static_cast<unsigned char>(
255 * (t_alpha + data_alpha * t_1_m_alpha));
ptr[0] = static_cast<unsigned char>(r);
ptr[1] = static_cast<unsigned char>(g);
ptr[2] = static_cast<unsigned char>(b);
ptr[3] = static_cast<unsigned char>(a);
ptr += 4;
++glyphPtr;
}
else
{
*ptr = iMetaData->rgba[0];
*ptr = fgRGB[0];
++ptr;
*ptr = iMetaData->rgba[1];
*ptr = fgRGB[1];
++ptr;
*ptr = iMetaData->rgba[2];
*ptr = fgRGB[2];
++ptr;
*ptr = static_cast<unsigned char>((*glyphPtr) * tpropAlpha);
*ptr = static_cast<unsigned char>((*glyphPtr) * fgA);
++ptr;
++glyphPtr;
}
++glyphPtr;
}
glyphPtrRow += bitmap->pitch;
ptr += dataPitch;
......
......@@ -647,13 +647,14 @@ bool vtkMatplotlibMathTextUtilities::RenderString(const char *str,
unsigned char fgR = static_cast<unsigned char>(fgColor[0] * 255);
unsigned char fgG = static_cast<unsigned char>(fgColor[1] * 255);
unsigned char fgB = static_cast<unsigned char>(fgColor[2] * 255);
double fgAlpha = tprop->GetOpacity();
double fgA = tprop->GetOpacity();
double *bgColor = tprop->GetBackgroundColor();
unsigned char bgR = static_cast<unsigned char>(bgColor[0] * 255);
unsigned char bgG = static_cast<unsigned char>(bgColor[1] * 255);
unsigned char bgB = static_cast<unsigned char>(bgColor[2] * 255);
double bgAlpha = tprop->GetBackgroundOpacity();
double bgA = tprop->GetBackgroundOpacity();
bool hasBackground = (static_cast<unsigned char>(bgA * 255) != 0);
vtkSmartPyObject resultTuple(PyObject_CallMethod(this->MaskParser,
const_cast<char*>("to_mask"),
......@@ -729,7 +730,7 @@ bool vtkMatplotlibMathTextUtilities::RenderString(const char *str,
{
return false;
}
const float alpha = fgAlpha * (PyInt_AsLong(item) / 255.f);
const unsigned char val = static_cast<unsigned char>(PyInt_AsLong(item));
if (this->CheckForError())
{
return false;
......@@ -737,12 +738,23 @@ bool vtkMatplotlibMathTextUtilities::RenderString(const char *str,
unsigned char *ptr =
static_cast<unsigned char*>(image->GetScalarPointer(col, row, 0));
const float s1mA = 1.f - alpha;
if (hasBackground)
{
const float fg_blend = fgA * (val / 255.f);
const float bg_blend = 1.f - fg_blend;
ptr[0] = static_cast<unsigned char>(s1mA * bgR + alpha * fgR);
ptr[1] = static_cast<unsigned char>(s1mA * bgG + alpha * fgG);
ptr[2] = static_cast<unsigned char>(s1mA * bgB + alpha * fgB);
ptr[3] = static_cast<unsigned char>(255 * (alpha + bgAlpha * s1mA));
ptr[0] = static_cast<unsigned char>(bg_blend * bgR + fg_blend * fgR);
ptr[1] = static_cast<unsigned char>(bg_blend * bgG + fg_blend * fgG);
ptr[2] = static_cast<unsigned char>(bg_blend * bgB + fg_blend * fgB);
ptr[3] = static_cast<unsigned char>(255 * (fg_blend + bgA * bg_blend));
}
else
{
ptr[0] = fgR;
ptr[1] = fgG;
ptr[2] = fgB;
ptr[3] = static_cast<unsigned char>(val * fgA);
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment