KiCad PCB EDA Suite
font_choice.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 (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <widgets/font_choice.h>
21#include <wx/fontenum.h>
22#include <font/fontconfig.h>
23
24// The "official" name of the building Kicad stroke font (always existing)
26
27
28FONT_CHOICE::FONT_CHOICE( wxWindow* aParent, int aId, wxPoint aPosition, wxSize aSize,
29 int nChoices, wxString* aChoices, int aStyle ) :
30 wxChoice( aParent, aId, aPosition, aSize, nChoices, aChoices, aStyle )
31{
32 m_systemFontCount = wxChoice::GetCount();
33
34 std::vector<std::string> fontNames;
35 Fontconfig()->ListFonts( fontNames );
36
37 wxArrayString menuList;
38
39 // The initial list of fonts has on top 1 or 2 options
40 // only "KiCad Font" (KICAD_FONT_NAME)
41 // "Default Font" and "KiCad Font" (KICAD_FONT_NAME)
42 // "KiCad Font" is also a keyword, and cannot be translated.
43 // So rebuilt the starting list
44 wxChoice::Clear();
45
46 if( m_systemFontCount > 1 )
47 Append( _( "Default Font" ) );
48
49 Append( KICAD_FONT_NAME );
50 m_systemFontCount = wxChoice::GetCount();
51
52 for( const std::string& name : fontNames )
53 menuList.Add( wxString( name ) );
54
55 menuList.Sort();
56 Append( menuList );
57
58 m_notFound = wxS( " " ) + _( "<not found>" );
59}
60
61
63{
64}
65
66
68{
69 if( !aFont )
70 {
71 SetSelection( 0 );
72 }
73 else
74 {
75 SetStringSelection( aFont->GetName() );
76
77 if( GetSelection() == wxNOT_FOUND )
78 {
79 Append( aFont->GetName() + m_notFound );
80 SetSelection( GetCount() );
81 }
82 }
83
84 SendSelectionChangedEvent( wxEVT_CHOICE );
85}
86
87
89{
90 int sel = GetSelection();
91
92 if( sel < 0 )
93 return false;
94
95 if( GetString( sel ).EndsWith( m_notFound ) )
96 return false;
97
98 return true;
99}
100
101
102KIFONT::FONT* FONT_CHOICE::GetFontSelection( bool aBold, bool aItalic ) const
103{
104 if( GetSelection() <= 0 )
105 return nullptr;
106 else if( GetSelection() == 1 && m_systemFontCount == 2 )
107 return KIFONT::FONT::GetFont( KICAD_FONT_NAME, aBold, aItalic );
108 else
109 return KIFONT::FONT::GetFont( GetStringSelection(), aBold, aItalic );
110}
111
const char * name
Definition: DXF_plotter.cpp:56
bool HaveFontSelection() const
Definition: font_choice.cpp:88
FONT_CHOICE(wxWindow *aParent, int aId, wxPoint aPosition, wxSize aSize, int nChoices, wxString *aChoices, int aStyle)
Definition: font_choice.cpp:28
int m_systemFontCount
Definition: font_choice.h:43
void SetFontSelection(KIFONT::FONT *aFont)
Definition: font_choice.cpp:67
virtual ~FONT_CHOICE()
Definition: font_choice.cpp:62
wxString m_notFound
Definition: font_choice.h:44
KIFONT::FONT * GetFontSelection(bool aBold, bool aItalic) const
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:105
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false)
Definition: font.cpp:65
const wxString & GetName() const
Definition: font.h:121
#define _(s)
FONTCONFIG * Fontconfig()
Definition: fontconfig.cpp:52
#define KICAD_FONT_NAME