Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirgamalyan committed Nov 30, 2021
2 parents 2252bcc + ba5f35b commit a37a9e0
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 82 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ option | default | comment
--spacing-horiz | 0 | spacing horizontal
--texture-width | 256 | texture width
--texture-height | 256 | texture height
--monochrome | | disable anti-aliasing
--max-texture-count | | maximum generated texture count (unlimited if not defined)

## Building Linux
Expand Down
9 changes: 5 additions & 4 deletions src/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ std::vector<rbp::RectSize> App::getGlyphRectangles(const Glyphs &glyphs, const s
return result;
}

App::Glyphs App::collectGlyphInfo(const std::vector<ft::Font>& fonts, const std::set<std::uint32_t>& codes)
App::Glyphs App::collectGlyphInfo(const std::vector<ft::Font>& fonts, const std::set<std::uint32_t>& codes, const Config& config)
{
Glyphs result;

Expand All @@ -33,7 +33,7 @@ App::Glyphs App::collectGlyphInfo(const std::vector<ft::Font>& fonts, const std:
{
if (fonts[i].isGlyphProvided(id))
{
ft::Font::GlyphMetrics glyphMetrics = fonts[i].renderGlyph(nullptr, 0, 0, 0, 0, id, 0);
ft::Font::GlyphMetrics glyphMetrics = fonts[i].renderGlyph(nullptr, 0, 0, 0, 0, id, 0, config.monochrome);
glyphInfo.fontIndex = i;
glyphInfo.width = glyphMetrics.width;
glyphInfo.height = glyphMetrics.height;
Expand Down Expand Up @@ -140,7 +140,8 @@ std::vector<std::string> App::renderTextures(const Glyphs& glyphs, const Config&
const auto x = glyph.x + config.padding.left;
const auto y = glyph.y + config.padding.up;

fonts[glyph.fontIndex].renderGlyph(&surface[0], config.textureSize.w, config.textureSize.h, x, y, kv.first, config.color.getBGR());
fonts[glyph.fontIndex].renderGlyph(&surface[0], config.textureSize.w, config.textureSize.h, x, y,
kv.first, config.color.getBGR(), config.monochrome);
}
}

Expand Down Expand Up @@ -275,7 +276,7 @@ void App::execute(const int argc, char* argv[])
for (auto& f: config.fontFile)
fonts.emplace_back(library, f, config.fontSize);

auto glyphs = collectGlyphInfo(fonts, config.chars);
auto glyphs = collectGlyphInfo(fonts, config.chars, config);
const auto pageCount = arrangeGlyphs(glyphs, config);
if (config.maxTextureCount != 0 && pageCount > config.maxTextureCount)
throw std::runtime_error("too many generated textures (more than --max-texture-count)");
Expand Down
2 changes: 1 addition & 1 deletion src/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class App
typedef std::map<std::uint32_t, GlyphInfo> Glyphs;

static std::vector<rbp::RectSize> getGlyphRectangles(const Glyphs& glyphs, std::uint32_t additionalWidth, std::uint32_t additionalHeight) ;
static Glyphs collectGlyphInfo(const std::vector<ft::Font>& fonts, const std::set<std::uint32_t>& codes) ;
static Glyphs collectGlyphInfo(const std::vector<ft::Font>& fonts, const std::set<std::uint32_t>& codes, const Config& config) ;
static std::uint32_t arrangeGlyphs(Glyphs& glyphs, const Config& config) ;
static std::vector<std::string> renderTextures(const Glyphs& glyphs, const Config& config, const std::vector<ft::Font>& fonts, std::uint32_t pageCount) ;
static void savePng(const std::string& fileName, const std::uint32_t* buffer, std::uint32_t w, std::uint32_t h, bool withAlpha) ;
Expand Down
1 change: 1 addition & 0 deletions src/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct Config
DataFormat dataFormat = DataFormat::Text;
bool includeKerningPairs = false;
std::uint32_t maxTextureCount = 0;
bool monochrome = false;

void validate() const
{
Expand Down
1 change: 1 addition & 0 deletions src/ProgramOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Config ProgramOptions::parseCommandLine(int argc, char* argv[])
("data-format", R"(output data file format, "xml" or "txt", default "xml")",
cxxopts::value<std::string>(dataFormat)->default_value("txt"))
("include-kerning-pairs", "include kerning pairs to output file", cxxopts::value<bool>(config.includeKerningPairs))
("monochrome", "disable antialiasing", cxxopts::value<bool>(config.monochrome))
("max-texture-count", "maximum generated textures", cxxopts::value<std::uint32_t>(config.maxTextureCount)->default_value("0"));

auto result = options.parse(argc, argv);
Expand Down
60 changes: 33 additions & 27 deletions src/freeType/FtFont.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,14 @@ class Font
if (error)
throw Exception("Couldn't load font file", error);

FT_CharMap found = nullptr;
for (auto i = 0; i < face->num_charmaps; i++) {
const auto charmap = face->charmaps[i];
if ((charmap->platform_id == 3 && charmap->encoding_id == 1) /* Windows Unicode */
|| (charmap->platform_id == 3 && charmap->encoding_id == 0) /* Windows Symbol */
|| (charmap->platform_id == 2 && charmap->encoding_id == 1) /* ISO Unicode */
|| (charmap->platform_id == 0)) { /* Apple Unicode */
found = charmap;
break;
}
}
if (found) {
/* If this fails, continue using the default charmap */
FT_Set_Charmap(face, found);
if (!face->charmap)
{
FT_Done_Face(face);
throw std::runtime_error("Font doesn't contain a Unicode charmap");
}

if (FT_IS_SCALABLE(face)) {
if (FT_IS_SCALABLE(face))
{
/* Set the character size and use default DPI (72) */
error = FT_Set_Pixel_Sizes(face, ptsize, ptsize);
if (error) {
Expand All @@ -75,7 +66,9 @@ class Font
const auto scale = face->size->metrics.y_scale;
yMax = FT_CEIL(FT_MulFix(face->bbox.yMax, scale));
height = FT_CEIL(FT_MulFix(face->height, scale));
} else {
}
else
{
/* Non-scalable font case. ptsize determines which family
* or series of fonts to grab from the non-scalable format.
* It is not the point size of the font.
Expand All @@ -99,12 +92,11 @@ class Font

/* Initialize the font face style */
face_style = TTF_STYLE_NORMAL;
if ( face->style_flags & FT_STYLE_FLAG_BOLD ) {
if (face->style_flags & FT_STYLE_FLAG_BOLD)
face_style |= TTF_STYLE_BOLD;
}
if ( face->style_flags & FT_STYLE_FLAG_ITALIC ) {

if (face->style_flags & FT_STYLE_FLAG_ITALIC)
face_style |= TTF_STYLE_ITALIC;
}

/* Set the default font style */
style = face_style;
Expand All @@ -116,13 +108,18 @@ class Font
glyph_italics *= height;
}

~Font() {
~Font()
{
FT_Done_Face(face);
}

GlyphMetrics renderGlyph(std::uint32_t* buffer, std::uint32_t surfaceW, std::uint32_t surfaceH, int x, int y, std::uint32_t ch, std::uint32_t color) const
GlyphMetrics renderGlyph(std::uint32_t* buffer, std::uint32_t surfaceW, std::uint32_t surfaceH, int x, int y,
std::uint32_t ch, std::uint32_t color, bool monochrome = false) const
{
const auto error = FT_Load_Char(face, ch, FT_LOAD_RENDER);
FT_Int32 loadFlags = FT_LOAD_RENDER;
if (monochrome)
loadFlags |= FT_LOAD_MONOCHROME;
const auto error = FT_Load_Char(face, ch, loadFlags);
if (error)
throw std::runtime_error("Load glyph error");

Expand Down Expand Up @@ -155,7 +152,8 @@ class Font
src = unpacked.data();
}

for (auto col = glyphMetrics.width; col > 0 && dst < dst_check; --col) {
for (auto col = glyphMetrics.width; col > 0 && dst < dst_check; --col)
{
const std::uint32_t alpha = *src++;
*dst++ = color | (alpha << 24u);
}
Expand Down Expand Up @@ -186,14 +184,24 @@ class Font
return delta.x >> 6u;
}

void debugInfo() const {
void debugInfo() const
{
std::cout << "num_charmaps " << face->num_charmaps << std::endl;
std::cout << "num_glyphs " << face->num_glyphs << std::endl;

for (auto i = 0; i < face->num_charmaps; i++) {
const auto charmap = face->charmaps[i];
std::cout << charmap->platform_id << ", " << charmap->encoding_id << std::endl;
}

const auto scale = face->size->metrics.y_scale;
std::cout << "face->size->metrics.y_scale " << scale << " (" << face->size->metrics.y_scale / 64.0 << ")" << "\n";
std::cout << "face->size->metrics.y_ppem " << face->size->metrics.x_ppem << "\n";
std::cout << "face->bbox.yMax " << FT_CEIL(FT_MulFix(face->bbox.yMax, scale)) << "\n";
std::cout << "face->bbox.yMin " << FT_FLOOR(FT_MulFix(face->bbox.yMin, scale)) << "\n";
std::cout << "face->ascender " << FT_CEIL(FT_MulFix(face->ascender, scale)) << "\n";
std::cout << "face->descender " << FT_FLOOR(FT_MulFix(face->descender, scale)) << "\n";
std::cout << "face->height " << FT_CEIL(FT_MulFix(face->height, scale)) << "\n";
}

std::string getFamilyNameOr(const std::string& defaultName) const
Expand All @@ -218,7 +226,6 @@ class Font
int height;
int yMax;


/* For non-scalable formats, we must remember which font index size */
int font_size_family;

Expand All @@ -230,7 +237,6 @@ class Font
/* Whether kerning is desired */
int kerning;


/* Extra width in glyph bounds for text styles */
int glyph_overhang;
float glyph_italics;
Expand Down
61 changes: 32 additions & 29 deletions src/freeType/FtLibrary.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
#include "FtLibrary.h"
#include "FtException.h"

namespace ft {

int Library::initialized = 0;

Library::Library()
{
if (!initialized)
{
const auto error = FT_Init_FreeType(&library);
if (error)
throw Exception("Couldn't init FreeType engine", error);
}

++initialized;
}

Library::~Library()
{
if (initialized) {
if (--initialized == 0) {
FT_Done_FreeType(library);
library = nullptr;
}
}
}
}
#include "FtLibrary.h"
#include "FtException.h"

namespace ft {

int Library::initialized = 0;

Library::Library()
{
if (!initialized)
{
const auto error = FT_Init_FreeType(&library);
if (error)
throw Exception("Couldn't init FreeType engine", error);
}

++initialized;
}

Library::~Library()
{
if (initialized)
{
if (--initialized == 0)
{
FT_Done_FreeType(library);
library = nullptr;
}
}
}

}
44 changes: 23 additions & 21 deletions src/freeType/FtLibrary.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
#pragma once
#include "FtInclude.h"

namespace ft {

class Library
{
public:
Library();
Library(const Library&) = delete;
Library& operator = (const Library&) = delete;
Library(Library&&) = delete;
Library& operator=(Library&&) = delete;
~Library();
FT_Library library = nullptr;

private:
static int initialized;
};

}
#pragma once
#include "FtInclude.h"

namespace ft {

class Library
{
public:
Library();
~Library();

FT_Library library = nullptr;

Library(const Library&) = delete;
Library(Library&&) = delete;
Library& operator = (const Library&) = delete;
Library& operator=(Library&&) = delete;

private:
static int initialized;
};

}

0 comments on commit a37a9e0

Please sign in to comment.