OSDev.org

The Place to Start for Operating System Developers
It is currently Sat Jan 23, 2021 11:45 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 35 posts ]  Go to page Previous  1, 2, 3
Author Message
 Post subject: A question of the transform between .SFN and .O
PostPosted: Tue Aug 11, 2020 7:53 am 
Offline

Joined: Tue Aug 11, 2020 7:18 am
Posts: 1
Hello everyone,

I'm currently developing a really simple GUI for my operating system. And I've just found this 1-year-ago topic of SSFN from this post: https://wiki.osdev.org/Scalable_Screen_Font

Recently I want to add the ability of rendering text (more precisely, Unicode) on VRam, I've tried to use PSFs format & plain data written in array, but these method are too... clumsy and difficult to maintain, so I changed my plan and started to use SSFN library to render text.

Unfortunately, my OS is still in early-stage. I cannot use any file function such as
Code:
fread()
, I couldn't read data from SFN file directly in my project directory.
The only way to read data from SFN, is to convert .sfn to .o file via objcopy when building, after that I can link datas in my code.

I found that there are a few fonts attached to the SSFN directory, I moved one of them, FreeSans.sfn, into my project directory. I simply created fonts.c and packaged an API:

Code:
#define SSFN_CONSOLEBITMAP_HICOLOR
#include "font.h"
#include "ssfn.h"

extern char _binary_drivers_FreeSans_sfn_start;

/*
* @brief Plot a character on the VRam.
*
* @param vram Video RAM,
* @param bpr Bytes per row.
* @param x X axis of the char.
* @param y Y axis of the char.
* @param w Width of the screen.
* @param h Height of the screen.
* @param fg Foreground color.
* @param bg Background color.
* @param unicode Unicode of the char.
*/
void plot_font(uint8_t *vram, uint16_t bpr, int x, int y, int w, int h,
               uint32_t fg, uint32_t bg, uint32_t unicode) {
    ssfn_src = &_binary_drivers_FreeSans_sfn_start;
    ssfn_dst.ptr = vram;
    ssfn_dst.p = bpr;
    ssfn_dst.fg = fg;
    ssfn_dst.bg = bg;
    ssfn_dst.x = x;
    ssfn_dst.y = y;
    ssfn_dst.w = w;
    ssfn_dst.h = h;

    ssfn_putc(unicode);
}

My Makefile:
Code:
C_SOURCES = ......
HEADERS = ......
OBJ = ${C_SOURCES:.c=.o ...... drivers/FreeSans.o}

CC = /path/to/cc
GDB = /usr/local/bin/gdb
CFLAGS = -g -ffreestanding -Wall -Wextra -fno-exceptions -m32

.............(omitted)

%.o: %.sfn
   i386-elf-objcopy -O elf32-i386 -B i386 -I binary $< $@


My project was successfully built, however it couldn't print the character 'A' when I use it in kernel.c. So I started debug. And I found that, in int ssfn_putc(uint32_t unicode), there's a line of code:
Code:
    if(!ssfn_src || ssfn_src->magic[0] != 'S' || ssfn_src->magic[1] != 'F' || ssfn_src->magic[2] != 'N' ||
        ssfn_src->magic[3] != '2' || !ssfn_dst.ptr || !ssfn_dst.p) return SSFN_ERR_INVINP;

The whole function was returned due to this if statement. I printed ssfn_src->magic via gdb and found that, there is no magic SFN number of my .O file.Hence, my transform from .SFN to .O wasn't correct.

So, my question is, how do I transform .sfn to .o correctly? If there is no such method, are there any alternatives :?:


Top
 Profile  
 
 Post subject: Re: Simple font rendering library
PostPosted: Fri Aug 14, 2020 3:17 pm 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1174
Hi,

Thanks for checking out!

TLHorse wrote:
I cannot use any file function
Don't you worry, this is quite common for kernels. That's why SSFN expects an in memory byte array without any file-related callbacks.

Your "plot_font" function is not okay. You should set up the ssfn_src and ssfn_dst variables in the initialize phase, and then only call ssfn_putc(). This is because ssfn_putc() calculates and adjusts the pen coordinate. Imagine you write an "A", which is 8 pixel wide, then x should be increased by 8. Then imagine that you print a CJK ideogram, which is double wide. In that case x should be increased by 16. If you set ssfn_dst.x every time you plot a character, you'll loose that information.

