Skip to content

Commit ebd77b6

Browse files
authored
Merge pull request processing#7716 from processing/pako
Start to add pako support
2 parents e8f9d6c + ca95f59 commit ebd77b6

File tree

3 files changed

+68
-55
lines changed

3 files changed

+68
-55
lines changed

src/type/lib/Typr.js

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
1+
import { inflate } from 'pako';
2+
// Mocking the pako module to just have inflate for a smaller package size
3+
const pako = { inflate };
24

35
var Typr = {};
46

@@ -82,13 +84,18 @@ Typr["parse"] = function (buff) {
8284

8385
otf.set(tab, toff); toff += oLe;
8486
}
85-
//console.log(otf);
87+
//console.log(otf);
8688
return otf;
8789
}
8890

8991

9092
var data = new Uint8Array(buff);
91-
if (data[0] == 0x77) data = woffToOtf(data);
93+
// PATCHED: keep around the compressed data if we inflate it
94+
let compressedData;
95+
if (data[0] == 0x77) {
96+
compressedData = data;
97+
data = woffToOtf(data);
98+
}
9299

93100
var tmap = {};
94101
var tag = bin.readASCII(data, 0, 4);
@@ -105,6 +112,7 @@ Typr["parse"] = function (buff) {
105112
return fnts;
106113
}
107114
var fnt = readFont(data, 0, 0, tmap); //console.log(fnt); throw "e";
115+
fnt._compressedData = compressedData; // PATCH: make compressed data accessible
108116
var fvar = fnt["fvar"];
109117
if (fvar) {
110118
var out = [fnt];
@@ -143,7 +151,7 @@ Typr["splitBy"] = function(data,tag) {
143151
data = new Uint8Array(data); console.log(data.slice(0,64));
144152
var bin = Typr["B"];
145153
var ttcf = bin.readASCII(data, 0, 4); if(ttcf!="ttcf") return {};
146-
154+
147155
var offset = 8;
148156
var numF = bin.readUint (data, offset); offset+=4;
149157
var colls = [], used={};
@@ -156,12 +164,12 @@ Typr["splitBy"] = function(data,tag) {
156164
for(var toff in used) {
157165
var offs = used[toff];
158166
var hlen = 12+4*offs.length;
159-
var out = new Uint8Array(hlen);
167+
var out = new Uint8Array(hlen);
160168
for(var i=0; i<8; i++) out[i]=data[i];
161169
bin.writeUint(out,8,offs.length);
162-
170+
163171
for(var i=0; i<offs.length; i++) hlen += 12+offs[i][1]*16;
164-
172+
165173
var hdrs = [out], tabs = [], hoff=out.length, toff=hlen, noffs={};
166174
for(var i=0; i<offs.length; i++) {
167175
bin.writeUint(out, 12+i*4, hoff); hoff+=12+offs[i][1]*16;
@@ -176,7 +184,7 @@ Typr["splitFonts"] = function(data) {
176184
data = new Uint8Array(data);
177185
var bin = Typr["B"];
178186
var ttcf = bin.readASCII(data, 0, 4); if(ttcf!="ttcf") return {};
179-
187+
180188
var offset = 8;
181189
var numF = bin.readUint (data, offset); offset+=4;
182190
var fnts = [];
@@ -191,26 +199,26 @@ Typr["splitFonts"] = function(data) {
191199
Typr["_cutFont"] = function(data,foff,hdrs,tabs,toff, noffs) {
192200
var bin = Typr["B"];
193201
var numTables = bin.readUshort(data, foff+4);
194-
202+
195203
var out = new Uint8Array(12+numTables*16); hdrs.push(out);
196204
for(var i=0; i<12; i++) out[i]=data[foff+i]; //console.log(out);
197-
205+
198206
var off = 12;
199207
for(var i=0; i<numTables; i++)
200208
{
201-
var tag = bin.readASCII(data, foff+off, 4);
209+
var tag = bin.readASCII(data, foff+off, 4);
202210
var checkSum = bin.readUint (data, foff+off+ 4);
203-
var toffset = bin.readUint (data, foff+off+ 8);
211+
var toffset = bin.readUint (data, foff+off+ 8);
204212
var length = bin.readUint (data, foff+off+12);
205-
213+
206214
while((length&3)!=0) length++;
207-
215+
208216
for(var j=0; j<16; j++) out[off+j]=data[foff+off+j];
209-
217+
210218
if(noffs[toffset]!=null) bin.writeUint(out,off+8,noffs[toffset]);
211219
else {
212220
noffs[toffset] = toff;
213-
bin.writeUint(out, off+8, toff);
221+
bin.writeUint(out, off+8, toff);
214222
tabs.push(new Uint8Array(data.buffer, toffset, length)); toff+=length;
215223
}
216224
off+=16;
@@ -396,7 +404,7 @@ Typr["T"].CFF = {
396404
for (var i = 0; i < sinds.length - 1; i++) strings.push(bin.readASCII(data, offset + sinds[i], sinds[i + 1] - sinds[i]));
397405
offset += sinds[sinds.length - 1];
398406

399-
// Global Subr INDEX (subroutines)
407+
// Global Subr INDEX (subroutines)
400408
CFF.readSubrs(data, offset, topdict);
401409

402410
// charstrings
@@ -522,12 +530,12 @@ Typr["T"].CFF = {
522530
/*readEncoding : function(data, offset, num)
523531
{
524532
var bin = Typr["B"];
525-
533+
526534
var array = ['.notdef'];
527535
var format = data[offset]; offset++;
528536
//console.log("Encoding");
529537
//console.log(format);
530-
538+
531539
if(format==0)
532540
{
533541
var nCodes = data[offset]; offset++;
@@ -545,9 +553,9 @@ Typr["T"].CFF = {
545553
for(var i=0; i<=nLeft; i++) { charset.push(first); first++; }
546554
}
547555
}
548-
556+
549557
else throw "error: unknown encoding format: " + format;
550-
558+
551559
return array;
552560
},*/
553561

@@ -809,7 +817,7 @@ Typr["T"].cmap = {
809817
var lang = rU(data, offset); offset += 4;
810818
var nGroups = rU(data, offset) * 3; offset += 4;
811819

812-
var gps = obj.groups = new Uint32Array(nGroups);//new Uint32Array(data.slice(offset, offset+nGroups*12).buffer);
820+
var gps = obj.groups = new Uint32Array(nGroups);//new Uint32Array(data.slice(offset, offset+nGroups*12).buffer);
813821

814822
for (var i = 0; i < nGroups; i += 3) {
815823
gps[i] = rU(data, offset + (i << 2));
@@ -1092,7 +1100,7 @@ Typr["T"].kern = {
10921100
parseV1: function (data, offset, length, font) {
10931101
var bin = Typr["B"], kern = Typr["T"].kern;
10941102

1095-
var version = bin.readFixed(data, offset); // 0x00010000
1103+
var version = bin.readFixed(data, offset); // 0x00010000
10961104
var nTables = bin.readUint(data, offset + 4); offset += 8;
10971105

10981106
var map = { glyph1: [], rval: [] };
@@ -1495,12 +1503,12 @@ Typr["T"].cpal = {
14951503
return new Uint8Array(data.buffer, ooff + fst, tot * 4);
14961504
/*
14971505
var coff=ooff+fst;
1498-
1506+
14991507
for(var i=0; i<tot; i++) {
15001508
console.log(data[coff],data[coff+1],data[coff+2],data[coff+3]);
15011509
coff+=4;
15021510
}
1503-
1511+
15041512
console.log(ets,pts,tot); */
15051513
}
15061514
else throw vsn;//console.log("unknown color palette",vsn);
@@ -1768,7 +1776,7 @@ Typr["T"].HVAR = {
17681776

17691777
off = oo + varO; // item variation store
17701778

1771-
// ItemVariationStore
1779+
// ItemVariationStore
17721780
var ioff = off;
17731781

17741782
var fmt = bin.readUshort(data, off); off += 2; if (fmt != 1) throw "e";
@@ -1777,7 +1785,7 @@ Typr["T"].HVAR = {
17771785
var vcnt = bin.readUshort(data, off); off += 2;
17781786

17791787
var offs = []; for (var i = 0; i < vcnt; i++) offs.push(bin.readUint(data, off + i * 4)); off += vcnt * 4; //if(offs.length!=1) throw "e";
1780-
//console.log(vregO,vcnt,offs);
1788+
//console.log(vregO,vcnt,offs);
17811789

17821790
off = ioff + vregO;
17831791
var acnt = bin.readUshort(data, off); off += 2;
@@ -1799,7 +1807,7 @@ Typr["T"].HVAR = {
17991807
var i8 = new Int8Array(data.buffer);
18001808
var varStore = [];
18011809
for (var i = 0; i < offs.length; i++) {
1802-
// ItemVariationData
1810+
// ItemVariationData
18031811
off = oo + varO + offs[i]; var vdata = []; varStore.push(vdata);
18041812
var icnt = bin.readUshort(data, off); off += 2; // itemCount
18051813
var dcnt = bin.readUshort(data, off); off += 2; if (dcnt & 0x8000) throw "e";
@@ -1828,7 +1836,7 @@ Typr["T"].HVAR = {
18281836

18291837
off = oo + advO; // advance widths
18301838

1831-
// DeltaSetIndexMap
1839+
// DeltaSetIndexMap
18321840

18331841
var fmt = data[off++]; if (fmt != 0) throw "e";
18341842
var entryFormat = data[off++];
@@ -1970,19 +1978,19 @@ Typr["U"] = function () {
19701978
var data=font["_data"], off = cmap.off+tab.off+6, bin=Typr["B"];
19711979
var shKey = bin.readUshort(data,off + 2*(code>>>8));
19721980
var shInd = off + 256*2 + shKey*8;
1973-
1981+
19741982
var firstCode = bin.readUshort(data,shInd);
19751983
var entryCount= bin.readUshort(data,shInd+2);
19761984
var idDelta = bin.readShort (data,shInd+4);
19771985
var idRangeOffset = bin.readUshort(data,shInd+6);
1978-
1986+
19791987
if(firstCode<=code && code<=firstCode+entryCount) {
19801988
// not completely correct
19811989
gid = bin.readUshort(data, shInd+6+idRangeOffset + (code&255)*2);
19821990
}
19831991
else gid=0;
19841992
//if(code>256) console.log(code,(code>>>8),shKey,firstCode,entryCount,idDelta,idRangeOffset);
1985-
1993+
19861994
//throw "e";
19871995
//console.log(tab, bin.readUshort(data,off));
19881996
//throw "e";
@@ -2959,4 +2967,4 @@ Typr["U"] = function () {
29592967
}();
29602968

29612969

2962-
export default Typr;
2970+
export default Typr;

src/type/p5.Font.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -360,25 +360,25 @@ class Font {
360360
return cmdContours.map((commands) => pathToPoints(commands, options, this));
361361
}
362362
/**
363-
*
363+
*
364364
* Converts text into a 3D model that can be rendered in WebGL mode.
365-
*
366-
* This method transforms flat text into extruded 3D geometry, allowing
365+
*
366+
* This method transforms flat text into extruded 3D geometry, allowing
367367
* for dynamic effects like depth, warping, and custom shading.
368-
*
369-
* It works by taking the outlines (contours) of each character in the
368+
*
369+
* It works by taking the outlines (contours) of each character in the
370370
* provided text string and constructing a 3D shape from them.
371-
*
372-
* Once your 3D text is ready, you can rotate it in 3D space using <a href="#/p5/orbitControl">orbitControl()</a>
371+
*
372+
* Once your 3D text is ready, you can rotate it in 3D space using <a href="#/p5/orbitControl">orbitControl()</a>
373373
* — just click and drag with your mouse to see it from all angles!
374-
*
375-
* Use the extrude slider to give your letters depth: slide it up, and your
374+
*
375+
* Use the extrude slider to give your letters depth: slide it up, and your
376376
* flat text turns into a solid, multi-dimensional object.
377-
*
378-
* You can also choose from various fonts such as "Anton", "Montserrat", or "Source Serif",
377+
*
378+
* You can also choose from various fonts such as "Anton", "Montserrat", or "Source Serif",
379379
* much like selecting fancy fonts in a word processor,
380-
*
381-
* The generated model (a Geometry object) can be manipulated further—rotated, scaled,
380+
*
381+
* The generated model (a Geometry object) can be manipulated further—rotated, scaled,
382382
* or styled with shaders—to create engaging, interactive visual art.
383383
*
384384
* @param {String} str The text string to convert into a 3D model.
@@ -387,7 +387,7 @@ class Font {
387387
* @param {Number} width Maximum width of the text block (wraps text if exceeded).
388388
* @param {Number} height Maximum height of the text block.
389389
* @param {Object} [options] Configuration options for the 3D text:
390-
* @param {Number} [options.extrude=0] The depth to extrude the text. A value of 0 produces
390+
* @param {Number} [options.extrude=0] The depth to extrude the text. A value of 0 produces
391391
* flat text; higher values create thicker, 3D models.
392392
* @param {Number} [options.sampleFactor=1] A factor controlling the level of detail for the text contours.
393393
* Higher values result in smoother curves.
@@ -419,7 +419,7 @@ class Font {
419419
* }
420420
* </code>
421421
* </div>
422-
*
422+
*
423423
* @example
424424
* <div modernizr='webgl'>
425425
* <code>
@@ -428,15 +428,15 @@ class Font {
428428
*
429429
* async function setup() {
430430
* createCanvas(200, 200, WEBGL);
431-
*
431+
*
432432
* // Alternative fonts:
433433
* // Anton: 'https://fonts.gstatic.com/s/anton/v25/1Ptgg87LROyAm0K08i4gS7lu.ttf'
434434
* // Montserrat: 'https://fonts.gstatic.com/s/montserrat/v29/JTUHjIg1_i6t8kCHKm4532VJOt5-QNFgpCtr6Ew-Y3tcoqK5.ttf'
435435
* // Source Serif: 'https://fonts.gstatic.com/s/sourceserif4/v8/vEFy2_tTDB4M7-auWDN0ahZJW3IX2ih5nk3AucvUHf6OAVIJmeUDygwjihdqrhxXD-wGvjU.ttf'
436-
*
436+
*
437437
* // Using Source Serif for this example:
438438
* font = await loadFont('https://fonts.gstatic.com/s/sourceserif4/v8/vEFy2_tTDB4M7-auWDN0ahZJW3IX2ih5nk3AucvUHf6OAVIJmeUDygwjihdqrhxXD-wGvjU.ttf');
439-
*
439+
*
440440
* geom = font.textToModel("Hello", 50, 0, { sampleFactor: 2, extrude: 5 });
441441
* geom.clearColors();
442442
* geom.normalize();
@@ -453,7 +453,7 @@ class Font {
453453
* }
454454
* </code>
455455
* </div>
456-
*
456+
*
457457
* @example
458458
* <div modernizr='webgl'>
459459
* <code>
@@ -472,7 +472,7 @@ class Font {
472472
* createCanvas(200, 200, WEBGL);
473473
*
474474
* // Using Anton as the default font for this example:
475-
*
475+
*
476476
* // Alternative fonts:
477477
* // Anton: 'https://fonts.gstatic.com/s/anton/v25/1Ptgg87LROyAm0K08i4gS7lu.ttf'
478478
* // Montserrat: 'https://fonts.gstatic.com/s/montserrat/v29/JTUHjIg1_i6t8kCHKm4532VJOt5-QNFgpCtr6Ew-Y3tcoqK5.ttf'
@@ -507,7 +507,7 @@ class Font {
507507
* return vec4(c, 1.);
508508
* }`
509509
* });
510-
*
510+
*
511511
* lineShader = baseStrokeShader().modify({
512512
* uniforms: {
513513
* 'float time': () => millis(),
@@ -925,7 +925,7 @@ function createFontFace(name, path, descriptors, rawFont) {
925925

926926
if (name.includes(' ')) name = "'" + name + "'"; // NOTE: must be single-quotes
927927

928-
let fontArg = rawFont?._data;
928+
let fontArg = rawFont?._compressedData ?? rawFont?._data;
929929
if (!fontArg) {
930930
if (!validFontTypesRe.test(path)) {
931931
throw Error(invalidFontError);

test/unit/type/loading.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ suite('Loading Fonts', function () {
1717
// tests ////////////////////////////////////////////////
1818
const fontFile = '/unit/assets/acmesa.ttf';
1919

20+
test('loadFont on zlib compressed fonts works', async () => {
21+
const font = await myp5.loadFont('https://fonts.gstatic.com/s/montserrat/v29/JTUFjIg1_i6t8kCHKm459Wx7xQYXK0vOoz6jq6R8aXw.woff');
22+
expect(font.data).toBeTruthy();
23+
});
24+
2025
test('loadFont.await', async () => {
2126
const pFont = await myp5.loadFont(fontFile, 'fredTheFont');
2227
assert.ok(pFont, 'acmesa.ttf loaded');

0 commit comments

Comments
 (0)