Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
font-factory.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
/*
5 * Authors: see git history
6 *
7 * Copyright (C) 2018 Authors
8 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
9 */
10#ifndef LIBNRTYPE_FONT_FACTORY_H
11#define LIBNRTYPE_FONT_FACTORY_H
12
13#include <cstddef>
14#include <functional>
15#include <algorithm>
16#include <glibmm/refptr.h>
17#include <pangomm/fontfamily.h>
18#include <pangomm/fontmap.h>
19#include <utility>
20#include <memory>
21#include <map>
22
23#include <pango/pango.h>
24#include "style.h"
25#include "util/statics.h"
26
27#include <pango/pangoft2.h>
28#include <ft2build.h>
29#include FT_FREETYPE_H
30
31#include "util/cached_map.h"
32
33class FontInstance;
34
35// Constructs a PangoFontDescription from SPStyle. Font size is not included.
36// User must free return value.
38
39// Wraps calls to pango_font_description_get_family with some name substitution
40char const *sp_font_description_get_family(PangoFontDescription const *fontDescr);
41
42// Map a non-existent font name to an existing one.
43std::string getSubstituteFontName(std::string const &font);
44
45// Class for style strings: both CSS and as suggested by font.
47{
48 StyleNames() = default;
49 StyleNames(Glib::ustring name) : StyleNames{name, std::move(name)} {}
50 StyleNames(Glib::ustring cssname, Glib::ustring displayname)
51 : css_name{std::move(cssname)}
52 , display_name{std::move(displayname)}
53 {}
54
55 Glib::ustring css_name; // Style as Pango/CSS would write it.
56 Glib::ustring display_name; // Style as Font designer named it.
57};
58
60 /*
61 * Using statics.h to ensure destruction before main() exits, otherwise Harfbuzz's internal
62 * FreeType instance will come before us in the static destruction order and our destructor will crash.
63 * Related - https://gitlab.com/inkscape/inkscape/-/issues/3765.
64 */
65 : public Inkscape::Util::EnableSingleton<FontFactory>
66{
67public:
68 // Refresh pango font configuration
69 void refreshConfig();
70
72 static constexpr double fontSize = 512;
73
76 Glib::ustring ConstructFontSpecification(FontInstance *font);
77
78 std::vector<std::string> GetAllFontNames();
79
81 Glib::ustring GetUIFamilyString(PangoFontDescription const *fontDescr);
82 Glib::ustring GetUIStyleString(PangoFontDescription const *fontDescr);
83 bool hasFontFamily(std::string const &family);
84
85 // Helpfully returns all font families in a map.
86 std::map<std::string, PangoFontFamily *> GetUIFamilies();
87 // Retrieves style information about a font family.
88 std::vector<StyleNames> GetUIStyles(PangoFontFamily *in);
89
90 std::vector<Glib::RefPtr<Pango::FontFamily>> get_font_families();
91
93 std::shared_ptr<FontInstance> FaceFromStyle(SPStyle const *style);
94 // Various functions to get a FontInstance from different descriptions.
95 std::shared_ptr<FontInstance> FaceFromDescr(char const *family, char const *style);
96 std::shared_ptr<FontInstance> FaceFromUIStrings(char const *uiFamily, char const *uiStyle);
97 std::shared_ptr<FontInstance> FaceFromPangoString(char const *pangoString);
98 std::shared_ptr<FontInstance> FaceFromFontSpecification(char const *fontSpecification);
99 std::shared_ptr<FontInstance> Face(PangoFontDescription *descr, bool canFail = true);
100
101 std::unique_ptr<FontInstance> create_face(PangoFontDescription* descr);
102
103# ifdef _WIN32
104 void AddFontFilesWin32(char const *directory_path);
105# endif
106
108 void AddFontsDir(char const *utf8dir);
109
111 void AddFontFile(char const *utf8file);
112
113 PangoContext *get_font_context() const { return fontContext; }
114 PangoFontDescription *parsePostscriptName(std::string const &name, bool substitute);
115
116protected:
117 FontFactory();
118 ~FontFactory();
119
120private:
121 // Pango data. Backend-specific structures are cast to these opaque types.
122 PangoFontMap *fontServer;
123 PangoContext *fontContext;
124 Glib::RefPtr<Pango::FontMap> _font_map;
125
126 // A hashmap of all the loaded font instances, indexed by their PangoFontDescription.
127 // Note: Since pango already does that, using the PangoFont could work too.
128 struct Hash
129 {
130 size_t operator()(PangoFontDescription const *x) const;
131 };
132 struct Compare
133 {
134 bool operator()(PangoFontDescription const *a, PangoFontDescription const *b) const;
135 };
137
138 // The following two commented out maps were an attempt to allow Inkscape to use font faces
139 // that could not be distinguished by CSS values alone. In practice, they never were that
140 // useful as PangoFontDescription, which is used throughout our code, cannot distinguish
141 // between faces anymore than raw CSS values (with the exception of two additional weight
142 // values).
143 //
144 // During various works, for example to handle font-family lists and fonts that are not
145 // installed on the system, the code has become less reliant on these maps. And in the work to
146 // cache style information to speed up start up times, the maps were not being filled.
147 // I've removed all code that used these maps as of Oct 2014 in the experimental branch.
148 // The commented out maps are left here as a reminder of the path that was attempted.
149 //
150 // One possible method to keep track of font faces would be to use the 'display name', keeping
151 // pointers to the appropriate PangoFontFace. The FontFactory loadedFaces map indexing would
152 // have to be changed to incorporate 'display name' (InkscapeFontDescription?).
153
154
155 // These two maps are used for translating between what's in the UI and a pango
156 // font description. This is necessary because Pango cannot always
157 // reproduce these structures from the names it gave us in the first place.
158
159 // Key: A string produced by FontFactory::ConstructFontSpecification
160 // Value: The associated PangoFontDescription
161 // typedef std::map<Glib::ustring, PangoFontDescription *> PangoStringToDescrMap;
162 // PangoStringToDescrMap fontInstanceMap;
163
164 // Key: Family name in UI + Style name in UI
165 // Value: The associated string that should be produced with FontFactory::ConstructFontSpecification
166 // typedef std::map<Glib::ustring, Glib::ustring> UIStringToPangoStringMap;
167 // UIStringToPangoStringMap fontStringMap;
168};
169
170#endif // LIBNRTYPE_FONT_FACTORY_H
171
172/*
173 Local Variables:
174 mode:c++
175 c-file-style:"stroustrup"
176 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
177 indent-tabs-mode:nil
178 fill-column:99
179 End:
180*/
181// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8 :
_PangoFontDescription PangoFontDescription
Definition Layout-TNG.h:44
An abstract gadget that implements a finite cache for a factory.
std::shared_ptr< FontInstance > FaceFromStyle(SPStyle const *style)
Retrieve a FontInstance from a style object, first trying to use the font-specification,...
std::shared_ptr< FontInstance > FaceFromPangoString(char const *pangoString)
PangoContext * fontContext
PangoFontDescription * parsePostscriptName(std::string const &name, bool substitute)
Use font config to parse the postscript name found in pdf/ps files and return font config family and ...
void AddFontFile(char const *utf8file)
Add a an additional font.
Glib::ustring GetUIFamilyString(PangoFontDescription const *fontDescr)
Returns strings to be used in the UI for family and face (or "style" as the column is labeled)
std::shared_ptr< FontInstance > FaceFromUIStrings(char const *uiFamily, char const *uiStyle)
Glib::ustring ConstructFontSpecification(PangoFontDescription *font)
Constructs a pango string for use with the fontStringMap (see below)
std::shared_ptr< FontInstance > Face(PangoFontDescription *descr, bool canFail=true)
Glib::ustring GetUIStyleString(PangoFontDescription const *fontDescr)
std::vector< std::string > GetAllFontNames()
Returns a list of all font names available in this font config.
Inkscape::Util::cached_map< PangoFontDescription *, FontInstance, Hash, Compare > loaded
std::shared_ptr< FontInstance > FaceFromDescr(char const *family, char const *style)
PangoFontMap * fontServer
std::vector< StyleNames > GetUIStyles(PangoFontFamily *in)
PangoContext * get_font_context() const
void refreshConfig()
The fontsize used as workaround for hinting.
bool hasFontFamily(std::string const &family)
void AddFontsDir(char const *utf8dir)
Add a directory from which to include additional fonts.
std::shared_ptr< FontInstance > FaceFromFontSpecification(char const *fontSpecification)
std::unique_ptr< FontInstance > create_face(PangoFontDescription *descr)
std::vector< Glib::RefPtr< Pango::FontFamily > > get_font_families()
static constexpr double fontSize
std::map< std::string, PangoFontFamily * > GetUIFamilies()
Glib::RefPtr< Pango::FontMap > _font_map
void AddFontFilesWin32(char const *directory_path)
FontInstance provides metrics, OpenType data, and glyph curves/pixbufs for a font.
A cached_map<Tk, Tv> is designed for use by a factory that takes as input keys of type Tk and produce...
Definition cached_map.h:50
An SVG style object.
Definition style.h:45
PangoFontDescription * ink_font_description_from_style(SPStyle const *style)
char const * sp_font_description_get_family(PangoFontDescription const *fontDescr)
std::string getSubstituteFontName(std::string const &font)
STL namespace.
Static objects with destruction before main() exit.
bool operator()(PangoFontDescription const *a, PangoFontDescription const *b) const
size_t operator()(PangoFontDescription const *x) const
Glib::ustring display_name
StyleNames(Glib::ustring name)
StyleNames()=default
Glib::ustring css_name
StyleNames(Glib::ustring cssname, Glib::ustring displayname)
SPStyle - a style object for SPItem objects.
Glib::ustring name
Definition toolbars.cpp:55