awesome-inc / FontAwesome.Sharp

A library for using Font Awesome in WPF & Windows Forms applications
Apache License 2.0
384 stars 89 forks source link

Specify IconFont when using IconChar.ToBitmap() [WinForms] #54

Closed hanlectin closed 3 years ago

hanlectin commented 3 years ago

Hi! would it be possible to add an (optional?) parameter to the .ToBitmap() fiunction which allows explicitly specifying which IconFont to use? it currently seems to use regular (if available) - but I sometimes need the solid version (e.g. for "disk")

thanks

mkoertgen commented 3 years ago

Hi @hanlectin ,

Some time ago we had a similar request and added the option to set the fontstyle, cf.:

Let me know if this helps.

hanlectin commented 3 years ago

thanks - I've already seen this, but I'm afraid this doesn't help in my case :( I really need to convert the IconChar directly to a bitmap. I cannot use the WinForm Controls IconButton, MenuItem, etc. so in my opinion the .ToBitmap() method really misses an option to specify the fontstyle...

mkoertgen commented 3 years ago

Ah, i see. Would using the extension method on font family instead of iconchar be an option?

example:

var bitmap = myfont.ToBitmap(icon, ...)
mkoertgen commented 3 years ago

To make quick access to often used fonts (fa-brand, fa-solid, fa-regular, .. ) more developer friendly we can probably add some lookup-shortcuts for the internally loaded fonts,

I probably plan on spending some time on this over the weekend. I would also be happy to accept a PR on this. So, if you can & want help, let me know.

hanlectin commented 3 years ago

I was already wondering how to instantiate or access a fa-FontFamily... looking at FontAwesome.Sharp-master it seems I can't, right? (everything I'd need is private or internal) ...this was before I saw your subsequent post!

so: 1) yes, the extension method on font family would help! (if I could access the internally loaded fonts) 2) I still feel like IconChar.ToBitmap(..) would be a cleaner and more consistent solution 3) making the internally used fa-fonts available seems like a good idea

I'd be happy to help if I can ;)

mkoertgen commented 3 years ago

Hi @hanlectin, i invested in some cleanup & documentation and also added some helper methods, cf.:

Please review with 5.15.2 once it is deployed.

hanlectin commented 3 years ago

thanks for your efforts and sorry for the late answer... I tested v5.12.3 - and it's almost working. or maybe I just get things wrong?

IconChar fa_icon = IconChar.Save;
Bitmap bmp;

bmp = fa_icon.ToBitmap(IconFont.Auto, 256, Color.Black);    //OK! disk-outlined
bmp = fa_icon.ToBitmap(IconFont.Brands, 256, Color.Black);  //OK! empty (=no brand icon)
bmp = fa_icon.ToBitmap(IconFont.Regular, 256, Color.Black); //ERR? empty (should be disk-outlined, right?)
bmp = fa_icon.ToBitmap(IconFont.Solid, 256, Color.Black);   //OK! disk-solid

IMHO using "IconFont.Regular" should return the same bitmap as "IconFont.Auto" - i.e. an outlined disk...

another (minor) issue: the returned bitmaps are not centered (shifted to the top-left). this is most likely correct, as the IconChars represent a font/letter and due to the font's baseline- and advance-offset this seems logical to me. I currently simply shift the image by a few percent to the right and to the bottom. but I use empirical percentages here. --> is there a way to calculate the correct offsets?

mkoertgen commented 3 years ago

Hi @hanlectin ,

With respect to centering there has been some ongoing discussion resulting in some slight enhancements, see

mkoertgen commented 3 years ago

With regard to the save-icon: This should be it right?

hanlectin commented 3 years ago

With regard to the save-icon: This should be it right?

yes - that would be correct. however, this "disk"-icon is only returned with bmp = fa_icon.ToBitmap(IconFont.Auto, 256, Color.Black); //OK! disk-outlined the following returns an empty bitmap: bmp = fa_icon.ToBitmap(IconFont.Regular, 256, Color.Black); //ERR? empty (should be disk-outlined, right?)

...I could perfectly live with that. I could only use .Auto and .Bold (no reason to use .Regular, as .Auto returns the .Regular as a default) howerver, I think .Regular should also return the same icon as .Auto, right?

(and thanks for pointing me to the discussion on centering the icon!)

mkoertgen commented 3 years ago

Hi @hanlectin , Sounds like you found a bug ;-)

mkoertgen commented 3 years ago

I had some time to look into it. The auto-font heuristic may cache the "Brands Regular"-font instead of the "Regular"-font.

{Name = "Font Awesome 5 Brands Regular"}
{Name = "Font Awesome 5 Free Regular"}
{Name = "Font Awesome 5 Free Solid"}

So in case you look up a brand-icon before a regular-icon the the library will lookup the wrong font resulting in an empty image.