From 373d6dfa148b0be3964fc720e150af33138b2ec1 Mon Sep 17 00:00:00 2001 From: liaoxianglian Date: Fri, 19 Sep 2025 11:56:32 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=BC=A7=E7=BA=BF=E5=92=8C?= =?UTF-8?q?=E7=BA=BF=E7=9A=84=E9=A2=9C=E8=89=B2=E9=97=AE=E9=A2=98=EF=BC=88?= =?UTF-8?q?=E6=9C=AA=E6=88=90=E5=8A=9F=E8=A7=A3=E5=86=B3=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/qelmt/arc.rs | 14 +++++++++---- src/qelmt/dynamictext.rs | 7 +++++-- src/qelmt/line.rs | 45 ++++++++++++++++++++++------------------ src/qelmt/mod.rs | 21 +++++++++++++++++-- 4 files changed, 59 insertions(+), 28 deletions(-) diff --git a/src/qelmt/arc.rs b/src/qelmt/arc.rs index b1fe576..e2cb1c6 100644 --- a/src/qelmt/arc.rs +++ b/src/qelmt/arc.rs @@ -1,6 +1,7 @@ use super::{two_dec, ScaleEntity}; use dxf::entities; use dxf::entities::LwPolyline; +use hex_color::HexColor; use simple_xml_builder::XMLElement; #[derive(Debug)] @@ -19,7 +20,13 @@ pub struct Arc { impl From<&entities::Arc> for Arc { fn from(arc: &entities::Arc) -> Self { + Self::from_arc_with_color(arc, HexColor::BLACK) + } +} +impl Arc { + /// 从DXF Arc实体创建Arc,支持自定义颜色 + pub fn from_arc_with_color(arc: &entities::Arc, color: HexColor) -> Self { let is_mirror = arc.normal.z < 0.0; let (start_angle, angle_span) = if is_mirror { @@ -74,11 +81,10 @@ impl From<&entities::Arc> for Arc { //reasons...I'm trying to think if there is a time we might want to turn it on? antialias: false, style: if arc.thickness > 0.1 { - "line-style:normal;line-weight:normal;filling:none;color:black" + format!("line-style:normal;line-weight:normal;filling:none;color:{}", color.display_rgb()) } else { - "line-style:normal;line-weight:thin;filling:none;color:black" - } - .into(), + format!("line-style:normal;line-weight:thin;filling:none;color:{}", color.display_rgb()) + }, } } } diff --git a/src/qelmt/dynamictext.rs b/src/qelmt/dynamictext.rs index 462a157..b69ba34 100644 --- a/src/qelmt/dynamictext.rs +++ b/src/qelmt/dynamictext.rs @@ -81,7 +81,7 @@ impl From<&DynamicText> for XMLElement { } } - let txt_width = if txt.reference_rectangle_width > 2.0 { + let txt_width = if txt.reference_rectangle_width > 2.0 { txt.reference_rectangle_width } else { (graphene_count as f64) * pt_size * 0.75 @@ -106,12 +106,15 @@ impl From<&DynamicText> for XMLElement { // } // }; + // 如果采用计算对齐的方式,可能还要考虑字体宽度的偏差。。 + println!("文本:{}", txt.text); println!("对齐方式:{}, {}", txt.h_alignment, txt.v_alignment); println!("文本宽度: {}", txt_width); println!("文本高度:{}", pt_size); println!("初始位置: x={}, y={}", txt.x, txt.y); println!("旋转角度: {}", txt.rotation); + println!("cjk_char_count: {}", cjk_char_count); // 计算基础位置(不考虑旋转) // txt.x和txt.y现在是对齐点坐标,需要根据对齐方式计算出左上角位置 @@ -128,7 +131,7 @@ impl From<&DynamicText> for XMLElement { // 根据是否包含中文字符调整字体大小相关的偏移量 let cjk_offset:f64 = (cjk_char_count as f64) * pt_size * 0.75; - // 左对齐是不需要偏移的,居中对齐需要偏移,右对齐要偏移 + // 左对齐是不需要中文偏移的,居中对齐需要偏移,右对齐要偏移 let base_x = left_x + 0.5 - (pt_size / 8.0) - 4.05 - match txt.h_alignment { HAlignment::Left => 0.0, diff --git a/src/qelmt/line.rs b/src/qelmt/line.rs index da8e51e..bd4bfdb 100644 --- a/src/qelmt/line.rs +++ b/src/qelmt/line.rs @@ -2,6 +2,7 @@ use super::two_dec; use super::LineEnd; use super::ScaleEntity; use dxf::entities::{self, LwPolyline, Polyline}; +use hex_color::HexColor; use simple_xml_builder::XMLElement; #[derive(Debug)] @@ -25,26 +26,7 @@ pub struct Leader(pub Vec); impl From<&entities::Line> for Line { fn from(line: &entities::Line) -> Self { - Line { - x1: line.p1.x, - y1: -line.p1.y, - length1: 1.5, //why is this statically set at 1.5? - end1: LineEnd::None, - x2: line.p2.x, - y2: -line.p2.y, - length2: 1.5, //why is this statically set at 1.5? - end2: LineEnd::None, - - //in the original code antialias is always set to false...I'm guessing for performance - //reasons...I'm trying to think if there is a time we might want to turn it on? - antialias: false, - style: if line.thickness > 0.5 { - "line-style:normal;line-weight:normal;filling:none;color:black" - } else { - "line-style:normal;line-weight:thin;filling:none;color:black" - } - .into(), - } + Self::from_line_with_color(line, HexColor::BLACK) } } @@ -174,6 +156,29 @@ impl From<&Line> for XMLElement { } impl Line { + /// 从DXF Line实体创建Line,支持自定义颜色 + pub fn from_line_with_color(line: &entities::Line, color: HexColor) -> Self { + Line { + x1: line.p1.x, + y1: -line.p1.y, + length1: 1.5, //why is this statically set at 1.5? + end1: LineEnd::None, + x2: line.p2.x, + y2: -line.p2.y, + length2: 1.5, //why is this statically set at 1.5? + end2: LineEnd::None, + + //in the original code antialias is always set to false...I'm guessing for performance + //reasons...I'm trying to think if there is a time we might want to turn it on? + antialias: false, + style: if line.thickness > 0.5 { + format!("line-style:normal;line-weight:normal;filling:none;color:{}", color.display_rgb()) + } else { + format!("line-style:normal;line-weight:thin;filling:none;color:{}", color.display_rgb()) + }, + } + } + pub fn new(x1: f64, y1: f64, x2: f64, y2: f64, style: String) -> Self { Line { length2: 1.5, diff --git a/src/qelmt/mod.rs b/src/qelmt/mod.rs index 244a79f..46dd709 100644 --- a/src/qelmt/mod.rs +++ b/src/qelmt/mod.rs @@ -642,7 +642,14 @@ impl<'a> ObjectsBuilder<'a> { Ok(Objects::Ellipse(ellipse)) } EntityType::Line(line) => { - let mut line: Line = line.into(); + // 获取颜色 + let color = if let Some(aci_index) = self.ent.common.color.index() { + aci_to_hex_color(aci_index) + } else { + HexColor::from_u32(self.ent.common.color_24_bit as u32) + }; + + let mut line: Line = Line::from_line_with_color(line, color); // 根据line_type_name更新线型样式 line.update_line_style(&update_line_style); @@ -661,7 +668,14 @@ impl<'a> ObjectsBuilder<'a> { Ok(Objects::Line(line)) } EntityType::Arc(arc) => { - let mut arc: Arc = arc.into(); + // 获取颜色 + let color = if let Some(aci_index) = self.ent.common.color.index() { + aci_to_hex_color(aci_index) + } else { + HexColor::from_u32(self.ent.common.color_24_bit as u32) + }; + + let mut arc: Arc = Arc::from_arc_with_color(arc, color); // 根据line_type_name更新线型样式 arc.update_line_style(&update_line_style); @@ -1352,6 +1366,7 @@ impl From for HAlignment { match value { HorizontalTextJustification::Left => HAlignment::Left, HorizontalTextJustification::Center => HAlignment::Center, + HorizontalTextJustification::Middle => HAlignment::Center, HorizontalTextJustification::Right => HAlignment::Right, //TODO: Handling the Aligned Middle and Fit alignments are a bit more complicated @@ -1700,6 +1715,8 @@ fn aci_to_rgb(aci: u8) -> (u8, u8, u8) { 8 => (128, 128, 128), // 深灰色 9 => (192, 192, 192), // 浅灰色 94 => (0, 129, 0), // 深绿色 + 15 => (129, 86, 86), // 深棕色 + // 更多标准颜色可以根据需要添加 _ => (0, 0, 0), // 默认黑色 }