Home Docs
Graphics.h Documentation
Compiler

Text and Fonts

Text & Fonts in graphics.h

Text rendering in graphics.h is controlled by font, direction, and size. This section covers the common APIs for drawing labels and titles in graphics mode.

On This Page

Core Text Functions

void outtext(char *textstring);
void outtextxy(int x, int y, char *textstring);
void settextstyle(int font, int direction, int charsize);
void settextjustify(int horiz, int vert);
int textwidth(char *textstring);
int textheight(char *textstring);
  • outtextxy() is most common for fixed-position labels.
  • settextstyle() changes font family, direction, and size.
  • textwidth()/textheight() help center text.

Font and Size

settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
outtextxy(30, 40, "Default text");

settextstyle(TRIPLEX_FONT, HORIZ_DIR, 2);
outtextxy(30, 80, "Bigger Triplex text");

charsize is integer-based and depends on selected font and graphics mode.

Text Demo Program

#include <graphics.h>
#include <conio.h>

int main() {
    int gd = DETECT, gm;
    int tw;
    initgraph(&gd, &gm, "");

    setbkcolor(BLACK);
    cleardevice();

    setcolor(LIGHTCYAN);
    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
    outtextxy(20, 30, "graphics.h text demo");

    setcolor(YELLOW);
    settextstyle(TRIPLEX_FONT, HORIZ_DIR, 2);
    outtextxy(20, 75, "TRIPLEX FONT");

    setcolor(LIGHTGREEN);
    settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 1);
    outtextxy(20, 130, "SANS_SERIF_FONT size 1");

    setcolor(WHITE);
    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
    tw = textwidth("Centered text");
    outtextxy((getmaxx() - tw) / 2, 190, "Centered text");

    getch();
    closegraph();
    return 0;
}

Click Run (DOS) to test this quickly.

Common Mistakes

  • Forgetting to set text color with setcolor().
  • Using unsupported font constants in some environments.
  • Hardcoding centered positions instead of using textwidth().

Related: Screen & Viewport for clipping text inside panel regions.