Skip to content
This repository was archived by the owner on Jun 8, 2021. It is now read-only.

SvgSurface: make filename param optional #294

Merged
merged 1 commit into from
Dec 5, 2019
Merged

SvgSurface: make filename param optional #294

merged 1 commit into from
Dec 5, 2019

Conversation

bilelmoussaoui
Copy link
Member

@bilelmoussaoui bilelmoussaoui commented Nov 28, 2019

Per the docs https://www.cairographics.org/manual/cairo-SVG-Surfaces.html
This allows drawing parts of a SVG file without saving it to a tmp file.

I'm not sure if what I did is right or not :P

Fixes #293

Copy link
Member

@GuillaumeGomez GuillaumeGomez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@bilelmoussaoui
Copy link
Member Author

@GuillaumeGomez Any idea why the CI fails with

error[E0599]: no method named `to_glib_none` found for type `std::option::Option<&std::string::String>` in the current scope

  --> src/svg.rs:44:61

   |

44 |                 ffi::cairo_svg_surface_create(path.as_ref().to_glib_none().0, width, height),

   |                                                             ^^^^^^^^^^^^ method not found in `std::option::Option<&std::string::String>`

It builds and works fine locally here.

@GuillaumeGomez
Copy link
Member

Try to change your code as follow:

path.map(|s| s.as_str()).to_glib_none().0

@bilelmoussaoui
Copy link
Member Author

My bad! I was running the tests without --features svg. It should pass this time. Thanks!

@bilelmoussaoui
Copy link
Member Author

It fails because tests are run without --features use_glib 🤦‍♂️

@GuillaumeGomez
Copy link
Member

Oh then it depends: is it supposed to be run even without glib or not? If so, please don't use to_glib calls.

@bilelmoussaoui
Copy link
Member Author

It doesn't need to. In such case, what I'm supposed to do?

@GuillaumeGomez
Copy link
Member

Like this:

let c_str = CString::new(c_str).unwrap();
// then:
c_str.as_ptr();

@sdroege
Copy link
Member

sdroege commented Dec 4, 2019

lgtm

src/svg.rs Outdated
#[cfg(windows)]
let path = {
// Based on GLib path to C
let path_str = path
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes that path is not an Option and already a Path. Needs the map(), as_ref() part from above too

src/svg.rs Outdated
CString::new(p.as_ref().as_os_str().as_bytes())
.expect("Invalid path with NULL bytes")
});
path.map(|p| p.as_ptr()).unwrap_or(ptr::null())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path will be freed once this block is done, and then you store a dangling pointer in path in the outer scope. Just return the Option<CString> from this block, and do the as_ptr() part for both Windows/UNIX just before calling the C function.

@sdroege
Copy link
Member

sdroege commented Dec 4, 2019

Looks good for real now :)

@GuillaumeGomez
Copy link
Member

Then I'll merge for real once CI passed. 😆

@EPashkin
Copy link
Member

EPashkin commented Dec 4, 2019

@bilelmoussaoui Thanks, 👍

@EPashkin
Copy link
Member

EPashkin commented Dec 4, 2019

@sdroege, @GuillaumeGomez Maybe we need to make these function public someday
https://github.com/gtk-rs/glib/blob/master/src/translate.rs#L422-L486

@GuillaumeGomez
Copy link
Member

@EPashkin Oh indeed! Do you mind opening an issue for it please? So it can be done for the next release. :)

@sdroege
Copy link
Member

sdroege commented Dec 4, 2019

That would not help here as otherwise cairo would have to always depend on glib

@EPashkin
Copy link
Member

EPashkin commented Dec 4, 2019

@sdroege Agree about this case, but IMHO here also better add separate path_to_c and use with map to make code more clear

@bilelmoussaoui
Copy link
Member Author

@sdroege Agree about this case, but IMHO here also better add separate path_to_c and use with map to make code more clear

I don't see a valid reason to do that as the function won't be used anywhere else but I agree it makes that part of code less readable.

The failing tests are not related to this change I think.

@EPashkin
Copy link
Member

EPashkin commented Dec 4, 2019

@bilelmoussaoui Why you think that it not related?

failures:

---- svg::test::file stdout ----

thread 'svg::test::file' panicked at 'called `Result::unwrap()` on an `Err` value: "error while writing to output stream"', src/libcore/result.rs:1189:5

failures:

    svg::test::file

test result: FAILED. 39 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out

@EPashkin
Copy link
Member

EPashkin commented Dec 4, 2019

IMHO test with None also will be good

@bilelmoussaoui
Copy link
Member Author

Because I had that issue once, tried to reproduce it and it was gone the next time I ran the tests. Yeah a test with None will be good. Will add that.

@sdroege
Copy link
Member

sdroege commented Dec 4, 2019

Still fails to write the output file, on Windows and macOS but not on Linux.

@EPashkin
Copy link
Member

EPashkin commented Dec 4, 2019

On Windows SvgSurface::new not work even for direct path

@EPashkin
Copy link
Member

EPashkin commented Dec 4, 2019

Old version works on let surface = SvgSurface::new(100., 100., "_.tmp").unwrap();,
I try to find problem

Per the docs https://www.cairographics.org/manual/cairo-SVG-Surfaces.html
This allows drawing parts of a SVG file without saving it to a tmp file.
@EPashkin
Copy link
Member

EPashkin commented Dec 4, 2019

CI passed

@sdroege
Copy link
Member

sdroege commented Dec 4, 2019

Great, let's get it in then :)

@GuillaumeGomez
Copy link
Member

Thanks!

@GuillaumeGomez GuillaumeGomez merged commit fe65952 into gtk-rs:master Dec 5, 2019
@bilelmoussaoui bilelmoussaoui deleted the patch-1 branch December 5, 2019 13:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SvgSurface path should be an Option
4 participants