Skip to content

Commit

Permalink
[WPF] Fix lighter weight support (microsoft#2923)
Browse files Browse the repository at this point in the history
* Fix font being a bunch of fonts concatenated by strings

* Fix default behaviour
  • Loading branch information
almedina-ms committed May 17, 2019
1 parent 60c74d2 commit e2bcbd5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.MarkedNet;
using System.IO;
using System.Xml;
using System.Drawing.Text;

namespace AdaptiveCards.Rendering.Wpf
{
Expand Down Expand Up @@ -56,8 +57,10 @@ private static void AddInlineTextRun(TextBlock uiRichTB, AdaptiveTextRun textRun

textRunSpan.Style = context.GetStyle($"Adaptive.{textRun.Type}");

textRunSpan.FontFamily = new FontFamily(context.Config.GetFontFamily(textRun.FontStyle));
textRunSpan.FontFamily = new FontFamily(RendererUtil.GetFontFamilyFromList(context.Config.GetFontFamily(textRun.FontStyle)));

textRunSpan.FontWeight = FontWeight.FromOpenTypeWeight(context.Config.GetFontWeight(textRun.FontStyle, textRun.Weight));

textRunSpan.FontSize = context.Config.GetFontSize(textRun.FontStyle, textRun.Size);

if (textRun.Italic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private static TextBlock CreateControl(AdaptiveTextBlock textBlock, AdaptiveRend

uiTextBlock.TextWrapping = TextWrapping.NoWrap;

uiTextBlock.FontFamily = new FontFamily(context.Config.GetFontFamily(textBlock.FontStyle));
uiTextBlock.FontFamily = new FontFamily(RendererUtil.GetFontFamilyFromList(context.Config.GetFontFamily(textBlock.FontStyle)));
uiTextBlock.FontWeight = FontWeight.FromOpenTypeWeight(context.Config.GetFontWeight(textBlock.FontStyle, textBlock.Weight));
uiTextBlock.FontSize = context.Config.GetFontSize(textBlock.FontStyle, textBlock.Size);

Expand Down
28 changes: 28 additions & 0 deletions source/dotnet/Library/AdaptiveCards.Rendering.Wpf/RendererUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -11,6 +12,33 @@ namespace AdaptiveCards.Rendering.Wpf
{
static class RendererUtil
{
/// <summary>
/// Checks if any of the fonts to be used for the TexBlock is installed, and if so, returns it
/// </summary>
/// <param name="fontList">Font list to check if any exist</param>
/// <returns>First font in list that is installed, otherwise, default system font</returns>
public static string GetFontFamilyFromList(string fontList)
{
var installedFonts = new InstalledFontCollection();
string[] fontFamilies = fontList.Split(',');

for (int i = 0; i < fontFamilies.Length; ++i)
{
string fontFamily = fontFamilies[i].Trim('\'');

foreach (var installedFontFamily in installedFonts.Families)
{
if (installedFontFamily.Name == fontFamily)
{
return fontFamily;
}
}
}

// If no valid font was found in list, return the system default font
return SystemFonts.MessageFontFamily.ToString();
}

public static void ApplyVerticalContentAlignment(FrameworkElement uiElement, AdaptiveCollectionElement element)
{
switch (element.VerticalContentAlignment)
Expand Down

0 comments on commit e2bcbd5

Please sign in to comment.