markup-web-thingy

This commit is contained in:
Schrottkatze 2024-01-04 21:07:50 +01:00
commit f964d57ca1
8 changed files with 1967 additions and 0 deletions

1
.envrc Normal file
View file

@ -0,0 +1 @@
use flake . --impure

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/target
.direnv
.devenv

1357
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

16
Cargo.toml Normal file
View file

@ -0,0 +1,16 @@
[package]
name = "webthing"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
raqote = "0.8.3"
clap = { version = "4.4", features = [ "derive" ] }
log = "0.4.20"
env_logger = "0.10.1"
font-kit = { version = "0.11", features = [ "loader-freetype" ]}
simple_logger = "4.3.3"
logos = "0.13.0"
cosmic-text = "0.10.0"

17
example.hswt Normal file
View file

@ -0,0 +1,17 @@
some text
more text
(bold "bold text too")
(italic "italic text too")
(struckThrough "struck through text too")
(sparkling "sparkling text too")
(heading level=1 "heading level 1 too")
(heading level=2 "heading level 2 too")
(heading level=3 "heading level 3 too")

305
flake.lock Normal file
View file

@ -0,0 +1,305 @@
{
"nodes": {
"devenv": {
"inputs": {
"flake-compat": "flake-compat",
"nix": "nix",
"nixpkgs": "nixpkgs",
"pre-commit-hooks": "pre-commit-hooks"
},
"locked": {
"lastModified": 1703939110,
"narHash": "sha256-GgjYWkkHQ8pUBwXX++ah+4d07DqOeCDaaQL6Ab86C50=",
"owner": "cachix",
"repo": "devenv",
"rev": "7354096fc026f79645fdac73e9aeea71a09412c3",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "devenv",
"type": "github"
}
},
"fenix": {
"inputs": {
"nixpkgs": "nixpkgs_2",
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1704349399,
"narHash": "sha256-Qjb/gZHifIIqizu8pTbK0kegTxInsBqoIumcjSGlrVo=",
"owner": "nix-community",
"repo": "fenix",
"rev": "4d3f3b9997eb11be47b6a619e7c7db756bbfcc06",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1673956053,
"narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1685518550,
"narHash": "sha256-o2d0KcvaXzTrPRIo0kOLV0/QXHhDQ5DTi+OxcjO8xqY=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "a1720a10a6cfe8234c0e93907ffe81be440f4cef",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"gitignore": {
"inputs": {
"nixpkgs": [
"devenv",
"pre-commit-hooks",
"nixpkgs"
]
},
"locked": {
"lastModified": 1660459072,
"narHash": "sha256-8DFJjXG8zqoONA1vXtgeKXy68KdJL5UaXR8NtVMUbx8=",
"owner": "hercules-ci",
"repo": "gitignore.nix",
"rev": "a20de23b925fd8264fd7fad6454652e142fd7f73",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "gitignore.nix",
"type": "github"
}
},
"lowdown-src": {
"flake": false,
"locked": {
"lastModified": 1633514407,
"narHash": "sha256-Dw32tiMjdK9t3ETl5fzGrutQTzh2rufgZV4A/BbxuD4=",
"owner": "kristapsdz",
"repo": "lowdown",
"rev": "d2c2b44ff6c27b936ec27358a2653caaef8f73b8",
"type": "github"
},
"original": {
"owner": "kristapsdz",
"repo": "lowdown",
"type": "github"
}
},
"nix": {
"inputs": {
"lowdown-src": "lowdown-src",
"nixpkgs": [
"devenv",
"nixpkgs"
],
"nixpkgs-regression": "nixpkgs-regression"
},
"locked": {
"lastModified": 1676545802,
"narHash": "sha256-EK4rZ+Hd5hsvXnzSzk2ikhStJnD63odF7SzsQ8CuSPU=",
"owner": "domenkozar",
"repo": "nix",
"rev": "7c91803598ffbcfe4a55c44ac6d49b2cf07a527f",
"type": "github"
},
"original": {
"owner": "domenkozar",
"ref": "relaxed-flakes",
"repo": "nix",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1678875422,
"narHash": "sha256-T3o6NcQPwXjxJMn2shz86Chch4ljXgZn746c2caGxd8=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "126f49a01de5b7e35a43fd43f891ecf6d3a51459",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-regression": {
"locked": {
"lastModified": 1643052045,
"narHash": "sha256-uGJ0VXIhWKGXxkeNnq4TvV3CIOkUJ3PAoLZ3HMzNVMw=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2",
"type": "github"
},
"original": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2",
"type": "github"
}
},
"nixpkgs-stable": {
"locked": {
"lastModified": 1685801374,
"narHash": "sha256-otaSUoFEMM+LjBI1XL/xGB5ao6IwnZOXc47qhIgJe8U=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "c37ca420157f4abc31e26f436c1145f8951ff373",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-23.05",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1704194953,
"narHash": "sha256-RtDKd8Mynhe5CFnVT8s0/0yqtWFMM9LmCzXv/YKxnq4=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "bd645e8668ec6612439a9ee7e71f7eac4099d4f6",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_3": {
"locked": {
"lastModified": 1704018918,
"narHash": "sha256-erjg/HrpC9liEfm7oLqb8GXCqsxaFwIIPqCsknW5aFY=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "2c9c58e98243930f8cb70387934daa4bc8b00373",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-23.05",
"repo": "nixpkgs",
"type": "github"
}
},
"pre-commit-hooks": {
"inputs": {
"flake-compat": [
"devenv",
"flake-compat"
],
"flake-utils": "flake-utils",
"gitignore": "gitignore",
"nixpkgs": [
"devenv",
"nixpkgs"
],
"nixpkgs-stable": "nixpkgs-stable"
},
"locked": {
"lastModified": 1688056373,
"narHash": "sha256-2+SDlNRTKsgo3LBRiMUcoEUb6sDViRNQhzJquZ4koOI=",
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "5843cf069272d92b60c3ed9e55b7a8989c01d4c7",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"type": "github"
}
},
"root": {
"inputs": {
"devenv": "devenv",
"fenix": "fenix",
"nixpkgs": "nixpkgs_3",
"systems": "systems_2"
}
},
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1704292810,
"narHash": "sha256-ynqFyUbzHEP9CH3KPnizBKZ1vNA42+sxZ301VXd2VSI=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "9279c6566b78c7dd9a11a648a7f287bbd37a0eea",
"type": "github"
},
"original": {
"owner": "rust-lang",
"ref": "nightly",
"repo": "rust-analyzer",
"type": "github"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"systems_2": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

57
flake.nix Normal file
View file

@ -0,0 +1,57 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.05";
systems.url = "github:nix-systems/default";
devenv.url = "github:cachix/devenv";
fenix.url = "github:nix-community/fenix";
};
nixConfig = {
extra-trusted-public-keys = "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=";
extra-substituters = "https://devenv.cachix.org";
};
outputs = {
self,
nixpkgs,
devenv,
systems,
...
} @ inputs: let
forEachSystem = nixpkgs.lib.genAttrs (import systems);
in {
devShells =
forEachSystem
(system: let
pkgs = nixpkgs.legacyPackages.${system};
in {
default = devenv.lib.mkShell {
inherit inputs pkgs;
modules = [
({
pkgs,
config,
...
}: {
languages.rust = {
enable = true;
channel = "nightly";
};
pre-commit.hooks = {
clippy.enable = true;
rustfmt.enable = true;
};
packages = with pkgs; [
pkg-config
cmakeMinimal
fontconfig
freetype
];
})
];
};
});
};
}

211
src/main.rs Normal file
View file

@ -0,0 +1,211 @@
use cosmic_text::{Attrs, Buffer, Color, FontSystem, Metrics, Shaping, SwashCache};
use std::{
collections::HashMap,
fs::{self, File},
path::PathBuf,
};
use clap::Parser;
use font_kit::loader::Loader;
use log::{debug, info};
use logos::{Lexer, Logos};
use raqote::{DrawOptions, DrawTarget, Point, SolidSource, Source};
#[derive(Clone, Debug, Parser)]
struct Args {
#[arg(short, default_value = "1920")]
x: u16,
#[arg(short, default_value = "1080")]
y: u16,
// swt means "Hyper Some Web Thing"
#[arg(short, long)]
file: PathBuf,
}
fn main() {
simple_logger::init().unwrap();
let args = Args::parse();
info!(
"Henlo world, resolution to render to is {}*{}px",
args.x, args.y
);
let txt = fs::read_to_string(args.file).unwrap();
dbg!(parse_the_shit_out_of_this_but_manually(&txt));
let mut dt = DrawTarget::new(args.x.into(), args.y.into());
render_text(&mut dt);
dt.write_png("out.png").unwrap();
}
fn parse_the_shit_out_of_this_but_manually(text: &str) -> Vec<LanguageStructureThingy> {
let mut lex = Token::lexer(text);
let mut r = Vec::new();
loop {
match lex.next() {
Some(Ok(Token::Text(s))) => r.push(LanguageStructureThingy::Text(s)),
Some(Ok(Token::ParenOpen)) => hehe_sexpression_funy(&mut r, &mut lex),
Some(Ok(Token::ParenClose)) => todo!(),
Some(Ok(Token::String(_))) => todo!(),
Some(Ok(Token::Equals)) => todo!(),
Some(Ok(Token::Asterisk)) => todo!(),
Some(Ok(Token::Underscore)) => todo!(),
Some(Ok(Token::Backslash)) => todo!(),
Some(Ok(Token::WavyThing)) => todo!(),
Some(Ok(Token::Sparkles)) => todo!(),
Some(Ok(Token::HeadingLevelIndicator)) => todo!(),
Some(Err(_)) => panic!("mauuu~ :("),
None => break,
}
}
r
}
fn hehe_sexpression_funy(r: &mut Vec<LanguageStructureThingy>, lex: &mut Lexer<'_, Token>) {
if let Some(Ok(Token::Text(s))) = lex.next() {
let strs = s.trim_start().split_whitespace().collect::<Vec<&str>>();
let name = strs[0].to_owned();
let mut attrs = HashMap::new();
if strs.len() == 1 {
} else if strs.len() == 2 && (lex.next() == Some(Ok(Token::Equals))) {
if let Some(Ok(Token::Text(next))) = lex.next() {
let _ = attrs.insert(strs[1].to_owned(), next.trim_end().to_owned());
}
} else {
todo!()
}
let content = if let Some(Ok(Token::String(s))) = lex.next() {
Box::new(LanguageStructureThingy::Text(s))
} else {
todo!()
};
r.push(LanguageStructureThingy::Sexpression {
name,
attrs,
content,
});
if let Some(Ok(Token::ParenClose)) = lex.next() {
return;
}
// handle other things too
todo!()
}
}
struct HswtRenderer {
dt: DrawTarget,
font_system: FontSystem,
swash_cache: SwashCache,
}
fn render_text(dt: &mut DrawTarget) {
// A FontSystem provides access to detected system fonts, create one per application
let mut font_system = FontSystem::new();
// A SwashCache stores rasterized glyphs, create one per application
let mut swash_cache = SwashCache::new();
// Text metrics indicate the font size and line height of a buffer
let metrics = Metrics::new(48.0, 60.0);
// A Buffer provides shaping and layout for a UTF-8 string, create one per text widget
let mut buffer = Buffer::new(&mut font_system, metrics);
// Borrow buffer together with the font system for more convenient method calls
let mut buffer = buffer.borrow_with(&mut font_system);
// Set a size for the text buffer, in pixels
buffer.set_size(1920.0, 1080.0);
// Attributes indicate what font to choose
let attrs = Attrs::new();
// Add some text!
buffer.set_text("Hello, Rust! 🦀\n", attrs, Shaping::Advanced);
// Perform shaping as desired
buffer.shape_until_scroll();
// Inspect the output runs
for run in buffer.layout_runs() {
for glyph in run.glyphs.iter() {
println!("{:#?}", glyph);
}
}
// Create a default text color
let text_color = Color::rgb(0xFF, 0xFF, 0xFF);
// Draw the buffer (for performance, instead use SwashCache directly)
buffer.draw(&mut swash_cache, text_color, |x, y, w, h, color| {
let (r, g, b, a) = color.as_rgba_tuple();
dt.fill_rect(
x as f32,
y as f32,
w as f32,
h as f32,
&Source::Solid(SolidSource::from_unpremultiplied_argb(a, r, g, b)),
&DrawOptions {
..Default::default()
},
)
});
}
#[derive(Debug)]
enum LanguageStructureThingy {
Text(String),
Newline,
Sexpression {
name: String,
attrs: HashMap<String, String>,
content: Box<LanguageStructureThingy>,
},
}
struct ObjectStyles {
height: Option<Size>,
width: Option<Size>,
}
enum Size {
Absolute(u16),
Relative(f32),
Auto,
}
#[derive(Logos, Debug, PartialEq)]
enum Token {
#[regex("[a-zA-Z\\d\\w]+", |lex| lex.slice().to_owned())]
Text(String),
#[token("(")]
ParenOpen,
#[token(")")]
ParenClose,
#[regex(r#""([^"\\]|\\["\\bnfrt]|u[a-fA-F0-9]{4})*""#, |lex| lex.slice().to_owned())]
String(String),
#[token("=")]
Equals,
#[token("*")]
Asterisk,
#[token("_")]
Underscore,
#[token("\\")]
Backslash,
#[token("~")]
WavyThing,
#[token("")]
Sparkles,
#[token("|| ")]
HeadingLevelIndicator,
}