Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Font Properties #1755

Merged
merged 1 commit into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void UpdateFont()
else
{
NativeButton.Typeface = font.ToTypeface();
NativeButton.SetTextSize(ComplexUnitType.Sp, (float)font.FontSize);
NativeButton.SetTextSize(ComplexUnitType.Sp, (float)font.Size);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ void UpdateFont()
else
{
Typeface = font.ToTypeface();
SetTextSize(ComplexUnitType.Sp, (float)font.FontSize);
SetTextSize(ComplexUnitType.Sp, (float)font.Size);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ void UpdateFont()
else
{
Typeface = font.ToTypeface();
SetTextSize(ComplexUnitType.Sp, (float)font.FontSize);
SetTextSize(ComplexUnitType.Sp, (float)font.Size);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ void UpdateFont()
_lastTypeface = newTypeface;
}

float newTextSize = (float)f.FontSize;
float newTextSize = (float)f.Size;
if (newTextSize != _lastTextSize)
{
SetTextSize(ComplexUnitType.Sp, newTextSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public override void UpdateMeasureState(TextPaint p)
void Apply(Paint paint)
{
paint.SetTypeface(Font.ToTypeface());
float value = (float)Font.FontSize;
float value = (float)Font.Size;
paint.TextSize = TypedValue.ApplyDimension(ComplexUnitType.Sp, value, TextView.Resources.DisplayMetrics);
if (Forms.IsLollipopOrNewer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void UpdateFont()
_lastTypeface = newTypeface;
}

float newTextSize = (float)f.FontSize;
float newTextSize = (float)f.Size;
if (newTextSize != _lastTextSize)
{
_view.SetTextSize(ComplexUnitType.Sp, newTextSize);
Expand Down
4 changes: 2 additions & 2 deletions src/Controls/src/Core/FontElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ static void OnFontPropertyChanged(BindableObject bindable, object oldValue, obje
}
else
{
bindable.SetValue(FontFamilyProperty, font.FontFamily);
bindable.SetValue(FontSizeProperty, font.FontSize);
bindable.SetValue(FontFamilyProperty, font.Family);
bindable.SetValue(FontSizeProperty, font.Size);
bindable.SetValue(FontAttributesProperty, font.GetFontAttributes());
}
SetCancelEvents(bindable, false);
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Core/FontExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static Font WithAttributes(this Font font, FontAttributes attributes)
public static FontAttributes GetFontAttributes(this Font font)
{
FontAttributes attributes = font.Weight == FontWeight.Bold ? FontAttributes.Bold : FontAttributes.None;
if (font.FontSlant != FontSlant.Default)
if (font.Slant != FontSlant.Default)
{
if (attributes == FontAttributes.None)
attributes = FontAttributes.Italic;
Expand Down
8 changes: 4 additions & 4 deletions src/Controls/src/Core/FontTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ public override string ConvertToInvariantString(object value)
if (!(value is Font font))
throw new NotSupportedException();
var parts = new List<string>();
if (!string.IsNullOrEmpty(font.FontFamily))
parts.Add(font.FontFamily);
if (!string.IsNullOrEmpty(font.Family))
parts.Add(font.Family);
if (font.Weight == FontWeight.Bold)
parts.Add("Bold");
if (font.FontSlant != FontSlant.Default)
if (font.Slant != FontSlant.Default)
parts.Add("Italic");
parts.Add($"{font.FontSize}");
parts.Add($"{font.Size}");
return string.Join(", ", parts);
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/Controls/tests/Core.UnitTests/FontUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,39 @@ public class FontUnitTests : BaseTestFixture
public void TestFontForSize()
{
var font = Font.OfSize("Foo", 12);
Assert.AreEqual("Foo", font.FontFamily);
Assert.AreEqual(12, font.FontSize);
Assert.AreEqual("Foo", font.Family);
Assert.AreEqual(12, font.Size);
}

[Test]
public void TestFontForSizeDouble()
{
var font = Font.OfSize("Foo", 12.7);
Assert.AreEqual("Foo", font.FontFamily);
Assert.AreEqual(12.7, font.FontSize);
Assert.AreEqual("Foo", font.Family);
Assert.AreEqual(12.7, font.Size);
}

[Test]
public void TestFontForNamedSize()
{
var size = Device.GetNamedSize(NamedSize.Large, null, false);
var font = Font.OfSize("Foo", size);
Assert.AreEqual("Foo", font.FontFamily);
Assert.AreEqual(size, font.FontSize);
Assert.AreEqual("Foo", font.Family);
Assert.AreEqual(size, font.Size);
}

[Test]
public void TestSystemFontOfSize()
{
var font = Font.SystemFontOfSize(12);
Assert.AreEqual(null, font.FontFamily);
Assert.AreEqual(12, font.FontSize);
Assert.AreEqual(null, font.Family);
Assert.AreEqual(12, font.Size);


var size = Device.GetNamedSize(NamedSize.Medium, null, false);
font = Font.SystemFontOfSize(size);
Assert.AreEqual(null, font.FontFamily);
Assert.AreEqual(size, font.FontSize);
Assert.AreEqual(null, font.Family);
Assert.AreEqual(size, font.Size);
}

[TestCase("en-US"), TestCase("tr-TR"), TestCase("fr-FR")]
Expand All @@ -53,13 +53,13 @@ public void CultureTestSystemFontOfSizeDouble(string culture)
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(culture);

var font = Font.SystemFontOfSize(12.7);
Assert.AreEqual(null, font.FontFamily);
Assert.AreEqual(12.7, font.FontSize);
Assert.AreEqual(null, font.Family);
Assert.AreEqual(12.7, font.Size);

var size = Device.GetNamedSize(NamedSize.Medium, null, false);
font = Font.SystemFontOfSize(size);
Assert.AreEqual(null, font.FontFamily);
Assert.AreEqual(size, font.FontSize);
Assert.AreEqual(null, font.Family);
Assert.AreEqual(size, font.Size);
}

[Test]
Expand Down
8 changes: 4 additions & 4 deletions src/Core/src/Fonts/FontManager.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ public FontManager(IFontRegistrar fontRegistrar, ILogger<FontManager>? logger =

public Typeface? GetTypeface(Font font)
{
if (font == Font.Default || (font.Weight == FontWeight.Regular && string.IsNullOrEmpty(font.FontFamily) && font.FontSlant == FontSlant.Default))
if (font == Font.Default || (font.Weight == FontWeight.Regular && string.IsNullOrEmpty(font.Family) && font.Slant == FontSlant.Default))
return DefaultTypeface;

return _typefaces.GetOrAdd((font.FontFamily, font.Weight, font.FontSlant != FontSlant.Default), CreateTypeface);
return _typefaces.GetOrAdd((font.Family, font.Weight, font.Slant != FontSlant.Default), CreateTypeface);
}

public float GetFontSize(Font font, float defaultFontSize = 0) =>
font.FontSize <= 0
font.Size <= 0
? (defaultFontSize > 0 ? defaultFontSize : 14f)
: (float)font.FontSize;
: (float)font.Size;


Typeface? GetFromAssets(string fontName)
Expand Down
8 changes: 4 additions & 4 deletions src/Core/src/Fonts/FontManager.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ public FontManager(IFontRegistrar fontRegistrar, ILogger<FontManager>? logger =

public FontFamily GetFontFamily(Font font)
{
if (font.IsDefault || string.IsNullOrWhiteSpace(font.FontFamily))
if (font.IsDefault || string.IsNullOrWhiteSpace(font.Family))
return DefaultFontFamily;

return _fonts.GetOrAdd(font.FontFamily, CreateFontFamily);
return _fonts.GetOrAdd(font.Family, CreateFontFamily);
}

public double GetFontSize(Font font, double defaultFontSize = 0) =>
font.FontSize <= 0
font.Size <= 0
? (defaultFontSize > 0 ? defaultFontSize : DefaultFontSize)
: font.FontSize;
: font.Size;

FontFamily CreateFontFamily(string fontFamily)
{
Expand Down
16 changes: 8 additions & 8 deletions src/Core/src/Fonts/FontManager.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public UIFont GetFont(Font font, double defaultFontSize = 0) =>
GetFont(font, defaultFontSize, CreateFont);

public double GetFontSize(Font font, double defaultFontSize = 0) =>
font.FontSize <= 0
font.Size <= 0
? (defaultFontSize > 0 ? (float)defaultFontSize : DefaultFont.PointSize)
: (nfloat)font.FontSize;
: (nfloat)font.Size;

static float GetWeightConstant(FontWeight self)
{
Expand All @@ -59,7 +59,7 @@ static float GetWeightConstant(FontWeight self)
UIFont GetFont(Font font, double defaultFont, Func<Font, UIFont> factory)
{
var size = GetFontSize(font, defaultFont);
if (size != font.FontSize)
if (size != font.Size)
font = font.WithSize(size);
return _fonts.GetOrAdd(font, factory);
}
Expand All @@ -81,10 +81,10 @@ static UIFontAttributes GetFontAttributes(Font font)
a.Traits = new UIFontTraits
{
Weight = GetWeightConstant(font.Weight),
Slant = font.FontSlant == FontSlant.Oblique ? 30.0f : 0.0f
Slant = font.Slant == FontSlant.Oblique ? 30.0f : 0.0f
};
}
if (font.FontSlant == FontSlant.Italic)
if (font.Slant == FontSlant.Italic)
traits |= UIFontDescriptorSymbolicTraits.Italic;

a.Traits.SymbolicTrait = traits;
Expand All @@ -93,12 +93,12 @@ static UIFontAttributes GetFontAttributes(Font font)

UIFont CreateFont(Font font)
{
var family = font.FontFamily;
var size = (nfloat)font.FontSize;
var family = font.Family;
var size = (nfloat)font.Size;

var hasAttributes =
font.Weight != FontWeight.Regular ||
font.FontSlant != FontSlant.Default;
font.Slant != FontSlant.Default;

if (family != null && family != DefaultFont.FamilyName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal CanvasImageSource RenderImageSource(IFontImageSource imageSource, float
var dpi = scale * BaseLogicalDpi;

var fontFamily = GetFontSource(imageSource);
var fontSize = (float)imageSource.Font.FontSize;
var fontSize = (float)imageSource.Font.Size;
var color = (imageSource.Color ?? Colors.White).ToWindowsColor();

var textFormat = new CanvasTextFormat
Expand Down Expand Up @@ -102,7 +102,7 @@ string GetFontSource(IFontImageSource imageSource)
// There's really no perfect solution to handle font families with fallbacks (comma-separated)
// So if the font family has fallbacks, only one is taken, because CanvasTextFormat
// only supports one font family
var source = imageSource.Font.FontFamily;
var source = imageSource.Font.Family;

foreach (var family in allFamilies)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Platform/Windows/FontExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.Maui
public static class FontExtensions
{
public static FontStyle ToFontStyle(this Font fontAttributes) =>
fontAttributes.FontSlant switch
fontAttributes.Slant switch
{
FontSlant.Italic => FontStyle.Italic,
FontSlant.Oblique => FontStyle.Oblique,
Expand Down
28 changes: 14 additions & 14 deletions src/Core/src/Primitives/Font.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ namespace Microsoft.Maui
{
public struct Font
{
public string FontFamily { get; private set; }
public string Family { get; private set; }

public double FontSize { get; private set; }
public double Size { get; private set; }

public FontSlant FontSlant { get; private set; }
public FontSlant Slant { get; private set; }

public bool IsDefault => FontFamily == null && FontSize == 0 && FontSlant == FontSlant.Default && Weight == FontWeight.Regular;
public bool IsDefault => Family == null && Size == 0 && Slant == FontSlant.Default && Weight == FontWeight.Regular;

static Font _default = default(Font).WithWeight(FontWeight.Regular);
public static Font Default => _default;
Expand All @@ -25,39 +25,39 @@ public FontWeight Weight

public Font WithSize(double size)
{
return new Font { FontFamily = FontFamily, FontSize = size, FontSlant = FontSlant, Weight = Weight };
return new Font { Family = Family, Size = size, Slant = Slant, Weight = Weight };
}

public Font WithSlant(FontSlant fontSlant)
{
return new Font { FontFamily = FontFamily, FontSize = FontSize, FontSlant = fontSlant, Weight = Weight };
return new Font { Family = Family, Size = Size, Slant = fontSlant, Weight = Weight };
}

public Font WithWeight(FontWeight weight)
{
return new Font { FontFamily = FontFamily, FontSize = FontSize, FontSlant = FontSlant, Weight = weight };
return new Font { Family = Family, Size = Size, Slant = Slant, Weight = weight };
}

public Font WithWeight(FontWeight weight, FontSlant fontSlant)
{
return new Font { FontFamily = FontFamily, FontSize = FontSize, FontSlant = fontSlant, Weight = weight };
return new Font { Family = Family, Size = Size, Slant = fontSlant, Weight = weight };
}

public static Font OfSize(string name, double size, FontWeight weight = FontWeight.Regular, FontSlant fontSlant = FontSlant.Default) =>
new() { FontFamily = name, FontSize = size, Weight = weight, FontSlant = fontSlant };
new() { Family = name, Size = size, Weight = weight, Slant = fontSlant };

public static Font SystemFontOfSize(double size, FontWeight weight = FontWeight.Regular, FontSlant fontSlant = FontSlant.Default) =>
new() { FontSize = size, Weight = weight, FontSlant = fontSlant };
new() { Size = size, Weight = weight, Slant = fontSlant };

public static Font SystemFontOfWeight(FontWeight weight, FontSlant fontSlant = FontSlant.Default)
{
var result = new Font { Weight = weight, FontSlant = fontSlant };
var result = new Font { Weight = weight, Slant = fontSlant };
return result;
}

bool Equals(Font other)
{
return string.Equals(FontFamily, other.FontFamily) && FontSize.Equals(other.FontSize) && Weight == other.Weight && FontSlant == other.FontSlant;
return string.Equals(Family, other.Family) && Size.Equals(other.Size) && Weight == other.Weight && Slant == other.Slant;
}

public override bool Equals(object? obj)
Expand All @@ -73,7 +73,7 @@ public override bool Equals(object? obj)
return Equals((Font)obj);
}

public override int GetHashCode() => (FontFamily, FontSize, Weight, FontSlant).GetHashCode();
public override int GetHashCode() => (Family, Size, Weight, Slant).GetHashCode();

public static bool operator ==(Font left, Font right)
{
Expand All @@ -86,6 +86,6 @@ public override bool Equals(object? obj)
}

public override string ToString()
=> $"FontFamily: {FontFamily}, FontSize: {FontSize}, Weight: {Weight}, FontSlant: {FontSlant}";
=> $"Family: {Family}, Size: {Size}, Weight: {Weight}, Slant: {Slant}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public async Task FontSizeInitializesCorrectly(int fontSize)
Font = Font.OfSize("Arial", fontSize)
};

await ValidatePropertyInitValue(button, () => button.Font.FontSize, GetNativeUnscaledFontSize, button.Font.FontSize);
await ValidatePropertyInitValue(button, () => button.Font.Size, GetNativeUnscaledFontSize, button.Font.Size);
}

[Theory(DisplayName = "Font Attributes Initialize Correctly")]
Expand All @@ -91,7 +91,7 @@ public async Task FontAttributesInitializeCorrectly(FontWeight weight, bool isBo
};

await ValidatePropertyInitValue(button, () => button.Font.Weight == FontWeight.Bold, GetNativeIsBold, isBold);
await ValidatePropertyInitValue(button, () => button.Font.FontSlant == FontSlant.Italic, GetNativeIsItalic, isItalic);
await ValidatePropertyInitValue(button, () => button.Font.Slant == FontSlant.Italic, GetNativeIsItalic, isItalic);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async Task FontSizeInitializesCorrectly(int fontSize)
Font = Font.OfSize("Arial", fontSize)
};

await ValidatePropertyInitValue(datePicker, () => datePicker.Font.FontSize, GetNativeUnscaledFontSize, datePicker.Font.FontSize);
await ValidatePropertyInitValue(datePicker, () => datePicker.Font.Size, GetNativeUnscaledFontSize, datePicker.Font.Size);
}
}
}
Loading