add stroke to text (outline) with Coldfusion and Java (classes/methods)

I've been trying to add stroke to text using Coldfusion code and Java classes / methods. I found some pointers on Leigh's old blog (cfsearching) and have been spending a lot of my free time reading more about Java objects, how to call Java class/method .etc on Ben Nadel's blog. I have a lot to learn on this and I would be happy to do so with your help :)

I put together a test code, here is what I have so far

<cfset text = "This is just a text sample">
<cfset img = imageNew("",500,500,"rgb","blue")>
<cfset graphics = ImageGetBufferedImage(img).getGraphics()>
<cfset renderContext = graphics.getFontRenderContext()>

<!---get font details--->
<cfset Font = createObject("java", "java.awt.Font")>
<cfset NewFont = Font.init( "Arial", Font.BOLD, javacast("int", 40))>
<cfset txtLayout = createObject("java", "java.awt.font.TextLayout").init( text,NewFont,renderContext)>
<cfset txtBounds = txtLayout.getBounds()>
<cfset txtWidth = txtBounds.getWidth()>
<cfset txtHeight = txtBounds.getHeight()>
<cfset shapeObj = createObject("java", "java.awt.geom.AffineTransform").init()>
<cfset shape = txtLayout.getOutline(shapeObj)>

<!---set stroke  ---> 
<cfset color = createObject("java", "java.awt.Color")> 
<cfset stroke = createObject("java", "java.awt.BasicStroke").init(10)> 
<cfset strokeWidth = stroke.getLineWidth()>
<cfset graphics.setStroke( stroke )>
<cfset graphics.setColor( color.decode("##ff0600"))>

<!---write on image--->
<cfset attr = { font="Arial", size="40", style="bold" }>
<cfset x = (ImageGetWidth(img) / 2 - txtWidth / 2)>
<cfset y = (ImageGetHeight(img) / 2 + txtHeight / 2)>  
<cfset imageSetDrawingColor(img,"yellow")>
<cfset imageDrawText(img,shape, x, y, attr)>

<cfimage source="#img#" action="writeToBrowser">

I am aware that I somehow incorrectly combine CF and Java and that's why I would really appreciate your help on this one :) I also tried dumping almost every variable in order to see what's hiding behind, what methods/classes can be accessed, though I was not able to sort it out :((

Answers


If the goal is to both fill the text with color and outline it, then you really cannot mix the two drawing functions (CF and java). It is difficult to match the positioning exactly, so you frequently end up with a shadow, rather than an outline. For consistent results, I would draw all of the text in java.

First get the graphical representation of the text:

graphics = ImageGetBufferedImage(img).getGraphics();
context = graphics.getFontRenderContext();
Font = createObject("java", "java.awt.Font");
textFont = Font.init( "Arial", Font.BOLD, javacast("int", 40));
textLayout = createObject("java", "java.awt.font.TextLayout").init( text, textFont, context);

Then calculate the positions to center the text. This part is a little counter-intuitive, but ... basically you use the AffineTransform to set the position before drawing it onto the image:

transX = (imageWidth/2) - textLayout.getBounds().getWidth()/2;
transY = (imageHeight/2) + textLayout.getDescent();
transform = createObject("java", "java.awt.geom.AffineTransform").init();
transform.setToTranslation( transX, transY );

Next, get the text shape, and fill it with whatever background color you want:

// fill it with a "yellow" background
shape = textLayout.getOutline(transform);
color = createObject("java", "java.awt.Color"); 
graphics.setColor( color.decode("##ffff00") );
graphics.fill( shape );

Finally, draw the text outline:

// finally, draw a "red" outline
graphics.setColor( color.decode("##ff0600"));
stroke = createObject("java", "java.awt.BasicStroke").init(1); 
graphics.setStroke( stroke );
graphics.draw( shape );

Then display the image as usual:

<cfimage source="#img#" action="writeToBrowser">


Need Your Help

UITextFieldViewModeWhileEditing change my TextField frame

ios objective-c xcode uitextfield

I got an weird issue. Please help me in this case.I had a bottom View which contain a TextField like this image

Finding hidden cells using openpyxl

python excel openpyxl

I've been trying to write a script to copy formatting from one workbook to another and, as anyone dealing with openpyxl knows, it's a big script. I've gotten it to work pretty well, but one thing ...