This commit is contained in:
Unknown
2018-03-18 20:50:58 -04:00
parent 17a7f8ccd1
commit 7725110e35
13 changed files with 411 additions and 112 deletions

31
ps.go
View File

@@ -8,7 +8,6 @@ package ps
import ( import (
"bytes" "bytes"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@@ -75,7 +74,7 @@ func Quit(save PSSaveOptions) error {
// DoJs runs a Photoshop javascript script file (.jsx) from the specified location. // DoJs runs a Photoshop javascript script file (.jsx) from the specified location.
// It can't directly return output, so instead the scripts write their output to // It can't directly return output, so instead the scripts write their output to
// a temporary file. // a temporary file.
func DoJs(path string, args ...string) ([]byte, error) { func DoJs(path string, args ...string) (out []byte, err error) {
// Temp file for js to output to. // Temp file for js to output to.
outpath := filepath.Join(os.Getenv("TEMP"), "js_out.txt") outpath := filepath.Join(os.Getenv("TEMP"), "js_out.txt")
defer os.Remove(outpath) defer os.Remove(outpath)
@@ -164,8 +163,8 @@ func SaveAs(path string) error {
// Layers returns an array of ArtLayers from the active document // Layers returns an array of ArtLayers from the active document
// based on the given path string. // based on the given path string.
func Layers(path string) ([]ArtLayer, error) { /*func Layers(path string) ([]ArtLayer, error) {
byt, err := DoJs("getLayers.jsx", path) byt, err := DoJs("getLayers.jsx", JSLayer(path))
var out []ArtLayer var out []ArtLayer
err = json.Unmarshal(byt, &out) err = json.Unmarshal(byt, &out)
if err != nil { if err != nil {
@@ -173,15 +172,25 @@ func Layers(path string) ([]ArtLayer, error) {
} }
return out, err return out, err
} }
*/
// Layer returns an ArtLayer from the active document given a specified
// path string. Layer calls Layers() and returns the first result.
func Layer(path string) (ArtLayer, error) {
lyrs, err := Layers(path)
return lyrs[0], err
}
// ApplyDataset fills out a template file with information from a given dataset (csv) file. // ApplyDataset fills out a template file with information from a given dataset (csv) file.
func ApplyDataset(name string) ([]byte, error) { func ApplyDataset(name string) ([]byte, error) {
return DoJs("applyDataset.jsx", name) return DoJs("applyDataset.jsx", name)
} }
// JSLayer retrurns javascript code to get the layer with a given path.
func JSLayer(path string) string {
pth := strings.Split(path, "/")
js := "app.activeDocument"
last := len(pth) - 1
if last > 0 {
for i := 0; i < last; i++ {
js += fmt.Sprintf(".layerSets.getByName('%s')", pth[i])
}
}
if pth[last] != "" {
js += fmt.Sprintf(".artLayers.getByName('%s')", pth[last])
}
return js + ";"
}

View File

@@ -21,6 +21,7 @@ func TestStart(t *testing.T) {
} }
} }
/*
func TestOpen(t *testing.T) { func TestOpen(t *testing.T) {
if testing.Short() { if testing.Short() {
t.Skip("Skipping \"TestOpen\"") t.Skip("Skipping \"TestOpen\"")
@@ -30,7 +31,8 @@ func TestOpen(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
} }
*/
/*
func TestClose(t *testing.T) { func TestClose(t *testing.T) {
if testing.Short() { if testing.Short() {
t.Skip("Skipping \"TestClose\"") t.Skip("Skipping \"TestClose\"")
@@ -106,29 +108,80 @@ func TestSaveAs(t *testing.T) {
} }
func TestLayers(t *testing.T) { func TestLayers(t *testing.T) {
byt, err := Layers("Areas/TitleBackground") l, err := Layers("Areas/TitleBackground/")
// _, err := Layers("Areas/TitleBackground") fmt.Println(l)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
for _, lyr := range byt {
fmt.Println(lyr.Name)
fmt.Println(lyr.Bounds)
}
} }
*/
/*
func TestLayer(t *testing.T) { func TestLayer(t *testing.T) {
lyr, err := Layer("Areas/TitleBackground") _, err := Layer("Border/Inner Border")
// _, err := Layer("Areas/TitleBackground")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
fmt.Println(lyr.Name) }*/
fmt.Println(lyr.Bounds)
/*func TestMove(t *testing.T) {
lyr, err := Layer("Group 1/Layer 1")
if err != nil {
t.Fatal(err)
}
lyr.Position(100, 50, "top")
}*/
/*
func TestLayerSet(t *testing.T) {
set, err := GetLayerSet("Indicators/")
fmt.Println(set)
fmt.Println(set.ArtLayers[0].Parent)
if err != nil {
t.Fatal(err)
}
}
*/
func TestDocument(t *testing.T) {
d, err := GetDocument()
fmt.Println(d)
fmt.Println(d.ArtLayers[0])
fmt.Println(d.ArtLayers[0].Parent)
fmt.Println(d.LayerSets[0])
fmt.Println(d.LayerSets[0].Parent)
fmt.Println(d.LayerSets[0].ArtLayers[0])
fmt.Println(d.LayerSets[0].ArtLayers[0].Parent)
fmt.Println(d.LayerSets[0].ArtLayers[0].Parent.Parent())
if err != nil {
t.Fatal(err)
}
if d != d.ArtLayers[0].Parent {
t.Fatal("Fucked")
}
if d != d.LayerSets[0].Parent() {
t.Fatal("Fucked")
}
if d.LayerSets[0] != d.LayerSets[0].ArtLayers[0].Parent {
t.Fatal("Fucked")
}
} }
/*func TestActiveDocument(t *testing.T) {
e, err := DoJs("compilejs.jsx", "alert('testing!')")
fmt.Println(string(e))
if err != nil {
t.Fatal(err)
}
doc, err := ActiveDocument()
fmt.Println(doc)
if err != nil {
t.Fatal(err)
}
}
*/
/*
func TestApplyDataset(t *testing.T) { func TestApplyDataset(t *testing.T) {
out := []byte("done!\r\n") out := []byte("done!\r\n")
ret, err := ApplyDataset("Anger") ret, err := ApplyDataset("Anger")
@@ -144,10 +197,49 @@ func TestApplyDataset(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
} }
*/
func TestDoJs_HideLayer(t *testing.T) { /*func TestDoJs_HideLayer(t *testing.T) {
_, err := DoJs("hideLayers.jsx", "Areas/TitleBackground") _, err := DoJs("setLayerVisibility.jsx", "Areas/TitleBackground", "false")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
}*/
//.8s
//.15
func BenchmarkHideLayer(b *testing.B) {
for i := 0; i < b.N; i++ {
// _, err := Layers("Areas/TitleBackground/")
// if err != nil {
// b.Fatal(err)
// }
}
}
// 59ns
func BenchmarkHelloWorld_go(b *testing.B) {
for i := 0; i < b.N; i++ {
fmt.Sprintf("Hello, world!")
}
}
// ~35200000ns (.0352s)
func BenchmarkHelloWorld_vbs(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := run("helloworld")
if err != nil {
b.Fatal(err)
}
}
}
// ~51700000 (0.0517)
func BenchmarkHelloWorld_js(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := DoJs("test.jsx", "Hello, World!")
if err != nil {
b.Fatal(err)
}
}
} }

4
scripts/compilejs.jsx Normal file
View File

@@ -0,0 +1,4 @@
#include lib.js
var stdout = newFile(arguments[0])
eval(arguments[1]);
stdout.close()

38
scripts/getActiveDoc.jsx Normal file
View File

@@ -0,0 +1,38 @@
#include lib.js
var stdout = newFile(arguments[0]);
var doc = app.activeDocument;
stdout.writeln(('{"Name": "' + doc.name +'", "Height":' +doc.height +
', "Width":' + doc.width + ', "ArtLayers": [').replace(/ px/g, ""));
function layers(lyrs) {
if (typeof lyrs === 'undefined')
return;
for (var i = 0; i < lyrs.length; i++) {
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();
}
}
layers(doc.artLayers)
stdout.writeln('], "LayerSets": [');
function lyrSets(sets, nm) {
if (typeof sets === 'undefined')
return;
for (var i = 0; i < sets.length; i++) {
var set = sets[i];
var name = nm + set.name + "/";
stdout.write('{"Name": "' + set.name + '", "LayerSets": [');
// lyrSets(set.layerSets, name);
stdout.write('], "ArtLayers": [');
layers(set.artLayers);
stdout.write(']}');
if (i+1 != sets.length)
stdout.write(',');
}
}
lyrSets(doc.layerSets)
stdout.write(']}');
stdout.close();

View File

@@ -0,0 +1,50 @@
#include lib.js
var stdout = newFile(arguments[0]);
var doc = app.activeDocument;
stdout.write(('{"Name": "' + doc.name +'", "Height":' +doc.height +
', "Width":' + doc.width + ", ").replace(/ px/g, ""));
function layersNsets(obj) {
stdout.write('"ArtLayers": [');
lyrss(obj.artLayers, "")
stdout.write('], "LayerSets": [');
lyrSets(obj.layerSets, "");
// stdout.write('], "LayerSets": [');
}
function lyrss(lyrs, set) {
if (typeof lyrs === 'undefined')
return;
for (var i = 0; i < lyrs.length; i++) {
var lyr = lyrs[i];
stdout.write(('{"Name":"' + lyr.name + '", "Bounds": [[' + lyr.bounds[0] + ',' +
lyr.bounds[1] + '],[' + lyr.bounds[2] + ',' +
lyr.bounds[3] + ']], "Path": "' + set +
'", "Visible": ' + lyr.visible + '}').replace(/ px/g, ""));
if (i+1 != lyrs.length)
stdout.write(',');
}
}
function lyrSets(sets, nm) {
if (typeof sets === 'undefined')
return;
for (var i = 0; i < sets.length; i++) {
var set = sets[i];
var name = nm + set.name + "/";
stdout.write('{"Name": "' + set.name + '", "LayerSets": [');
lyrSets(set.layerSets, name);
stdout.write('], "Layers": [');
lyrss(set.artLayers, name);
stdout.write(']}');
if (i+1 != sets.length)
stdout.write(',');
}
}
layersNsets(doc)
stdout.writeln(']}');
alert(doc.layerSets.getByName("Group 2").layerSets.getByName("Group 1").layers.getByName("Layer 1").name)
stdout.close();

8
scripts/getLayer.jsx Normal file
View File

@@ -0,0 +1,8 @@
#include lib.js
var stdout = newFile(arguments[0]);
var lyr = eval(arguments[1]);
stdout.writeln(('{"Name":"' + lyr.name + '", "Bounds": [[' + lyr.bounds[0] + ',' +
lyr.bounds[1] + '],[' + lyr.bounds[2] + ',' +
lyr.bounds[3] + ']], "Visible": ' + lyr.visible + '}').replace(/ px/g, ""));
stdout.close();

15
scripts/getLayerSet.jsx Normal file
View File

@@ -0,0 +1,15 @@
#include lib.js
var stdout = newFile(arguments[0]);
var set = eval(arguments[1]);
stdout.writeln('{"Name": "'+set.name+'", "ArtLayers":[');
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, ""));
if (i != set.artLayers.length - 1)
stdout.writeln(",");
}
stdout.write("]}")
stdout.close();

