Skip to content

Commit

Permalink
update font
Browse files Browse the repository at this point in the history
add grafixFont
add dep ft2build
remove image limit
bug in font rendering (fix in next commit)
  • Loading branch information
ManojTGN committed Apr 4, 2023
1 parent 07f6f4a commit bff6ab1
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 80 deletions.
81 changes: 81 additions & 0 deletions src/font.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "font.h"

int createGrafixFont(grafixFont *font, char* directory ,int fontSize){

FT_Library library;
if (FT_Init_FreeType(&library)) {
setGrafixError("grafixError:Unable To Load Library;reason:Can't Init FreeType;");
return 0;
}

if (FT_New_Face(library, directory, 0, &font->face)) {
setGrafixError("grafixError:Unable To Load Face;reason:unknow;");
return 0;
}

font->fontSize = fontSize;
font->directory = directory;
font->slot = font->face->glyph;

return 1;
}


void drawGrafixText(grafixWindow window, grafixFont font, int x, int y, char* text, grafixColor color){
if( WINDOWS[window.id] == NULL|| window.isDead ) return;

if (FT_Set_Pixel_Sizes(font.face, 0, font.fontSize)) {
setGrafixError("grafixError:Unable To Set FontSize;reason:unknow;");
return;
}

int width = 0, height = 0;
FT_GlyphSlot slot = font.face->glyph;
FT_Vector pen = {0, 0};

int text_length = strlen(text);
for (int i = 0; i < text_length; ++i) {
if (FT_Load_Char(font.face, text[i], FT_LOAD_RENDER)) {
continue;
}

pen.x += slot->advance.x >> 6;
pen.y += slot->advance.y >> 6;

if (pen.x > width) {
width = pen.x;
}
if (pen.y > height) {
height = pen.y;
}
}

unsigned char* buffer = (unsigned char*)malloc(width * height * sizeof(unsigned char));
if (!buffer) {
setGrafixError("grafixError:Unable To Create Buffer;reason:No Memory Available For Buffer;");
return;
}
memset(buffer, 0, width * height * sizeof(unsigned char));

pen.x = 0;
pen.y = 0;
for (int i = 0; i < text_length; ++i) {
if (FT_Load_Char(font.face, text[i], FT_LOAD_RENDER)) {
continue;
}

FT_Bitmap bitmap = slot->bitmap;
for (int _y = 0; _y < bitmap.rows; ++_y) {
for (int _x = 0; _x < bitmap.width; ++_x) {
_setPixel(window,x+_x+pen.x,y+_y+pen.y,(grafixColor){bitmap.buffer[y * bitmap.width + x],0,0});
// BUFFERS[window.id].frameBuffer[(pen.y + y) * width + pen.x + x] = bitmap.buffer[(y * bitmap.width + x) ];
// BUFFERS[window.id].frameBuffer[((pen.y + y) * width + pen.x + x)+1] = bitmap.buffer[(y * bitmap.width + x)+1 ];
// BUFFERS[window.id].frameBuffer[((pen.y + y) * width + pen.x + x)+2] = bitmap.buffer[(y * bitmap.width + x)+2 ];
}
}

pen.x += slot->advance.x >> 6;
pen.y += slot->advance.y >> 6;
}

}
17 changes: 17 additions & 0 deletions src/font.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef FONT_H
#define FONT_H

#include "grafix.h"

typedef struct FONT{
char* directory;
int fontSize;
FT_Face face;
FT_GlyphSlot slot;
}grafixFont;

int createGrafixFont(grafixFont *font, char* directory ,int fontSize);
void drawGrafixText(grafixWindow window, grafixFont font, int x, int y, char* text, grafixColor color);


#endif /* FONT_H */
3 changes: 0 additions & 3 deletions src/grafix.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,6 @@ int createGrafixWindow(grafixWindow* window, int WIDTH, int HEIGHT, char* NAME){
time._waitCounter = 0;
time._startCounter = clock();

// QueryPerformanceCounter(&TIMES[time.id]._startCounter);
// QueryPerformanceFrequency(&TIMES[time.id]._frequency);

TIMES[time.id] = time;

}
Expand Down
3 changes: 3 additions & 0 deletions src/grafix.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <ft2build.h>
#include FT_FREETYPE_H

#endif /* _DEPENDENCIES_H_ */

Expand All @@ -18,6 +20,7 @@
#include "event.h"
#include "time.h"
#include "image.h"
#include "font.h"

#endif /* _GRAFIX_DEPENDENCIES_H_ */

Expand Down
6 changes: 0 additions & 6 deletions src/image.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
#include "image.h"

static int IMAGE_ID;
grafixImage _IMAGES[MAX_IMAGE];

