Skip to content

Commit

Permalink
Handle font resource loading. Fixes #122
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Nov 9, 2020
1 parent 64fc42e commit 6187014
Show file tree
Hide file tree
Showing 144 changed files with 1,088 additions and 657 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
*/
package org.kordamp.ikonli;

import java.io.InputStream;
import java.net.URL;

/**
* @author Andres Almiray
*/
Expand All @@ -25,7 +28,9 @@ public interface IkonHandler {

Ikon resolve(String description);

String getFontResourcePath();
URL getFontResource();

InputStream getFontResourceAsStream();

String getFontFamily();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@

import org.kordamp.jipsy.ServiceProviderFor;

import java.io.InputStream;
import java.net.URL;

/**
* @author Andres Almiray
*/
@ServiceProviderFor(IkonHandler.class)
public class IkonliIkonResolver extends AbstractIkonHandler {
private static final String FONT_RESOURCE = "/META-INF/resources/ikonli/0.0.0/fonts/ikonli.ttf";

@Override
public boolean supports(String description) {
return description != null && description.startsWith("ikn-");
Expand All @@ -35,8 +40,13 @@ public Ikon resolve(String description) {
}

@Override
public String getFontResourcePath() {
return "META-INF/resources/ikonli/0.0.0/fonts/ikonli.ttf";
public URL getFontResource() {
return getClass().getResource(FONT_RESOURCE);
}

@Override
public InputStream getFontResourceAsStream() {
return getClass().getResourceAsStream(FONT_RESOURCE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public FontIcon(Ikon iconCode) {
setIconCode(iconCode);
}

@Override
public String toString() {
Ikon iconCode = getIconCode();
return (iconCode != null ? iconCode.getDescription() : "<undef>") + ":" + getIconSize() + ":" + getIconColor();
Expand Down Expand Up @@ -233,6 +234,7 @@ public void setIconLiteral(String iconCode) {
resolvePaint(iconCode, parts);
}

@Override
public List<CssMetaData<? extends Styleable, ?>> getCssMetaData() {
return FontIcon.getClassCssMetaData();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class IkonResolver extends AbstractIkonResolver {
ServiceLoader<IkonHandler> loader = ServiceLoader.load(IkonHandler.class.getModule().getLayer(), IkonHandler.class);
for (IkonHandler handler : loader) {
HANDLERS.add(handler);
handler.setFont(Font.loadFont(handler.getFontResourcePath(), 16));
handler.setFont(Font.loadFont(handler.getFontResource().toExternalForm(), 16));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import org.kordamp.ikonli.AbstractIkonResolver;
import org.kordamp.ikonli.IkonHandler;

import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.ServiceLoader;
Expand All @@ -36,13 +34,12 @@ public class IkonResolver extends AbstractIkonResolver {
static {
INSTANCE = new IkonResolver();

ClassLoader classLoader = IkonResolver.class.getClassLoader();
ServiceLoader<IkonHandler> loader = ServiceLoader.load(IkonHandler.class, classLoader);
ServiceLoader<IkonHandler> loader = ServiceLoader.load(IkonHandler.class.getModule().getLayer(), IkonHandler.class);
for (IkonHandler handler : loader) {
HANDLERS.add(handler);

try {
InputStream stream = classLoader.getResourceAsStream(handler.getFontResourcePath());
InputStream stream = handler.getFontResourceAsStream();
Font font = Font.createFont(Font.TRUETYPE_FONT, stream);
GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font);
stream.close();
Expand Down
16 changes: 12 additions & 4 deletions docs/guide/src/docs/asciidoc/authoring.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,13 @@ import org.kordamp.ikonli.AbstractIkonHandler;
import org.kordamp.ikonli.Ikon;
import org.kordamp.ikonli.IkonHandler;
import org.kordamp.jipsy.ServiceProviderFor;
import java.io.InputStream;
import java.net.URL;
@ServiceProviderFor(IkonHandler.class) // <1>
public class MyIconIkonHandler extends AbstractIkonHandler {
private static final String FONT_RESOURCE = "/META-INF/resources/myicon/1.2.3/fonts/myicon.ttf";
@Override
public boolean supports(String description) {
return description != null && description.startsWith("my-"); // <2>
Expand All @@ -75,9 +79,13 @@ public class MyIconIkonHandler extends AbstractIkonHandler {
}
@Override
public String getFontResourcePath() {
return getClass().getResource("/META-INF/resources/myicon/1.2.3/fonts/myicon.ttf")
.toExternalFrom(); // <3>
public URL getFontResource() {
return getClass().getResource(FONT_RESOURCE); // <3>
}
@Override
public InputStream getFontResourceAsStream() {
return getClass().getResourceAsStream(FONT_RESOURCE); // <3>
}
@Override
Expand All @@ -88,7 +96,7 @@ public class MyIconIkonHandler extends AbstractIkonHandler {
----
<1> Generates an entry at `META-INF/services/org.kordamp.ikonli.IkonHandler`.
<2> Note the use of the icon prefix.
<3> Location of the font resource.
<3> Locate the font resource.
<4> Name of the font family.

The use of the `@ServiceProviderFor` annotation requires having link:https://github.com/kordamp/jipsy/[jipsy] configured
Expand Down
2 changes: 2 additions & 0 deletions docs/guide/src/docs/asciidoc/introduction.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ If you are upgrading to `12.0.0` or later be aware of the following changes:
* The `icon-hawkconsfilled-pack` and `icon-hawkconsstroke-pack` icon packs were merged into a single `icon-hawkcons-pack` icon pack.
* The method `getCode()` of `org.kordamp.ikonli.Ikon` now returns `int` instead of `char`. This change is needed to accomodate
icon packs whose icon codes are larger than a single char, such as MaterialDesign.
* The interface `org.kordamp.ikonli.IkonHandler` no longer has a method `String getFontResourcePath()`; it has two new
methods instead: `URL getFontResource()` and `InputStream getFontResourceAsStream()`.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@
import org.kordamp.ikonli.IkonHandler;
import org.kordamp.jipsy.ServiceProviderFor;

import java.io.InputStream;
import java.net.URL;

/**
* @author Andres Almiray
*/
@ServiceProviderFor(IkonHandler.class)
public class AntDesignIconsFilledIkonHandler extends AbstractIkonHandler {
private static final String FONT_RESOURCE = "/META-INF/resources/antdesignicons/4.0.0/fonts/AntDesign-Icons-Filled.ttf";

@Override
public boolean supports(String description) {
return description != null && description.startsWith("antf-");
Expand All @@ -38,9 +43,13 @@ public Ikon resolve(String description) {
}

@Override
public String getFontResourcePath() {
return getClass().getResource("/META-INF/resources/antdesignicons/4.0.0/fonts/AntDesign-Icons-Filled.ttf")
.toExternalForm();
public URL getFontResource() {
return getClass().getResource(FONT_RESOURCE);
}

@Override
public InputStream getFontResourceAsStream() {
return getClass().getResourceAsStream(FONT_RESOURCE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@
import org.kordamp.ikonli.IkonHandler;
import org.kordamp.jipsy.ServiceProviderFor;

import java.io.InputStream;
import java.net.URL;

/**
* @author Andres Almiray
*/
@ServiceProviderFor(IkonHandler.class)
public class AntDesignIconsOutlinedIkonHandler extends AbstractIkonHandler {
private static final String FONT_RESOURCE = "/META-INF/resources/antdesignicons/4.0.0/fonts/AntDesign-Icons-Outlined.ttf";

@Override
public boolean supports(String description) {
return description != null && description.startsWith("anto-");
Expand All @@ -38,9 +43,13 @@ public Ikon resolve(String description) {
}

@Override
public String getFontResourcePath() {
return getClass().getResource("/META-INF/resources/antdesignicons/4.0.0/fonts/AntDesign-Icons-Outlined.ttf")
.toExternalForm();
public URL getFontResource() {
return getClass().getResource(FONT_RESOURCE);
}

@Override
public InputStream getFontResourceAsStream() {
return getClass().getResourceAsStream(FONT_RESOURCE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@
import org.kordamp.ikonli.IkonHandler;
import org.kordamp.jipsy.ServiceProviderFor;

import java.io.InputStream;
import java.net.URL;

/**
* @author Andres Almiray
*/
@ServiceProviderFor(IkonHandler.class)
public class BoxiconsLogosIkonHandler extends AbstractIkonHandler {
private static final String FONT_RESOURCE = "/META-INF/resources/boxicons/2.0.7/fonts/boxicons.ttf";

@Override
public boolean supports(String description) {
return description != null && description.startsWith("bxl-");
Expand All @@ -38,9 +43,13 @@ public Ikon resolve(String description) {
}

@Override
public String getFontResourcePath() {
return getClass().getResource("/META-INF/resources/boxicons/2.0.7/fonts/boxicons.ttf")
.toExternalForm();
public URL getFontResource() {
return getClass().getResource(FONT_RESOURCE);
}

@Override
public InputStream getFontResourceAsStream() {
return getClass().getResourceAsStream(FONT_RESOURCE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,39 @@
*/
package org.kordamp.ikonli.boxicons;

import org.kordamp.ikonli.AbstractIkonHandler;
import org.kordamp.ikonli.Ikon;
import org.kordamp.ikonli.IkonHandler;
import org.kordamp.jipsy.ServiceProviderFor;

import java.io.InputStream;
import java.net.URL;

/**
* @author Andres Almiray
*/
@org.kordamp.jipsy.ServiceProviderFor(org.kordamp.ikonli.IkonHandler.class)
public class BoxiconsRegularIkonHandler extends org.kordamp.ikonli.AbstractIkonHandler {
@ServiceProviderFor(IkonHandler.class)
public class BoxiconsRegularIkonHandler extends AbstractIkonHandler {
private static final String FONT_RESOURCE = "/META-INF/resources/boxicons/2.0.7/fonts/boxicons.ttf";

@Override
public boolean supports(String description) {
return description != null && description.startsWith("bx-");
}

@Override
public org.kordamp.ikonli.Ikon resolve(String description) {
public Ikon resolve(String description) {
return BoxiconsRegular.findByDescription(description);
}

@Override
public String getFontResourcePath() {
return getClass().getResource("/META-INF/resources/boxicons/2.0.7/fonts/boxicons.ttf")
.toExternalForm();
public URL getFontResource() {
return getClass().getResource(FONT_RESOURCE);
}

@Override
public InputStream getFontResourceAsStream() {
return getClass().getResourceAsStream(FONT_RESOURCE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,39 @@
*/
package org.kordamp.ikonli.boxicons;

import org.kordamp.ikonli.AbstractIkonHandler;
import org.kordamp.ikonli.Ikon;
import org.kordamp.ikonli.IkonHandler;
import org.kordamp.jipsy.ServiceProviderFor;

import java.io.InputStream;
import java.net.URL;

/**
* @author Andres Almiray
*/
@org.kordamp.jipsy.ServiceProviderFor(org.kordamp.ikonli.IkonHandler.class)
public class BoxiconsSolidIkonHandler extends org.kordamp.ikonli.AbstractIkonHandler {
@ServiceProviderFor(IkonHandler.class)
public class BoxiconsSolidIkonHandler extends AbstractIkonHandler {
private static final String FONT_RESOURCE = "/META-INF/resources/boxicons/2.0.7/fonts/boxicons.ttf";

@Override
public boolean supports(String description) {
return description != null && description.startsWith("bxs-");
}

@Override
public org.kordamp.ikonli.Ikon resolve(String description) {
public Ikon resolve(String description) {
return BoxiconsSolid.findByDescription(description);
}

@Override
public String getFontResourcePath() {
return getClass().getResource("/META-INF/resources/boxicons/2.0.7/fonts/boxicons.ttf")
.toExternalForm();
public URL getFontResource() {
return getClass().getResource(FONT_RESOURCE);
}

@Override
public InputStream getFontResourceAsStream() {
return getClass().getResourceAsStream(FONT_RESOURCE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@
import org.kordamp.ikonli.IkonHandler;
import org.kordamp.jipsy.ServiceProviderFor;

import java.io.InputStream;
import java.net.URL;

/**
* @author Andres Almiray
*/
@ServiceProviderFor(IkonHandler.class)
public class BpmnIkonHandler extends AbstractIkonHandler {
private static final String FONT_RESOURCE = "/META-INF/resources/bpmn/0.10.0/fonts/bpmn.ttf";

@Override
public boolean supports(String description) {
return description != null && description.startsWith("bpmn-");
Expand All @@ -38,9 +43,13 @@ public Ikon resolve(String description) {
}

@Override
public String getFontResourcePath() {
return getClass().getResource("/META-INF/resources/bpmn/0.10.0/fonts/bpmn.ttf")
.toExternalForm();
public URL getFontResource() {
return getClass().getResource(FONT_RESOURCE);
}

@Override
public InputStream getFontResourceAsStream() {
return getClass().getResourceAsStream(FONT_RESOURCE);
}

@Override
Expand Down
Loading

0 comments on commit 6187014

Please sign in to comment.