StyleRanges stops counting length

I am trying to split styleRanges line by line. It was mostly successful. However, if the number of characters increases, the “character length” and the “total of styleRanges length” do not match, and a new array cannot be created correctly.

How can I get the correct styleRanges length? Here is sample code.

function lengthTest(selection) {
  const textNode = selection.items[0] ;
  
  const contents = textNode.text ;
  const textLength = contents.length ;
  
  const styleRanges = textNode.styleRanges ;
  let styleLength = 0 ;
  styleRanges.forEach((aItem) => {styleLength += aItem.length ;}) ;
  
  console.log(`textLength: ${textLength}, styleLength: ${styleLength}, os: ${os.platform()} ${os.release()}, appVersion: ${app.version}, contents: 
${contents}
styleRanges: `) ;
  console.log(styleRanges) ;
}

And results.

textLength: 169, styleLength: 67, os: darwin 17.7.0, appVersion: 22.3.12.2, contents: 
山路を登りながら、こう考えた。
情に棹させば流される。智に働けば角が立つ。どこへ越しても住みにくいと悟った時、詩が生れて、画が出来る。情に棹させば流される。智に働けば角が立つ。どこへ越しても住みにくいと悟った時、詩が生れて、画が出来る。情に棹させば流される。智に働けば角が立つ。どこへ越しても住みにくいと悟った時、詩が生れて、画が出来る。
styleRanges: 
[ { length: 2,
    linkId: '',
    fontFamily: 'Tsukushi A Round Gothic',
    fontStyle: 'Bold',
    fontSize: 50,
    charSpacing: 0,
    underline: true,
    textTransform: 'none',
    textScript: 'none',
    fill: { value: 4285117696 },
    strikethrough: false },
  { length: 1,
    linkId: '',
    fontFamily: 'Tsukushi A Round Gothic',
    fontStyle: 'Bold',
    fontSize: 36,
    charSpacing: 0,
    underline: true,
    textTransform: 'none',
    textScript: 'none',
    fill: { value: 4279386019 },
    strikethrough: false },
  { length: 1,
    linkId: '',
    fontFamily: 'Tsukushi A Round Gothic',
    fontStyle: 'Bold',
    fontSize: 50,
    charSpacing: 0,
    underline: true,
    textTransform: 'none',
    textScript: 'none',
    fill: { value: 4285117696 },
    strikethrough: false },
  { length: 12,
    linkId: '',
    fontFamily: 'Tsukushi A Round Gothic',
    fontStyle: 'Bold',
    fontSize: 36,
    charSpacing: 0,
    underline: true,
    textTransform: 'none',
    textScript: 'none',
    fill: { value: 4279386019 },
    strikethrough: false },
  { length: 51,
    linkId: '',
    fontFamily: 'A-OTF Ryumin Pro',
    fontStyle: 'R-KL',
    fontSize: 24,
    charSpacing: 0,
    underline: false,
    textTransform: 'none',
    textScript: 'none',
    fill: { value: 4282071867 },
    strikethrough: false } ]

Quote from the documentation:

Any characters past the end of the last range use the same style as the last range.

(https://adobexdplatform.com/plugin-docs/reference/scenegraph.html#textstyleranges--arraylengthnumber-fontfamilystring-fontstylestring-fontsizenumber-fillcolor-charspacingnumber-underlineboolean-strikethroughboolean-texttransformstring-textscriptstring)

Therefore, it is to be expected that sometimes, the lengths won’t match, in which case the last styleRange will get used for remaining characters…

Hope this helps :wink:

1 Like

My expression may have been misleading. I meant that I can’t get the correct length when there were a lot of characters.

Texts are added manually and the above plugin does not edit the characters at all. So the length of the last styleRange is expected to return an exact number.

However, if the problem is in the last styleRange, I can solve it by filling in the gap myself. Thank you for the hint.

1 Like