document: add more styling support

- support character spacing and kerning in run style
- support contextual spacing in paragraph style
- add title style to new documents
- fix en-us -> en-US typo
This commit is contained in:
Todd 2017-09-02 10:40:39 -05:00
parent 09041bcba5
commit 90d005a914
8 changed files with 96 additions and 68 deletions

View File

@ -15,6 +15,21 @@ func main() {
para.SetStyle("Title")
run.AddText("Simple Document Formatting")
para = doc.AddParagraph()
para.SetStyle("Heading1")
run = para.AddRun()
run.AddText("Some Heading Text")
para = doc.AddParagraph()
para.SetStyle("Heading2")
run = para.AddRun()
run.AddText("Some Heading Text")
para = doc.AddParagraph()
para.SetStyle("Heading3")
run = para.AddRun()
run.AddText("Some Heading Text")
para = doc.AddParagraph()
run = para.AddRun()
run.AddText("A run is a string of characters with the same formatting. ")
@ -25,6 +40,7 @@ func main() {
run.SetFontSize(15)
run.SetColor(color.Red)
run.AddText("Multiple runs with different formatting can exist in the same paragraph. ")
run = para.AddRun()
run.AddText("Adding breaks to a run will insert line breaks after the run. ")
run.AddBreak()

View File

@ -81,3 +81,13 @@ func (p ParagraphStyleProperties) SetOutlineLevel(lvl int) {
p.x.OutlineLvl = wml.NewCT_DecimalNumber()
p.x.OutlineLvl.ValAttr = int64(lvl)
}
// SetContextualSpacing controls whether to Ignore Spacing Above and Below When
// Using Identical Styles
func (p ParagraphStyleProperties) SetContextualSpacing(b bool) {
if !b {
p.x.ContextualSpacing = nil
} else {
p.x.ContextualSpacing = wml.NewCT_OnOff()
}
}

View File

@ -57,6 +57,40 @@ func (r RunStyleProperties) SetSize(size measurement.Distance) {
szCs.ValAttr.ST_UnsignedDecimalNumber = gooxml.Uint64(uint64(size / measurement.HalfPoint))
}
// SetKerning sets the run's font kerning.
func (r RunStyleProperties) SetKerning(size measurement.Distance) {
var kern *wml.CT_HpsMeasure
for _, b := range r.x.EG_RPrBase {
if b.Kern != nil {
kern = b.Kern
}
}
if kern == nil {
b := wml.NewEG_RPrBase()
b.Kern = wml.NewCT_HpsMeasure()
kern = b.Kern
r.x.EG_RPrBase = append(r.x.EG_RPrBase, b)
}
kern.ValAttr.ST_UnsignedDecimalNumber = gooxml.Uint64(uint64(size / measurement.HalfPoint))
}
// SetCharacterSpacing sets the run's Character Spacing Adjustment.
func (r RunStyleProperties) SetCharacterSpacing(size measurement.Distance) {
var spacing *wml.CT_SignedTwipsMeasure
for _, b := range r.x.EG_RPrBase {
if b.Spacing != nil {
spacing = b.Spacing
}
}
if spacing == nil {
b := wml.NewEG_RPrBase()
b.Spacing = wml.NewCT_SignedTwipsMeasure()
spacing = b.Spacing
r.x.EG_RPrBase = append(r.x.EG_RPrBase, b)
}
spacing.ValAttr.Int64 = gooxml.Int64(int64(size / measurement.Twips))
}
// Fonts returns the style's Fonts.
func (r RunStyleProperties) Fonts() Fonts {
var ctf *wml.CT_Fonts

View File

@ -11,7 +11,6 @@ import (
"fmt"
"baliance.com/gooxml"
"baliance.com/gooxml/color"
"baliance.com/gooxml/measurement"
"baliance.com/gooxml/schema/schemas.openxmlformats.org/officeDocument/2006/sharedTypes"
wml "baliance.com/gooxml/schema/schemas.openxmlformats.org/wordprocessingml"
@ -57,7 +56,6 @@ func (s Styles) InitializeDefault() {
s.initializeDocDefaults()
s.initializeStyleDefaults()
}
func (s Styles) initializeStyleDefaults() {
// Normal
normal := s.AddStyle("Normal", wml.ST_StyleTypeParagraph, true)
@ -71,32 +69,38 @@ func (s Styles) initializeStyleDefaults() {
dpf.SetSemiHidden(true)
dpf.SetUnhideWhenUsed(true)
// Heading1
heading1Char := s.AddStyle("Heading1Char", wml.ST_StyleTypeCharacter, true)
heading1Char.SetName("Heading 1 Char")
heading1Char.SetLinkedStyle("Heading1")
heading1Char.SetBasedOn(dpf.StyleID())
heading1Char.RunProperties().SetSize(32 / measurement.HalfPoint)
heading1Char.RunProperties().Fonts().SetASCIITheme(wml.ST_ThemeMajorAscii)
heading1Char.RunProperties().Fonts().SetEastAsiaTheme(wml.ST_ThemeMajorEastAsia)
heading1Char.RunProperties().Fonts().SetHANSITheme(wml.ST_ThemeMajorHAnsi)
heading1Char.RunProperties().Fonts().SetCSTheme(wml.ST_ThemeMajorBidi)
heading1Char.RunProperties().Color().SetColor(color.FromHex("2F5496"))
heading1Char.RunProperties().Color().SetThemeColor(wml.ST_ThemeColorAccent1)
heading1Char.RunProperties().Color().SetThemeShade(0xbf)
// Title
titleChar := s.AddStyle("TitleChar", wml.ST_StyleTypeCharacter, false)
titleChar.SetName("Title Char")
titleChar.SetBasedOn(dpf.StyleID())
titleChar.SetLinkedStyle("Title")
titleChar.SetUISortOrder(10)
titleChar.RunProperties().Fonts().SetASCIITheme(wml.ST_ThemeMajorAscii)
titleChar.RunProperties().Fonts().SetEastAsiaTheme(wml.ST_ThemeMajorEastAsia)
titleChar.RunProperties().Fonts().SetHANSITheme(wml.ST_ThemeMajorHAnsi)
titleChar.RunProperties().Fonts().SetCSTheme(wml.ST_ThemeMajorBidi)
titleChar.RunProperties().SetSize(28 * measurement.Point)
titleChar.RunProperties().SetKerning(14 * measurement.Point)
titleChar.RunProperties().SetCharacterSpacing(-10 * measurement.Twips)
heading1 := s.AddStyle("Heading1", wml.ST_StyleTypeParagraph, true)
heading1.SetName("heading 1")
heading1.SetBasedOn(normal.StyleID())
heading1.SetNextStyle(normal.StyleID())
heading1.SetPrimaryStyle(true)
heading1.ParagraphProperties().SetKeepNext(true)
heading1.ParagraphProperties().SetKeepOnOnePage(true)
heading1.ParagraphProperties().SetSpacing(240*measurement.Twips, measurement.Zero)
heading1.ParagraphProperties().SetOutlineLevel(0)
titlePara := s.AddStyle("Title", wml.ST_StyleTypeParagraph, false)
titlePara.SetName("Title")
titlePara.SetBasedOn(normal.StyleID())
titlePara.SetNextStyle(normal.StyleID())
titlePara.SetLinkedStyle(titleChar.StyleID())
titlePara.SetUISortOrder(10)
titlePara.SetPrimaryStyle(true)
titlePara.ParagraphProperties().SetContextualSpacing(true)
titlePara.RunProperties().Fonts().SetASCIITheme(wml.ST_ThemeMajorAscii)
titlePara.RunProperties().Fonts().SetEastAsiaTheme(wml.ST_ThemeMajorEastAsia)
titlePara.RunProperties().Fonts().SetHANSITheme(wml.ST_ThemeMajorHAnsi)
titlePara.RunProperties().Fonts().SetCSTheme(wml.ST_ThemeMajorBidi)
titlePara.RunProperties().SetSize(28 * measurement.Point)
titlePara.RunProperties().SetKerning(14 * measurement.Point)
titlePara.RunProperties().SetCharacterSpacing(-10 * measurement.Twips)
// TableNormal
tbl := s.AddStyle("TableNormal", wml.ST_StyleTypeTable, true)
tbl := s.AddStyle("TableNormal", wml.ST_StyleTypeTable, false)
tbl.SetName("Normal Table")
tbl.SetUISortOrder(99)
tbl.SetSemiHidden(true)
@ -126,50 +130,12 @@ func (s Styles) initializeStyleDefaults() {
tw.SetValue(108 * measurement.Dxa)
// NoList
nbr := s.AddStyle("NoList", wml.ST_StyleTypeNumbering, true)
nbr := s.AddStyle("NoList", wml.ST_StyleTypeNumbering, false)
nbr.SetName("No List")
nbr.SetUISortOrder(1)
nbr.SetSemiHidden(true)
nbr.SetUnhideWhenUsed(true)
// HeaderChar
hc := s.AddStyle("HeaderChar", wml.ST_StyleTypeCharacter, true)
hc.SetName("Header Char")
hc.SetBasedOn(dpf.StyleID())
hc.SetLinkedStyle("Header")
hc.SetUISortOrder(99)
// Header
hdr := s.AddStyle("Header", wml.ST_StyleTypeParagraph, true)
hdr.SetName("header")
hdr.SetBasedOn(normal.StyleID())
hdr.SetUISortOrder(1)
hdr.SetSemiHidden(true)
hdr.SetUnhideWhenUsed(true)
hdr.SetLinkedStyle(hc.StyleID())
hdr.ParagraphProperties().AddTabStop(4680*measurement.Twips, wml.ST_TabJcCenter, wml.ST_TabTlcUnset)
hdr.ParagraphProperties().AddTabStop(9360*measurement.Twips, wml.ST_TabJcRight, wml.ST_TabTlcUnset)
hdr.ParagraphProperties().SetSpacing(0, 240*measurement.Twips)
// FooterChar
fc := s.AddStyle("FooterChar", wml.ST_StyleTypeCharacter, true)
fc.SetName("Footer Char")
fc.SetBasedOn(dpf.StyleID())
fc.SetLinkedStyle("Footer")
fc.SetUISortOrder(99)
// Footer
ftr := s.AddStyle("Footer", wml.ST_StyleTypeParagraph, true)
ftr.SetName("footer")
ftr.SetBasedOn(normal.StyleID())
ftr.SetUISortOrder(1)
ftr.SetSemiHidden(true)
ftr.SetUnhideWhenUsed(true)
ftr.SetLinkedStyle(fc.StyleID())
ftr.ParagraphProperties().AddTabStop(4680*measurement.Twips, wml.ST_TabJcCenter, wml.ST_TabTlcUnset)
ftr.ParagraphProperties().AddTabStop(9360*measurement.Twips, wml.ST_TabJcRight, wml.ST_TabTlcUnset)
ftr.ParagraphProperties().SetSpacing(0, 240*measurement.Twips)
fontSizes := []measurement.Distance{16, 13, 12, 11, 11, 11, 11, 11, 11}
spacing := []measurement.Distance{240, 40, 40, 40, 40, 40, 40, 40, 40}
for i := 0; i < 9; i++ {
@ -221,9 +187,11 @@ func (s Styles) initializeDocDefaults() {
base = wml.NewEG_RPrBase()
s.x.DocDefaults.RPrDefault.RPr.EG_RPrBase = append(s.x.DocDefaults.RPrDefault.RPr.EG_RPrBase, base)
base.Lang = wml.NewCT_Language()
base.Lang.ValAttr = gooxml.String("en-us")
base.Lang.EastAsiaAttr = gooxml.String("en-us")
base.Lang.ValAttr = gooxml.String("en-US")
base.Lang.EastAsiaAttr = gooxml.String("en-US")
base.Lang.BidiAttr = gooxml.String("ar-SA")
s.x.DocDefaults.PPrDefault = wml.NewCT_PPrDefault()
}
// Styles returns all styles.

Binary file not shown.

Binary file not shown.