NCL 210: Styling the Display
In NCL 209, we saw that escape sequences can insert particular Unicode characters into strings.
NCS/e reserves some characters for a special purpose.
The Display interprets them as inline attributes that change how following text is drawn.
For example:
D.TXT "\s01ERROR\s0F"
D.BLT
The word:
ERROR
is displayed using palette entry 1.
The escape:
\s01
changes the Display's active text attributes.
Then:
\s0F
changes them back to normal, non-inverted text using palette entry F.
Neither escape produces a visible glyph of its own. Instead, the Display interprets the characters they produce as attribute commands.
Choosing a colour
For normal text, we can use:
\s0X
where X is the hexadecimal number of a palette entry.
The standard palette contains sixteen entries:
X |
Standard colour |
|---|---|
0 |
Black |
1 |
Red |
2 |
Blue |
3 |
Purple |
4 |
Green |
5 |
Gray |
6 |
Medium blue |
7 |
Light blue |
8 |
Brown |
9 |
Orange |
A |
Light gray |
B |
Pink |
C |
Light green |
D |
Yellow |
E |
Aqua |
F |
White |
The hexadecimal digits A through F should look familiar from NCL 207.
For example:
D.TXT "\s01RED \s02BLUE \s0DYELLOW\s0F"
D.BLT
Each attribute changes the colour used by the text that follows it.
Palette entries can be changed
These names describe the colours in the standard NCS/e palette.
The Display's palette can be changed, so palette entry 1 does not necessarily have to contain red.
We'll work with the Display and its palette more directly in later lessons.
Attribute changes persist
An inline attribute doesn't apply only to the next character.
It changes the Display's active attributes.
Consider:
D.TXT "\s01RED "
D.TXT "STILL RED "
D.TXT "\s0FNORMAL"
D.BLT
The second D.TXT does not contain an attribute character, but its text is still red. Palette entry 1 remains active from the previous write.
The final:
\s0F
restores normal, non-inverted text using palette entry F.
Because attributes persist, styled output should usually restore the attributes it expects to leave behind. Otherwise, one piece of styled output can unexpectedly affect whatever is displayed next.
Inverting text
The Display can also invert its text.
So far, our attribute escapes have started with:
\s0
Changing that 0 to 1 enables inversion:
\s1X
The final hexadecimal digit still chooses the palette entry.
For example:
D.TXT "\s02BLUE "
D.TXT "\s12INVERTED BLUE"
D.TXT "\s0F"
D.BLT
Both sections use palette entry 2.
The difference is inversion:
| Escape | Colour | Inverted |
|---|---|---|
\s02 |
Palette entry 2 |
No |
\s12 |
Palette entry 2 |
Yes |
We can combine different colours and inversion in the same string:
D.TXT "\s04READY \s1DWARNING \s11ERROR\s0F"
D.BLT
Each attribute changes how the text following it is displayed.
For now, these are the two forms we need:
| Form | Meaning |
|---|---|
\s0X |
Normal text using palette entry X |
\s1X |
Inverted text using palette entry X |
The Display supports additional text attributes. The complete set is described in the NCL Reference.
What's actually in the string?
So far, \s01 might look like formatting metadata attached to an NCL string.
It isn't.
\s01 inserts a character.
Specifically, it inserts the Unicode code point:
U+F101
Likewise:
| NCL escape | Unicode code point | Display meaning |
|---|---|---|
\s01 |
U+F101 | Normal, palette entry 1 |
\s0F |
U+F10F | Normal, palette entry F |
\s12 |
U+F112 | Inverted, palette entry 2 |
\s1F |
U+F11F | Inverted, palette entry F |
These characters are really present in the string.
Consider:
#message "\s01ERROR\s0F"
S.LEN #length #message
D.TXT #length
D.TXT "\r\n"
D.TXT #message
D.BLT
The visible word:
ERROR
contains five characters.
But #message contains seven Unicode characters:
| Position | Character |
|---|---|
| 0 | U+F101 |
| 1 | E |
| 2 | R |
| 3 | R |
| 4 | O |
| 5 | R |
| 6 | U+F10F |
So S.LEN reports:
7
The two attribute characters are part of the string even though the Display doesn't draw them as glyphs.
Private-use characters
Why does Unicode have a character at U+F101 for changing the colour of an NCS/e Display?
It doesn't.
Unicode reserves some ranges of code points for private use. These are called the Private Use Areas, or PUA.
Unicode does not assign a standardized character or meaning to the code points in these ranges. A particular system, application, or community can assign meanings to them itself.
NCS/e uses the range:
U+F100–U+F1FF
for inline Display attributes.
The escape:
\s01
is a convenient way to insert U+F101 into a string.
We can also identify that code point directly:
D.TXT "\uF101RED\s0F"
D.BLT
or construct it numerically:
S.CHAR #attribute 0xF101
D.TXT #attribute
D.TXT "RED"
D.TXT "\s0F"
D.BLT
All three forms produce the same character:
| Source | Resulting character |
|---|---|
\s01 |
U+F101 |
\uF101 |
U+F101 |
S.CHAR #attribute 0xF101 |
U+F101 |
The Display receives the same character in all three cases.
\sXX is simply the convenient NCL notation intended for constructing inline Display attributes.
We can inspect that character using the operations from the previous lessons:
S.ORD #code "\s01" 0
-- > #code = "57601"
S.HEX #code #code
-- > #code = "F101"
S.ORD returns the numeric code point. S.HEX formats that same value as hexadecimal text.
We've now encountered the same value in several forms:
| Form | Meaning |
|---|---|
| U+F101 | Unicode code-point notation |
\uF101 |
Unicode escape |
\s01 |
NCS/e Display attribute escape |
0xF101 |
NCL hexadecimal integer literal |
"F101" |
Hexadecimal text produced by S.HEX |
"57601" |
Decimal representation produced by S.ORD |
They are different ways of describing or representing the value involved. There is no separate formatting object hidden inside the string.
As far as NCL's ordinary string operations are concerned, U+F101 is simply a character.
Private use in the wild
NCS/e isn't alone in assigning meanings to private-use code points.
One famous example is the Apple logo, traditionally assigned to U+F8FF in Apple fonts. Unicode does not define U+F8FF as the Apple logo; Apple gives that code point its meaning.
Icon fonts use the same idea, assigning interface symbols and other graphics to private-use code points.
Private-use code points are also useful for fictional writing systems such as Klingon from Star Trek, Aurebesh from Star Wars, and the Standard Galactic Alphabet from Commander Keen—the last of which is perhaps better known today as the writing on Minecraft's enchantment table.
"Private use" does not mean that the meaning has to be private or undocumented.
A company, project, or community can publish a mapping and use it consistently across different fonts and software. That convention can become widely recognized and standardized within that ecosystem.
The important difference is who defines the meaning.
Unicode defines what U+0041 means. It does not define what U+F101 means.
NCS/e does.
You can use the Private Use Area in your own software too.
The NCS/e Display does not have glyphs for arbitrary private-use characters, but your program does not have to use them as visible characters. It can assign its own meanings to private-use code points and interpret them as markers, commands, tokens, or other application-specific values inside a string.
If several programs agree on those meanings, you've created your own convention.
That's exactly what NCS/e is doing with its Display attributes.
The Display gives it meaning
The distinction between storing a character and interpreting it should be familiar from NCL 208.
Consider:
S.MOVE #message "\s01ERROR\s0F"
This does not change the Display colour.
It only stores a string.
We can measure it:
S.LEN #length #message
We can inspect its characters:
S.ORD #code #message 0
We can join it with other strings:
S.JOIN #message #message "!"
None of those operations cause anything to become red.
The attribute takes effect when we send the string to the Display:
D.TXT #message
D.BLT
The Display encounters U+F101, recognizes it as an inline attribute, and changes its active attributes.
It then displays ERROR using palette entry 1 until it encounters U+F10F and restores normal white text.
The ! was joined after U+F10F, so it is displayed using the restored attributes.
This is the same general idea we saw with control characters such as line feed:
- NCL source gives us a way to represent a character.
- That character becomes part of a string.
- Something receiving the string may interpret that character specially.
The meaning belongs to the receiving device.
Building styled strings
Because the attributes are characters inside strings, they can be combined with the string operations we've already learned.
Suppose we want to build an error message:
S.MOVE #message "\s01ERROR: "
S.JOIN #message #message "DISK NOT FOUND"
S.JOIN #message #message "\s0F"
D.TXT #message
D.BLT
The resulting string carries its formatting with it.
We can build its contents dynamically too:
#device "DISPLAY"
S.MOVE #message "\s1DWARNING: "
S.JOIN #message #message #device
S.JOIN #message #message " NOT READY"
S.JOIN #message #message "\s0F"
D.TXT #message
D.BLT
String construction works exactly as before. Some of the characters simply have a special meaning when the Display receives them.
Inline attributes and D.COL
Inline attribute characters aren't the only way to change how the Display draws text.
The Display also provides:
D.COL 1 0 0
which sets its active attributes separately from the text being written.
For example:
D.COL 1 0 0
D.TXT "ERROR"
D.COL 15 0 0
D.BLT
also displays ERROR using palette entry 1, then restores the default attributes.
The difference is where the attribute change lives.
With:
D.COL 1 0 0
D.TXT "ERROR"
the text itself contains only:
ERROR
The Display state was changed separately.
With:
D.TXT "\s01ERROR\s0F"
the attribute changes are characters carried inside the string itself.
We'll work with D.COL and the Display more directly in later lessons. For now, \sXX is particularly useful when attribute changes naturally belong inside the text.
Try it
Start with:
D.TXT "\s04READY\s0F"
D.BLT
Change the final hexadecimal digit of the first escape and see how the text changes.
Try palette entries such as:
1
2
6
9
D
E
Then invert one of them by changing:
\s0X
to:
\s1X
Build a line containing three differently styled sections:
READY WARNING ERROR
Choose the palette entries yourself, and make at least one section inverted.
Store the entire line in memory before displaying it:
S.MOVE #message "\s04READY \s1DWARNING \s01ERROR\s0F"
D.TXT #message
D.BLT
Use S.LEN to measure #message.
Before running the program, count the visible characters and the inline attribute characters separately. Predict the resulting length.
Then inspect one of the attributes:
S.ORD #code #message 0
S.HEX #code #code
Compare the hexadecimal result with the \sXX escape that produced it.
Finally, replace one of the \sXX escapes with its equivalent \uF1XX escape.
For example:
D.TXT "\s01RED\s0F"
D.TXT "\r\n"
D.TXT "\uF101RED\s0F"
D.BLT
The Display receives the same U+F101 character either way, so the two lines are styled identically.
Inline attributes let strings carry information about how the Display should present their text.
They are also ordinary Unicode characters inside the string, so the string operations we've already learned can store, inspect, combine, and manipulate them.
The Private Use Area gives NCS/e somewhere to define these characters without conflicting with Unicode's standardized meanings—and the same mechanism is available to your own programs.
In NCL 211: Working with Text, we'll bring the tools from the entire 200-level together.