Skip to content

Font Library: Merge the font faces instead of overriding when parsing settings#59119

Closed
arthur791004 wants to merge 1 commit intotrunkfrom
fix/font-library-install-fonts-variant-overrides-theme-fonts
Closed

Font Library: Merge the font faces instead of overriding when parsing settings#59119
arthur791004 wants to merge 1 commit intotrunkfrom
fix/font-library-install-fonts-variant-overrides-theme-fonts

Conversation

@arthur791004
Copy link
Copy Markdown
Contributor

What?

This PR is to combine the font faces provided by ALL origins instead of overriding.

Why?

Referring to #58764 (comment), the installed font variants override the font variants provided by the theme. For example, if a theme provides a Cardo font with 400, and 500 font variants and people install a new Cardo font with a 600 font variant, then only the Cardo font with a 600 font variant will take effect. As a result, we have to merge the font faces across different origins to prevent this issue.

How?

Instead of overriding, now we check whether the new font face exists or not. If it exists, we will merge the new one into the existing font face. If not, we just add it to the array.

Testing Instructions

  • Make this change take effect by commenting out the core files
    • Run wp-env run cli bash
    • Open the wp-settings.php
    • Comment out the following lines
      // require ABSPATH . WPINC . '/fonts/class-wp-font-face-resolver.php';
      // require ABSPATH . WPINC . '/fonts/class-wp-font-face.php';           
      // require ABSPATH . WPINC . '/fonts.php';      
  • Open a site with TT4
  • Go to the Editor
  • Create the following paragraph:
    • Cardo Normal. Set the Font Family to Cardo, and the Appearance to “Regular”
    • Cardo Bold. Set the Font Family to Cardo, and the Appearance to “Bold”
    • Cardo Normal Italic. Set the Font Family to Cardo, and the Appearance to “Regular Italic”
  • Install the Cardo font with 400 font variant from the Install Fonts tab of the Font Library Modal
  • Save changes and refresh the page
  • Make sure the rendered font of those paragraphs is Cardo-Regular

Testing Instructions for Keyboard

Screenshots or screencast

Here is the video to reproduce the issue:

font-library-installed-fonts-overrides-theme-fonts.mov

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 16, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: arthur791004 <arthur791004@git.wordpress.org>
Co-authored-by: okmttdhr <okat@git.wordpress.org>
Co-authored-by: matiasbenedetto <mmaattiiaass@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@arthur791004 arthur791004 added the [Type] Bug An existing feature does not function as intended label Feb 16, 2024
@github-actions
Copy link
Copy Markdown

This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress.

If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged.

If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack.

Thank you! ❤️

View changed files
❔ lib/compat/wordpress-6.4/fonts/font-face/class-wp-font-face-resolver.php

@arthur791004 arthur791004 force-pushed the fix/font-library-install-fonts-variant-overrides-theme-fonts branch from cbdb05d to a4dd8d6 Compare February 16, 2024 07:22
foreach ( $font_faces_to_add as $font_face_to_add ) {
$offset = static::search_font_face( $font_face_to_add, $fonts[ $font_family_name ] );
if ( false !== $offset ) {
$fonts[ $font_family_name ][ $offset ] = array_merge( $fonts[ $font_family_name ][ $offset ], $font_face_to_add );
Copy link
Copy Markdown
Contributor

@okmttdhr okmttdhr Feb 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of array_merge here combines the properties of the existing font face ($fonts[ $font_family_name ][ $offset ]) with those of the new font face ($font_face_to_add). This operation prioritizes the properties from $font_face_to_add when the same keys exist in both arrays. For instance, if both arrays contain a src property, the src from $font_face_to_add might overwrite the one in the existing font face.

Should we keep the original font face when all font-family & font-style & font-weight are the same?

I thought just $fonts[ $font_family_name ][] = $font_face_to_add; does a good job of merging the different variants of the original and the installed font.
WDYT? 💭 (Or CMIIW) @arthur791004

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a fair point. IMHO, I assume it's fine to override the src if all the font-family & font-style & font-weight are the same as the fonts here are for generating and printing font-face styles to the HTML. I don't think we have to print the same font face multiple times. Do you have any concerns about that?

Copy link
Copy Markdown
Contributor

@okmttdhr okmttdhr Feb 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was somewhat hesitant to overwrite the built-in fonts because they might have already been set (I mean I might prefer array_merge( $font_face_to_add, $fonts[ $font_family_name ][ $offset ] ); instead of array_merge( $fonts[ $font_family_name ][ $offset ], $font_face_to_add );). However, as you mentioned,

I assume it's fine to override the src if all the font-family & font-style & font-weight are the same as the fonts here are for generating and printing font-face styles to the HTML

so it should be fine. 👍

Copy link
Copy Markdown
Contributor

@matiasbenedetto matiasbenedetto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @arthur791004 and @okmttdhr , for working on this!

I think this change is working mostly as expected, but it has a weak point produced by relying on one unnecessary aspect of the current implementation.

$fonts[ $font_family_name ] = static::convert_font_face_properties( $definition['fontFace'], $font_family_name );

could be

$fonts[] = static::convert_font_face_properties( $definition['fontFace'], $font_family_name ); 

I don't see any reason why $fonts should be an associative array and not an indexed one. Is there still any reason for that? Maybe @hellofromtonya has thoughts about that.

If we convert $fonts into an indexed array, the complexity added by this PR won't be needed. So, we can solve the problem without adding extra complexity and extra logic in a part of the code that can impact the public frontend rendering performance.

@matiasbenedetto
Copy link
Copy Markdown
Contributor

Apart from that, I think it would be useful to implement these changes in the WordPress core repo first to be able to run the unit tests for this functionality.

@matiasbenedetto
Copy link
Copy Markdown
Contributor

There's an alternative approach implementing what i mentioned here: #59119 (review) I'd appreciate if you can take a look.

@arthur791004
Copy link
Copy Markdown
Contributor Author

Reviewed WordPress/wordpress-develop#6161 (review) and it looks good 🙂

Do we still need this PR? I'm unsure whether we have a mechanism to sync changes from Core.

@matiasbenedetto
Copy link
Copy Markdown
Contributor

matiasbenedetto commented Feb 23, 2024

Do we still need this PR?

Nope no longer needed.

I'm unsure whether we have a mechanism to sync changes from Core.

In this case, we need to manually copy these changes from the core to the Gutenberg repo when this is merged.

@arthur791004
Copy link
Copy Markdown
Contributor Author

Okay, thanks!

@arthur791004
Copy link
Copy Markdown
Contributor Author

arthur791004 commented Feb 26, 2024

Closing it in favor of WordPress/wordpress-develop#6161 and #59321

@arthur791004 arthur791004 deleted the fix/font-library-install-fonts-variant-overrides-theme-fonts branch February 26, 2024 04:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Font Library [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants