Clean text rendering in editor.

This commit is contained in:
James Grogan 2022-12-02 11:50:15 +00:00
parent 290b64e230
commit f16dd7c0d9
45 changed files with 59 additions and 60 deletions

View file

@ -1,7 +1,7 @@
#include "FontGlyph.h"
FontGlyph::FontGlyph(unsigned width, unsigned height, unsigned bearingX, unsigned bearingY,
unsigned advanceX, std::unique_ptr<Image<unsigned char> > image)
FontGlyph::FontGlyph(unsigned width, unsigned height, int bearingX, int bearingY,
int advanceX, std::unique_ptr<Image<unsigned char> > image)
: mImage(std::move(image)),
mWidth(width),
mHeight(height),
@ -27,17 +27,17 @@ unsigned FontGlyph::getHeight() const
return mHeight;
}
unsigned FontGlyph::getBearingX() const
int FontGlyph::getBearingX() const
{
return mBearingX;
}
unsigned FontGlyph::getBearingY() const
int FontGlyph::getBearingY() const
{
return mBearingY;
}
unsigned FontGlyph::getAdvanceX() const
int FontGlyph::getAdvanceX() const
{
return mAdvanceX;
}

View file

@ -8,22 +8,22 @@ class FontGlyph
{
public:
FontGlyph(unsigned width, unsigned height, unsigned bearingX, unsigned bearingY,
unsigned advanceX, std::unique_ptr<Image<unsigned char> > image);
FontGlyph(unsigned width, unsigned height, int bearingX, int bearingY,
int advanceX, std::unique_ptr<Image<unsigned char> > image);
Image<unsigned char>* getImage() const;
unsigned getWidth() const;
unsigned getHeight() const;
unsigned getBearingX() const;
unsigned getBearingY() const;
unsigned getAdvanceX() const;
int getBearingX() const;
int getBearingY() const;
int getAdvanceX() const;
private:
unsigned mWidth{0};
unsigned mHeight{0};
unsigned mBearingX{0};
unsigned mBearingY{0};
unsigned mAdvanceX{0};
int mBearingX{0};
int mBearingY{0};
int mAdvanceX{0};
std::unique_ptr<Image<unsigned char> > mImage;
};

View file

@ -50,7 +50,7 @@ public:
}
private:
unsigned mSize{16};
unsigned mSize{24};
std::string mFaceName;
std::filesystem::path mPath;
};

View file

@ -29,9 +29,9 @@ IFontEngine* FontsManager::getFontEngine() const
return mFontEngine.get();
}
FontGlyph* FontsManager::getGlyph(const std::string& fontFace, int size, char c)
FontGlyph* FontsManager::getGlyph(const std::string& fontFace, int size, uint32_t c)
{
auto path = std::filesystem::path("truetype/msttcorefonts/arial.ttf");
auto path = std::filesystem::path("truetype/liberation/LiberationSans-Regular.ttf");
mFontEngine->loadFontFace(path, size);

View file

@ -15,12 +15,12 @@ public:
IFontEngine* getFontEngine() const;
FontGlyph* getGlyph(const std::string& fontFace, int size, char c);
FontGlyph* getGlyph(const std::string& fontFace, int size, uint32_t c);
private:
std::unique_ptr<IFontEngine> mFontEngine;
std::unordered_map<char, std::unique_ptr<FontGlyph> > mGlyphs;
std::unordered_map<uint32_t, std::unique_ptr<FontGlyph> > mGlyphs;
};
using FontsManagerPtr = std::unique_ptr<FontsManager>;

View file

