Saturday, April 1, 2017

C Program to explain function setusercharsize()

Graphics in C-Scaling of text size using function setusercharsize()


Here is a simple program to describe one more text setting function. Please type this program in your editor.

#include<graphics.h>
void main()
{
          int gd = DETECT, gm;
          initgraph(&gd, &gm, "c:\\tc\\bgi");

          /* Default font, normal(default) size */
            outtextxy(0,100, "Hello World");

          /* set stroked triplex font */
          settextstyle(TRIPLEX_FONT, HORIZ_DIR, 0);

          /* text display in normal(default) size */
          outtextxy(0, 120, "Hello World");

          /* default text width scaled by 2:1, and
              height scaled by 3:2 */
          setusercharsize(2, 1, 3, 2);
          outtextxy(0, 150, "Hello World");

          /* default text width scaled by 3:1, and
             height scaled by 2:1 */
          setusercharsize(3, 1, 2, 1);
          outtextxy(0, 200, "Hello World");
          getch();
          closegraph();
          restorecrtmode();
}

Compile and execute this program. The output would be a “Hello World” message in different  sizes.

For changing the size I have used the function setusercharsize(). Function setusercharsize() uses a user-defined character magnification factor for stroked fonts. With function setusercharsize() you specify the factors by which the width and height are scaled.

Declaration:

void far setusercharsize(int multx, int divx, int multy, int divy);

Syntax for calling this function is:

setusercharsize(multx, divx, multy, divy);

where multx : divx is the scaling ratio for text width, and multy : divy is the scaling ratio for height. It means the default width is scaled by multx : divx, and the default height is scaled by multy : divy.

Suppose you want to make the text width double of the normal size, you should set multx = 2, divx = 1 (2 : 1), and text height triple of the normal size, the values of multy, and divy would be ‘3’, and ‘1(3 : 1).

Function setusercharsize() can be used only for stroked fonts.

In the given program, I have displayed a string in the default font and size.

outtextxy(0,100, "Hello World");

Then I have set the font style as ‘triplex font’ by function settextstyle() , since function setusercharsize() can be used on stroked fonts only(triplex font is a stroked font. Enumerated values 1 to 10 are stroked font styles. Triplex font has enumerated value ‘1’).

settextstyle(TRIPLEX_FONT, HORIZ_DIR, 0);

In this statement, I have set charsize = 0, it is because of the fact that values set by function setusercharsize() are effective only if you set charsize to ‘0’ by a previous call to function settextstyle().

The statements:

setusercharsize(2, 1, 3, 2);
outtextxy(0, 150, "Hello World");

display the text string “Hello World” in the size, default width scaled by the factor  (2 : 1), twice as wide as default width, and default height scaled by the ratio (3 : 2), 50% taller than the default height.

The statements:

setusercharsize(3, 1, 2, 1);
outtextxy(0, 200, "Hello World");

can also be explained the same way.

No comments:

Post a Comment

Please give your comment