Foxite.COM Community Weblog

Foxite.COM Community Weblog - free weblog service for the Visual FoxPro Community.
Welcome to Foxite.COM Community Weblog Sign in | Join | Help
in
Home Blogs Forum Photos Forum Archives

VFP IMAGING



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:

 

 

Prerequisites
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.
http://www.codeplex.com/VFPX/Wiki/View.aspx?title=GDIPlusX&referringTitle=Home

 


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 = .
Bitmap.New(350,370)
loGfx = .Graphics.FromImage(loBmp)
loGfx.
Clear(.Color.White)
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)
ENDFOR
loBmp.Save("TestUnicodes.Png", .Imaging.ImageFormat.Png)
 
ENDWITH

 

 


FUNCTION
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)
ENDCASE

** Optional parameter
** The C++ classes show this parameter as NULL if not specified
IF VARTYPE(m.toFormat) = "O"
   m.lhFormat = m.toFormat.Handle
ELSE
   m.lhFormat = 0
ENDIF

m.lqLayoutRect = BINTOC(m.tnX,"F")+BINTOC(m.tnY,"F")+;
   BINTOC(m.lnWidth,"F")+BINTOC(m.lnHeight,"F")

=xfcGdipDrawString(toGfx.Handle, m.tcString+0h00,
LENC(tcString)/2, ;
   m.toFont.Handle, @lqLayoutRect, m.lhFormat, m.toBrush.Handle)
ENDFUNC

 
FUNCTION
HexToUnicode(tcHex)
   LOCAL n, lcHex, lcUnicode
   lcUnicode =
SPACE(0)
   FOR n = 1 TO GETWORDCOUNT(tcHex, SPACE(1))
      lcHex =
EVALUATE("0x" + GETWORDNUM(tcHex, n, SPACE(1)))
      lcUnicode = lcUnicode +
BINTOC(lcHex, "4RS")
   ENDFOR 
   RETURN
lcUnicode
ENDFUNC

Published Sunday, March 01, 2009 12:58 PM by cesarchalom

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Shaun Davis said:

Hey just a heads up, I wanted to let everyone know that Xio Dibin speaks English.  I hope I posted in the right location?
April 23, 2009 9:51 AM

What do you think?

(required) 
(optional)
(required) 

This Blog

Post Calendar

<March 2009>
SuMoTuWeThFrSa
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

Syndication