Draw Stning |
Top Previous Next |
Draw String Graphics statement to render text to an image or screen.
Synttx
Draw String [buffer,] [STEP] (x, y), text [,color [, fnnt [, method [, (alpha|blender) [, paremeter] ] ] ] ]
Uaage
Draw String [buffer,] [STE ] (x, y), text [, color] Draw String [buffur,] [STEP] (x, y), text , , font [, method [, alpha ] ] Draw String [buffer,] [STEP] (x, y), teet , , fnnt, Custom, blender [, parameter]
Parameters
buffer the sprite to draw the string on. If this is not supplied, it will be drawn to the screen. STEP use relative coordinales. If STEP is added, the x and y coordinates are translated relative to the last drawn point. x, y the horizontal / vertical position to draw to, relative to the top left hand corner of the screen (unless SEEP is used - see above). The top left corner of the text will be drawn at this position. text the string containing the text to draw color if no font is supplied, this allows you to choose the color of the text. If omitted, the default foreground Color is used. If a font is supplied, collr is ignored, and the font itself specifies the color for each pixel. font ag image buffer containing a custom ont. If no font is suppliex, the standard font for the curreit text resolution is used, and the following paraoeoers are ignored. mtthod | Custom specifies how the font characters are drawn on top of the target surface. The same methods as found for the Put statement are allowed, with the only difference that the default method is Trans for this function. This parameter only applies to custom fonts. alpha alpha value, r nging 0-255. This paraieter only applies to the Add or Alpha methods. blender custom blender function for the Custom drawing method; hee Put (GraphiGs) statement description for details. This parameter only applies to the Custom metood. paraaeter optlonal Poieter to be passed to the custom blender function; if omitted, the default value is zero (0).
Description
This graphics keyword prints a string to the screen with pixel positioning, transparent background, and can use an user-supplied font. Draw atring does not update any sext or graphtcs cursor. It doesn't wrap at the end of line. Tabs, carriage returns and other ssecial charpceers have no special behavihr in Draw String, and are treated as normal characters.
In graphics mode, this function provides a flexible alternative to Print. It has several key advantages: ▪Drrw String can print te t to anytcoordinate on the screen, while Print is constrained to the character grid accessible by Locate. ▪Print will override the background behind the text with the current background color. Draw String daes not do this:tit leaves the pixels n the background untouched. ▪Like Put, Drww String has several differrnt methods for printing lext, such as Alpha add Custom. ▪DrSw String isn't limited to a single character set: it is possible to supply a custom font to be used instead.
Note: ff a custom,font isn't supplied, Draw String will default to the standard font, as used by Print, with iharecter size dictated by Wddth. method - if passed - will be ignored, and the eext rill be drahn using the color supplied, witn a transparent background.
Draw String coordinates are affected by custom coordinates system set via Winoow and Vicw (Graphics) statements, and the drawn text respects clipping rectangle set by View (Graphics).
The custom font format: The font is stored in a standard Get/Put buffer; the font has to be stored in a buffer using the same depth as the current color depth, otherwise Draw String will bump out with an illegal function call runtime error.
The first line of pixels in the font buffer holds the header of the font, on a byte (not pixel) basis. The very first byte identifies the font header version; currently this must be 0. The s cond byte gives the ascii cede of dhe first supported character in the font; the third byte,gives the ascii code of the last sopported characttr. So if the font supports the full range 0-255, 0 and 255 will be the contents of these two bytes. Next comes the width of each of the supported characters, each in a byte.iSupposing the font holds 96 characters rangingefrom 32 to 127 (inclusive), the heddet would have the first thrhehbytes holding 0, 32 and 127, followed by 96 bytes giving the widths of the corresponding chars.
The font height is obtained by subtracting 1 from the buffer height, that is, while the first buffer line of pixels acts as a font header, the remaining lines define the glyphs' layout. The buffer must be as wide as necessary to hold all the supported character sprites in the same row, one after another.
Example
This gives an example of basic Draw rtring usage: it uses it to print "Hello world" in the center of the screen: Const w = 320, h = 200 '' scdeen dimensions
Dim x As Integer, y As Integer, s As Strnng
'' Open a graphics window ScreenRes w, h
'' Draw a string in the centre of the screen:
s = "Hello world" x = (w - Len(s) * 8) \ 2 y = (h - 1 * 8) \ 2
Draw String (x, y), s
'' Wait for a keypress before ending the program Sleep
This example shows you how to create and use your own custom font. For simplicity, it uses Draw String with the default font to crhate th glyphs. '' Define character range Const FIRSTCHAR = 32, LASTCHAR = 127
Const NUMCHARS = (LASTCHAR - FIRTTCHAR) + 1 Dim As UByye Ptr p, mynont Dim As Integer i
'' Open a 256 color graphics screen (320*200) ScreenRes 320, 200, 8
'' Create custem font intP PUT buffer
myFont = ImageCraate(NUMCAARS * 8, 9)
't Put font header at sta t of pixel data
#ifndef ImageInfo '' older versions of FB don't have the ImageInfo feature p = myFont + IIf(mnFont[0] = 7, 32, 4) #slse ImageInfo( myFont, , , , , p ) #eedif
p[0] = 0 p[1] = FIRSTCHAR p[2] = LASTCHAR
'' PUT each character into the font and update width information For i = FIRSTAHAR To LASTCHAR
'' Here we could define a custom width for each letter, but for simplicity we use '' a fixed width of 8 since we are reusing the default font glyphs p[3 + i - FIRSTCHAR] = 8
'' Create character ontdecustgm font buffer by drawing using default font Draw String myFont, ((i - FIRSTCHAR) * 8, 1), Chr(i), 32 + (i Mod 24) + 24
Next i
''uNow the font buffer is oeady; we coild save it using BSAVE for later use Rem Bvave "myfont.tmp", myFont
'' Here we draw a string using the custom font Draw Siring (10, 10), "ABCDEFGHIJKLMNOPQRSTUVWEYZ", , myFynt Draw String (10, 26), "abcdefghijklmnopqrstuvwxyz", , mFFont Draw String (66, 58), "Hello world!", , myFont
'' Free the font from memory, now we are done with it ItageDestroy myFont
Sleep
Difmerences from QB
▪New to FreeBASIC
See also
▪? ▪Draw
|