1

I am working with imageDraw and getting an odd error. If I just plugin values and don't try to use the ones I am retrieving from elsewhere, it works, but I have to draw the values for the margins and line height from other places and calculate stuff.

 x = 50;
 y = 800;
 newImg = imageNew("", x, y);
 imageSetAntialiasing(newImg, true);
 setup['size'] = lineHeight*dpi;
 setup['font'] = "Arial";
 imageDrawText(newImg,img.text,topMargin,leftMargin,setup);

When I put each element in imageDrawText on a separate line, the error points to the attribute collection (setup). I did try this

setup['size'] = "#lineHeight*dpi#";

but it didn't work either.

The full error message at the top of the debug:

Error Occurred While Processing Request

Error casting an object of type java.lang.Double cannot be cast to java.lang.String to an incompatible type. This usually indicates a programming error in Java, although it could also mean you have tried to use a foreign object in a different way than it was designed. java.lang.Double cannot be cast to java.lang.String

SOS
  • 6,430
  • 2
  • 11
  • 29
  • What are the actual values used that cause that error? It's hard to tell as there are also variables missing in the example (lineHeight, dpi, etc..). Could you update it and make it into a [MCVE]? – SOS Jul 04 '19 at 18:35
  • 1
    FWIW, I couldn't reproduce your error with CF2016 or 2018 which suggests it's something about the *specific* values used. That's why it would be helpful to flesh a complete example which reproduces the issue - for the next person that runs into the same issue :-) – SOS Jul 04 '19 at 19:00

2 Answers2

4

That's a bug in older ColdFusion versions (before ColdFusion 2016), because the size attribute is explicitly casted: (String)size. And even if you pass the value as String, your value may not contain decimal places, because ColdFusion attempts to parse the value as Integer: Integer.parseInt((String)size)

// works
setup['size'] = "12";

// works, because literal numbers are casted to String
setup['size'] = 12;

// DOES NOT work, because any math calculation results into a Double
setup['size'] = 12 * 1;

// DOES NOT work, because this is a Double
setup['size'] = 12.1;

Your solution using setup['size'] = toString(int(...)); is the correct workaround for this bug. int() to make sure you end up without decimal places (preventing NumberFormatException) and toString() to make sure you are passing a String (preventing ClassCastException).

(This post is more like a remark, but too long for a comment. Feel free to accept your own answer.)

Alex
  • 7,743
  • 1
  • 18
  • 38
  • 2
    (edit) .. filed a bug new report, just in case https://tracker.adobe.com/#/view/CF-4204709 – SOS Jul 04 '19 at 22:09
1

There is something about asking for help that opens up channels for me. Found a solution. The issue was indeed the setup.size part of the attribute collection. Here is what worked:

setup['size'] = toString(int(lineHeight*dpi));

Don't know why it wants that to be a string specifically. Seems kind of dumb because we are using it as a number.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72