vbscript code that helps write UTF-8 text
I think this code should be shared. When we switched our database to storing all data in UTF-8 format, we encountered a problem when using imageMagick to write text to graphic (typically when having non standard fonts)
Using ImageMagick to write text to a graphic file from a text-file, using -annotate, resulted in a strange character before the text. It turned out that this char was the Byte Order Mark that Windows uses in UTF-8 files.
I therefore created a couple of vbscript functions that remove the BOM and resaves the file.
Code: Select all
' Input is Unicode text and filePath is path to the image file we wish to create. First we create a utf-8 file ' named the same as the image file and then we use the utf-8 file as an input to when creating the image file. strResult = img.Convert( _ "-size" , "200x200", _ "-font" , "Arial-Bold", _ "-pointsize" , "12", _ "-fill" , "#B6B6B6", _ "-annotate" , "0x0+25+18" ,writeUnicodeADODB(UCase(sText), strDefaultPath & "top_" & sFilename), _ "-trim", _ strDefaultPath & "template.png", _ strDefaultPath & "top_" & sFilename) Function writeUnicodeADODB(txtInput,filePath) ' Create and open stream Dim objStream Set objStream = CreateObject("ADODB.Stream") objStream.Open 'Reset the position and indicate the charactor encoding objStream.Position = 0 objStream.Charset = "UTF-8" 'Write to the steam objStream.WriteText txtInput 'Save the stream to a file filePath = filePath & ".txt" objStream.SaveToFile filePath, 2 ' overwrite if exists ' Return filepath with an @ so that imagemagick understands that it's a file writeUnicodeADODB = "@" & RemoveBOM(filePath) ' Kill stream Set objStream = Nothing End Function
Code: Select all
' Removes the Byte Order Mark - BOM from a text file with UTF-8 encoding ' The BOM defines that the file was stored with an UTF-8 encoding. Public function RemoveBOM(filePath) ' Create a reader and a writer Dim writer,reader, fileSize Set writer = CreateObject("Adodb.Stream") Set reader = CreateObject("Adodb.Stream") ' Load from the text file we just wrote reader.Open reader.LoadFromFile filePath ' Copy all data from reader to writer, except the BOM writer.Mode=3 writer.Type=1 writer.Open reader.position=5 reader.copyto writer,-1 ' Overwrite file writer.SaveToFile filePath,2 ' Return file name RemoveBOM = filePath ' Kill objects Set writer = Nothing Set reader = Nothing end Function