fix widow_control xml, add character_spacing_control (#633)

* fix widow_control xml

* add character_spacing_control

* snapshot
fix - character_spacing_values, snack to camelCase

* wasm snapshot

* snapshot

---------

Co-authored-by: gwq <guowanqi@tianchuangsec.com>
main
Griklit 2023-06-22 23:20:27 +08:00 committed by GitHub
parent 45b58dc3b7
commit 2897f0823c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
44 changed files with 137 additions and 48 deletions

View File

@ -176,9 +176,7 @@ fn inner_build(p: &ParagraphProperty) -> Vec<u8> {
}
if let Some(v) = p.widow_control {
if v {
b = b.widow_control()
}
b = b.widow_control(if v { "1" } else { "0" })
}
if !p.tabs.is_empty() {
@ -206,7 +204,6 @@ impl BuildXML for Box<ParagraphProperty> {
#[cfg(test)]
mod tests {
use super::*;
use crate::types::LineSpacingType;
#[cfg(test)]

View File

@ -1,6 +1,7 @@
use super::*;
use crate::documents::BuildXML;
use crate::types::{CharacterSpacingValues};
use crate::xml_builder::*;
use serde::Serialize;
@ -14,6 +15,7 @@ pub struct Settings {
doc_vars: Vec<DocVar>,
even_and_odd_headers: bool,
adjust_line_height_in_table: bool,
character_spacing_control: Option<CharacterSpacingValues>,
}
impl Settings {
@ -45,6 +47,11 @@ impl Settings {
self.adjust_line_height_in_table = true;
self
}
pub fn character_spacing_control(mut self, val: CharacterSpacingValues) -> Self {
self.character_spacing_control = Some(val);
self
}
}
impl Default for Settings {
@ -56,6 +63,7 @@ impl Default for Settings {
doc_vars: vec![],
even_and_odd_headers: false,
adjust_line_height_in_table: false,
character_spacing_control: None,
}
}
}
@ -75,6 +83,10 @@ impl BuildXML for Settings {
.ul_trail_space()
.do_not_expand_shift_return();
if let Some(v) = self.character_spacing_control {
b = b.character_spacing_control(&v.to_string());
}
if self.adjust_line_height_in_table {
b = b.adjust_line_height_table();
}
@ -131,7 +143,6 @@ impl BuildXML for Settings {
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;

View File

@ -1,10 +1,11 @@
use std::io::Read;
use xml::reader::{EventReader, XmlEvent};
use std::str::FromStr;
use super::*;
use crate::reader::{FromXML, ReaderError};
use crate::types::CharacterSpacingValues;
use std::str::FromStr;
impl FromXML for Settings {
fn from_xml<R: Read>(reader: R) -> Result<Self, ReaderError> {
@ -15,8 +16,8 @@ impl FromXML for Settings {
let e = parser.next();
match e {
Ok(XmlEvent::StartElement {
attributes, name, ..
}) => {
attributes, name, ..
}) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
match e {
XMLElement::DocId => {
@ -52,6 +53,13 @@ impl FromXML for Settings {
XMLElement::AdjustLineHeightInTable => {
settings = settings.adjust_line_height_in_table();
}
XMLElement::CharacterSpacingControl => {
let val = read_val(&attributes);
if let Some(val) = val {
settings = settings.character_spacing_control(
CharacterSpacingValues::from_str(&val).unwrap());
}
}
_ => {}
}
}

View File

@ -145,6 +145,7 @@ pub enum XMLElement {
DefaultTabStop,
RunPropertyDefault,
AdjustLineHeightInTable,
CharacterSpacingControl,
SectionProperty,
PageSize,
PageMargin,
@ -211,6 +212,7 @@ pub enum WpsXMLElement {
BodyPr,
Unsupported,
}
#[derive(PartialEq, Debug)]
pub enum VXMLElement {
Rect,
@ -363,6 +365,7 @@ impl FromStr for XMLElement {
"docGrid" => Ok(XMLElement::DocGrid),
"rPrDefault" => Ok(XMLElement::RunPropertyDefault),
"adjustLineHeightInTable" => Ok(XMLElement::AdjustLineHeightInTable),
"characterSpacingControl" => Ok(XMLElement::CharacterSpacingControl),
"defaultTabStop" => Ok(XMLElement::DefaultTabStop),
"divId" => Ok(XMLElement::DivId),
"div" => Ok(XMLElement::Div),
@ -495,6 +498,6 @@ impl FromStr for PicXMLElement {
pub trait ElementReader {
fn read<R: Read>(r: &mut EventReader<R>, attrs: &[OwnedAttribute]) -> Result<Self, ReaderError>
where
Self: std::marker::Sized;
where
Self: std::marker::Sized;
}

View File

@ -0,0 +1,39 @@
use std::fmt;
use std::str::FromStr;
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;
use serde::{Serialize, Deserialize};
use super::errors;
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize)]
pub enum CharacterSpacingValues {
DoNotCompress,
CompressPunctuation,
CompressPunctuationAndJapaneseKana,
Unsupported,
}
impl fmt::Display for CharacterSpacingValues {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
CharacterSpacingValues::DoNotCompress => write!(f, "doNotCompress"),
CharacterSpacingValues::CompressPunctuation => write!(f, "compressPunctuation"),
CharacterSpacingValues::CompressPunctuationAndJapaneseKana => write!(f, "compressPunctuationAndJapaneseKana"),
_ => write!(f, "unsupported"),
}
}
}
impl FromStr for CharacterSpacingValues {
type Err = errors::TypeError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"doNotCompress" => Ok(CharacterSpacingValues::DoNotCompress),
"compressPunctuation" => Ok(CharacterSpacingValues::CompressPunctuation),
"compressPunctuationAndJapaneseKana" => Ok(CharacterSpacingValues::CompressPunctuationAndJapaneseKana),
_ => Err(errors::TypeError::Unsupported(s.to_string()))
}
}
}

View File

@ -28,6 +28,7 @@ pub mod vert_align_type;
pub mod vertical_align_type;
pub mod vertical_merge_type;
pub mod width_type;
pub mod character_spacing_values;
pub use alignment_type::*;
pub use border_position::*;
@ -59,3 +60,4 @@ pub use vert_align_type::*;
pub use vertical_align_type::*;
pub use vertical_merge_type::*;
pub use width_type::*;
pub use character_spacing_values::*;

View File

@ -438,6 +438,7 @@ impl XMLBuilder {
closed!(ul_trail_space, "w:ulTrailSpace");
closed!(do_not_expand_shift_return, "w:doNotExpandShiftReturn");
closed!(adjust_line_height_table, "w:adjustLineHeightInTable");
closed!(character_spacing_control,"w:characterSpacingControl","w:val");
closed!(use_fe_layout, "w:useFELayout");
closed!(
compat_setting,
@ -450,7 +451,7 @@ impl XMLBuilder {
closed!(keep_next, "w:keepNext");
closed!(keep_lines, "w:keepLines");
closed!(page_break_before, "w:pageBreakBefore");
closed!(widow_control, "w:widowControl");
closed!(widow_control, "w:widowControl", "w:val");
/*
<w:lvlOverride w:ilvl="0">
@ -593,7 +594,6 @@ impl XMLBuilder {
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -581,6 +581,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "FB0AE6E2-8FB8-3345-A8FC-78CE6F3ABE4E",
"docVars": Array [],
@ -1445,6 +1446,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": null,
"defaultTabStop": 840,
"docId": null,
"docVars": Array [],
@ -2688,6 +2690,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "3A5F0B6E-9417-4662-A075-C7869185C909",
"docVars": Array [],
@ -4233,6 +4236,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "A1898E6C-1AED-3C4D-9CCF-C24208DA732E",
"docVars": Array [],
@ -5541,6 +5545,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": null,
"defaultTabStop": 709,
"docId": "50d61cff-8055-4197-917b-3993d0243c46",
"docVars": Array [],
@ -5905,6 +5910,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "4A9883C0-7AEC-574E-926D-DA6DB000DD90",
"docVars": Array [],
@ -7228,6 +7234,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "60C41423-192A-BA49-94E6-2051A402BFCA",
"docVars": Array [],
@ -10227,6 +10234,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "E43E077A-3477-A242-BD53-4313974E06A2",
"docVars": Array [],
@ -11118,6 +11126,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "B1317C39-ACCF-5B4D-BE97-FD2D73F14B00",
"docVars": Array [],
@ -12750,6 +12759,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "1D08889F-3E41-C046-8805-654C0B247DE3",
"docVars": Array [],
@ -14303,6 +14313,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "932EBF18-9F5F-C245-A499-C2EB430024C4",
"docVars": Array [],
@ -20071,6 +20082,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": false,
"characterSpacingControl": null,
"defaultTabStop": 840,
"docId": null,
"docVars": Array [],
@ -22908,6 +22920,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": null,
"defaultTabStop": 840,
"docId": null,
"docVars": Array [],
@ -24087,6 +24100,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "10BE20B6-DCA9-7441-B548-606D7D9EDD92",
"docVars": Array [],
@ -26275,6 +26289,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "DoNotCompress",
"defaultTabStop": 709,
"docId": "70F68831-609A-4ACC-87CE-416E9D216976",
"docVars": Array [
@ -28040,6 +28055,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "A8BB00BC-8D6F-2840-B682-9341438874CF",
"docVars": Array [],
@ -29858,6 +29874,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "F952540B-8E26-9040-BEAB-F11A2F10B576",
"docVars": Array [],
@ -32375,6 +32392,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "D69B5D79-AA39-424E-9D58-EF8E35CE7D20",
"docVars": Array [],
@ -80848,6 +80866,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": false,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 851,
"docId": "BF832BC0-91B3-4538-AB66-1291F36977FA",
"docVars": Array [
@ -101988,6 +102007,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": false,
"characterSpacingControl": "DoNotCompress",
"defaultTabStop": 720,
"docId": null,
"docVars": Array [
@ -107932,6 +107952,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "F963E3C9-F24C-354B-A852-391D33A55DB4",
"docVars": Array [],
@ -117057,6 +117078,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": false,
"characterSpacingControl": "DoNotCompress",
"defaultTabStop": 720,
"docId": "D4D4776C-D5E8-4FE1-8892-3B40BC54EF41",
"docVars": Array [],
@ -127767,6 +127789,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "C272B628-6DE8-264A-8734-9D2219FFD42F",
"docVars": Array [],
@ -128787,6 +128810,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "C11ED300-8EA6-3D41-8D67-5E5DE3410CF8",
"docVars": Array [],
@ -130142,6 +130166,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "67BB3557-1628-9C4D-816A-CC1F65CA4B80",
"docVars": Array [],
@ -133590,6 +133615,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "A3F1E202-8404-D445-9EDD-BBC3885575A6",
"docVars": Array [],
@ -137236,6 +137262,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "9F52F717-3F03-584C-ACEF-96E0106FA905",
"docVars": Array [],
@ -138256,6 +138283,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 840,
"docId": "7501A506-09EA-0F4C-ACFF-789B81CBED2D",
"docVars": Array [],
@ -147712,6 +147740,7 @@ Object {
},
"settings": Object {
"adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation",
"defaultTabStop": 420,
"docId": "A426FE01-5E89-4BAE-8422-F2CC8611EC68",
"docVars": Array [],