added font support

+ Kobject
+ TTY
This commit is contained in:
Morten Delenk 2017-04-23 20:04:39 +00:00
parent 2c92af7cb0
commit b8d42f1506
9 changed files with 63747 additions and 1 deletions

1
.gitignore vendored
View file

@ -5,3 +5,4 @@ gawk/
builddir/
config.cmake
config.h
gen*.h

View file

@ -52,4 +52,32 @@ with open("config.h", "w") as f:
f.write("#define "+key+" ("+str(val)+")\n")
else:
f.write("#define "+key+' "'+val+'"\n')
if "ENABLE_FRAMEBUFFER_UNICODE" in config:
#Write the font
chars=range(65536)
if not config["ENABLE_FRAMEBUFFER_UNICODE"]:
chars = [0x0000, 0x263A, 0x263B, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022, 0x25D8, 0x25CB, 0x25D9, 0x2642, 0x2640, 0x266A, 0x266B, 0x263C,
0x25BA, 0x25C4, 0x2195, 0x203C, 0x00B6, 0x00A7, 0x25AC, 0x21A8, 0x2191, 0x2193, 0x2192, 0x2190, 0x221F, 0x2194, 0x25B4, 0x25BC]
chars += list(range(0x20, 0x7F))
chars += [0x2302]
chars += [0xC7, 0xFC, 0xE9, 0xE2, 0xE4, 0xE0, 0xE5, 0xE7, 0xEA, 0xEB, 0xE8, 0xEF, 0xEE, 0xEC, 0xC4, 0xC5,
0xC9, 0xE6, 0xC6, 0xF4, 0xF6, 0xF2, 0xFB, 0xF9, 0xFF, 0xD6, 0xDC, 0xA2, 0xA3, 0xA5, 0x20A7, 0x0192,
0xE1, 0xED, 0xF3, 0xFA, 0xF1, 0xD1, 0xAA, 0xBA, 0xBF, 0x2310, 0xAC, 0xBD, 0xBC, 0xA1, 0xAB, 0xBB,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0]
chars.sort()
ch=chars
chars=[]
for c in ch:
chars.append("{0:0{1}X}".format(c,4))
x=open("unifont.hex").readlines()
from subprocess import *
p = Popen(["./fontgen.py"], stdin=PIPE)
for l in x:
if l[:4] in chars:
p.stdin.write(l.encode("UTF-8"))
p.stdin.close()
p.wait()

60
fontgen.py Executable file
View file

