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

Min viable FontImageSource #4747

Merged
merged 4 commits into from
Jan 3, 2019
Merged

Min viable FontImageSource #4747

merged 4 commits into from
Jan 3, 2019

Conversation

kingces95
Copy link
Contributor

@kingces95 kingces95 commented Dec 15, 2018

Description of Change

FontImageSource derives from ImageSource and accepts a glyph (character) from a font plus a size and color which it'll use to create a .png which can then be displayed as an image. Unfortunately, many places where you'd like to use this currently accept a FileImageSource but that'll be a separate work item.

Issues Resolved

  • fixes Xamarin.Forms.FontIconSource Spec #3189 tho that spec has aspirations of unifying the metadata needed to identify the font such that the XAML is simplified to a single word. That's a great idea but is beyond what's necessary for a minimum viable feature. And anyway, abstracting resources across platforms is a larger issue.

API Changes

Added:
FontImageSource
see https://github.com/xamarin/Xamarin.Forms/compare/font-image-source?expand=1#diff-6e54598e403bbd75200d983f2182df8fR564

Before/After Screenshots

snag_cad069f

Testing Procedure

Run Font ImageSource - Legacy

@charlesroddie
Copy link

Why the intermediate png?

@samhouts samhouts added this to In Review in v3.6.0 Dec 15, 2018
@samhouts samhouts added this to Ready for Review (PRs) in Sprint 145 Dec 15, 2018
@samhouts samhouts added this to Ready for Review (PRs) in Sprint 146 Dec 17, 2018
@samhouts samhouts added the e/6 🕕 6 label Dec 17, 2018
{
public partial class FontImageSource : ImageSource
{
private static BindableProperty CreateBindableProperty<T>(
Copy link
Member

@StephaneDelcroix StephaneDelcroix Dec 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd avoid trying to be smart here. it makes the code harder to understand in the big picture. this code is basically boilerplate. And I agree we could make this boilerplate thinner, but not here, ideally...


namespace Xamarin.Forms
{
public partial class FontImageSource : ImageSource
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ImageSourceConverter needs to be extended with a new format to create a FontImageSource easily from XAML. please propose something

Copy link
Member

@StephaneDelcroix StephaneDelcroix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weren't we discussing an UWP approach too ?

@paymicro
Copy link
Contributor

I have an implementation for UWP. Do we need to add this?
screenshot_1

@paymicro paymicro mentioned this pull request Dec 18, 2018
3 tasks
@samhouts samhouts added the approved Has two approvals, no pending reviews, and no changes requested label Dec 18, 2018
@mattleibow mattleibow self-requested a review December 18, 2018 19:30
Xamarin.Forms.Controls/CoreGallery.cs Outdated Show resolved Hide resolved
Xamarin.Forms.Platform.Android/Renderers/FontExtensions.cs Outdated Show resolved Hide resolved
Xamarin.Forms.Core/FontImageSource.cs Outdated Show resolved Hide resolved
Pavel Yakovlev and others added 2 commits December 19, 2018 10:36
* [UWP] font image source

* address comment

* [UWP] performance optimization of the rendering of icons font
- the minimum Dpi of the icon is 300
@kingces95
Copy link
Contributor Author

Why the intermediate png?

To allow emoji to be used everywhere an ImageSource is allowed as opposed to just where a label is allowed. At least, that's the dream. We still need to change FileImageSource to ImageSource all over the place to realize the dream. And yes, the dream does come at the cost of drawing a png...

@mattleibow
Copy link
Contributor

If we cache the image somehow, then this really is an optimization - in a sense - because we don't have to redraw the emoji each time.

return null;

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note about DPI on UWP: it can change during the lifetime of an app and even the control. The density can be changed via settings, but also by just dragging the app to a new monitor. I have a 200% surface and a 150% externbal display.

This might also change if accessibility is used - possibly on iOS and Android as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Truth! But should fixing that use case block the PR? Kinda sounds like a corner case on a corner platform, no? Otherwise, we'll have to detect that the DPI changed and then run around and update glyphs? Sounds icky (or is it simple)? Do we think this is the only place we have that issue? Which is to say, if there are other places we cache DPI specific resources then how about we open an enhancement, subject it to prioritization, and fix 'em all at once.

I'm outta chaff.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll have to detect that the DPI changed and then run around and update glyphs?

it simple.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this is not a blocking issue at this time, but just wanted to make sure we all knew about this possibility. And, as you mention, we possibly have the same issue in other areas - maybe we need another issue...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. So we'll open a separate "Fix multimon DPI change on UAP" and fixing FontImageSource can be task one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. We probably can actually make this a public concept - the app may want to adjust to things like this. I was doing this for macOS for Xamarin.Essentials - a way to track when the resolution/scale changes. If the OS does not support it, then the event just never fires.

macOS

NSNotificationCenter.DefaultCenter.AddObserver(NSApplication.DidChangeScreenParametersNotification, OnDidChangeScreenParameters);

UWP:

var di = DisplayInformation.GetForCurrentView();
di.DpiChanged += OnDisplayInformationChanged;

else
{
throw new InvalidCastException($"\"{Control.Source.GetType().FullName}\" is not supported.");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this into an extension method because it probably needs to be used on other image-bearing controls - such as image buttons or page icons.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More Truth! But if we're gonna go on a de-duplication jihad, how about we start with BP definition boilerplate (see above...).

@kingces95
Copy link
Contributor Author

Caching the images would be a win, but how about we wait for a customer to complain first. Not trying to be facetious, but caches also have their problems... They take up memory, they need to be flushed, they need to be invalidated (if we ever do the DPI stuff). If anything, I'd expose the png so the user can cache and own the policy.

Or do we already have an image cache?

@samhouts
Copy link
Member

samhouts commented Jan 3, 2019

Failing tests are unrelated to this PR

@samhouts samhouts merged commit 5716f8d into master Jan 3, 2019
v3.6.0 automation moved this from In Review to Done Jan 3, 2019
Sprint 146 automation moved this from Ready for Review (PRs) to Done Jan 3, 2019
@samhouts samhouts deleted the font-image-source branch January 3, 2019 17:46
PureWeen pushed a commit that referenced this pull request Jan 24, 2019
* min viable fontImageSource

* [UWP] Min viable FontImageSource (#4817)

* [UWP] font image source

* address comment

* [UWP] performance optimization of the rendering of icons font
- the minimum Dpi of the icon is 300

* char -> string; CR feedback
andreinitescu pushed a commit to andreinitescu/Xamarin.Forms that referenced this pull request Jan 29, 2019
* min viable fontImageSource

* [UWP] Min viable FontImageSource (xamarin#4817)

* [UWP] font image source

* address comment

* [UWP] performance optimization of the rendering of icons font
- the minimum Dpi of the icon is 300

* char -> string; CR feedback
@samhouts samhouts added this to Done in v4.0.0 Feb 2, 2019
@samhouts samhouts removed this from Done in v3.6.0 Feb 2, 2019
@samhouts samhouts modified the milestones: 4.0.0, 3.6.0 Feb 6, 2019
@samhouts samhouts removed this from Done in v4.0.0 Feb 7, 2019
@samhouts samhouts added this to Done in v3.6.0 Feb 7, 2019
@minaairsupport
Copy link

Nice feature but I think it will be better if you have a helper class like that as no one will memorize all this Glyph

https://github.com/neilkennedy/FontAwesome.Xamarin/blob/master/FontAwesome.Xamarin/FontAwesome.cs

@andreinitescu
Copy link
Contributor

Is it really necessary to keep an array in memory, even if it's just 4 items, having to iterate over it using a loop with temporary variables?
https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/FontImageSource.cs#L47
Why not just use a switch like how all the other code in the framework uses

Also having CreateBindableProperty just in that class seems odd to me, at least why not put it in BindableObject or an utility class...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
approved Has two approvals, no pending reviews, and no changes requested e/6 🕕 6 p/Android p/iOS 🍎 t/enhancement ➕
Projects
No open projects
Sprint 145
  
Ready for Review (PRs)
Sprint 146
  
Done
v3.6.0
  
Done
Development

Successfully merging this pull request may close these issues.

Xamarin.Forms.FontIconSource Spec
8 participants