Support char spacing (#637)

* fix: spacing

* fix: spacing

* fix

* fix

* fix: snaps
main
bokuweb 2023-06-23 19:09:27 +09:00 committed by GitHub
parent e363ad8183
commit 637c2a86f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 90 additions and 61 deletions

View File

@ -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/), 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). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## docx-wasm@0.0.278-rc14 (23. Jun, 2023)
- Support character spacing in run property
## docx-wasm@0.0.278-rc10 (23. Jun, 2023) ## docx-wasm@0.0.278-rc10 (23. Jun, 2023)
- Support character space control function - Support character space control function

View File

@ -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("./584.docx").unwrap(); let mut file = File::open("./hello.docx").unwrap();
let mut buf = vec![]; let mut buf = vec![];
file.read_to_end(&mut buf).unwrap(); file.read_to_end(&mut buf).unwrap();

View File

@ -302,6 +302,11 @@ impl Paragraph {
self self
} }
pub fn character_spacing(mut self, spacing: i32) -> Self {
self.property = self.property.character_spacing(spacing);
self
}
pub fn delete(mut self, author: impl Into<String>, date: impl Into<String>) -> Self { pub fn delete(mut self, author: impl Into<String>, date: impl Into<String>) -> Self {
self.property.run_property.del = Some(Delete::new().author(author).date(date)); self.property.run_property.del = Some(Delete::new().author(author).date(date));
self self

View File

@ -85,6 +85,11 @@ impl ParagraphProperty {
self self
} }
pub fn character_spacing(mut self, spacing: i32) -> Self {
self.run_property.character_spacing = Some(CharacterSpacing::new(spacing));
self
}
pub fn keep_next(mut self, v: bool) -> Self { pub fn keep_next(mut self, v: bool) -> Self {
self.keep_next = Some(v); self.keep_next = Some(v);
self self

View File

@ -89,8 +89,9 @@ 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) {
let v = value_to_dax(&v)?; if let Ok(s) = i32::from_str(&v) {
rp = rp.spacing(v) rp = rp.spacing(s)
}
} }
} }
XMLElement::RunFonts => { XMLElement::RunFonts => {

View File

@ -8,6 +8,7 @@ use super::errors;
#[cfg_attr(feature = "wasm", wasm_bindgen)] #[cfg_attr(feature = "wasm", wasm_bindgen)]
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize)] #[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum CharacterSpacingValues { pub enum CharacterSpacingValues {
DoNotCompress, DoNotCompress,
CompressPunctuation, CompressPunctuation,

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

@ -12,10 +12,7 @@ import { setParagraphProperty } from "./paragraph-property";
import * as wasm from "./pkg"; import * as wasm from "./pkg";
type Child = | Paragraph type Child = Paragraph | Table | Comment | Hyperlink;
| Table
| Comment
| Hyperlink;
function buildHyperlink(child: Hyperlink) { function buildHyperlink(child: Hyperlink) {
let hyperlink = wasm.createHyperlink(child.v, convertHyperlinkType(child)); let hyperlink = wasm.createHyperlink(child.v, convertHyperlinkType(child));
@ -90,6 +87,12 @@ function buildParagraph(child: Paragraph) {
); );
} }
if (child.property.runProperty.characterSpacing != null) {
paragraph = paragraph.character_spacing(
child.property.runProperty.characterSpacing
);
}
if (child.property.paragraphPropertyChange) { if (child.property.paragraphPropertyChange) {
let change = wasm.createParagraphPropertyChange(); let change = wasm.createParagraphPropertyChange();
change = change change = change

View File

@ -13,8 +13,8 @@ export class DocDefaults {
return this; return this;
} }
spacing(spacing: number) { characterSpacing(characterSpacing: number) {
this.runProperty = { ...this.runProperty, spacing }; this.runProperty = { ...this.runProperty, characterSpacing };
return this; return this;
} }
} }

View File

