@hexze/mctext 1.0.3 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -12,10 +12,11 @@ Minecraft text formatting, parsing, and rendering library. Features all the exac
12
12
 
13
13
  ## Features
14
14
 
15
- - **Text Parsing** - Parse legacy `§` formatting codes and JSON chat components
15
+ - **Builder API** - Fluent interface for constructing formatted text
16
16
  - **Color Support** - All 16 named Minecraft colors plus RGB hex colors
17
17
  - **Style Handling** - Bold, italic, underlined, strikethrough, obfuscated
18
18
  - **Font Rendering** - Measure and render text with authentic Minecraft fonts
19
+ - **Legacy Support** - Parse `§` formatting codes and JSON chat components
19
20
 
20
21
  ## Font Showcase
21
22
 
@@ -42,9 +43,13 @@ mctext = { version = "1.0", features = ["legacy-fonts"] }
42
43
  ```
43
44
 
44
45
  ```rust
45
- use mctext::McText;
46
+ use mctext::{MCText, NamedColor};
47
+
48
+ let text = MCText::new()
49
+ .add("Red ").color(NamedColor::Red)
50
+ .then("Bold").color(NamedColor::Red).bold()
51
+ .build();
46
52
 
47
- let text = McText::parse("§cRed §lBold");
48
53
  for span in text.spans() {
49
54
  println!("{}: {:?}", span.text, span.color);
50
55
  }
@@ -59,7 +64,8 @@ pip install mctext
59
64
  ```python
60
65
  import mctext
61
66
 
62
- text = mctext.parse("§cRed §lBold")
67
+ text = mctext.MCText().add("Red ").color("red").then("Bold").color("red").bold().build()
68
+
63
69
  for span in text.spans():
64
70
  print(f"{span.text}: {span.color}")
65
71
  ```
@@ -71,9 +77,12 @@ npm install @hexze/mctext
71
77
  ```
72
78
 
73
79
  ```javascript
74
- import { McText } from '@hexze/mctext';
80
+ import init, { MCText } from '@hexze/mctext';
81
+
82
+ await init();
83
+
84
+ let text = new MCText().add("Red ").color("red").then("Bold").color("red").bold().build();
75
85
 
76
- const text = McText.parse("§cRed §lBold");
77
86
  for (const span of text.spans()) {
78
87
  console.log(`${span.text}: ${span.color}`);
79
88
  }
package/mctext_wasm.d.ts CHANGED
@@ -17,17 +17,16 @@ export class LayoutOptions {
17
17
  constructor(size: number);
18
18
  }
19
19
 
20
- export class McText {
20
+ export class MCText {
21
21
  free(): void;
22
22
  [Symbol.dispose](): void;
23
- charCount(): number;
24
- static parseJson(json: string): McText;
23
+ static parseJson(json: string): MCText;
25
24
  plainText(): string;
25
+ add(text: string): SpanBuilder;
26
26
  constructor();
27
- static parse(text: string): McText;
27
+ static parse(text: string): MCText;
28
28
  spans(): any;
29
29
  toJson(): string;
30
- isEmpty(): boolean;
31
30
  toLegacy(): string;
32
31
  }
33
32
 
@@ -40,11 +39,25 @@ export class RenderResult {
40
39
  height(): number;
41
40
  }
42
41
 
42
+ export class SpanBuilder {
43
+ private constructor();
44
+ free(): void;
45
+ [Symbol.dispose](): void;
46
+ obfuscated(): SpanBuilder;
47
+ underlined(): SpanBuilder;
48
+ strikethrough(): SpanBuilder;
49
+ bold(): SpanBuilder;
50
+ then(text: string): SpanBuilder;
51
+ build(): MCText;
52
+ color(color: string): SpanBuilder;
53
+ italic(): SpanBuilder;
54
+ }
55
+
43
56
  export function countVisibleChars(text: string): number;
44
57
 
45
58
  export function namedColors(): any;
46
59
 
47
- export function render(font_system: FontSystem, text: string, width: number, height: number, options: LayoutOptions): RenderResult;
60
+ export function render(font_system: FontSystem, text: MCText, width: number, height: number, options: LayoutOptions): RenderResult;
48
61
 
49
62
  export function stripCodes(text: string): string;
50
63
 
@@ -53,9 +66,9 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl
53
66
  export interface InitOutput {
54
67
  readonly memory: WebAssembly.Memory;
55
68
  readonly __wbg_mctext_free: (a: number, b: number) => void;
69
+ readonly __wbg_spanbuilder_free: (a: number, b: number) => void;
56
70
  readonly countVisibleChars: (a: number, b: number) => number;
57
- readonly mctext_charCount: (a: number) => number;
58
- readonly mctext_isEmpty: (a: number) => number;
71
+ readonly mctext_add: (a: number, b: number, c: number) => number;
59
72
  readonly mctext_new: () => number;
60
73
  readonly mctext_parse: (a: number, b: number) => number;
61
74
  readonly mctext_parseJson: (a: number, b: number) => [number, number, number];
@@ -64,6 +77,14 @@ export interface InitOutput {
64
77
  readonly mctext_toJson: (a: number) => [number, number];
65
78
  readonly mctext_toLegacy: (a: number) => [number, number];
66
79
  readonly namedColors: () => any;
80
+ readonly spanbuilder_bold: (a: number) => number;
81
+ readonly spanbuilder_build: (a: number) => number;
82
+ readonly spanbuilder_color: (a: number, b: number, c: number) => number;
83
+ readonly spanbuilder_italic: (a: number) => number;
84
+ readonly spanbuilder_obfuscated: (a: number) => number;
85
+ readonly spanbuilder_strikethrough: (a: number) => number;
86
+ readonly spanbuilder_then: (a: number, b: number, c: number) => number;
87
+ readonly spanbuilder_underlined: (a: number) => number;
67
88
  readonly stripCodes: (a: number, b: number) => [number, number];
68
89
  readonly __wbg_fontsystem_free: (a: number, b: number) => void;
69
90
  readonly __wbg_layoutoptions_free: (a: number, b: number) => void;
@@ -73,7 +94,7 @@ export interface InitOutput {
73
94
  readonly layoutoptions_new: (a: number) => number;
74
95
  readonly layoutoptions_withMaxWidth: (a: number, b: number) => number;
75
96
  readonly layoutoptions_withShadow: (a: number, b: number) => number;
76
- readonly render: (a: number, b: number, c: number, d: number, e: number, f: number) => number;
97
+ readonly render: (a: number, b: number, c: number, d: number, e: number) => number;
77
98
  readonly renderresult_data: (a: number) => [number, number];
78
99
  readonly renderresult_height: (a: number) => number;
79
100
  readonly renderresult_width: (a: number) => number;
package/mctext_wasm.js CHANGED
@@ -119,7 +119,7 @@ const LayoutOptionsFinalization = (typeof FinalizationRegistry === 'undefined')
119
119
  ? { register: () => {}, unregister: () => {} }
120
120
  : new FinalizationRegistry(ptr => wasm.__wbg_layoutoptions_free(ptr >>> 0, 1));
121
121
 
122
- const McTextFinalization = (typeof FinalizationRegistry === 'undefined')
122
+ const MCTextFinalization = (typeof FinalizationRegistry === 'undefined')
123
123
  ? { register: () => {}, unregister: () => {} }
124
124
  : new FinalizationRegistry(ptr => wasm.__wbg_mctext_free(ptr >>> 0, 1));
125
125
 
@@ -127,6 +127,10 @@ const RenderResultFinalization = (typeof FinalizationRegistry === 'undefined')
127
127
  ? { register: () => {}, unregister: () => {} }
128
128
  : new FinalizationRegistry(ptr => wasm.__wbg_renderresult_free(ptr >>> 0, 1));
129
129
 
130
+ const SpanBuilderFinalization = (typeof FinalizationRegistry === 'undefined')
131
+ ? { register: () => {}, unregister: () => {} }
132
+ : new FinalizationRegistry(ptr => wasm.__wbg_spanbuilder_free(ptr >>> 0, 1));
133
+
130
134
  export class FontSystem {
131
135
  static __wrap(ptr) {
132
136
  ptr = ptr >>> 0;
@@ -214,34 +218,27 @@ export class LayoutOptions {
214
218
  }
215
219
  if (Symbol.dispose) LayoutOptions.prototype[Symbol.dispose] = LayoutOptions.prototype.free;
216
220
 
217
- export class McText {
221
+ export class MCText {
218
222
  static __wrap(ptr) {
219
223
  ptr = ptr >>> 0;
220
- const obj = Object.create(McText.prototype);
224
+ const obj = Object.create(MCText.prototype);
221
225
  obj.__wbg_ptr = ptr;
222
- McTextFinalization.register(obj, obj.__wbg_ptr, obj);
226
+ MCTextFinalization.register(obj, obj.__wbg_ptr, obj);
223
227
  return obj;
224
228
  }
225
229
  __destroy_into_raw() {
226
230
  const ptr = this.__wbg_ptr;
227
231
  this.__wbg_ptr = 0;
228
- McTextFinalization.unregister(this);
232
+ MCTextFinalization.unregister(this);
229
233
  return ptr;
230
234
  }
231
235
  free() {
232
236
  const ptr = this.__destroy_into_raw();
233
237
  wasm.__wbg_mctext_free(ptr, 0);
234
238
  }
235
- /**
236
- * @returns {number}
237
- */
238
- charCount() {
239
- const ret = wasm.mctext_charCount(this.__wbg_ptr);
240
- return ret >>> 0;
241
- }
242
239
  /**
243
240
  * @param {string} json
244
- * @returns {McText}
241
+ * @returns {MCText}
245
242
  */
246
243
  static parseJson(json) {
247
244
  const ptr0 = passStringToWasm0(json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
@@ -250,7 +247,7 @@ export class McText {
250
247
  if (ret[2]) {
251
248
  throw takeFromExternrefTable0(ret[1]);
252
249
  }
253
- return McText.__wrap(ret[0]);
250
+ return MCText.__wrap(ret[0]);
254
251
  }
255
252
  /**
256
253
  * @returns {string}
@@ -267,21 +264,32 @@ export class McText {
267
264
  wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
268
265
  }
269
266
  }
267
+ /**
268
+ * @param {string} text
269
+ * @returns {SpanBuilder}
270
+ */
271
+ add(text) {
272
+ const ptr = this.__destroy_into_raw();
273
+ const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
274
+ const len0 = WASM_VECTOR_LEN;
275
+ const ret = wasm.mctext_add(ptr, ptr0, len0);
276
+ return SpanBuilder.__wrap(ret);
277
+ }
270
278
  constructor() {
271
279
  const ret = wasm.mctext_new();
272
280
  this.__wbg_ptr = ret >>> 0;
273
- McTextFinalization.register(this, this.__wbg_ptr, this);
281
+ MCTextFinalization.register(this, this.__wbg_ptr, this);
274
282
  return this;
275
283
  }
276
284
  /**
277
285
  * @param {string} text
278
- * @returns {McText}
286
+ * @returns {MCText}
279
287
  */
280
288
  static parse(text) {
281
289
  const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
282
290
  const len0 = WASM_VECTOR_LEN;
283
291
  const ret = wasm.mctext_parse(ptr0, len0);
284
- return McText.__wrap(ret);
292
+ return MCText.__wrap(ret);
285
293
  }
286
294
  /**
287
295
  * @returns {any}
@@ -305,13 +313,6 @@ export class McText {
305
313
  wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
306
314
  }
307
315
  }
308
- /**
309
- * @returns {boolean}
310
- */
311
- isEmpty() {
312
- const ret = wasm.mctext_isEmpty(this.__wbg_ptr);
313
- return ret !== 0;
314
- }
315
316
  /**
316
317
  * @returns {string}
317
318
  */
@@ -328,7 +329,7 @@ export class McText {
328
329
  }
329
330
  }
330
331
  }
331
- if (Symbol.dispose) McText.prototype[Symbol.dispose] = McText.prototype.free;
332
+ if (Symbol.dispose) MCText.prototype[Symbol.dispose] = MCText.prototype.free;
332
333
 
333
334
  export class RenderResult {
334
335
  static __wrap(ptr) {
@@ -374,6 +375,97 @@ export class RenderResult {
374
375
  }
375
376
  if (Symbol.dispose) RenderResult.prototype[Symbol.dispose] = RenderResult.prototype.free;
376
377
 
378
+ export class SpanBuilder {
379
+ static __wrap(ptr) {
380
+ ptr = ptr >>> 0;
381
+ const obj = Object.create(SpanBuilder.prototype);
382
+ obj.__wbg_ptr = ptr;
383
+ SpanBuilderFinalization.register(obj, obj.__wbg_ptr, obj);
384
+ return obj;
385
+ }
386
+ __destroy_into_raw() {
387
+ const ptr = this.__wbg_ptr;
388
+ this.__wbg_ptr = 0;
389
+ SpanBuilderFinalization.unregister(this);
390
+ return ptr;
391
+ }
392
+ free() {
393
+ const ptr = this.__destroy_into_raw();
394
+ wasm.__wbg_spanbuilder_free(ptr, 0);
395
+ }
396
+ /**
397
+ * @returns {SpanBuilder}
398
+ */
399
+ obfuscated() {
400
+ const ptr = this.__destroy_into_raw();
401
+ const ret = wasm.spanbuilder_obfuscated(ptr);
402
+ return SpanBuilder.__wrap(ret);
403
+ }
404
+ /**
405
+ * @returns {SpanBuilder}
406
+ */
407
+ underlined() {
408
+ const ptr = this.__destroy_into_raw();
409
+ const ret = wasm.spanbuilder_underlined(ptr);
410
+ return SpanBuilder.__wrap(ret);
411
+ }
412
+ /**
413
+ * @returns {SpanBuilder}
414
+ */
415
+ strikethrough() {
416
+ const ptr = this.__destroy_into_raw();
417
+ const ret = wasm.spanbuilder_strikethrough(ptr);
418
+ return SpanBuilder.__wrap(ret);
419
+ }
420
+ /**
421
+ * @returns {SpanBuilder}
422
+ */
423
+ bold() {
424
+ const ptr = this.__destroy_into_raw();
425
+ const ret = wasm.spanbuilder_bold(ptr);
426
+ return SpanBuilder.__wrap(ret);
427
+ }
428
+ /**
429
+ * @param {string} text
430
+ * @returns {SpanBuilder}
431
+ */
432
+ then(text) {
433
+ const ptr = this.__destroy_into_raw();
434
+ const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
435
+ const len0 = WASM_VECTOR_LEN;
436
+ const ret = wasm.spanbuilder_then(ptr, ptr0, len0);
437
+ return SpanBuilder.__wrap(ret);
438
+ }
439
+ /**
440
+ * @returns {MCText}
441
+ */
442
+ build() {
443
+ const ptr = this.__destroy_into_raw();
444
+ const ret = wasm.spanbuilder_build(ptr);
445
+ return MCText.__wrap(ret);
446
+ }
447
+ /**
448
+ * @param {string} color
449
+ * @returns {SpanBuilder}
450
+ */
451
+ color(color) {
452
+ const ptr = this.__destroy_into_raw();
453
+ const ptr0 = passStringToWasm0(color, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
454
+ const len0 = WASM_VECTOR_LEN;
455
+ const ret = wasm.spanbuilder_color(ptr, ptr0, len0);
456
+ return SpanBuilder.__wrap(ret);
457
+ }
458
+ /**
459
+ * @returns {SpanBuilder}
460
+ */
461
+ italic() {
462
+ const ptr = this.__destroy_into_raw();
463
+ const ret = wasm.spanbuilder_italic(ptr);
464
+ return SpanBuilder.__wrap(ret);
465
+ }
466
+ }
467
+ if (Symbol.dispose) SpanBuilder.prototype[Symbol.dispose] = SpanBuilder.prototype.free;
468
+
377
469
  /**
378
470
  * @param {string} text
379
471
  * @returns {number}
@@ -395,7 +487,7 @@ export function namedColors() {
395
487
 
396
488
  /**
397
489
  * @param {FontSystem} font_system
398
- * @param {string} text
490
+ * @param {MCText} text
399
491
  * @param {number} width
400
492
  * @param {number} height
401
493
  * @param {LayoutOptions} options
@@ -403,10 +495,9 @@ export function namedColors() {
403
495
  */
404
496
  export function render(font_system, text, width, height, options) {
405
497
  _assertClass(font_system, FontSystem);
406
- const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
407
- const len0 = WASM_VECTOR_LEN;
498
+ _assertClass(text, MCText);
408
499
  _assertClass(options, LayoutOptions);
409
- const ret = wasm.render(font_system.__wbg_ptr, ptr0, len0, width, height, options.__wbg_ptr);
500
+ const ret = wasm.render(font_system.__wbg_ptr, text.__wbg_ptr, width, height, options.__wbg_ptr);
410
501
  return RenderResult.__wrap(ret);
411
502
  }
412
503
 
Binary file
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@hexze/mctext",
3
3
  "type": "module",
4
4
  "description": "Minecraft text formatting for JavaScript/WASM",
5
- "version": "1.0.3",
5
+ "version": "1.1.0",
6
6
  "license": "MIT",
7
7
  "repository": {
8
8
  "type": "git",