Git Product home page Git Product logo

Comments (2)

BearOve avatar BearOve commented on July 25, 2024

I do have a solution, but it is far from ideal. I wanted to look into adding support for this trough code-changes in wgpu_glyph itself, but haven't had time yet.

This is a simplified version of how I ended up doing it. After each draw I can use self.last_bounds + the index of the glyph to
highlight a rect that covers it like a text selection.

Note: To handle whitespace I ended up replacing it with a "_" and making the color invisible. When I used regular whitepace
it does not draw anything and the bound rect was missing causing the indexes to be wrong.

    fn my_draw_text(&mut self, ...) {
        let layout = ...;
        let section = ...;

        // Avoid re-allocating new vec's every time by storing them internally
        self.last_glyphs.clear();
        self.last_bounds.clear();

        // Get all the glyphs for the current section to calculate their bounds. Due to
        // mutable borrow, this must be stored first.
        self.last_glyphs
            .extend(self.brush.glyphs_custom_layout(&s, &layout).cloned());

        // Calculate the bounds of each glyph
        self.last_bounds
            .extend(self.last_glyphs.iter().map(|glyph| {
                let bounds = &fonts[glyph.font_id.0].glyph_bounds(&glyph.glyph);
                Rect::new(
                    Vec2::new(bounds.min.x, bounds.min.y),
                    Vec2::new(bounds.max.x, bounds.max.y),
                )
            }));

        // Queue the glyphs for drawing
        self.brush.queue_custom_layout(s, &layout);
    }

from wgpu_glyph.

BokuBeam avatar BokuBeam commented on July 25, 2024

This is helpful, thank you for posting your example!

from wgpu_glyph.

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.