CS 70

Free Swim

This part of the assignment is both optional and open-ended. It's intended to let you have fun and go beyond what we explicitly ask you to do in the assignment. The focus of the “free swim” in this assignment is the fontfun program.

We'll give you a few bonus points for doing this part, but ideally you shouldn't do it for the points; you should do it for the joy of creating something awesome!

Below are a few suggestions for things you could do…

Support Additional Fonts

There are various sources of fonts in this format, but one notable website is ZX Origins, which has 463 fonts across 250 distinct typeface families. You could experiment with different fonts by downloading the .ch8 files and modifying the fontfun program to load and display them.

You can either find a font you like and replace the existing font.ch8 file, or you can modify the fontfun program to allow it to load different font files.

Improve the Code

The code is mostly nicely arranged, but you may have noticed one significant duplication of a key algorithm. When you've figured out what that is, abstract it out cleanly into a new function.

Add New Features

There are many ways you could improve the code. Here are some ideas.

Add Color

The terminal supports ANSI escape codes, which can be used to change the text color. For example, here's a tiny program that prints purple text on a yellow background:

#include <iostream>

int main()
{
    // Classic ANSI Escape Codes
    std::cout << "\033[35m\033[103mHello, World!\033[0m" << std::endl;
    // Nicer colors using 256-color ANSI Escape Codes
    std::cout << "\033[38;5;135m\033[48;5;226mHello, World!\033[0m" << std::endl;
    return 0;
}

You can learn more about these color escape codes.

Rotate Vertical Text

Currently, when printing text vertically, it is shown one character at a time down the screen, but the letters themselves are still displayed as left-to-right. Figure out how to rotate the text 90 degrees clockwise to create a more visually interesting display.

Avoid Wrapping

If people type in text that is too long, it will wrap in the terminal and be unreadable. You might modify the program to detect the terminal width and avoid wrapping by either truncating the text or by splitting it across multiple lines.

Here's some C++ code that can detect the dimensions of the terminal screen:

// ---------------------------------------------------------------
// Additional #include for terminal size detection
// ---------------------------------------------------------------
#include <unistd.h>
#include <sys/ioctl.h>

// ---------------------------------------------------------------
// Terminal Query Functions
// ---------------------------------------------------------------
//
// These functions use low-level operating system magic to work out
// the size of the terminal window in characters.

/**
 * \brief  Get the terminal width
 * \return The width of the terminal in characters, or 80 if it can't
 *         be determined
 */
int getTerminalWidth()
{
    struct winsize w;
    if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0)
        return w.ws_col;
    return 80;
}

/**
 * \brief  Get the terminal height
 * \return The height of the terminal in characters, or 24 if it can't
 *         be determined
 */
int getTerminalHeight()
{
    struct winsize w;
    if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0)
        return w.ws_row;
    return 24;
}

Another option is to horizontally scroll long text. You could implement this functionality by continuously shifting the text to the left and redrawing it at the new position, creating the illusion of scrolling.

The ANSI escape codes for cursor movement can be used to achieve this effect. For example, you move the cursor back to the top-left corner of the terminal using the escape code "\033[H". Also, on Unix-like systems like our server, you can add a delay using the usleep function to control the speed of the scrolling. This function takes an argument in microseconds.

Or Something Else

These are all just ideas to consider. Perhaps you can think of something else entirely! You could even write a whole new program that is merely inspired by this one.

  • Duck speaking

    What would Prof. Melissa do, I wonder…

  • LHS Cow speaking

    On the server, you can use the fzxshow to run Prof. Melissa's extension of this problem. It provides a renderer for a more complex proportional font system for the ZX Spectrum known as FZX format. You can run

    fzxshow -f Sabon 'What fun!'
    

    to see an example, or run

    less /usr/local/share/fzxshow/README.txt
    

    to see the full list of all 349 available fonts you can try.

  • Rabbit speaking

    Supporting the fzx format is a bit intricate. It probably isn't a good use of your time.

To Complete This Part of the Assignment…

If you did make changes, also tell us what you did here (you can paste the same text you put in written.md):

(When logged in, completion status appears here.)