Had space(more than 1px) between letters with custom font

Posted on
of 2
/ 2
  • Hello,
    I'm newb with espruino (i love using javascript for produce string... So gorgeous in front arduino...)
    Sorry for the stupid question.
    I've customized DennisFont8 to produce a DennisFont16, it works, but there is only 1 px (one pixel) between each letter, is there a way to had one more?
    To have 2 pix in fact.
    Best regards


    1 Attachment

    • 2017-03-18 11.37.20.jpg
  • Looks really nice! There's no 'padding' in drawString - I'm pretty sure it puts each character one after the other. I guess you have 2 options:

    • Change your font to add another column or 2 of pixels after each character
    • Override drawString - you should be able to do something like:

      g._ds = g.drawString;
      g.drawString = function(s,x,y) {
      for (var i=0;i<s.length;i++)
  • thanks @Gordon, and if i follow, the "10" is the step for letter width + kerning?

    The other way:
    i've used the online tool to convert and check the autoWidth, i should try with a fixed width with a double pixel as kerning...

  • I've try to compile with static width but the font doesnot seems to work, i've got an error with setDennisFont16() fonction...
    works with the js with automatic width, not with the fixed width...
    i'll try to ovewrite the default drawString().

  • @gordon, it works, BUT, the DennisFont has no fixed width. there is less width for an "i" or "l" than a "e"... and there is a gap...
    i've tried that

    g._ds = g.drawString;
      g.drawString = function(s,x,y) {
      for (var i=0;i<s.length;i++)

    it does not work for fine letter as i, l

    any idea?

  • I guess the font tool adds just 1px of padding, and it isn't configurable. Unfortunately it looks like it's server-side so you can't easily fix that. You could use the command-line tool that i originally made, and could modify the place where that adds an extra column? https://github.com/espruino/Espruino/blo­b/master/scripts/create_custom_font.js#L­58

    I guess what you could do is use Espruino to re-encode the fonts yourself. I'll see if I can come up with an example.

    Your use of g.stringWidth looks like it should work fine - could you maybe print the values you're getting from it and see if they are changing?

  • Ok, so this is more for your interest than anything else. Using an Espruino Pico with a PCD8544 display, this code actually creates a new double-size font (with spaces) on the Espruino itself:

    A5.write(0); // GND
    A7.write(1); // VCC
    var charFrom = 32;
    var charTo = 126;
    var charHeight;
    var charWidths, charData;
    function doFont() {
      var height = 6;
      var gbig;
      var gsmall = Graphics.createArrayBuffer(height*2, height, 1);
      var totalWidth = 0;
      for (var i=charFrom;i<=charTo;i++)
        totalWidth += gsmall.stringWidth(String.fromCharCode(i­)*2);
      charWidths = "";
      charHeight = height*2;
      charData = "";
      for (i=charFrom;i<=charTo;i++) {
        console.log("Char "+i);
        var ch = String.fromCharCode(i);
        var w = gsmall.stringWidth(ch);
        gbig = Graphics.createArrayBuffer(charHeight, w*2, 1,{msb:true});
        for (var y=0;y<height;y++)
          for (var x=0;x<w;x++)
            if (gsmall.getPixel(x,y))
        charData += E.toString(gbig.buffer);
    // Setup SPI
    var spi = new SPI();
    spi.setup({ sck:B1, mosi:B10 });
    // Initialise the LCD
    var g = require("PCD8544").connect(spi,B13,B14,B­15, function() {
      // When it's initialised, clear it and write some text
      g.setFontCustom(charData, charFrom, charWidths, charHeight);

    You could then re-encode it with btoa and make your own custom font again.

    It'll only work if width*height of each character in the new font is a multiple of 8, but for your case that should be fine.

    You should be able to modify that to just print your existing font (not doubling the size) while also adding a blank column.

    Honestly though - I'd just use the code I linked above and change the number for padding :)

  • Many Thanks @Gordon, it works great!
    I use it with DennisFont (cause i'm writing in french, have to had more chars, count until 255) with a height of height (arf)
    i keep line 4 to 34 and modifying as follow after

    function centre(text, y) {
      g.drawString(text, (g.getWidth() - g.stringWidth(text))/2, y);
    function init() {
      centre("en attendant",22);
      centre("en short",22);
      g.fillRect(10,0, g.getWidth()-20, 18);
      g.setFontCustom(charData, charFrom, charWidths, charHeight);
      centre("Ta m\xe8re",2);
      // affiche
    var g;
    function onInit() {
      // initialise l'écran oled
      g = require("SSD1306").connect(I2C1, init, { height : 32 });



    1 Attachment

    • 2017-03-27 23.38.25.jpg
  • Looks great!

    I quite like the pixellated look, but if you did want to do your own font it should be possible to find/make some tool that created a higher resolution font tile that you could then import.

  • Hi @Mrbbp,

    like the small oleds you are using, can you please share where you ordered them?

    @Gordon this is what I was thinking about for #978 - thanks for sharing!

  • Hi @MaBe,
    They come from ebay, hongkong
    0,98" 128x32 i2c oled display they are in white and blue. Be aware in the description

  • And i discovered that the é was 1pix larger. I redesign minus with 1 pix larger.
    The fontDennisAlternate

    /* Copyright (c) 2015 Dennis Bemmann. See the file LICENSE for copying permission. */
    from fontDennis8
    Redesign mrbbp

    g.drawString("\x90 \x91\x92\x93\x94\x95\x96\x97 Dennis8 \x9f \x8c \x8d \x89",0,0);
    g.drawString("ça ¿s� \xbc\xa3 \xbd\xb5\xa6 \x82\x83\x84\x85 \xac\xad\x86",0,12);
    g.drawString("\xa0 Bäh böse Ärzte in Küche\xaf",0,24);

    exports.add = function(graphics) {
      graphics.prototype.setFontDennis8Alt = function() {
        this.setFontCustom(font, 32, widths, 8);
  • Thanks, can you please share a link, because the last one I ordered did not work(ebay.de/itm/201537040004).

  • Thanks

  • Yes, I know what you mean about #978, but I think it's a pretty rare use case when the vector fonts are available - and it's pretty easy to work around with the font that @Mrbbp has just posted.

    @Mrbbp do you want to add this as a new font library on the Espruino site?

  • @Gordon Yes, but i'm not the original designer, i just modified the minus and accents... You have to ask to DennisFont designer...


  • By the way @Gordon, does the ide use a cache when compile with a new module?
    When update the font, i have to rename the module at each iteration to use the updated version in the espruino board.
    Is there a better/easier way to disable cache?


  • The IDE shouldn't cache the module... Are you serving it from a website somewhere? It could be that the website doesn't transfer it with the no-cache headers so something along the line (maybe even just Chrome) is cacheing it for you.

  • Hello,

    I go back up and update this post with a question about kerning again! (yes i'm graphic designer with graphic design concerns)

    I asked my students to draw a ** slanted ** font on an LED matrix to be able to work on the graphics of the texts displayed. (of course if the work is successful we will share the drawn fonts)

    This time I need to decrease the space between the letters (i think, not sure)

    Perhaps I am wrong on the mode of operation, but if the text algorithm, detects the last drawn pixel of a letter then adds a space of 1 pixels, on a slanted font, the kerning will be too large.

    I wish I could chain the letters a pixel or two kerning, like the example provided. (for my point of view, the kerning is too short)

    @Gordon, Do you think DrawString is able to draw font as in the picture?

    1 Attachment

    • test mot.jpg
  • some other test

    The first pic is with a kerning of 2 px (as it should be)
    The second pic is with a 1 px space between letters (as it should not)

    2 Attachments

    • test mot_2px.jpg
    • test mot_large.jpg
  • Hi - I'm afraid right now drawString won't handle any kind of overlap at all :( What I'd suggest is maybe drawImage? There's a neat function called createImage: http://www.espruino.com/Reference#l_Grap­hics_createImage

    So you could do:

    var font = { 
      A: {width:2,img:Graphics.createImage(...)},­
      B: {width:2,Graphics.createImage(...)},

    Then create your own drawString function?

  • ok!
    is there a simplier way to draw?
    can i draw with drawString only one letter in an image?
    i prefer the way you use a font with a picture than use x, space and carrier return...to draw my font... it look like in Arduino.

    well and after using drawImage i suppose i have to paste the drawing with g.drawImage(img,0,0)?
    can i overlapping ?
    0+1 = 1
    1+1 = 0
    1+0 = 1

    in Photoshop it's call "product" mode. black is transparent (0), white is opaque (1)


  • Here's a method to draw some text with whatever adjustment to the spacing you want:

    function drawStringWithSpacing(text, x, y, spacing) {
      for (var char of text) {
        g.drawString(char, x, y);
        x += g.stringWidth(char) + spacing;
  • thanks @NebbishHacker, but the question is now to overlap on previous drawn letter.
    Cause with a slanted font the width's letter is not the place for the next letter as in comment #21

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Had space(more than 1px) between letters with custom font

Posted by Avatar for Mrbbp @Mrbbp