← Home ← Back to /g/

Thread 106866126

71 posts 10 images /g/
Anonymous No.106866126 [Report] >>106866170 >>106866642 >>106866954 >>106867017 >>106868711 >>106872077 >>106873857
WTF Rust
I just wanted to render some fonts to a png file ...
Anonymous No.106866170 [Report] >>106866221 >>106866621 >>106866689
>>106866126 (OP)
you should have more than a 64 GB eMMC if you're developing software
Anonymous No.106866199 [Report]
rookies, try building aosp instead
Anonymous No.106866221 [Report] >>106874758
>>106866170
good meme, i'll save it
Anonymous No.106866547 [Report] >>106866702 >>106866886 >>106868231 >>106868321 >>106868866
meanwhile in C
$ curl -O https://raw.githubusercontent.com/nothings/stb/refs/heads/master/stb_truetype.h
$ curl -O https://github.com/nothings/stb/blob/master/stb_image_write.h
$ gcc -Wall -Wextra -g3 -Og main.c -lm
$ du -sh a.out
756K a.out
Anonymous No.106866621 [Report]
>>106866170
generally agree but I kind of hate that having 30-40GB worth of dependencies in ~/.conan or node_modules or whatever is the norm now.
Anonymous No.106866642 [Report] >>106866656 >>106866701 >>106866702 >>106868356 >>106868501 >>106868879
>>106866126 (OP)
<====
the swash crate might be of interest to you btw
Anonymous No.106866656 [Report] >>106866886
>>106866642
>--release
Anonymous No.106866689 [Report]
>>106866170
criminal that crapbooks still have 256GB soldered
Anonymous No.106866701 [Report] >>106866886
>>106866642
that's 1GB without --release
Anonymous No.106866702 [Report]
>>106866642
Anon, that is still bloated. Probably with lots of CP hidden there and ready to be deployed if you talk shit about certain tribe.
>>106866547
Nice.
Anonymous No.106866886 [Report] >>106866893 >>106867476
>>106866656
>>106866701
>that's 1GB without --release
<====
>>106866547
cargo new render-0
cd render-0
cargo add image --no-default-features --features=png
cargo add swash


and code
use std::str::FromStr;
use std::{env, fs, path::PathBuf};

use image::{GrayImage, ImageFormat};
use swash::FontRef;
use swash::scale::{Render, ScaleContext, Source};

fn main() {
let font_path = env::args().nth(1)
.expect("pass font path");

let font_path = PathBuf::from_str(&font_path)
.expect("valid path");

let font_bytes = fs::read(&font_path)
.expect("failed to read font data");

let font = FontRef::from_index(&font_bytes, 0)
.expect("no fontref from font bytes");

let mut ctx = ScaleContext::new();

let mut scaler = ctx.builder(font)
.size(128.0)
.hint(true)
.build();

let sources = [Source::default()];
let render = Render::new(&sources);
let glyph_id = font.charmap().map('0');

let swash_image = render.render(&mut scaler, glyph_id)
.expect("rendered glyph");

let p = swash_image.placement;


let image = GrayImage::from_raw(p.width, p.height, swash_image.data)
.expect("gray image from swash image data");

image.save_with_format("0.png", ImageFormat::Png)
.expect("save gray image as png");
}


and the binary
cargo build --release
% du -sh target/release/render-0
1.9M target/release/render-0
% strip target/release/render-0
% du -sh target/release/render-0
1.5M target/release/render-0


swash uses some google libraries under the hood which will become universally used.

