even trying to use a custom font in captcha image ...
# lucee
g
even trying to use a custom font in captcha image but its not working https://trycf.com/gist/336fd5164b9815606ad86381f6aba7d2/lucee5?theme=monokai
i tried this code but it does not work, just keep showing me in english
Copy code
<!--- download a ttf file --->
<cfset filePath = ExpandPath("assets/fonts/NotoSansGurmukhi-Regular")>
<cfhttp url="<https://notofonts.github.io/gurmukhi/fonts/NotoSerifGurmukhi/full/ttf/NotoSerifGurmukhi-Regular.ttf?raw=true>"
        getasbinary="true" file="#filePath#"/>

<!--- add the font to java.awt.GraphicsEnvironment  --->

    <cfset oFont = createObject('java', 'java.awt.Font') />
    <cfset stream = createObject('java', 'java.io.FileInputStream').init(filePath) />
    <cfset sFont = oFont.createFont(oFont.TRUETYPE_FONT, stream) />
    <cfset createObject('java', 'java.awt.GraphicsEnvironment').getLocalGraphicsEnvironment().registerFont(sFont) />
    <cfset application.__fontAdded = 1 />


<!--- create an image and write text to it --->
<cfset text = "This IS a custom Font Test with imageDrawText()" />
<cfset myImg = imageNew("", 800, 50) />
<cfset imageSetAntialiasing(myImg, true) />
<cfset imageDrawText(myImg, text, 10, 35, {
      size="40.0"
    , font="NotoSerifGurmukhi-Regular"// <<<------ this is what it was all about!
}) />
<cfimage action="write" source="#myImg#" destination="#expandPath('./test1.png')#" quality="1" overwrite="true" />
<!--- output test1 --->
<img src="test1.png" />
<hr/>

<!--- create a captcha file --->
<cfimage action="captcha" text="Captcha test" width="600" height="150" fonts="NotoSerifGurmukhi-Regular" fontSize="50"
        destination="test2.png" overwrite="true" />
<!--- output test2 --->
<img src="test2.png" />
t
You are putting English text into your image (
text = "This IS a custom Font Test with imageDrawText()"
), do you expect cfimage does the magic to translate that English text to India Punjabi writing??
g
I am not expecting it I am using it to register the font first in lucee and then make it happen It should be able to read the font at least
I had tested the font and it displays right Only when it comes to cfimage it turns into boxes
t
Works for me
Maybe the IDE / code editor you used to put India Punjabi text into your code (the
<cfset text = "This IS a custom Font Test with imageDrawText()" />
and
<cfimage action="captcha" text="Captcha test" ...>
does not support UTF?
g
i am using sublime
the same code i shared works for you
that is really starnge
the ultimate goal is the browser
t
Try create a simple page in a totally new project folder (so it does not use any framework, even does not have an applicaiton.cfc), and put your code and test Put your codes there and put the India Punjabi text likes I did, and test it
if that still not working, maybe it is the differences of the CF server you use and I use? I am testing it with Lucee 5.4.6+9
g
i am on lucee6
i ran using commandbox for 5.4.6, i still see unknown characters, i did the same code as you mentioned above, can you copypaste code so we are on same page
i even added pageencoding utf-8 but still issues
t
Copy code
<!--- download a ttf file --->
<cfset filePath = ExpandPath("./NotoSansGurmukhi-Regular")>
<cfhttp url="<https://notofonts.github.io/gurmukhi/fonts/NotoSerifGurmukhi/full/ttf/NotoSerifGurmukhi-Regular.ttf?raw=true>"
        getasbinary="true" file="#filePath#"/>

<!--- add the font to java.awt.GraphicsEnvironment  --->

    <cfset oFont = createObject('java', 'java.awt.Font') />
    <cfset stream = createObject('java', 'java.io.FileInputStream').init(filePath) />
    <cfset sFont = oFont.createFont(oFont.TRUETYPE_FONT, stream) />
    <cfset createObject('java', 'java.awt.GraphicsEnvironment').getLocalGraphicsEnvironment().registerFont(sFont) />
    <cfset application.__fontAdded = 1 />


<!--- create an image and write text to it --->
<cfset text = "test1.png  ਸਰਮਨ" />
<cfset myImg = imageNew("", 800, 50) />
<cfset imageSetAntialiasing(myImg, true) />
<cfset imageDrawText(myImg, text, 10, 35, {
      size="40.0"
    , font="NotoSerifGurmukhi-Regular"
}) />
<cfimage action="write" source="#myImg#" destination="#expandPath('./test1.png')#" quality="1" overwrite="true" />
<!--- output test1 --->
<img src="test1.png" />
<hr/>

<!--- create a captcha file --->
<cfimage action="captcha" text="test2.png  ਸਰਮਨ" width="600" height="150" fonts="NotoSerifGurmukhi-Regular" fontSize="50"
        destination="#expandPath('./test2.png')#" overwrite="true" />
<!--- output test2 --->
<img src="test2.png" />
Tested that on Lucee 6.0.1+83 and is fine too
g
image.png
t
hmm...
g
quite interesting, for you its working , for me its failing \
t
I still think the IDE (sublime) or your OS not saving those India Punjabi text properly when you save your cfm file. Can you open up your cfm in another text editor, see if those India text still showing correctly?
g
i am now doing in notepad++
even used cfprocessingdirective
tried saving with UTF-8 with BOM
t
How about in a new cfm file, just put
ਸਰਮਨ
and then see if can see it correctly in your browser?
g
sure, i even tried the locales which are present in lucee i set it as <cfset setLocale('punjabi (gurmukhi)')>, but no effect
if i do output, i see it right,
when it goes to cfimage, it shows boxes
image.png
t
ok, it might because your OS does not have that font it works for me as I am using a Mac and it has some fonts for "Noto" but I tested it on a old Windows server and the codes do not work, that Windows server does not have any "Noto" fonts I know, your code register the font for Java, but I think for some reasons your OS still need to have it try add/install that font into your OS and test?
g
ok
i tried registrting the font in windows tpoo
not working
you are right @Tomy Saman i installed windows fonts and then restarted machine and it started working
👍 1
but its very annoying that even after installing the font in java code, it still does not recognizze it
t
Giving you said installing the font was not working at the first time, but then it works after Windows is restarted; I am thinking maybe the codes that add the font to Java requires a machine restart as well ?? If you want to test, remove the font from Windows. Run your codes again (so it register the font to Java), and then restart Windows, see what happen?
g
ok