Skip to content

Commit

Permalink
Add page dimensions & margins to exported data
Browse files Browse the repository at this point in the history
  • Loading branch information
richardwilkes committed Aug 1, 2023
1 parent b2bc82e commit 0f8e769
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
41 changes: 35 additions & 6 deletions model/gurps/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package gurps

import (
"bufio"
"bytes"
"encoding/base64"
htmltmpl "html/template"
"io"
Expand Down Expand Up @@ -232,6 +231,19 @@ type exportedPoints struct {
PointsBreakdown
}

type exportedMargins struct {
Top string
Left string
Bottom string
Right string
}

type exportedPage struct {
Width string
Height string
Margins exportedMargins
}

type exportedEntity struct {
Name string
EmbeddedPortraitDataURL htmltmpl.URL
Expand Down Expand Up @@ -269,6 +281,7 @@ type exportedEntity struct {
MeleeWeapons []*exportedMeleeWeapon
RangedWeapons []*exportedRangedWeapon
GridTemplate htmltmpl.CSS
Page exportedPage
}

// ExportSheets exports the files to a text representation.
Expand Down Expand Up @@ -303,14 +316,14 @@ func Export(entity *Entity, templatePath, exportPath string) error {
return errs.Wrap(err)
}
entity.Recalculate()
switch {
case bytes.HasPrefix(line, []byte("GCS HTML Template v1")):
switch string(line) {
case "GCS HTML Template v1":
var t *htmltmpl.Template
if t, err = htmltmpl.New("").Funcs(createTemplateFuncs()).Parse(string(tmpl[advance:])); err != nil {
return errs.Wrap(err)
}
return export(entity, t, exportPath)
case bytes.HasPrefix(line, []byte("GCS Text Template v1")):
case "GCS Text Template v1":
var t *texttmpl.Template
if t, err = texttmpl.New("").Funcs(createTemplateFuncs()).Parse(string(tmpl[advance:])); err != nil {
return errs.Wrap(err)
Expand Down Expand Up @@ -381,6 +394,8 @@ func export(entity *Entity, tmpl exporter, exportPath string) (err error) {
data := &exportedEntity{
Name: entity.Profile.Name,
Player: entity.Profile.PlayerName,
CreatedOn: entity.CreatedOn.String(),
ModifiedOn: entity.ModifiedOn.String(),
Title: entity.Profile.Title,
Organization: entity.Profile.Organization,
Religion: entity.Profile.Religion,
Expand Down Expand Up @@ -425,8 +440,7 @@ func export(entity *Entity, tmpl exporter, exportPath string) (err error) {
OtherValue: entity.WealthNotCarried(),
},
GridTemplate: htmltmpl.CSS(entity.SheetSettings.BlockLayout.HTMLGridTemplate()),
CreatedOn: entity.CreatedOn.String(),
ModifiedOn: entity.ModifiedOn.String(),
Page: newExportedPage(entity.SheetSettings.Page),
}
if entity.SheetSettings.ExcludeUnspentPointsFromTotal {
data.Points.Total = pb.Total()
Expand Down Expand Up @@ -662,6 +676,21 @@ func newExportedEquipment(entity *Entity, list []*Equipment, carried bool) []*ex
return result
}

func newExportedPage(settings *PageSettings) exportedPage {
width, height := settings.Size.Dimensions()
adjustedWidth, adjustedHeight := settings.Orientation.Dimensions(width, height)
return exportedPage{
Width: adjustedWidth.CSSString(),
Height: adjustedHeight.CSSString(),
Margins: exportedMargins{
Top: settings.TopMargin.CSSString(),
Left: settings.LeftMargin.CSSString(),
Bottom: settings.BottomMargin.CSSString(),
Right: settings.RightMargin.CSSString(),
},
}
}

func groupOrItem(isContainer bool) string {
if isContainer {
return "group"
Expand Down
5 changes: 5 additions & 0 deletions model/gurps/paper_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func (l PaperLength) String() string {
return strconv.FormatFloat(l.Length, 'f', -1, 64) + " " + l.Units.Key()
}

// CSSString returns a CSS-compatible version of the value.
func (l PaperLength) CSSString() string {
return strconv.FormatFloat(l.Length, 'f', -1, 64) + l.Units.Key()
}

// Pixels returns the number of 72-pixels-per-inch pixels this represents.
func (l PaperLength) Pixels() float32 {
return l.Units.ToPixels(l.Length)
Expand Down

0 comments on commit 0f8e769

Please sign in to comment.