SprinkleOS/text.c

128 lines
3.3 KiB
C
Executable File

#include "text.h"
#include <Library/MemoryAllocationLib.h>
#include <Library/PrintLib.h>
#include <Protocol/SimpleFileSystem.h>
#include "fb.h"
#include "font.h"
/*
char keycode[] = {
0x0,
};
*/
/*
void textinit(EFI_SIMPLE_FILE_SYSTEM_PROTOCOL* fsp) {
EFI_FILE_PROTOCOL *root, *file;
EFI_STATUS Status = fsp->OpenVolume(fsp, &root);
if (EFI_ERROR(Status)) {
Print(L"Error:Failed to open root filesystem - %r", Status);
return;
}
Status = root->Open(root, &file, L"font.bdf", EFI_FILE_MODE_READ,
EFI_FILE_READ_ONLY);
if (EFI_ERROR(Status)) {
Print(L"Error:Failed to open font file - %r", Status);
return;
}
UINTN buf_size = 1;
char* fontfiledata = AllocateRuntimeZeroPool(buf_size);
if (!fontfiledata) {
Print(L"Error:Failed to allocate font memory");
return;
}
Status = file->Read(file, &buf_size, (VOID*)fontfiledata);
if (Status == EFI_BUFFER_TOO_SMALL) {
FreePool(fontfiledata);
fontfiledata = AllocateRuntimeZeroPool(buf_size);
if (!fontfiledata) {
Print(L"Error:Failed to allocate font memory");
return;
}
Status = file->Read(file, &buf_size, (VOID*)fontfiledata);
if (EFI_ERROR(Status)) {
Print(L"Error:Failed to read font file - %r", Status);
return;
}
} else if (EFI_ERROR(Status)) {
Print(L"Error:Failed to read font file - %r", Status);
return;
}
file->Close(file);
root->Close(root);
}*/
void textout(UINT8 code) {
static UINTN x = 0, y = 0;
if (code == '\n') {
x = 0;
y++;
return;
}
for (int row = 0; row < 13; row++) {
for (int column = 0; column < 8; column++) {
EFI_GRAPHICS_OUTPUT_BLT_PIXEL *pixel =
(EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)buffer.BaseAddress +
y * 13 * buffer.HorizontalRes + row * buffer.HorizontalRes +
8 * x + column;
if ((font[code][row] >> (7 - column)) & 0x1)
RGBtoPixel(pixel, 0xFFFFFF);
}
}
if (x < (buffer.HorizontalRes / 8) - 1)
x++;
else if (y < buffer.VerticalRes / 13) {
x = 0;
y++;
}
}
void print(char *string) {
do {
textout(*string);
string++;
} while (*string != '\0');
}
UINT64 pow(UINT64 base, UINT64 exponent) {
UINT64 ans = 1;
for (UINT64 i = 0; i < exponent; i++) {
ans *= base;
}
return ans;
}
void printhex(UINT64 num, UINT8 digit) {
if (digit == 0) {
UINT8 maxdigit = 0;
while (num >= pow(16, maxdigit)) maxdigit++;
print("0x");
if (maxdigit == 0) textout('0' + num);
while (maxdigit) {
UINT64 weight = pow(16, maxdigit - 1);
UINT64 ans = num / weight;
if (ans < 10)
textout('0' + ans);
else
textout('A' + ans - 10);
num -= ans * weight;
maxdigit--;
}
} else {
print("0x");
while (digit) {
UINT64 weight = pow(16, digit - 1);
UINT64 ans = num / weight;
if (ans < 10)
textout('0' + ans);
else
textout('A' + ans - 10);
num -= ans * weight;
digit--;
}
}
}