From b6d01b193095cce21f453ca9e48b8882839efc50 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Fri, 14 Jul 2023 11:08:45 +0900 Subject: [PATCH] fix: number type (#641) * fix: number type * fix * fix --- CHANGELOG.md | 4 ++ docx-core/src/reader/attributes/border.rs | 4 +- docx-core/src/reader/attributes/indent.rs | 10 ++-- .../src/reader/attributes/line_spacing.rs | 10 ++-- docx-core/src/reader/drawing.rs | 48 +++++++++---------- docx-core/src/reader/pic.rs | 8 ++-- docx-core/src/reader/run_property.rs | 8 ++-- docx-wasm/package.json | 2 +- 8 files changed, 50 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55bc740..37c0e49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## docx-wasm@0.0.278-rc16 (14. Jul, 2023) + +- Improve read numbering types. + ## docx-wasm@0.0.278-rc15 (23. Jun, 2023) - Make docGrid Optional in sectionProperty diff --git a/docx-core/src/reader/attributes/border.rs b/docx-core/src/reader/attributes/border.rs index 5c8f0e3..b87486c 100644 --- a/docx-core/src/reader/attributes/border.rs +++ b/docx-core/src/reader/attributes/border.rs @@ -23,9 +23,9 @@ pub fn read_border(attrs: &[OwnedAttribute]) -> Result if local_name == "color" { color = a.value.to_owned(); } else if local_name == "sz" { - size = Some(u32::from_str(&a.value)?); + size = Some(f64::from_str(&a.value)? as u32); } else if local_name == "space" { - space = Some(u32::from_str(&a.value)?); + space = Some(f64::from_str(&a.value)? as u32); } else if local_name == "val" { border_type = BorderType::from_str(&a.value)?; } diff --git a/docx-core/src/reader/attributes/indent.rs b/docx-core/src/reader/attributes/indent.rs index 673323d..5ef845b 100644 --- a/docx-core/src/reader/attributes/indent.rs +++ b/docx-core/src/reader/attributes/indent.rs @@ -31,7 +31,7 @@ pub fn read_indent(attrs: &[OwnedAttribute]) -> ReadIndentResult { let v = super::value_to_dax(&a.value)?; start = Some(v); } else if local_name == "leftChars" || local_name == "startChars" { - start_chars = Some(i32::from_str(&a.value)?); + start_chars = Some(f64::from_str(&a.value)? as i32); } else if local_name == "end" || local_name == "right" { let v = super::value_to_dax(&a.value)?; end = Some(v); @@ -42,12 +42,12 @@ pub fn read_indent(attrs: &[OwnedAttribute]) -> ReadIndentResult { let v = super::value_to_dax(&a.value)?; special = Some(SpecialIndentType::FirstLine(v)) } else if local_name == "firstLineChars" { - if let Ok(chars) = i32::from_str(&a.value) { - first_line_chars = Some(chars); + if let Ok(chars) = f64::from_str(&a.value) { + first_line_chars = Some(chars as i32); } } else if local_name == "hangingChars" { - if let Ok(chars) = i32::from_str(&a.value) { - hanging_chars = Some(chars); + if let Ok(chars) = f64::from_str(&a.value) { + hanging_chars = Some(chars as i32); } } } diff --git a/docx-core/src/reader/attributes/line_spacing.rs b/docx-core/src/reader/attributes/line_spacing.rs index 075e313..aa81cd9 100644 --- a/docx-core/src/reader/attributes/line_spacing.rs +++ b/docx-core/src/reader/attributes/line_spacing.rs @@ -10,22 +10,22 @@ pub fn read_line_spacing(attributes: &[OwnedAttribute]) -> Result { - spacing = spacing.before(u32::from_str(&a.value)?); + spacing = spacing.before(f64::from_str(&a.value)? as u32); } "after" => { - spacing = spacing.after(u32::from_str(&a.value)?); + spacing = spacing.after(f64::from_str(&a.value)? as u32); } "line" => { - spacing = spacing.line(u32::from_str(&a.value)?); + spacing = spacing.line(f64::from_str(&a.value)? as u32); } "lineRule" => { spacing = spacing.line_rule(LineSpacingType::from_str(&a.value)?); } "beforeLines" => { - spacing = spacing.before_lines(u32::from_str(&a.value)?); + spacing = spacing.before_lines(f64::from_str(&a.value)? as u32); } "afterLines" => { - spacing = spacing.after_lines(u32::from_str(&a.value)?); + spacing = spacing.after_lines(f64::from_str(&a.value)? as u32); } _ => {} } diff --git a/docx-core/src/reader/drawing.rs b/docx-core/src/reader/drawing.rs index e598fa0..6128ee3 100644 --- a/docx-core/src/reader/drawing.rs +++ b/docx-core/src/reader/drawing.rs @@ -27,8 +27,8 @@ fn read_position_h( let e = r.next(); match e { Ok(XmlEvent::Characters(c)) => { - if let Ok(p) = i32::from_str(&c) { - offset = p; + if let Ok(p) = f64::from_str(&c) { + offset = p as i32; } } Ok(XmlEvent::EndElement { name, .. }) => { @@ -59,8 +59,8 @@ fn read_position_v( let e = r.next(); match e { Ok(XmlEvent::Characters(c)) => { - if let Ok(p) = i32::from_str(&c) { - offset = p; + if let Ok(p) = f64::from_str(&c) { + offset = p as i32; } } Ok(XmlEvent::EndElement { name, .. }) => { @@ -153,23 +153,23 @@ impl ElementReader for Drawing { } } if let Some(d) = read(&attributes, "distT") { - if let Ok(d) = i32::from_str(&d) { - dist_t = d; + if let Ok(d) = f64::from_str(&d) { + dist_t = d as i32; } } if let Some(d) = read(&attributes, "distB") { - if let Ok(d) = i32::from_str(&d) { - dist_b = d; + if let Ok(d) = f64::from_str(&d) { + dist_b = d as i32; } } if let Some(d) = read(&attributes, "distL") { - if let Ok(d) = i32::from_str(&d) { - dist_l = d; + if let Ok(d) = f64::from_str(&d) { + dist_l = d as i32; } } if let Some(d) = read(&attributes, "distR") { - if let Ok(d) = i32::from_str(&d) { - dist_r = d; + if let Ok(d) = f64::from_str(&d) { + dist_r = d as i32; } } if let Some(d) = read(&attributes, "layoutInCell") { @@ -178,8 +178,8 @@ impl ElementReader for Drawing { } } if let Some(d) = read(&attributes, "relativeHeight") { - if let Ok(d) = u32::from_str(&d) { - relative_height = d; + if let Ok(d) = f64::from_str(&d) { + relative_height = d as u32; } } if let Some(d) = read(&attributes, "allowOverlap") { @@ -191,18 +191,18 @@ impl ElementReader for Drawing { WpXMLElement::Inline => { drawing_position_type = DrawingPositionType::Inline; if let Some(d) = read(&attributes, "distT") { - if let Ok(d) = i32::from_str(&d) { - dist_t = d; + if let Ok(d) = f64::from_str(&d) { + dist_t = d as i32; } } if let Some(d) = read(&attributes, "distB") { - if let Ok(d) = i32::from_str(&d) { - dist_b = d; + if let Ok(d) = f64::from_str(&d) { + dist_b = d as i32; } } if let Some(d) = read(&attributes, "distL") { - if let Ok(d) = i32::from_str(&d) { - dist_l = d; + if let Ok(d) = f64::from_str(&d) { + dist_l = d as i32; } } if let Some(d) = read(&attributes, "distR") { @@ -213,13 +213,13 @@ impl ElementReader for Drawing { } WpXMLElement::SimplePos => { if let Some(x) = read(&attributes, "x") { - if let Ok(x) = i32::from_str(&x) { - simple_pos_x = x; + if let Ok(x) = f64::from_str(&x) { + simple_pos_x = x as i32; } } if let Some(y) = read(&attributes, "y") { - if let Ok(y) = i32::from_str(&y) { - simple_pos_y = y; + if let Ok(y) = f64::from_str(&y) { + simple_pos_y = y as i32; } } } diff --git a/docx-core/src/reader/pic.rs b/docx-core/src/reader/pic.rs index 575aff8..ad1bcd8 100644 --- a/docx-core/src/reader/pic.rs +++ b/docx-core/src/reader/pic.rs @@ -31,13 +31,13 @@ impl ElementReader for Pic { let mut offset_x: i32 = 0; let mut offset_y: i32 = 0; if let Some(x) = read(&attributes, "x") { - if let Ok(x) = i32::from_str(&x) { - offset_x = x; + if let Ok(x) = f64::from_str(&x) { + offset_x = x as i32; } } if let Some(y) = read(&attributes, "y") { - if let Ok(y) = i32::from_str(&y) { - offset_y = y; + if let Ok(y) = f64::from_str(&y) { + offset_y = y as i32; } } pic = pic.offset_x(offset_x).offset_y(offset_y); diff --git a/docx-core/src/reader/run_property.rs b/docx-core/src/reader/run_property.rs index b438405..5d8bda3 100644 --- a/docx-core/src/reader/run_property.rs +++ b/docx-core/src/reader/run_property.rs @@ -86,11 +86,13 @@ impl ElementReader for RunProperty { } } XMLElement::Color => rp = rp.color(attributes[0].value.clone()), - XMLElement::Size => rp = rp.size(usize::from_str(&attributes[0].value)?), + XMLElement::Size => { + rp = rp.size(f64::from_str(&attributes[0].value)? as usize) + } XMLElement::Spacing => { if let Some(v) = read_val(&attributes) { - if let Ok(s) = i32::from_str(&v) { - rp = rp.spacing(s) + if let Ok(s) = f64::from_str(&v) { + rp = rp.spacing(s as i32) } } } diff --git a/docx-wasm/package.json b/docx-wasm/package.json index ea02958..13356d8 100644 --- a/docx-wasm/package.json +++ b/docx-wasm/package.json @@ -1,6 +1,6 @@ { "name": "docx-wasm", - "version": "0.0.278-rc15", + "version": "0.0.278-rc16", "main": "dist/node/index.js", "browser": "dist/web/index.js", "author": "bokuweb ",