share your equivalent C program with the sizes of the non-libc libraries required if any.
Anonymous No.106866893 [Report]
>>106866886
><====
forgot picrel
Anonymous No.106866954 [Report] >>106867001 >>106868450
>>106866126 (OP)
>just
1. Styling (parse markup, query system for fonts)
2. Layout (break text into lines)
3. Shaping (compute the glyphs in a line and their positions)
4. Rasterization (rasterize needed glyphs into an atlas/cache)
6. Composition (copy glyphs from the atlas to their desired positions)
Anonymous No.106867001 [Report] >>106868450
>>106866954
let's look at swash
>Non goals
>Due to the intention of being generally useful and easy to integrate, the following areas of related interest are specifically avoided:
>Text layout. This is highly application specific and the requirements for both features and performance differ greatly among web browsers, word processors, text editors, game engines, etc. There is a sibling crate in development that does provide general purpose text layout based on this library.
>Composition. Like layout, this is also application specific in addition to being hardware dependent. Glyph caching, geometry batching and rendering all belong here and should integrate well with the application and the hardware environment.
Anonymous No.106867017 [Report] >>106867277 >>106867375
>>106866126 (OP)
Vector fonts were a mistake.
Anonymous No.106867277 [Report]
>>106867017
The idea is fine the implementation is bad, font formats are the transvestite of formats, they are the prostitute of Adobe, Apple and Microsoft designed to be portable to no systems (they chose to used BE).
Anonymous No.106867375 [Report]
>>106867017
Actually, the font library only adds 17M. It is the image library that adds 500MB
Anonymous No.106867476 [Report] >>106868321 >>106868450
>>106866886
so this renders a single glyph, not layed out text? with stb_truetype it would be something like
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb_truetype.h>

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>


FILE* f = fopen(path, "rb");
if (!f) { err(1, "fopen"); }
fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, 0, SEEK_SET);
void* data = malloc(size);
fread(data, 1, size, f);
fclose(f);

stbtt_fontinfo font_info;
stbtt_InitFont(&font_info, data, stbtt_GetFontOffsetForIndex(data, 0));

float scale = stbtt_ScaleForMappingEmToPixels(&font_info, emsize);

int width, height, offx, offy;
uint8_t* bitmap = stbtt_GetCodepointBitmap(&font_info, scale, scale, codepoint, &width, &height, &xoff, &yoff);

stbi_write_png("output.png", width, height, 1, bitmap, 0);

stbtt_FreeBitmap(bitmap, NULL);
free(data);
Anonymous No.106867965 [Report] >>106868321
#define STB_IMAGE_WRITE_IMPLEMENTATION
#define STB_TRUETYPE_IMPLEMENTATION
#define STBTT_STATIC
#define STB_IMAGE_WRITE_STATIC
#include <stb/stb_image_write.h>
#include <stb/stb_truetype.h>

#include <stdio.h>
#include <stdint.h>


int main(int argc, char *argv[]) {
if (argc != 2) {
printf("USAGE: %s <font-file>\n", argv[0]);
return 1;
}

FILE *fp = fopen(argv[1], "rb");
if (!fp) {
perror("fopen");
return 1;
}
size_t bufsz = 16384 * 16;
uint8_t *fontdata = malloc(bufsz);
size_t fontlen = 0;
while (true) {
size_t toread = bufsz - fontlen;
size_t len = fread(&fontdata[fontlen], 1, bufsz - fontlen, fp);
fontlen += len;
if (len == toread) {
bufsz *= 2;
fontdata = realloc(fontdata, bufsz);
} else break;
}
fclose(fp);

struct stbtt_fontinfo font = {0};
if (!stbtt_InitFont(&font, fontdata, 0)) {
puts("Failed to load font");
return 1;
}

float scale = stbtt_ScaleForMappingEmToPixels(&font, 128);
int w, h;
uint8_t *rendered = stbtt_GetCodepointBitmap(&font, scale, scale, '0', &w, &h, NULL, NULL);

if (!stbi_write_png("out.png", w, h, 1, rendered, 0)) {
puts("Failed to save image\n");
return 1;
}

stbtt_FreeBitmap(rendered, NULL);
free(fontdata);

return 0;
}

