diff --git a/CHANGELOG.md b/CHANGELOG.md index 72ed729..c31c1e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,8 @@ Other new features: 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 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, "_". * The "=" key is bound to "Zoom In", like "+" key. * The numpad decimal separator key is bound to "." regardless of locale. diff --git a/THIRD_PARTIES.txt b/THIRD_PARTIES.txt new file mode 100644 index 0000000..ad04bf6 --- /dev/null +++ b/THIRD_PARTIES.txt @@ -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. + + ----------- diff --git a/res/CMakeLists.txt b/res/CMakeLists.txt index 35287f5..0b51b47 100644 --- a/res/CMakeLists.txt +++ b/res/CMakeLists.txt @@ -212,6 +212,7 @@ add_resources( fonts/private/6-stipple-dash.png fonts/private/7-stipple-zigzag.png fonts/unicode.lff.gz + fonts/BitstreamVeraSans-Roman-builtin.ttf shaders/imesh.frag shaders/imesh.vert shaders/imesh_point.frag diff --git a/res/fonts/BitstreamVeraSans-Roman-builtin.ttf b/res/fonts/BitstreamVeraSans-Roman-builtin.ttf new file mode 100644 index 0000000..550fe36 Binary files /dev/null and b/res/fonts/BitstreamVeraSans-Roman-builtin.ttf differ diff --git a/src/mouse.cpp b/src/mouse.cpp index 5c7844e..9f145b5 100644 --- a/src/mouse.cpp +++ b/src/mouse.cpp @@ -1107,7 +1107,7 @@ void GraphicsWindow::MouseLeftDown(double mx, double my) { AddToPending(hr); Request *r = SK.GetRequest(hr); r->str = "Abc"; - r->font = "arial.ttf"; + r->font = "BitstreamVeraSans-Roman-builtin.ttf"; SK.GetEntity(hr.entity(1))->PointForceTo(v); SK.GetEntity(hr.entity(2))->PointForceTo(v); diff --git a/src/ttf.cpp b/src/ttf.cpp index bd257d1..3dc44c2 100644 --- a/src/ttf.cpp +++ b/src/ttf.cpp @@ -63,6 +63,14 @@ void TtfFontList::LoadAll() { 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. std::sort(&l.elem[0], &l.elem[l.n], [](const TtfFont &a, const TtfFont &b) { return a.name < b.name; }); @@ -84,11 +92,14 @@ TtfFont *TtfFontList::LoadFont(const std::string &font) LoadAll(); 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->fontFace == NULL) { - tf->LoadFromFile(fontLibrary, /*nameOnly=*/false); + if(tf->IsResource()) + tf->LoadFromResource(fontLibrary, /*nameOnly=*/false); + else + tf->LoadFromFile(fontLibrary, /*nameOnly=*/false); } return tf; } else { @@ -131,11 +142,22 @@ std::string TtfFont::FontFileBaseName() const { } //----------------------------------------------------------------------------- -// Load a TrueType font into memory. We care about the curves that define -// the letter shapes, and about the mappings that determine which glyph goes -// with which character. +// Convenience method to set fontFile for resource-loaded fonts as res:// +//----------------------------------------------------------------------------- +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) { + ssassert(!IsResource(), "Cannot load a font provided by a resource as a file."); + FT_Open_Args args = {}; args.flags = FT_OPEN_PATHNAME; 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 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(_size); + const FT_Byte *buffer = reinterpret_cast(_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)) { dbp("freetype: loading unicode CMap for file '%s' failed: %s", fontFile.raw.c_str(), ft_error_string(fterr)); diff --git a/src/ttf.h b/src/ttf.h index 2eea016..0f1ce70 100644 --- a/src/ttf.h +++ b/src/ttf.h @@ -11,17 +11,23 @@ class TtfFont { public: - Platform::Path fontFile; + Platform::Path fontFile; // or resource path/name as res:// std::string name; FT_FaceRec_ *fontFace; double capHeight; + void SetResourceID(const std::string &resource); + bool IsResource() const; + std::string FontFileBaseName() const; bool LoadFromFile(FT_LibraryRec_ *fontLibrary, bool nameOnly = true); + bool LoadFromResource(FT_LibraryRec_ *fontLibrary, bool nameOnly = true); void PlotString(const std::string &str, SBezierList *sbl, Vector origin, Vector u, Vector v); double AspectRatio(const std::string &str); + + bool ExtractTTFData(bool nameOnly); }; class TtfFontList {