Case of Missing Font

Once upon a time I was Visual Basic 6 programmer. These days one of those applications needed small update. I did change within five minutes, compiled it (in P-code because it doesn’t work when compiled as Native - but that is another story) and deployed it on network. Of course next day I got a call about a bug.

Barcode fonts

In those days it was hard to put barcode (Code 128 to be exact) on Data Report. Easiest path you could take was to use barcode font (or create it as I did) and store some textual representation of that code in database. When report is created, it would use that font to display text and by accident it would printout barcode. It wasn’t elegant solution but it worked.

The Bug

Since I don’t use same “barcode style” these days I hadn’t had that font installed on my system. For one reason or another something happened during compile that caused application to forget about my barcode font defined in Data Report. It just used same font as for rest of page which caused big numbers (my encoding of barcode used quite few of them) to appear where barcode should be.

Solution was quite simple. I installed that font on my Vista (yes I know, bug may as well be because VB6 is not supported on Vista officially) and recompiled application. Everything was ok this time.

It left me wondering however. Are there any other bugs that I introduced by simple fact of recompiling application?

Living in High DPI

What’s the problem?

Most of applications are pretty ignorant regarding DPI settings (including here a majority of mine applications also). Developers tend to optimize for 96 dpi and leave it at that. With more and more high DPI LCDs it is very hard for users to stay at that setting so lot of them are working on 120%.

On Windows XP this would look very bad if application is not high DPI aware but on Vista they decided to stretch client area of those misbehaving applications to match user selected dpi settings. Form is little bit blurry but it is better than element misplacement and text clippings. Old misbehaving applications look almost decent there.

Problem is that on Vista even applications that took effort to look good on higher DPI settings are affected with that scaling. All that work was done for nothing since Vista makes your application think it is working at 96 dpi.

Telling Vista that I am smart guy

There is a way to let Vista know that we took that additional effort. One just needs to call SetProcessDPIAware API (Vista only!) and Windows will leave us alone to manage our own interface. Since P/Invoke is not my favorite way of doing things from .NET I am happy that there is possibility of embedding it in manifest also.

If you have Visual Studio 2008 embedding this manifest is easy as creating new file but in older versions you will have some more work to do.

Some time later I will lead you through actual creation of well behaving application.

Default System Font

.NET Framework 1.0 and above

By Microsoft’s design guidelines you should be using system defined font for displaying user interface elements in your program. Of course, Microsoft found it very helpful to ignore that and assign Microsoft Sans Serif at 8.25 points as default font for all new .NET applications. Since both XP and Vista brought us new font (Tahoma at 8.25 points and Segoe UI at 9 points, respectively) most of .NET applications are just looking slightly out of place.

Solution is quite simple - you should only manually assign new font to form and all elements will pick up that setting (if you left them at default). Only problem is retrieving a font to use.

Proper way

Microsoft states that we should use GetThemeFont API to get this information. Problem is that this function does not exist in managed world and you don’t always have option to make P/Invoke calls (i.e. because of security settings) and portability also comes into question (Windows older than XP and mono).

SystemFonts.???

One would think that SystemFonts.DefaultFont would return what we need but for some reason known only to guy who made that function it returns Microsoft Sans Serif in almost all cases. Why does it ignore real windows default font is unknown to me. Some reflecting is probably needed but I tend to be on lazy side when there is no actual possibility to change anything.

Good choice is strangely SystemFonts.MessageBoxFont since this return proper font on all platforms I have used it. It is not official source for that information but should be good enough if you really want (or need) to stay in managed world.

If one wants to see example of it, I am happy to provide.

How to Choose Barcode Symbology

For one project I needed to select proper barcode symbology (way of encoding). Requirements were clear: variable length (that excludes EAN/UPC subset), some form of check-sum and it needs to work with standard (aka cheap) equipment. That left me with few candidates which I will try to describe.

2 of 5

This symbology is well supported since it is quite old (circa 1960) and it is used within industry. It is numeric only code and there is optional support for check-sum (modulo 10) digit, but that digit needs to be encoded and decoded by software (this may be a problem if you have some devices which are not under your control). Problem is also in width of code since this one encodes data only in bars which makes for a lot of wasted space. It’s low density could be a problem if you are “space challenged”.

To overcome excessive width of standard 2 of 5, somebody though of encoding data in both bars and spaces. That effectively made code twice as dense as standard one (or twice as short) for same amount of data but support is only available for even number of digits. Everything else said for standard version is same for this one.

Codabar (NW-7)

Illustration

Used mostly within blood banks this symbology allows for numbers with limited number of symbols (- $ : / . +). Since it is self-checking there is no additional check-sum needed but one can always use a software one. Code can start and end in four different ways (usually called A B C D) so there is possibility of differentiating code on that base also (but be aware of lock-in since no other code has that option). Since characters are separated with space code is not among shortest.

Code 3 of 9 (Code 39)

This is alphanumeric code and enables encoding numbers, characters (big case only) and some symbols (space - . $ / + % *). It is self checking so check-sum is not necessary but there is defined way of doing it if more security is needed. There is also possibility of concatenating multiple barcodes together but that possibility is rarely used (just don’t start your code with space). There is also extended variant that can encode whole ASCII (0-127) range.

Code 128

Illustration

This symbology is three in one deal. Three different encodings not only allow for full ASCII (0-127) to be encoded but there is also special double density mode which allows it to encode numeric only data in half the width. Check-sum (modulo 103) is mandatory part of encoding so no additional support is needed within software. Since symbols are also self-checking this gives very high confidence in reading. There were small problems with reading Code 128 with old barcode readers but everything that is recent on market supports it. Since there are three different ways of encoding data (and switching between them within) writing optimal encoder is not an easy task.

Conclusion

At the end, I selected Code 128. Not only that it gives highest possible level of security but it also has shortest code (with numeric data at double density). Slightly complex encoding was just another challenge that needed overcoming. C# source code is available.

Useful pages

Here are few pages where you can find more information - enough to write your own encoder.

Bug With CenterParent

One may think that centering a window is an easy task. Just set StartPosition to CenterParent and everything should work. This is true in most cases, however there is one bug that makes things more annoying than it should be.

Reproducing bug

First step should always be to reproduce buggy behaviour. In this case it is fairly easy. Just make new application with two forms. One parent we will at default settings and one child for which we will change StartPosition to CenterParent. On main form create two buttons. First button should show modal window (ShowDialog) and second should create just owned window (Show) like in this example.

When you click on second button you will notice that form isn’t centered at all. Every window created just goes to next default position.

Although this is definitely a bug, Microsoft decided not to fix it. Explanation is rather strange: “a fix here would be a breaking change to the behavior of WinForms 1, 1.1 and 2.”. I am not challenging that it would be a change to fix it but how many people would it really affect in negative manner. I personally cannot imagine one person setting child form to CenterParent and then being angry because form is centered.

Workaround

Like in “good old days” we need to center it manually. Do not forget to set StartPosition to manual and then just use some good old mathematics to fix it.

Resolution

Real solution would be to fix it in framework but since it is already rejected, I wouldn’t hold my breath for it. Easier solution (.NET Framework 3.5) would be to create extension method and solve it there. It is not resolution as such but it makes whole look nice when you need it.