Added a TextItem Struct to Artlayers.

* Converted scripts to use the same getLayers() function from lib.js
This commit is contained in:
Unknown
2018-04-26 13:09:36 -04:00
parent e136ea8a83
commit 40cde546bc
8 changed files with 179 additions and 85 deletions

View File

@@ -122,14 +122,14 @@ func TestLayerSet(t *testing.T) {
} }
func TestLayer(t *testing.T) { func TestLayer(t *testing.T) {
_, err := Layer("Border/Inner Border") _, err := layer("Border/Inner Border")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
func TestMove(t *testing.T) { func TestMove(t *testing.T) {
lyr, err := Layer("Group 1/Layer 1") lyr, err := layer("Group 1/Layer 1")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -182,15 +182,10 @@ func TestColor(t *testing.T) {
} }
func TestApplyDataset(t *testing.T) { func TestApplyDataset(t *testing.T) {
out := []byte("done!\r\n") err := ApplyDataset("Anger")
ret, err := ApplyDataset(" Anger")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if string(ret) != string(out) {
fail := fmt.Sprintf("TestJS failed.\ngot:\t\"%s\"\nwant:\t\"%s\"", ret, out)
t.Fatal(fail)
}
} }
func TestDocumentLayerSet(t *testing.T) { func TestDocumentLayerSet(t *testing.T) {
@@ -248,6 +243,37 @@ func TestDoJs_HideLayer(t *testing.T) {
} }
} }
func TestTextItem(t *testing.T) {
// err := Open("F:\\GitLab\\dreamkeepers-psd\\Template009.1.psd")
// if err != nil {
// t.Fatal(err)
// }
d, err := ActiveDocument()
if err != nil {
t.Fatal(err)
}
for _, lyr := range d.ArtLayers() {
if lyr.Name() == "Text" {
lyr.SetText("Butts")
lyr.FmtText(0, 5, "Arial", "Regular")
lyr.FmtText(0, 3, "Arial", "Bold")
}
}
/* byt := []byte(`{"Name": "lyr", "TextItem": {"Contents": "lyr", "Size": 12.000, "Font": "ArialItalic"}}`)
lyr := &ArtLayer{}
// byt := []byte(`{"Name": "lyr"}`)
// lyr := &TextItem{}
err := lyr.UnmarshalJSON(byt)
fmt.Printf("%+v\n", lyr)
fmt.Println(lyr.TextItem)
if err != nil {
t.Fatal(err)
}
*/
}
func BenchmarkDoc_Go(b *testing.B) { func BenchmarkDoc_Go(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_, err := ActiveDocument() _, err := ActiveDocument()

View File

@@ -1,12 +1,12 @@
var start = parseInt(arguments[1]);
var end = parseInt(arguments[2]);
var fontName = arguments[3];
var fontStyle = arguments[4];
var colorArray = [0, 0, 0];
if(app.activeDocument.activeLayer.kind == LayerKind.TEXT){ if(app.activeDocument.activeLayer.kind == LayerKind.TEXT){
var activeLayer = app.activeDocument.activeLayer; var activeLayer = app.activeDocument.activeLayer;
var fontSize = activeLayer.textItem.size;
if(activeLayer.kind == LayerKind.TEXT){ if(activeLayer.kind == LayerKind.TEXT){
var start = parseInt(arguments[1]);
var end = parseInt(arguments[2]);
var fontName = arguments[3];
var fontStyle = arguments[4];
var fontSize = activeLayer.textItem.size;
var colorArray = [0, 0, 0];
if((activeLayer.textItem.contents != "")&&(start >= 0)&&(end <= activeLayer.textItem.contents.length)){ if((activeLayer.textItem.contents != "")&&(start >= 0)&&(end <= activeLayer.textItem.contents.length)){
var idsetd = app.charIDToTypeID( "setd" ); var idsetd = app.charIDToTypeID( "setd" );
var action = new ActionDescriptor(); var action = new ActionDescriptor();

View File

@@ -3,25 +3,8 @@ var stdout = newFile(arguments[0]);
var doc = app.activeDocument; var doc = app.activeDocument;
stdout.writeln(('{"Name": "'+doc.name+'", "Height":'+doc.height+ stdout.writeln(('{"Name": "'+doc.name+'", "Height":'+doc.height+
', "Width":'+doc.width+', "ArtLayers": [').replace(/ px/g, "")); ', "Width":'+doc.width+', "ArtLayers": [').replace(/ px/g, ""));
function layers(lyrs) {
if (typeof lyrs === 'undefined') stdout.writeln(layers(doc.artLayers))
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+', "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)
stdout.writeln('], "LayerSets": ['); stdout.writeln('], "LayerSets": [');
function lyrSets(sets, nm) { function lyrSets(sets, nm) {
if (typeof sets === 'undefined') if (typeof sets === 'undefined')

View File

@@ -2,6 +2,9 @@
app.displayDialogs=DialogModes.NO app.displayDialogs=DialogModes.NO
var stdout = newFile(arguments[0]); var stdout = newFile(arguments[0]);
var lyr = eval(arguments[1]); var lyr = eval(arguments[1]);
var lyrs = [lyr];
stdout.writeln(layers(lyrs))
/*
stdout.write(('{"Name":"' + lyr.name + '","Bounds":[[' + lyr.bounds[0] + ',' + stdout.write(('{"Name":"' + lyr.name + '","Bounds":[[' + lyr.bounds[0] + ',' +
lyr.bounds[1] + '],[' + lyr.bounds[2] + ',' + lyr.bounds[1] + '],[' + lyr.bounds[2] + ',' +
lyr.bounds[3] + ']],"Visible":' + lyr.visible+',"Text":').replace(/ px/g, "")); lyr.bounds[3] + ']],"Visible":' + lyr.visible+',"Text":').replace(/ px/g, ""));
@@ -12,3 +15,4 @@ else
stdout.write(null) stdout.write(null)
stdout.writeln('}') stdout.writeln('}')
stdout.close(); stdout.close();
*/

View File

@@ -3,6 +3,10 @@ var stdout = newFile(arguments[0]);
var set = eval(arguments[1]); var set = eval(arguments[1]);
stdout.writeln('{"Name": "'+set.name+'", "Visible": '+ set.visible +', "ArtLayers":['); stdout.writeln('{"Name": "'+set.name+'", "Visible": '+ set.visible +', "ArtLayers":[');
stdout.flush(); stdout.flush();
var str = layers(set.artLayers);
str = str.replace(/\r/g, "\\r");
stdout.writeln(str);
/*
for (var i = 0; i < set.artLayers.length; i++) { for (var i = 0; i < set.artLayers.length; i++) {
var lyr = set.artLayers[i]; var lyr = set.artLayers[i];
stdout.write(('{"Name":"' + lyr.name + '", "Bounds": [[' + lyr.bounds[0] + ',' + stdout.write(('{"Name":"' + lyr.name + '", "Bounds": [[' + lyr.bounds[0] + ',' +
@@ -17,6 +21,7 @@ for (var i = 0; i < set.artLayers.length; i++) {
stdout.writeln(","); stdout.writeln(",");
stdout.flush(); stdout.flush();
} }
*/
stdout.writeln("]"); stdout.writeln("]");
stdout.write(', "LayerSets": [') stdout.write(', "LayerSets": [')
for (var i = 0; i < set.layerSets.length; i++) { for (var i = 0; i < set.layerSets.length; i++) {
@@ -27,13 +32,6 @@ for (var i = 0; i < set.layerSets.length; i++) {
stdout.flush() stdout.flush()
} }
stdout.writeln(']') stdout.writeln(']')
// app.activeDocument.activeLayer=set;
// set.merge();
// set=eval(arguments[2]);
stdout.write(', "Bounds": [[],[]]'); stdout.write(', "Bounds": [[],[]]');
// stdout.write((', "Bounds": [[' + set.bounds[0] + ',' +
// set.bounds[1] + '],[' + set.bounds[2] + ',' +
// set.bounds[3] + ']]').replace(/ px/g, ""));
stdout.write("}"); stdout.write("}");
// Undo();
stdout.close(); stdout.close();

View File

@@ -98,3 +98,25 @@ function setFormatting(start, end, fontName, fontStyle, fontSize, colorArray) {
} }
} }
} }
function layers(lyrs) {
if (typeof lyrs === 'undefined')
return;
var str = "";
for (var i = 0; i < lyrs.length; i++) {
var lyr = lyrs[i];
str += ('{"Name":"' + lyr.name + '", "Bounds": [[' + lyr.bounds[0] + ',' +
lyr.bounds[1] + '],[' + lyr.bounds[2] + ',' +
lyr.bounds[3] + ']], "Visible": ' + lyr.visible+', "TextItem": ').replace(/ px/g, "");
if (lyr.kind == LayerKind.TEXT) {
str += ('{"Contents": "'+lyr.textItem.contents+'",').replace(/\r/g, '\\r');
str += (' "Size": '+lyr.textItem.size+',').replace(/ pt/g, '');
str += ' "Font": "'+lyr.textItem.font+'"}\n'
} else
str += "null";
str += "}";
if (i+1 != lyrs.length)
str += ',';
}
return str
}

View File

@@ -1,10 +1,10 @@
#include lib.js #include lib.js
// var saveFile = File(arguments[0]); // var saveFile = File(arguments[0]);
var arg = 'app.activeDocument.layerSets.getByName("ResolveGem");'; var arg = 'app.activeDocument.layerSets.getByName("Text").artLayers.getByName("short");';
var set = eval(arg); var set = eval(arg);
set.visible=false; // set.textItem.size=10;
alert(set.visible) alert(set.textItem.font)
// var doc=app.activeDocument // var doc=app.activeDocument
// doc.layerSets.getByName("ResolveGem").merge(); // doc.layerSets.getByName("ResolveGem").merge();
// alert(doc.artLayers.getByName("ResolveGem").bounds); // alert(doc.artLayers.getByName("ResolveGem").bounds);

View File

@@ -180,16 +180,16 @@ func (d *Document) Dump() {
// ArtLayer reflects certain values from an Art Layer in a Photoshop document. // ArtLayer reflects certain values from an Art Layer in a Photoshop document.
// //
// TODO: Make TextLayer a subclass of ArtLayer. // TODO: (2) Make TextLayer a subclass of ArtLayer.
type ArtLayer struct { type ArtLayer struct {
name string // The layer's name. name string // The layer's name.
Text *string // The contents of a text layer.
bounds [2][2]int // The layers' corners. bounds [2][2]int // The layers' corners.
parent Group // The LayerSet/Document this layer is in. parent Group // The LayerSet/Document this layer is in.
visible bool // Whether or not the layer is visible. visible bool // Whether or not the layer is visible.
current bool // Whether we've checked this layer since we loaded from disk. current bool // Whether we've checked this layer since we loaded from disk.
Color // The layer's color overlay. Color // The layer's color overlay.
*Stroke // The layer's stroke. *Stroke // The layer's stroke.
*TextItem //The layer's text, if it's a text layer.
} }
// Bounds returns the furthest corners of the ArtLayer. // Bounds returns the furthest corners of the ArtLayer.
@@ -207,23 +207,12 @@ type ArtLayerJSON struct {
Color [3]int Color [3]int
Stroke [3]int Stroke [3]int
StrokeAmt float32 StrokeAmt float32
Text *string TextItem *TextItem
}
func (a *ArtLayer) SetText(txt string) {
lyr := strings.TrimRight(JSLayer(a.Path()), ";")
js := fmt.Sprintf("%s.textItem.contents='%s';", lyr, txt)
_, err := DoJs("compilejs.jsx", js)
if err != nil {
a.Text = &txt
}
a.Refresh()
} }
// MarshalJSON implements the json.Marshaler interface, allowing the ArtLayer to be // MarshalJSON implements the json.Marshaler interface, allowing the ArtLayer to be
// saved to disk in JSON format. // saved to disk in JSON format.
func (a *ArtLayer) MarshalJSON() ([]byte, error) { func (a *ArtLayer) MarshalJSON() ([]byte, error) {
// txt := strings.Replace(*a.Text, "\r", "\\r", -1)
return json.Marshal(&ArtLayerJSON{ return json.Marshal(&ArtLayerJSON{
Name: a.name, Name: a.name,
Bounds: a.bounds, Bounds: a.bounds,
@@ -231,7 +220,7 @@ func (a *ArtLayer) MarshalJSON() ([]byte, error) {
Color: a.Color.RGB(), Color: a.Color.RGB(),
Stroke: a.Stroke.RGB(), Stroke: a.Stroke.RGB(),
StrokeAmt: a.Stroke.Size, StrokeAmt: a.Stroke.Size,
Text: a.Text, TextItem: a.TextItem,
}) })
} }
@@ -245,11 +234,11 @@ func (a *ArtLayer) UnmarshalJSON(b []byte) error {
a.Color = RGB{tmp.Color[0], tmp.Color[1], tmp.Color[2]} 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.Stroke = &Stroke{tmp.StrokeAmt, RGB{tmp.Stroke[0], tmp.Stroke[1], tmp.Stroke[2]}}
a.visible = tmp.Visible a.visible = tmp.Visible
if tmp.Text != nil {
// s := strings.Replace(*tmp.Text, "\\r", "\r", -1)
a.Text = tmp.Text
}
a.current = false a.current = false
a.TextItem = tmp.TextItem
if a.TextItem != nil {
a.TextItem.parent = a
}
return nil return nil
} }
@@ -371,18 +360,6 @@ func (a *ArtLayer) Path() string {
return fmt.Sprintf("%s%s", a.parent.Path(), a.name) return fmt.Sprintf("%s%s", a.parent.Path(), a.name)
} }
// TODO: Documentation for Format()
func (a *ArtLayer) Format(start, end int, font, style string) {
if !a.Visible() {
return
}
_, err := DoJs("fmtText.jsx", fmt.Sprint(start), fmt.Sprint(end),
font, style)
if err != nil {
log.Panic(err)
}
}
// Layer returns an ArtLayer from the active document given a specified // Layer returns an ArtLayer from the active document given a specified
// path string. // path string.
func layer(path string) (ArtLayer, error) { func layer(path string) (ArtLayer, error) {
@@ -419,8 +396,6 @@ func (a *ArtLayer) Visible() bool {
// SetPos snaps the given layer boundry to the given point. // SetPos snaps the given layer boundry to the given point.
// Valid options for bound are: "TL", "TR", "BL", "BR" // Valid options for bound are: "TL", "TR", "BL", "BR"
//
// TODO: Test TR and BR
func (a *ArtLayer) SetPos(x, y int, bound string) { func (a *ArtLayer) SetPos(x, y int, bound string) {
if !a.visible || (x == 0 && y == 0) { if !a.visible || (x == 0 && y == 0) {
return return
@@ -462,7 +437,10 @@ func (a *ArtLayer) Refresh() error {
tmp.SetParent(a.Parent()) tmp.SetParent(a.Parent())
a.name = tmp.name a.name = tmp.name
a.bounds = tmp.bounds a.bounds = tmp.bounds
a.Text = tmp.Text a.TextItem = tmp.TextItem
if a.TextItem != nil {
a.TextItem.parent = a
}
a.parent = tmp.Parent() a.parent = tmp.Parent()
a.visible = tmp.visible a.visible = tmp.visible
a.current = true a.current = true
@@ -721,3 +699,86 @@ func (l *LayerSet) Refresh() {
l.visible = tmp.visible l.visible = tmp.visible
l.current = true l.current = true
} }
type TextItem struct {
contents string
size float64
// color Color
font string
parent *ArtLayer
}
type TextItemJSON struct {
Contents string
Size float64
// Color [3]int
Font string
}
func (t *TextItem) Contents() string {
return t.contents
}
func (t *TextItem) Size() float64 {
return t.size
}
// MarshalJSON implements the json.Marshaler interface, allowing the TextItem to be
// saved to disk in JSON format.
func (t *TextItem) MarshalJSON() ([]byte, error) {
return json.Marshal(&TextItemJSON{
Contents: t.contents,
Size: t.size,
// Color: t.color.RGB(),
Font: t.font,
})
}
func (t *TextItem) UnmarshalJSON(b []byte) error {
tmp := &TextItemJSON{}
if err := json.Unmarshal(b, &tmp); err != nil {
return err
}
t.contents = tmp.Contents
t.size = tmp.Size
// t.color = RGB{tmp.Color[0], tmp.Color[1], tmp.Color[2]}
t.font = tmp.Font
return nil
}
func (t *TextItem) SetText(txt string) {
if txt == t.contents {
return
}
lyr := strings.TrimRight(JSLayer(t.parent.Path()), ";")
js := fmt.Sprintf("%s.textItem.contents='%s';", lyr, txt)
_, err := DoJs("compilejs.jsx", js)
if err != nil {
t.contents = txt
}
}
func (t *TextItem) SetSize(s float64) {
if t.size == s {
return
}
lyr := strings.TrimRight(JSLayer(t.parent.Path()), ";")
js := fmt.Sprintf("%s.textItem.size=%f;", lyr, s)
_, err := DoJs("compilejs.jsx", js)
if err != nil {
t.size = s
}
}
// TODO: Documentation for Format(), make to textItem
func (t *TextItem) Fmt(start, end int, font, style string) {
var err error
if !t.parent.Visible() {
return
}
_, err = DoJs("fmtText.jsx", fmt.Sprint(start), fmt.Sprint(end),
font, style)
if err != nil {
log.Panic(err)
}
}