Link to sketch https://openprocessing.org/sketch/2684885
I found a library called opentype on github - a quick inspection of the readme file suggests that this might do the job.
Disclaimer: although I am aware there is an otf fonto format I have never looked into it before so I can’t be sure this will do exactly what you want.
Unfortunately I don’t have OpenProcessing plus membership so I can’t save your sketch and work on it. Is there a link we could use to download the font
Here is a zip file of Abelone.otf, a very nice shaded colorfont.
(Attachment Abelone.zip is missing)
After several hours of research and experimentation I managed to get it to work
The code turned out to be very simple
let otf;
function setup() {
let p5canvas = createCanvas(640, 100);
getOFT('Gilbert-Color Bold Preview5.otf');
}
function draw() {
background(240);
// Font
// Display text X Y size
otf?.draw(drawingContext, 'Quark does it again LOL!', 20, 60, 60);
}
async function getOFT(url) {
const res = await fetch(url);
const buffer = await res.arrayBuffer();
otf = await opentype.parse(buffer);
}
Just a few of things to note
-
The font is loaded asynchronously so the variable
oft
may be undefined when we attempt to use it in draw, hence the conditional access operator (?.
) must be used to prevent exceptions. -
The HTML file must link to the OpenType source so thhis link should appear in the header section of the HTML file
<script src="https://opentype.js.org/dist/opentype.js"></script>
-
Obviously being a color font it will ignore the current stroke and fill colors when rendered.
This sketch simply shows that otf colorfonts can be displayed in p5js but incorporating it into the p5js language would be a very much bigger task.
That’s is a great solution. I will create a few sketches using the technique and credit you in every one. Sinan may be interested in adding the library to OpenProcessing.
Thanks!
Richard