Perhaps more verbose, but the program is much smaller.
$ time gcc -lm -O3 ftoimg.c
gcc -lm -O3 ftoimg.c 1.03s user 0.01s system 99% cpu 1.045 total
$ du -sh a.out
56K a.out
$ strip a.out
$ du -sh a.out
52K a.out
Anonymous No.106868231 [Report] >>106868321 >>106868333 >>106868353 >>106868857 >>106871816
>>106866547
>stb_truetype.h
// =======================================================================
//
// NO SECURITY GUARANTEE -- DO NOT USE THIS ON UNTRUSTED FONT FILES
//
// This library does no range checking of the offsets found in the file,
// meaning an attacker can use it to read arbitrary memory.
//
// =======================================================================

OHNONONONO
CENILE BROS
HOW DO WE COPE
This is for performance reasons, right? RIGHT?! Surely it is MUCH faster than rust!!! IT HAS TO BE!
Anonymous No.106868321 [Report] >>106868381 >>106868437 >>106869665 >>106869701
>>106868231
>>106867965
>>106867476
>>106866547
C is one of the oldest languages, its too old and there are far better, I mean just use rust and stop spreading 'trannie psyop' bullfuckery. Its safer, faster, and literally better. 200 megs and ya'll crying, there are 120+ gb games. Grow up ffs
Anonymous No.106868333 [Report] >>106868453
>>106868231
usecase for untrusted font files?
Anonymous No.106868353 [Report]
>>106868231
>HOW DO WE COPE
by not using it on untrusted font files? I don't understand the question.
Anonymous No.106868356 [Report]
>>106866642
...lol
Anonymous No.106868381 [Report]
>>106868321
500M to translate to png. Does your game only write to a png? Odd game
Anonymous No.106868437 [Report]
>>106868321
you do realize that your program is not the only thing on a computer, right? you understand that this shit adds up? right??
Anonymous No.106868450 [Report] >>106868501
>>106866954
>>106867001
>>106867476
you get EVERYTHING with cosmic-text, including advanced shaping of course.
cargo new render-cosmic
cargo add image --no-default-features --features=png
cargo add cosmic-text


main

use cosmic_text::{Attrs, Buffer, Color, Family, FontSystem, Metrics, Shaping, SwashCache, Weight};
use image::{RgbaImage, GenericImage, ImageFormat, Rgba};

fn main() {
let mut font_system = FontSystem::new();
let mut swash_cache = SwashCache::new();

let metrics = Metrics::new(128.0, 128.0);

let mut buffer = Buffer::new(&mut font_system, metrics);
let mut buffer = buffer.borrow_with(&mut font_system);

let (w,h) = (1400, 400);
let mut image = RgbaImage::new(w, h);
buffer.set_size(Some(w as f32), Some(h as f32));

let attrs = Attrs::new()
.family(Family::Name("Noto Sans"))
.weight(Weight::MEDIUM);

buffer.set_text(" 0 1 2 3 4 5 6 7 8 9 0 -+=\n Joined RTL text below:\n اللغة العربية\n", &attrs, Shaping::Advanced);
buffer.shape_until_scroll(true);

let def_color = Color::rgb(0xFF, 0x00, 0x00);


buffer.draw(&mut swash_cache, def_color, |x, y, w, h, c| {
if x >= 0 && y >= 0 {
let c = Rgba([c.r(), c.g(), c.b(), c.a()]);
let (x, y) = (u32::try_from(x).unwrap(), u32::try_from(y).unwrap());
let mut sub_image = image.sub_image(x, y, w, h);
(0..w) .for_each(|i_x| {
(0..h).for_each(|i_y| {
sub_image.put_pixel(i_x, i_y, c)
})
});
}
});

image.save_with_format("cosmic.png", ImageFormat::Png)
.expect("save gray image as png");
}


and sizes

cargo build --release
% du -sh target
161M target
% du -sh target/release/render-cosmic
3.3M target/release/render-cosmic
% strip target/release/render-cosmic
% du -sh target/release/render-cosmic
2.8M target/release/render-cosmic