View File

@@ -1,18 +0,0 @@
#include lib.js
var stdout = newFile(arguments[0])
var set = getLayers(arguments[1])
stdout.writeln('[');
for (var l = 0; l < set.layers.length; l++) {
var lyr = set.layers[l]
var lyrset = arguments[1].replace(lyr.name, "")
stdout.write(('{"Name":"' + lyr.name + '", "Bounds": [[' + lyr.bounds[0] + ',' +
lyr.bounds[1] + '],[' + lyr.bounds[2] + ',' +
lyr.bounds[3] + ']], "LayerSet": "' + lyrset + '"}').replace(/ px/g, ""));
if (l != set.layers.length - 1)
stdout.write(',');
stdout.writeln();
}
stdout.writeln(']');
stdout.close()

View File

@@ -8,27 +8,27 @@ function newFile(path) {
return f return f
} }
// Returns an array of ArtLayers from a layerSet or an ArtLayer. // Moves a layer
function getLayers(path) { function positionLayer(lyr, x, y, alignment){
try { if(lyr.iisBackgroundLayer||lyr.positionLocked) return
var doc = app.activeDocument var layerBounds = lyr.bounds;
var path = path.split('/') var layerX = layerBounds[0].value;
var lyrs = doc.layerSets.getByName(path[0]) if (alignment == 'top' || alignment == null)
for (var i = 1; i < path.length; i++) { var layerY = layerBounds[1].value;
try { else if (alignment == 'bottom')
lyrs = lyrs.layerSets.getByName(path[i]) var layerY = layerBounds[3].value;
} catch (e) { var deltaX = x-layerX;
lyrs = { layers: [lyrs.layers.getByName(path[i])] } } var deltaY = y-layerY;
} lyr.translate(deltaX, deltaY);
return lyrs }
} catch (e) {
if (e.message.indexOf('User') == -1)
alert(err(e));
else
throw new Exception('User cancelled the operation');
}
}
// Prints an error message.
function err(e) { function err(e) {
return 'ERROR: ' + e.message + ' at ' + e.fileName + ':' + e.line; return 'ERROR: ' + e.message + ' at ' + e.fileName + ':' + e.line;
}
function bounds(lyr) {
return ('"Bounds": [[' + lyr.bounds[0] + ',' +
lyr.bounds[1] + '],[' + lyr.bounds[2] + ',' +
lyr.bounds[3] + ']]').replace(/ px/g, "");
} }

