Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit ba53417

Browse files
authored
Fixed wrong font path in FontImageSourceHandler UWP (#12362) fixes #12344 fixes #12635
1 parent 8b425fe commit ba53417

File tree

6 files changed

+97
-12
lines changed

6 files changed

+97
-12
lines changed

Xamarin.Forms.ControlGallery.WindowsUniversal/Xamarin.Forms.ControlGallery.WindowsUniversal.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@
279279
</Content>
280280
</ItemGroup>
281281
<ItemGroup>
282+
<Content Include="Assets\Fonts\materialdesignicons-webfont.ttf" />
282283
<None Include="Xamarin.Forms.ControlGallery.WindowsUniversal_TemporaryKey.pfx" />
283284
</ItemGroup>
284285
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<controls:TestContentPage
3+
xmlns:controls="clr-namespace:Xamarin.Forms.Controls"
4+
xmlns="http://xamarin.com/schemas/2014/forms"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
x:Class="Xamarin.Forms.Controls.Issues.Issue12344"
7+
Title="Issue 12344">
8+
<ContentPage.Resources>
9+
<OnPlatform x:Key="MaterialFontFamily" x:TypeArguments="x:String">
10+
<On Platform="UWP" Value="Assets/Fonts/materialdesignicons-webfont.ttf#Material Design Icons" />
11+
</OnPlatform>
12+
</ContentPage.Resources>
13+
<StackLayout>
14+
<Label
15+
Padding="12"
16+
BackgroundColor="Black"
17+
TextColor="White"
18+
Text="If you can see the icon, the test has passed." />
19+
<Image
20+
HeightRequest="44"
21+
HorizontalOptions="CenterAndExpand"
22+
VerticalOptions="CenterAndExpand"
23+
WidthRequest="44">
24+
<Image.Source>
25+
<FontImageSource
26+
FontFamily="{DynamicResource MaterialFontFamily}"
27+
Glyph="&#xf10b;"
28+
Size="44"
29+
Color="Black" />
30+
</Image.Source>
31+
</Image>
32+
</StackLayout>
33+
</controls:TestContentPage>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Xamarin.Forms.CustomAttributes;
2+
using Xamarin.Forms.Internals;
3+
4+
namespace Xamarin.Forms.Controls.Issues
5+
{
6+
[Preserve(AllMembers = true)]
7+
[Issue(IssueTracker.Github, 12344, "[Bug] FontImageSource does not work on UWP", PlatformAffected.UWP)]
8+
public partial class Issue12344 : TestContentPage
9+
{
10+
public Issue12344()
11+
{
12+
#if APP
13+
InitializeComponent();
14+
#endif
15+
}
16+
17+
protected override void Init()
18+
{
19+
20+
}
21+
}
22+
}

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
<Compile Include="$(MSBuildThisFileDirectory)Issue10744.cs" />
3030
<Compile Include="$(MSBuildThisFileDirectory)Issue10909.cs" />
3131
<Compile Include="$(MSBuildThisFileDirectory)Issue11769.cs" />
32+
<Compile Include="$(MSBuildThisFileDirectory)Issue12344.xaml.cs">
33+
<DependentUpon>Issue12344.xaml</DependentUpon>
34+
</Compile>
3235
<Compile Include="$(MSBuildThisFileDirectory)Issue12246.cs" />
3336
<Compile Include="$(MSBuildThisFileDirectory)Issue8613.cs" />
3437
<Compile Include="$(MSBuildThisFileDirectory)Issue9137.cs" />
@@ -2256,4 +2259,10 @@
22562259
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
22572260
</EmbeddedResource>
22582261
</ItemGroup>
2262+
<ItemGroup>
2263+
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue12344.xaml">
2264+
<SubType>Designer</SubType>
2265+
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
2266+
</EmbeddedResource>
2267+
</ItemGroup>
22592268
</Project>

Xamarin.Forms.Platform.UAP/FontImageSourceHandler.cs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Microsoft.Graphics.Canvas.Text;
66
using Microsoft.Graphics.Canvas.UI.Xaml;
77
using Windows.UI.Xaml.Controls;
8-
using Windows.UI.Xaml.Media;
98
using WFontIconSource = Microsoft.UI.Xaml.Controls.FontIconSource;
109

1110
namespace Xamarin.Forms.Platform.UWP
@@ -23,19 +22,9 @@ public sealed class FontImageSourceHandler : IImageSourceHandler, IIconElementHa
2322
var device = CanvasDevice.GetSharedDevice();
2423
var dpi = Math.Max(_minimumDpi, Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi);
2524

26-
// There's really no perfect solution to handle font families with fallbacks (comma-separated)
27-
// So if the font family has fallbacks, only the first one is taken, because CanvasTextFormat
28-
// only supports one font family
29-
30-
var fontFamily = fontsource.FontFamily.ToFontFamily();
31-
var allFamilies = fontFamily.Source.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
32-
33-
if (allFamilies.Length < 1)
34-
return null;
35-
3625
var textFormat = new CanvasTextFormat
3726
{
38-
FontFamily = allFamilies[0],
27+
FontFamily = GetFontSource(fontsource),
3928
FontSize = (float)fontsource.Size,
4029
HorizontalAlignment = CanvasHorizontalAlignment.Center,
4130
VerticalAlignment = CanvasVerticalAlignment.Center,
@@ -105,5 +94,36 @@ public sealed class FontImageSourceHandler : IImageSourceHandler, IIconElementHa
10594

10695
return Task.FromResult(image);
10796
}
97+
98+
string GetFontSource(FontImageSource fontImageSource)
99+
{
100+
if (fontImageSource == null)
101+
return string.Empty;
102+
103+
var fontFamily = fontImageSource.FontFamily.ToFontFamily();
104+
105+
string fontSource = fontFamily.Source;
106+
107+
var allFamilies = fontFamily.Source.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
108+
109+
if (allFamilies.Length > 1)
110+
{
111+
// There's really no perfect solution to handle font families with fallbacks (comma-separated)
112+
// So if the font family has fallbacks, only one is taken, because CanvasTextFormat
113+
// only supports one font family
114+
string source = fontImageSource.FontFamily;
115+
116+
foreach(var family in allFamilies)
117+
{
118+
if(family.Contains(source))
119+
{
120+
fontSource = family;
121+
break;
122+
}
123+
}
124+
}
125+
126+
return fontSource;
127+
}
108128
}
109129
}

0 commit comments

Comments
 (0)