feat: Add style

main
bokuweb 2019-11-14 19:21:45 +09:00
parent 84972af477
commit a9eb8f96b2
17 changed files with 316 additions and 9 deletions

View File

@ -30,11 +30,6 @@ impl BuildXML for DocumentRels {
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings",
"settings.xml",
)
.relationship(
"rId4",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/tag",
"tag.xml",
)
.close()
.build()
}
@ -59,7 +54,6 @@ mod tests {
<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/tag" Target="tag.xml" />
</Relationships>"#
);
}

View File

@ -0,0 +1,57 @@
use crate::documents::BuildXML;
use crate::types::*;
use crate::xml_builder::*;
#[derive(Debug)]
pub struct Font<'a> {
name: &'a str,
charset: &'a str,
family: &'a str,
pitch: FontPitchType,
}
impl<'a> Font<'a> {
pub fn new(name: &'a str, charset: &'a str, family: &'a str, pitch: FontPitchType) -> Font<'a> {
Font {
name,
charset,
family,
pitch,
}
}
}
impl<'a> BuildXML for Font<'a> {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.open_font(self.name)
.charset(self.charset)
.family(self.family)
.pitch(&self.pitch.to_string())
.close()
.build()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
fn test_build() {
let c = Font::new("Arial", "00", "swiss", FontPitchType::Variable);
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:font w:name="Arial">
<w:charset w:val="00" />
<w:family w:val="swiss" />
<w:pitch w:val="variable" />
</w:font>"#
);
}
}

View File

@ -5,6 +5,7 @@ mod br;
mod color;
mod default_tab_stop;
mod doc_defaults;
mod font;
mod grid_span;
mod highlight;
mod indent;
@ -48,6 +49,7 @@ pub use br::*;
pub use color::*;
pub use default_tab_stop::*;
pub use doc_defaults::*;
pub use font::*;
pub use grid_span::*;
pub use highlight::*;
pub use indent::*;

View File

@ -43,6 +43,26 @@ impl Style {
..default
}
}
pub fn size(mut self, size: usize) -> Self {
self.run_property = self.run_property.size(size);
self
}
pub fn color(mut self, color: &str) -> Self {
self.run_property = self.run_property.color(color);
self
}
pub fn bold(mut self) -> Self {
self.run_property = self.run_property.bold();
self
}
pub fn italic(mut self) -> Self {
self.run_property = self.run_property.italic();
self
}
}
impl BuildXML for Style {

View File

@ -0,0 +1,67 @@
use super::Font;
use crate::documents::BuildXML;
use crate::types::FontPitchType;
use crate::xml_builder::*;
#[derive(Debug)]
pub struct FontTable {}
impl FontTable {
pub fn new() -> FontTable {
Default::default()
}
}
impl Default for FontTable {
fn default() -> Self {
Self {}
}
}
impl BuildXML for FontTable {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
let times = Font::new("Times New Roman", "00", "roman", FontPitchType::Variable);
let symbol = Font::new("Symbol", "02", "roman", FontPitchType::Variable);
let arial = Font::new("Arial", "00", "swiss", FontPitchType::Variable);
b.declaration(Some(true))
.open_fonts()
.add_child(&times)
.add_child(&symbol)
.add_child(&arial)
.close()
.build()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
fn test_settings() {
let c = FontTable::new();
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:fonts xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><w:font w:name="Times New Roman">
<w:charset w:val="00" />
<w:family w:val="roman" />
<w:pitch w:val="variable" />
</w:font><w:font w:name="Symbol">
<w:charset w:val="02" />
<w:family w:val="roman" />
<w:pitch w:val="variable" />
</w:font><w:font w:name="Arial">
<w:charset w:val="00" />
<w:family w:val="swiss" />
<w:pitch w:val="variable" />
</w:font></w:fonts>"#
);
}
}

View File

