vtk-m/vtkm/rendering/BitmapFont.cxx

87 lines
3.1 KiB
C++
Raw Normal View History

2019-04-15 23:24:21 +00:00
//============================================================================
2016-08-26 19:30:52 +00:00
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
2019-04-15 23:24:21 +00:00
//============================================================================
2016-08-26 19:30:52 +00:00
#include <vtkm/rendering/BitmapFont.h>
2017-05-18 14:29:41 +00:00
namespace vtkm
{
namespace rendering
{
2016-08-26 19:30:52 +00:00
BitmapFont::BitmapFont()
{
2017-05-18 14:29:41 +00:00
for (int i = 0; i < 256; ++i)
2016-08-26 19:30:52 +00:00
ShortMap[i] = 0;
2017-05-18 14:29:41 +00:00
this->PadL = 0;
this->PadR = 0;
this->PadT = 0;
this->PadB = 0;
2016-08-26 19:30:52 +00:00
}
2017-05-18 14:29:41 +00:00
const vtkm::rendering::BitmapFont::Character& BitmapFont::GetChar(char c) const
2016-08-26 19:30:52 +00:00
{
2017-05-18 14:29:41 +00:00
std::size_t mappedCharIndex = static_cast<std::size_t>(this->ShortMap[(unsigned char)c]);
2016-08-26 19:30:52 +00:00
return this->Chars[mappedCharIndex];
}
2017-05-18 14:29:41 +00:00
vtkm::Float32 BitmapFont::GetTextWidth(const std::string& text) const
2016-08-26 19:30:52 +00:00
{
vtkm::Float32 width = 0;
2017-05-18 14:29:41 +00:00
for (unsigned int i = 0; i < text.length(); ++i)
2016-08-26 19:30:52 +00:00
{
Character c = this->GetChar(text[i]);
2017-05-18 14:29:41 +00:00
char nextchar = (i < text.length() - 1) ? text[i + 1] : 0;
2016-08-26 19:30:52 +00:00
const bool kerning = true;
2017-05-18 14:29:41 +00:00
if (kerning && nextchar > 0)
2016-08-26 19:30:52 +00:00
width += vtkm::Float32(c.kern[int(nextchar)]) / vtkm::Float32(this->Height);
width += vtkm::Float32(c.adv) / vtkm::Float32(this->Height);
}
return width;
}
void BitmapFont::GetCharPolygon(char character,
vtkm::Float32& x,
vtkm::Float32& y,
vtkm::Float32& vl,
vtkm::Float32& vr,
vtkm::Float32& vt,
vtkm::Float32& vb,
vtkm::Float32& tl,
vtkm::Float32& tr,
vtkm::Float32& tt,
vtkm::Float32& tb,
char nextchar) const
2016-08-26 19:30:52 +00:00
{
Character c = this->GetChar(character);
// By default, the origin for the font is at the
// baseline. That's nice, but we'd rather it
// be at the actual bottom, so create an offset.
vtkm::Float32 yoff = -vtkm::Float32(this->Descender) / vtkm::Float32(this->Height);
2017-05-18 14:29:41 +00:00
tl = vtkm::Float32(c.x + this->PadL) / vtkm::Float32(this->ImgW);
tr = vtkm::Float32(c.x + c.w - this->PadR) / vtkm::Float32(this->ImgW);
tt = 1.f - vtkm::Float32(c.y + this->PadT) / vtkm::Float32(this->ImgH);
2016-08-26 19:30:52 +00:00
tb = 1.f - vtkm::Float32(c.y + c.h - this->PadB) / vtkm::Float32(this->ImgH);
2017-05-18 14:29:41 +00:00
vl = x + vtkm::Float32(c.offx + this->PadL) / vtkm::Float32(this->Height);
vr = x + vtkm::Float32(c.offx + c.w - this->PadR) / vtkm::Float32(this->Height);
vt = yoff + y + vtkm::Float32(c.offy - this->PadT) / vtkm::Float32(this->Height);
2016-08-26 19:30:52 +00:00
vb = yoff + y + vtkm::Float32(c.offy - c.h + this->PadB) / vtkm::Float32(this->Height);
const bool kerning = true;
2017-05-18 14:29:41 +00:00
if (kerning && nextchar > 0)
2016-08-26 19:30:52 +00:00
x += vtkm::Float32(c.kern[int(nextchar)]) / vtkm::Float32(this->Height);
x += vtkm::Float32(c.adv) / vtkm::Float32(this->Height);
}
}
} // namespace vtkm::rendering