2013-07-29 06:08:34 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Routines to read a TrueType font as vector outlines, and generate them
|
|
|
|
// as entities, since they're always representable as either lines or
|
|
|
|
// quadratic Bezier curves.
|
|
|
|
//
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
// Copyright 2016 whitequark, Peter Barfuss.
|
2013-07-29 06:08:34 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
#include <ft2build.h>
|
|
|
|
#include FT_FREETYPE_H
|
|
|
|
#include FT_OUTLINE_H
|
|
|
|
#include FT_ADVANCES_H
|
|
|
|
|
|
|
|
/* Yecch. Irritatingly, you need to do this nonsense to get the error string table,
|
|
|
|
since nobody thought to put this exact function into FreeType itsself. */
|
|
|
|
#undef __FTERRORS_H__
|
|
|
|
#define FT_ERRORDEF(e, v, s) { (e), (s) },
|
|
|
|
#define FT_ERROR_START_LIST
|
|
|
|
#define FT_ERROR_END_LIST { 0, NULL }
|
|
|
|
|
|
|
|
struct ft_error {
|
|
|
|
int err;
|
|
|
|
const char *str;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct ft_error ft_errors[] = {
|
|
|
|
#include FT_ERRORS_H
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" const char *ft_error_string(int err) {
|
|
|
|
const struct ft_error *e;
|
|
|
|
for(e = ft_errors; e->str; e++)
|
|
|
|
if(e->err == err)
|
|
|
|
return e->str;
|
|
|
|
return "Unknown error";
|
2008-06-30 17:09:17 +08:00
|
|
|
}
|
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
/* Okay, we're done with that. */
|
|
|
|
#undef FT_ERRORDEF
|
|
|
|
#undef FT_ERROR_START_LIST
|
|
|
|
#undef FT_ERROR_END_LIST
|
2008-06-30 17:09:17 +08:00
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
#include "solvespace.h"
|
2008-06-30 17:09:17 +08:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
// Get the list of available font filenames, and load the name for each of
|
|
|
|
// them. Only that, though, not the glyphs too.
|
2008-06-30 17:09:17 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
TtfFontList::TtfFontList() {
|
|
|
|
FT_Init_FreeType(&fontLibrary);
|
2008-06-30 17:09:17 +08:00
|
|
|
}
|
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
TtfFontList::~TtfFontList() {
|
|
|
|
FT_Done_FreeType(fontLibrary);
|
2008-06-30 17:09:17 +08:00
|
|
|
}
|
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
void TtfFontList::LoadAll() {
|
|
|
|
if(loaded) return;
|
2008-06-30 17:09:17 +08:00
|
|
|
|
2018-07-18 10:20:25 +08:00
|
|
|
for(const Platform::Path &font : Platform::GetFontFiles()) {
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
TtfFont tf = {};
|
|
|
|
tf.fontFile = font;
|
|
|
|
if(tf.LoadFromFile(fontLibrary))
|
|
|
|
l.Add(&tf);
|
2008-06-30 17:09:17 +08:00
|
|
|
}
|
|
|
|
|
2018-07-13 02:58:44 +08:00
|
|
|
// Add builtin font to end of font list so it is displayed first in the UI
|
|
|
|
{
|
|
|
|
TtfFont tf = {};
|
|
|
|
tf.SetResourceID("fonts/BitstreamVeraSans-Roman-builtin.ttf");
|
|
|
|
if(tf.LoadFromResource(fontLibrary))
|
|
|
|
l.Add(&tf);
|
|
|
|
}
|
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
// Sort fonts according to their actual name, not filename.
|
|
|
|
std::sort(&l.elem[0], &l.elem[l.n],
|
|
|
|
[](const TtfFont &a, const TtfFont &b) { return a.name < b.name; });
|
2008-06-30 17:09:17 +08:00
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
// Filter out fonts with the same family and style name. This is not
|
|
|
|
// strictly necessarily the exact same font, but it will almost always be.
|
|
|
|
TtfFont *it = std::unique(&l.elem[0], &l.elem[l.n],
|
|
|
|
[](const TtfFont &a, const TtfFont &b) { return a.name == b.name; });
|
|
|
|
l.RemoveLast(&l.elem[l.n] - it);
|
2008-06-30 17:09:17 +08:00
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
// TODO: identify fonts by their name and not filename, which may change
|
|
|
|
// between OSes.
|
2008-06-30 17:09:17 +08:00
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
loaded = true;
|
|
|
|
}
|
2008-06-30 17:09:17 +08:00
|
|
|
|
2016-10-11 09:58:04 +08:00
|
|
|
TtfFont *TtfFontList::LoadFont(const std::string &font)
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
{
|
|
|
|
LoadAll();
|
2015-03-29 08:30:52 +08:00
|
|
|
|
2016-10-11 09:58:04 +08:00
|
|
|
TtfFont *tf = std::find_if(l.begin(), l.end(),
|
2018-07-13 02:58:44 +08:00
|
|
|
[&font](const TtfFont &tf) { return tf.FontFileBaseName() == font; });
|
2008-06-30 17:09:17 +08:00
|
|
|
|
2016-10-11 09:58:04 +08:00
|
|
|
if(tf != l.end()) {
|
2016-10-11 05:16:22 +08:00
|
|
|
if(tf->fontFace == NULL) {
|
2018-07-13 02:58:44 +08:00
|
|
|
if(tf->IsResource())
|
|
|
|
tf->LoadFromResource(fontLibrary, /*nameOnly=*/false);
|
|
|
|
else
|
|
|
|
tf->LoadFromFile(fontLibrary, /*nameOnly=*/false);
|
2016-10-11 05:16:22 +08:00
|
|
|
}
|
2016-10-11 09:58:04 +08:00
|
|
|
return tf;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TtfFontList::PlotString(const std::string &font, const std::string &str,
|
|
|
|
SBezierList *sbl, Vector origin, Vector u, Vector v)
|
|
|
|
{
|
|
|
|
TtfFont *tf = LoadFont(font);
|
|
|
|
if(!str.empty() && tf != NULL) {
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
tf->PlotString(str, sbl, origin, u, v);
|
2008-06-30 17:09:17 +08:00
|
|
|
} else {
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
// No text or no font; so draw a big X for an error marker.
|
|
|
|
SBezier sb;
|
|
|
|
sb = SBezier::From(origin, origin.Plus(u).Plus(v));
|
|
|
|
sbl->l.Add(&sb);
|
|
|
|
sb = SBezier::From(origin.Plus(v), origin.Plus(u));
|
|
|
|
sbl->l.Add(&sb);
|
2008-06-30 17:09:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 09:58:04 +08:00
|
|
|
double TtfFontList::AspectRatio(const std::string &font, const std::string &str)
|
|
|
|
{
|
|
|
|
TtfFont *tf = LoadFont(font);
|
|
|
|
if(tf != NULL) {
|
|
|
|
return tf->AspectRatio(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
2008-06-30 17:09:17 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Return the basename of our font filename; that's how the requests and
|
|
|
|
// entities that reference us will store it.
|
|
|
|
//-----------------------------------------------------------------------------
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
std::string TtfFont::FontFileBaseName() const {
|
2017-03-11 22:43:21 +08:00
|
|
|
return fontFile.FileName();
|
2008-06-30 17:09:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2018-07-13 02:58:44 +08:00
|
|
|
// Convenience method to set fontFile for resource-loaded fonts as res://<path>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void TtfFont::SetResourceID(const std::string &resource) {
|
|
|
|
fontFile = { "res://" + resource };
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TtfFont::IsResource() const {
|
|
|
|
return fontFile.raw.compare(0, 6, "res://") == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Load a TrueType font into memory.
|
2008-06-30 17:09:17 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
2016-05-07 12:20:06 +08:00
|
|
|
bool TtfFont::LoadFromFile(FT_Library fontLibrary, bool nameOnly) {
|
2018-07-13 02:58:44 +08:00
|
|
|
ssassert(!IsResource(), "Cannot load a font provided by a resource as a file.");
|
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
FT_Open_Args args = {};
|
|
|
|
args.flags = FT_OPEN_PATHNAME;
|
2017-03-11 22:43:21 +08:00
|
|
|
args.pathname = &fontFile.raw[0]; // FT_String is char* for historical reasons
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
|
2017-03-11 22:43:21 +08:00
|
|
|
// We don't use OpenFile() here to let freetype do its own memory management.
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
// This is OK because on Linux/OS X we just delegate to fopen and on Windows
|
|
|
|
// we only look into C:\Windows\Fonts, which has a known short path.
|
|
|
|
if(int fterr = FT_Open_Face(fontLibrary, &args, 0, &fontFace)) {
|
|
|
|
dbp("freetype: loading font from file '%s' failed: %s",
|
2017-03-11 22:43:21 +08:00
|
|
|
fontFile.raw.c_str(), ft_error_string(fterr));
|
2008-06-30 17:09:17 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-13 02:58:44 +08:00
|
|
|
return ExtractTTFData(nameOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Load a TrueType from resource in memory. Implemented to load bundled fonts
|
|
|
|
// through theresource system.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool TtfFont::LoadFromResource(FT_Library fontLibrary, bool nameOnly) {
|
|
|
|
ssassert(IsResource(), "Font to be loaded as resource is not provided by a resource "
|
|
|
|
"or does not have the 'res://' prefix.");
|
|
|
|
|
|
|
|
size_t _size;
|
|
|
|
// substr to cut off 'res://' (length: 6)
|
|
|
|
const void *_buffer = Platform::LoadResource(fontFile.raw.substr(6, fontFile.raw.size()),
|
|
|
|
&_size);
|
|
|
|
|
|
|
|
FT_Long size = static_cast<FT_Long>(_size);
|
|
|
|
const FT_Byte *buffer = reinterpret_cast<const FT_Byte*>(_buffer);
|
|
|
|
|
|
|
|
if(int fterr = FT_New_Memory_Face(fontLibrary, buffer, size, 0, &fontFace)) {
|
|
|
|
dbp("freetype: loading font '%s' from memory failed: %s",
|
|
|
|
fontFile.raw.c_str(), ft_error_string(fterr));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ExtractTTFData(nameOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Extract font information. We care about the font name and unit size.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool TtfFont::ExtractTTFData(bool nameOnly) {
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
if(int fterr = FT_Select_Charmap(fontFace, FT_ENCODING_UNICODE)) {
|
|
|
|
dbp("freetype: loading unicode CMap for file '%s' failed: %s",
|
2017-03-11 22:43:21 +08:00
|
|
|
fontFile.raw.c_str(), ft_error_string(fterr));
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
FT_Done_Face(fontFace);
|
2016-10-11 05:16:22 +08:00
|
|
|
fontFace = NULL;
|
2008-06-30 17:09:17 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
name = std::string(fontFace->family_name) +
|
|
|
|
" (" + std::string(fontFace->style_name) + ")";
|
2016-05-07 12:20:06 +08:00
|
|
|
|
|
|
|
if(nameOnly) {
|
|
|
|
FT_Done_Face(fontFace);
|
|
|
|
fontFace = NULL;
|
2016-11-02 16:39:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We always ask Freetype to give us a unit size character.
|
|
|
|
// It uses fixed point; put the unit size somewhere in the middle of the dynamic
|
|
|
|
// range of its 26.6 fixed point type, and adjust the factors so that the unit
|
|
|
|
// matches cap height.
|
|
|
|
FT_Size_RequestRec sizeRequest;
|
|
|
|
sizeRequest.type = FT_SIZE_REQUEST_TYPE_REAL_DIM;
|
|
|
|
sizeRequest.width = 1 << 16;
|
|
|
|
sizeRequest.height = 1 << 16;
|
|
|
|
sizeRequest.horiResolution = 128;
|
|
|
|
sizeRequest.vertResolution = 128;
|
|
|
|
if(int fterr = FT_Request_Size(fontFace, &sizeRequest)) {
|
|
|
|
dbp("freetype: cannot set character size: %s",
|
|
|
|
ft_error_string(fterr));
|
|
|
|
FT_Done_Face(fontFace);
|
|
|
|
fontFace = NULL;
|
|
|
|
return false;
|
2016-05-07 12:20:06 +08:00
|
|
|
}
|
|
|
|
|
2016-11-02 16:59:33 +08:00
|
|
|
char chr = 'A';
|
|
|
|
uint32_t gid = FT_Get_Char_Index(fontFace, 'A');
|
|
|
|
if (gid == 0) {
|
|
|
|
dbp("freetype: CID-to-GID mapping for CID 0x%04x failed: %s; using CID as GID",
|
|
|
|
chr, ft_error_string(gid));
|
2017-04-27 07:06:50 +08:00
|
|
|
dbp("Assuming cap height is the same as requested height (this is likely wrong).");
|
|
|
|
capHeight = (double)sizeRequest.height;
|
2016-11-02 16:59:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(gid) {
|
|
|
|
if(int fterr = FT_Load_Glyph(fontFace, gid, FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING)) {
|
|
|
|
dbp("freetype: cannot load glyph for GID 0x%04x: %s",
|
|
|
|
gid, ft_error_string(fterr));
|
|
|
|
FT_Done_Face(fontFace);
|
|
|
|
fontFace = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FT_BBox bbox;
|
|
|
|
FT_Outline_Get_CBox(&fontFace->glyph->outline, &bbox);
|
|
|
|
capHeight = (double)bbox.yMax;
|
|
|
|
}
|
|
|
|
|
2008-06-30 17:09:17 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
typedef struct OutlineData {
|
|
|
|
Vector origin, u, v; // input parameters
|
|
|
|
SBezierList *beziers; // output bezier list
|
|
|
|
float factor; // ratio between freetype and solvespace coordinates
|
|
|
|
FT_Pos bx; // x offset of the current glyph
|
|
|
|
FT_Pos px, py; // current point
|
|
|
|
} OutlineData;
|
|
|
|
|
|
|
|
static Vector Transform(OutlineData *data, FT_Pos x, FT_Pos y) {
|
|
|
|
Vector r = data->origin;
|
|
|
|
r = r.Plus(data->u.ScaledBy((float)(data->bx + x) * data->factor));
|
|
|
|
r = r.Plus(data->v.ScaledBy((float)y * data->factor));
|
|
|
|
return r;
|
2008-06-30 17:09:17 +08:00
|
|
|
}
|
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
static int MoveTo(const FT_Vector *p, void *cc)
|
|
|
|
{
|
|
|
|
OutlineData *data = (OutlineData *) cc;
|
|
|
|
data->px = p->x;
|
|
|
|
data->py = p->y;
|
|
|
|
return 0;
|
2008-06-30 17:09:17 +08:00
|
|
|
}
|
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
static int LineTo(const FT_Vector *p, void *cc)
|
|
|
|
{
|
|
|
|
OutlineData *data = (OutlineData *) cc;
|
|
|
|
SBezier sb = SBezier::From(
|
|
|
|
Transform(data, data->px, data->py),
|
|
|
|
Transform(data, p->x, p->y));
|
|
|
|
data->beziers->l.Add(&sb);
|
|
|
|
data->px = p->x;
|
|
|
|
data->py = p->y;
|
|
|
|
return 0;
|
|
|
|
}
|
2008-06-30 17:09:17 +08:00
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
static int ConicTo(const FT_Vector *c, const FT_Vector *p, void *cc)
|
|
|
|
{
|
|
|
|
OutlineData *data = (OutlineData *) cc;
|
|
|
|
SBezier sb = SBezier::From(
|
|
|
|
Transform(data, data->px, data->py),
|
|
|
|
Transform(data, c->x, c->y),
|
|
|
|
Transform(data, p->x, p->y));
|
|
|
|
data->beziers->l.Add(&sb);
|
|
|
|
data->px = p->x;
|
|
|
|
data->py = p->y;
|
|
|
|
return 0;
|
|
|
|
}
|
2008-06-30 17:09:17 +08:00
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
static int CubicTo(const FT_Vector *c1, const FT_Vector *c2, const FT_Vector *p, void *cc)
|
|
|
|
{
|
|
|
|
OutlineData *data = (OutlineData *) cc;
|
|
|
|
SBezier sb = SBezier::From(
|
|
|
|
Transform(data, data->px, data->py),
|
|
|
|
Transform(data, c1->x, c1->y),
|
|
|
|
Transform(data, c2->x, c2->y),
|
|
|
|
Transform(data, p->x, p->y));
|
|
|
|
data->beziers->l.Add(&sb);
|
|
|
|
data->px = p->x;
|
|
|
|
data->py = p->y;
|
|
|
|
return 0;
|
|
|
|
}
|
2008-06-30 17:09:17 +08:00
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
void TtfFont::PlotString(const std::string &str,
|
|
|
|
SBezierList *sbl, Vector origin, Vector u, Vector v)
|
|
|
|
{
|
2016-05-19 06:51:36 +08:00
|
|
|
ssassert(fontFace != NULL, "Expected font face to be loaded");
|
2016-05-07 12:20:06 +08:00
|
|
|
|
2016-08-01 20:27:29 +08:00
|
|
|
FT_Outline_Funcs outlineFuncs;
|
|
|
|
outlineFuncs.move_to = MoveTo;
|
|
|
|
outlineFuncs.line_to = LineTo;
|
|
|
|
outlineFuncs.conic_to = ConicTo;
|
|
|
|
outlineFuncs.cubic_to = CubicTo;
|
|
|
|
outlineFuncs.shift = 0;
|
|
|
|
outlineFuncs.delta = 0;
|
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
FT_Pos dx = 0;
|
2016-11-02 16:40:38 +08:00
|
|
|
for(char32_t cid : ReadUTF8(str)) {
|
|
|
|
uint32_t gid = FT_Get_Char_Index(fontFace, cid);
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
if (gid == 0) {
|
|
|
|
dbp("freetype: CID-to-GID mapping for CID 0x%04x failed: %s; using CID as GID",
|
2016-11-02 16:40:38 +08:00
|
|
|
cid, ft_error_string(gid));
|
|
|
|
gid = cid;
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
}
|
2015-03-29 08:30:52 +08:00
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
/*
|
|
|
|
* Stupid hacks:
|
|
|
|
* - if we want fake-bold, use FT_Outline_Embolden(). This actually looks
|
|
|
|
* quite good.
|
|
|
|
* - if we want fake-italic, apply a shear transform [1 s s 1 0 0] here using
|
|
|
|
* FT_Set_Transform. This looks decent at small font sizes and bad at larger
|
|
|
|
* ones, antialiasing mitigates this considerably though.
|
|
|
|
*/
|
|
|
|
if(int fterr = FT_Load_Glyph(fontFace, gid, FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING)) {
|
2016-11-02 16:59:33 +08:00
|
|
|
dbp("freetype: cannot load glyph for GID 0x%04x: %s",
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
gid, ft_error_string(fterr));
|
|
|
|
return;
|
|
|
|
}
|
2008-06-30 17:09:17 +08:00
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
/* A point that has x = xMin should be plotted at (dx0 + lsb); fix up
|
|
|
|
* our x-position so that the curve-generating code will put stuff
|
|
|
|
* at the right place.
|
|
|
|
*
|
|
|
|
* There's no point in getting the glyph BBox here - not only can it be
|
|
|
|
* needlessly slow sometimes, but because we're about to render a single glyph,
|
|
|
|
* what we want actually *is* the CBox.
|
|
|
|
*
|
|
|
|
* This is notwithstanding that this makes extremely little sense, this
|
|
|
|
* looks like a workaround for either mishandling the start glyph on a line,
|
|
|
|
* or as a really hacky pseudo-track-kerning (in which case it works better than
|
|
|
|
* one would expect! especially since most fonts don't set track kerning).
|
|
|
|
*/
|
|
|
|
FT_BBox cbox;
|
|
|
|
FT_Outline_Get_CBox(&fontFace->glyph->outline, &cbox);
|
|
|
|
FT_Pos bx = dx - cbox.xMin;
|
|
|
|
// Yes, this is what FreeType calls left-side bearing.
|
|
|
|
// Then interchangeably uses that with "left-side bearing". Sigh.
|
|
|
|
bx += fontFace->glyph->metrics.horiBearingX;
|
|
|
|
|
|
|
|
OutlineData data = {};
|
|
|
|
data.origin = origin;
|
|
|
|
data.u = u;
|
|
|
|
data.v = v;
|
|
|
|
data.beziers = sbl;
|
2017-03-11 03:39:55 +08:00
|
|
|
data.factor = (float)(1.0 / capHeight);
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
data.bx = bx;
|
2016-08-01 20:27:29 +08:00
|
|
|
if(int fterr = FT_Outline_Decompose(&fontFace->glyph->outline, &outlineFuncs, &data)) {
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
dbp("freetype: bezier decomposition failed (gid %d): %s",
|
|
|
|
gid, ft_error_string(fterr));
|
|
|
|
}
|
2008-06-30 17:09:17 +08:00
|
|
|
|
Rewrite TTF to Bezier conversion using Freetype.
Benefits:
* Much simpler code.
* Handles the entire TTF spec, not just a small subset that
only really worked well on Windows fonts.
* Handles all character sets as well as accented characters.
* Much faster parsing, since Freetype lazily loads and
caches glyphs.
* Support for basically every kind of font that was invented,
not just TTF.
Note that OpenType features, e.g. ligatures, are not yet supported.
This means that Arabic and Devanagari scripts, among others, will
not be rendered in their proper form.
RTL scripts are not supported either, neither in TTF nor in
the text window. Adding RTL support is comparatively easy, but
given that Arabic would not be legibly rendered anyway, this is not
done so far.
2016-01-30 09:42:44 +08:00
|
|
|
// And we're done, so advance our position by the requested advance
|
|
|
|
// width, plus the user-requested extra advance.
|
|
|
|
dx += fontFace->glyph->advance.x;
|
2008-06-30 17:09:17 +08:00
|
|
|
}
|
|
|
|
}
|
2016-10-11 09:58:04 +08:00
|
|
|
|
|
|
|
double TtfFont::AspectRatio(const std::string &str) {
|
|
|
|
ssassert(fontFace != NULL, "Expected font face to be loaded");
|
|
|
|
|
|
|
|
// We always request a unit size character, so the aspect ratio is the same as advance length.
|
|
|
|
double dx = 0;
|
|
|
|
for(char32_t chr : ReadUTF8(str)) {
|
|
|
|
uint32_t gid = FT_Get_Char_Index(fontFace, chr);
|
|
|
|
if (gid == 0) {
|
|
|
|
dbp("freetype: CID-to-GID mapping for CID 0x%04x failed: %s; using CID as GID",
|
|
|
|
chr, ft_error_string(gid));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(int fterr = FT_Load_Glyph(fontFace, gid, FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING)) {
|
|
|
|
dbp("freetype: cannot load glyph (GID 0x%04x): %s",
|
|
|
|
gid, ft_error_string(fterr));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dx += (double)fontFace->glyph->advance.x / capHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dx;
|
|
|
|
}
|