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

Commit

Permalink
Added Android implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
GalaxiaGuy committed Mar 29, 2020
1 parent e156bb7 commit fb51a94
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 13 deletions.
1 change: 1 addition & 0 deletions Xamarin.Forms.Platform.Android/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
[assembly: ExportImageSourceHandler(typeof(StreamImageSource), typeof(StreamImagesourceHandler))]
[assembly: ExportImageSourceHandler(typeof(UriImageSource), typeof(ImageLoaderSourceHandler))]
[assembly: ExportImageSourceHandler(typeof(FontImageSource), typeof(FontImageSourceHandler))]
[assembly: ExportImageSourceHandler(typeof(LayeredFontImageSource), typeof(FontImageSourceHandler))]
[assembly: Xamarin.Forms.Dependency(typeof(Deserializer))]
[assembly: Xamarin.Forms.Dependency(typeof(ResourcesProvider))]
[assembly: Preserve]
Expand Down
86 changes: 74 additions & 12 deletions Xamarin.Forms.Platform.Android/Renderers/FontImageSourceHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Android.Content;
Expand All @@ -9,31 +9,93 @@ namespace Xamarin.Forms.Platform.Android
{
public sealed class FontImageSourceHandler : IImageSourceHandler
{
private class LayerProperties
{
public Paint Paint { get; set; }
public int Baseline { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public string Glyph { get; set; }
}

public Task<Bitmap> LoadImageAsync(
ImageSource imagesource,
ImageSource imageSource,
Context context,
CancellationToken cancelationToken = default(CancellationToken))
{
Bitmap image = null;
var fontsource = imagesource as FontImageSource;
if (fontsource != null)
var layerPropertiesList = new List<LayerProperties>();
var maxWidth = 0;
var maxHeight = 0;

if (imageSource is LayeredFontImageSource layeredFontImageSource)
{
var baseSize = TypedValue.ApplyDimension(ComplexUnitType.Dip, (float)layeredFontImageSource.Size, context.Resources.DisplayMetrics);
var baseColor = (layeredFontImageSource.Color != Color.Default ? layeredFontImageSource.Color : Color.White).ToAndroid();
var baseFont = layeredFontImageSource.FontFamily.ToTypeFace();

foreach (var layer in layeredFontImageSource.Layers)
{
var size = layer.IsSet(FontImageSource.SizeProperty) ?
TypedValue.ApplyDimension(ComplexUnitType.Dip, (float)layer.Size, context.Resources.DisplayMetrics) :
baseSize;
var color = layer.Color.IsDefault ? baseColor : layer.Color.ToAndroid();
var font = layer.FontFamily == null ? baseFont : layer.FontFamily.ToTypeFace();
var paint = new Paint
{
TextSize = size,
Color = (layer.Color != Color.Default ? layer.Color : Color.White).ToAndroid(),
TextAlign = Paint.Align.Left,
AntiAlias = true,
};

paint.SetTypeface(font);

var width = (int)(paint.MeasureText(layer.Glyph) + .5f);
var baseline = (int)(-paint.Ascent() + .5f);
var height = (int)(baseline + paint.Descent() + .5f);

if (width > maxWidth)
{
maxWidth = width;
}
if (height > maxHeight)
{
maxHeight = height;
}

layerPropertiesList.Add(new LayerProperties { Paint = paint, Baseline = (int)(-paint.Ascent() + .5f), Width = width, Height = height, Glyph = layer.Glyph });
}
}
else if (imageSource is FontImageSource fontSource)
{
var paint = new Paint
{
TextSize = TypedValue.ApplyDimension(ComplexUnitType.Dip, (float)fontsource.Size, context.Resources.DisplayMetrics),
Color = (fontsource.Color != Color.Default ? fontsource.Color : Color.White).ToAndroid(),
TextSize = TypedValue.ApplyDimension(ComplexUnitType.Dip, (float)fontSource.Size, context.Resources.DisplayMetrics),
Color = (fontSource.Color != Color.Default ? fontSource.Color : Color.White).ToAndroid(),
TextAlign = Paint.Align.Left,
AntiAlias = true,
};

paint.SetTypeface(fontsource.FontFamily.ToTypeFace());
paint.SetTypeface(fontSource.FontFamily.ToTypeFace());

var width = (int)(paint.MeasureText(fontsource.Glyph) + .5f);
var width = (int)(paint.MeasureText(fontSource.Glyph) + .5f);
var baseline = (int)(-paint.Ascent() + .5f);
var height = (int)(baseline + paint.Descent() + .5f);
image = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888);
var canvas = new Canvas(image);
canvas.DrawText(fontsource.Glyph, 0, baseline, paint);

maxWidth = width;
maxHeight = height;

layerPropertiesList.Add(new LayerProperties { Paint = paint, Baseline = (int)(-paint.Ascent() + .5f), Width = width, Height = height, Glyph = fontSource.Glyph });
}

var image = Bitmap.CreateBitmap(maxWidth, maxHeight, Bitmap.Config.Argb8888);
var canvas = new Canvas(image);

foreach (var layer in layerPropertiesList)
{
var x = (maxWidth - layer.Width) / 2;
var y = (maxHeight - layer.Height) / 2 + layer.Baseline;
canvas.DrawText(layer.Glyph, x, y, layer.Paint);
}

return Task.FromResult(image);
Expand Down
1 change: 0 additions & 1 deletion Xamarin.Forms.Platform.MacOS/ImageSourceHandlers.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AppKit;
Expand Down

0 comments on commit fb51a94

Please sign in to comment.