int createGrafixImage(grafixImage *image, const char* filePath, int fileType){
image->_isInit = 0;
image->id = IMAGE_ID++;
image->fileType = fileType;

FILE *image_file;
image_file = fopen(filePath, "rb");
if (image_file == NULL){
IMAGE_ID--;
setGrafixError("grafixError:Unable To Load Image;Reason:No File Found");
return 0;
}
Expand All @@ -27,7 +22,6 @@ int createGrafixImage(grafixImage *image, const char* filePath, int fileType){

image->_frame = (unsigned char*) malloc(image->width * image->height * 3);
if(image->_frame == NULL){
IMAGE_ID--;
fclose(image_file);
setGrafixError("grafixError:Unable To Load Image;Reason:No Memory Available");
return 0;
Expand Down
3 changes: 0 additions & 3 deletions src/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#define FT_JPG 2

typedef struct IMAGE {
int id;

char* filePath;
int fileType;
Expand All @@ -27,8 +26,6 @@ typedef struct IMAGE {

} grafixImage;

static int IMAGE_ID;
extern grafixImage _IMAGES[MAX_IMAGE];

int createGrafixImage(grafixImage *image, const char* filePath, int fileType);
void drawGrafixImage(grafixWindow window, grafixImage image, int x, int y);
Expand Down
65 changes: 0 additions & 65 deletions src/shapes.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,68 +129,3 @@ void drawGrafixCircle(grafixWindow window, int xc, int yc, int r, grafixColor co
}

}

void drawGrafixText(grafixWindow window, int x, int y, char* text, int fontSize, grafixColor color){
if( WINDOWS[window.id] == NULL|| window.isDead ) return;

HDC hdc = CreateCompatibleDC(NULL);

HFONT hFont = CreateFont(fontSize, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,"Arial");
SelectObject(hdc, hFont);

SIZE size;
GetTextExtentPoint32(hdc, text, strlen(text), &size);

RECT rc = { 0, 0, size.cx, size.cy };
DrawText(hdc, text, -1, &rc, DT_CALCRECT);

BITMAPINFO bmi = { 0 };
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = size.cx;
bmi.bmiHeader.biHeight = -size.cy;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;
unsigned char* pixels = (unsigned char*)malloc(bmi.bmiHeader.biWidth * bmi.bmiHeader.biHeight * 3);
pixels[0];

HBITMAP hBitmap = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**)&pixels, NULL, 0);
HGDIOBJ hOldBitmap = SelectObject(hdc, hBitmap);

if( color.red != 0 && color.green != 0 && color.blue != 0 ){
SetBkMode(hdc, TRANSPARENT);
}else{
SetBkMode(hdc,OPAQUE);
SetBkColor(hdc,RGB(255,255,255));
}

SetTextColor(hdc, RGB(color.red, color.green, color.blue));
DrawText(hdc, text, -1, &rc, DT_LEFT | DT_TOP);

int index = 0;int pindex = 0;
for(int i = x; i < x+size.cx; i++){
for(int j = y; j < y+size.cy; j++){
if(i >= window.width || j >= window.height || i < 0 || j < 0 ) continue;

index = (i + j * window.width) * 3;
pindex = ( (i-x) + (j-y) * size.cx) * 3;

if(
((color.red != 0 && color.green != 0 && color.blue != 0) &&
(pixels[pindex] == 0 && pixels[pindex + 1] == 0 && pixels[pindex + 2] == 0)) ||
((color.red == 0 && color.green == 0 && color.blue == 0) &&
(pixels[pindex] == 255 && pixels[pindex + 1] == 255 && pixels[pindex + 2] == 255))

)continue;

BUFFERS[window.id].frameBuffer[index] = pixels[pindex];
BUFFERS[window.id].frameBuffer[index + 1] = pixels[pindex + 1];
BUFFERS[window.id].frameBuffer[index + 2] = pixels[pindex + 2];
}
}

SelectObject(hdc, hOldBitmap);
DeleteObject(hBitmap);
DeleteDC(hdc);

}
3 changes: 0 additions & 3 deletions src/shapes.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,4 @@ void drawGrafixLine(grafixWindow window, int x1, int y1, int x2, int y2, grafixC
void drawGrafixRect(grafixWindow window, int x, int y, int width, int height, grafixColor color, int fillMode, int borderWidth);
void drawGrafixCircle(grafixWindow window, int x, int y, int radius, grafixColor color, int fillMode, int borderWidth);

//I haven't created a seperate header file
void drawGrafixText(grafixWindow window, int x, int y, char* text, int fontSize, grafixColor color);

#endif /* SHAPES_H */

0 comments on commit bff6ab1

Please sign in to comment.