mirror of
https://github.com/imgfloat/server.git
synced 2026-02-05 03:39:26 +00:00
Add text rendering and ttf support
This commit is contained in:
1
.gitattributes
vendored
1
.gitattributes
vendored
@@ -39,3 +39,4 @@ doc/marketplace-scripts/lichess-tv-chess/attachments/Chess_rdt45.png filter=lfs
|
|||||||
doc/marketplace-scripts/lichess-tv-chess/attachments/Chess_rlt45.png filter=lfs diff=lfs merge=lfs -text
|
doc/marketplace-scripts/lichess-tv-chess/attachments/Chess_rlt45.png filter=lfs diff=lfs merge=lfs -text
|
||||||
doc/marketplace-scripts/lichess-tv-chess/attachments/Chess_blt45.png filter=lfs diff=lfs merge=lfs -text
|
doc/marketplace-scripts/lichess-tv-chess/attachments/Chess_blt45.png filter=lfs diff=lfs merge=lfs -text
|
||||||
doc/marketplace-scripts/lichess-tv-chess/logo.png filter=lfs diff=lfs merge=lfs -text
|
doc/marketplace-scripts/lichess-tv-chess/logo.png filter=lfs diff=lfs merge=lfs -text
|
||||||
|
doc/marketplace-scripts/custom-text/attachments/AtkinsonHyperlegible-Regular.ttf filter=lfs diff=lfs merge=lfs -text
|
||||||
|
|||||||
92
doc/marketplace-scripts/custom-text/OFL.txt
Normal file
92
doc/marketplace-scripts/custom-text/OFL.txt
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
Copyright 2020 Braille Institute of America, Inc.
|
||||||
|
|
||||||
|
This Font Software is licensed under the SIL Open Font License, Version 1.1.
|
||||||
|
This license is copied below, and is also available with a FAQ at:
|
||||||
|
http://scripts.sil.org/OFL
|
||||||
|
|
||||||
|
-----------------------------------------------------------
|
||||||
|
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
||||||
|
-----------------------------------------------------------
|
||||||
|
|
||||||
|
PREAMBLE
|
||||||
|
The goals of the Open Font License (OFL) are to stimulate worldwide
|
||||||
|
development of collaborative font projects, to support the font creation
|
||||||
|
efforts of academic and linguistic communities, and to provide a free and
|
||||||
|
open framework in which fonts may be shared and improved in partnership
|
||||||
|
with others.
|
||||||
|
|
||||||
|
The OFL allows the licensed fonts to be used, studied, modified and
|
||||||
|
redistributed freely as long as they are not sold by themselves. The
|
||||||
|
fonts, including any derivative works, can be bundled, embedded,
|
||||||
|
redistributed and/or sold with any software provided that any reserved
|
||||||
|
names are not used by derivative works. The fonts and derivatives,
|
||||||
|
however, cannot be released under any other type of license. The
|
||||||
|
requirement for fonts to remain under this license does not apply
|
||||||
|
to any document created using the fonts or their derivatives.
|
||||||
|
|
||||||
|
DEFINITIONS
|
||||||
|
"Font Software" refers to the set of files released by the Copyright
|
||||||
|
Holder(s) under this license and clearly marked as such. This may
|
||||||
|
include source files, build scripts and documentation.
|
||||||
|
|
||||||
|
"Reserved Font Name" refers to any names specified as such after the
|
||||||
|
copyright statement(s).
|
||||||
|
|
||||||
|
"Original Version" refers to the collection of Font Software components as
|
||||||
|
distributed by the Copyright Holder(s).
|
||||||
|
|
||||||
|
"Modified Version" refers to any derivative made by adding to, deleting,
|
||||||
|
or substituting -- in part or in whole -- any of the components of the
|
||||||
|
Original Version, by changing formats or by porting the Font Software to a
|
||||||
|
new environment.
|
||||||
|
|
||||||
|
"Author" refers to any designer, engineer, programmer, technical
|
||||||
|
writer or other person who contributed to the Font Software.
|
||||||
|
|
||||||
|
PERMISSION & CONDITIONS
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
||||||
|
redistribute, and sell modified and unmodified copies of the Font
|
||||||
|
Software, subject to the following conditions:
|
||||||
|
|
||||||
|
1) Neither the Font Software nor any of its individual components,
|
||||||
|
in Original or Modified Versions, may be sold by itself.
|
||||||
|
|
||||||
|
2) Original or Modified Versions of the Font Software may be bundled,
|
||||||
|
redistributed and/or sold with any software, provided that each copy
|
||||||
|
contains the above copyright notice and this license. These can be
|
||||||
|
included either as stand-alone text files, human-readable headers or
|
||||||
|
in the appropriate machine-readable metadata fields within text or
|
||||||
|
binary files as long as those fields can be easily viewed by the user.
|
||||||
|
|
||||||
|
3) No Modified Version of the Font Software may use the Reserved Font
|
||||||
|
Name(s) unless explicit written permission is granted by the corresponding
|
||||||
|
Copyright Holder. This restriction only applies to the primary font name as
|
||||||
|
presented to the users.
|
||||||
|
|
||||||
|
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
||||||
|
Software shall not be used to promote, endorse or advertise any
|
||||||
|
Modified Version, except to acknowledge the contribution(s) of the
|
||||||
|
Copyright Holder(s) and the Author(s) or with their explicit written
|
||||||
|
permission.
|
||||||
|
|
||||||
|
5) The Font Software, modified or unmodified, in part or in whole,
|
||||||
|
must be distributed entirely under this license, and must not be
|
||||||
|
distributed under any other license. The requirement for fonts to
|
||||||
|
remain under this license does not apply to any document created
|
||||||
|
using the Font Software.
|
||||||
|
|
||||||
|
TERMINATION
|
||||||
|
This license becomes null and void if any of the above conditions are
|
||||||
|
not met.
|
||||||
|
|
||||||
|
DISCLAIMER
|
||||||
|
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
||||||
|
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
||||||
|
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
|
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
||||||
|
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
||||||
|
OTHER DEALINGS IN THE FONT SOFTWARE.
|
||||||
BIN
doc/marketplace-scripts/custom-text/attachments/AtkinsonHyperlegible-Regular.ttf
LFS
Normal file
BIN
doc/marketplace-scripts/custom-text/attachments/AtkinsonHyperlegible-Regular.ttf
LFS
Normal file
Binary file not shown.
4
doc/marketplace-scripts/custom-text/metadata.json
Normal file
4
doc/marketplace-scripts/custom-text/metadata.json
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"name": "Marketplace Text Line",
|
||||||
|
"description": "Render a single line of text with configurable font styling and positioning."
|
||||||
|
}
|
||||||
79
doc/marketplace-scripts/custom-text/source.js
Normal file
79
doc/marketplace-scripts/custom-text/source.js
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
const TEXT_CONFIG = {
|
||||||
|
text: "Hello from the marketplace!",
|
||||||
|
fontFamily: "Atkinson Hyperlegible",
|
||||||
|
fontSize: 64,
|
||||||
|
fontStyle: "normal",
|
||||||
|
position: {
|
||||||
|
x: 80,
|
||||||
|
y: 120,
|
||||||
|
},
|
||||||
|
color: "#00ff00",
|
||||||
|
textAlign: "left",
|
||||||
|
textBaseline: "top",
|
||||||
|
};
|
||||||
|
|
||||||
|
const FONT_ATTACHMENT_NAME = "AtkinsonHyperlegible-Regular.ttf";
|
||||||
|
|
||||||
|
function resolveFontAttachment(assets) {
|
||||||
|
if (!Array.isArray(assets)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const normalizedName = FONT_ATTACHMENT_NAME.toLowerCase();
|
||||||
|
return assets.find((asset) => asset?.name?.toLowerCase() === normalizedName) || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadFont(context, state) {
|
||||||
|
if (state.fontLoading || state.fontReady) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
state.fontLoading = true;
|
||||||
|
const attachment = resolveFontAttachment(context.assets);
|
||||||
|
if (!attachment?.url) {
|
||||||
|
state.fontReady = true;
|
||||||
|
state.fontLoading = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (typeof FontFace !== "function" || !self?.fonts) {
|
||||||
|
state.fontReady = true;
|
||||||
|
state.fontLoading = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const response = await fetch(attachment.url);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error("Unable to load font attachment");
|
||||||
|
}
|
||||||
|
const buffer = await response.arrayBuffer();
|
||||||
|
const font = new FontFace(TEXT_CONFIG.fontFamily, buffer);
|
||||||
|
await font.load();
|
||||||
|
self.fonts.add(font);
|
||||||
|
} catch (error) {
|
||||||
|
state.fontError = error;
|
||||||
|
} finally {
|
||||||
|
state.fontReady = true;
|
||||||
|
state.fontLoading = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function init(context, state) {
|
||||||
|
await loadFont(context, state);
|
||||||
|
}
|
||||||
|
|
||||||
|
function tick(context, state) {
|
||||||
|
const { ctx, width, height } = context;
|
||||||
|
if (!ctx) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!state?.fontReady) {
|
||||||
|
loadFont(context, state);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ctx.clearRect(0, 0, width, height);
|
||||||
|
ctx.save();
|
||||||
|
ctx.fillStyle = TEXT_CONFIG.color;
|
||||||
|
ctx.textAlign = TEXT_CONFIG.textAlign;
|
||||||
|
ctx.textBaseline = TEXT_CONFIG.textBaseline;
|
||||||
|
ctx.font = `${TEXT_CONFIG.fontStyle} ${TEXT_CONFIG.fontSize}px "${TEXT_CONFIG.fontFamily}", sans-serif`;
|
||||||
|
ctx.fillText(TEXT_CONFIG.text, TEXT_CONFIG.position.x, TEXT_CONFIG.position.y);
|
||||||
|
ctx.restore();
|
||||||
|
}
|
||||||
@@ -41,6 +41,10 @@ public class MarketplaceScriptSeedLoader {
|
|||||||
java.util.Map.entry("mp3", "audio/mpeg"),
|
java.util.Map.entry("mp3", "audio/mpeg"),
|
||||||
java.util.Map.entry("wav", "audio/wav"),
|
java.util.Map.entry("wav", "audio/wav"),
|
||||||
java.util.Map.entry("ogg", "audio/ogg"),
|
java.util.Map.entry("ogg", "audio/ogg"),
|
||||||
|
java.util.Map.entry("ttf", "font/ttf"),
|
||||||
|
java.util.Map.entry("otf", "font/otf"),
|
||||||
|
java.util.Map.entry("woff", "font/woff"),
|
||||||
|
java.util.Map.entry("woff2", "font/woff2"),
|
||||||
java.util.Map.entry("glb", "model/gltf-binary"),
|
java.util.Map.entry("glb", "model/gltf-binary"),
|
||||||
java.util.Map.entry("gltf", "model/gltf+json"),
|
java.util.Map.entry("gltf", "model/gltf+json"),
|
||||||
java.util.Map.entry("obj", "model/obj")
|
java.util.Map.entry("obj", "model/obj")
|
||||||
|
|||||||
@@ -61,6 +61,10 @@ public class MediaOptimizationService {
|
|||||||
return new OptimizedAsset(bytes, mediaType, 0, 0, null);
|
return new OptimizedAsset(bytes, mediaType, 0, 0, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mediaType.startsWith("font/")) {
|
||||||
|
return new OptimizedAsset(bytes, mediaType, 0, 0, null);
|
||||||
|
}
|
||||||
|
|
||||||
if (mediaType.startsWith("model/")) {
|
if (mediaType.startsWith("model/")) {
|
||||||
return new OptimizedAsset(bytes, mediaType, 0, 0, null);
|
return new OptimizedAsset(bytes, mediaType, 0, 0, null);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,6 +76,10 @@ public final class MediaTypeRegistry {
|
|||||||
map.put("wav", "audio/wav");
|
map.put("wav", "audio/wav");
|
||||||
map.put("ogg", "audio/ogg");
|
map.put("ogg", "audio/ogg");
|
||||||
map.put("flac", "audio/flac");
|
map.put("flac", "audio/flac");
|
||||||
|
map.put("ttf", "font/ttf");
|
||||||
|
map.put("otf", "font/otf");
|
||||||
|
map.put("woff", "font/woff");
|
||||||
|
map.put("woff2", "font/woff2");
|
||||||
map.put("glb", "model/gltf-binary");
|
map.put("glb", "model/gltf-binary");
|
||||||
map.put("gltf", "model/gltf+json");
|
map.put("gltf", "model/gltf+json");
|
||||||
map.put("obj", "model/obj");
|
map.put("obj", "model/obj");
|
||||||
@@ -105,6 +109,10 @@ public final class MediaTypeRegistry {
|
|||||||
map.put("audio/ogg", ".ogg");
|
map.put("audio/ogg", ".ogg");
|
||||||
map.put("audio/webm", ".webm");
|
map.put("audio/webm", ".webm");
|
||||||
map.put("audio/flac", ".flac");
|
map.put("audio/flac", ".flac");
|
||||||
|
map.put("font/ttf", ".ttf");
|
||||||
|
map.put("font/otf", ".otf");
|
||||||
|
map.put("font/woff", ".woff");
|
||||||
|
map.put("font/woff2", ".woff2");
|
||||||
map.put("model/gltf-binary", ".glb");
|
map.put("model/gltf-binary", ".glb");
|
||||||
map.put("model/gltf+json", ".gltf");
|
map.put("model/gltf+json", ".gltf");
|
||||||
map.put("model/obj", ".obj");
|
map.put("model/obj", ".obj");
|
||||||
|
|||||||
Reference in New Issue
Block a user