@ -192,8 +192,8 @@ export class Docx {
return this; return this;
} }
defaultSpacing(spacing: number) { defaultCharacterSpacing(spacing: number) {
this.styles.defaultSpacing(spacing); this.styles.defaultCharacterSpacing(spacing);
return this; return this;
} }
@ -547,9 +547,9 @@ export class Docx {
docx = docx.default_size(this.styles.docDefaults.runProperty.size); docx = docx.default_size(this.styles.docDefaults.runProperty.size);
} }
if (this.styles.docDefaults.runProperty?.spacing) { if (this.styles.docDefaults.runProperty?.characterSpacing) {
docx = docx.default_spacing( docx = docx.default_spacing(
this.styles.docDefaults.runProperty.spacing this.styles.docDefaults.runProperty.characterSpacing
); );
} }
} }

View File

@ -46,7 +46,7 @@ export type RunPropertyJSON = {
italicCs?: boolean | null; italicCs?: boolean | null;
vanish?: boolean | null; vanish?: boolean | null;
specVanish?: boolean | null; specVanish?: boolean | null;
spacing?: number | null; characterSpacing?: number | null;
textBorder?: TextBorderJSON | null; textBorder?: TextBorderJSON | null;
ins?: InsertJSONData | null; ins?: InsertJSONData | null;
del?: DeleteJSONData | null; del?: DeleteJSONData | null;

View File

@ -91,8 +91,8 @@ export class Level {
return this; return this;
} }
spacing(spacing: number) { characterSpacing(characterSpacing: number) {
this.runProperty = { ...this.runProperty, spacing }; this.runProperty = { ...this.runProperty, characterSpacing };
return this; return this;
} }
} }

View File

@ -106,6 +106,11 @@ export class Paragraph {
return this; return this;
} }
characterSpacing(spacing: number) {
this.property.runProperty.characterSpacing = spacing;
return this;
}
keepNext(v: boolean) { keepNext(v: boolean) {
this.property = { ...this.property, keepNext: v }; this.property = { ...this.property, keepNext: v };
return this; return this;

View File

@ -40,7 +40,7 @@ export type RunProperty = {
underline?: string; underline?: string;
vanish?: boolean; vanish?: boolean;
fonts?: RunFonts; fonts?: RunFonts;
spacing?: number; characterSpacing?: number;
textBorder?: TextBorder; textBorder?: TextBorder;
ins?: RunPropertyIns; ins?: RunPropertyIns;
del?: RunPropertyDel; del?: RunPropertyDel;
@ -253,8 +253,8 @@ export class Run {
return this; return this;
} }
spacing(spacing: number) { spacing(characterSpacing: number) {
this.property = { ...this.property, spacing }; this.property = { ...this.property, characterSpacing };
return this; return this;
} }
@ -373,8 +373,8 @@ export const setRunProperty = <T extends wasm.Run | wasm.Style>(
target = target.vanish() as T; target = target.vanish() as T;
} }
if (property.spacing != null) { if (property.characterSpacing != null) {
target = target.spacing(property.spacing) as T; target = target.character_spacing(property.characterSpacing) as T;
} }
if (property.textBorder) { if (property.textBorder) {

View File

@ -128,8 +128,8 @@ export class Style {
return this; return this;
} }
spacing(spacing: number) { characterSpacing(characterSpacing: number) {
this._runProperty = { ...this._runProperty, spacing }; this._runProperty = { ...this._runProperty, characterSpacing };
return this; return this;
} }

View File

@ -16,8 +16,8 @@ export class Styles {
return this; return this;
} }
defaultSpacing(spacing: number) { defaultCharacterSpacing(spacing: number) {
this.docDefaults.spacing(spacing); this.docDefaults.characterSpacing(spacing);
return this; return this;
} }
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "docx-wasm", "name": "docx-wasm",
"version": "0.0.278-rc10", "version": "0.0.278-rc14",
"main": "dist/node/index.js", "main": "dist/node/index.js",
"browser": "dist/web/index.js", "browser": "dist/web/index.js",
"author": "bokuweb <bokuweb12@gmail.com>", "author": "bokuweb <bokuweb12@gmail.com>",

View File

@ -188,6 +188,11 @@ impl Paragraph {
self self
} }
pub fn character_spacing(mut self, spacing: i32) -> Self {
self.0 = self.0.character_spacing(spacing);
self
}
pub fn keep_next(mut self, v: bool) -> Self { pub fn keep_next(mut self, v: bool) -> Self {
self.0 = self.0.keep_next(v); self.0 = self.0.keep_next(v);
self self

View File

@ -96,7 +96,7 @@ impl Run {
self self
} }
pub fn spacing(mut self, spacing: i32) -> Run { pub fn character_spacing(mut self, spacing: i32) -> Run {
self.0.run_property = self.0.run_property.spacing(spacing); self.0.run_property = self.0.run_property.spacing(spacing);
self self
} }

View File

@ -73,7 +73,7 @@ impl Style {
self self
} }
pub fn spacing(mut self, spacing: i32) -> Self { pub fn character_spacing(mut self, spacing: i32) -> Self {
self.0.run_property = self.0.run_property.spacing(spacing); self.0.run_property = self.0.run_property.spacing(spacing);
self self
} }

