我使用 SDL 库模拟了一个屏幕。我想在这个屏幕上绘制字体,只需使用我已经制作的“draw_pixel”函数即可。
网上查了很多资料。 我找到了这个网站http://jared.geek.nz/2014/jan/custom-fonts-for-microcontrollers并且代码运行良好。但是它不支持可变宽度字符
我只想使用源代码。
您能告诉我是否有源代码或光源库可以从像素绘制字体吗?
编辑:这是我从 M Oehm 答案更改的代码。
int DrawChar(char c, uint8_t x, uint8_t y, int r, int g, int b, SDL_Surface *rectangle, SDL_Surface *ecran, SDL_Rect position)
{
uint8_t i,j;
// Convert the character to an index
c = c & 0x7F;
if (c < ' ') {
c = 0;
} else {
c -= ' ';
}
const uint8_t* chr = font[c];
int w = chr[0];
chr++;
for (j = 0; j < w; j++) {
for (i = 0; i < CHAR_HEIGHT; i++) {
if (chr[j] & (1 << i)) {
draw_pixel(x+j, y+i, r, g, b, rectangle, ecran, position);
}
}
}
return w + CHAR_GAP;
}
void DrawString(const char* str, uint8_t x, uint8_t y, int r, int g, int b, SDL_Surface *rectangle, SDL_Surface *ecran, SDL_Rect position)
{
while (*str) {
x += DrawChar(*str++, x, y, r, g, b, rectangle, ecran, position);
}
}
<小时/>
const unsigned char font[96][6] = {
{3, 0x00, 0x00, 0x00}, //
{3, 0x00, 0x2f, 0x00}, // !
{4, 0x03, 0x00, 0x00, 0x03}, // "
结果如下:字符仍然具有相同的大小。 http://hpics.li/d54f000
编辑: 解决方案在这里http://www.riuson.com/lcd-image-converter 您必须对代码进行一些更改,因为它们存在一些错误,但它可以工作。
最佳答案
您可以轻松地将给定代码扩展到可变宽度字符。更改字体的定义:
const unsigned char font[96][10] = {
{3, 0x00, 0x00, 0x00}, //
{3, 0x00, 0x2f, 0x00}, // !
{4, 0x03, 0x00, 0x00, 0x03}, // "
...
};
第一个条目是字符的宽度。必须选择第二个维度来容纳最宽的字符。在本例中,它的宽度可以是 9 像素。
然后扩展DrawChar
函数,使用给定的宽度并返回绘制位置应前进的宽度,即字符的宽度加上一定的间隙。 (您可以将间隙设置为参数,以便打印双倍行距文本。)
然后,DrawString
函数使用返回的宽度:
int DrawChar(char c, uint8 x, uint8 y, uint8 brightness)
{
uint8 i, j;
// Convert the character to an index
c = c & 0x7F;
if (c < ' ') {
c = 0;
} else {
c -= ' ';
}
const uint8* chr = font[c];
int w = chr[0];
chr++;
for (j = 0; j < w; j++) {
for (i = 0; i < CHAR_HEIGHT; i++) {
if (chr[j] & (1 << i)) {
DrawPixel(x + j, y + i, brightness);
}
}
}
return w + CHAR_GAP;
}
void DrawString(const char* str, uint8 x, uint8 y, uint8 brightness)
{
while (*str) {
x += DrawChar(*str++, x, y, brightness);
}
}
编辑:这是一个更完整的示例,仅定义了一些字母:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef unsigned char uint8;
char screen[25][80];
#define CHAR_HEIGHT 8
#define CHAR_GAP 2
const unsigned char font[96][10] = {
[' ' - 32] = {1, 0x00},
['A' - 32] = {5, 0x3e, 0x09, 0x09, 0x09, 0x3e},
['B' - 32] = {5, 0x3f, 0x25, 0x25, 0x25, 0x19},
['D' - 32] = {5, 0x3f, 0x21, 0x21, 0x21, 0x1e},
['E' - 32] = {5, 0x3f, 0x25, 0x25, 0x25, 0x21},
['H' - 32] = {5, 0x3f, 0x04, 0x04, 0x04, 0x3f},
['I' - 32] = {1, 0x3f},
['L' - 32] = {4, 0x3f, 0x20, 0x20, 0x20},
['M' - 32] = {7, 0x3f, 0x02, 0x04, 0x18, 0x04, 0x02, 0x3f},
['O' - 32] = {5, 0x1e, 0x21, 0x21, 0x21, 0x1e},
['P' - 32] = {5, 0x3f, 0x09, 0x09, 0x09, 0x06},
['R' - 32] = {5, 0x3f, 0x09, 0x19, 0x19, 0x26},
['S' - 32] = {5, 0x22, 0x25, 0x25, 0x25, 0x19},
['W' - 32] = {7, 0x07, 0x38, 0x0c, 0x03, 0x0c, 0x38, 0x07},
};
void DrawPixel(int x, int y, uint8 c)
{
screen[y][x] = c;
}
int DrawChar(char c, uint8 x, uint8 y, uint8 brightness)
{
uint8 i, j;
// Convert the character to an index
c = c & 0x7F;
if (c < ' ') {
c = 0;
} else {
c -= ' ';
}
const uint8* chr = font[c];
int w = chr[0];
chr++;
for (j = 0; j < w; j++) {
for (i = 0; i < CHAR_HEIGHT; i++) {
if (chr[j] & (1 << i)) {
DrawPixel(x + j, y + i, brightness);
}
}
}
return w + CHAR_GAP;
}
void DrawString(const char* str, uint8 x, uint8 y, uint8 brightness)
{
while (*str) {
x += DrawChar(*str++, x, y, brightness);
}
}
int main()
{
int i;
memset(screen, '.', sizeof(screen));
DrawString("HELLO WORLD", 2, 2, 'O');
DrawString("MISSISSIPPI", 8, 10, '#');
for (i = 0; i < 25; i++) printf("%.80s\n", screen[i]);
return 0;
}
关于c - 用C在屏幕上绘制字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33876089/