stuff-from-scratch/src/visual_elements/TextNode.cpp

99 lines
2.1 KiB
C++
Raw Normal View History

2022-11-13 17:02:09 +00:00
#include "TextNode.h"
2022-11-15 09:32:28 +00:00
#include "Rectangle.h"
#include "FontsManager.h"
#include "IFontEngine.h"
#include "MeshPrimitives.h"
2022-11-13 17:02:09 +00:00
#include "Color.h"
TextNode::TextNode(const std::string& content, const DiscretePoint& loc)
: AbstractVisualNode(loc),
mContent(content),
mFontLabel("fixed"),
mFillColor(Color(255, 255, 255)),
mStrokeColor(Color(0, 0, 0))
{
// https://en.wikipedia.org/wiki/Fixed_(typeface)#:~:text=misc%2Dfixed%20is%20a%20collection,to%20a%20single%20font%20family.
}
TextNode::~TextNode()
{
}
std::unique_ptr<TextNode> TextNode::Create(const std::string& content, const DiscretePoint& loc)
{
return std::make_unique<TextNode>(content, loc);
}
const Color& TextNode::getFillColor() const
{
return mFillColor;
}
const Color& TextNode::getStrokeColor() const
{
return mStrokeColor;
}
std::string TextNode::getFontLabel() const
{
return mFontLabel;
}
std::string TextNode::getContent() const
{
return mContent;
}
void TextNode::setContent(const std::string& content)
{
mContent = content;
}
void TextNode::setFillColor(const Color& color)
{
mFillColor = color;
}
void TextNode::setStrokeColor(const Color& color)
{
mStrokeColor = color;
}
2022-11-15 09:32:28 +00:00
void TextNode::update(FontsManager* drawingManager)
{
updateMesh();
updateTexture(drawingManager);
}
void TextNode::updateMesh()
{
double font_height = 16;
double font_width = 16;
double text_width = mContent.size() * font_width;
const auto rect = Rectangle(mLocation, text_width, font_height);
auto mesh = MeshPrimitives::build(rect);
auto color = Color(0, 0, 0, 1.0);
mesh->addConstantFaceVectorAttribute("Color", color.getAsVectorDouble());
mMesh = std::move(mesh);
}
void TextNode::updateTexture(FontsManager* fontsManager)
{
fontsManager->getFontEngine()->loadFontFace("truetype/msttcorefonts/arial.ttf");
auto glyph = fontsManager->getFontEngine()->loadGlyph('A');
if (!glyph)
{
return;
}
mTexture = std::make_unique<Image<unsigned char> >(glyph->getWidth(), glyph->getHeight());
mTexture->setData(glyph->getData());
}