forked from unidoc/unioffice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
document: add more fine grained control of line spacing
- Loading branch information
Showing
4 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |