Drawing Unicode texts with GdiPlusX

we forgot to add to gdiplusx a specific function to allow sending unicodes directly, similar to the function drawstring.


but as you'll see below, it's really simple to adapt the original function and provide this possibility:

in the sample below you'll see the function "drawstringw", that is just an adapted version from the original xfcgraphics.drawstring function.

it accepts the same parameters and overloads from the original function - the sole difference is the first parameter that was introduced - the graphics object.

another helper function - "hextounicode" was introduced. it converts a string containing hex values separated by a space into the unicode needed by gdiplus.dll to  draw the string.

just run the script and you'll obtain a result similar to the one below:



visual foxpro 9 and the gdiplusx library from vfpx 

please make sure that you have the latest version, because this sample may be using some functions that were added or fixed recently.


do locfile("system.app")

local n, x, y, lncolor
local lobmp as xfcbitmap, logfx as xfcgraphics, lofont as xfcfont
local lobrush as xfcsolidbrush
local lawords(9,2)
* greek
lawords(1,1) = "greek"
lawords(1,2) = "03b5 03b9 03c1 03ae 03bd 03b7"
* korean
lawords(2,1) = "korean"
lawords(2,2) = "d3c9 d654"
* hebrew
lawords(3,1) = "hebrew"
lawords(3,2) = "05e9 05dc 05d5 05dd"
* bulgarian
lawords(4,1) = "bulgarian"
lawords(4,2) = "043c 0438 0440"
* arabic
lawords(5,1) = "arabic"
lawords(5,2) = "0633 0644 0627 0645"
* simplified chinese
lawords(6,1) = "chinese"
lawords(6,2) = "548c 5e73"
* thai
lawords(7,1) = "thai"
lawords(7,2) = "0e04 0e27 0e32 0e21 0e2a 0e07 0e1a"
* russian
lawords(8,1) = "russian"
lawords(8,2) = "043c 0438 0440"
* japanese
lawords(9,1) = "japanese"
lawords(9,2) = "5e73 548c"
with _screen.system.drawing
lofont = .
font.new("tahoma", 18)
lobmp = .
logfx = .graphics.fromimage(lobmp)
logfx.drawstring("gdi+x drawing unicodes", lofont,;
   .brushes.black, 10,5)
for n = 1 to 9
   y = n * 35
   x = 160

   lncolor =
rgb(rand() * 255, rand() * 255, rand() * 255)
   * create a solidbrush with randomic color
   lobrush = .solidbrush.new(.color.fromrgb(lncolor))

   * draw the language name
   logfx.drawstring(lawords(n,1), .font.new("tahoma", 10),;
         .brushes.black, 65, y + 5)

   * draw the text in unicodes
   =drawstringw(logfx, hextounicode(lawords(n,2)), lofont,;
         lobrush, x, y)
lobmp.save("testunicodes.png", .imaging.imageformat.png)



drawstringw(togfx, ;
   tcstring, tofont
as xfcfont, tobrush as xfcbrush, tnx, tny ;
   , toformat
as xfcstringformat)
   *********** tcstring, tofont as xfcfont, tobrush as xfcbrush, topoint as xfcpointf ;
   , toformat as xfcstringformat
   *********** tcstring, tofont as xfcfont, tobrush as xfcbrush, torectangle as xfcrectanglef ;
   , toformat as xfcstringformat

local lqlayoutrect
local lnwidth, lnheight, lopoint as xfcpointf, lorect as xfcrectanglef
local lhformat
store 0 to lnwidth, lnheight
store null to lopoint, lorect
m.lqlayoutrect = 0h00

** handle overload parameters
do case
case vartype
(tnx) = "n"

case vartype(tnx) = "o" and inlist(tnx.basename,"point","pointf")
   m.lopoint = m.tnx
   m.toformat = m.tny
   m.lopoint.getextent(@tnx, @tny)

case vartype(tnx) = "o" and inlist(tnx.basename,"rectangle","rectanglef")
   m.lorect = m.tnx
   m.toformat = m.tny
   m.lorect.getextent(@tnx, @tny, @lnwidth, @lnheight)

** optional parameter
** the c++ classes show this parameter as null if not specified
if vartype(m.toformat) = "o"
   m.lhformat = m.toformat.handle
   m.lhformat = 0

m.lqlayoutrect = bintoc(m.tnx,"f")+bintoc(m.tny,"f")+;

=xfcgdipdrawstring(togfx.handle, m.tcstring+0h00,
lenc(tcstring)/2, ;
   m.tofont.handle, @lqlayoutrect, m.lhformat, m.tobrush.handle)

   local n, lchex, lcunicode
   lcunicode =
   for n = 1 to getwordcount(tchex, space(1))
      lchex =
evaluate("0x" + getwordnum(tchex, n, space(1)))
      lcunicode = lcunicode +
bintoc(lchex, "4rs")

1 comment:

  1. Hey just a heads up, I wanted to let everyone know that Xio Dibin speaks English.  I hope I posted in the right location?