Giter Site home page Giter Site logo

Word wrap for TextNode about revery HOT 6 CLOSED

revery-ui avatar revery-ui commented on May 1, 2024 2
Word wrap for TextNode

from revery.

Comments (6)

OhadRau avatar OhadRau commented on May 1, 2024 3

I'm pasting this here for now since it provides a good starting place: https://github.com/namniak/canvas-text-wrapper/blob/master/canvas-text-wrapper.js.

There's also Knuth's algorithm for this which is well documented, but it doesn't match the behavior exhibited by most browsers (it uses intra-word spacing to accomplish evenly sized lines)

I might take a look at this early next week to see if I can get a good start on this

from revery.

saitonakamura avatar saitonakamura commented on May 1, 2024 2

I started an implementation in #300

from revery.

jchavarri avatar jchavarri commented on May 1, 2024 1

@saitonakamura I had not seen your PR and created another in #303 😄 Both implementations are really similar so I'll close my PR, but maybe there's something there you might find useful.

I took a lot of code from https://github.com/superbobry/ocaml-textwrap.

from revery.

OhadRau avatar OhadRau commented on May 1, 2024

One important consideration is that since fonts aren't necessarily monospace, we want to split based on glyph width rather than string length. One way of finding a good break point while still handling kerning and ligatures might be to calculate a running sum of the string's width after each char. This will also be useful for a content aware wrapping algorithm: e.g. breaking only on whitespace (so you'd need to look ahead and see the next sum before you can decide where to break). That said, any wrapping would be an improvement over what we have now so it might be good to do a proof of concept version and then iterate on that.

from revery.

bryphe avatar bryphe commented on May 1, 2024

Thanks for thinking about this @OhadRau !

That said, any wrapping would be an improvement over what we have now so it might be good to do a proof of concept version and then iterate on that.

Agree 100% ! 😄

That link you posted seems reasonable to me - wrapping based on words will be the simplest, since it will account for ligatures too.

from revery.

saitonakamura avatar saitonakamura commented on May 1, 2024

@jchavarri I would say I'll throw away my wrapping implementation and replace it with yours, other code is pretty similar

from revery.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.