Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FlatSVGIcon explicit scaling #196

Closed
xDUDSSx opened this issue Oct 23, 2020 · 3 comments
Closed

FlatSVGIcon explicit scaling #196

xDUDSSx opened this issue Oct 23, 2020 · 3 comments
Milestone

Comments

@xDUDSSx
Copy link
Contributor

xDUDSSx commented Oct 23, 2020

Hello,
I was wondering if there is any way to set a FlatSVGIcon's size directly that it would scale the svg itself into given dimensions like 16x16, eg. the same size as a 16x16 pixel ImageIcon.
From what I understand right now. The svg icon renders in whatever size the source .svg file has defined in the file itself.

To elaborate on why I'm interested in such functionality. When I download an .svg file from, say, an online icon library. It's not necessarily going to be 16x16 pixels and I have to use some tools to scale it into the size I desire.

Another issue arises when I want to have a set of 16 pixel icons and a bigger set that is like 24 pixels big for some other use cases. One would think that when I'm already using svg icons such icons could be generated by just scaling the same .svg file.

The ability to support multiple sizes is certainly part of the appeal of using svg icons. Am I missing something or is there no way to achieve that so it would work with hiDpi scaling and so on?

@xDUDSSx xDUDSSx changed the title SVG icon explicit scaling FlatSVGIcon explicit scaling Oct 23, 2020
@electriq
Copy link

It will be also useful to be able to create multiple FlatSVGIcon with different sizes from the same SVG file once loaded. So we have a single SVG icon file and can use it with size 16x16 in the main menu, 24x24 in the toolbar and 32x32 as an icon in the message dialog window, for example. Looks like overriding size in constructor is a good idea: Icon icon16 = new FlatSVGIcon(resourceName, 16, 16);. Or it may be implemented as a cloning method, sharing the SVGDiagram (but it seems it's too little resources to optimize here), like FlatSVGIcon icon16 = icon32.scaledInstance(16, 16); Now we actually need to store two or more identical SVG files with different sizes in resources.

@DevCharly
Copy link
Collaborator

Thx, this is now implemented in master branch.

There are additional constructors in FlatSVGIcon where you can pass width and height:

/**
* Creates an SVG icon from the given resource name with the given width and height.
* <p>
* The icon is scaled if the given size is different to the size specified in the SVG file.
*
* @param name the name of the SVG resource (a '/'-separated path)
* @param width the width of the icon
* @param height the height of the icon
* @see ClassLoader#getResource(String)
*/
public FlatSVGIcon( String name, int width, int height ) {

or a scale factor:

/**
* Creates an SVG icon from the given resource name that is scaled by the given amount.
* <p>
* The SVG attributes {@code width} and {@code height} (or {@code viewBox})
* in the tag {@code <svg>} are used as base icon size, which is multiplied
* by the given scale factor.
*
* @param name the name of the SVG resource (a '/'-separated path)
* @param scale the amount by which the icon size is scaled
* @see ClassLoader#getResource(String)
*/
public FlatSVGIcon( String name, float scale ) {

There are also derive methods that allow quick creation of icons in various sizes:

/**
* Creates a new icon with given width and height, which is derived from this icon.
*
* @param width the width of the new icon
* @param height the height of the new icon
* @return a new icon
*/
public FlatSVGIcon derive( int width, int height ) {

@DevCharly DevCharly added this to the 0.44 milestone Oct 26, 2020
@xDUDSSx
Copy link
Contributor Author

xDUDSSx commented Oct 26, 2020

Exactly what I had in mind. Works well, good job. Not just with this but with the project overall. Looks great and it's nice to see all the progress you're making with each update. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants