diff options
author | bptato <nincsnevem662@gmail.com> | 2023-06-02 00:36:54 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-06-05 03:58:21 +0200 |
commit | 8027e52cb221c432bed64517015ebf3182e6166d (patch) | |
tree | 18991f9e74c8dcfc0ed7439f3bc78a0cfec9b2d6 /src/js/intl.nim | |
parent | b3b97465805b7367df461a4b7b830fabaccf3a89 (diff) | |
download | chawan-8027e52cb221c432bed64517015ebf3182e6166d.tar.gz |
Add support for canvas and multipart
Quite incomplete canvas implementation. Crucially, the layout engine can't do much with whatever is drawn because it doesn't support images yet. I've re-introduced multipart as well, with the FormData API. For the append function I've also introduced a hack to the JS binding generator that allows requesting the JSContext pointer in nim procs. Really I should just fix the union generator thing and add support for overloading. In conclusion, for now the only thing canvas can be used for is exporting it as PNG and uploading it somewhere. Also, we now have PNG encoding and decoding too. (Now if only we had sixels as well...)
Diffstat (limited to 'src/js/intl.nim')
-rw-r--r-- | src/js/intl.nim | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/js/intl.nim b/src/js/intl.nim new file mode 100644 index 00000000..28cc42d5 --- /dev/null +++ b/src/js/intl.nim @@ -0,0 +1,46 @@ +# Very minimal Intl module... TODO make it more complete + +import bindings/quickjs +import js/javascript + +type + NumberFormat = ref object + +#TODO ...yeah +proc newNumberFormat(name: string = "en-US", + options = none(JSObject)): NumberFormat {.jsctor.} = + return NumberFormat() + +#TODO: this should accept string/BigInt too +proc format(nf: NumberFormat, num: float64): string {.jsfunc.} = + let s = $num + var i = 0 + var L = s.len + for k in countdown(s.high, 0): + if s[k] == '.': + L = k + break + if L mod 3 != 0: + while i < L mod 3: + result &= s[i] + inc i + if i < L: + result &= ',' + let j = i + while i < L: + if j != i and i mod 3 == j: + result &= ',' + result &= s[i] + inc i + if i + 1 < s.len and s[i] == '.': + if not (s[i + 1] == '0' and s.len == i + 2): + while i < s.len: + result &= s[i] + inc i + +proc addIntlModule*(ctx: JSContext) = + let global = JS_GetGlobalObject(ctx) + let intl = JS_NewObject(ctx) + ctx.registerType(NumberFormat, namespace = intl) + ctx.defineProperty(global, "Intl", intl) + JS_FreeValue(ctx, global) |