Skip to content

Commit

Permalink
chore: Wasm :/
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Jun 19, 2024
1 parent 9fdb66b commit 1201ae9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void SynchronizeHtmlParagraphAttributes()
{
ConditionalUpdate(ref _fontStyleChanged, () => this.SetFontStyle(FontStyle));
ConditionalUpdate(ref _fontWeightChanged, () => this.SetFontWeight(FontWeight));
ConditionalUpdate(ref _fontFamilyChanged, () => this.SetFontFamily(FontFamily));
ConditionalUpdate(ref _fontFamilyChanged, () => this.SetFontFamily(FontFamily, FontStyle, FontWeight, FontStretch));
ConditionalUpdate(ref _fontSizeChanged, () => this.SetFontSize(FontSize));
ConditionalUpdate(ref _maxLinesChanged, () => this.SetMaxLines(MaxLines));
ConditionalUpdate(ref _textAlignmentChanged, () => this.SetTextAlignment(TextAlignment));
Expand Down
57 changes: 57 additions & 0 deletions src/Uno.UI/UI/Xaml/FontFamily.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,68 @@ public partial class FontFamily
{
private FontFamilyLoader _loader;

/// <summary>
/// Contains the prefix of font-face name to use in CSS.
/// </summary>
internal string CssFontNamePrefix { get; private set; }

internal string? ExternalSource { get; private set; }

partial void Init(string fontName)
{
ParseSource(Source);
_loader = FontFamilyLoader.GetLoaderForFontFamily(this);
}

[MemberNotNull(nameof(CssFontNamePrefix))]
private void ParseSource(string source)
{
var sourceParts = source.Split('#', 2, StringSplitOptions.RemoveEmptyEntries);

if (sourceParts.Length > 0)
{
if (TryGetExternalUri(sourceParts[0], out var externalUri) && externalUri is { })
{
ExternalSource = externalUri.OriginalString;
CssFontNamePrefix = "font" + ExternalSource.GetHashCode();
}
else
{
CssFontNamePrefix = sourceParts[sourceParts.Length == 2 ? 1 : 0];
}
}
else
{
throw new InvalidOperationException("FontFamily source cannot be empty");
}
}

private static bool TryGetExternalUri(string? source, out Uri? uri)
{
if (source is not null && (source.IndexOf('.') > -1 || source.IndexOf('/') > -1))
{
uri = new Uri(source, UriKind.RelativeOrAbsolute);

if (!uri.IsAbsoluteUri || source.StartsWith('/'))
{
// Support for implicit ms-appx resolution
var assetUri = AssetsPathBuilder.BuildAssetUri(Uri.EscapeDataString(source.TrimStart('/')).Replace("%2F", "/"));
uri = new Uri(assetUri, UriKind.RelativeOrAbsolute);
}

if (Uno.UI.Xaml.XamlFilePathHelper.TryGetMsAppxAssetPath(uri, out var path))
{
var assetUri = AssetsPathBuilder.BuildAssetUri(path);
uri = new Uri(assetUri, UriKind.RelativeOrAbsolute);
}

return true;
}

uri = default;
return false;
}

/// <summary>
/// Use this to launch the loading of a font before it is actually required to
/// minimize loading time and prevent potential flicking.
Expand Down
6 changes: 3 additions & 3 deletions src/Uno.UI/UI/Xaml/UIElement.TextHelper.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal void SetFontWeight(object localValue)
}
}

internal void SetFontFamily(object localValue)
internal void SetFontFamily(object localValue, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch)
{
if (localValue == DependencyProperty.UnsetValue)
{
Expand All @@ -90,8 +90,8 @@ internal void SetFontFamily(object localValue)
{
font = FontFamily.Default;
}
// TODO: Maybe introduce CssFontNamePrefix to FontFamily, and use something like $"{font.CssFontNamePrefix}_{FontStyle}_{FontWeight}_{FontStretch}" ?
SetStyle("font-family", font.CssFontName);

SetStyle("font-family", $"{font.CssFontNamePrefix}_{fontStyle}_{fontWeight}_{fontStretch}");

font.RegisterForInvalidateMeasureOnFontLoaded(this);
}
Expand Down

0 comments on commit 1201ae9

Please sign in to comment.