dora_ssr/dora/svg.rs
1/* Copyright (c) 2016-2025 Li Jin <dragon-fly@qq.com>
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8
9extern "C" {
10 fn svg_type() -> i32;
11 fn svgdef_get_width(slf: i64) -> f32;
12 fn svgdef_get_height(slf: i64) -> f32;
13 fn svgdef_render(slf: i64);
14 fn svgdef_new(filename: i64) -> i64;
15}
16use crate::dora::IObject;
17/// A struct used for Scalable Vector Graphics rendering.
18pub struct SVG { raw: i64 }
19crate::dora_object!(SVG);
20impl SVG {
21 pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
22 (unsafe { svg_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
23 match raw {
24 0 => None,
25 _ => Some(Box::new(SVG { raw: raw }))
26 }
27 })
28 }
29 /// Gets the width of the SVG object.
30 pub fn get_width(&self) -> f32 {
31 return unsafe { svgdef_get_width(self.raw()) };
32 }
33 /// Gets the height of the SVG object.
34 pub fn get_height(&self) -> f32 {
35 return unsafe { svgdef_get_height(self.raw()) };
36 }
37 /// Renders the SVG object, should be called every frame for the render result to appear.
38 pub fn render(&mut self) {
39 unsafe { svgdef_render(self.raw()); }
40 }
41 /// Creates a new SVG object from the specified SVG file.
42 ///
43 /// # Arguments
44 ///
45 /// * `filename` - The path to the SVG format file.
46 ///
47 /// # Returns
48 ///
49 /// * `Svg` - The created SVG object.
50 pub fn new(filename: &str) -> Option<SVG> {
51 unsafe { return SVG::from(svgdef_new(crate::dora::from_string(filename))); }
52 }
53}