Add a built-in Bitstream Vera Sans Roman font.

Before this commit, the default font chosen for TTF text is Arial
(chosen by the basename of arial.ttf), which isn't present on most
Linux systems, and cannot be redistributed. After this commit, it is
replaced with Bitstream Vera Sans, which can be. Existing files
are not affected.

The font name in the TTF file was artificially modified to add
the (built-in) suffix, which will need to be done if more built-in
fonts are added.
This commit is contained in:
whitequark 2018-07-12 18:58:44 +00:00
parent e69478e61a
commit 94b26ddfac
7 changed files with 120 additions and 7 deletions

View File

@ -69,6 +69,8 @@ Other new features:
the entity from a request is selected. For example, dragging a point on the entity from a request is selected. For example, dragging a point on
a face of an extrusion coincident with the source sketch plane will a face of an extrusion coincident with the source sketch plane will
drag the point from the source sketch. drag the point from the source sketch.
* The default font for TTF text is now Bitstream Vera Sans, which is
included in the resources such that it is available on any OS.
* In expressions, numbers can contain the digit group separator, "_". * In expressions, numbers can contain the digit group separator, "_".
* The "=" key is bound to "Zoom In", like "+" key. * The "=" key is bound to "Zoom In", like "+" key.
* The numpad decimal separator key is bound to "." regardless of locale. * The numpad decimal separator key is bound to "." regardless of locale.

50
THIRD_PARTIES.txt Normal file
View File

@ -0,0 +1,50 @@
This file outlines third party licenses that apply to the files and resources
listed with them. All file paths are relative to the SolveSpace project root.
When redistributing SolveSpace, make sure to also reproduce this file such that
the paths indicating the scope of each license remain intact.
================================================================================
Bitstream Vera, ./res/fonts/BitstreamVeraSans-Roman-builtin.ttf
~
Copyright (c) 2003 by Bitstream, Inc. All Rights Reserved. Bitstream Vera is a
trademark of Bitstream, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of
the fonts accompanying this license (“Fonts”) and associated documentation files
(the “Font Software”), to reproduce and distribute the Font Software, including
without limitation the rights to use, copy, merge, publish, distribute, and/or
sell copies of the Font Software, and to permit persons to whom the Font
Software is furnished to do so, subject to the following conditions:
The above copyright and trademark notices and this permission notice shall be
included in all copies of one or more of the Font Software typefaces.
The Font Software may be modified, altered, or added to, and in particular the
designs of glyphs or characters in the Fonts may be modified and additional
glyphs or characters may be added to the Fonts, only if the fonts are renamed to
names not containing either the words “Bitstream” or the word “Vera”.
This License becomes null and void to the extent applicable to Fonts or Font
Software that has been modified and is distributed under the “Bitstream Vera”
names.
The Font Software may be sold as part of a larger software package but no copy
of one or more of the Font Software typefaces may be sold by itself.
THE FONT SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, TRADEMARK, OR
OTHER RIGHT. IN NO EVENT SHALL BITSTREAM OR THE GNOME FOUNDATION BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT,
INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR
FROM OTHER DEALINGS IN THE FONT SOFTWARE.
Except as contained in this notice, the names of GNOME, the GNOME Foundation,
and Bitstream Inc., shall not be used in advertising or otherwise to promote the
sale, use or other dealings in this Font Software without prior written
authorization from the GNOME Foundation or Bitstream Inc., respectively. For
further information, contact: fonts at gnome dot org.
-----------

View File

