Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
Add the following to your `goboscript.toml` file:

```toml
std = "2.0.0"
std = "2.1.0"
```
127 changes: 127 additions & 0 deletions font.gs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# Usage:
# copy font.txt to your project directory
# include the font data in the font_data list
# list font_data = file ```font.txt```;
# generate your own font using font.py
# in inkscape, use these options:
# Input/Output > SVG output > Path data > Path string format = Absolute
# Input/Output > SVG output > Path data > Force repeat commands = Checked

var font_offset = 0;
var font_x = 0;
var font_y = 0;
var font_scale = 1;
var font_x1 = 0;
var font_x2 = 240;
var font_char_spacing = 0;
var font_line_spacing = 0;
var font_linelen = 0;

%define FONT_NAME font_data[font_offset+1]
%define FONT_CREATOR font_data[font_offset+2]
%define FONT_RIGHTS font_data[font_offset+3]
%define FONT_WIDTH font_data[font_offset+4]
%define FONT_HEIGHT font_data[font_offset+5]

%define FONT_CALCULATE_WIDTH_FROM_LENGTH(LENGTH) \
(LENGTH*(FONT_WIDTH+2+font_char_spacing)-(2+font_char_spacing))*font_scale
%define FONT_CALCULATE_WIDTH(TEXT) FONT_CALCULATE_WIDTH_FROM_LENGTH(length($TEXT))

proc font_render_char char {
switch_costume $char;
local x = "";
local y = "";
local i = font_offset+font_data[5+font_offset+costume_number()];
forever {
if font_data[i] == "M" {
pen_up;
goto font_x+font_scale*font_data[i+1], font_y-font_scale*font_data[i+2];
i += 3;
if x == "" {
x = x_position();
y = y_position();
}
} elif font_data[i] == "L" {
pen_down;
goto font_x+font_scale*font_data[i+1], font_y-font_scale*font_data[i+2];
i += 3;
} elif font_data[i] == "H" {
pen_down;
set_x font_x+font_scale*font_data[i+1];
i += 2;
} elif font_data[i] == "V" {
pen_down;
set_y font_y-font_scale*font_data[i+1];
i += 2;
} elif font_data[i] == "Z" {
pen_down;
goto x, y;
i += 1;
} else {
pen_up;
stop_this_script;
}
}
}

proc font_render_begin {
font_x = font_x1;
font_linelen = 0;
}

proc font_render_text text {
local i = 1;
repeat length($text) {
font_render_char $text[i];
font_x += (FONT_WIDTH+2+font_char_spacing)*font_scale;
i++;
}
}

proc font_render_text_softwrap text {
local maxlen = (font_x2 - font_x1) // ((FONT_WIDTH+2+font_char_spacing)*font_scale);
local i = 1;
local font_linelen = 0;
local word = "";
until i > length($text) {
until $text[i] == " " or i > length($text) {
word &= $text[i];
i++;
}
if font_linelen + length(word) > maxlen {
font_y -= (FONT_HEIGHT+4+font_line_spacing)*font_scale;
font_x = font_x1;
font_linelen = 0;
}
local j = 1;
repeat length(word) {
font_render_char word[j];
font_x += (FONT_WIDTH+2+font_char_spacing)*font_scale;
font_linelen += 1;
if font_x > font_x2 {
font_y -= (FONT_HEIGHT+4+font_line_spacing)*font_scale;
font_x = font_x1;
font_linelen = 0;
}
j++;
}
word = "";
until $text[i] != " " or i > length($text) {
word &= $text[i];
i++;
}
local j = 1;
repeat length(word) {
font_render_char word[j];
font_x += (FONT_WIDTH+2+font_char_spacing)*font_scale;
font_linelen += 1;
if font_x > font_x2 {
font_y -= (FONT_HEIGHT+4+font_line_spacing)*font_scale;
font_x = font_x1;
font_linelen = 0;
}
j++;
}
word = "";
}
}
90 changes: 90 additions & 0 deletions font.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# pyright: reportAny=false
# pyright: reportUnusedCallResult=false

import argparse
import re
import xml.etree.ElementTree
from dataclasses import dataclass
from pathlib import Path
from typing import cast

ns = {
"svg": "http://www.w3.org/2000/svg",
"inkscape": "http://www.inkscape.org/namespaces/inkscape",
"dc": "http://purl.org/dc/elements/1.1/",
"cc": "http://creativecommons.org/ns#",
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
}

CHARSET = "".join(map(chr, range(ord(" "), ord("~") + 1)))

argparser = argparse.ArgumentParser()
argparser.add_argument("font", type=Path, help="Font .svg file to convert")
argparser.add_argument("output", type=Path, help="Output .txt file")


@dataclass
class Args:
font: Path
output: Path


args = Args(**argparser.parse_args().__dict__)
name = args.font.stem
root = xml.etree.ElementTree.parse(args.font).getroot()
width = int(cast(str, root.get("width")))
height = int(cast(str, root.get("height")))
creator = root.find(".//dc:creator/dc:Agent/dc:title", ns) or ""
rights = root.find(".//dc:rights/dc:Agent/dc:title", ns) or ""
chars: dict[str, list[str]] = {}


def modulate(d: list[str]) -> list[str]:
cmd: str = ""
i = 0
while i < len(d):
if d[i].isalpha():
cmd = d[i]
i += 1
if cmd.upper() in "ML":
if cmd.isupper():
d[i] = str(int(float(d[i])) % (width * 2))
i += 2
elif cmd.upper() == "H":
if cmd.isupper():
d[i] = str(int(float(d[i])) % (width * 2))
i += 1
elif cmd.upper() == "V":
i += 1
return d


for path in root.findall(".//svg:path", ns):
sd = cast(str, path.get("d"))
label = cast(str, path.get("{%s}label" % ns["inkscape"]))
if len(label) == 1:
chars[label] = modulate(re.split(r"[,\s]+", sd))

with args.output.open("w") as f:

def print(*objs: object, sep: str = " ", end: str = "\n") -> None:
f.write(sep.join(map(str, objs)) + end)

print(name)
print(creator)
print(rights)
print(width)
print(height)

i = 0
for ch in CHARSET:
d = chars.get(ch, [])
d.append("#")
print(6 + len(CHARSET) + i)
i += len(d)
for ch in CHARSET:
d: list[str] = chars.get(ch, ["#"])
for x in d:
if str(x).islower() and x not in "Zz":
x = "d" + x
print(x)
Loading