@@ -486,10 +486,10 @@ NAN_METHOD(Canvas::RegisterFont) {
486486
487487 String::Utf8Value filePath (info[0 ]);
488488
489- if (!register_font ((unsigned char *) *filePath, &face.target_desc )) {
489+ if (!register_font ((unsigned char *) *filePath, &face.sys_desc )) {
490490 Nan::ThrowError (" Could not load font to the system's font host" );
491491 } else {
492- PangoFontDescription* d = pango_font_description_new ();
492+ PangoFontDescription * d = pango_font_description_new ();
493493
494494 if (!info[1 ]->IsObject ()) {
495495 Nan::ThrowError (GENERIC_FACE_ERROR);
@@ -499,9 +499,9 @@ NAN_METHOD(Canvas::RegisterFont) {
499499 Local<String> weight_prop = Nan::New<String>(" weight" ).ToLocalChecked ();
500500 Local<String> style_prop = Nan::New<String>(" style" ).ToLocalChecked ();
501501
502- const char * family;
503- const char * weight = " normal" ;
504- const char * style = " normal" ;
502+ const char * family;
503+ const char * weight = " normal" ;
504+ const char * style = " normal" ;
505505
506506 Local<Value> family_val = desc->Get (family_prop);
507507 if (family_val->IsString ()) {
@@ -535,9 +535,9 @@ NAN_METHOD(Canvas::RegisterFont) {
535535 pango_font_description_set_style (d, Canvas::GetStyleFromCSSString (style));
536536 pango_font_description_set_family (d, family);
537537
538- free ((char *)family);
539- if (desc->HasOwnProperty (weight_prop)) free ((char *)weight);
540- if (desc->HasOwnProperty (style_prop)) free ((char *)style);
538+ free ((char *)family);
539+ if (desc->HasOwnProperty (weight_prop)) free ((char *)weight);
540+ if (desc->HasOwnProperty (style_prop)) free ((char *)style);
541541
542542 face.user_desc = d;
543543 _font_face_list.push_back (face);
@@ -658,31 +658,48 @@ Canvas::GetWeightFromCSSString(const char *weight) {
658658}
659659
660660/*
661- * Tries to find a matching font given to registerFont
661+ * Given a user description, return a description that will select the
662+ * font either from the system or @font-face
662663 */
663664
664665PangoFontDescription *
665- Canvas::FindCustomFace (PangoFontDescription *desc) {
666- PangoFontDescription* best_match = NULL ;
667- PangoFontDescription* best_match_target = NULL ;
668- std::vector<FontFace>::iterator it = _font_face_list.begin ();
669-
670- while (it != _font_face_list.end ()) {
671- FontFace f = *it;
672-
673- if (g_ascii_strcasecmp (pango_font_description_get_family (desc),
674- pango_font_description_get_family (f.user_desc )) == 0 ) {
675-
676- if (best_match == NULL || pango_font_description_better_match (desc, best_match, f.user_desc )) {
677- best_match = f.user_desc ;
678- best_match_target = f.target_desc ;
666+ Canvas::ResolveFontDescription (const PangoFontDescription *desc) {
667+ FontFace best;
668+ PangoFontDescription *ret = NULL ;
669+
670+ // One of the user-specified families could map to multiple SFNT family names
671+ // if someone registered two different fonts under the same family name.
672+ // https://drafts.csswg.org/css-fonts-3/#font-style-matching
673+ char **families = g_strsplit (pango_font_description_get_family (desc), " ," , -1 );
674+ GString *resolved_families = g_string_new (" " );
675+
676+ for (int i = 0 ; families[i]; ++i) {
677+ GString *renamed_families = g_string_new (" " );
678+ std::vector<FontFace>::iterator it = _font_face_list.begin ();
679+
680+ for (; it != _font_face_list.end (); ++it) {
681+ if (g_ascii_strcasecmp (families[i], pango_font_description_get_family (it->user_desc )) == 0 ) {
682+ if (renamed_families->len ) g_string_append (renamed_families, " ," );
683+ g_string_append (renamed_families, pango_font_description_get_family (it->sys_desc ));
684+
685+ if (i == 0 && (best.user_desc == NULL || pango_font_description_better_match (desc, best.user_desc , it->user_desc ))) {
686+ best = *it;
687+ }
679688 }
680689 }
681690
682- ++it;
691+ if (resolved_families->len ) g_string_append (resolved_families, " ," );
692+ g_string_append (resolved_families, renamed_families->len ? renamed_families->str : families[i]);
693+ g_string_free (renamed_families, true );
683694 }
684695
685- return best_match_target;
696+ ret = pango_font_description_copy (best.sys_desc ? best.sys_desc : desc);
697+ pango_font_description_set_family_static (ret, resolved_families->str );
698+
699+ g_strfreev (families);
700+ g_string_free (resolved_families, false );
701+
702+ return ret;
686703}
687704
688705/*
0 commit comments