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:








No comments:

Post a Comment

Please give your comment