basiljs/basil.js

b.text with emoji crashes InDesign

Closed this issue Β· 18 comments

Running b.text() with an emoji or other non-typical UTF-8 character immediately crashes InDesign.

Lastest master
InDesign CC 13.1 x64

can you provide some example code and test this one:

var doc = app.documents.add();
var tf = doc.pages[0].textFrames.add({
    geometricBounds:[0,0,100,100],
    contents : "πŸš€"});

Tested it with the latest development version of basil.js from develop branch

//@include "path/to/basil.js"
function setup(){
text("πŸš€", 0, 0, 100, 100);
    }

That works fine.

Tested with the latest state from master branch ( /* Basil.js v1.1.0 2016.11.11-15:33:03 */):

//@include "path/to/basil.js"
function setup(){
b.text("πŸš€", 0, 0, 100, 100);
    }

b.go()

also works.

I simply downloaded the current master branch (yesterday) to a zip from the GitHub interface.
Here is a simplified version of my script:

#include "../../bundle/basil.js";

var msgA;

function setup() {

  b.println('loading files');
  msgA = b.loadStrings("myfile.txt");
  b.println('loaded files');

  b.clear (b.doc());

  for (var i = 0; i < msgA.length; i+=10) {
    b.addPage();
    b.canvasMode(b.MARGIN)
    for (var j = 0; j < 11; j++) {
        b.println(i+j);
        b.fill(0);
        b.textSize(12);
        b.textAlign(Justification.LEFT_ALIGN, VerticalJustification.CENTER_ALIGN );
        var x = 0;
        var y = j*(b.height/10)
        var w = b.width
        var h = b.height/10
        b.text(msgA[i+j], x,y,w,h);
    };
    b.canvasMode(b.PAGE)    
  };
}

b.go(); 

And an input file (UTF-8):

testing123πŸ˜‚
funπŸ’ͺπŸ»πŸ€“πŸ™
testest πŸ˜‰lorem
Über
accΓ©nt
éééé
weird char?😊🧁

The emoji cause it to crash. Accented and related characters cause it to cut off a line or display it with all the letters on top of each other.

Okay. I can reproduce your error. Here is my error log.

crash.log

trych commented

Just one quick question, does the same thing happen, when you don't include the last emoji? Because that one cannot even be displayed on my system (macOS 10.13.6). Maybe that's the one that causes the issue?

No, I have the line number print in there so I could tell when it crashed. It crashed on the first one (valid emoji). Also, it only crashes on b.text(), not when simply loading the file.

Thank you for taking a look into this so quickly!

@fabianmoronzirfas were you able to reproduce the accent display issue?

For the time being and for my own testing of my project, I removed the emojis and other characters from my file and it all works fine.

trych commented

