Support orientation (#261)

* feat: Support page orient

* 0.0.175

* update snamps
main
bokuweb 2021-03-24 16:51:11 +09:00 committed by GitHub
parent 90d1e9a92c
commit 298821cf90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
47 changed files with 203 additions and 45 deletions

View File

@ -135,6 +135,11 @@ impl Document {
self self
} }
pub fn page_orient(mut self, o: crate::types::PageOrientationType) -> Self {
self.section_property = self.section_property.page_orient(o);
self
}
pub fn doc_grid(mut self, doc_grid: DocGrid) -> Self { pub fn doc_grid(mut self, doc_grid: DocGrid) -> Self {
self.section_property = self.section_property.doc_grid(doc_grid); self.section_property = self.section_property.doc_grid(doc_grid);
self self

View File

@ -1,4 +1,5 @@
use crate::documents::BuildXML; use crate::documents::BuildXML;
use crate::types::*;
use crate::xml_builder::*; use crate::xml_builder::*;
use serde::Serialize; use serde::Serialize;
@ -8,13 +9,18 @@ use serde::Serialize;
pub struct PageSize { pub struct PageSize {
w: u32, w: u32,
h: u32, h: u32,
orient: Option<PageOrientationType>,
} }
// These values were based on microsoft office word2019 windows edition. // These values were based on microsoft office word2019 windows edition.
// <w:pgSz w:w="11906" w:h="16838"/> // <w:pgSz w:w="11906" w:h="16838"/>
impl Default for PageSize { impl Default for PageSize {
fn default() -> PageSize { fn default() -> PageSize {
PageSize { w: 11906, h: 16838 } PageSize {
w: 11906,
h: 16838,
orient: None,
}
} }
} }
@ -24,7 +30,11 @@ impl PageSize {
} }
pub fn size(self, w: u32, h: u32) -> PageSize { pub fn size(self, w: u32, h: u32) -> PageSize {
PageSize { w, h } PageSize {
w,
h,
orient: self.orient,
}
} }
pub fn width(mut self, w: u32) -> PageSize { pub fn width(mut self, w: u32) -> PageSize {
@ -36,15 +46,30 @@ impl PageSize {
self.h = h; self.h = h;
self self
} }
pub fn orient(mut self, o: PageOrientationType) -> PageSize {
self.orient = Some(o);
self
}
} }
impl BuildXML for PageSize { impl BuildXML for PageSize {
fn build(&self) -> Vec<u8> { fn build(&self) -> Vec<u8> {
if let Some(orient) = self.orient {
XMLBuilder::new()
.page_size_with_orient(
&format!("{}", self.w),
&format!("{}", self.h),
&orient.to_string(),
)
.build()
} else {
XMLBuilder::new() XMLBuilder::new()
.page_size(&format!("{}", self.w), &format!("{}", self.h)) .page_size(&format!("{}", self.w), &format!("{}", self.h))
.build() .build()
} }
} }
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View File

@ -31,6 +31,11 @@ impl SectionProperty {
self self
} }
pub fn page_orient(mut self, o: PageOrientationType) -> Self {
self.page_size = self.page_size.orient(o);
self
}
pub fn doc_grid(mut self, doc_grid: DocGrid) -> Self { pub fn doc_grid(mut self, doc_grid: DocGrid) -> Self {
self.doc_grid = doc_grid; self.doc_grid = doc_grid;
self self

View File

@ -224,6 +224,11 @@ impl Docx {
self self
} }
pub fn page_orient(mut self, o: crate::types::PageOrientationType) -> Self {
self.document = self.document.page_orient(o);
self
}
pub fn default_size(mut self, size: usize) -> Self { pub fn default_size(mut self, size: usize) -> Self {
self.styles = self.styles.default_size(size); self.styles = self.styles.default_size(size);
self self

View File

@ -9,6 +9,7 @@ pub mod font_pitch_type;
pub mod height_rule; pub mod height_rule;
pub mod level_suffix_type; pub mod level_suffix_type;
pub mod page_margin; pub mod page_margin;
pub mod page_orientation_type;
pub mod section_type; pub mod section_type;
pub mod shd_type; pub mod shd_type;
pub mod spacing; pub mod spacing;
@ -32,6 +33,7 @@ pub use font_pitch_type::*;
pub use height_rule::*; pub use height_rule::*;
pub use level_suffix_type::*; pub use level_suffix_type::*;
pub use page_margin::*; pub use page_margin::*;
pub use page_orientation_type::*;
pub use section_type::*; pub use section_type::*;
pub use shd_type::*; pub use shd_type::*;
pub use spacing::*; pub use spacing::*;

View File

@ -0,0 +1,35 @@
use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;
use wasm_bindgen::prelude::*;
use super::errors;
#[wasm_bindgen]
#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum PageOrientationType {
Landscape,
Portrait,
}
impl fmt::Display for PageOrientationType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
PageOrientationType::Landscape => write!(f, "landscape"),
PageOrientationType::Portrait => write!(f, "portrait"),
}
}
}
impl FromStr for PageOrientationType {
type Err = errors::TypeError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"landscape" => Ok(PageOrientationType::Landscape),
"portrait" => Ok(PageOrientationType::Portrait),
_ => Ok(PageOrientationType::Portrait),
}
}
}