@ -212,6 +212,7 @@ add_resources(
fonts/private/6-stipple-dash.png fonts/private/6-stipple-dash.png
fonts/private/7-stipple-zigzag.png fonts/private/7-stipple-zigzag.png
fonts/unicode.lff.gz fonts/unicode.lff.gz
fonts/BitstreamVeraSans-Roman-builtin.ttf
shaders/imesh.frag shaders/imesh.frag
shaders/imesh.vert shaders/imesh.vert
shaders/imesh_point.frag shaders/imesh_point.frag

Binary file not shown.

View File

@ -1107,7 +1107,7 @@ void GraphicsWindow::MouseLeftDown(double mx, double my) {
AddToPending(hr); AddToPending(hr);
Request *r = SK.GetRequest(hr); Request *r = SK.GetRequest(hr);
r->str = "Abc"; r->str = "Abc";
r->font = "arial.ttf"; r->font = "BitstreamVeraSans-Roman-builtin.ttf";
SK.GetEntity(hr.entity(1))->PointForceTo(v); SK.GetEntity(hr.entity(1))->PointForceTo(v);
SK.GetEntity(hr.entity(2))->PointForceTo(v); SK.GetEntity(hr.entity(2))->PointForceTo(v);

View File

@ -63,6 +63,14 @@ void TtfFontList::LoadAll() {
l.Add(&tf); l.Add(&tf);
} }
// 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);
}
// Sort fonts according to their actual name, not filename. // Sort fonts according to their actual name, not filename.
std::sort(&l.elem[0], &l.elem[l.n], std::sort(&l.elem[0], &l.elem[l.n],
[](const TtfFont &a, const TtfFont &b) { return a.name < b.name; }); [](const TtfFont &a, const TtfFont &b) { return a.name < b.name; });
@ -84,11 +92,14 @@ TtfFont *TtfFontList::LoadFont(const std::string &font)
LoadAll(); LoadAll();
TtfFont *tf = std::find_if(l.begin(), l.end(), TtfFont *tf = std::find_if(l.begin(), l.end(),
[&](const TtfFont &tf) { return tf.FontFileBaseName() == font; }); [&font](const TtfFont &tf) { return tf.FontFileBaseName() == font; });
if(tf != l.end()) { if(tf != l.end()) {
if(tf->fontFace == NULL) { if(tf->fontFace == NULL) {
tf->LoadFromFile(fontLibrary, /*nameOnly=*/false); if(tf->IsResource())
tf->LoadFromResource(fontLibrary, /*nameOnly=*/false);
else
tf->LoadFromFile(fontLibrary, /*nameOnly=*/false);
} }
return tf; return tf;
} else { } else {
@ -131,11 +142,22 @@ std::string TtfFont::FontFileBaseName() const {
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Load a TrueType font into memory. We care about the curves that define // Convenience method to set fontFile for resource-loaded fonts as res://<path>
// the letter shapes, and about the mappings that determine which glyph goes //-----------------------------------------------------------------------------
// with which character. 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.
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool TtfFont::LoadFromFile(FT_Library fontLibrary, bool nameOnly) { bool TtfFont::LoadFromFile(FT_Library fontLibrary, bool nameOnly) {
ssassert(!IsResource(), "Cannot load a font provided by a resource as a file.");
FT_Open_Args args = {}; FT_Open_Args args = {};
args.flags = FT_OPEN_PATHNAME; args.flags = FT_OPEN_PATHNAME;
args.pathname = &fontFile.raw[0]; // FT_String is char* for historical reasons args.pathname = &fontFile.raw[0]; // FT_String is char* for historical reasons
@ -149,6 +171,38 @@ bool TtfFont::LoadFromFile(FT_Library fontLibrary, bool nameOnly) {
return false; return false;
} }
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) {
if(int fterr = FT_Select_Charmap(fontFace, FT_ENCODING_UNICODE)) { if(int fterr = FT_Select_Charmap(fontFace, FT_ENCODING_UNICODE)) {
dbp("freetype: loading unicode CMap for file '%s' failed: %s", dbp("freetype: loading unicode CMap for file '%s' failed: %s",
fontFile.raw.c_str(), ft_error_string(fterr)); fontFile.raw.c_str(), ft_error_string(fterr));

View File

@ -11,17 +11,23 @@
class TtfFont { class TtfFont {
public: public:
Platform::Path fontFile; Platform::Path fontFile; // or resource path/name as res://<path>
std::string name; std::string name;
FT_FaceRec_ *fontFace; FT_FaceRec_ *fontFace;
double capHeight; double capHeight;
void SetResourceID(const std::string &resource);
bool IsResource() const;
std::string FontFileBaseName() const; std::string FontFileBaseName() const;
bool LoadFromFile(FT_LibraryRec_ *fontLibrary, bool nameOnly = true); bool LoadFromFile(FT_LibraryRec_ *fontLibrary, bool nameOnly = true);
bool LoadFromResource(FT_LibraryRec_ *fontLibrary, bool nameOnly = true);
void PlotString(const std::string &str, void PlotString(const std::string &str,
SBezierList *sbl, Vector origin, Vector u, Vector v); SBezierList *sbl, Vector origin, Vector u, Vector v);
double AspectRatio(const std::string &str); double AspectRatio(const std::string &str);
bool ExtractTTFData(bool nameOnly);
}; };
class TtfFontList { class TtfFontList {