From 669d1182f47650f8698dcf55496efb35fda212fc Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 29 Mar 2018 14:30:42 -0400 Subject: [PATCH] Got text working. --- Variables.go | 43 ++++++++++++++++++++++++++++++++++++++++ cmd/ps/main.go | 27 ------------------------- colors.go | 5 ----- ps.go | 13 ------------ ps_test.go | 2 ++ scripts/getActiveDoc.jsx | 13 ++++++++---- scripts/getLayer.jsx | 10 ++++++++-- scripts/getLayerSet.jsx | 7 ++++++- structs.go | 38 +++++++++++++++++------------------ 9 files changed, 87 insertions(+), 71 deletions(-) create mode 100644 Variables.go delete mode 100644 cmd/ps/main.go diff --git a/Variables.go b/Variables.go new file mode 100644 index 0000000..f5f1c58 --- /dev/null +++ b/Variables.go @@ -0,0 +1,43 @@ +package ps + +import ( + "fmt" +) + +var Colors map[string]Color = map[string]Color{ + "Gray": &RGB{128, 128, 128}, + "White": &RGB{255, 255, 255}, +} + +// ModeEnum determines how aggressively the package will attempt to sync with Photoshop. +type ModeEnum int + +// Holds the current mode. +var Mode ModeEnum + +// Fast mode never checks layers before returning. +const Fast ModeEnum = 2 + +// Normal Mode Always checks to see if layers are up to date +// before returning them. +const Normal ModeEnum = 0 + +// Safe Mode Always loads the document from scratch. (Very Slow) +const Safe ModeEnum = 1 + +// PSSaveOptions is an enum for options when closing a document. +type PSSaveOptions int + +func (p *PSSaveOptions) String() string { + return fmt.Sprint("", *p) +} + +// PSSaveChanges saves changes before closing documents. +const PSSaveChanges PSSaveOptions = 1 + +// PSDoNotSaveChanges closes documents without saving. +const PSDoNotSaveChanges PSSaveOptions = 2 + +// PSPromptToSaveChanges prompts the user whether to save each +// document before closing it. +const PSPromptToSaveChanges PSSaveOptions = 3 diff --git a/cmd/ps/main.go b/cmd/ps/main.go deleted file mode 100644 index b1d0fa9..0000000 --- a/cmd/ps/main.go +++ /dev/null @@ -1,27 +0,0 @@ -package main - -import ( - "fmt" - "github.com/sbrow/ps" - "os" -) - -func main() { - args := []string{} - cmd := "" - switch { - case len(os.Args) > 1: - args = os.Args[2:] - fallthrough - case len(os.Args) > 0: - cmd = os.Args[1] - } - - fmt.Println(os.Args, cmd, args) - if cmd == "action" { - err := ps.DoAction(args[0], args[1]) - if err != nil { - panic(err) - } - } -} diff --git a/colors.go b/colors.go index 6a4e311..84126cf 100644 --- a/colors.go +++ b/colors.go @@ -4,11 +4,6 @@ import ( "encoding/hex" ) -var Colors map[string]Color = map[string]Color{ - "Gray": &RGB{128, 128, 128}, - "White": &RGB{255, 255, 255}, -} - // Color is an interface for color objects, allowing colors to be // used in various formats. // diff --git a/ps.go b/ps.go index fd49357..44c3411 100644 --- a/ps.go +++ b/ps.go @@ -23,19 +23,6 @@ var Cmd string var Opts string var pkgpath string -// PSSaveOptions is an enum for options when closing a document. -type PSSaveOptions int - -func (p *PSSaveOptions) String() string { - return fmt.Sprint("", *p) -} - -const ( - PSSaveChanges PSSaveOptions = 1 - PSDoNotSaveChanges PSSaveOptions = 2 - PSPromptToSaveChanges PSSaveOptions = 3 -) - func init() { _, file, _, _ := runtime.Caller(0) pkgpath = filepath.Dir(file) diff --git a/ps_test.go b/ps_test.go index 34e293b..04ba17d 100644 --- a/ps_test.go +++ b/ps_test.go @@ -137,6 +137,7 @@ func TestMove(t *testing.T) { } func TestActiveDocument(t *testing.T) { + Mode = Safe if testing.Short() { t.Skip("Skipping \"TestDocument\"") } @@ -167,6 +168,7 @@ func TestActiveDocument(t *testing.T) { } s := Stroke{Size: 4, Color: &RGB{0, 0, 0}} lyr.SetStroke(s, &RGB{128, 128, 128}) + d.Dump() } func TestColor(t *testing.T) { diff --git a/scripts/getActiveDoc.jsx b/scripts/getActiveDoc.jsx index eb14b4e..fbcfb7d 100644 --- a/scripts/getActiveDoc.jsx +++ b/scripts/getActiveDoc.jsx @@ -10,10 +10,15 @@ function layers(lyrs) { var lyr = lyrs[i]; stdout.write(('{"Name":"' + lyr.name + '", "Bounds": [[' + lyr.bounds[0] + ',' + lyr.bounds[1] + '],[' + lyr.bounds[2] + ',' + - lyr.bounds[3] + ']], "Visible": ' + lyr.visible + '}').replace(/ px/g, "")); - if (i+1 != lyrs.length) - stdout.write(','); - stdout.writeln(); + lyr.bounds[3] + ']], "Visible": ' + lyr.visible+',"Text":').replace(/ px/g, "")); + if (lyr.kind == LayerKind.TEXT) + stdout.write('"'+lyr.textItem.contents+'"'); + else + stdout.write("null"); + stdout.write("}") + if (i+1 != lyrs.length) + stdout.write(','); + stdout.writeln(); } } layers(doc.artLayers) diff --git a/scripts/getLayer.jsx b/scripts/getLayer.jsx index ac439ea..66cbfd5 100644 --- a/scripts/getLayer.jsx +++ b/scripts/getLayer.jsx @@ -2,7 +2,13 @@ var stdout = newFile(arguments[0]); var lyr = eval(arguments[1]); -stdout.writeln(('{"Name":"' + lyr.name + '","Bounds":[[' + lyr.bounds[0] + ',' + +stdout.write(('{"Name":"' + lyr.name + '","Bounds":[[' + lyr.bounds[0] + ',' + lyr.bounds[1] + '],[' + lyr.bounds[2] + ',' + - lyr.bounds[3] + ']],"Visible":' + lyr.visible + '}').replace(/ px/g, "")); + lyr.bounds[3] + ']],"Visible":' + lyr.visible+',"Text":').replace(/ px/g, "")); +if (lyr.kind == LayerKind.TEXT) { + stdout.write('"'+lyr.textItem.contents.replace(/\r/g, "\\r")+'"'); +} +else + stdout.write(null) +stdout.writeln('}') stdout.close(); \ No newline at end of file diff --git a/scripts/getLayerSet.jsx b/scripts/getLayerSet.jsx index c0eae16..289d9b9 100644 --- a/scripts/getLayerSet.jsx +++ b/scripts/getLayerSet.jsx @@ -7,7 +7,12 @@ for (var i = 0; i < set.artLayers.length; i++) { var lyr = set.artLayers[i]; stdout.write(('{"Name":"' + lyr.name + '", "Bounds": [[' + lyr.bounds[0] + ',' + lyr.bounds[1] + '],[' + lyr.bounds[2] + ',' + - lyr.bounds[3] + ']], "Visible": ' + lyr.visible + '}').replace(/ px/g, "")); + lyr.bounds[3] + ']], "Visible": ' + lyr.visible + ',"Text":').replace(/ px/g, "")); + if (lyr.kind == LayerKind.TEXT) + stdout.write('"'+lyr.textItem.contents.replace(/\r/g, "\\r")+'"'); + else + stdout.write("null"); + stdout.write("}") if (i != set.artLayers.length - 1) stdout.writeln(","); } diff --git a/structs.go b/structs.go index c7e3465..9d57bce 100644 --- a/structs.go +++ b/structs.go @@ -12,21 +12,6 @@ import ( "strings" ) -type ModeEnum int - -// Mode determines how aggressively ps will attempt to sync with Photoshop. -var Mode ModeEnum - -// Normal Mode Always checks to see if layers are updated -// before returning them. -const Normal ModeEnum = 0 - -// Safe Mode Always loads the document from scratch. (Slow) -const Safe ModeEnum = 1 - -// Fast mode never checks layers before returning. -const Fast ModeEnum = 2 - // Group represents a Document or LayerSet. type Group interface { Name() string @@ -182,8 +167,8 @@ func (d *Document) Dump() { // ArtLayer reflects certain values from an Art Layer in a Photoshop document. type ArtLayer struct { - name string // The layer's name. - // TextItem string + name string // The layer's name. + Text *string // The contents of a text layer. bounds [2][2]int // The layers' corners. parent Group // The LayerSet/Document this layer is in. visible bool // Whether or not the layer is visible. @@ -207,11 +192,13 @@ type ArtLayerJSON struct { Color [3]int Stroke [3]int StrokeAmt float32 + Text *string } -// MarshalJSON fufills the json.Marshaler interface, allowing the ArtLayer to be +// MarshalJSON fulfills the json.Marshaler interface, allowing the ArtLayer to be // saved to disk in JSON format. func (a *ArtLayer) MarshalJSON() ([]byte, error) { + // txt := strings.Replace(*a.Text, "\r", "\\r", -1) return json.Marshal(&ArtLayerJSON{ Name: a.name, Bounds: a.bounds, @@ -219,6 +206,7 @@ func (a *ArtLayer) MarshalJSON() ([]byte, error) { Color: a.Color.RGB(), Stroke: a.Stroke.RGB(), StrokeAmt: a.Stroke.Size, + Text: a.Text, }) } @@ -232,6 +220,10 @@ func (a *ArtLayer) UnmarshalJSON(b []byte) error { a.Color = RGB{tmp.Color[0], tmp.Color[1], tmp.Color[2]} a.Stroke = &Stroke{tmp.StrokeAmt, RGB{tmp.Stroke[0], tmp.Stroke[1], tmp.Stroke[2]}} a.visible = tmp.Visible + if tmp.Text != nil { + // s := strings.Replace(*tmp.Text, "\\r", "\r", -1) + a.Text = tmp.Text + } a.current = false return nil } @@ -264,7 +256,7 @@ func (a *ArtLayer) SetParent(c Group) { a.parent = c } -// SetActive makes this layer active in photoshop. +// SetActive makes this layer active in Photoshop. // Layers need to be active to perform certain operations func (a *ArtLayer) SetActive() ([]byte, error) { js := fmt.Sprintf("app.activeDocument.activeLayer=%s", JSLayer(a.Path())) @@ -473,10 +465,14 @@ func (l *LayerSet) ArtLayer(name string) *ArtLayer { } var lyr2 *ArtLayer err = json.Unmarshal(byt, &lyr2) + if err != nil { + log.Panic(err) + } lyr.name = lyr2.name lyr.bounds = lyr2.bounds lyr.visible = lyr2.visible lyr.current = true + lyr.Text = lyr2.Text } return lyr } @@ -519,6 +515,10 @@ func NewLayerSet(path string, g Group) (*LayerSet, error) { byt, err := DoJs("getLayerSet.jsx", JSLayer(path)) var out *LayerSet err = json.Unmarshal(byt, &out) + if err != nil { + log.Println(string(byt)) + log.Panic(err) + } if flag.Lookup("test.v") != nil { // log.Println(path) // log.Println(out)