2019-02-12 04:04:46 +00:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.Graphics.Sprites ;
namespace osu.Game.Graphics
{
2019-03-27 10:29:27 +00:00
public static class OsuFont
2019-02-12 04:04:46 +00:00
{
2019-02-20 12:04:18 +00:00
/// <summary>
/// The default font size.
/// </summary>
2019-02-12 04:04:46 +00:00
public const float DEFAULT_FONT_SIZE = 16 ;
2019-02-20 12:04:18 +00:00
/// <summary>
/// The default font.
/// </summary>
2019-02-12 04:04:46 +00:00
public static FontUsage Default = > GetFont ( ) ;
2019-02-22 10:42:09 +00:00
public static FontUsage Numeric = > GetFont ( Typeface . Venera , weight : FontWeight . Regular ) ;
2019-02-20 12:04:18 +00:00
/// <summary>
/// Retrieves a <see cref="FontUsage"/>.
/// </summary>
/// <param name="typeface">The font typeface.</param>
/// <param name="size">The size of the text in local space. For a value of 16, a single line will have a height of 16px.</param>
/// <param name="weight">The font weight.</param>
/// <param name="italics">Whether the font is italic.</param>
/// <param name="fixedWidth">Whether all characters should be spaced the same distance apart.</param>
/// <returns>The <see cref="FontUsage"/>.</returns>
2019-02-20 07:52:36 +00:00
public static FontUsage GetFont ( Typeface typeface = Typeface . Exo , float size = DEFAULT_FONT_SIZE , FontWeight weight = FontWeight . Medium , bool italics = false , bool fixedWidth = false )
2019-02-20 10:32:30 +00:00
= > new FontUsage ( GetFamilyString ( typeface ) , size , GetWeightString ( typeface , weight ) , italics , fixedWidth ) ;
2019-02-12 04:04:46 +00:00
2019-02-20 12:04:18 +00:00
/// <summary>
/// Retrieves the string representation of a <see cref="Typeface"/>.
/// </summary>
/// <param name="typeface">The <see cref="Typeface"/>.</param>
/// <returns>The string representation.</returns>
2019-02-20 10:32:30 +00:00
public static string GetFamilyString ( Typeface typeface )
2019-02-12 04:04:46 +00:00
{
switch ( typeface )
{
case Typeface . Exo :
return "Exo2.0" ;
2019-04-01 03:44:46 +00:00
2019-02-12 04:04:46 +00:00
case Typeface . Venera :
return "Venera" ;
}
return null ;
}
2019-02-20 12:04:18 +00:00
/// <summary>
/// Retrieves the string representation of a <see cref="FontWeight"/>.
/// </summary>
/// <param name="typeface">The <see cref="Typeface"/>.</param>
/// <param name="weight">The <see cref="FontWeight"/>.</param>
/// <returns>The string representation of <paramref name="weight"/> in the specified <paramref name="typeface"/>.</returns>
2019-02-20 10:32:30 +00:00
public static string GetWeightString ( Typeface typeface , FontWeight weight )
= > GetWeightString ( GetFamilyString ( typeface ) , weight ) ;
2019-02-12 04:04:46 +00:00
2019-02-20 12:04:18 +00:00
/// <summary>
/// Retrieves the string representation of a <see cref="FontWeight"/>.
/// </summary>
2019-04-25 08:36:17 +00:00
/// <param name="family">The family string.</param>
2019-02-20 12:04:18 +00:00
/// <param name="weight">The <see cref="FontWeight"/>.</param>
2019-04-25 08:36:17 +00:00
/// <returns>The string representation of <paramref name="weight"/> in the specified <paramref name="family"/>.</returns>
2019-02-20 10:32:30 +00:00
public static string GetWeightString ( string family , FontWeight weight )
2019-02-12 04:04:46 +00:00
{
string weightString = weight . ToString ( ) ;
// Only exo has an explicit "regular" weight, other fonts do not
2019-02-21 10:26:02 +00:00
if ( family ! = GetFamilyString ( Typeface . Exo ) & & weight = = FontWeight . Regular )
2019-02-12 04:04:46 +00:00
weightString = string . Empty ;
return weightString ;
}
}
2019-02-20 10:32:30 +00:00
public static class OsuFontExtensions
{
2019-02-20 12:04:18 +00:00
/// <summary>
/// Creates a new <see cref="FontUsage"/> by applying adjustments to this <see cref="FontUsage"/>.
/// </summary>
2019-04-25 08:36:17 +00:00
/// <param name="usage">The base <see cref="FontUsage"/>.</param>
2019-02-20 12:04:18 +00:00
/// <param name="typeface">The font typeface. If null, the value is copied from this <see cref="FontUsage"/>.</param>
/// <param name="size">The text size. If null, the value is copied from this <see cref="FontUsage"/>.</param>
/// <param name="weight">The font weight. If null, the value is copied from this <see cref="FontUsage"/>.</param>
/// <param name="italics">Whether the font is italic. If null, the value is copied from this <see cref="FontUsage"/>.</param>
/// <param name="fixedWidth">Whether all characters should be spaced apart the same distance. If null, the value is copied from this <see cref="FontUsage"/>.</param>
/// <returns>The resulting <see cref="FontUsage"/>.</returns>
2019-02-20 10:32:30 +00:00
public static FontUsage With ( this FontUsage usage , Typeface ? typeface = null , float? size = null , FontWeight ? weight = null , bool? italics = null , bool? fixedWidth = null )
{
string familyString = typeface ! = null ? OsuFont . GetFamilyString ( typeface . Value ) : usage . Family ;
string weightString = weight ! = null ? OsuFont . GetWeightString ( familyString , weight . Value ) : usage . Weight ;
return usage . With ( familyString , size , weightString , italics , fixedWidth ) ;
}
}
2019-02-12 04:04:46 +00:00
public enum Typeface
{
Exo ,
Venera ,
}
public enum FontWeight
{
Light ,
Regular ,
Medium ,
SemiBold ,
Bold ,
Black
}
}