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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <memory>
21#include <vector>
22
23#include <wx/filename.h>
24
26
27#include <font/font.h>
28#include <font/glyph.h>
29#include <font/outline_font.h>
30
31BOOST_AUTO_TEST_SUITE( OutlineFont )
32
33
34static wxString getTestFontPath( const wxString& aFileName )
35{
36 wxFileName fontFile( KI_TEST::GetTestDataRootDir() );
37 fontFile.RemoveLastDir();
38 fontFile.AppendDir( wxT( "resources" ) );
39 fontFile.AppendDir( wxT( "fonts" ) );
40 fontFile.SetFullName( aFileName );
41
42 return fontFile.GetFullPath();
43}
44
45
46// Legacy "symbol" fonts place their glyphs in the U+F000..U+F0FF private-use range and carry no
47// Unicode charmap that maps Basic Latin, so forcing the Unicode charmap maps every character to
48// .notdef and text renders as tofu boxes. Drive the full load-and-shape path and confirm such a
49// font resolves the symbol charmap and produces distinct ASCII glyphs.
50BOOST_AUTO_TEST_CASE( SymbolFontRendersAsciiGlyphs )
51{
52 wxString fontPath = getTestFontPath( wxT( "symbol_pua_test.ttf" ) );
53 BOOST_REQUIRE( wxFileExists( fontPath ) );
54
55 std::vector<wxString> embeddedFonts{ fontPath };
56 KIFONT::FONT* font = KIFONT::FONT::GetFont( wxT( "KiCadSymbolTest" ), false, false,
57 &embeddedFonts );
58
59 BOOST_REQUIRE( font );
60 BOOST_REQUIRE( font->IsOutline() );
61
62 KIFONT::OUTLINE_FONT* outline = static_cast<KIFONT::OUTLINE_FONT*>( font );
63 FT_Face face = outline->GetFace();
64
65 // A Unicode charmap would mean fontconfig substituted a different font, or the symbol charmap
66 // was not selected; either way the glyph lookup that resolves the symbol layout would not run.
67 BOOST_REQUIRE( face );
68 BOOST_REQUIRE( face->charmap );
69 BOOST_CHECK_EQUAL( face->charmap->encoding, FT_ENCODING_MS_SYMBOL );
70
71 std::vector<std::unique_ptr<KIFONT::GLYPH>> glyphs;
72 outline->GetTextAsGlyphs( nullptr, &glyphs, wxT( "AB" ), VECTOR2I( 10000, 10000 ),
73 VECTOR2I( 0, 0 ), ANGLE_0, false, VECTOR2I( 0, 0 ), 0 );
74
75 BOOST_REQUIRE_EQUAL( glyphs.size(), 2 );
76
77 // The test font draws 'A' wider than 'B'; tofu would map both to the same .notdef glyph.
78 double widthA = glyphs[0]->BoundingBox().GetWidth();
79 double widthB = glyphs[1]->BoundingBox().GetWidth();
80
81 BOOST_CHECK_GT( widthA, 0.0 );
82 BOOST_CHECK_GT( widthB, 0.0 );
83 BOOST_CHECK_GT( widthA, widthB );
84}
85
86
87// A normal Unicode font must keep its Unicode charmap and still map Basic Latin directly.
88BOOST_AUTO_TEST_CASE( UnicodeFontKeepsUnicodeCharmap )
89{
90 wxString fontPath = getTestFontPath( wxT( "NotoSans-Regular.ttf" ) );
91 BOOST_REQUIRE( wxFileExists( fontPath ) );
92
93 std::vector<wxString> embeddedFonts{ fontPath };
94 KIFONT::FONT* font = KIFONT::FONT::GetFont( wxT( "Noto Sans" ), false, false,
95 &embeddedFonts );
96
97 BOOST_REQUIRE( font );
98 BOOST_REQUIRE( font->IsOutline() );
99
100 KIFONT::OUTLINE_FONT* outline = static_cast<KIFONT::OUTLINE_FONT*>( font );
101 FT_Face face = outline->GetFace();
102
103 BOOST_REQUIRE( face );
104 BOOST_REQUIRE( face->charmap );
105 BOOST_CHECK_EQUAL( face->charmap->encoding, FT_ENCODING_UNICODE );
106 BOOST_CHECK( FT_Get_Char_Index( face, 'A' ) != 0 );
107}
108
109
FONT is an abstract base class for both outline and stroke fonts.
Definition font.h:94
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:143
virtual bool IsOutline() const
Definition font.h:102
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:683