Saturday, March 25, 2017

C Program to justify text using settextjustify()

Graphics in C-Justifying text using function settextjustify()


Here is something more about text settings in graphics mode. Please type this program in your editor.

#include<graphics.h>
#include<stdio.h>
void plus(int x, int y);
char *horiz[] = {
                               "LEFT_TEXT",
                               "CENTER_TEXT",
                               "RIGHT_TEXT"
                         };

char *vert[] = {
                              "BOTTOM_TEXT",
                              "CENTER_TEXT",
                              "TOP_TEXT"
                        };

void main()
{
          int gd = DETECT, gm, i, j, midx, midy;
          char msg[50];
          initgraph(&gd, &gm, "c:\\tc\\bgi");
          midx = getmaxx()/2;
          midy = getmaxy()/2;
          plus(midx, midy);
          for(i = LEFT_TEXT; i <= RIGHT_TEXT; i++)
          {
                   for(j = BOTTOM_TEXT; j <= TOP_TEXT; j++)
                   {
                               cleardevice();
                               setcolor(RED);
                               plus(midx, midy);
                               setcolor(WHITE);
           outtextxy(120, 50, "(Horizontal justification, Vertical                          justification)");
                               settextjustify(i, j);
                               sprintf(msg, "(%s, %s)", horiz[i], vert[j]);
                               outtextxy(midx, midy, msg);
                               settextjustify(LEFT_TEXT, TOP_TEXT);
                               outtextxy(120, 360, "Press any key...");
                               getch();
                       }
              }
              closegraph();
              restorecrtmode();
}

void plus(int x, int y)
{
          line(x-100, y, x+100, y);
          line(x, y-100, x, y+100);
}

Compile and execute this program. The output will be a red big plus or cross sign. This sign I have created to figure out the text justification. In the center of this cross, text is being displayed telling the text justification settings, in which this text has been justified.

In the program, I have declared the prototype of the function plus(), which I have defined after the function main(). This prototype is telling that this function plus() has two integer type parameters and return type is ‘void’, means this function returns nothing. I have declared this function outside the function main(), therefore its scope is global. It will be accessible throughout the program.

void plus(int x, int y);

I have declared two arrays of pointers to strings and initialised them with names of constants of text justification. I will explain these text justification later. You can define these arrays inside main function too.

char *horiz[] = {
                               "LEFT_TEXT",
                               "CENTER_TEXT",
                               "RIGHT_TEXT"
                         };

char *vert[] = {
                              "BOTTOM_TEXT",
                              "CENTER_TEXT",
                              "TOP_TEXT"
                        };

In the function main(), I have obtained the coordinates of the midpoint of the view port.

midx = getmaxx()/2;
midy = getmaxy()/2;

Then, I have called the user defined function plus() and passed the coordinates of this midpoint as arguments.

plus(midx, midy);

This function draws one horizontal line and one vertical line, intersecting each other thus forming a big plus sign or cross-hair. This function has been defined after the function main().

void plus(int x, int y)
{
          line(x-100, y, x+100, y);
          line(x, y-100, x, y+100);
}

This program is mainly about the text justification. For text justification, I have used the function settextjustify(). Function settextjustify() sets text justification for graphics mode.

Declaration:

void far settextjustify(int horiz, int vert);

where ‘horiz’ is the horizontal justification, and ‘vert’ is the vertical justification.
Syntax for calling this function is:

settextjustify(horiz, vert);

Text output, after a call to function settextjustify(), is justified around the current position(C.P.) horizontally and vertically, as specified.

Function settextjustify() affects text written with function outtext() and function outtextxy(), and can’t be used with text mode.

The enumerated values and constant names for horizontal justification and vertical justification are given below:

Argument      Constant           Value               Meaning
horiz                LEFT_TEXT          0                  Left justified text
                        CENTER_TEXT    1                  Centered horizontally
                        RIGHT_TEXT        2                  Right justified text
vert                  BOTTOM_TEXT    0                  Justified from bottom
                        CENTER_TEXT    1                   Centered vertically
                        TOP_TEXT            2                  Justified from top  

The default justification settings are LEFT_TEXT for ‘horiz’ and TOP_TEXT for ‘vert’.
                            
In the program, I have taken two nested ‘for’ loops. Outer ‘for’ loop loops through values LEFT_TEXT to RIGHT_TEXT. Inner ‘for’ loop loops through values BOTTOM_TEXT to TOP_TEXT. Counter variables are ‘i’ and ‘j’. In every iteration, function settextjustfy() sets the horizontal and vertical justification according to the values of i and j.

settextjustify(i, j);

Then, function sprintf() converts the strings ‘horiz[i]’ and ‘vert[j]’ in the specified format and stores them in a single string ‘msg’. Function outtextxy() outputs this string around the midpoint in the specified horizontal and vertical justification.

settextjustify(i, j);
sprintf(msg, "(%s, %s)", horiz[i], vert[j]);
outtextxy(midx, midy, msg);

I have again set the text justification (LEFT_TEXT, TOP_TEXT) for other messages on the screen.

settextjustify(LEFT_TEXT, TOP_TEXT);
outtextxy(120, 360, "Press any key...");

On pressing any key, every time you will see text displayed around the midpoint of the cross hair in different horizontal and vertical justification.

Now, the demonstration of the function settextjustify() is completed. For the explanation of the rest of the functions and terms, please refer my previous posts.

OUTPUT:








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.