Skip to content

Commit

Permalink
Clean some things up.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Brown authored and GalaxiaGuy committed Mar 29, 2020
1 parent fb14d3a commit 88c7fc9
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 40 deletions.
86 changes: 69 additions & 17 deletions Xamarin.Forms.Controls/GalleryPages/FontImageSourceGallery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,6 @@ public FontImageSourceGallery()
});

var i = 1;
foreach (char c in Ionicons)
{
grid.Children.Add(new Image
{
Source = new FontImageSource
{
Glyph = c.ToString(),
FontFamily = fontFamily,
Size = 20
},
BackgroundColor = Color.Black,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
}, i % 10, i / 10);
i++;
}

grid.Children.Add(new Image
{
Expand All @@ -79,7 +63,7 @@ public FontImageSourceGallery()
{
Glyph = '\uf100'.ToString(),
FontFamily = fontFamily,
Color = Color.Red,
Color = Color.Yellow,
Size = 20
},
},
Expand All @@ -88,6 +72,74 @@ public FontImageSourceGallery()
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
}, i % 10, i / 10);
i++;

grid.Children.Add(new Image
{
Source = new LayeredFontImageSource
{
FontFamily = fontFamily,
Size = 20,
Layers = new List<FontImageSource> {
new FontImageSource
{
Glyph = '\uf23e'.ToString(),
Color = Color.White,
},
new FontImageSource
{
Glyph = '\uf23f'.ToString(),
Color = Color.CornflowerBlue,
},
},
},
BackgroundColor = Color.Black,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
}, i % 10, i / 10);
i++;

grid.Children.Add(new Image
{
Source = new LayeredFontImageSource
{
FontFamily = fontFamily,
Size = 20,
Layers = new List<FontImageSource> {
new FontImageSource
{
Glyph = '\uf24c'.ToString(),
Color = Color.White,
},
new FontImageSource
{
Glyph = '\uf24d'.ToString(),
Color = Color.Red,
},
},
},
BackgroundColor = Color.Black,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
}, i % 10, i / 10);
i++;

foreach (char c in Ionicons)
{
grid.Children.Add(new Image
{
Source = new FontImageSource
{
Glyph = c.ToString(),
FontFamily = fontFamily,
Size = 20
},
BackgroundColor = Color.Black,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
}, i % 10, i / 10);
i++;
}

