diff --git a/src/qelmt/dynamictext.rs b/src/qelmt/dynamictext.rs index c186e6f..8253799 100644 --- a/src/qelmt/dynamictext.rs +++ b/src/qelmt/dynamictext.rs @@ -169,6 +169,8 @@ impl ScaleEntity for DynamicText { self.y *= fact_y; self.align_x *= fact_x; self.align_y *= fact_y; + self.text_width *= fact_x.abs(); + self.text_height *= fact_y.abs(); self.font.scale(fact_y); } @@ -177,8 +179,7 @@ impl ScaleEntity for DynamicText { } fn right_bound(&self) -> f64 { - //todo!() - 1.0 + self.x + self.text_width } fn top_bound(&self) -> f64 { @@ -186,8 +187,7 @@ impl ScaleEntity for DynamicText { } fn bot_bound(&self) -> f64 { - //todo!() - 1.0 + self.y - self.text_height } } @@ -358,8 +358,14 @@ impl<'a> DTextBuilder<'a> { dbg!(&y); dbg!(&self.text);*/ let value = super::strip_mtext_control_sequences(&value); - // 将 DXF 中读取到的 text_height 放大 2 倍(用户要求) + // 将 DXF 中读取到的 text_height 放大 2 倍(用户要求) let text_height = text_height * 2.0; + let text_width = if reference_rectangle_width > 0.0 { + reference_rectangle_width + } else { + estimate_text_width(&value, text_height) + }; + DynamicText { //x: x - (calc_width as f64/2.0), x, @@ -387,7 +393,7 @@ impl<'a> DTextBuilder<'a> { text_from: "UserText".into(), frame: false, text_height, - text_width: -1.0, // for now, until I figure out what this should be + text_width, color: self.color.unwrap_or(HexColor::BLACK), text: value, @@ -482,3 +488,21 @@ pub fn adjust_mtext_coordinates( (adjusted_x, adjusted_y) } + +fn estimate_text_width(text: &str, line_height: f64) -> f64 { + let effective_height = line_height.abs(); + if effective_height == 0.0 { + return 0.0; + } + + let max_graphemes = text + .lines() + .map(|line| line.graphemes(true).count() as f64) + .fold(0.0, f64::max); + + if max_graphemes > 0.0 { + max_graphemes * effective_height * 0.75 + } else { + effective_height + } +}