Skip to content

Commit a8c035f

Browse files
committed
Revert "select fonts via postscript name on Linux"
This reverts commit ddce10f.
1 parent f5894db commit a8c035f

File tree

2 files changed

+8
-63
lines changed

2 files changed

+8
-63
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ This release notably changes to using N-API. 🎉
3030
* Add Node.js v20 to CI. (#2237)
3131
* Replaced `dtslint` with `tsd` (#2313)
3232
* Changed PNG consts to static properties of Canvas class
33+
* Reverted improved font matching on Linux (#1572) because it doesn't work if fonts are installed. If you experience degraded font selection, please file an issue and use v3.0.0-rc3 in the meantime.
34+
3335
### Added
3436
* Added string tags to support class detection
3537
* Throw Cairo errors in canvas.toBuffer()

src/register_font.cc

+6-63
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "register_font.h"
22

3-
#include <string>
43
#include <pango/pangocairo.h>
54
#include <pango/pango-fontmap.h>
65
#include <pango/pango.h>
@@ -12,7 +11,6 @@
1211
#include <memory>
1312
#else
1413
#include <fontconfig/fontconfig.h>
15-
#include <pango/pangofc-fontmap.h>
1614
#endif
1715

1816
#include <ft2build.h>
@@ -35,29 +33,11 @@
3533
#define PREFERRED_ENCODING_ID TT_MS_ID_UNICODE_CS
3634
#endif
3735

38-
// With PangoFcFontMaps (the pango font module on Linux) we're able to add a
39-
// hook that lets us get perfect matching. Tie the conditions for enabling that
40-
// feature to one variable
41-
#if !defined(__APPLE__) && !defined(_WIN32) && PANGO_VERSION_CHECK(1, 47, 0)
42-
#define PERFECT_MATCHES_ENABLED
43-
#endif
44-
4536
#define IS_PREFERRED_ENC(X) \
4637
X.platform_id == PREFERRED_PLATFORM_ID && X.encoding_id == PREFERRED_ENCODING_ID
4738

48-
#ifdef PERFECT_MATCHES_ENABLED
49-
// On Linux-like OSes using FontConfig, the PostScript name ranks higher than
50-
// preferred family and family name since we'll use it to get perfect font
51-
// matching (see fc_font_map_substitute_hook)
52-
#define GET_NAME_RANK(X) \
53-
((IS_PREFERRED_ENC(X) ? 1 : 0) << 2) | \
54-
((X.name_id == TT_NAME_ID_PS_NAME ? 1 : 0) << 1) | \
55-
(X.name_id == TT_NAME_ID_PREFERRED_FAMILY ? 1 : 0)
56-
#else
5739
#define GET_NAME_RANK(X) \
58-
((IS_PREFERRED_ENC(X) ? 1 : 0) << 1) | \
59-
(X.name_id == TT_NAME_ID_PREFERRED_FAMILY ? 1 : 0)
60-
#endif
40+
(IS_PREFERRED_ENC(X) ? 1 : 0) + (X.name_id == TT_NAME_ID_PREFERRED_FAMILY ? 1 : 0)
6141

6242
/*
6343
* Return a UTF-8 encoded string given a TrueType name buf+len
@@ -125,31 +105,15 @@ get_family_name(FT_Face face) {
125105
for (unsigned i = 0; i < FT_Get_Sfnt_Name_Count(face); ++i) {
126106
FT_Get_Sfnt_Name(face, i, &name);
127107

128-
if (
129-
name.name_id == TT_NAME_ID_FONT_FAMILY ||
130-
#ifdef PERFECT_MATCHES_ENABLED
131-
name.name_id == TT_NAME_ID_PS_NAME ||
132-
#endif
133-
name.name_id == TT_NAME_ID_PREFERRED_FAMILY
134-
) {
135-
int rank = GET_NAME_RANK(name);
108+
if (name.name_id == TT_NAME_ID_FONT_FAMILY || name.name_id == TT_NAME_ID_PREFERRED_FAMILY) {
109+
char *buf = to_utf8(name.string, name.string_len, name.platform_id, name.encoding_id);
136110

137-
if (rank > best_rank) {
138-
char *buf = to_utf8(name.string, name.string_len, name.platform_id, name.encoding_id);
139-
if (buf) {
111+
if (buf) {
112+
int rank = GET_NAME_RANK(name);
113+
if (rank > best_rank) {
140114
best_rank = rank;
141115
if (best_buf) free(best_buf);
142116
best_buf = buf;
143-
144-
#ifdef PERFECT_MATCHES_ENABLED
145-
// Prepend an '@' to the postscript name
146-
if (name.name_id == TT_NAME_ID_PS_NAME) {
147-
std::string best_buf_modified = "@";
148-
best_buf_modified += best_buf;
149-
free(best_buf);
150-
best_buf = strdup(best_buf_modified.c_str());
151-
}
152-
#endif
153117
} else {
154118
free(buf);
155119
}
@@ -320,21 +284,6 @@ get_pango_font_description(unsigned char* filepath) {
320284
return NULL;
321285
}
322286

323-
#ifdef PERFECT_MATCHES_ENABLED
324-
static void
325-
fc_font_map_substitute_hook(FcPattern *pat, gpointer data) {
326-
FcChar8 *family;
327-
328-
for (int i = 0; FcPatternGetString(pat, FC_FAMILY, i, &family) == FcResultMatch; i++) {
329-
if (family[0] == '@') {
330-
FcPatternAddString(pat, FC_POSTSCRIPT_NAME, (FcChar8 *)family + 1);
331-
FcPatternRemove(pat, FC_FAMILY, i);
332-
i -= 1;
333-
}
334-
}
335-
}
336-
#endif
337-
338287
/*
339288
* Register font with the OS
340289
*/
@@ -365,12 +314,6 @@ register_font(unsigned char *filepath) {
365314
// font families.
366315
pango_cairo_font_map_set_default(NULL);
367316

368-
#ifdef PERFECT_MATCHES_ENABLED
369-
PangoFontMap* map = pango_cairo_font_map_get_default();
370-
PangoFcFontMap* fc_map = PANGO_FC_FONT_MAP(map);
371-
pango_fc_font_map_set_default_substitute(fc_map, fc_font_map_substitute_hook, NULL, NULL);
372-
#endif
373-
374317
return true;
375318
}
376319

0 commit comments

Comments
 (0)