Skip to content

Commit

Permalink
fix: RectSizeの作成
Browse files Browse the repository at this point in the history
  • Loading branch information
PigeonsHouse committed Jan 6, 2025
1 parent 4316cab commit ebf097f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
15 changes: 14 additions & 1 deletion vsml_core/src/schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,23 @@ pub enum ObjectType<I, A> {
Other(Arc<dyn ObjectProcessor<I, A>>),
}

#[derive(Debug)]
pub struct RectSize {
pub width: f32,
pub height: f32,
}

impl RectSize {
pub const fn new(width: f32, height: f32) -> RectSize {
RectSize { width, height }
}
pub const ZERO: RectSize = RectSize::new(0.0, 0.0);
}

pub trait ObjectProcessor<I, A> {
fn name(&self) -> &str;
fn default_duration(&self, attributes: &HashMap<String, String>) -> f64;
fn default_image_size(&self, attributes: &HashMap<String, String>) -> (f32, f32);
fn default_image_size(&self, attributes: &HashMap<String, String>) -> RectSize;
fn process_image(
&self,
render_sec: f64,
Expand Down
19 changes: 10 additions & 9 deletions vsml_iv_converter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::sync::Arc;
use vsml_ast::vsml::{Content, Element, Meta, VSML};
use vsml_ast::vss::{Rule, VSSItem, VSSSelector, VSSSelectorTree};
use vsml_core::schemas::{
Duration, IVData, LayerMode, ObjectData, ObjectProcessor, ObjectType, Order, StyleData,
Duration, IVData, LayerMode, ObjectData, ObjectProcessor, ObjectType, Order, RectSize,
StyleData,
};
use vsml_core::ElementRect;

Expand Down Expand Up @@ -186,8 +187,8 @@ fn convert_tag_element<'a, I, A>(
ObjectType::Other(processor) => processor.default_duration(attributes),
};
let mut rule_target_duration = None;
let mut target_size: (f32, f32) = match &object_type {
ObjectType::Wrap => (0.0, 0.0),
let mut target_size: RectSize = match &object_type {
ObjectType::Wrap => RectSize::ZERO,
ObjectType::Other(processor) => processor.default_image_size(attributes),
};

Expand Down Expand Up @@ -274,11 +275,11 @@ fn convert_tag_element<'a, I, A>(
}
if layer_mode == LayerMode::Single {
children_offset_position.0 += element_rect.width;
target_size.0 += element_rect.width;
target_size.1 = target_size.1.max(element_rect.height);
target_size.width += element_rect.width;
target_size.height = target_size.height.max(element_rect.height);
} else {
target_size.0 = target_size.0.max(element_rect.width);
target_size.1 = target_size.1.max(element_rect.height);
target_size.width = target_size.width.max(element_rect.width);
target_size.height = target_size.height.max(element_rect.height);
}
}
object_data_children.push(child_object_data);
Expand All @@ -298,8 +299,8 @@ fn convert_tag_element<'a, I, A>(
parent_alignment: Default::default(),
x: offset_position.0,
y: offset_position.1,
width: target_size.0,
height: target_size.1,
width: target_size.width,
height: target_size.height,
},
styles: StyleData::default(),
children: object_data_children,
Expand Down
6 changes: 3 additions & 3 deletions vsml_processer/src/audio.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;
use vsml_common_audio::Audio as VsmlAudio;
use vsml_core::schemas::ObjectProcessor;
use vsml_core::schemas::{ObjectProcessor, RectSize};

pub struct AudioProcessor;

Expand All @@ -15,8 +15,8 @@ impl<I> ObjectProcessor<I, VsmlAudio> for AudioProcessor {
reader.duration() as f64 / reader.spec().sample_rate as f64
}

fn default_image_size(&self, _attributes: &HashMap<String, String>) -> (f32, f32) {
(0.0, 0.0)
fn default_image_size(&self, _attributes: &HashMap<String, String>) -> RectSize {
RectSize::ZERO
}

fn process_image(
Expand Down
6 changes: 3 additions & 3 deletions vsml_processer/src/image.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;
use vsml_common_image::Image as VsmlImage;
use vsml_core::schemas::ObjectProcessor;
use vsml_core::schemas::{ObjectProcessor, RectSize};

pub struct ImageProcessor;

Expand All @@ -13,10 +13,10 @@ impl<A> ObjectProcessor<VsmlImage, A> for ImageProcessor {
f64::INFINITY
}

fn default_image_size(&self, attributes: &HashMap<String, String>) -> (f32, f32) {
fn default_image_size(&self, attributes: &HashMap<String, String>) -> RectSize {
let src_path = attributes.get("src").unwrap();
let image = image::open(src_path).unwrap();
(image.width() as f32, image.height() as f32)
RectSize::new(image.width() as f32, image.height() as f32)
}

fn process_image(
Expand Down

0 comments on commit ebf097f

Please sign in to comment.