Esempio n. 1
0
        private Bitmap LoadImage(ImageSource imagesource, Context context)
        {
            Bitmap image = null;

            if (imagesource is IconImageSource iconsource && FontRegistry.HasFont(iconsource.Name, out var font))
            {
                var paint = new Paint
                {
                    TextSize  = TypedValue.ApplyDimension(ComplexUnitType.Dip, (float)iconsource.Size, context.Resources.DisplayMetrics),
                    Color     = (iconsource.Color != Color.Default ? iconsource.Color : Color.White).ToAndroid(),
                    TextAlign = Paint.Align.Left,
                    AntiAlias = true,
                };

                using (var typeface = Typeface.CreateFromAsset(context.ApplicationContext.Assets, font.FontFileName))
                    paint.SetTypeface(typeface);

                paint.SetTypeface(font.Alias.ToTypeFace());

                var glyph    = font.GetGlyph(iconsource.Name);
                var width    = (int)(paint.MeasureText(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(glyph, 0, baseline, paint);
            }

            return(image);
        }
Esempio n. 2
0
 private static void OnGlyphNameChanged(BindableObject bindable, object oldValue, object newValue)
 {
     if (bindable is IconSpan span && FontRegistry.HasFont(span.GlyphName, out var font))
     {
         span.FontFamily = font.FontFileName;
         span.Text       = font.GetGlyph(span.GlyphName);
     }
Esempio n. 3
0
        protected override async void OnAttached()
        {
            ImageSource source;

            if (!string.IsNullOrWhiteSpace(Effect.Icon) && FontRegistry.HasFont(Effect.Icon, out _))
            {
                source = new IconImageSource
                {
                    Color = Effect.LineColor,
                    Name  = Effect.Icon,
                    Size  = Effect.ImageWidth
                };
            }
            else if (Effect.ImageSource != null)
            {
                source = Effect.ImageSource;
            }
            else
            {
                return;
            }

            var handler = Xamarin.Forms.Internals.Registrar.Registered.GetHandlerForObject <IImageSourceHandler>(source);

            if (handler != null)
            {
                SetImage(await handler.LoadImageAsync(source));
            }
        }
        public static TElement SetIcon <TElement>(this TElement element, string icon, Color?color = null)
            where TElement : Element, IFontElement
        {
            if (!FontRegistry.HasFont(icon, out var font))
            {
                return(element);
            }

            var glyphColor = color ?? Color.Default;
            var glyph      = font.GetGlyph(icon);

            switch (element)
            {
            case Label label:
                SetIcon(label, font.FontFileName, glyph, glyphColor);
                break;

            case Span span:
                SetIcon(span, font.FontFileName, glyph, glyphColor);
                break;

            case Editor editor:
                SetIcon(editor, font.FontFileName, glyph, glyphColor);
                break;

            case Button button:
                SetIcon(button, font.FontFileName, glyph, glyphColor);
                break;

            case SearchBar searchBar:
                SetIcon(searchBar, font.FontFileName, glyph, glyphColor);
                break;
            }
            return(element);
        }
Esempio n. 5
0
        public Task <UIImage> LoadImageAsync(ImageSource imagesource, CancellationToken cancelationToken = default, float scale = 1)
        {
            UIImage image = null;

            if (imagesource is IconImageSource iconsource && FontRegistry.HasFont(iconsource.Name, out var font))
            {
                // This will allow lookup from the Embedded Fonts
                var glyph        = font.GetGlyph(iconsource.Name);
                var cleansedname = FontExtensions.CleanseFontName(font.Alias);
                var uifont       = UIFont.FromName(cleansedname ?? string.Empty, (float)iconsource.Size) ??
                                   UIFont.SystemFontOfSize((float)iconsource.Size);
                var iconcolor = iconsource.Color.IsDefault ? _defaultColor : iconsource.Color;
                var attString = new NSAttributedString(glyph, font: uifont, foregroundColor: iconcolor.ToUIColor());
                var imagesize = ((NSString)glyph).GetSizeUsingAttributes(attString.GetUIKitAttributes(0, out _));

                UIGraphics.BeginImageContextWithOptions(imagesize, false, 0f);
                var ctx          = new NSStringDrawingContext();
                var boundingRect = attString.GetBoundingRect(imagesize, (NSStringDrawingOptions)0, ctx);
                attString.DrawString(new RectangleF(
                                         (imagesize.Width / 2) - (boundingRect.Size.Width / 2),
                                         (imagesize.Height / 2) - (boundingRect.Size.Height / 2),
                                         imagesize.Width,
                                         imagesize.Height));
                image = UIGraphics.GetImageFromCurrentImageContext();
                UIGraphics.EndImageContext();

                if (image != null && iconcolor != _defaultColor)
                {
                    image = image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
                }
            }
            return(Task.FromResult(image));
        }
Esempio n. 6
0
 private static void OnSelectorChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
 {
     if (!string.IsNullOrEmpty((string)args.NewValue) &&
         FontRegistry.HasFont((string)args.NewValue, out var font) &&
         dependencyObject is IconBlock iconBlock)
     {
     }
 }
        public void RegistryReturnsExpectedChar(string icon, string expectedGlyph)
        {
            Assert.True(FontRegistry.HasFont(icon, out var font));
            var glyph = font.GetGlyph(icon);

            Assert.False(string.IsNullOrEmpty(glyph));
            Assert.Equal(expectedGlyph, glyph);
        }
Esempio n. 8
0
        public Task <WindowsImageSource> LoadImageAsync(ImageSource imagesource, CancellationToken cancellationToken = default)
        {
            if (!(imagesource is IconImageSource iconsource) || !FontRegistry.HasFont(iconsource.Name, out var font))
            {
                return(Task.FromResult(default(WindowsImageSource)));
            }

            return(new FontImageSourceHandler().LoadImageAsync(iconsource.ToFontImageSource(font), cancellationToken));
        }
        public Task <Bitmap> LoadImageAsync(ImageSource imagesource, Context context, CancellationToken cancelationToken = default)
        {
            Bitmap image = null;

            if (imagesource is IconImageSource iconsource && FontRegistry.HasFont(iconsource.Name, out var font))
            {
                return(new FontImageSourceHandler().LoadImageAsync(iconsource.ToFontImageSource(font), context, cancelationToken));
            }

            return(Task.FromResult(image));
        }
        public static FontImageSource SetIcon(this FontImageSource imageSource, string icon, Color?color = null)
        {
            if (FontRegistry.HasFont(icon, out var font))
            {
                imageSource.Glyph      = font.GetGlyph(icon);
                imageSource.FontFamily = font.FontFileName;
                imageSource.Color      = color ?? Color.Default;
            }

            return(imageSource);
        }
        public Task <UIImage> LoadImageAsync(
            ImageSource imagesource,
            CancellationToken cancelationToken = default(CancellationToken),
            float scale = 1f)
        {
            UIImage image = null;

            if (imagesource is IconImageSource iconsource && FontRegistry.HasFont(iconsource.Name, out var font))
            {
                return(new FontImageSourceHandler().LoadImageAsync(iconsource.ToFontImageSource(font), cancelationToken, scale));
            }

            return(Task.FromResult(image));
        }
Esempio n. 12
0
        private WindowsImageSource LoadImage(ImageSource imagesource)
        {
            if (!(imagesource is IconImageSource iconsource) || !FontRegistry.HasFont(iconsource.Name, out var icon))
            {
                return(null);
            }

            var device = CanvasDevice.GetSharedDevice();
            var dpi    = Math.Max(_minimumDpi, Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi);

            var textFormat = new CanvasTextFormat
            {
                FontFamily          = icon.FontFileName,
                FontSize            = (float)iconsource.Size,
                HorizontalAlignment = CanvasHorizontalAlignment.Center,
                VerticalAlignment   = CanvasVerticalAlignment.Center,
                Options             = CanvasDrawTextOptions.Default
            };

            var glyph = icon.GetGlyph(iconsource.Name);

            using (var layout = new CanvasTextLayout(device, glyph, textFormat, (float)iconsource.Size, (float)iconsource.Size))
            {
                var canvasWidth  = (float)layout.LayoutBounds.Width + 2;
                var canvasHeight = (float)layout.LayoutBounds.Height + 2;

                var imageSource = new CanvasImageSource(device, canvasWidth, canvasHeight, dpi);
                using (var ds = imageSource.CreateDrawingSession(Windows.UI.Colors.Transparent))
                {
                    var iconcolor = (iconsource.Color != Color.Default ? iconsource.Color : Color.White).ToWindowsColor();

                    // offset by 1 as we added a 1 inset
                    var x = (float)layout.DrawBounds.X * -1;

                    ds.DrawTextLayout(layout, x, 1f, iconcolor);
                }

                return(imageSource);
            }
        }
        private static void OnIconChanged(BindableObject bindable, object oldValue, object newValue)
        {
            string fontFamily = null;
            string glyph      = null;
            string selector   = (string)bindable.GetValue(IconProperty);

            if (!string.IsNullOrEmpty(selector) && FontRegistry.HasFont(selector, out var font))
            {
                fontFamily = font.FontFileName;
                glyph      = font.GetGlyph(selector);
            }

            switch (bindable)
            {
            case Label label:
                label.Text       = glyph;
                label.FontFamily = fontFamily;
                break;

            case Button btn:
                btn.Text       = glyph;
                btn.FontFamily = fontFamily;
                break;

            case Span span:
                span.Text       = glyph;
                span.FontFamily = fontFamily;
                break;

            case MenuItem menuItem:
                menuItem.IconImageSource = CreateIconImageSource(bindable, selector);
                break;

            default:
                FontRegistry.OnChanged?.Invoke(bindable, selector, glyph, fontFamily);
                break;
            }
        }
Esempio n. 14
0
        public string ProvideValue(IServiceProvider serviceProvider)
        {
            if (serviceProvider is null)
            {
                throw new ArgumentNullException("The IconGlyphExtension requires a ServiceProvider");
            }

            if (FontRegistry.HasFont(IconName, out var font))
            {
                var provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
                var element            = provideValueTarget.TargetObject;
                var elementType        = element.GetType();
                var fontFamilyProperty = elementType.GetProperty("FontFamily");
                if (fontFamilyProperty is null)
                {
                    throw new NotSupportedException($"The target element {elementType.FullName} does not have a property for the FontFamily. This element is not supported for Icon Glyphs");
                }

                fontFamilyProperty.SetValue(element, font.FontFileName);
                return(font.GetGlyph(IconName));
            }

            return("Unknown Icon");
        }