@ -4,6 +4,7 @@ mod doc_props;
mod document;
mod document_rels;
mod elements;
mod font_table;
mod rels;
mod settings;
mod styles;
@ -16,6 +17,7 @@ pub use doc_props::*;
pub use document::*;
pub use document_rels::*;
pub use elements::*;
pub use font_table::*;
pub use rels::*;
pub use settings::*;
pub use styles::*;
@ -30,6 +32,7 @@ pub struct Docx {
styles: Styles,
document: Document,
settings: Settings,
font_table: FontTable,
}
impl Default for Docx {
@ -41,6 +44,7 @@ impl Default for Docx {
let document = Document::new();
let document_rels = DocumentRels::new();
let settings = Settings::new();
let font_table = FontTable::new();
Docx {
content_type,
rels,
@ -49,6 +53,7 @@ impl Default for Docx {
document,
document_rels,
settings,
font_table,
}
}
}
@ -77,6 +82,7 @@ impl Docx {
document: self.document.build(),
document_rels: self.document_rels.build(),
settings: self.settings.build(),
font_table: self.font_table.build(),
}
}
}

View File

@ -1,5 +1,6 @@
use super::{DocDefaults, Style};
use crate::documents::BuildXML;
use crate::types::*;
use crate::xml_builder::*;
#[derive(Debug)]
@ -31,8 +32,10 @@ impl Default for Styles {
impl BuildXML for Styles {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
let normal = Style::new("Normal", "Normal", StyleType::Paragraph);
b.open_styles()
.add_child(&self.doc_defaults)
.add_child(&normal)
.add_children(&self.styles)
.close()
.build()
@ -54,7 +57,7 @@ mod tests {
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:styles xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" mc:Ignorable="w14 w15"><w:docDefaults><w:rPrDefault><w:rPr /></w:rPrDefault></w:docDefaults><w:style w:type="paragraph" w:styleId="Title"><w:name w:val="TitleName" /><w:rPr /><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr><w:basedOn w:val="Normal" /><w:next w:val="Normal" /><w:qFormat /></w:style></w:styles>"#
r#"<w:styles xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" mc:Ignorable="w14 w15"><w:docDefaults><w:rPrDefault><w:rPr /></w:rPrDefault></w:docDefaults><w:style w:type="paragraph" w:styleId="Normal"><w:name w:val="Normal" /><w:rPr /><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr><w:basedOn w:val="Normal" /><w:next w:val="Normal" /><w:qFormat /></w:style><w:style w:type="paragraph" w:styleId="Title"><w:name w:val="TitleName" /><w:rPr /><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr><w:basedOn w:val="Normal" /><w:next w:val="Normal" /><w:qFormat /></w:style></w:styles>"#
);
}
}

View File

@ -13,6 +13,7 @@ pub struct XMLDocx {
pub document: Vec<u8>,
pub document_rels: Vec<u8>,
pub settings: Vec<u8>,
pub font_table: Vec<u8>,
}
impl XMLDocx {

View File

@ -0,0 +1,20 @@
use std::fmt;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
#[derive(Copy, Clone, Debug)]
pub enum FontPitchType {
Default,
Fixed,
Variable,
}
impl fmt::Display for FontPitchType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
FontPitchType::Default => write!(f, "default"),
FontPitchType::Fixed => write!(f, "fixed"),
FontPitchType::Variable => write!(f, "variable"),
}
}
}

View File

@ -2,6 +2,7 @@ pub mod alignment_type;
pub mod border_position;
pub mod border_type;
pub mod break_type;
pub mod font_pitch_type;
pub mod special_indent_type;
pub mod style_type;
pub mod table_alignment_type;
@ -12,6 +13,7 @@ pub use alignment_type::*;
pub use border_position::*;
pub use border_type::*;
pub use break_type::*;
pub use font_pitch_type::*;
pub use special_indent_type::*;
pub use style_type::*;
pub use table_alignment_type::*;

View File

@ -128,6 +128,11 @@ impl XMLBuilder {
closed_el!(br, "w:br", "w:type");
closed_el!(zoom, "w:zoom", "w:percent");
only_usize_val_el!(default_tab_stop, "w:defaultTabStop");
opened_el!(open_font, "w:font", "w:name");
only_str_val_el!(pitch, "w:pitch");
only_str_val_el!(family, "w:family");
only_str_val_el!(charset, "w:charset");
}
#[cfg(test)]