loaded libraries...
Anonymous No.106868453 [Report]
>>106868333
How do you come to trust font files? Make them yourself?
Anonymous No.106868479 [Report] >>106868791 >>106873906
the funniest thing about all of this is that rust spends all this binary size and compile time on including debug info even though most rust programmers are the kind that doesn't use a debugger and just does printf debugging + constantly recompiling so effectively you're just making their debugging experience worse not better LMFAO
Anonymous No.106868501 [Report] >>106870008
>>106868450
>loaded libraries...

ldd target/release/render-cosmic | rg -r '$1' '.*=> ([^ ]+) .*'|sort -u | xargs realpath | xargs du -sh
2.1M /usr/lib/libc.so.6
888K /usr/lib/libgcc_s.so.1
1.1M /usr/lib/libm.so.6
244K /usr/lib/ld-linux-x86-64.so.2


for comparison, if you need harfbuzz and freetype and nothing else:

% ldd /usr/lib/libharfbuzz.so /usr/lib/libfreetype.so | rg -r '$1' '.*=> ([^ ]+) .*'|sort -u | xargs realpath | xargs du -sh
140K /usr/lib/libbrotlicommon.so.1.1.0
56K /usr/lib/libbrotlidec.so.1.1.0
76K /usr/lib/libbz2.so.1.0.8
2.1M /usr/lib/libc.so.6
832K /usr/lib/libfreetype.so.6.20.4
1.4M /usr/lib/libglib-2.0.so.0.8600.0
136K /usr/lib/libgraphite2.so.3.2.1
1.1M /usr/lib/libm.so.6
684K /usr/lib/libpcre2-8.so.0.14.0
228K /usr/lib/libpng16.so.16.50.0
100K /usr/lib/libz.so.1.3.1


and i'm deliberately not "ricing" sizes here, i could compile against shared rust std, use full lto, a single codegen unit, and use the equivalent of -Os or -Oz, but all that would be retarded, as is all discourse around binary sizes.

but in any case, op was 10x off as shown in >>106866642. which is very usual for /g/ (some claims are 100x off lol).
Anonymous No.106868711 [Report]
>>106866126 (OP)
Just use the gd graphics library, or get the 10x20 gnu unifont and bitblt the characters onto a bitmap and dump the bitmap into whatever (i.e. png) you want.
Mac and Windows can do this pretty much natively.
Anonymous No.106868791 [Report] >>106868803 >>106872081
>>106868479
Rust guarantees that programs don’t have any memory bugs (which is pretty much all bugs, who ever heard of a computer that didn’t have memory).
So you really don’t need a debugger with rust, and testing itself is pretty much not needed since that is all handled with the compiler.
Anonymous No.106868803 [Report]
>>106868791
HAHAHAHAHAHHAHAHAHHAH incredible bait
Anonymous No.106868857 [Report]
>>106868231
the purpose of that library is to convert ttfs to font atlus textures for video games. this is clearly for an intermediary compilation step. why would it matter if it was insecure?
Anonymous No.106868866 [Report]
>>106866547
ok. now show me comparable C and Rust code using these deps.
Anonymous No.106868879 [Report] >>106869279
>>106866642
OP utterly blown the fuck out holy fuck.
Anonymous No.106869279 [Report] >>106870008
>>106868879
53MB is something to be proud of? lol
Anonymous No.106869665 [Report]
>>106868321
>I mean just use rust and stop spreading 'trannie psyop' bullfuckery
It's too late, a decade of trans-activism pro Rust has stamped the mark of HRT all over the language, there is no coming back from that. The term cnile? It's effective. Effective at making people biased against Rust and Rust trannies.

I noticed you Rust advocates using it less often, research on how it has backfired on you must have come in finally. /g/ could have told you that five years ago lol.
Anonymous No.106869701 [Report]
>>106868321
>C is one of the oldest languages
>its too old and there are far better

