A strage error in an ImageJ script in JavaScript

javascript
textroi

#1

I’m trying to write a script in JavaScript and the goal here is to change the color of TextRois.

In the script below; I can use the getName() method of TextRoi without an error, but when I try to use the getSize() method, an error is issued and says TypeError: rois[i].getSize() is not a function. and I’m very much perplexed about this.

# @ImagePlus imp
# @int (style="slider", min=1, max=300) fontSize


importClass(Packages.ij.IJ);
importClass(Packages.ij.plugin.frame.RoiManager);
importClass(Packages.ij.gui.TextRoi);

var ol1 = imp.getOverlay();

var rois = ol1.toArray();

// IJ.log('Number of ROIs:' + rois.length);
print('Number of ROIs:' + rois.length);

var font1;
var style1;
var oldSize1;
for (i = 0; i < 1; i++) {
    if (rois[i] instanceof TextRoi) {
		print(rois[i].getName());
	
    	rois[i].setName(rois[i].getText()); // needs to be getText()
		// IJ.log(rois[i].getText());
		print(rois[i].getName());

		print(rois[i].getSize());
    	
    }
}

I’ve tried to do similar things using ImageJ-MATLAB and rois(1).getSize() worked well in MATLAB. Why it does not work in JavaScript then?

Is that a bug?


Translation of the above JavaScript code to Python works as expected.


# @ImagePlus imp
# @int (style="slider", min=1, max=300) fontSize

from ij import IJ
from ij.plugin.frame import RoiManager
from ij.gui import TextRoi

ol1 = imp.getOverlay();

rois = ol1.toArray();

print('Number of ROIs:' + str(len(rois)));

for i in range(0, len(rois)):
    if type(rois[i]).__name__ == "TextRoi":
		print(rois[i].getName());
		rois[i].setName(rois[i].getText()); # needs to be getText()
		# IJ.log(rois[i].getText());
		print(rois[3].getName());
		print(5)
		print(rois[3].getSize());

#2

The getSize() method in the TextRoi class is a static method that returns the default font size. It should have been called getDefaultFontSize(). You call static methods using the class name:

defaultFontSize = TextRoi.getSize();

This example shows how to get the default font size and the roi (instance) font size:

  font = new Font("SansSerif", Font.PLAIN, 24);
  roi = new TextRoi(100, 100, "Some text", font);
  print("Default font size: "+TextRoi.getSize());
  print("Instance font size: "+roi.getCurrentFont().getSize());

#3

I see! Now it makes sense. I overlooked static. The getCurrentFont() method was what I was thinking of.

By the way, I’m glad to finally “meet” the founder! Thank you for your invention.

ChangeTextRoiFontSize.js

# @ImagePlus imp
# @int (style="slider", min=1, max=300) fontSize

importClass(Packages.ij.IJ);
importClass(Packages.ij.plugin.frame.RoiManager);
importClass(Packages.ij.gui.TextRoi);

var ol1 = imp.getOverlay();

var rois = ol1.toArray();

// IJ.log('Number of ROIs:' + rois.length);
print('Number of ROIs:' + rois.length);

var thisFont;
var re = new RegExp('\n');
for (i = 0; i < rois.length; i++) {
    if (rois[i] instanceof TextRoi) {
    	rois[i].setName(rois[i].getText()); // needs to be getText()

		thisFont = rois[i].getCurrentFont();

		//print(rois[i].getName().replace(re,'') + ', size: ' + thisFont.getSize().toString());

		//newFont = thisFont.deriveFont(fontSize); //TODO not working
		newFont = thisFont.deriveFont(thisFont.getStyle(),fontSize);
		
		rois[i].setCurrentFont(newFont);
		TextRoi.setFont(newFont.getFontName(),fontSize,newFont.getStyle(),true); // default

		thisFont = rois[i].getCurrentFont();

		print(rois[i].getName().replace(re,'') + ', size: ' + thisFont.getSize().toString());
    }
}
		

IJ.log('Font size was changed to ' + fontSize.toString());
IJ.log('Default Font size was changed to ' + fontSize.toString());