Draw a variable width psf glyph in the framebuffer

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
pwhx
Posts: 1
Joined: Fri Sep 02, 2022 11:36 am

Draw a variable width psf glyph in the framebuffer

Post by pwhx »

Ok so I've been working on this for days now, but I can't display a glyph on the framebuffer.
I was able to read the info in the psf header very well, and now I need to display some characters.
The problem with the wiki tutorial (https://wiki.osdev.org/PC_Screen_Font) is that it only deals with 8-bit wide fonts, while I would like to be able to support any psf font.

I came across this post: viewtopic.php?f=1&t=41549&start=0. It describes my problem quite well, but even adapting my code with the remarks made there nothing works.

I also want to mention that I use a 12x24 font, and absolutely NOTHING appears on the screen when I try my putchar function (or I don't see it?)

Here is my code:

Code: Select all

void fb_putchar(char c) {
  if (c == '\n')
    goto newline;
  int line;
  int bytesperline = font->width/32;
  int offset = (y_pos * font->height * pitch) +
        (x_pos * (font->width+1) * 4);

  u8 *glyph = (u8 *)&_binary_font_psf_start + font->headersize +
              c * font->bytesperglyph;
  serial_print("%x\n", glyph);
  for (u32 y = 0; y < font->height; y++) {
    line = offset;
    for (u32 x = 0; x < font->width; x++) {
      *((u32 *)(fb + line)) =
          glyph[x / 8] & (0x80 >> (x & 7)) ? fg_color : bg_color;
      line += 4;
    }
    glyph += bytesperline;
    offset += pitch;
  }
  x_pos++;
  if (x_pos >= width / 8) {
  newline:
    y_pos++;
    x_pos = 0;
    fb_putchar(' ');
  }
}
Does anyone notice a problem in my code, and is willing to help me?

Thank you very much, and I hope my post is correct, since it's the first one I'm doing here.
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Draw a variable width psf glyph in the framebuffer

Post by Octocontrabass »

Code: Select all

  int bytesperline = font->width/32;
This is bytes per glyph line, right? If so, there are two problems: there are 8 bits per byte, not 32, and division rounds down but you need it to round up.

Code: Select all

      *((u32 *)(fb + line)) =
What type is "fb"? You might be running into pointer arithmetic problems.

Code: Select all

  if (x_pos >= width / 8) {
This looks like it might be wrong.

Code: Select all

    fb_putchar(' ');
Why is this part of moving to the next line?
Post Reply