Giter Club home page Giter Club logo

Comments (3)

Pomax avatar Pomax commented on August 30, 2024

Did a quick and dirty hack attempt to effect a copy using


window.copyGlyph = function(g) {
    var g2 = new Glyph(g);
    if(g.points) {
        g2.points = [];
        g.points.forEach(function(p) {
            g2.points.push({
                lastPointOfContour: p.lastPointOfContour,
                onCurve: p.onCurve,
                x: p.x,
                y: p.y
            });
        });
    }
    return g2;
}

but this does interesting things - even if I modify the x/y values of points, calling g.draw on the resulting glyph shows me the origin glyph, not the modified one

(the original problem was that modifying the glyph meant modifying every instance, since there is only one, so instead of too much modification, I now have too little =)

from opentype.js.

Pomax avatar Pomax commented on August 30, 2024

With a g.path = window.getGlyphPath(g.points) (an alias for the getPath function) before calling g.draw works. There's a patch in here somewhere, I can smell it.

from opentype.js.

Connum avatar Connum commented on August 30, 2024

Glyph.points is actually just stored once during parsing of TrueType fonts, but it's not used again when calling Glyph.path,
as that path is generated during parsing as well. But actually, now that variable fonts are implemented, those points are used for building the transformed path for TrueType fonts. Glyph.points and Glyph.path currenly have to be modified separately, which is something we need to remedy in the future. When setting the path, points shold be recalculated, and vice versa.

To answer your original question: There's currently no built-in way to duplicate a glyph. You need to do something like this:

new Glyph(Object.assign({}, glyph, {points: deepCloneOfPoints, path: deepCloneOfPath}));

[...glyph.points] and [...glyph.path] will not be enough, because they still hold references to the objects, in those arrays, you'll actually need a deep copy either by using a package made for that, or the quick-and-dirty JSON.parse(JSON.stringify(glyph.path))

from opentype.js.

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.