Make WordPress Core


Ignore:
Timestamp:
06/20/2023 07:58:03 AM (2 years ago)
Author:
isabel_brison
Message:

Editor: use logarithmic scale for fluid typography.

Introduces logarithmic scale factor to calculate a minimum font scale that tapers out as the font size increases.

Props ramonopoly.
Fixes #58523.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-supports/typography.php

    r55946 r55947  
    461461 * @since 6.1.1 Adjusted rules for min and max font sizes.
    462462 * @since 6.2.0 Added 'settings.typography.fluid.minFontSize' support.
    463  * @since 6.3.0 Using layout.wideSize as max viewport width.
     463 * @since 6.3.0 Using layout.wideSize as max viewport width, and logarithmic scale factor to calculate minimum font scale.
    464464 *
    465465 * @param array $preset                     {
     
    508508
    509509    // Defaults.
    510     $default_maximum_viewport_width   = isset( $layout_settings['wideSize'] ) ? $layout_settings['wideSize'] : '1600px';
    511     $default_minimum_viewport_width   = '768px';
    512     $default_minimum_font_size_factor = 0.75;
    513     $default_scale_factor             = 1;
    514     $has_min_font_size                = isset( $fluid_settings['minFontSize'] ) && ! empty( wp_get_typography_value_and_unit( $fluid_settings['minFontSize'] ) );
    515     $default_minimum_font_size_limit  = $has_min_font_size ? $fluid_settings['minFontSize'] : '14px';
     510    $default_maximum_viewport_width       = isset( $layout_settings['wideSize'] ) ? $layout_settings['wideSize'] : '1600px';
     511    $default_minimum_viewport_width       = '320px';
     512    $default_minimum_font_size_factor_max = 0.75;
     513    $default_minimum_font_size_factor_min = 0.25;
     514    $default_scale_factor                 = 1;
     515    $has_min_font_size                    = isset( $fluid_settings['minFontSize'] ) &&
     516        ! empty( wp_get_typography_value_and_unit( $fluid_settings['minFontSize'] ) );
     517    $default_minimum_font_size_limit      = $has_min_font_size ? $fluid_settings['minFontSize'] : '14px';
    516518
    517519    // Font sizes.
     
    568570     */
    569571    if ( ! $minimum_font_size_raw ) {
    570         $calculated_minimum_font_size = round(
    571             $preferred_size['value'] * $default_minimum_font_size_factor,
    572             3
    573         );
     572        $preferred_font_size_in_px = 'px' === $preferred_size['unit'] ? $preferred_size['value'] : $preferred_size['value'] * 16;
     573
     574        /*
     575         * The scale factor is a multiplier that affects how quickly the curve will move towards the minimum,
     576         * that is, how quickly the size factor reaches 0 given increasing font size values.
     577         * For a - b * log2(), lower values of b will make the curve move towards the minimum faster.
     578         * The scale factor is constrained between min and max values.
     579         */
     580        $minimum_font_size_factor     = min( max( 1 - 0.075 * log( $preferred_font_size_in_px, 2 ), $default_minimum_font_size_factor_min ), $default_minimum_font_size_factor_max );
     581        $calculated_minimum_font_size = round( $preferred_size['value'] * $minimum_font_size_factor, 3 );
    574582
    575583        // Only use calculated min font size if it's > $minimum_font_size_limit value.
Note: See TracChangeset for help on using the changeset viewer.