Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums

Forums > C64 Coding > Variable Width Fonts and color alignment
2023-03-09 07:27

Registered: Jul 2009
Posts: 514
Variable Width Fonts and color alignment


I am writing a routine to display text on a hires bitmap screen with a variable width font ('W' is 7 pixels vs. 'I' is 3 pixels or whatever).

The routine is done and it's able to handle alignment to the left, right, centred and distributed (spaces are padded so the first and last word touches the left/right margin).

I started thinking about changing color on the text while keeping hte routine flexible in terms of alingment. I came up with the following example on how it might be done, but I can't help thinking I'm overcomplicating it:

Example: "FA" should be aligned within it's own '1-8' block:
  |123456781234567812345678123456781234567812345678| <- 1-8 blocks
  |do re mi ti FA so la ti to... end of line.      | <- Distributed aligned text, but last line so it's left aligned.

We move FA to the next 1-8 block:
  |do re mi ti     FA so la ti to... end of line.  |

We move 'so' to the next 1-8 block:
  |do re mi ti     FA      so la ti to... end of line.|

We centre FA in the middle of one or more 8 block(s):
  |do re mi ti        FA   so la ti to... end of line.|

We distribute spaces on the left block, ensuring 'ti' does not enter FA's block:
  |do   re  mi   ti   FA   so la ti to... end of line.|

The new "minimum" space distance is 2, so we apply that to the right section:
  |do   re  mi   ti   FA   so  la  ti  to...  end  of  line.|

We now need to shave off the end so the line fits within the available width:
  |do   re  mi   ti   FA   so  la  ti  to...  end  |
  |of line.                                        |

If the text is block aligned, the last word (end) must be aligned to the right margin:
  |do   re  mi   ti   FA   so  la  ti  to...    end|
  |of line.                                        |

Finally the spaces in the last block are be distributed:
  |do   re  mi   ti   FA   so  la   ti  to...   end|
  |of line.                                        |

And this off course is one word with different color. It might be every character changing word, it might be one color chane or it might be 10.

So it seems it would take a lot of checking and calculating to make it look right, and wanted to hear if anyone has some thoughts or input to the aproach?
2023-03-09 12:37

Registered: Jul 2006
Posts: 186
Depending on use case, it might be easier to burden user to align the words himself. Ie. if it will be used in demo, and never change, then you can align it by hand.

For the general use routine your solution seems right.

I would however change the color on the closest 1-8 block boundary and do not mess with the spacing at all. It wouldn't look as good in some cases, but for some would be ok, and would be much simpler to implement.

Or you could add some special characters to manipulate how the text is supposed to be displayed. IE. change spacing betwen letters to 1,2,3... pixels. Change the spacing betwen words to 1,2,3... pixels, etc.

All depends on use cases.
2023-03-10 00:18

Registered: Jul 2003
Posts: 490
My suggestion is similar to TWW's. My proportional text editor back in the days had some different width space characters for alignment. BG/FG coloring was also done by adding invisible characters to the text. So, it was up to the author to do the alignments. If i recall correctly, $e0-$ef was being used for BG colors and $f0-$ff for FG colors. It was a WYSIWYG editor. So, it was easy to see any color clashes and fix by adding some prefix/postfix space characters. And you were able to delete all invisible characters like colors when you press backspace key. In other words, there were no seperate tables, everything was embedded in the text data.

This is where my editor was used.
64 Times #1
2023-03-10 05:59

Registered: Aug 2004
Posts: 1270
Reverse background and foreground, do all the colours with an expanded multicolour sprite underlay, enforce a minimum 3 pixel gap at any colour change (either by only colour changing on word boundaries, or having 3px between letters).

Any letter can then be any of d021/sprite mcm1/sprite mcm2, or the sprite colour for that group of 6 chars.

Orrr, seeing as you're targeting bitmap mode and already have one bg colour per char, use an expanded hires sprite underlay, then you only need a 1px gap between letters for arbitrary split positions and up to 8 colours available for patching splits that occur mid character.

But failing that, yes your approach seems sound. Don't forget you can adjust the split positions by using X-scroll. And I concur with others that if it's for a limited set of strings known in advance there's a lot to be said for just hand tuning it.
2023-03-10 08:30

Registered: Jul 2009
Posts: 514
Thanks for the suggestions. Slight clarification;

The routine is not an editor, but should be able to take some random text (f.eks. the "Lorem ipsum.." placeholder text) and be able to handle any color change within the sentence (also preferably be able to handle colorchanges in a word if you f.ex. wanted to write "rainbow" with each letter coloured).

I think the aproach would have to be to:
- calculate width of one line of text
- map spaces (used for padding out the line if it's margin aligned left/right)
- count any color blocks
- adjust text within each color block
- adjust spaces as best possible and push any excess to next line.

The sprite idea is good and perhaps if the # of color blocks within a line is below, say 4, SC sprites could be put to handle these cases and the line could be done without any color limit alignment.
2023-03-10 13:36

Registered: Feb 2009
Posts: 251
A simpler approach is to make every space between words of different colour a minimum of 7 pixels wide. That ensures you won't get clashes.

It means the fitting of text on a line won't be optimal, but in practice nobody will notice.
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Users Online
Guests online: 96
Top Demos
1 Edge of Disgrace  (9.6)
2 Coma Light 13  (9.6)
3 Uncensored  (9.6)
4 Comaland 100%  (9.6)
5 Lunatico  (9.6)
6 Bromance  (9.5)
7 E2IRA  (9.5)
8 Unboxed  (9.5)
9 The World Is Not Eno..  (9.5)
10 Memento Mori  (9.5)
Top onefile Demos
1 Party Elk 2  (9.9)
2 Copper Booze  (9.6)
3 Barry Boomer - Trapp..  (9.5)
4 Dawnfall V1.1  (9.5)
5 Daah, Those Acid Pil..  (9.5)
6 Onscreen 5k  (9.5)
7 POKE 56576,1 for Unl..  (9.5)
8 No Mercy for the Tro..  (9.4)
9 Offering  (9.4)
10 Selbuvotter Latitudes  (9.4)
Top Groups
1 Booze Design  (9.4)
2 Censor Design  (9.3)
3 Crest  (9.3)
4 Oxyron  (9.2)
5 Maniacs of Noise  (9.2)
Top Graphicians
1 Talent  (9.9)
2 Mirage  (9.8)
3 Archmage  (9.8)
4 Electric  (9.7)
5 Mikael  (9.7)

Home - Disclaimer
Copyright © No Name 2001-2023
Page generated in: 0.052 sec.