@ -0,0 +1,60 @@
#!/usr/bin/env python3
"""
Generates a kernel/src/include/genfont.h from stdin
"""
import binascii
import sys
f = open("kernel/src/include/genfont.h", "w")
f.write("//Generated by fontgen.py\n")
lines = sys.stdin.readlines()
font = {}
#font -> unicode, font pair
print("Parsing")
for l in lines:
font[int.from_bytes(binascii.unhexlify(
l[:4]), byteorder="big")] = binascii.unhexlify(l[5:-1])
fontbytes = bytearray()
widths = []
offs = []
for i in range(65536):
if not i in font:
widths.append(0)
offs.append(None)
else:
fo = font[i]
widths.append(len(fo)//2)
offs.append(len(fontbytes))
fontbytes += font[i]
print("Writing")
f.write("char font_widths[] = {\n")
for i,width in enumerate(widths):
if not i % 32:
f.write(" ")
f.write(str(width)+",")
if i % 32 == 31:
f.write("\n")
f.write("};\n")
f.write("char font_data[] = {\n")
for i,c in enumerate(fontbytes):
if not i % 32:
f.write(" ")
f.write(hex(c)+", ")
if i % 32 == 31:
f.write("\n")
f.write("\n};\n")
f.write("char *font_ptr[] = {\n")
for i,off in enumerate(offs):
if not i % 8:
f.write(" ")
if off is None:
f.write("0,")
else:
f.write("&(font_data["+hex(off)+"]),")
if i % 8 == 7:
f.write("\n")
f.write("};")
print("done.")

View file

@ -0,0 +1,3 @@
config["ENABLE_FRAMEBUFFER"] = get_yes_no("Use VESA Framebuffer?", True)
if config["ENABLE_FRAMEBUFFER"]:
config["ENABLE_FRAMEBUFFER_UNICODE"] = get_yes_no("Enable full Unicode BMP font (Adds around 1MB to binary)", False)

0
kernel/src/include/.keep Normal file
View file

View file

@ -0,0 +1,48 @@
#pragma once
enum class kobjectType {
POINTER, //Pointer to non-object
KOBJECT, //garbage collected kobject
};
class Kobject {
public:
kobjectType type;
void *ptr;
unsigned int refctr;
bool set;
Kobject(kobjectType type) {
this->type = type;
refctr = 1;
set = false;
}
template<typename T>
auto set(T *data) -> Kobject & {
if(set)
return *this;
ptr = (void*)data;
set=true;
return *this;
}
template<typename T>
T *get() {
return (T*)ptr;
}
auto operator++(int) -> Kobject & {
unsigned int old = refctr;
refctr++;
if(old > refctr) {
//uh-oh this shouldn't happen
//Put panic() here, because this object could lead to kernel execution when the refctr overflows.
for(;;);
}
return *this;
}
auto operator--(int) -> Kobject & {
refctr--;
if(!refctr) {
//delete ptr;
set = false;
ptr = nullptr;
}
return *this;
}
};

View file

@ -0,0 +1,65 @@
#pragma once
enum Color {
BLACK,
BLUE,
GREEN,
CYAN,
RED,
MAGENTA,
BROWN,
LIGHT_GREY,
GRAY,
LIGHT_BLUE,
LIGHT_GREEN,
LIGHT_CYAN,
LIGHT_RED,
LIGHT_MAGENTA,
YELLOW,
WHITE
};
class TTY {
protected:
int x;
int y;
int width;
int height;
Color curColor;
unsigned int rgbColor;
bool useRGB;
virtual auto plotChar(int x, int y, int c) -> void;
public:
TTY(int width, int height);
virtual ~TTY();
virtual auto rgbSupport() -> bool;
virtual auto setColor(Color c) -> void;
virtual auto setColor(unsigned int c) -> void;
virtual auto putChar(int c) -> void;
virtual auto puts(const char *s) -> void;
template<typename T>
auto puti(T x) -> TTY & {
T output = x;
const char* chars="0123456789ABCDEF";
char buf[sizeof(T)*8+1];
buf[sizeof(T)*8]='\0';
char* ptr=buf+sizeof(T)*8-1;
do {
*(ptr--)=chars[output&0xF];
output>>=4;
} while(output && (ptr != buf));
puts(ptr+1);
return *this;
}
template<typename T>
auto operator<<(T x) -> TTY & {
puts(x);
return *this;
}
};
template <>
auto TTY::operator<<<int>(int) -> TTY &;
template <>
auto TTY::operator<<<unsigned int>(unsigned int) -> TTY &;
template <>
auto TTY::operator<<<long long>(long long) -> TTY &;

55
kernel/src/tty.cpp Normal file
View file

@ -0,0 +1,55 @@
#include <tty.hpp>
TTY::TTY(int width, int height): x(0), y(0), width(width), height(height), curColor(WHITE), rgbColor(0xFFFFFF), useRGB(false) {}
TTY::~TTY() {}
auto TTY::plotChar(int x, int y, int c) -> void {}
auto TTY::rgbSupport() -> bool {return true;}
auto TTY::setColor(Color c) -> void {
curColor = c;
useRGB=false;
}
auto TTY::setColor(unsigned int c) -> void {
rgbColor = c;
useRGB=true;
}
auto TTY::puts(const char *str) -> void {
for(int i=0;str[i];i++) {
//Decode UTF-8
int character=0;
if(str[i]<0x80) {
character=str[i];
}
//UTF-8 character
if((str[i]&0b11000000)==0b10000000) {
character=0xFFFD; //ILLEGAL SEQUENCE
}
//Now count how many bytes follow
int bytesToFollow=0;
if((str[i]&0b11100000)==0b11000000) {
bytesToFollow=1;
character=str[i]&0b11111;
} else if((str[i]&0b11110000)==0b11100000) {
bytesToFollow=2;
character=str[i]&0b1111;
} else if((str[i]&0b11111000)==0b11110000) {
bytesToFollow=3;
character=str[i]&0b111;
} else if(!character)
character=0xFFFD; //ILLEGAL SEQUENCE
while(bytesToFollow) {
i++;
if(!str[i]) {
putChar(0xFFFD); //Incomplete
return;
}
if((str[i]&0b11000000)!=0b10000000) {
putChar(0xFFFD); //ILLEGAL SEQUENCE
break;
}
character <<= 6;
character |= str[i] & 0b111111;
bytesToFollow--;
}
if(!bytesToFollow)
putChar(character);
}
}

63486
unifont.hex Normal file

File diff suppressed because it is too large Load diff