-
Notifications
You must be signed in to change notification settings - Fork 325
Generate icons from a font file #5504
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Copyright 2021 The Chromium Authors. All rights reserved. | ||
* Use of this source code is governed by a BSD-style license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
package io.flutter.utils; | ||
|
||
import com.intellij.openapi.diagnostic.Logger; | ||
import com.intellij.util.TripleFunction; | ||
import io.flutter.FlutterUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import javax.imageio.ImageIO; | ||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.awt.font.FontRenderContext; | ||
import java.awt.font.LineMetrics; | ||
import java.awt.geom.AffineTransform; | ||
import java.awt.geom.Rectangle2D; | ||
import java.awt.image.BufferedImage; | ||
import java.io.*; | ||
import java.util.Properties; | ||
|
||
@SuppressWarnings("UseJBColor") | ||
public class IconPreviewGenerator { | ||
private static final Logger LOG = Logger.getInstance(IconPreviewGenerator.class); | ||
|
||
@NotNull final String fontFilePath; | ||
int iconSize = 16; | ||
int fontSize = 16; | ||
@NotNull Color fontColor = Color.gray; | ||
|
||
public IconPreviewGenerator(@NotNull String fontFilePath) { | ||
this.fontFilePath = fontFilePath; | ||
} | ||
|
||
public IconPreviewGenerator(@NotNull String fontFilePath, int iconSize, int fontSize, @Nullable Color fontColor) { | ||
this(fontFilePath); | ||
this.iconSize = iconSize; | ||
this.fontSize = fontSize; | ||
if (fontColor != null) { | ||
this.fontColor = fontColor; | ||
} | ||
} | ||
|
||
public Icon convert(String hexString) { | ||
if (hexString.startsWith("0x") || hexString.startsWith("0X")) { | ||
hexString = hexString.substring(2); | ||
} | ||
return convert(Integer.parseInt(hexString, 16)); | ||
} | ||
|
||
public Icon convert(int code) { | ||
return runInGraphicsContext((BufferedImage image, Graphics2D graphics, FontRenderContext frc) -> { | ||
char ch = Character.toChars(code)[0]; | ||
String codepoint = Character.toString(ch); | ||
|
||
drawGlyph(codepoint, graphics, frc); | ||
return new ImageIcon(image); | ||
}); | ||
} | ||
|
||
// Given a file at path-to-font-properties in the format generated by tools_metadata (on github), | ||
// to generate double-size icons for all glyphs in a font: | ||
// IconPreviewGenerator ipg = new IconPreviewGenerator("path-to-ttf-file", 32, 32, Color.black); | ||
// ipg.batchConvert("path-to-out-dir", "path-to-font-properties", "@2x"); | ||
public void batchConvert(@NotNull String outPath, @NotNull String path, @NotNull String suffix) { | ||
final String outputPath = outPath.endsWith("/") ? outPath : outPath + "/"; | ||
//noinspection ResultOfMethodCallIgnored | ||
new File(outputPath).mkdirs(); | ||
|
||
runInGraphicsContext((BufferedImage image, Graphics2D graphics, FontRenderContext frc) -> { | ||
Properties fontMap = new Properties(); | ||
File file = new File(path); | ||
try (InputStream stream = new BufferedInputStream(new FileInputStream(file))) { | ||
fontMap.load(stream); | ||
for (Object nextKey : fontMap.keySet()) { | ||
if (!(nextKey instanceof String)) continue; | ||
String codepoint = (String)nextKey; | ||
if (!codepoint.endsWith(".codepoint")) continue; | ||
String iconName = fontMap.getProperty(codepoint); | ||
codepoint = codepoint.substring(0, codepoint.indexOf(".codepoint")); | ||
int code = Integer.parseInt(codepoint, 16); | ||
char ch = Character.toChars(code)[0]; | ||
codepoint = Character.toString(ch); | ||
|
||
drawGlyph(codepoint, graphics, frc); | ||
ImageIO.write(image, "PNG", new File(outputPath + iconName + suffix + ".png")); | ||
} | ||
} | ||
catch (IOException ex) { | ||
FlutterUtils.warn(LOG, ex); | ||
} | ||
return null; | ||
}); | ||
} | ||
|
||
private Icon runInGraphicsContext(TripleFunction<BufferedImage, Graphics2D, FontRenderContext, Icon> callback) { | ||
Icon result = null; | ||
Graphics2D graphics = null; | ||
//noinspection UndesirableClassUsage | ||
BufferedImage image = new BufferedImage(iconSize, iconSize, BufferedImage.TYPE_4BYTE_ABGR); | ||
try (InputStream inputStream = new FileInputStream(new File(fontFilePath))) { | ||
Font font = Font.createFont(Font.TRUETYPE_FONT, inputStream).deriveFont(Font.PLAIN, fontSize); | ||
graphics = image.createGraphics(); | ||
graphics.setFont(font); | ||
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | ||
FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, true); | ||
result = callback.fun(image, graphics, frc); | ||
} | ||
catch (IOException | FontFormatException ex) { | ||
FlutterUtils.warn(LOG, ex); | ||
} | ||
finally { | ||
if (graphics != null) graphics.dispose(); | ||
} | ||
return result; | ||
} | ||
|
||
private void drawGlyph(String codepoint, Graphics2D graphics, FontRenderContext frc) { | ||
Font font = graphics.getFont(); | ||
Rectangle2D rect = font.getStringBounds(codepoint, frc); | ||
LineMetrics metrics = font.getLineMetrics(codepoint, frc); | ||
float lineHeight = metrics.getHeight(); | ||
float ascent = metrics.getAscent(); | ||
float width = (float)rect.getWidth(); | ||
float x0 = (iconSize - width) / 2.0f; | ||
float y0 = (iconSize - lineHeight) / 2.0f + ascent + (lineHeight - ascent) / 2.0f; | ||
|
||
graphics.setComposite(AlphaComposite.Clear); | ||
graphics.fillRect(0, 0, iconSize, iconSize); | ||
|
||
graphics.setComposite(AlphaComposite.Src); | ||
graphics.setColor(fontColor); | ||
graphics.drawString(codepoint, x0, y0); | ||
} | ||
} |
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When is this single icon convert meant to be used? If the end user has a color hex in their code? If possible it would be nice to have a test for this method as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is the entry-point for the plugin to use while IntelliJ is walking the Dart AST. We'd look up the name if an icon was referenced by name, to get the hex number, or get the number from the AST if it was referenced by codepoint.