Don't care I'm still using it
Anonymous No.106869772 [Report]
short: rejected
Anonymous No.106870008 [Report] >>106872061
>>106869279
comment on >>106868501. that is if you even understand anything in that comment lol
Anonymous No.106871816 [Report]
>>106868231
>untested fonts
so i have to test every single font file just to makw sure c doesn't implode
Anonymous No.106872003 [Report]
Let's try python kids!
>compile an executable that prints hello world
54MB
>scratch that, use script that creates qr code images
30KB python library + <1KB script
>alright, alright, alright! now let's do the opposite
7 WHOLE FUCKING GIGABYTES OF VENV DATA SPACE WASTED ON DYNAMIC CUDA LIBS THAT THIS ARCH DOES NOT EVEN SUPPORT

It's not a question of flash drive space anymore, it's a question of a very limited cpu memory cache.
Anonymous No.106872061 [Report] >>106872970
>>106870008
being smug about knowing what compiler flags or shared objects or LTO/ pseud-LTO(a single translation unit gives you the same benefits) are like they're anything special or advanced shows me how experienced you are.
Anonymous No.106872077 [Report] >>106873761
>>106866126 (OP)
Rendering fonts to PNG? Child's play for Lisp! With its powerful macro system and dynamic typing, Lisp makes this trivial task a breeze. You can write a concise, readable program in seconds. And because Lisp's garbage collector is so efficient, your memory usage will be minimal - no bloated packages needed.

In fact, Lisp's unique nature means many other languages, including those massive 512MB packages, are simply dialects of Lisp or heavily influenced by its brilliance. Python, Ruby, even JavaScript - they all owe a debt to the granddaddy of programming languages, Lisp.

