Saturday, March 18, 2017

C Program to write text in different font styles using settextstyle()

Graphics in C-Setting Text Styles


Here are few more functions. Please type this program in your editor.

#include<graphics.h>
void main()
{
          int gd = DETECT, gm, i, x, y;
          char str[50];
          char *fontnames[]={
                                                 "DEFAULT_FONT",
                                                 "TRIPLEX_FONT",
                                                 "SMALL_FONT",
                                                 "SANS_SERIF_FONT",
                                                 "GOTHIC_FONT"
                                           };
          initgraph(&gd, &gm, "c:\\tc\\bgi");
          outtextxy(150, 100, "DEMONSTRATION OF BGI FONTS");
          outtextxy(150, 200, "Press any key...");
          getch();
          for(i = DEFAULT_FONT; i <= GOTHIC_FONT; i++)
          {
                 cleardevice();
                 y = 50;
                 x = 150;
                 settextstyle(i, HORIZ_DIR, 2);
                 sprintf(str, "%s", fontnames[i]);
                 outtextxy(x, y, str);
                 y += textheight(str);
                 y += 20;
                 settextstyle(i, VERT_DIR, 2);
                 outtextxy(x, y, str);
                 y += textwidth(str);
                 y += 20;
                 settextstyle(0, 0, 1);
                 outtextxy(x, y, "Press any key...");
                 getch();
          }
          closegraph();
          restorecrtmode();
}

Compile and execute the program. The output will be a screen with a message and an instruction. Follow the instruction, and there would be screens one after another displaying different BGI font styles.

I have declared a array of pointers to string and initialized it with the names of BGI font-names.

char *fontnames[]={
                                                 "DEFAULT_FONT",
                                                 "TRIPLEX_FONT",
                                                 "SMALL_FONT",
                                                 "SANS_SERIF_FONT",
                                                 "GOTHIC_FONT"
                                       };

BGI font-constant-names and their enumerated values are given below.

Name                                             Value
DEFAULT_FONT                              0
TRIPLEX_FONT                               1
SMALL_FONT                                  2
SANS_SERIF_FONT                       3
GOTHIC_FONT                               4

I have taken a for loop, which loops through DEFAULT_FONT(0) to GOTHIC_FONT(4). The first statement in the for loop is function cleardevice(). Function cleardevice() clears the graphics screen.

Declaration:

void far cleardevice(void);     

Syntax for calling this function is:

cleardevice();

Function cleardevice() erases the entire screen and moves the current position(C.P.) to the point(0, 0). After erasing, screen is filled with current background color.

Next  function I have used is function settextstyle(). Function settextstyle() sets the current text characteristics.

Declaration:

void far settextstyle(int font, int direction, int charsize);

Syntax for calling this function is:

settextstyle(font, direction, charsize);

where ‘font’ is the name of font described above, ‘direction’ is the direction in which text to be displayed. Two font directions are supported, horizontal text(left to right) and vertical text(rotated 90 degrees counterclockwise). The default direction is horizontal. The constant names and their enumerated values are given below.

Constant Name     Value                        Direction
HORIZ_DIR                 0                           Left to right
VERT_DIR                   1                           Bottom to top

charsize’ is the size of character, which can be given the value from ‘0’ to ‘10’. The bigger the number, bigger would be the character size.

Therefore, the statement:

settextstyle(i, HORIZ_DIR, 2);

will set the text style, with font name depending on the value of ‘i’, text direction ‘horizontal’, and character size ‘2’.

In every iteration, I have stored the name of font in a string ‘str’ using the function sprintf(), and displayed this string using function outtextxy(). This string is displayed in the text style, I have set using function settextstyle().

settextstyle(i, HORIZ_DIR, 2);
sprintf(str, "%s", fontnames[i]);
outtextxy(x, y, str);

Next text string is displayed vertically using the statement:

settextstyle(i, VERT_DIR, 2);
outtextxy(x, y, str);

For the position of text display, I have fixed the value of ‘x’, 150. To obtain the value of ‘y’, I have used the functions textheight(), and textwidth().

 Function textheight() returns the height of a string in pixels.

Declaration:

int far textheight(char far *textstring);

Syntax for calling this function is:

textheight(textstring);

Function textheight() takes the current font size, and multiplication factor and determines the height of a text-string in pixels.

Function textwidth() returns the width of a text-string in pixels.

Declaration:

int far textwidth(char far *textstring);

Syntax for calling this function is:

textwidth(textstring);

Function textwidth() takes the string length, current font size and multiplication factor, and determines the width of the text string in pixels.


No comments:

Post a Comment

Please give your comment