Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Courier Ception

The program must accept any string as input and output an pixel image that shows the input string in Courier. All the letters that contain a 'hole' (like abdegopqABDPQR etc) that is surrounded by black pixels must be also filled black.


The Program must be able to accept any ASCII string as input. The input can be any way you want, as long as the program code itself does not have to change in order to accept a different input. (Except for e.g. the filename of the file that is to be read.) No standard loopholes. You can assume that each input contains at least one printable letter.


The output must be a black and white (no gray) pixel graphic that shows the string written in Courier (in black, background white), with the specified 'holes' filled. The fontsize of the whole string must be constant (that means no different scaling for each different letters) so that the full sized letters (e.g. ABCDEFGHIJKLMNOPRSTUVWXYZ but j and Q are bigger) must be at least 10px in height. (You do not have to write it to a file, any kind of display is ok as long as it is generated as pixelgraphics, as e.g. canvas in JavaScript.) Please post this graphic with your answer.

Access to courier.ttf / font libraries is allowed.

The program must as well count the number of black pixels and write it to console or what ever output method prefered in the end.


The score is evaluated as follows: The full program code must be used as input string to your program. The number of black pixels will be your score. Programcode that contains no printable or non-ASCII letters is not allowed. (As well as standard loopholes.) The lower the score the better.

share|improve this question
Output is specified as b/w, so no grey pixels allowed. And thank you for asking about the encoding, this is gonna be limited to ASCII because of the font. – flawr Jul 18 '14 at 19:40
There are at least two serious problems which need clarification. Firstly, it's not clear whether the program is allowed access to a Courier.ttf and to font libraries which can use it. Secondly, do you seriously intend to prohibit programs which don't contain non-ASCII characters? – Peter Taylor Jul 18 '14 at 19:59
Yes, access to Courier.ttf and font libraries is allowed - it wouldn't make sense otherwise, thank you. I was not able to come up with any major language that does rely on non-ASCII characters - do you know any languages that need non-ASCII characters? – flawr Jul 18 '14 at 21:51
@flawr APL. And in Mathematica you could shorten a few things using Unicode characters. – Martin Ender Jul 18 '14 at 21:53
Is ImageMagick allowed? – Digital Trauma Jul 18 '14 at 22:20

Mathematica, 4864 pixels

l = ImageData[Binarize[Rasterize[Style[j, FontSize -> 15]], .71]]
i = {{1, 1}}
While[Length[i] > 1 - 1,
 {r, c} = j = i[[1]]; l[[r, c]] = 2; i = i[[2 ;; -1]];
 If[FreeQ[i, {r, c} = J = j + #] && l[[r, c]] == 1, 
    i = i~Join~{J}] & /@
  {{1, 1 - 1}, {1 - 1, 1}, {-1, 1 - 1}, {1 - 1, -1}}
Image[l = l /. 1 -> 1 - 1 /. 2 -> 1]
Count[l, 1 - 1, {2}]

Here is the picture:

enter image description here

In Mathematica when you write a "program" you just write a snippet. So this expects the input stored in j and the last thing it returns is the image and the count. This also spits out a bunch of errors, because I don't do bounds checking on l, but it produces the desired result anyway.

where % refers to said last output.

Thanks to Geobits for the idea for the algorithm. I'm flood-filling the image from the top-left corner with an invalid intensity, then I replace all remaining white pixels with black pixels, and the invalid ones with white ones.

Note that the FreeQ check isn't actually necessary for the program to work correctly, but for it to finish in a reasonable amount of time. If I'd leave it out, I'd actually score about 300 pixels less.

share|improve this answer
The counting should be part of the program itself! Other than that it's a nice solution, did you calculate which letters (as variable names) use the least number of pixels? – flawr Jul 18 '14 at 21:55
@flawr yes :) (now for all of them) (fixing the counting in a second) – Martin Ender Jul 18 '14 at 22:37


Haven't handled the counting yet. I'll probably need to wrap it in bash.

Renders each character normally, to get the correct spacing to advance, then takes all the curves which describe the glyph and fills each one. Normally the interior and exterior curves are described with different orientations, so a fill will leave the interior empty, regardless of whether it's using the non-zero winding rule or and even-odd rule. Filling separately, it all gets filled.

/Courier 60 selectfont
50 400 moveto

(a test of DOPQ)
    ( ) dup 0 4 3 roll put 
    currentpoint 3 2 roll
    dup show
    currentpoint 3 2 roll
    5 3 roll moveto
    true charpath
    [ [{/moveto cvx}{/lineto cvx}{/curveto cvx}{/closepath cvx]cvx[}pathforall pop] ]
    { exec currentpoint fill moveto } forall
    pop moveto
} forall

snip from output

share|improve this answer
I did not expect anyone using the vector data of teh font, but that a really neat solution! – flawr Jul 22 '14 at 8:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.