Basic Font integration.

This commit is contained in:
James Grogan 2022-11-15 09:32:28 +00:00
parent ce11c52ae5
commit 72123bc333
36 changed files with 325 additions and 198 deletions

View file

@ -3,8 +3,11 @@
#include "AbstractMesh.h"
#include "Image.h"
#include "DiscretePoint.h"
#include <memory>
class FontsManager;
class AbstractVisualNode
{
public:
@ -21,11 +24,21 @@ public:
return mMesh.get();
}
virtual void update(FontsManager* fontsManager)
{
}
virtual void updateMesh()
{
}
virtual void updateTexture(FontsManager* fontsManager)
{
}
Image<unsigned char>* getImage() const
{
return mImage.get();
@ -36,8 +49,14 @@ public:
return mLocation;
}
Image<unsigned char>* getTexture() const
{
return mTexture.get();
}
protected:
DiscretePoint mLocation;
std::unique_ptr<AbstractMesh> mMesh;
std::unique_ptr<Image<unsigned char> > mImage;
std::unique_ptr<Image<unsigned char> > mTexture;
};

View file

@ -12,7 +12,7 @@ target_include_directories(visual_elements PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}"
)
target_link_libraries(visual_elements PUBLIC core geometry mesh image)
target_link_libraries(visual_elements PUBLIC core geometry fonts mesh image)
set_property(TARGET visual_elements PROPERTY FOLDER src)

View file

@ -33,6 +33,11 @@ unsigned RectangleNode::getHeight() const
return mHeight;
}
void RectangleNode::update(FontsManager* fontsManager)
{
updateMesh();
}
void RectangleNode::updateMesh()
{
const auto rect = Rectangle(mLocation, mWidth, mHeight);

View file

@ -15,9 +15,10 @@ public:
unsigned getWidth() const;
unsigned getHeight() const;
void updateMesh() override;
void update(FontsManager* fontsManager) override;
private:
void updateMesh() override;
unsigned mWidth{1};
unsigned mHeight{1};
};

View file

@ -3,16 +3,19 @@
#include "VisualLayer.h"
#include "GeometryNode.h"
#include "RectangleNode.h"
#include "TextNode.h"
#include "MeshBuilder.h"
#include "TriMesh.h"
#include "Image.h"
void Scene::syncLayers(const std::vector<VisualLayer*>& layers)
{
mLayers = layers;
}
void Scene::update(Image<unsigned char>* image)
void Scene::update(FontsManager* fontsManager, Image<unsigned char>* image)
{
if (image)
{
@ -28,11 +31,23 @@ void Scene::update(Image<unsigned char>* image)
auto node = layer->getShapeNode();
if (layer->getIsDirty())
{
node->updateMesh();
node->update(fontsManager);
layer->setIsDirty(false);
}
mWorkingMeshs.push_back(dynamic_cast<TriMesh*>(node->getMesh()));
mTextures.push_back(nullptr);
}
else
{
auto node = layer->getTextNode();
if (layer->getIsDirty())
{
node->update(fontsManager);
layer->setIsDirty(false);
}
mWorkingMeshs.push_back(dynamic_cast<TriMesh*>(node->getMesh()));
mTextures.push_back(node->getTexture());
}
}
}

View file

@ -5,6 +5,7 @@
class VisualLayer;
class TriMesh;
class FontsManager;
class RectangleNode;
@ -18,16 +19,19 @@ public:
void syncLayers(const std::vector<VisualLayer*>& layers);
void update(Image<unsigned char>* image = nullptr);
void update(FontsManager* fontsManager, Image<unsigned char>* image = nullptr);
unsigned getNumMeshes() const;
TriMesh* getMesh(std::size_t idx) const;
Image<unsigned char>* getTexture(std::size_t idx) const;
private:
void processRectangleNode(RectangleNode* node);
std::vector<TriMesh*> mWorkingMeshs;
std::vector<VisualLayer*> mLayers;
std::vector<Image<unsigned char>* > mTextures;
};

View file

@ -1,5 +1,10 @@
#include "TextNode.h"
#include "Rectangle.h"
#include "FontsManager.h"
#include "IFontEngine.h"
#include "MeshPrimitives.h"
#include "Color.h"
TextNode::TextNode(const std::string& content, const DiscretePoint& loc)
@ -55,3 +60,39 @@ void TextNode::setStrokeColor(const Color& color)
{
mStrokeColor = color;
}
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());
}

View file

@ -28,7 +28,12 @@ public:
void setFillColor(const Color& color);
void setStrokeColor(const Color& color);
void update(FontsManager* fontsManager) override;
private:
void updateMesh() override;
void updateTexture(FontsManager* fontsManager) override;
std::string mContent;
std::string mFontLabel;
Color mFillColor;