@DaAwesomeP Ok, maybe Fabian can find something out, otherwise I will have a look at a later point (I'll be really busy for the next few days). But thanks for reporting, good to get some feedback from the outside.

Also, I am not sure, if this is relevant for you: We are working on a basil 2.0 version currently. While it is not yet released, it is in many ways already much improved over the version in the master branch. If you feel adventurous, you can download it from the develop branch. It simplyfies the syntax (main part is, it skips the b. and the b.go() command) and has lot's of new functions. Here's a (wip) reference reflecting this develop version and in the develop branch of the example repo are some example scripts using this new syntax.

@trych Thanks! I'll give it a whirl when I get a chance!

@trych @DaAwesomeP I'm also out for today. I'll try to take a look tomorrow. might be something with

  if (!(isString(txt) || isNumber(txt))) warning("b.text(), the first parameter has to be a string! But is something else: " + typeof txt + ". Use: b.text(txt, x, y, w, h)");
  var textFrame = currentPage().textFrames.add(currentLayer());
  with (textFrame) {
    contents = txt.toString();
    geometricBounds = [y, x, (y + h), (x + w)];
    textFramePreferences.verticalJustification = currYAlign;
  }

Okay. I boiled it down to this.

If the text file contains a character follow by a emoji it crashes. E.g.

1πŸ˜‚

If it has an emoji followed by a character it does not crash.

πŸ˜‚1

This is the code I am using. It is nothing we can fix. It is related to InDesign itself.

function myloadStrings(path) {
  var lines = [];
  var file = File(path);
  if (file.exists) {
    file.open('r');
    while (!file.eof) {
      lines.push(file.readln());
    }
    file.close();
  }
  $.writeln(lines);
  return lines;
}
function main() {
  $.writeln('loading files');
  var lines = myloadStrings( File($.fileName).parent +"/data/crash.txt");
  $.writeln('loaded lines');
  $.writeln("the lines " + lines.toString());
  $.writeln("the lines.length " + lines.length);
  // alert(lines);
  var doc = app.activeDocument;
  var pg = doc.pages[0];
  var tf = pg.textFrames.add({
    contents: lines[0].toString(),
    geometricBounds: [0,0,10,100]
  });
  $.writeln('done');
}
main();

Attached is also a .zip with my setup.

crash.zip

I guess we should write a bug report. https://indesign.uservoice.com/forums/601180-adobe-indesign-bugs

What is the probability that Adobe actually fixes it?

I suppose as a workaround I could convert my files to a JSON/JS array and paste it directly into the file.

Good question. I guess they have something in the pipeline.

Wink wink.

I suppose as a workaround I could convert my files to a JSON/JS array and paste it directly into the file

Yes you could try that. Another thing that could help if you explicitly set the encoding of the file while loading it

Sorry autocorrect πŸ‘†πŸ½

Another thing that could help if explicitly seeing the encoding of the file while loading it

How do I set the encoding as I load the file?

From the Javascript Tools Guide.pdf

The following list of names is a basic set of encoding names supported by the File object. Some of the character encoders are built in, while the operating system is queried for most of the other encoders. Depending on the language packs installed, some of the encodings may not be available. Names that refer to the same encoding are listed in one line. Underlines are replaced with dashes before matching an encoding name.

The File object processes an extended Unicode character with a value greater that 65535 as a Unicode
surrogate pair (two characters in the range between 0xD700-0xDFFF).
Built-in encodings are:

US-ASCII, ASCII,ISO646-US,I SO-646.IRV:1991, ISO-IR-6,
ANSI-X3.4-1968,CP367,IBM367,US,ISO646.1991-IRV
UCS-2,UCS2, ISO-10646-UCS-2
UCS2LE,UCS-2LE,ISO-10646-UCS-2LE
UCS2BE,UCS-2BE,ISO-10646-UCS-2BE
UCS-4,UCS4, ISO-10646-UCS-4
UCS4LE,UCS-4LE,ISO-10646-UCS-4LE
UCS4BE,UCS-4BE,ISO-10646-UCS-4BE
UTF-8,UTF8,UNICODE-1-1-UTF-8,UNICODE-2-0-UTF-8,X-UNICODE-2-0-UTF-8
UTF16,UTF-16,ISO-10646-UTF-16
UTF16LE,UTF-16LE,ISO-10646-UTF-16LE
UTF16BE,UTF-16BE,ISO-10646-UTF-16BE
CP1252,WINDOWS-1252,MS-ANSI
ISO-8859-1,ISO-8859-1,ISO-8859-1:1987,ISO-IR-100,LATIN1
MACINTOSH,X-MAC-ROMAN
BINARY

The code should be something like this.

function myloadStrings(file) {
  var lines = [];
  if (file.exists) {
    file.open('r');
    while (!file.eof) {
      lines.push(file.readln());
    }
    file.close();
  }
  $.writeln(lines);
  return lines;
}
function main() {
  $.writeln('loading files');
  var txtFile = File(File($.fileName).parent +"/data/crash.txt");
  txtFile.encoding = 'UTF8'; // <----SET THE ENCODING
  var lines = myloadStrings(txtFile);
  $.writeln('loaded lines');
  $.writeln("the lines " + lines.toString());
  $.writeln("the lines.length " + lines.length);
  // alert(lines);
  var doc = app.activeDocument;
  var pg = doc.pages[0];
  var tf = pg.textFrames.add({
    contents: lines[0].toString(),
    geometricBounds: [0,0,10,100]
  });
  $.writeln('done');
}
main();

Just tested it with UTF8 still crashes.