View File

@ -0,0 +1,21 @@
use super::XMLBuilder;
use super::XmlEvent;
impl XMLBuilder {
pub(crate) fn open_fonts(mut self) -> Self {
self.writer
.write(
XmlEvent::start_element("w:fonts")
.attr(
"xmlns:w",
"http://schemas.openxmlformats.org/wordprocessingml/2006/main",
)
.attr(
"xmlns:r",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships",
),
)
.expect("should write to buf");
self
}
}

View File

@ -5,6 +5,7 @@ mod core_properties;
mod declaration;
mod document;
mod elements;
mod fonts;
mod properties;
mod relationship;
mod settings;

View File

@ -35,6 +35,8 @@ where
zip.write_all(&xml.styles)?;
zip.start_file("word/settings.xml", options)?;
zip.write_all(&xml.settings)?;
zip.start_file("word/fontTable.xml", options)?;
zip.write_all(&xml.font_table)?;
zip.finish()?;
Ok(())
}

View File

@ -4,6 +4,17 @@ use docx_core::*;
pub const DUMMY: &str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
#[test]
pub fn hello() -> Result<(), DocxError> {
let path = std::path::Path::new("./tests/output/hello.docx");
let file = std::fs::File::create(&path).unwrap();
Docx::new()
.add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello")))
.build()
.pack(file)?;
Ok(())
}
#[test]
pub fn indent() -> Result<(), DocxError> {
let path = std::path::Path::new("./tests/output/indent.docx");

View File

@ -1,3 +1,6 @@
<?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"/>
<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"/>
</Relationships>

View File

@ -1,2 +1,94 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="w14"><w:docDefaults><w:rPrDefault><w:rPr><w:rFonts w:ascii="Liberation Serif" w:hAnsi="Liberation Serif" w:eastAsia="Noto Sans CJK JP" w:cs="Lohit Devanagari"/><w:kern w:val="2"/><w:sz w:val="24"/><w:szCs w:val="24"/><w:lang w:val="en-US" w:eastAsia="ja-JP" w:bidi="hi-IN"/></w:rPr></w:rPrDefault><w:pPrDefault><w:pPr><w:widowControl/></w:pPr></w:pPrDefault></w:docDefaults><w:style w:type="paragraph" w:styleId="Normal"><w:name w:val="Normal"/><w:qFormat/><w:pPr><w:widowControl/></w:pPr><w:rPr><w:rFonts w:ascii="Liberation Serif" w:hAnsi="Liberation Serif" w:eastAsia="Noto Sans CJK JP" w:cs="Lohit Devanagari"/><w:color w:val="auto"/><w:kern w:val="2"/><w:sz w:val="24"/><w:szCs w:val="24"/><w:lang w:val="en-US" w:eastAsia="ja-JP" w:bidi="hi-IN"/></w:rPr></w:style><w:style w:type="paragraph" w:styleId="Style14"><w:name w:val="見出し"/><w:basedOn w:val="Normal"/><w:next w:val="Style15"/><w:qFormat/><w:pPr><w:keepNext w:val="true"/><w:spacing w:before="240" w:after="120"/></w:pPr><w:rPr><w:rFonts w:ascii="Liberation Sans" w:hAnsi="Liberation Sans" w:eastAsia="Noto Sans CJK JP" w:cs="Lohit Devanagari"/><w:sz w:val="28"/><w:szCs w:val="28"/></w:rPr></w:style><w:style w:type="paragraph" w:styleId="Style15"><w:name w:val="Body Text"/><w:basedOn w:val="Normal"/><w:pPr><w:spacing w:lineRule="auto" w:line="276" w:before="0" w:after="140"/></w:pPr><w:rPr></w:rPr></w:style><w:style w:type="paragraph" w:styleId="Style16"><w:name w:val="List"/><w:basedOn w:val="Style15"/><w:pPr></w:pPr><w:rPr><w:rFonts w:cs="Lohit Devanagari"/></w:rPr></w:style><w:style w:type="paragraph" w:styleId="Style17"><w:name w:val="Caption"/><w:basedOn w:val="Normal"/><w:qFormat/><w:pPr><w:suppressLineNumbers/><w:spacing w:before="120" w:after="120"/></w:pPr><w:rPr><w:rFonts w:cs="Lohit Devanagari"/><w:i/><w:iCs/><w:sz w:val="24"/><w:szCs w:val="24"/></w:rPr></w:style><w:style w:type="paragraph" w:styleId="Style18"><w:name w:val="索引"/><w:basedOn w:val="Normal"/><w:qFormat/><w:pPr><w:suppressLineNumbers/></w:pPr><w:rPr><w:rFonts w:cs="Lohit Devanagari"/></w:rPr></w:style></w:styles>
<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="w14">
<w:docDefaults>
<w:rPrDefault>
<w:rPr>
<w:rFonts w:ascii="Liberation Serif" w:hAnsi="Liberation Serif" w:eastAsia="Noto Sans CJK JP" w:cs="Lohit Devanagari"/>
<w:kern w:val="2"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
<w:lang w:val="en-US" w:eastAsia="ja-JP" w:bidi="hi-IN"/>
</w:rPr>
</w:rPrDefault>
<w:pPrDefault>
<w:pPr>
<w:widowControl/>
</w:pPr>
</w:pPrDefault>
</w:docDefaults>
<w:style w:type="paragraph" w:styleId="Normal">
<w:name w:val="Normal"/>
<w:qFormat/>
<w:pPr>
<w:widowControl/>
</w:pPr>
<w:rPr>
<w:rFonts w:ascii="Liberation Serif" w:hAnsi="Liberation Serif" w:eastAsia="Noto Sans CJK JP" w:cs="Lohit Devanagari"/>
<w:color w:val="auto"/>
<w:kern w:val="2"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
<w:lang w:val="en-US" w:eastAsia="ja-JP" w:bidi="hi-IN"/>
</w:rPr>
</w:style>
<w:style w:type="paragraph" w:styleId="Style14">
<w:name w:val="見出し"/>
<w:basedOn w:val="Normal"/>
<w:next w:val="Style15"/>
<w:qFormat/>
<w:pPr>
<w:keepNext w:val="true"/>
<w:spacing w:before="240" w:after="120"/>
</w:pPr>
<w:rPr>
<w:rFonts w:ascii="Liberation Sans" w:hAnsi="Liberation Sans" w:eastAsia="Noto Sans CJK JP" w:cs="Lohit Devanagari"/>
<w:sz w:val="28"/>
<w:szCs w:val="28"/>
</w:rPr>
</w:style>
<w:style w:type="paragraph" w:styleId="Style15">
<w:name w:val="Body Text"/>
<w:basedOn w:val="Normal"/>
<w:pPr>
<w:spacing w:lineRule="auto" w:line="276" w:before="0" w:after="140"/>
</w:pPr>
<w:rPr></w:rPr>
</w:style>
<w:style w:type="paragraph" w:styleId="Style16">
<w:name w:val="List"/>
<w:basedOn w:val="Style15"/>
<w:pPr></w:pPr>
<w:rPr>
<w:rFonts w:cs="Lohit Devanagari"/>
</w:rPr>
</w:style>
<w:style w:type="paragraph" w:styleId="Style17">
<w:name w:val="Caption"/>
<w:basedOn w:val="Normal"/>
<w:qFormat/>
<w:pPr>
<w:suppressLineNumbers/>
<w:spacing w:before="120" w:after="120"/>
</w:pPr>
<w:rPr>
<w:rFonts w:cs="Lohit Devanagari"/>
<w:i/>
<w:iCs/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
</w:style>
<w:style w:type="paragraph" w:styleId="Style18">
<w:name w:val="索引"/>
<w:basedOn w:val="Normal"/>
<w:qFormat/>
<w:pPr>
<w:suppressLineNumbers/>
</w:pPr>
<w:rPr>
<w:rFonts w:cs="Lohit Devanagari"/>
</w:rPr>
</w:style>
</w:styles>