mgeisler / textwrap

An efficient and powerful Rust library for word wrapping text.
MIT License
446 stars 44 forks source link

Does `textwrap` actually need `Canvas#data` #509

Closed bacloud23 closed 1 year ago

bacloud23 commented 1 year ago

Hi, I'm trying to run textwrap on a canvas that I'm creating in a Node environment. I'm able to create a Canvas normally like the browser one, but in a Node environment, however, I want to optimize resources as much as possible.

Is canvas._data used in anyway in the calculations ?

Kindly.

mgeisler commented 1 year ago

Hi @yanna92yar,

Textwrap itself doens't know anything about canvas or node. However, I wrote a demo to show how you can call Textwrap from a wasm environment: https://github.com/mgeisler/textwrap/tree/master/examples/wasm.

The code there uses the canvas to measure the size of the text, then tells Textwrap about those sizes, wraps the text, and finally draws the text on the canvas.

I hope that helps?

bacloud23 commented 1 year ago

Actually I managed to call draw function just like delivered on the wasm demo, and I'm using a Node canvas implementation, thanks a lot. for instance , I'm passing same values that would work on the demo just to test what's going wrong: https://github.com/mgeisler/textwrap/blob/872f221fede7aa78e730bb7895074455269a431a/examples/wasm/www/index.js#L35

this function draw does not fail, however I think it does not change my canvas object (as far as I could inspect / not really sure, as getting the image buffer is related to the other library that implements Canvas in Node)

ok, let's forget about the Canvas object , as long as textwrap does not necessarily deal with the canvas (except the example); do you provide some API with raw results ? (of calculations) ? hopefully in wasm build as well.

Thanks a lot :)

mgeisler commented 1 year ago

ok, let's forget about the Canvas object , as long as textwrap does not necessarily deal with the canvas (except the example); do you provide some API with raw results ? (of calculations) ? hopefully in wasm build as well.

The interface is abstract: you implement Fragment yourself and this trait is then used by the rest of the Textwrap calculations. As an example, the wrap_first_fit function has this signature:

pub fn wrap_first_fit<'a, 'b, T: Fragment>(
    fragments: &'a [T],
    line_widths: &'b [f64]
) -> Vec<&'a [T]>

which means that you give it a slice of fragments (can be anything, you decide the T type here since you've have implemented Fragment for it) and then you get back the wrapped lines in the form of a vector of slices.

That's the most raw result in Textwrap. You're then supposed to use the vector of slices in your own drawing routine: you will presumably walk through each slice and draw the fragments one after each other. That's what the Wasm demo I include does.

bacloud23 commented 1 year ago

Ok thanks a lot @mgeisler for the clarifications I appreciate that.

mgeisler commented 1 year ago

Great, I'm glad to hear that you got it working!