Skip to content

Commit

Permalink
document: add more fine grained control of line spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
tbaliance committed May 28, 2018
1 parent 4293d9c commit 9471100
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
Binary file added _examples/document/line-spacing/line-spacing.docx
Binary file not shown.
35 changes: 35 additions & 0 deletions _examples/document/line-spacing/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2017 Baliance. All rights reserved.
package main

import (
"log"

"baliance.com/gooxml/document"
"baliance.com/gooxml/measurement"
"baliance.com/gooxml/schema/soo/wml"
)

func main() {
doc := document.New()
lorem := `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin lobortis, lectus dictum feugiat tempus, sem neque finibus enim, sed eleifend sem nunc ac diam. Vestibulum tempus sagittis elementum.`

// single spaced
para := doc.AddParagraph()
run := para.AddRun()
run.AddText(lorem)
run.AddText(lorem)
run.AddBreak()

// double spaced is twice the text height (24 points in this case as the text height is 12 points)
para = doc.AddParagraph()
para.Properties().Spacing().SetLineSpacing(24*measurement.Point, wml.ST_LineSpacingRuleAuto)
run = para.AddRun()
run.AddText(lorem)
run.AddText(lorem)
run.AddBreak()

if err := doc.Validate(); err != nil {
log.Fatalf("error during validation: %s", err)
}
doc.SaveToFile("line-spacing.docx")
}
13 changes: 12 additions & 1 deletion document/paragraphproperties.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,25 @@ func (p ParagraphProperties) X() *wml.CT_PPr {
}

// SetSpacing sets the spacing that comes before and after the paragraph.
// Deprecated: See Spacing() instead which allows finer control.
func (p ParagraphProperties) SetSpacing(before, after measurement.Distance) {
p.x.Spacing = wml.NewCT_Spacing()
if p.x.Spacing == nil {
p.x.Spacing = wml.NewCT_Spacing()
}
p.x.Spacing.BeforeAttr = &sharedTypes.ST_TwipsMeasure{}
p.x.Spacing.BeforeAttr.ST_UnsignedDecimalNumber = gooxml.Uint64(uint64(before / measurement.Twips))
p.x.Spacing.AfterAttr = &sharedTypes.ST_TwipsMeasure{}
p.x.Spacing.AfterAttr.ST_UnsignedDecimalNumber = gooxml.Uint64(uint64(after / measurement.Twips))
}

// Spacing returns the paragraph spacing settings.
func (p ParagraphProperties) Spacing() ParagraphSpacing {
if p.x.Spacing == nil {
p.x.Spacing = wml.NewCT_Spacing()
}
return ParagraphSpacing{p.x.Spacing}
}

// SetAlignment controls the paragraph alignment
func (p ParagraphProperties) SetAlignment(align wml.ST_Jc) {
if align == wml.ST_JcUnset {
Expand Down
64 changes: 64 additions & 0 deletions document/paragraphspacing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2018 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting [email protected].

package document

import (
"baliance.com/gooxml"
"baliance.com/gooxml/measurement"
"baliance.com/gooxml/schema/soo/ofc/sharedTypes"
"baliance.com/gooxml/schema/soo/wml"
)

// ParagraphSpacing controls the spacing for a paragraph and its lines.
type ParagraphSpacing struct {
x *wml.CT_Spacing
}

// SetBefore sets the spacing that comes before the paragraph.
func (p ParagraphSpacing) SetBefore(before measurement.Distance) {
p.x.BeforeAttr = &sharedTypes.ST_TwipsMeasure{}
p.x.BeforeAttr.ST_UnsignedDecimalNumber = gooxml.Uint64(uint64(before / measurement.Twips))
}

// SetAfter sets the spacing that comes after the paragraph.
func (p ParagraphSpacing) SetAfter(after measurement.Distance) {
p.x.AfterAttr = &sharedTypes.ST_TwipsMeasure{}
p.x.AfterAttr.ST_UnsignedDecimalNumber = gooxml.Uint64(uint64(after / measurement.Twips))
}

// SetLineSpacing sets the spacing between lines in a paragraph.
func (p ParagraphSpacing) SetLineSpacing(d measurement.Distance, rule wml.ST_LineSpacingRule) {
if rule == wml.ST_LineSpacingRuleUnset {
p.x.LineRuleAttr = wml.ST_LineSpacingRuleUnset
p.x.LineAttr = nil
} else {
p.x.LineRuleAttr = rule
p.x.LineAttr = &wml.ST_SignedTwipsMeasure{}
p.x.LineAttr.Int64 = gooxml.Int64(int64(d / measurement.Twips))
}
}

// SetBeforeAuto controls if spacing before a paragraph is automatically determined.
func (p ParagraphSpacing) SetBeforeAuto(b bool) {
if b {
p.x.BeforeAutospacingAttr = &sharedTypes.ST_OnOff{}
p.x.BeforeAutospacingAttr.Bool = gooxml.Bool(true)
} else {
p.x.BeforeAutospacingAttr = nil
}
}

// SetAfterAuto controls if spacing after a paragraph is automatically determined.
func (p ParagraphSpacing) SetAfterAuto(b bool) {
if b {
p.x.AfterAutospacingAttr = &sharedTypes.ST_OnOff{}
p.x.AfterAutospacingAttr.Bool = gooxml.Bool(true)
} else {
p.x.AfterAutospacingAttr = nil
}
}

0 comments on commit 9471100

Please sign in to comment.