@ -4,6 +4,8 @@
#include "FileLogger.h"
#include "FontGlyph.h"
#include <iostream>
void FreeTypeFontEngine::initialize()
{
if (mLibrary)
@ -43,7 +45,7 @@ void FreeTypeFontEngine::loadFontFace(const std::filesystem::path& fontFile, int
FT_Set_Pixel_Sizes(mWorkingFontFace, penSize, 0);
}
std::unique_ptr<FontGlyph> FreeTypeFontEngine::loadGlyph(unsigned charCode)
std::unique_ptr<FontGlyph> FreeTypeFontEngine::loadGlyph(uint32_t charCode)
{
auto glyph_index = FT_Get_Char_Index( mWorkingFontFace, charCode );
if (glyph_index == 0)
@ -89,7 +91,6 @@ std::unique_ptr<FontGlyph> FreeTypeFontEngine::loadGlyph(unsigned charCode)
}
}
image->setData(data);
auto glyph = std::make_unique<FontGlyph>(mWorkingFontFace->glyph->bitmap.width,
mWorkingFontFace->glyph->bitmap.rows,
mWorkingFontFace->glyph->bitmap_left,

View file

@ -19,7 +19,7 @@ public:
void loadFontFace(const std::filesystem::path& fontFile, int penSize = 16) override;
std::unique_ptr<FontGlyph> loadGlyph(unsigned charCode) override;
std::unique_ptr<FontGlyph> loadGlyph(uint32_t charCode) override;
private:
std::filesystem::path mSystemFontLocation{"/usr/share/fonts/"};

View file

@ -15,5 +15,5 @@ public:
virtual void loadFontFace(const std::filesystem::path& fontFile, int penSize = 16) = 0;
virtual std::unique_ptr<FontGlyph> loadGlyph(unsigned charCode) = 0;
virtual std::unique_ptr<FontGlyph> loadGlyph(uint32_t charCode) = 0;
};

View file

@ -27,8 +27,6 @@
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
OpenGlTextPainter::OpenGlTextPainter()
{
@ -118,7 +116,7 @@ void OpenGlTextPainter::paint(SceneText* text, DrawingContext* context)
auto texture = mFontTextures[c].get();
float xpos = x + texture->getGlyph()->getBearingX();
float ypos = y - (texture->getGlyph()->getHeight() - texture->getGlyph()->getBearingY());
float ypos = y - (int(texture->getGlyph()->getHeight()) - texture->getGlyph()->getBearingY());
float w = texture->getGlyph()->getWidth();
float h = texture->getGlyph()->getHeight();
@ -131,7 +129,6 @@ void OpenGlTextPainter::paint(SceneText* text, DrawingContext* context)
{ xpos + w, ypos, 1.0f, 1.0f },
{ xpos + w, ypos + h, 1.0f, 0.0f }
};
glBindTexture(GL_TEXTURE_2D, texture->getHandle());
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
@ -140,12 +137,11 @@ void OpenGlTextPainter::paint(SceneText* text, DrawingContext* context)
glDrawArrays(GL_TRIANGLES, 0, 6);
x += (texture->getGlyph()->getAdvanceX() >> 6); // bitshift by 6 to get value in pixels (2^6 = 64)
auto offset = (texture->getGlyph()->getAdvanceX() >> 6); // bitshift by 6 to get value in pixels (2^6 = 64)
x += offset;
}
line_offset += line_delta;
}
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
}

View file

@ -42,8 +42,8 @@ PdfPage::PdfPage(PdfPageTree* parent)
std::string pageContent = "BT\n";
pageContent += "/F1 24 Tf\n";
pageContent += "100 100 Td\n";
pageContent += "(Hello World) Tj\n";
pageContent += "100 700 Td\n";
pageContent += "(Hello \nWorld) Tj\n";
pageContent += "ET";
mContent->setContent(pageContent);

View file

@ -17,6 +17,5 @@ public:
void updateDictionary() override;
private:
PdfPagePtr mRootPage;
};

View file

@ -57,7 +57,15 @@ bool TextBox::onMyKeyboardEvent(const KeyboardEvent* event)
else
{
const auto keyString = event->getKeyString();
appendContent(keyString);
if (keyString.length() < 2)
{
appendContent(keyString);
}
else
{
appendContent("?");
}
}
return true;
}