sketch-hq/SketchAPI

Text.Style.fontAxes issue

qjebbs opened this issue · 2 comments

error.zip

Above is a file reported by one my user, many of them encountered issues in this part.

How to reproduce

  1. Sketch prompts font is missing
  2. Replace the font with Work Sans-Medium

then, try:

    console.log(layer.style.fontAxes)
    console.log(JSON.stringify(layer.style.fontAxes))
    console.log(typeof (layer.style.fontAxes as any).Weight.value)

Output:

  { Weight: { id: 2003265652, min: 100, max: 900, value: 500 } }
  '{"Weight":{"id":{},"min":{},"max":{},"value":{}}}'
  'object'

Issues

  1. If I assign above layer.style to another layer, Sketch crashes instantly
  2. Text.Style.fontAxes doesn't have property Weight according to documentation
  3. All values provided by Sketch API should be JS type. But Weight.value is object (Obj-C?). So JSON.stringify doesn't work with it

No matter what has happened, I think the API should do the compatible magic to these circumstances.

PS

The screenshot of evaluate Object.keys() on null, happens on Text.Style.fontAxes=null(Obj-C type of null?), on uncertain conditions, I haven't find a way to reproduce.

I use following codes to bypass:

// prepare to copy from style source
let styleStr = JSON.stringify(layer.style);
let styleBase = JSON.parse(styleStr);
if (styleBase.fontAxes) {
    // fontAxes issue: https://github.com/sketch-hq/SketchAPI/issues/810
    styleBase.fontAxes = Object.assign({}, <FontAxes>{
        id: styleBase.fontAxes.id,
        min: styleBase.fontAxes.min,
        max: styleBase.fontAxes.max,
        value: styleBase.fontAxes.value
    })
} else {
    // bypass Sketch API evaluating Object.keys() on null fontAxes
    delete styleBase.fontAxes;
}
styleStr = JSON.stringify(styleBase);

// ...

newLayer.style = JSON.parse(styleStr)

This file shows more issue about fontAxes: error2.zip

run:

let layer = context.document.pages[0].layers[0].layers[0] as Text;
console.log(layer.style.fontAxes);

output:

  fontAxes: 
     { 'Optical Size': { id: 1869640570, min: 19.8999, max: 20, value: 19.8999 },
       GRAD: { id: 1196572996, min: 400, max: 1000, value: 400 },
       YAXS: { id: 1497454675, min: 400, max: 1000, value: 400 },
       Weight: { id: 2003265652, min: 1, max: 1000, value: 400 } } }

If I assign this style to another layer, Sketch crashes.