Got text working.

This commit is contained in:
Unknown
2018-03-29 14:30:42 -04:00
parent ff38043a10
commit 669d1182f4
9 changed files with 87 additions and 71 deletions

43
Variables.go Normal file
View File

@@ -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

View File

@@ -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)
}
}
}

View File

@@ -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.
//

13
ps.go
View File

@@ -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)

View File

@@ -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) {

View File

@@ -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)

View File

@@ -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();

View File

@@ -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(",");
}

View File

@@ -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)