Giter Site home page Giter Site logo

Library extension about nanosvg HOT 5 OPEN

memononen avatar memononen commented on August 26, 2024
Library extension

from nanosvg.

Comments (5)

memononen avatar memononen commented on August 26, 2024

Hi! These sounds like interesting additions. Yes, I'd be interested to add them.

from nanosvg.

alecive avatar alecive commented on August 26, 2024

Ok, great!
Let me then ask you some questions while I am implementing the methods (this is the main reason why I opened an issue and not directly a pull request). I am a C++ developer so some things are not that obvious to me.

First things first: is this nsvgCopyPath function ok with you?

NSVGpath* nsvgCopyPath(NSVGpath* _p)
{
    NSVGpath* res = (NSVGpath*)malloc(sizeof(NSVGpath));
    res->npts = _p->npts;
    res->pts  = (float*)malloc(res->npts*sizeof(float));

    for (int i = 0; i < res->npts; ++i)
    {
        res->pts[i] = _p->pts[i];
    }

    res->closed = _p->closed;

    for (int i = 0; i < 4; ++i)
    {
        res->bounds[i] = _p->bounds[i];
    }

    res->next = _p->next;

    return res;
}

With "ok with you" I mean if it's ok to have a function declaration like the one above or something different (for example, bool nsvgCopyPath(NSVGpath* _src, NSVGpath* _res). It's completely up to you. I rather prefer the first option because it saves the user the step of allocating memory for the path.

from nanosvg.

memononen avatar memononen commented on August 26, 2024

Copy should be int nsvgCopyPath(NSVGPath* dst, NSVGPath* src), the above function should be called nsvgDuplicatePath() as it returns a dupe :)

The above function is not in line how the rest of the lib is written (i.e. should not use those underscores), and public API should be more paranoid. Something like this:

NSVGpath* nsvgDuplicatePath(NSVGpath* p)
{
    NSVGpath* res = NULL;

    if (p == NULL)
        return NULL;

    res = malloc(sizeof(NSVGpath));
    if (res == NULL) goto error;
    memset(res, 0, sizeof(NSVGpath));

    res->pts = malloc(p->npts * sizeof(float) * 2);
    if (res->pts == NULL) goto error;
    memcpy(res->pts, p->pts, p->npts * sizeof(float) * 2);
    res->npts = p->npts;

    memcpy(res->bounds, p->bounds, sizeof(p->bounds));

    res->closed = p->closed;

    return res;

error:
    if (res != NULL) {
        free(res->puts);
        free(res);
    }
    return NULL;
}

void nsvgAddPathToShape(NSVGpath* s, NSVGpath* p)
{
    if (s != NULL && p != NULL) {
        p->next = s->paths;
        s->paths = p;
    }
}

I think it should not add the path to the linked list automatically.

from nanosvg.

alecive avatar alecive commented on August 26, 2024

Ok great! I will then proceed with nsvgDuplicatePath and open a pull request (just to warm us up).

Some questions to go on:

  • What do you mean with "I think it should not add the path to the linked list automatically"? My idea was not to implement an addPathToShape, but rather an addShapeToImage (but the mechanism is similar so the question remains)
  • In order for me to implement nsvgDuplicateShape, I need to implement also a duplicate method for NSVGgradient and NSVGpaint. I was thinking to not expose them to the user. Shall I call them nsvg__duplicateGradient then (like any other method that is not exposed)?
  • I have some problems with the union in NSVGpaint (never seen it before). How can I copy that?
  • In NSVGgradient, the variable stops is defined as NSVGgradientStop stops[1];. Is it correct to assume that this array is going to be generally with a size bigger than 1?

from nanosvg.

memononen avatar memononen commented on August 26, 2024

The nsvgDuplicatePath() function I made does not add the path to any shape's linked list. I would like the adding to be explicit, so that you can do things like duplicate a path and add it to another shape. Also, there should be a matching nsvgDeletePath() and nsvgRemovePathFromShape().

I don't think you need nsvg__duplicateGradient() just make nsvg__copyPaint(), which should handle copying the gradient too when need be (and also deal with existing gradient in dst paint).

Everything in the union will occupy the same memory location. So you can only use one of the values. This is done based on the type. If the type is NSVG_PAINT_LINEAR_GRADIENT or NSVG_PAINT_RADIAL_GRADIENT then the pointer is used. Take a look at nsvg__deletePaint().

The one sized array in gradient is old C trick for variable sized array, take a look at how it is allocated:
https://github.com/memononen/nanosvg/blob/master/src/nanosvg.h#L824

from nanosvg.

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.