TLHorse wrote:
I printed ssfn_src->magic via gdb and found that, there is no magic SFN number of my .O file.Hence, my transform from .SFN to .O wasn't correct.
The question is, does it contain exactly the same bytes as the file?

First of all, if you have downloaded the font directly from the repo, then it is very likely gzip compressed. The normal renderer ssfn_load()/ssfn_render() can transparently uncompress those fonts, but since ssfn_putc() is not allowed to make any function calls (not even malloc) it requires an inflated font.

From the repository's main README:
Quote:
IMPORTANT NOTE: unlike the normal renderer, this one (ssfn_putc) does not handle gzip compressed fonts. Always pass an inflated font in ssfn_src.

And from the docs/API.md section Render a Glyph with ssfn_putc:
Quote:
Unlike the functions in the normal renderer, this function does not check its input, can't uncompress gzipped fonts on its own, and it can't handle font collections either. Passing invalid parameters results in an undefined behaviour. This is as simple as it gets.

As well as from docs/sfn_format.md in section Header
Quote:
If the file starts with the bytes 0x1f and 0x8b, then it is stream compressed. You might want to uncompress it first using a gzip decompression filter (inflate). For command line, use gzip -d. The normal renderer is able to uncompress these fonts transparently, but in lack of memory management the simple renderer can't.

(Just a side note, if you use Midnight Commander F3 View, that also transparently uncompresses gzip, so you'll see the magic there while the file actually starts with the bytes 0x1F 0x8B.)

Try
Code:
mv FreeSans.sfn FreeSans.sfn.gz
gzip -d FreeSans.sfn.gz
(gzip will rename .sfn.gz back to .sfn) After that check if the file starts with the "SFN2" magic. Then repeat the objcopy command and check in gdb that sfn->magic contains "SFN2" as well.

Btw, using objcopy is a good solution. Under POSIX compliant systems (read non-MacOS) I also like to use the ld command, like this:
Code:
i386-elf-ld -r -b binary -o font.o FreeSans.sfn


Second, that FreeSans.sfn contains a vector font. Only the normal renderer can render those, ssfn_putc() is limited to bitmap fonts. Try downloading unifont.sfn.gz or u_vga16.sfn.gz (those are bitmap console fonts).

Alternatively you can rasterize any vector font into a bitmap font using sfnconv -B (height), for example
Code:
./sfnconv -U -B 16 FreeSans.sfn sansconsole.sfn
(where -U tells sfnconv to save uncompressed (non-gzipped) font, and -B 16 means rasterize the vector font at 16 pixels and save as bitmap font.)

Finally, if you really wish to use scalable vector fonts on your console (not recommended), then use the normal renderer, read the API doc on memory management for details.

Cheers,
bzt


Top
 Profile  
 
 Post subject: Re: Simple font rendering library
PostPosted: Wed Aug 19, 2020 3:15 am 
Offline

Joined: Wed Aug 19, 2020 2:59 am
Posts: 1
Hello @bzt

Would like to ask some questions about Scalable Screen Library Font 2.0 regarding using it in another free software proyect and maybe requiring some help to start integration.

Please /msg me, I tried to add you as friend. Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Simple font rendering library
PostPosted: Fri Aug 21, 2020 7:05 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1174
rabsym wrote:
Hello @bzt

Would like to ask some questions about Scalable Screen Library Font 2.0 regarding using it in another free software proyect and maybe requiring some help to start integration.

Please /msg me, I tried to add you as friend. Thanks in advance.
PM sent. In general SSFN is a Free and Open Source Software licensed under the terms of MIT license. For getting help, please use the gitlab's issue tracker.

Cheers,
bzt


Top
 Profile  
 
 Post subject: Re: Simple font rendering library
PostPosted: Fri Oct 16, 2020 4:07 am 
Offline
Member
Member

Joined: Mon Jun 01, 2020 8:15 am
Posts: 28
Wow nice!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 35 posts ]  Go to page Previous  1, 2, 3

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group