KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_outline_font.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <memory>
25#include <vector>
26
27#include <wx/filename.h>
28
30
31#include <font/font.h>
32#include <font/glyph.h>
33#include <font/outline_font.h>
34
35BOOST_AUTO_TEST_SUITE( OutlineFont )
36
37
38static wxString getTestFontPath( const wxString& aFileName )
39{
40 wxFileName fontFile( KI_TEST::GetTestDataRootDir() );
41 fontFile.RemoveLastDir();
42 fontFile.AppendDir( wxT( "resources" ) );
43 fontFile.AppendDir( wxT( "fonts" ) );
44 fontFile.SetFullName( aFileName );
45
46 return fontFile.GetFullPath();
47}
48
49
50// Legacy "symbol" fonts place their glyphs in the U+F000..U+F0FF private-use range and carry no
51// Unicode charmap that maps Basic Latin, so forcing the Unicode charmap maps every character to
52// .notdef and text renders as tofu boxes. Drive the full load-and-shape path and confirm such a
53// font resolves the symbol charmap and produces distinct ASCII glyphs.
54BOOST_AUTO_TEST_CASE( SymbolFontRendersAsciiGlyphs )
55{
56 wxString fontPath = getTestFontPath( wxT( "symbol_pua_test.ttf" ) );
57 BOOST_REQUIRE( wxFileExists( fontPath ) );
58
59 std::vector<wxString> embeddedFonts{ fontPath };
60 KIFONT::FONT* font = KIFONT::FONT::GetFont( wxT( "KiCadSymbolTest" ), false, false,
61 &embeddedFonts );
62
63 BOOST_REQUIRE( font );
64 BOOST_REQUIRE( font->IsOutline() );
65
66 KIFONT::OUTLINE_FONT* outline = static_cast<KIFONT::OUTLINE_FONT*>( font );
67 FT_Face face = outline->GetFace();
68
69 // A Unicode charmap would mean fontconfig substituted a different font, or the symbol charmap
70 // was not selected; either way the glyph lookup that resolves the symbol layout would not run.
71 BOOST_REQUIRE( face );
72 BOOST_REQUIRE( face->charmap );
73 BOOST_CHECK_EQUAL( face->charmap->encoding, FT_ENCODING_MS_SYMBOL );
74
75 std::vector<std::unique_ptr<KIFONT::GLYPH>> glyphs;
76 outline->GetTextAsGlyphs( nullptr, &glyphs, wxT( "AB" ), VECTOR2I( 10000, 10000 ),
77 VECTOR2I( 0, 0 ), ANGLE_0, false, VECTOR2I( 0, 0 ), 0 );
78
79 BOOST_REQUIRE_EQUAL( glyphs.size(), 2 );
80
81 // The test font draws 'A' wider than 'B'; tofu would map both to the same .notdef glyph.
82 double widthA = glyphs[0]->BoundingBox().GetWidth();
83 double widthB = glyphs[1]->BoundingBox().GetWidth();
84
85 BOOST_CHECK_GT( widthA, 0.0 );
86 BOOST_CHECK_GT( widthB, 0.0 );
87 BOOST_CHECK_GT( widthA, widthB );
88}
89
90
91// A normal Unicode font must keep its Unicode charmap and still map Basic Latin directly.
92BOOST_AUTO_TEST_CASE( UnicodeFontKeepsUnicodeCharmap )
93{
94 wxString fontPath = getTestFontPath( wxT( "NotoSans-Regular.ttf" ) );
95 BOOST_REQUIRE( wxFileExists( fontPath ) );
96
97 std::vector<wxString> embeddedFonts{ fontPath };
98 KIFONT::FONT* font = KIFONT::FONT::GetFont( wxT( "Noto Sans" ), false, false,
99 &embeddedFonts );
100
101 BOOST_REQUIRE( font );
102 BOOST_REQUIRE( font->IsOutline() );
103
104 KIFONT::OUTLINE_FONT* outline = static_cast<KIFONT::OUTLINE_FONT*>( font );
105 FT_Face face = outline->GetFace();
106
107 BOOST_REQUIRE( face );
108 BOOST_REQUIRE( face->charmap );
109 BOOST_CHECK_EQUAL( face->charmap->encoding, FT_ENCODING_UNICODE );
110 BOOST_CHECK( FT_Get_Char_Index( face, 'A' ) != 0 );
111}
112
113
FONT is an abstract base class for both outline and stroke fonts.
Definition font.h:98
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false, const std::vector< wxString > *aEmbeddedFiles=nullptr, bool aForDrawingSheet=false)
Definition font.cpp:147
virtual bool IsOutline() const
Definition font.h:106
Class OUTLINE_FONT implements outline font drawing.
VECTOR2I GetTextAsGlyphs(BOX2I *aBoundingBox, std::vector< std::unique_ptr< GLYPH > > *aGlyphs, const wxString &aText, const VECTOR2I &aSize, const VECTOR2I &aPosition, const EDA_ANGLE &aAngle, bool aMirror, const VECTOR2I &aOrigin, TEXT_STYLE_FLAGS aTextStyle) const override
Convert text string to an array of GLYPHs.
const FT_Face & GetFace() const
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:411
std::string GetTestDataRootDir()
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
static wxString getTestFontPath(const wxString &aFileName)
BOOST_AUTO_TEST_CASE(SymbolFontRendersAsciiGlyphs)
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687