Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
font-instance.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) 2022 Authors
8 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
9 */
10#ifndef LIBNRTYPE_FONT_INSTANCE_H
11#define LIBNRTYPE_FONT_INSTANCE_H
12
13#include <map>
14#include <optional>
15#include <unordered_map>
16#include <unordered_set>
17
18#include <2geom/pathvector.h>
19#include <cairo/cairo.h> // cairo_font_face_t
20#include <pango/pango-types.h>
21#include <pango/pango-font.h>
22
23#include "font-glyph.h"
24#include "OpenTypeUtil.h"
25#include "style-enums.h"
26
27namespace Inkscape {
28class Pixbuf;
29} // namespace Inkscape
30
43{
44public:
47
49 struct CtorException : std::runtime_error
50 {
51 template <typename... Args>
52 CtorException(Args&&... args) : std::runtime_error(std::forward<Args>(args)...) {}
53 };
54
56
57 unsigned int MapUnicodeChar(gunichar c) const; // calls the relevant unicode->glyph index function
58
59 // Loads the given glyph's info. Glyphs are lazy-loaded, but never unloaded or modified
60 // as long as the FontInstance still exists. Pointers to FontGlyphs also remain valid.
61 FontGlyph const *LoadGlyph(unsigned int glyph_id);
62
63 // nota: all coordinates returned by these functions are on a [0..1] scale; you need to multiply
64 // by the fontsize to get the real sizes
65
66 // Return 2geom pathvector for glyph. Deallocated when font instance dies.
67 Geom::PathVector const *PathVector(unsigned int glyph_id);
68
69 // Various bounding boxes. Color fonts complicate this as some types don't have paths.
70 Geom::Rect BBoxExact(unsigned int glyph_id); // Bounding box of glyph. From Harfbuzz.
71 Geom::Rect BBoxPick( unsigned int glyph_id); // For picking. (Height: embox, Width: glyph advance.)
72 Geom::Rect BBoxDraw( unsigned int glyph_id); // Contains all inked areas including possible text decorations.
73
74 // Return if font has various tables.
75 bool FontHasSVG() const { return has_svg; };
76
77 auto const &get_opentype_varaxes() const { return data->openTypeVarAxes; }
78
79 // Return the font's OpenType tables, possibly loading them on-demand.
80 std::map<Glib::ustring, OTSubstitution> const &get_opentype_tables();
81
82 // Return pixbuf of SVG glyph or nullptr if no SVG glyph exists. As with glyphs, pixbufs
83 // are lazy-loaded but immutable once loaded. They are guaranteed to be in Cairo pixel format.
84 Inkscape::Pixbuf const *PixBuf(unsigned int glyph_id);
85
86 // Horizontal advance if 'vertical' is false, vertical advance if true.
87 double Advance(unsigned int glyph_id, bool vertical);
88
89 // Return a shared pointer that will keep alive the pathvector and pixbuf data, but nothing else.
90 std::shared_ptr<void const> share_data() const { return data; }
91
92 double GetTypoAscent() const { return _ascent; }
93 double GetTypoDescent() const { return _descent; }
94 double GetXHeight() const { return _xheight; }
95 double GetMaxAscent() const { return _ascent_max; }
96 double GetMaxDescent() const { return _descent_max; }
97 const double* GetBaselines() const { return _baselines; }
98 int GetDesignUnits() const { return _design_units; }
99
100 Glib::ustring GetFilename() const;
101
102 bool FontMetrics(double &ascent, double &descent, double &leading) const;
103
104 bool FontDecoration(double &underline_position, double &underline_thickness,
105 double &linethrough_position, double &linethrough_thickness) const;
106
107 bool FontSlope(double &run, double &rise) const; // for generating slanted cursors for oblique fonts
108
109 bool IsOutlineFont() const { return FT_IS_SCALABLE(face); }
110 bool has_vertical() const { return FT_HAS_VERTICAL(face); }
111
112 auto get_descr() const { return descr; }
113 auto get_font() const { return p_font; }
114
115 bool is_fixed_width() const { return _fixed_width; }
116 bool is_oblique() const { return _oblique; }
117 unsigned short family_class() const { return _family_class; }
118
119private:
121 void release();
122 void init_face();
123 void find_font_metrics(); // Find ascent, descent, x-height, and baselines.
124
125 /*
126 * Resources
127 */
128
129 // The font's fingerprint; this particular PangoFontDescription gives the key at which this font instance
130 // resides in the font cache. It may differ from the PangoFontDescription belonging to p_font.
132
133 // The real source of the font
135
136 // We need to keep around an rw copy of the (read-only) hb font to extract the freetype face.
137 hb_font_t *hb_font;
138 hb_font_t *hb_font_copy;
139
140 // Useful for getting metrics without dealing with FreeType archaics.
141 hb_face_t *hb_face;
142
143 // It's a pointer in fact; no worries to ref/unref it, pango does its magic
144 // as long as p_font is valid, face is too.
145 FT_Face face;
146
147 bool has_svg = false; // SVG glyphs
148
149 /*
150 * Metrics
151 */
152
153 // Font metrics in em-box units
154 double _ascent; // Typographic ascent.
155 double _descent; // Typographic descent.
156 double _xheight; // x-height of font.
157 double _ascent_max; // Maximum ascent of all glyphs in font.
158 double _descent_max; // Maximum descent of all glyphs in font.
159 int _design_units; // Design units, (units per em, typically 1000 or 2048).
160 double _italic_angle = 0.0; // angle for oblique fonts, if specified in a font
161 bool _fixed_width = false; // monospaced font (if advertised as such)
162 bool _oblique = false; // oblique or italic font
163 unsigned short _family_class = 0; // OS/2 sFamily class field
164
165 // Baselines
167
168 // Set of OpenType tables in font.
169 std::unordered_set<std::string> openTypeTableList;
170
171 struct Data
172 {
173 /*
174 * Tables
175 */
176
177 // Map of SVG in OpenType entries
178 std::map<int, std::string> openTypeSVGData;
179
180 // Map of SVG in OpenType glyphs
181 std::map<unsigned int, SVGGlyphEntry> openTypeSVGGlyphs;
182
183 // Maps for font variations.
184 std::map<Glib::ustring, OTVarAxis> openTypeVarAxes; // Axes with ranges
185
186 // Map of GSUB OpenType tables found in font. Transparently lazy-loaded.
187 std::optional<std::map<Glib::ustring, OTSubstitution>> openTypeTables;
188
189 /*
190 * Glyphs
191 */
192
193 // Lookup table mapping pango glyph ids to glyphs.
194 std::unordered_map<unsigned int, std::unique_ptr<FontGlyph const>> glyphs;
195 };
196
197 std::shared_ptr<Data> data;
198};
199
200#endif // LIBNRTYPE_FONT_INSTANCE_H
201
202/*
203 Local Variables:
204 mode:c++
205 c-file-style:"stroustrup"
206 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
207 indent-tabs-mode:nil
208 fill-column:99
209 End:
210*/
211// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
_PangoFontDescription PangoFontDescription
Definition Layout-TNG.h:44
TODO: insert short description here.
struct _PangoFont PangoFont
FontInstance provides metrics, OpenType data, and glyph curves/pixbufs for a font.
Glib::ustring GetFilename() const
Attempt to get the ttf filename for this font instance.
Geom::PathVector const * PathVector(unsigned int glyph_id)
hb_face_t * hb_face
std::map< Glib::ustring, OTSubstitution > const & get_opentype_tables()
double _baselines[SP_CSS_BASELINE_SIZE]
PangoFontDescription * descr
std::shared_ptr< Data > data
Geom::Rect BBoxPick(unsigned int glyph_id)
double GetXHeight() const
unsigned short _family_class
bool IsOutlineFont() const
int GetDesignUnits() const
bool has_vertical() const
double _italic_angle
double _descent_max
Geom::Rect BBoxDraw(unsigned int glyph_id)
bool FontSlope(double &run, double &rise) const
Geom::Rect BBoxExact(unsigned int glyph_id)
std::shared_ptr< void const > share_data() const
auto const & get_opentype_varaxes() const
double GetTypoDescent() const
std::unordered_set< std::string > openTypeTableList
unsigned int MapUnicodeChar(gunichar c) const
auto get_descr() const
hb_font_t * hb_font_copy
double GetMaxAscent() const
bool FontDecoration(double &underline_position, double &underline_thickness, double &linethrough_position, double &linethrough_thickness) const
void find_font_metrics()
FontGlyph const * LoadGlyph(unsigned int glyph_id)
const double * GetBaselines() const
bool FontHasSVG() const
Inkscape::Pixbuf const * PixBuf(unsigned int glyph_id)
hb_font_t * hb_font
double Advance(unsigned int glyph_id, bool vertical)
PangoFont * p_font
auto get_font() const
bool is_fixed_width() const
unsigned short family_class() const
bool FontMetrics(double &ascent, double &descent, double &leading) const
double GetMaxDescent() const
bool is_oblique() const
void acquire(PangoFont *p_font, PangoFontDescription *descr)
double GetTypoAscent() const
Sequence of subpaths.
Definition pathvector.h:122
Axis aligned, non-empty rectangle.
Definition rect.h:92
Class to hold image data for raster images.
Definition cairo-utils.h:31
double c[8][4]
Struct describing a single glyph in a font.
Helper class to stream background task notifications as a series of messages.
STL namespace.
PathVector - a sequence of subpaths.
Exception thrown if construction fails.
CtorException(Args &&... args)
std::unordered_map< unsigned int, std::unique_ptr< FontGlyph const > > glyphs
std::map< Glib::ustring, OTVarAxis > openTypeVarAxes
std::map< int, std::string > openTypeSVGData
std::optional< std::map< Glib::ustring, OTSubstitution > > openTypeTables
std::map< unsigned int, SVGGlyphEntry > openTypeSVGGlyphs
SPStyle enums: named public enums that correspond to SVG property values.
@ SP_CSS_BASELINE_SIZE
unsigned int gunichar