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

Fix the FontImageSource for Windows Unpackaged #22646

Merged
merged 6 commits into from
May 30, 2024
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 @@ -19,7 +19,7 @@ protected override void Build(StackLayout stackLayout)
{
base.Build(stackLayout);

var namedSizeMediumBoldContainer = new ViewContainer<Label>(Test.Label.FontAttibutesBold, new Label { Text = "Medium Bold Font", FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)), FontAttributes = FontAttributes.Bold });
var namedSizeMediumBoldContainer = new ViewContainer<Label>(Test.Label.FontAttributesBold, new Label { Text = "Medium Bold Font", FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)), FontAttributes = FontAttributes.Bold });

var namedSizeMediumItalicContainer = new ViewContainer<Label>(Test.Label.FontAttributesItalic, new Label { Text = "Medium Italic Font", FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)), FontAttributes = FontAttributes.Italic });

Expand Down
4 changes: 3 additions & 1 deletion src/Controls/tests/CustomAttributes/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ public enum Frame
public enum Image
{
Source,
Source_FontImageSource,
Aspect,
IsOpaque,
IsLoading,
Expand Down Expand Up @@ -463,7 +464,7 @@ public enum Label
Text,
Padding,
FormattedText,
FontAttibutesBold,
FontAttributesBold,
FontAttributesItalic,
TextDecorationUnderline,
TextDecorationStrike,
Expand All @@ -489,6 +490,7 @@ public enum Label
HtmlTextTypeMultipleLines,
HtmlTextLabelProperties,
TextTypeToggle,
FontFamily,
}

public enum ProgressBar
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions src/Controls/tests/TestCases.Shared.Tests/Tests/ImageUITests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests;