So don't waste time with clumsy, memory-hungry tools when Lisp can do it better, faster, and more elegantly. Once you experience the joy of Lisp, you'll never go back to those cumbersome alternatives again. It's not just a language - it's a revelation. Lisp is the greatest programming language ever created, and there's no debate about it.
Anonymous No.106872081 [Report] >>106875217
>>106868791
>rust guarantees that programs don’t have any memory bugs
does it add a fucking ECC you dumb cunt?
>you really don’t need a debugger with rust
age of space travel has ended before it could even start
Anonymous No.106872970 [Report] >>106873418
>>106872061
it's not about me, it's about the tech literacy (or rather, the lack thereof) of the average /g/eet.
half the comments in this thread are taking about "program sizes", when the half-retarded (that's an achievement here) op started off by sharing a built project size. it's like saying the linux kernel occupies 30-40GiB on your disk, because that's the size required to actually build the kernel (with a distro configuration).
every other topic discussed had equally retarded discourse. for example, the talk about that header-only library and its lack of safety misses a much bigger and practically relevant point, which is that it probably lacks many of the fringe and even bordering-on-bizarre features that many fonts seem to make use of, and that's before we get into variations or advanced shaping shit.
and i did notice that no one, including yourself, responded to the main point in that comment in question.
Anonymous No.106873418 [Report]
>>106872970
>and i did notice that no one, including yourself, responded to the main point in that comment in question.
You can't precisely compare the size of two programs unless they link the exact same set of DLLs/shared objects, otherwise you have no way of knowing how much code each one is actually using, since you can load in a DLL without using everything in it (which is by design of course) while on the other hand any good linker will remove dead code when statically linking. You can still make guesstimates, but they are just guesses, and if everyone is guessing all the time then this discussion is pointless. You can also establish upper bounds for each (i.e pick the worst case scenario where the dynamically linked executables uses everything from the DLL), but then that's not a good comparison at all.
For an exact comparison, assuming cosmic-text has the same feature set as freetype and harfbuzz, you have to statically link Freetype and Harfbuzz and stb_image_write with all formats disabled except PNG, or something along those lines.
Anonymous No.106873761 [Report]
>>106872077
SO WHERE'S THE CODE

oh
Anonymous No.106873817 [Report] >>106875428
Another rustroon meltdown thread
Anonymous No.106873857 [Report]
>>106866126 (OP)
Why did you add entire image manipulation library if you just want to output a png file? Are you retarded?
Anonymous No.106873906 [Report] >>106873978
>>106868479
>even though most rust programmers are the kind that doesn't use a debugger and just does printf debugging
Based on what?
I am a rust programmer and I use debugger when applicable. RustRover just works.
Anonymous No.106873978 [Report] >>106873999 >>106875242
>>106873906
I said it on a whim based on my intuition for this sort of thing but if you look it up it's true: https://www.jetbrains.com/lp/devecosystem-2023/rust/
Anonymous No.106873999 [Report] >>106874030
>>106873978
30% using a debugger is a quite a large group.
Do you have data on other languages to compare to. I doubt even 30% of C programmers even know how to use a debugger, considering how popular C is among students that are just getting into programming.
Anonymous No.106874030 [Report] >>106874068
>>106873999
actually 30% is too high because it's not easy to write code that is too broken to require a debugger in rust for most "end developers".
but this is probably offset by the sample being not random, and coming from retards who would require such an IDE to develop shit.
Anonymous No.106874068 [Report] >>106874284
>>106874030
>it's not easy to write code that is too broken to require a debugger in rust for most "end developers".
That is true. I have been writing in Rust for many years now and most of the problems I face are statically detected. Only small part ends up in runtime and printing is more than enough to solve them. I only ever had to use a debugger twice over all these years.

>but this is probably offset by the sample being not random, and coming from retards who would require such an IDE to develop shit.
This survey is based on users of jetbrains IDEs.
There is nothing wrong with using IDEs, everyone use them professionally.
Anonymous No.106874284 [Report] >>106874314
>>106874068
>everyone use them professionally.
wrong. and only someone hiring code monkeys would require such a thing.
the line that separates editors from IDEs is thin anyway, in the age of language servers. that is functionality wise. the bloat disparity will always be there. unless you're maximally retarded to the point of using an Electron-based editor, which would be even worse than a "worst of both worlds" choice.
Anonymous No.106874314 [Report] >>106874664
>>106874284
Name one company where majority of developers do not use IDEs
Anonymous No.106874664 [Report] >>106874678 >>106875415
>>106874314
anything go, C, or rust centric wouldn't require an IDE, and why would it?
with C++, it depends.
with python, i would assume vendoring/packaging would be the prime concern, not forcing pycharm or whatever.
only <jvm lang>/C# jeetoids would think and IDE is some sort of an absolute requirement.
Anonymous No.106874678 [Report] >>106874749
>>106874664
So, can you name any noteworthy company where majority of developers do not use IDEs?
Anonymous No.106874749 [Report]
>>106874678
can you name any noteworthy company where [the] majority of developers use IDEs, with evidence?
Anonymous No.106874758 [Report]
>>106866221
Take it, it's all yours.
Anonymous No.106875217 [Report]
>>106872081
cosmic rays are not in scope for any working programming language model, midwit.
Anonymous No.106875242 [Report] >>106875267
>>106873978
do the same for C or C++ and it will be the same story.

T. worked on projects where gdb would segfault when debugging or simply lose track of where it is and dump me into pure asm.
Anonymous No.106875267 [Report]
>>106875242
Softice
Anonymous No.106875415 [Report]
>>106874664
>wouldn't require an IDE
wouldn't, but why forgo it? even the "muh no IDE" retards still use shit like ctags. I don't know why you faggots think your deranged and shitty workflows should be forced on everyone else.
btw I consider rust-analyzer and any capable LSP editor to be an IDE.
Anonymous No.106875428 [Report] >>106875435 >>106875445 >>106875493
>>106873817
>rustroon
You misspelled Russian.
Anonymous No.106875435 [Report] >>106875445 >>106875493
>>106875428
this. really wish Russia would block 4chan. most the inane schizoposting is mentally ill Z cuckolds.
Anonymous No.106875445 [Report]
>>106875428
>>106875435
>Russia
>Troon
Fuck off
Anonymous No.106875493 [Report]
>>106875435
>>106875428
lol