Font Library: Merge the font faces instead of overriding when parsing settings#59119
Font Library: Merge the font faces instead of overriding when parsing settings#59119arthur791004 wants to merge 1 commit intotrunkfrom
Conversation
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
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 |
cbdb05d to
a4dd8d6
Compare
| 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 ); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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. 👍
There was a problem hiding this comment.
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.
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.
|
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. |
|
There's an alternative approach implementing what i mentioned here: #59119 (review) I'd appreciate if you can take a look. |
|
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. |
Nope no longer needed.
In this case, we need to manually copy these changes from the core to the Gutenberg repo when this is merged. |
|
Okay, thanks! |
|
Closing it in favor of WordPress/wordpress-develop#6161 and #59321 |
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
wp-env run cli bashwp-settings.phpTesting Instructions for Keyboard
Screenshots or screencast
Here is the video to reproduce the issue:
font-library-installed-fonts-overrides-theme-fonts.mov