diff --git a/src/main.rs b/src/main.rs index 7a38eca..c102bc7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -108,9 +108,11 @@ fn main() -> Result<()> { .file_stem() .unwrap_or_else(|| file_name.as_os_str()) .to_string_lossy(); + // 加载dxf文件,获取dxf解析对象drawing let drawing: Drawing = Drawing::load_file(&file_name).context(format!( "Failed to load {friendly_file_name}...\n\tMake sure the file is a valid .dxf file.", ))?; + // 传入文件名、步长、dxf解析对象drawing,创建elmt的definition标签 let q_elmt = Definition::new(friendly_file_name.clone(), args.spline_step, &drawing); if !args.verbose && args.info { println!("{friendly_file_name} loaded..."); diff --git a/src/qelmt/dynamictext.rs b/src/qelmt/dynamictext.rs index b69ba34..3cd8b2b 100644 --- a/src/qelmt/dynamictext.rs +++ b/src/qelmt/dynamictext.rs @@ -32,6 +32,8 @@ pub struct DynamicText { keep_visual_rotation: bool, color: HexColor, reference_rectangle_width: f64, + pub align_x: f64, + pub align_y: f64, } impl From<&DynamicText> for XMLElement { @@ -61,30 +63,64 @@ impl From<&DynamicText> for XMLElement { // o.k. ... as long as we do not know the real width: // "guess" the width by number of characters and font-size: // - // let graphene_count = txt.text.graphemes(true).count(); + let graphene_count = txt.text.graphemes(true).count(); let mut cjk_char_count = 0; - let mut graphene_count = 0; - for grapheme in txt.text.graphemes(true) { - graphene_count += 1; - if grapheme.chars().any(|c| { - // 检查是否为中文字符(包括中日韩统一表意文字) - (c >= '\u{4E00}' && c <= '\u{9FFF}') || // CJK统一表意文字 - (c >= '\u{3400}' && c <= '\u{4DBF}') || // CJK扩展A - (c >= '\u{20000}' && c <= '\u{2A6DF}') || // CJK扩展B - (c >= '\u{2A700}' && c <= '\u{2B73F}') || // CJK扩展C - (c >= '\u{2B740}' && c <= '\u{2B81F}') || // CJK扩展D - (c >= '\u{2B820}' && c <= '\u{2CEAF}') || // CJK扩展E - (c >= '\u{F900}' && c <= '\u{FAFF}') || // CJK兼容表意文字 - (c >= '\u{2F800}' && c <= '\u{2FA1F}') // CJK兼容表意文字补充 - }) { - cjk_char_count += 1; - } - } + // let mut graphene_count = 0; + // for grapheme in txt.text.graphemes(true) { + // graphene_count += 1; + // if grapheme.chars().any(|c| { + // // 检查是否为中文字符(包括中日韩统一表意文字) + // (c >= '\u{4E00}' && c <= '\u{9FFF}') || // CJK统一表意文字 + // (c >= '\u{3400}' && c <= '\u{4DBF}') || // CJK扩展A + // (c >= '\u{20000}' && c <= '\u{2A6DF}') || // CJK扩展B + // (c >= '\u{2A700}' && c <= '\u{2B73F}') || // CJK扩展C + // (c >= '\u{2B740}' && c <= '\u{2B81F}') || // CJK扩展D + // (c >= '\u{2B820}' && c <= '\u{2CEAF}') || // CJK扩展E + // (c >= '\u{F900}' && c <= '\u{FAFF}') || // CJK兼容表意文字 + // (c >= '\u{2F800}' && c <= '\u{2FA1F}') // CJK兼容表意文字补充 + // }) { + // cjk_char_count += 1; + // } + // } let txt_width = if txt.reference_rectangle_width > 2.0 { txt.reference_rectangle_width } else { + // 不同的字母,宽度与高度的比例并不相同,0.75只是一个平均值 (graphene_count as f64) * pt_size * 0.75 + // 根据字符类型计算宽度,考虑不同字符的宽高比例 + // let mut total_width = 0.0; + // for grapheme in txt.text.graphemes(true) { + // let char_width = if grapheme.chars().any(|c| { + // // 检查是否为中文字符(包括中日韩统一表意文字) + // (c >= '\u{4E00}' && c <= '\u{9FFF}') || // CJK统一表意文字 + // (c >= '\u{3400}' && c <= '\u{4DBF}') || // CJK扩展A + // (c >= '\u{20000}' && c <= '\u{2A6DF}') || // CJK扩展B + // (c >= '\u{2A700}' && c <= '\u{2B73F}') || // CJK扩展C + // (c >= '\u{2B740}' && c <= '\u{2B81F}') || // CJK扩展D + // (c >= '\u{2B820}' && c <= '\u{2CEAF}') || // CJK扩展E + // (c >= '\u{F900}' && c <= '\u{FAFF}') || // CJK兼容表意文字 + // (c >= '\u{2F800}' && c <= '\u{2FA1F}') // CJK兼容表意文字补充 + // }) { + // // 中文字符:宽高比 1:1 + // cjk_char_count += 1; + // pt_size * 1.5 + // } else if grapheme.chars().any(|c| matches!(c, 'i' | 'l' | 'I' | ',' | ';' | ':' | '!')) { + // pt_size * 0.3 + // } else if grapheme.chars().any(|c| matches!(c, 'W' | 'M' | '@' )) { + // pt_size * 1.0 + // } else if grapheme.chars().any(|c| matches!(c, 'w' |'m' | '%' )) { + // pt_size * 0.9 + // }else if grapheme.chars().any(|c| matches!(c, 'f' |'j' | 't' | 'r' | 'J' | 'T' | 'F' | '/' | '\\' | 's' | 'z' )) { + // // 其他标点符号:宽高比约 0.4:1 + // pt_size * 0.5 + // } else { + // // 中等宽度字母(默认):宽高比约 0.6:1 + // pt_size * 0.7 + // }; + // total_width += char_width; + // } + // total_width }; // let x_pos = { @@ -112,39 +148,36 @@ impl From<&DynamicText> for XMLElement { println!("对齐方式:{}, {}", txt.h_alignment, txt.v_alignment); println!("文本宽度: {}", txt_width); println!("文本高度:{}", pt_size); - println!("初始位置: x={}, y={}", txt.x, txt.y); + println!("初始位置: x={}, y={}", txt.align_x, txt.align_y); println!("旋转角度: {}", txt.rotation); - println!("cjk_char_count: {}", cjk_char_count); + // println!("cjk_char_count: {}", cjk_char_count); // 计算基础位置(不考虑旋转) // txt.x和txt.y现在是对齐点坐标,需要根据对齐方式计算出左上角位置 // 首先根据水平对齐方式计算左边界的x坐标 - let left_x = match txt.h_alignment { - HAlignment::Left => txt.x, - HAlignment::Center => txt.x - txt_width / 2.0, - HAlignment::Right => txt.x - txt_width, - }; + let left_x = txt.x; // 根据垂直对齐方式计算顶部的y坐标 - let top_y = txt.y; + let top_y = match txt.v_alignment { + VAlignment::Top => txt.y + pt_size / 2.0, + VAlignment::Center => txt.y - pt_size / 2.0, + VAlignment::Bottom => txt.y - pt_size + pt_size / 2.0, + }; + + println!("左上角位置: x={}, y={}", left_x, top_y); // 根据是否包含中文字符调整字体大小相关的偏移量 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, - HAlignment::Center => cjk_offset / 2.0, - HAlignment::Right => cjk_offset, - }; + let base_x = left_x + 0.5 - (pt_size / 8.0) - 4.05; + // - match txt.h_alignment { + // HAlignment::Left => 0.0, + // HAlignment::Center => cjk_offset / 2.0, + // HAlignment::Right => cjk_offset, + // }; - let base_y = top_y + 0.5 - (7.0 / 5.0 * pt_size + 26.0 / 5.0) - + match txt.v_alignment { - VAlignment::Top => pt_size / 2.0, - VAlignment::Center => pt_size, - VAlignment::Bottom => pt_size / 2.0, - }; + let base_y = top_y + 0.5 - (7.0 / 5.0 * pt_size + 26.0 / 5.0) + pt_size; // 如果有旋转角度,应用旋转变换 let (x_pos, y_pos) = if txt.rotation != 0.0 { @@ -280,16 +313,19 @@ impl<'a> DTextBuilder<'a> { h_alignment, v_alignment, reference_rectangle_width, + align_x, + align_y, + ) = match self.text { TextEntity::Text(txt) => { - let (x, y) = if txt.horizontal_text_justification == HorizontalTextJustification::Left && (txt.vertical_text_justification == VerticalTextJustification::Bottom || txt.vertical_text_justification == VerticalTextJustification::Baseline) { - (txt.location.x, -txt.location.y) - } else { - (txt.second_alignment_point.x, -txt.second_alignment_point.y) - }; + // let (x, y) = if txt.horizontal_text_justification == HorizontalTextJustification::Left && (txt.vertical_text_justification == VerticalTextJustification::Bottom || txt.vertical_text_justification == VerticalTextJustification::Baseline) { + // (txt.location.x, -txt.location.y) + // } else { + // (txt.second_alignment_point.x, -txt.second_alignment_point.y) + // }; ( - x, - y, + txt.location.x, + -txt.location.y, txt.location.z, txt.rotation, &txt.text_style_name, @@ -298,6 +334,8 @@ impl<'a> DTextBuilder<'a> { HAlignment::from(txt.horizontal_text_justification), VAlignment::from(txt.vertical_text_justification), 0.0, // as Placeholder: no "reference_rectangle_width" with Text!!! + txt.second_alignment_point.x, + -txt.second_alignment_point.y )}, TextEntity::MText(mtxt) => { // 计算实际的旋转角度,优先使用x_axis_direction向量 @@ -336,19 +374,21 @@ impl<'a> DTextBuilder<'a> { HAlignment::from(mtxt.attachment_point), VAlignment::from(mtxt.attachment_point), mtxt.reference_rectangle_width, + mtxt.insertion_point.x, + -mtxt.insertion_point.y, )}, TextEntity::Attrib(attrib) => { // 根据DXF文本对齐规则,当水平对齐不是Left时,使用second_alignment_point - let (x, y) = if attrib.horizontal_text_justification == HorizontalTextJustification::Left && (attrib.vertical_text_justification == VerticalTextJustification::Bottom || attrib.vertical_text_justification == VerticalTextJustification::Baseline) { - println!("{}",111); - (attrib.location.x, -attrib.location.y) - } else { - println!("{}",222); - (attrib.second_alignment_point.x, -attrib.second_alignment_point.y) - }; + // let (x, y) = if attrib.horizontal_text_justification == HorizontalTextJustification::Left && (attrib.vertical_text_justification == VerticalTextJustification::Bottom || attrib.vertical_text_justification == VerticalTextJustification::Baseline) { + // println!("{}",111); + // (attrib.location.x, -attrib.location.y) + // } else { + // println!("{}",222); + // (attrib.second_alignment_point.x, -attrib.second_alignment_point.y) + // }; ( - x, - y, + attrib.location.x, + -attrib.location.y, attrib.location.z, attrib.rotation, &attrib.text_style_name, @@ -357,6 +397,8 @@ impl<'a> DTextBuilder<'a> { HAlignment::from(attrib.horizontal_text_justification), VAlignment::from(attrib.vertical_text_justification), 0.0, // as Placeholder: not need to check if Attrib has something similar + attrib.second_alignment_point.x, + -attrib.second_alignment_point.y ) }, }; @@ -427,6 +469,9 @@ impl<'a> DTextBuilder<'a> { text: value, keep_visual_rotation: false, info_name: None, + align_x, + align_y, + } } } diff --git a/src/qelmt/mod.rs b/src/qelmt/mod.rs index 8daf129..c07871c 100644 --- a/src/qelmt/mod.rs +++ b/src/qelmt/mod.rs @@ -201,7 +201,7 @@ impl Circularity for LwPolyline { } // 实现Definition的方法 -// 最后返回文本的definition标签了 +// 最后返回文本的definition标签 impl Definition { // 创建新的 Definition 实例,对应elmt的标签 传入参数:文件名, 步长, dxf图纸 pub fn new(name: impl Into, spline_step: u32, drw: &Drawing) -> Self { @@ -217,7 +217,7 @@ impl Definition { description }; - // 计算宽度,x热点坐标 + // 计算图纸的宽度,x热点坐标 //The below calculation for width and hotspot_x are taken from the qet source code let (width, hotspot_x) = { let tmp_width = description.right_bound() - description.left_bound(); @@ -237,7 +237,7 @@ impl Definition { ) }; - // 计算高度,y热点坐标 + // 计算图纸的高度,y热点坐标 //The below calculation for height and hotspot_y are taken from the qet source code let (height, hotspot_y) = { let tmp_height = description.bot_bound() - description.top_bound(); @@ -257,7 +257,7 @@ impl Definition { ) }; - // 结构体初始化 :设置所有必要字段,包括类型、版本、UUID等 + // Definition结构体初始化 :设置所有必要字段,包括类型、版本、UUID等 Definition { r#type: ItemType::Element, width, @@ -614,6 +614,7 @@ impl<'a> ObjectsBuilder<'a> { println!("line_type_name:{line_type_name}"); // 只有当line_type_name需要虚线样式时才进行修改,提高性能 if line_type_name.contains("DASH") || line_type_name == "BORDURE" || line_type_name == "INTERROMPU" || line_type_name == "INTERROMPUx2" || line_type_name == "TRACECAD_ISO02W100" { + // 使用正则表达式替换line-style部分 if style.contains("line-style:") { *style = style.replace( @@ -663,9 +664,7 @@ impl<'a> ObjectsBuilder<'a> { line.x2 += self.offset.x; line.y2 -= self.offset.y; - - println!("Line: x1: {}, y1: {}, x2: {}, y2: {}", line.x1, line.y1, line.x2, line.y2); - + Ok(Objects::Line(line)) } @@ -1692,6 +1691,7 @@ impl Default for FontInfo { //but I'll put something in for now Self { family: "宋体".into(), + // 字号12? point_size: 12.0, pixel_size: i32::default(), style_hint: FontStyleHint::Helvetica,