GithubHelp home page GithubHelp logo

Comments (6)

cormullion avatar cormullion commented on June 2, 2024

Hi! I don't think there's anything simple that will do this.

Interactive drawing applications probably store all graphics 'in the abstract' and then render them on any size drawing at scale/position, etc. And image editing applications can probably copy a buffer quickly onto another surface of a different size. With Cairo/Luxor, it's more like when you start to draw on a sheet of paper or whiteboard and then realize too late that you started out in the wrong place... :)

The nearest you're going to get at present is by using a recording surface and snapshots. I don't know anything about these (the feature was coded by @hustf :)), but the idea is that you specify a bounding box when you output the file. Of course, you still have to be able to keep track of and calculate the bounding box somehow (since graphics are inaccessible once they've been drawn).

Drawing(NaN, NaN, :rec) # no bounds 
background("antiquewhite")
n_circles = 5
margin = 10
@layer begin # non-permanent transformations 
    for _ in 1:n_circles
        randomhue()
        circle(O, 10, :fill)
        translate(30, 0)
    end
end
snapshot(fname="/tmp/temp.svg", 
    cb=BoundingBox(Point(-margin, -100), Point(150, 100)))
finish()

from luxor.jl.

cormullion avatar cormullion commented on June 2, 2024

Talking of recording surfaces, there appears to be a Cairo function called cairo_recording_surface_ink_extents() (https://cairographics.org/manual/cairo-Recording-Surfaces.html#cairo-recording-surface-ink-extents). This suggests that it could calculate the bounding box required to enclose the recording surface. However, my abilities in calling Cairo functions - and my understanding of the recording surface feature - are limited, and my attempt below doesn't work.

function recording_surface_ink_extents()
    x0 = Cdouble[0]
    y0 = Cdouble[0]
    w = Cdouble[0]
    h = Cdouble[0]
    d = Luxor._get_current_drawing_save()
    if d.surfacetype === :rec
        ccall((:cairo_recording_surface_ink_extents, Luxor.Cairo.libcairo),
            Ptr{Nothing},
            (Ptr{Nothing}, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cdouble}),
            d.surface.ptr, x0, y0, w, h)
        return BoundingBox(Point(x0[1], y0[1]), Point(x0[1] + w[1], y0[1] + h[1]))
    else
        throw(error("not a recording surface"))
    end
end

See JuliaGraphics/Cairo.jl#356 for upstream solution.

from luxor.jl.

evanfields avatar evanfields commented on June 2, 2024

Hi! I don't think there's anything simple that will do this.

Thanks for the speedy response! I think the recording surface + snapshot looks like a great solution, and luckily in my case manually tracking the bounding box isn't too hard. From the docs on snapshot referencing drawing it seems like the snapshots can also be in memory with fname = :svg? Seems excellent.

from luxor.jl.

cormullion avatar cormullion commented on June 2, 2024

If you encounter problems, it night be worth running on the current master. There’s been some recent work on this which hasn’t yet been released. (Here’s a place to start: #150 )

from luxor.jl.

hustf avatar hustf commented on June 2, 2024

Yes, this complicates things fast, which is why it's not in Luxor currently. Also, a surprising number of Luxor functions uses the limits defined in Drawing. In #258, I linked to a proposal, which makes alternative functions for limitless drawings.

from luxor.jl.

stale avatar stale commented on June 2, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

from luxor.jl.

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.