Skip to content

Commit

Permalink
fix(reg): Include explicitly specified fonts in info.plist for backwa…
Browse files Browse the repository at this point in the history
…rd compatibilty
  • Loading branch information
jeromelaban committed Jan 20, 2023
1 parent fb2bd15 commit 9204b8f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
23 changes: 21 additions & 2 deletions src/SourceGenerators/Uno.UI.Tasks/Assets/RetargetAssets.Fonts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Xml;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Uno.UI.Tasks.Helpers;
Expand Down Expand Up @@ -31,10 +32,15 @@ public partial class RetargetAssets_v0
</plist>
""";

private ITaskItem[] GenerateFontPartialManifest(List<string> fontAssets)
private ITaskItem[] GenerateFontPartialManifest(List<string> fontAssets, string iOSAppManifest)
{
if (TargetPlatform == "ios")
{
// For compatibility measures, get the fonts from the iOS app manifest
// and merge them with the generated ones, so existing apps don't lose
// explicitly specified ones.
var existingFonts = EnumerateFontsFromPList(IosAppManifest);

var outputManifestFile = Path.Combine(IntermediateOutputPath, "FontsPartialInfo.plist");

using var writer = File.CreateText(outputManifestFile);
Expand All @@ -46,7 +52,7 @@ private ITaskItem[] GenerateFontPartialManifest(List<string> fontAssets)
writer.WriteLine(" <key>UIAppFonts</key>");
writer.WriteLine(" <array>");

foreach (var font in fontAssets)
foreach (var font in fontAssets.Concat(existingFonts))
{
writer.WriteLine(" <string>" + font + "</string>");
}
Expand All @@ -63,4 +69,17 @@ private ITaskItem[] GenerateFontPartialManifest(List<string> fontAssets)
return Array.Empty<ITaskItem>();
}
}

private string[] EnumerateFontsFromPList(string iosAppManifest)
{
XmlDocument doc = new();
doc.Load(iosAppManifest);

// Get the list of registered fonts in the info.plist
return doc
.SelectNodes("//key[text()='UIAppFonts']/following-sibling::array[1]/string")
.OfType<XmlNode>()
.Select(n => n.InnerText)
.ToArray();
}
}
4 changes: 3 additions & 1 deletion src/SourceGenerators/Uno.UI.Tasks/Assets/RetargetAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public partial class RetargetAssets_v0 : Task

public string AndroidAssetsPrefix { get; set; }

public string IosAppManifest { get; set; }

[Required]
public string DefaultLanguage { get; set; }

Expand Down Expand Up @@ -169,7 +171,7 @@ private void ProcessContentItems(ITaskItem[] assets, Func<ResourceCandidate, str
}

RetargetedAssets = retargetdAssets.ToArray();
PartialAppManifests = GenerateFontPartialManifest(fontAssets);
PartialAppManifests = GenerateFontPartialManifest(fontAssets, IosAppManifest);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
IntermediateOutputPath="$(IntermediateOutputPath)"
ContentItems="@(Content);@(_SourceItemsToCopyToOutputDirectory)"
AndroidAssetsPrefix="$(MonoAndroidAssetsPrefix)"
IosAppManifest="$(AppBundleManifest)"
Condition="'$(XamarinProjectType)'!=''">
<Output TaskParameter="Assets"
ItemName="Assets" />
Expand Down

0 comments on commit 9204b8f

Please sign in to comment.