SpritesJs.java
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2025, Arnaud Roques
*
* Project Info: https://plantuml.com
*
* If you like this project or if you find it useful, you can support us at:
*
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
*
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*
* Original Author: Arnaud Roques
*
*/
package net.sourceforge.plantuml.teavm.js;
import org.teavm.jso.JSBody;
import net.sourceforge.plantuml.teavm.js.browser.ScriptLoaderJs;
// ::remove file when JAVA8
/**
* Where the internal sprites (src/main/resources/sprites, used as
* {@code <$archimate/business-actor>} or by the ArchiMate elements) are found in
* the browser build, which has no class loader: in the global
* {@code PLANTUML_SPRITES}, a map from the name of the sprite (for instance
* {@code "archimate/business-actor"}) to the content of its SVG file. The map
* is made by {@code sprites.js}, generated by {@code SpritesJsGenerator} and
* loaded on demand, or registered by the host.
* <p>
* Like the C build (see {@code SpriteImage.fromEmbedded}), only the SVG sprites
* are available: the few PNG ones (the ArchiMate relations) are not.
*/
public final class SpritesJs {
public static final String SPRITES_JS = "sprites.js";
// Same lookup as the other generated scripts (emoji.js, themes.js), so that a
// Web Worker can register the data too
private static final String GLOBAL = "var g = (typeof globalThis !== 'undefined') ? globalThis"
+ " : ((typeof self !== 'undefined') ? self : this);";
private SpritesJs() {
}
/**
* The content of the SVG file of this sprite, or null if there is none.
* <p>
* Never waits: when sprites.js is not loaded yet, this rendering attempt goes
* on without the sprite and is rendered again once it is (PendingResources).
*/
public static String getSvg(String name) {
// The internal sprites always sit in a folder (archimate/...): a plain name
// is a sprite of the diagram that does not exist, which is no reason to
// fetch sprites.js
if (name.indexOf('/') <= 0)
return null;
if (jsHasSprites() == false) {
try {
if (ScriptLoaderJs.require(SPRITES_JS) == false)
return null;
} catch (RuntimeException fetchFailed) {
// sprites.js is not deployed where the page can fetch it: the diagram keeps
// rendering, with the stereotype text instead of the icon.
ScriptLoaderJs.consoleWarn("PlantUML: " + SPRITES_JS + " could not be loaded, so the sprite '"
+ name + "' is missing. Serve " + SPRITES_JS + " next to the page"
+ " or register globalThis.PLANTUML_SPRITES (importing " + SPRITES_JS
+ " as a module does this).");
return null;
}
if (jsHasSprites() == false)
return null;
}
return jsGet(name);
}
@JSBody(script = GLOBAL + "return !!(g && g.PLANTUML_SPRITES);")
private static native boolean jsHasSprites();
@JSBody(params = "name", script = GLOBAL + "var d = g && g.PLANTUML_SPRITES;"
+ "return (d && Object.prototype.hasOwnProperty.call(d, name)) ? d[name] : null;")
private static native String jsGet(String name);
}