6
scripts/moveLayer.jsx Normal file
View File

@@ -0,0 +1,6 @@
#include lib.js
var stdout = newFile(arguments[0]);
var lyr = eval(arguments[1]);
lyr.translate((Number)(arguments[2]), (Number)(arguments[3]));
stdout.writeln('{' + bounds(lyr) + '}')
stdout.close();

View File

@@ -1,11 +0,0 @@
#include lib.js
var stdout = newFile(arguments[0])
var lyrs = getLayers(arguments[1])
var vis = arguments[2] == "true"
try {
for (var i = 0; i < lyrs.layers.length; i++)
lyrs.layers[i].visible = vis
} catch (e) {
stdout.writeln(err(e))
}
stdout.close()

View File

@@ -24,47 +24,34 @@ function main() {
formatText() formatText()
} }
DeckCardPSD.prototype.formatText = function() { function formatText() {
var speed = this.textLayers.getByName('speed');
var lyrs = getLayers(arguments[1])
var speed = lyrs.getByName('speed');
if (speed.visible) { if (speed.visible) {
this.changeStroke(speed, (speed.textItem.contents == 1) ? [128, 128, 128] : [255, 255, 255], this.changeStroke(speed, (speed.textItem.contents == 1) ? [128, 128, 128] : [255, 255, 255],
this.colors.banner) this.colors.banner)
} }
/**
* The lowest we allow a text layer to go.
* @type {int}
*/
var bottom = this.doc.height-this.tolerance.flavor_text var bottom = this.doc.height-this.tolerance.flavor_text
// Get our text layers. // var short_text = this.setTextLayer('short_text', undefined, null, 'Arial', 'Regular',[this.bold_words, "Bold"]);
var short_text = this.setTextLayer('short_text', undefined, null, 'Arial', 'Regular',[this.bold_words, "Bold"]); var short_text = lyrs.getByName('short_text')
var long_text = this.textLayers.getByName('long_text'); var long_text = lyrs.getByName('long_text');
var flavor_text = this.textLayers.getByName('flavor_text'); var flavor_text = lyrs.getByName('flavor_text');
// Position the layers.
positionLayer(this.short_textBackground, this.short_textBackground.bounds[0], short_text.bounds[3] + this.tolerance.short_text, 'bottom'); positionLayer(this.short_textBackground, this.short_textBackground.bounds[0], short_text.bounds[3] + this.tolerance.short_text, 'bottom');
positionLayer(long_text, long_text.bounds[0], this.short_textBackground.bounds[3] + this.tolerance.long_text, 'top'); positionLayer(long_text, long_text.bounds[0], this.short_textBackground.bounds[3] + this.tolerance.long_text, 'top');
positionLayer(flavor_text, flavor_text.bounds[0], bottom, 'bottom'); positionLayer(flavor_text, flavor_text.bounds[0], bottom, 'bottom');
/**
* Make our layers visible
* @todo hack, fix.
*/
short_text.visible = short_text.textItem.contents != "“"; short_text.visible = short_text.textItem.contents != "“";
long_text.visible = long_text.textItem.contents != "“"; long_text.visible = long_text.textItem.contents != "“";
flavor_text.visible = flavor_text.textItem.contents != "“"; flavor_text.visible = flavor_text.textItem.contents != "“";
//Hide long_text if too long.
if (long_text.bounds[3] > this.doc.height - bottom) { if (long_text.bounds[3] > this.doc.height - bottom) {
long_text.visible == false; long_text.visible == false;
} }
this.log.debug(short_text.bounds)
this.log.debug(long_text.bounds)
this.log.debug(flavor_text.bounds)
//Hide flavor text if too long.
if ( (long_text.visible && flavor_text.bounds[1] < long_text.bounds[3]) if ( (long_text.visible && flavor_text.bounds[1] < long_text.bounds[3])
|| (short_text.visible && flavor_text.bounds[1] < short_text.bounds[3])) { || (short_text.visible && flavor_text.bounds[1] < short_text.bounds[3])) {
flavor_text.visible = false; flavor_text.visible = false;

View File

@@ -1,25 +1,144 @@
package ps package ps
// type layer interface { import (
// Name() string "encoding/json"
// TextItem() []string "fmt"
// } )
type Group interface {
Parent() Group
GetArtLayers() []*ArtLayer
GetLayerSets() []*LayerSet
}
type Document struct {
Name string
Height int
Width int
ArtLayers []*ArtLayer
LayerSets []*LayerSet
}
func (d *Document) GetArtLayers() []*ArtLayer {
return d.ArtLayers
}
func (d *Document) GetLayerSets() []*LayerSet {
return d.LayerSets
}
func (d *Document) Parent() Group {
return nil
}
// ActiveDocument returns a Document object from Photoshop's active document.
func ActiveDocument() (Document, error) {
byt, err := DoJs("getActiveDocument.jsx")
var d Document
err = json.Unmarshal(byt, &d)
fmt.Println(string(byt))
return d, err
}
func GetDocument() (*Document, error) {
byt, err := DoJs("getActiveDoc.jsx")
var d *Document
err = json.Unmarshal(byt, &d)
for _, lyr := range d.ArtLayers {
lyr.Parent = d
}
for _, set := range d.LayerSets {
set.parent = d
for _, lyr := range set.ArtLayers {
lyr.Parent = set
}
}
fmt.Println(string(byt))
return d, err
}
type ArtLayer struct { type ArtLayer struct {
Name string Name string
TextItem string // TextItem string
Bounds [2][2]int Bounds [2][2]int
LayerSet string Parent Group
Path string
Visiblity bool
} }
// func (a *ArtLayer) setParent(c Group) {
// ArtLayer.Parent = c
// }
// Layer returns an ArtLayer from the active document given a specified
// path string.
func Layer(path string) (ArtLayer, error) {
byt, err := DoJs("getLayer.jsx", JSLayer(path))
var out ArtLayer
err = json.Unmarshal(byt, &out)
if err != nil {
return ArtLayer{}, err
}
out.Path = path
return out, err
}
// SetVisible makes the layer visible.
func (a *ArtLayer) SetVisible() { func (a *ArtLayer) SetVisible() {
DoJs("setLayerVisibility.jsx", a.LayerSet+"/"+a.Name, "true") js := JSLayer(a.Path) + fmt.Sprintf(".visible=%s;", true)
DoJs("compilejs.jsx", js)
} }
// func (a *ArtLayer) Name() string { // Position moves the layer to pos(x, y), measuring from
// return a.name // the top or bottom left-hand corner.
// } // TODO: Improve
func (a *ArtLayer) Position(x, y int, align string) {
var lyrX, lyrY int
lyrX = a.Bounds[0][0]
if align != "bottom" {
lyrY = a.Bounds[0][1]
} else {
lyrY = a.Bounds[1][1]
}
byt, err := DoJs("moveLayer.jsx", JSLayer(a.Path), fmt.Sprint(x-lyrX), fmt.Sprint(y-lyrY))
if err != nil {
panic(err)
}
fmt.Println("byte", string(byt))
fmt.Println("bounds", a.Bounds)
json.Unmarshal(byt, &a)
fmt.Println("after", a.Bounds)
}
// func (a *ArtLayer) TextItem() string { type LayerSet struct {
// return a.textItem Name string
// } parent Group
ArtLayers []*ArtLayer
LayerSets []*LayerSet
}
func (l *LayerSet) GetArtLayers() []*ArtLayer {
return l.ArtLayers
}
func (l *LayerSet) GetLayerSets() []*LayerSet {
return l.LayerSets
}
func (l *LayerSet) Parent() Group {
return l.parent
}
func GetLayerSet(path string) (LayerSet, error) {
byt, err := DoJs("getLayerSet.jsx", JSLayer(path))
var out LayerSet
fmt.Println(string(byt)) // TODO: Debug
err = json.Unmarshal(byt, &out)
if err != nil {
return LayerSet{}, err
}
for _, lyr := range out.ArtLayers {
lyr.Parent = &out
}
return out, err
}