fix: document error (#188)
parent
bd1811581f
commit
bfd0deb127
|
@ -4,7 +4,7 @@ use std::fs::File;
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let mut file = File::open("./style_test.docx").unwrap();
|
let mut file = File::open("./issue2405.docx").unwrap();
|
||||||
let mut buf = vec![];
|
let mut buf = vec![];
|
||||||
file.read_to_end(&mut buf).unwrap();
|
file.read_to_end(&mut buf).unwrap();
|
||||||
dbg!(read_docx(&buf).unwrap().json());
|
dbg!(read_docx(&buf).unwrap().json());
|
||||||
|
|
|
@ -21,21 +21,23 @@ pub fn read_indent(attrs: &[OwnedAttribute]) -> ReadIndentResult {
|
||||||
let mut start_chars: Option<i32> = None;
|
let mut start_chars: Option<i32> = None;
|
||||||
let mut end: Option<i32> = None;
|
let mut end: Option<i32> = None;
|
||||||
let mut special: Option<SpecialIndentType> = None;
|
let mut special: Option<SpecialIndentType> = None;
|
||||||
|
|
||||||
for a in attrs {
|
for a in attrs {
|
||||||
let local_name = &a.name.local_name;
|
let local_name = &a.name.local_name;
|
||||||
if local_name == "left" || local_name == "start" {
|
if local_name == "left" || local_name == "start" {
|
||||||
start = Some(f64::from_str(&a.value)? as i32);
|
let v = super::value_to_dax(&a.value)?;
|
||||||
|
start = Some(v);
|
||||||
} else if local_name == "leftChars" || local_name == "startChars" {
|
} else if local_name == "leftChars" || local_name == "startChars" {
|
||||||
start_chars = Some(i32::from_str(&a.value)?);
|
start_chars = Some(i32::from_str(&a.value)?);
|
||||||
} else if local_name == "end" || local_name == "right" {
|
} else if local_name == "end" || local_name == "right" {
|
||||||
end = Some(f64::from_str(&a.value)? as i32);
|
let v = super::value_to_dax(&a.value)?;
|
||||||
|
end = Some(v);
|
||||||
} else if local_name == "hanging" {
|
} else if local_name == "hanging" {
|
||||||
special = Some(SpecialIndentType::Hanging(f64::from_str(&a.value)? as i32))
|
let v = super::value_to_dax(&a.value)?;
|
||||||
|
special = Some(SpecialIndentType::Hanging(v))
|
||||||
} else if local_name == "firstLine" {
|
} else if local_name == "firstLine" {
|
||||||
special = Some(SpecialIndentType::FirstLine(f64::from_str(&a.value)? as i32))
|
let v = super::value_to_dax(&a.value)?;
|
||||||
|
special = Some(SpecialIndentType::FirstLine(v))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((start, end, special, start_chars))
|
Ok((start, end, special, start_chars))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
use xml::attribute::OwnedAttribute;
|
use xml::attribute::OwnedAttribute;
|
||||||
|
|
||||||
|
use super::super::errors::*;
|
||||||
|
|
||||||
pub fn read_val(attrs: &[OwnedAttribute]) -> Option<String> {
|
pub fn read_val(attrs: &[OwnedAttribute]) -> Option<String> {
|
||||||
for a in attrs {
|
for a in attrs {
|
||||||
let local_name = &a.name.local_name;
|
let local_name = &a.name.local_name;
|
||||||
|
@ -9,3 +13,12 @@ pub fn read_val(attrs: &[OwnedAttribute]) -> Option<String> {
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn value_to_dax(v: &str) -> Result<i32, ReaderError> {
|
||||||
|
if v.ends_with("pt") {
|
||||||
|
let v = f64::from_str(&v.replace("pt", ""))? as i32;
|
||||||
|
Ok(v * 20)
|
||||||
|
} else {
|
||||||
|
Ok(f64::from_str(v)? as i32)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -76,14 +76,20 @@ pub fn read_docx(buf: &[u8]) -> Result<Docx, ReaderError> {
|
||||||
// (physically located at /document.xml in the package):
|
// (physically located at /document.xml in the package):
|
||||||
let main_rel = rels
|
let main_rel = rels
|
||||||
.find_target(DOC_RELATIONSHIP_TYPE)
|
.find_target(DOC_RELATIONSHIP_TYPE)
|
||||||
.ok_or(ReaderError::DocumentNotFoundError)?;
|
.ok_or(ReaderError::DocumentNotFoundError);
|
||||||
|
|
||||||
|
let document_path = if let Ok(rel) = main_rel {
|
||||||
|
rel.2.clone()
|
||||||
|
} else {
|
||||||
|
"word/document.xml".to_owned()
|
||||||
|
};
|
||||||
let document = {
|
let document = {
|
||||||
let data = read_zip(&mut archive, &main_rel.2)?;
|
let data = read_zip(&mut archive, &document_path)?;
|
||||||
Document::from_xml(&data[..])?
|
Document::from_xml(&data[..])?
|
||||||
};
|
};
|
||||||
let mut docx = Docx::new().document(document);
|
let mut docx = Docx::new().document(document);
|
||||||
// Read document relationships
|
// Read document relationships
|
||||||
let rels = read_document_rels(&mut archive, &main_rel.2)?;
|
let rels = read_document_rels(&mut archive, &document_path)?;
|
||||||
|
|
||||||
// Read styles
|
// Read styles
|
||||||
let style_path = rels.find_target_path(STYLE_RELATIONSHIP_TYPE);
|
let style_path = rels.find_target_path(STYLE_RELATIONSHIP_TYPE);
|
||||||
|
|
|
@ -13,7 +13,7 @@ fn read_lineheight(attributes: &[OwnedAttribute]) -> Option<u32> {
|
||||||
for a in attributes {
|
for a in attributes {
|
||||||
let local_name = &a.name.local_name;
|
let local_name = &a.name.local_name;
|
||||||
if let "line" = local_name.as_str() {
|
if let "line" = local_name.as_str() {
|
||||||
return f32::from_str(&a.value).ok().map(|l| l as u32);
|
return value_to_dax(&a.value).ok().map(|l| l as u32);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
|
|
|
@ -31,7 +31,8 @@ impl ElementReader for RunProperty {
|
||||||
XMLElement::Size => rp = rp.size(usize::from_str(&attributes[0].value)?),
|
XMLElement::Size => rp = rp.size(usize::from_str(&attributes[0].value)?),
|
||||||
XMLElement::Spacing => {
|
XMLElement::Spacing => {
|
||||||
if let Some(v) = read_val(&attributes) {
|
if let Some(v) = read_val(&attributes) {
|
||||||
rp = rp.spacing(f32::from_str(&v)? as i32)
|
let v = value_to_dax(&v)?;
|
||||||
|
rp = rp.spacing(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
XMLElement::Underline => rp = rp.underline(&attributes[0].value.clone()),
|
XMLElement::Underline => rp = rp.underline(&attributes[0].value.clone()),
|
||||||
|
|
|
@ -12,10 +12,10 @@ fn read_page_size(attributes: &[OwnedAttribute]) -> Result<PageSize, ReaderError
|
||||||
let local_name = &a.name.local_name;
|
let local_name = &a.name.local_name;
|
||||||
match local_name.as_str() {
|
match local_name.as_str() {
|
||||||
"w" => {
|
"w" => {
|
||||||
size = size.width(f32::from_str(&a.value)? as u32);
|
size = size.width(value_to_dax(&a.value)? as u32);
|
||||||
}
|
}
|
||||||
"h" => {
|
"h" => {
|
||||||
size = size.height(f32::from_str(&a.value)? as u32);
|
size = size.height(value_to_dax(&a.value)? as u32);
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
@ -31,25 +31,25 @@ fn read_page_margin(
|
||||||
let local_name = &a.name.local_name;
|
let local_name = &a.name.local_name;
|
||||||
match local_name.as_str() {
|
match local_name.as_str() {
|
||||||
"top" => {
|
"top" => {
|
||||||
margin = margin.top(f32::from_str(&a.value)? as u32);
|
margin = margin.top(value_to_dax(&a.value)? as u32);
|
||||||
}
|
}
|
||||||
"right" => {
|
"right" => {
|
||||||
margin = margin.right(f32::from_str(&a.value)? as u32);
|
margin = margin.right(value_to_dax(&a.value)? as u32);
|
||||||
}
|
}
|
||||||
"bottom" => {
|
"bottom" => {
|
||||||
margin = margin.bottom(f32::from_str(&a.value)? as u32);
|
margin = margin.bottom(value_to_dax(&a.value)? as u32);
|
||||||
}
|
}
|
||||||
"left" => {
|
"left" => {
|
||||||
margin = margin.left(f32::from_str(&a.value)? as u32);
|
margin = margin.left(value_to_dax(&a.value)? as u32);
|
||||||
}
|
}
|
||||||
"header" => {
|
"header" => {
|
||||||
margin = margin.header(f32::from_str(&a.value)? as u32);
|
margin = margin.header(value_to_dax(&a.value)? as u32);
|
||||||
}
|
}
|
||||||
"footer" => {
|
"footer" => {
|
||||||
margin = margin.footer(f32::from_str(&a.value)? as u32);
|
margin = margin.footer(value_to_dax(&a.value)? as u32);
|
||||||
}
|
}
|
||||||
"gutter" => {
|
"gutter" => {
|
||||||
margin = margin.gutter(f32::from_str(&a.value)? as u32);
|
margin = margin.gutter(value_to_dax(&a.value)? as u32);
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,10 @@ impl ElementReader for TableRow {
|
||||||
}
|
}
|
||||||
XMLElement::TableRowHeight => {
|
XMLElement::TableRowHeight => {
|
||||||
if let Some(v) = read_val(&attributes) {
|
if let Some(v) = read_val(&attributes) {
|
||||||
row_height = Some(f32::from_str(&v)?);
|
let h = f32::from_str(&v);
|
||||||
|
if let Ok(h) = h {
|
||||||
|
row_height = Some(h);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
Loading…
Reference in New Issue