[Category(UITestCategories.Image)]
internal class ImageUITests : _ViewUITests
{
const string ImageGallery = "Image Gallery";

public ImageUITests(TestDevice device)
: base(device)
{
}

protected override void NavigateToGallery()
{
App.NavigateToGallery(ImageGallery);
}

public override void IsEnabled()
{
Assert.Ignore("Image elements do not really have a concept of being \"disabled\".");
}

[Test]
public void Source_FontImageSource()
{
var remote = GoToStateRemote();
VerifyScreenshot("ImageUITests_Source_FontImageSource_FontAwesome");

remote.TapStateButton();
VerifyScreenshot("ImageUITests_Source_FontImageSource_Ionicons");

remote.TapStateButton();
VerifyScreenshot("ImageUITests_Source_FontImageSource_FontAwesome");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Microsoft.Maui.TestCases.Tests;

[Category(UITestCategories.Label)]
public class LabelUITests : _ViewUITests
{
const string LabelGallery = "Label Gallery";
Expand All @@ -23,7 +24,6 @@ public override void IsEnabled()
}

[Test]
[Category(UITestCategories.Label)]
public void SpanTapped()
{
if (Device == TestDevice.Mac)
Expand All @@ -45,4 +45,17 @@ public void SpanTapped()
var textAfterClick = remote.GetEventLabel().GetText();
ClassicAssert.AreEqual("Event: SpanTapped (fired 1)", textAfterClick);
}

[Test]
public void FontFamily()
{
var remote = GoToStateRemote();
VerifyScreenshot("LabelUITests_FontFamily_FontAwesome");

remote.TapStateButton();
VerifyScreenshot("LabelUITests_FontFamily_Ionicons");

remote.TapStateButton();
VerifyScreenshot("LabelUITests_FontFamily_FontAwesome");
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<MauiImage Include="Resources\Images\dotnet_bot.svg" Link="Resources\Images\small_dotnet_bot.svg" Color="#FFFFFF" BaseSize="64,64" />
<MauiImage Include="Resources\AppIcons\appicon.svg" ForegroundFile="Resources\AppIcons\appicon_foreground.svg" IsAppIcon="true" />
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#FFFFFF" BaseSize="168,208" />
<MauiFont Include="Resources\Fonts\OpenSans-Regular.ttf" />
<MauiFont Include="Resources\Fonts\**" />
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class StateViewContainer<T> : ViewContainer<T>
public Button StateChangeButton { get; private set; }
public Label ViewInteractionLabel { get; private set; }

public StateViewContainer(Enum formsMember, T view) : base(formsMember, view)
public StateViewContainer(Enum formsMember, T view, Func<object, string> converterFunc = null) : base(formsMember, view)
{
var name = formsMember.ToString();

Expand All @@ -38,6 +38,9 @@ public StateViewContainer(Enum formsMember, T view) : base(formsMember, view)

var converter = new GenericValueConverter(o =>
{
if (converterFunc is not null)
return converterFunc(o);

try
{
var attr = o.GetType().GetCustomAttribute<TypeConverterAttribute>();
Expand Down
19 changes: 19 additions & 0 deletions src/Controls/tests/TestCases/Elements/ImageCoreGalleryPage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;

namespace Maui.Controls.Sample;

Expand All @@ -14,5 +15,23 @@ protected override void InitializeElement(Image element)
protected override void Build()
{
base.Build();

{
var image = new Image
{
Source = new FontImageSource { FontFamily = "FA", Glyph = "\xf133", Size = 48, Color = Colors.Black },
WidthRequest = 48,
HeightRequest = 48,
};
var familyContainer = new StateViewContainer<Image>(Test.Image.Source_FontImageSource, image, o => ((FontImageSource)o).FontFamily);
familyContainer.StateChangeButton.Clicked += (s, a) =>
{
if (image.Source is FontImageSource fis && fis.FontFamily == "FA")
image.Source = new FontImageSource { FontFamily = "Ion", Glyph = "\xf30c", Size = 48, Color = Colors.Black };
else
image.Source = new FontImageSource { FontFamily = "FA", Glyph = "\xf133", Size = 48, Color = Colors.Black };
};
Add(familyContainer);
}
}
}
18 changes: 18 additions & 0 deletions src/Controls/tests/TestCases/Elements/LabelCoreGalleryPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,5 +321,23 @@ protected override void Build()
var toggleHtmlPlainTextLabelContainer = new ViewContainer<Label>(Test.Label.TextTypeToggle, toggleLabel);
Add(toggleHtmlPlainTextLabelContainer);
}

{
var label = new Label
{
FontFamily = "FA",
FontSize = 48,
Text = "\xf133",
WidthRequest = 48,
HeightRequest = 48,
};
var familyContainer = new StateViewContainer<Label>(Test.Label.FontFamily, label);
familyContainer.StateChangeButton.Clicked += (s, a) =>
{
label.FontFamily = label.FontFamily == "FA" ? "Ion" : "FA";
label.Text = label.FontFamily == "FA" ? "\xf133" : "\xf30c";
};
Add(familyContainer);
}
}
}
6 changes: 3 additions & 3 deletions src/Controls/tests/TestCases/Issues/Issue21202.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
TextTransform="Uppercase"
FontFamily="OpenSansRegular">
<Button.ImageSource>
<FontImageSource Glyph="A" Color="Black" FontFamily="OpenSansRegular"/>
<FontImageSource Glyph="&#xf133;" Color="Black" FontFamily="FA"/>
Copy link
Member Author

Choose a reason for hiding this comment

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

Changing this because this test also had an issue with the font because it has a space. However, it was not noticeable since the fonts are close.

</Button.ImageSource>
</Button>
<Label
Expand All @@ -29,7 +29,7 @@
TextTransform="Uppercase"
FontFamily="OpenSansRegular">
<Button.ImageSource>
<FontImageSource Glyph="A" Color="Black" FontFamily="OpenSansRegular"/>
<FontImageSource Glyph="&#xf133;" Color="Black" FontFamily="FA"/>
</Button.ImageSource>
</Button>
<Label
Expand All @@ -41,7 +41,7 @@
TextTransform="Uppercase"
FontFamily="OpenSansRegular">
<Button.ImageSource>
<FontImageSource Glyph="A" Size="60" Color="Black" FontFamily="OpenSansRegular"/>
<FontImageSource Glyph="&#xf133;" Size="60" Color="Black" FontFamily="FA"/>
</Button.ImageSource>
</Button>
</VerticalStackLayout>
Expand Down
2 changes: 2 additions & 0 deletions src/Controls/tests/TestCases/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public static MauiApp CreateMauiApp()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("FontAwesome.ttf", "FA");
fonts.AddFont("ionicons.ttf", "Ion");
})
.Issue21109AddMappers();

Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,20 @@ string GetFontSource(IFontImageSource imageSource)
// unpackaged apps can't load files using packaged schemes
if (!AppInfoUtils.IsPackagedApp)
{
// the Uri type encodes the fragment, so let's remove first
var fragment = "";
if (fontSource.IndexOf('#', StringComparison.OrdinalIgnoreCase) is int index && index >= 0)
{
fragment = fontSource.Substring(index);
fontSource = fontSource.Substring(0, index);
}

var fontUri = new Uri(fontSource, UriKind.RelativeOrAbsolute);

var path = fontUri.AbsolutePath.TrimStart('/');
if (FileSystemUtils.TryGetAppPackageFileUri(path, out var uri))
{
fontSource = uri + fontUri.Fragment;
fontSource = uri + fragment;
mattleibow marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Loading