-using System.IO;
+using System;
+using System.IO;
using System.Threading;
using System.Threading.Tasks;
using AppKit;
+using CoreGraphics;
+using CoreText;
+using Foundation;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Platform.MacOS
return image;
}
}
+
+ public sealed class FontImageSourceHandler : IImageSourceHandler
+ {
+ readonly Color _defaultColor = Color.White;
+
+ public Task<NSImage> LoadImageAsync(
+ ImageSource imagesource,
+ CancellationToken cancelationToken = default(CancellationToken),
+ float scale = 1f)
+ {
+ NSImage image = null;
+ var fontsource = imagesource as FontImageSource;
+ if (fontsource != null)
+ {
+ var font = NSFont.FromFontName(fontsource.FontFamily ?? string.Empty, (float)fontsource.Size) ??
+ NSFont.SystemFontOfSize((float)fontsource.Size);
+ var iconcolor = fontsource.Color.IsDefault ? _defaultColor : fontsource.Color;
+ var attString = new NSAttributedString(fontsource.Glyph, font: font, foregroundColor: iconcolor.ToNSColor());
+ var imagesize = ((NSString)fontsource.Glyph).StringSize(attString.GetAttributes(0, out _));
+
+ using (var context = new CGBitmapContext(IntPtr.Zero, (nint)imagesize.Width, (nint)imagesize.Height, 8, (nint)imagesize.Width * 4, NSColorSpace.GenericRGBColorSpace.ColorSpace, CGImageAlphaInfo.PremultipliedFirst))
+ {
+ using (var ctline = new CTLine(attString))
+ {
+ ctline.Draw(context);
+ }
+
+ using (var cgImage = context.ToImage())
+ {
+ image = new NSImage(cgImage, imagesize);
+ }
+ }
+ }
+
+ return Task.FromResult(image);
+ }
+ }
}
\ No newline at end of file
[assembly: ExportImageSourceHandler(typeof(FileImageSource), typeof(FileImageSourceHandler))]
[assembly: ExportImageSourceHandler(typeof(StreamImageSource), typeof(StreamImagesourceHandler))]
[assembly: ExportImageSourceHandler(typeof(UriImageSource), typeof(ImageLoaderSourceHandler))]
+[assembly: ExportImageSourceHandler(typeof(FontImageSource), typeof(FontImageSourceHandler))]
[assembly: ExportRenderer(typeof(Page), typeof(PageRenderer))]
[assembly: ExportRenderer(typeof(CarouselPage), typeof(CarouselPageRenderer))]
[assembly: ExportRenderer(typeof(MasterDetailPage), typeof(MasterDetailPageRenderer))]