Updates, Improvements, and Fixes

* Moved scripts / runner to separate package
    allows future replacement with PS Plugin.
* Fixed issues with Refresh and removed "layer" function.
* Added github documentation via godocdown.
* Reduced number of calls to Panic.
* Updated Tests
* Updated documentation.
* Fixed warnings.
* .gitignore now ignores .test and .out files.
This commit is contained in:
Spencer Brower
2018-06-18 23:51:52 -04:00
parent 47f24275b1
commit 206b94dcac
43 changed files with 450 additions and 298 deletions

View File

@@ -7,26 +7,30 @@ import (
"strings"
)
// TextItem holds the text element of a TextLayer.
type TextItem struct {
contents string
size float64
// color Color
font string
parent *ArtLayer
font string
parent *ArtLayer
}
// TextItemJSON is the exported version of TextItem
// that allows it to be marshaled and unmarshaled
// into JSON.
type TextItemJSON struct {
Contents string
Size float64
// Color [3]int
Font string
Font string
}
func (t *TextItem) Contents() string {
// Contents returns the raw text of the TextItem.
func (t TextItem) Contents() string {
return t.contents
}
func (t *TextItem) Size() float64 {
// Size returns the font size of the TextItem
func (t TextItem) Size() float64 {
return t.size
}
@@ -36,34 +40,38 @@ func (t *TextItem) MarshalJSON() ([]byte, error) {
return json.Marshal(&TextItemJSON{
Contents: t.contents,
Size: t.size,
// Color: t.color.RGB(),
Font: t.font,
Font: t.font,
})
}
func (t *TextItem) UnmarshalJSON(b []byte) error {
// UnmarshalJSON loads the JSON data into the TextItem
func (t *TextItem) UnmarshalJSON(data []byte) error {
tmp := &TextItemJSON{}
if err := json.Unmarshal(b, &tmp); err != nil {
if err := json.Unmarshal(data, &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
}
// SetText sets the text to the given string.
func (t *TextItem) SetText(txt string) {
if txt == t.contents {
return
}
var err error
lyr := strings.TrimRight(JSLayer(t.parent.Path()), ";")
bndtext := "[[' + lyr.bounds[0] + ',' + lyr.bounds[1] + '],[' + lyr.bounds[2] + ',' + lyr.bounds[3] + ']]"
js := fmt.Sprintf(`%s.textItem.contents='%s';var lyr = %[1]s;stdout.writeln(('%[3]s').replace(/ px/g, ''));`,
lyr, txt, bndtext)
byt, err := DoJs("compilejs.jsx", js)
var byt []byte
if byt, err = DoJS("compilejs.jsx", js); err != nil {
log.Panic(err)
}
var bnds *[2][2]int
json.Unmarshal(byt, &bnds)
err = json.Unmarshal(byt, &bnds)
if err != nil || bnds == nil {
log.Println("text:", txt)
log.Println("js:", js)
@@ -74,26 +82,26 @@ func (t *TextItem) SetText(txt string) {
t.parent.bounds = *bnds
}
// SetSize sets the size of the TextItem's font.
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)
_, err := DoJS("compilejs.jsx", js)
if err != nil {
t.size = s
}
}
// TODO: Documentation for Format(), make to textItem
// Fmt applies the given font and style to all characters
// in the range [start, end].
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)
_, err := DoJS("fmtText.jsx", fmt.Sprint(start), fmt.Sprint(end), font, style)
if err != nil {
log.Panic(err)
}