Content = new ScrollView
{
Expand Down
5 changes: 3 additions & 2 deletions Xamarin.Forms.Core/LayeredFontImageSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Xamarin.Forms
{
[ContentProperty(nameof(Layers))]
public class LayeredFontImageSource : ImageSource
{
public override bool IsEmpty => Layers == null || Layers.Count == 0 || Layers.All(x => x.IsEmpty);
Expand Down Expand Up @@ -54,7 +55,7 @@ public double Size
set => SetValue(SizeProperty, value);
}

private void OnLayersChanged(IList<FontImageSource> oldLayers, IList<FontImageSource> newLayers)
void OnLayersChanged(IList<FontImageSource> oldLayers, IList<FontImageSource> newLayers)
{
if (oldLayers != null)
{
Expand All @@ -73,7 +74,7 @@ private void OnLayersChanged(IList<FontImageSource> oldLayers, IList<FontImageSo
OnSourceChanged();
}

private void LayerSourceChanged(object sender, EventArgs e)
void LayerSourceChanged(object sender, EventArgs e)
{
OnSourceChanged();
}
Expand Down
46 changes: 25 additions & 21 deletions Xamarin.Forms.Platform.iOS/Renderers/ImageRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ public sealed class FontImageSourceHandler : IImageSourceHandler
//should this be the default color on the BP for iOS?
readonly Color _defaultColor = Color.White;

static readonly double _defaultFontSize = 30;

[Preserve(Conditional = true)]
public FontImageSourceHandler()
{
Expand All @@ -264,27 +266,29 @@ private class LayerProperties
}

public Task<UIImage> LoadImageAsync(
ImageSource imagesource,
ImageSource imageSource,
CancellationToken cancelationToken = default(CancellationToken),
float scale = 1f)
{
UIImage image = null;
UIImage image;
float baseSize = 0;
var layerPropertiesList = new List<LayerProperties>();
CGSize maxImageSize = CGSize.Empty;
bool renderingModeAlwaysOriginal = false;
var maxImageSize = CGSize.Empty;
var renderingModeAlwaysOriginal = false;

if (imagesource is LayeredFontImageSource layeredFontImageSource)
if (imageSource is LayeredFontImageSource layeredFontImageSource)
{
var baseFont = UIFont.FromName(layeredFontImageSource.FontFamily ?? string.Empty, (float)layeredFontImageSource.Size) ??
UIFont.SystemFontOfSize((float)layeredFontImageSource.Size);
baseSize = (float)layeredFontImageSource.Size;
var baseFont = UIFont.FromName(layeredFontImageSource.FontFamily ?? string.Empty, baseSize) ?? UIFont.SystemFontOfSize(baseSize);
var baseIconColor = layeredFontImageSource.Color.IsDefault ? _defaultColor : layeredFontImageSource.Color;
var baseAttString = layeredFontImageSource.Glyph == null ? null : new NSAttributedString(layeredFontImageSource.Glyph, font: baseFont, foregroundColor: baseIconColor.ToUIColor());
maxImageSize = layeredFontImageSource.Glyph == null ? CGSize.Empty : ((NSString)layeredFontImageSource.Glyph).GetSizeUsingAttributes(baseAttString.GetUIKitAttributes(0, out _));

foreach (var layer in layeredFontImageSource.Layers)
{
var font = layer.FontFamily == null ? baseFont : UIFont.FromName(layer.FontFamily ?? string.Empty, (float)layer.Size) ??
UIFont.SystemFontOfSize((float)layer.Size);
var size = layer.Size == _defaultFontSize ? baseSize : (float)layer.Size;
var font = layer.FontFamily == null ? baseFont : UIFont.FromName(layer.FontFamily ?? string.Empty, size) ??
UIFont.SystemFontOfSize(size);
var iconcolor = layer.Color.IsDefault ? baseIconColor : layer.Color;
var attString = layer.Glyph == null ? baseAttString : new NSAttributedString(layer.Glyph, font: font, foregroundColor: iconcolor.ToUIColor());
var imagesize = ((NSString)layer.Glyph).GetSizeUsingAttributes(attString.GetUIKitAttributes(0, out _));
Expand All @@ -304,17 +308,17 @@ public Task<UIImage> LoadImageAsync(

renderingModeAlwaysOriginal = baseIconColor != _defaultColor;
}
else if (imagesource is FontImageSource fontsource)
else if (imageSource is FontImageSource fontSource)
{
var font = UIFont.FromName(fontsource.FontFamily ?? string.Empty, (float)fontsource.Size) ??
UIFont.SystemFontOfSize((float)fontsource.Size);
var iconcolor = fontsource.Color.IsDefault ? _defaultColor : fontsource.Color;
var attString = new NSAttributedString(fontsource.Glyph, font: font, foregroundColor: iconcolor.ToUIColor());
var imagesize = ((NSString)fontsource.Glyph).GetSizeUsingAttributes(attString.GetUIKitAttributes(0, out _));
var size = (float)fontSource.Size;
var font = UIFont.FromName(fontSource.FontFamily ?? string.Empty, size) ?? UIFont.SystemFontOfSize(size);
var iconColor = fontSource.Color.IsDefault ? _defaultColor : fontSource.Color;
var attString = new NSAttributedString(fontSource.Glyph, font: font, foregroundColor: iconColor.ToUIColor());
var imageSize = ((NSString)fontSource.Glyph).GetSizeUsingAttributes(attString.GetUIKitAttributes(0, out _));

layerPropertiesList.Add(new LayerProperties { Font = font, IconColor = iconcolor, AttString = attString, ImageSize = imagesize });
layerPropertiesList.Add(new LayerProperties { Font = font, IconColor = iconColor, AttString = attString, ImageSize = imageSize });

maxImageSize = imagesize;
maxImageSize = imageSize;
}

UIGraphics.BeginImageContextWithOptions(maxImageSize, false, 0f);
Expand All @@ -323,18 +327,18 @@ public Task<UIImage> LoadImageAsync(
foreach (var layerProperties in layerPropertiesList)
{
var font = layerProperties.Font;
var iconcolor = layerProperties.IconColor;
var iconColor = layerProperties.IconColor;
var attString = layerProperties.AttString;
var imagesize = layerProperties.ImageSize;
var imageSize = layerProperties.ImageSize;

var boundingRect = attString.GetBoundingRect(imagesize, (NSStringDrawingOptions)0, ctx);
var boundingRect = attString.GetBoundingRect(imageSize, (NSStringDrawingOptions)0, ctx);
attString.DrawString(new RectangleF(
maxImageSize.Width / 2 - boundingRect.Size.Width / 2,
maxImageSize.Height / 2 - boundingRect.Size.Height / 2,
maxImageSize.Width,
maxImageSize.Height));

renderingModeAlwaysOriginal |= iconcolor != _defaultColor;
renderingModeAlwaysOriginal |= iconColor != _defaultColor;
}
image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
Expand Down

0 comments on commit 88c7fc9

Please sign in to comment.