View File

@ -581,7 +581,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "FB0AE6E2-8FB8-3345-A8FC-78CE6F3ABE4E", "docId": "FB0AE6E2-8FB8-3345-A8FC-78CE6F3ABE4E",
"docVars": Array [], "docVars": Array [],
@ -2689,7 +2689,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "3A5F0B6E-9417-4662-A075-C7869185C909", "docId": "3A5F0B6E-9417-4662-A075-C7869185C909",
"docVars": Array [], "docVars": Array [],
@ -4235,7 +4235,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "A1898E6C-1AED-3C4D-9CCF-C24208DA732E", "docId": "A1898E6C-1AED-3C4D-9CCF-C24208DA732E",
"docVars": Array [], "docVars": Array [],
@ -5908,7 +5908,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "4A9883C0-7AEC-574E-926D-DA6DB000DD90", "docId": "4A9883C0-7AEC-574E-926D-DA6DB000DD90",
"docVars": Array [], "docVars": Array [],
@ -7232,7 +7232,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "60C41423-192A-BA49-94E6-2051A402BFCA", "docId": "60C41423-192A-BA49-94E6-2051A402BFCA",
"docVars": Array [], "docVars": Array [],
@ -10232,7 +10232,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "E43E077A-3477-A242-BD53-4313974E06A2", "docId": "E43E077A-3477-A242-BD53-4313974E06A2",
"docVars": Array [], "docVars": Array [],
@ -11124,7 +11124,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "B1317C39-ACCF-5B4D-BE97-FD2D73F14B00", "docId": "B1317C39-ACCF-5B4D-BE97-FD2D73F14B00",
"docVars": Array [], "docVars": Array [],
@ -12757,7 +12757,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "1D08889F-3E41-C046-8805-654C0B247DE3", "docId": "1D08889F-3E41-C046-8805-654C0B247DE3",
"docVars": Array [], "docVars": Array [],
@ -14311,7 +14311,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "932EBF18-9F5F-C245-A499-C2EB430024C4", "docId": "932EBF18-9F5F-C245-A499-C2EB430024C4",
"docVars": Array [], "docVars": Array [],
@ -24096,7 +24096,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "10BE20B6-DCA9-7441-B548-606D7D9EDD92", "docId": "10BE20B6-DCA9-7441-B548-606D7D9EDD92",
"docVars": Array [], "docVars": Array [],
@ -26285,7 +26285,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "DoNotCompress", "characterSpacingControl": "doNotCompress",
"defaultTabStop": 709, "defaultTabStop": 709,
"docId": "70F68831-609A-4ACC-87CE-416E9D216976", "docId": "70F68831-609A-4ACC-87CE-416E9D216976",
"docVars": Array [ "docVars": Array [
@ -28051,7 +28051,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "A8BB00BC-8D6F-2840-B682-9341438874CF", "docId": "A8BB00BC-8D6F-2840-B682-9341438874CF",
"docVars": Array [], "docVars": Array [],
@ -29870,7 +29870,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "F952540B-8E26-9040-BEAB-F11A2F10B576", "docId": "F952540B-8E26-9040-BEAB-F11A2F10B576",
"docVars": Array [], "docVars": Array [],
@ -32388,7 +32388,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "D69B5D79-AA39-424E-9D58-EF8E35CE7D20", "docId": "D69B5D79-AA39-424E-9D58-EF8E35CE7D20",
"docVars": Array [], "docVars": Array [],
@ -80862,7 +80862,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": false, "adjustLineHeightInTable": false,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 851, "defaultTabStop": 851,
"docId": "BF832BC0-91B3-4538-AB66-1291F36977FA", "docId": "BF832BC0-91B3-4538-AB66-1291F36977FA",
"docVars": Array [ "docVars": Array [
@ -102003,7 +102003,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": false, "adjustLineHeightInTable": false,
"characterSpacingControl": "DoNotCompress", "characterSpacingControl": "doNotCompress",
"defaultTabStop": 720, "defaultTabStop": 720,
"docId": null, "docId": null,
"docVars": Array [ "docVars": Array [
@ -107948,7 +107948,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "F963E3C9-F24C-354B-A852-391D33A55DB4", "docId": "F963E3C9-F24C-354B-A852-391D33A55DB4",
"docVars": Array [], "docVars": Array [],
@ -117074,7 +117074,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": false, "adjustLineHeightInTable": false,
"characterSpacingControl": "DoNotCompress", "characterSpacingControl": "doNotCompress",
"defaultTabStop": 720, "defaultTabStop": 720,
"docId": "D4D4776C-D5E8-4FE1-8892-3B40BC54EF41", "docId": "D4D4776C-D5E8-4FE1-8892-3B40BC54EF41",
"docVars": Array [], "docVars": Array [],
@ -127785,7 +127785,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "C272B628-6DE8-264A-8734-9D2219FFD42F", "docId": "C272B628-6DE8-264A-8734-9D2219FFD42F",
"docVars": Array [], "docVars": Array [],
@ -128806,7 +128806,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "C11ED300-8EA6-3D41-8D67-5E5DE3410CF8", "docId": "C11ED300-8EA6-3D41-8D67-5E5DE3410CF8",
"docVars": Array [], "docVars": Array [],
@ -130162,7 +130162,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "67BB3557-1628-9C4D-816A-CC1F65CA4B80", "docId": "67BB3557-1628-9C4D-816A-CC1F65CA4B80",
"docVars": Array [], "docVars": Array [],
@ -133611,7 +133611,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "A3F1E202-8404-D445-9EDD-BBC3885575A6", "docId": "A3F1E202-8404-D445-9EDD-BBC3885575A6",
"docVars": Array [], "docVars": Array [],
@ -137258,7 +137258,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "9F52F717-3F03-584C-ACEF-96E0106FA905", "docId": "9F52F717-3F03-584C-ACEF-96E0106FA905",
"docVars": Array [], "docVars": Array [],
@ -138279,7 +138279,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 840, "defaultTabStop": 840,
"docId": "7501A506-09EA-0F4C-ACFF-789B81CBED2D", "docId": "7501A506-09EA-0F4C-ACFF-789B81CBED2D",
"docVars": Array [], "docVars": Array [],
@ -147736,7 +147736,7 @@ Object {
}, },
"settings": Object { "settings": Object {
"adjustLineHeightInTable": true, "adjustLineHeightInTable": true,
"characterSpacingControl": "CompressPunctuation", "characterSpacingControl": "compressPunctuation",
"defaultTabStop": 420, "defaultTabStop": 420,
"docId": "A426FE01-5E89-4BAE-8422-F2CC8611EC68", "docId": "A426FE01-5E89-4BAE-8422-F2CC8611EC68",
"docVars": Array [], "docVars": Array [],