View File

@ -232,6 +232,7 @@ impl XMLBuilder {
closed_with_str!(type_tag, "w:type"); closed_with_str!(type_tag, "w:type");
closed!(page_size, "w:pgSz", "w:w", "w:h"); closed!(page_size, "w:pgSz", "w:w", "w:h");
closed!(page_size_with_orient, "w:pgSz", "w:w", "w:h", "w:orient");
closed!( closed!(
page_margin, page_margin,
"w:pgMar", "w:pgMar",

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

@ -18,7 +18,11 @@ import { BookmarkEnd } from "./bookmark-end";
import { Settings } from "./settings"; import { Settings } from "./settings";
import { DocProps } from "./doc-props"; import { DocProps } from "./doc-props";
import { Styles } from "./styles"; import { Styles } from "./styles";
import { SectionProperty, PageMargin } from "./section-property"; import {
SectionProperty,
PageMargin,
PageOrientationType,
} from "./section-property";
import { DocGridType, DocxJSON } from "./json"; import { DocGridType, DocxJSON } from "./json";
import * as wasm from "./pkg"; import * as wasm from "./pkg";
@ -127,6 +131,11 @@ export class Docx {
return this; return this;
} }
pageOrientation(o: PageOrientationType) {
this.sectionProperty.pageOrientation(o);
return this;
}
docGrid(type: DocGridType, linePitch?: number, charSpace?: number) { docGrid(type: DocGridType, linePitch?: number, charSpace?: number) {
this.sectionProperty.docGrid(type, linePitch, charSpace); this.sectionProperty.docGrid(type, linePitch, charSpace);
return this; return this;
@ -735,8 +744,16 @@ export class Docx {
} }
if (this.sectionProperty._pageSize) { if (this.sectionProperty._pageSize) {
const { w, h } = this.sectionProperty._pageSize; const { w, h, orient } = this.sectionProperty._pageSize;
docx = docx.page_size(w, h); docx = docx.page_size(w, h);
switch (orient) {
case "landscape":
docx = docx.page_orient(wasm.PageOrientationType.Landscape);
break;
case "portrait":
docx = docx.page_orient(wasm.PageOrientationType.Portrait);
break;
}
} }
if (this.sectionProperty._docGrid) { if (this.sectionProperty._docGrid) {

View File

@ -7,7 +7,10 @@ export type DocGrid = {
}; };
export class SectionProperty { export class SectionProperty {
_pageSize: PageSize | null = null; _pageSize: PageSize = {
w: 11906,
h: 16838,
};
_pageMargin: PageMargin | null = null; _pageMargin: PageMargin | null = null;
_docGrid: DocGrid = { _docGrid: DocGrid = {
gridType: "lines", gridType: "lines",
@ -15,7 +18,8 @@ export class SectionProperty {
}; };
pageSize(w: number, h: number) { pageSize(w: number, h: number) {
this._pageSize = { w, h }; this._pageSize.w = w;
this._pageSize.h = h;
return this; return this;
} }
@ -24,13 +28,20 @@ export class SectionProperty {
return this; return this;
} }
pageOrientation(orient: PageOrientationType) {
this._pageSize.orient = orient;
return this;
}
docGrid(gridType: DocGridType, linePitch?: number, charSpace?: number) { docGrid(gridType: DocGridType, linePitch?: number, charSpace?: number) {
this._docGrid = { gridType, linePitch, charSpace }; this._docGrid = { gridType, linePitch, charSpace };
return this; return this;
} }
} }
export type PageSize = { w: number; h: number }; export type PageOrientationType = "landscape" | "portrait";
export type PageSize = { w: number; h: number; orient?: PageOrientationType };
export type PageMargin = { export type PageMargin = {
top: number; top: number;

View File

@ -1,6 +1,6 @@
{ {
"name": "docx-wasm", "name": "docx-wasm",
"version": "0.0.174", "version": "0.0.175",
"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

@ -68,6 +68,11 @@ impl Docx {
self self
} }
pub fn page_orient(mut self, o: docx_rs::PageOrientationType) -> Docx {
self.0 = self.0.page_orient(o);
self
}
pub fn page_margin(mut self, margin: PageMargin) -> Docx { pub fn page_margin(mut self, margin: PageMargin) -> Docx {
self.0 = self.0.page_margin(margin.take()); self.0 = self.0.page_margin(margin.take());
self self

View File

@ -643,6 +643,7 @@ Object {
}, },
"pageSize": Object { "pageSize": Object {
"h": 16838, "h": 16838,
"orient": null,
"w": 11906, "w": 11906,
}, },
"sectionType": null, "sectionType": null,
@ -2864,6 +2865,7 @@ Object {
}, },
"pageSize": Object { "pageSize": Object {
"h": 16838, "h": 16838,
"orient": null,
"w": 11906, "w": 11906,
}, },
"sectionType": null, "sectionType": null,
@ -3698,6 +3700,7 @@ Object {
}, },
"pageSize": Object { "pageSize": Object {
"h": 16840, "h": 16840,
"orient": null,
"w": 11900, "w": 11900,
}, },
"sectionType": null, "sectionType": null,
@ -7374,6 +7377,7 @@ Object {
}, },
"pageSize": Object { "pageSize": Object {
"h": 16838, "h": 16838,
"orient": null,
"w": 11906, "w": 11906,
}, },
"sectionType": null, "sectionType": null,
@ -10215,6 +10219,7 @@ Object {
}, },
"pageSize": Object { "pageSize": Object {
"h": 16840, "h": 16840,
"orient": null,
"w": 11900, "w": 11900,
}, },
"sectionType": null, "sectionType": null,
@ -11238,6 +11243,7 @@ Object {
}, },
"pageSize": Object { "pageSize": Object {
"h": 16840, "h": 16840,
"orient": null,
"w": 11900, "w": 11900,
}, },
"sectionType": null, "sectionType": null,
@ -12004,6 +12010,31 @@ exports[`writer should write page margin 3`] = `
</w:num></w:numbering>" </w:num></w:numbering>"
`; `;
exports[`writer should write page orientation 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<Relationships xmlns=\\"http://schemas.openxmlformats.org/package/2006/relationships\\">
<Relationship Id=\\"rId1\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\\" Target=\\"styles.xml\\" />
<Relationship Id=\\"rId2\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable\\" Target=\\"fontTable.xml\\" />
<Relationship Id=\\"rId3\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\\" Target=\\"settings.xml\\" />
<Relationship Id=\\"rId4\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/header\\" Target=\\"header1.xml\\" />
<Relationship Id=\\"rId5\\" Type=\\"http://schemas.microsoft.com/office/2011/relationships/commentsExtended\\" Target=\\"commentsExtended.xml\\" />
</Relationships>"
`;
exports[`writer should write page orientation 2`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?>
<w:document xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" xmlns:r=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\\" xmlns:v=\\"urn:schemas-microsoft-com:vml\\" xmlns:w=\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\" xmlns:w10=\\"urn:schemas-microsoft-com:office:word\\" xmlns:wp=\\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\\" xmlns:wps=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\\" xmlns:wpg=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\\" xmlns:mc=\\"http://schemas.openxmlformats.org/markup-compatibility/2006\\" xmlns:wp14=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\\" xmlns:w14=\\"http://schemas.microsoft.com/office/word/2010/wordml\\" xmlns:w15=\\"http://schemas.microsoft.com/office/word/2012/wordml\\" mc:Ignorable=\\"w14 wp14\\">
<w:body><w:p w14:paraId=\\"00000001\\"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr><w:rFonts /></w:rPr><w:t xml:space=\\"preserve\\">Hello </w:t></w:r></w:p><w:sectPr><w:pgSz w:w=\\"16838\\" w:h=\\"11906\\" w:orient=\\"landscape\\" /><w:pgMar w:top=\\"1985\\" w:right=\\"1701\\" w:bottom=\\"1701\\" w:left=\\"1701\\" w:header=\\"851\\" w:footer=\\"992\\" w:gutter=\\"0\\" /><w:headerReference w:type=\\"default\\" r:id=\\"rId4\\" /><w:cols w:space=\\"425\\" /><w:docGrid w:type=\\"lines\\" w:linePitch=\\"360\\" /></w:sectPr></w:body>
</w:document>"
`;
exports[`writer should write page orientation 3`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?>
<w:numbering xmlns:r=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\\" xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" xmlns:v=\\"urn:schemas-microsoft-com:vml\\" xmlns:w=\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\"><w:abstractNum w:abstractNumId=\\"1\\"><w:lvl w:ilvl=\\"0\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"%1.\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"420\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"1\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"(%2)\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"840\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"2\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimalEnclosedCircle\\" /><w:lvlText w:val=\\"%3\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"1260\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"3\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"%4.\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"1680\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"4\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"(%5)\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"2100\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"5\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimalEnclosedCircle\\" /><w:lvlText w:val=\\"%6\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"2520\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"6\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"%7.\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"2940\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"7\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"(%8)\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"3360\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"8\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimalEnclosedCircle\\" /><w:lvlText w:val=\\"%9\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"3780\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl></w:abstractNum><w:num w:numId=\\"1\\">
<w:abstractNumId w:val=\\"1\\" />
</w:num></w:numbering>"
`;
exports[`writer should write page size 1`] = ` exports[`writer should write page size 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?> "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<Relationships xmlns=\\"http://schemas.openxmlformats.org/package/2006/relationships\\"> <Relationships xmlns=\\"http://schemas.openxmlformats.org/package/2006/relationships\\">

View File

@ -244,4 +244,20 @@ describe("writer", () => {
} }
} }
}); });
test("should write page orientation", () => {
const p = new w.Paragraph().addRun(new w.Run().addText("Hello "));
const buffer = new w.Docx()
.addParagraph(p)
.pageSize(16838, 11906)
.pageOrientation("landscape")
.build();
writeFileSync("../output/page_orientation.docx", buffer);
const z = new Zip(Buffer.from(buffer));
for (const e of z.getEntries()) {
if (e.entryName.match(/document.xml|numbering.xml/)) {
expect(z.readAsText(e)).toMatchSnapshot();
}
}
});
}); });