12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <assert.h>
- #define BDF2U8G_COMPACT_OUTPUT
- #define BDF2U8G_VERSION "1.01"
- void bdf_aa_ClearDoShow(void);
- void bdf_aa_Do(void);
- #define DATA_BUF_SIZE (1024 * 64)
- unsigned char data_buf[DATA_BUF_SIZE];
- int data_pos = 0;
- void data_Init(void) {
- data_pos = 0;
- }
- void data_Put(unsigned char c) {
- data_buf[data_pos] = c;
- data_pos++;
- }
- void data_Write(FILE *out_fp, const char *indent) {
- int i;
- int bytes_per_line = 16;
- for (i = 0; i < data_pos; i++) {
- fprintf(out_fp, "0x%02X", data_buf[i]);
- if (i + 1 != data_pos)
- fprintf(out_fp, ",");
- if ((i + 1) % bytes_per_line == 0)
- fprintf(out_fp, "\n%s", indent);
- }
- }
- #define P_BUF_SIZE (1024 * 4)
- int p_current_char;
- const char *p_line;
- char p_buf[P_BUF_SIZE];
- static int p_next_char(void) {
- p_current_char = *p_line;
- if (p_current_char == '\0')
- return 0;
- p_line++;
- return 1;
- }
- int p_first_char(const char *line) {
- p_line = line;
- return p_next_char();
- }
- void p_err(const char *msg) {
- }
- int p_skip_space(void) {
- for (;;) {
- if (p_current_char == 0 || p_current_char > 32)
- break;
- if (p_next_char() == 0)
- return 0;
- }
- return 1;
- }
- int p_get_identifier(void) {
- int i = 0;
- if (p_current_char == '\0')
- return p_err("unexpected EOF (expected identifier)"), 0;
- for (;;) {
- if (p_current_char <= 32)
- break;
- p_buf[i++] = p_current_char;
- if (p_next_char() == 0)
- break;
- }
- p_buf[i++] = '\0';
- p_skip_space();
- return 1;
- }
- int p_get_identifier_with_blank(void) {
- int i = 0;
- for (;;) {
- if (p_current_char < 32)
- break;
- p_buf[i++] = p_current_char;
- if (p_next_char() == 0)
- break;
- }
- p_buf[i++] = '\0';
- p_skip_space();
- return 1;
- }
- int p_get_string(void) {
- int i = 0;
- if (p_current_char == '\0')
- return 0;
- if (p_current_char != '\"')
- return p_err("\" expected"), 0;
- if (p_next_char() == 0)
- return p_err("unexpected EOF (\")"), 0;
- for (;;) {
- if (p_current_char == '\\') {
- if (p_next_char() == 0)
- return p_err("unexpected EOF (\\)"), 0;
- }
- else if (p_current_char == '\"') {
- p_next_char();
- break;
- }
- p_buf[i++] = p_current_char;
- if (p_next_char() == 0)
- return p_err("unexpected EOF (\")"), 0;
- }
- p_buf[i] = '\0';
- return 1;
- }
- int p_get_any(void) {
- if (p_current_char == '\"')
- return p_get_string();
- return p_get_identifier();
- }
- int p_get_val(void) {
- p_get_any();
- return atoi(p_buf);
- }
- int p_get_hex(void) {
- int value = 0;
- if (p_current_char >= '0' && p_current_char <= '9')
- value = p_current_char - '0';
- else if (p_current_char >= 'a' && p_current_char <= 'f')
- value = p_current_char - 'a' + 10;
- else if (p_current_char >= 'A' && p_current_char <= 'F')
- value = p_current_char - 'A' + 10;
- p_next_char();
- return value;
- }
- int p_get_hex_byte(void) {
- int v;
- v = p_get_hex();
- v *= 16;
- v += p_get_hex();
- return v;
- }
- int map_u8g_to_unicode[256];
- int map_UnicodeToU8G(int unicode) {
- int i;
- for (i = 0; i < 256; i++)
- if (map_u8g_to_unicode[i] == unicode)
- return i;
- return 0;
- }
- void map_Init(void) {
- int i;
- map_u8g_to_unicode[0] = 0;
- for (i = 0; i < 256; i++) map_u8g_to_unicode[i] = i;
- }
- void map_UpperLowerPage(int lower_page, int upper_page, int shift, int upper_shift) {
- int i;
- int encoding;
- int tmp[256];
-
- for (i = 0; i < 128; i++) {
- encoding = i + lower_page * 128;
- map_u8g_to_unicode[i] = encoding;
- }
- for (i = 128; i < 256; i++) {
- encoding = i - 128 + upper_page * 128;
- if (i + upper_shift < 256)
- map_u8g_to_unicode[i + upper_shift] = encoding;
- }
- for (i = 0; i < 256; i++) tmp[i] = map_u8g_to_unicode[i];
- for (i = 0; i < shift; i++) map_u8g_to_unicode[i] = -1;
- for (i = shift; i < 256; i++) map_u8g_to_unicode[i] = tmp[(i + 256 - shift) % 256];
-
- }
- long bdf_last_line_start_pos;
- long bdf_encoding_pos[256];
- void bdf_InitFilePos(void) {
- int i;
- for (i = 0; i < 256; i++) bdf_encoding_pos[i] = 0;
- }
- void bdf_SetFilePos(FILE *fp, int encoding) {
- if (encoding < 0)
- return;
- if (bdf_encoding_pos[encoding] == 0L)
- return;
- fseek(fp, bdf_encoding_pos[encoding], SEEK_SET);
-
- }
- int bdf_IsEncodingAvailable(int encoding) {
- if (bdf_encoding_pos[encoding] == 0L)
-
- return 0;
- return 1;
- }
- void bdf_StoreFilePos(int encoding, long pos) {
-
-
- if (bdf_encoding_pos[encoding] != 0L)
- return;
- bdf_encoding_pos[encoding] = pos;
- }
- int bdf_font_format = 0;
- #define BDF_STATE_FONT_DATA 0
- #define BDF_STATE_ENCODING 1
- int bdf_state = BDF_STATE_FONT_DATA;
- int bdf_requested_encoding = 0;
- #define BDF_LINE_MAX (1024 * 4)
- #define BDF_LINE_STATE_KEYWORDS 0
- #define BDF_LINE_STATE_BITMAP 1
- #define BDF_MAX_HEIGHT 200
- #define BDF_AA_OFFSET 1
- char bdf_copyright[BDF_LINE_MAX];
- char bdf_font[BDF_LINE_MAX];
- unsigned char bdf_bitmap_line[BDF_MAX_HEIGHT][20];
- unsigned char bdf_aa_bitmap_line[BDF_MAX_HEIGHT + 2 * BDF_AA_OFFSET][(20 + 2 * BDF_AA_OFFSET) * 8];
- int bdf_line_state = BDF_LINE_STATE_KEYWORDS;
- int bdf_line_bm_line = 0;
- int bdf_font_size;
- int bdf_font_width;
- int bdf_font_height;
- int bdf_font_x;
- int bdf_font_y;
- int bdf_capital_A_height;
- int bdf_capital_1_height;
- int bdf_lower_g_descent;
- int bdf_char_width;
- int bdf_char_max_width;
- int bdf_char_height;
- int bdf_char_ascent;
- int bdf_char_xascent;
- int bdf_char_xdescent;
- int bdf_char_max_ascent;
- int bdf_char_max_height;
- int bdf_char_x;
- int bdf_char_max_x;
- int bdf_char_min_x;
- int bdf_char_y;
- int bdf_char_max_y;
- int bdf_char_min_y;
- int bdf_delta_x_default = -1;
- int bdf_delta_x = -1;
- int bdf_delta_max_x;
- int bdf_delta_min_x;
- int bdf_delta_y;
- int bdf_delta_max_y;
- int bdf_delta_min_y;
- int bdf_glyph_data_len;
- int bdf_glyph_data_max_len;
- int bdf_encoding;
- int bdf_encoding_65_pos;
- int bdf_encoding_97_pos;
- int bdf_is_encoding_successfully_done;
- char bdf_info[32000 * 2];
- int bdf_is_put_glyph_completed = 0;
- void bdf_ResetMax(void) {
- bdf_char_max_width = 0;
- bdf_char_max_height = 0;
- bdf_char_max_x = 0;
- bdf_char_max_y = 0;
- bdf_delta_max_x = 0;
- bdf_delta_max_y = 0;
- bdf_char_min_x = 0;
- bdf_char_min_y = 0;
- bdf_delta_min_x = 0;
- bdf_delta_min_y = 0;
- bdf_glyph_data_max_len = 0;
- bdf_char_max_ascent = 0;
- bdf_char_xascent = 0;
- bdf_char_xdescent = 0;
- }
- void bdf_UpdateMax(void) {
- if (bdf_char_max_width < bdf_char_width)
- bdf_char_max_width = bdf_char_width;
- if (bdf_char_max_height < bdf_char_height)
- bdf_char_max_height = bdf_char_height;
- if (bdf_char_max_x < bdf_char_x)
- bdf_char_max_x = bdf_char_x;
- if (bdf_char_max_y < bdf_char_y)
- bdf_char_max_y = bdf_char_y;
- if (bdf_delta_max_x < bdf_delta_x)
- bdf_delta_max_x = bdf_delta_x;
- if (bdf_delta_max_y < bdf_delta_y)
- bdf_delta_max_y = bdf_delta_y;
- if (bdf_char_min_x > bdf_char_x)
- bdf_char_min_x = bdf_char_x;
- if (bdf_char_min_y > bdf_char_y)
- bdf_char_min_y = bdf_char_y;
- if (bdf_delta_min_x > bdf_delta_x)
- bdf_delta_min_x = bdf_delta_x;
- if (bdf_delta_min_y > bdf_delta_y)
- bdf_delta_min_y = bdf_delta_y;
- if (bdf_glyph_data_max_len < bdf_glyph_data_len)
- bdf_glyph_data_max_len = bdf_glyph_data_len;
- if (bdf_char_max_ascent < bdf_char_ascent)
- bdf_char_max_ascent = bdf_char_ascent;
- }
- void bdf_ShowGlyph(void) {
- #ifdef VERBOSE
- int x, y, byte, bit;
- int gx, gy;
- char *p;
- gy = bdf_char_height - 1 + bdf_char_y;
- printf("bbx %d %d %d %d encoding %d\n", bdf_char_width, bdf_char_height, bdf_char_x, bdf_char_y, bdf_encoding);
- for (y = 0; y < bdf_line_bm_line; y++) {
- printf("%02d ", gy);
- gx = bdf_char_x;
- for (x = 0; x < bdf_char_width; x++) {
- byte = x >> 3;
- bit = 7 - (x & 7);
- if ((bdf_bitmap_line[y][byte] & (1 << bit)) == 0)
- p = " .";
- else
- p = " *";
- if (gy == 0 && gx == 0)
- p = " o";
- printf("%s", p);
- gx++;
- }
- printf(" ");
- for (x = 0; x < ((bdf_char_width + 7) / 8); x++) printf( "%02X", bdf_bitmap_line[y][x]);
- gy--;
- printf("\n");
- }
- #else
- printf("bbx %d %d %d %d encoding %d\n", bdf_char_width, bdf_char_height, bdf_char_x, bdf_char_y, bdf_encoding);
- fflush(stdout);
- #endif
- }
- void bdf_ClearGlyphBuffer(void) {
- int x, y;
- for (y = 0; y < BDF_MAX_HEIGHT; y++)
- for (x = 0; x < 20; x++) bdf_bitmap_line[y][x] = 0;
- }
- void bdf_PutGlyph(void) {
- int len;
- int y, x;
- if (bdf_state == BDF_STATE_ENCODING) {
-
- bdf_char_ascent = bdf_char_height + bdf_char_y;
-
- if (bdf_encoding == 'A')
- bdf_capital_A_height = bdf_char_height;
- if (bdf_encoding == '1')
- bdf_capital_1_height = bdf_char_height;
- if (bdf_encoding == 'g')
- bdf_lower_g_descent = bdf_char_y;
- if (bdf_char_xascent < bdf_capital_A_height)
- bdf_char_xascent = bdf_capital_A_height;
- if (bdf_char_xascent < bdf_capital_1_height)
- bdf_char_xascent = bdf_capital_1_height;
- if (bdf_encoding == '(')
- if (bdf_char_xascent < bdf_char_ascent)
- bdf_char_xascent = bdf_char_ascent;
- if (bdf_encoding == '[')
- if (bdf_char_xascent < bdf_char_ascent)
- bdf_char_xascent = bdf_char_ascent;
- if (bdf_char_xdescent > bdf_lower_g_descent)
- bdf_char_xdescent = bdf_lower_g_descent;
- if (bdf_encoding == '(')
- if (bdf_char_xdescent > bdf_char_y)
- bdf_char_xdescent = bdf_char_y;
- if (bdf_encoding == '[')
- if (bdf_char_xdescent > bdf_char_y)
- bdf_char_xdescent = bdf_char_y;
- if (bdf_requested_encoding != bdf_encoding)
- return;
- assert( bdf_line_bm_line == bdf_char_height);
- bdf_ShowGlyph();
- #ifdef VERBOSE
- bdf_aa_ClearDoShow();
- #endif
- bdf_UpdateMax();
- if (bdf_font_format <= 1)
- len = (bdf_char_width + 7) / 8 * bdf_char_height;
- else
- len = (bdf_char_width + 2 * BDF_AA_OFFSET + 3) / 4 * (bdf_char_height + 2 * BDF_AA_OFFSET);
- if (len > 255) {
- fprintf(stderr, "Glyph with encoding %d is too large (%d > 255)\n", bdf_encoding, len);
- exit(1);
- }
- bdf_glyph_data_len = len;
-
- if (bdf_font_format == 0) {
- data_Put(bdf_char_width);
- data_Put(bdf_char_height);
- data_Put(bdf_glyph_data_len);
- data_Put(bdf_delta_x);
- data_Put(bdf_char_x);
- data_Put(bdf_char_y);
-
- bdf_is_encoding_successfully_done = 1;
- }
- else if (bdf_font_format == 2) {
- data_Put(bdf_char_width + 2 * BDF_AA_OFFSET);
- data_Put(bdf_char_height + 2 * BDF_AA_OFFSET);
- data_Put(bdf_glyph_data_len);
- data_Put(bdf_delta_x);
- data_Put(bdf_char_x - BDF_AA_OFFSET);
- data_Put(bdf_char_y - BDF_AA_OFFSET);
-
- bdf_is_encoding_successfully_done = 1;
- }
- else {
-
- if (bdf_glyph_data_len < 0 || bdf_glyph_data_len > 15) {
- fprintf(stderr, "Glyph with encoding %d does not fit for format 1 (data len = %d)\n", bdf_encoding, bdf_glyph_data_len);
- exit(1);
- }
- if (bdf_delta_x < 0 || bdf_delta_x > 15) {
- fprintf(stderr, "Glyph with encoding %d does not fit for format 1 (DWIDTH = %d)\n", bdf_encoding, bdf_delta_x);
- exit(1);
- }
- if (bdf_char_x < 0 || bdf_char_x > 15) {
- fprintf(stderr, "Glyph with encoding %d does not fit for format 1 (x-off = %d)\n", bdf_encoding, bdf_char_x);
- exit(1);
- }
- if (bdf_char_y < -2 || bdf_char_y > 13) {
- fprintf(stderr, "Glyph with encoding %d does not fit for format 1 (y-off = %d [%d..%d])\n", bdf_encoding, bdf_char_y, bdf_char_min_y, bdf_char_max_y);
- exit(1);
- }
- if (bdf_char_width < 0 || bdf_char_width > 15) {
- fprintf(stderr, "Glyph with encoding %d does not fit for format 1 (width = %d)\n", bdf_encoding, bdf_char_width);
- exit(1);
- }
- if (bdf_char_height < 0 || bdf_char_height > 15) {
- fprintf(stderr, "Glyph with encoding %d does not fit for format 1 (height = %d)\n", bdf_encoding, bdf_char_height);
- exit(1);
- }
-
- if (((bdf_char_x << 4) | (bdf_char_y + 2)) == 255) {
- fprintf(stderr, "Glyph with encoding %d does not fit for format 1 (skip mark generated)\n", bdf_encoding);
- exit(1);
- }
- data_Put((bdf_char_x << 4) | (bdf_char_y + 2));
- data_Put((bdf_char_width << 4) | bdf_char_height );
- data_Put((bdf_delta_x << 4) | bdf_glyph_data_len );
- bdf_is_encoding_successfully_done = 1;
- }
- sprintf(bdf_info + strlen(bdf_info), "/* encoding %d %c, bbx %d %d %d %d asc %d dx %d*/\n",
- bdf_encoding,
- bdf_encoding > 32 && bdf_encoding <= 'z' ? bdf_encoding : ' ',
- bdf_char_width,
- bdf_char_height,
- bdf_char_x,
- bdf_char_y,
- bdf_char_ascent,
- bdf_delta_x);
- if (bdf_font_format <= 1) {
- for (y = 0; y < bdf_char_height; y++)
- for (x = 0; x < ((bdf_char_width + 7) / 8); x++) {
- data_Put(bdf_bitmap_line[y][x]);
- len--;
- }
- assert( len == 0 );
- bdf_is_put_glyph_completed = 1;
- }
- else {
- int b, cnt;
- bdf_aa_Do();
- for (y = 0; y < bdf_char_height + 2 * BDF_AA_OFFSET; y++) {
- b = 0;
- cnt = 0;
- for (x = 0; x < bdf_char_width + 2 * BDF_AA_OFFSET; x++) {
- b <<= 2;
- b |= bdf_aa_bitmap_line[y][x] & 3;
- cnt++;
- if (cnt == 4) {
- data_Put(b);
- b = 0;
- cnt = 0;
- len--;
- }
- }
- if (cnt != 0) {
- b <<= 2 * (4 - cnt);
- data_Put(b);
- b = 0;
- cnt = 0;
- len--;
- }
- }
- assert( len == 0 );
- }
- }
- }
- int bdf_IsColZero(int x) {
- int y, byte, bit;
- for (y = 0; y < bdf_char_height; y++) {
- byte = x >> 3;
- bit = 7 - (x & 7);
- if ((bdf_bitmap_line[y][byte] & (1 << bit)) != 0)
- return 0;
- }
- return 1;
- }
- int bdf_IsRowZero(int y) {
- int x, byte, bit;
- for (x = 0; x < bdf_char_width; x++) {
- byte = x >> 3;
- bit = 7 - (x & 7);
- if ((bdf_bitmap_line[y][byte] & (1 << bit)) != 0)
- return 0;
- }
- return 1;
- }
- void bdf_DeleteFirstCol(void) {
- int m = (bdf_char_width + 7) / 8;
- int x, y;
- for (y = 0; y < bdf_char_height; y++)
- for (x = 0; x < m; x++) {
- bdf_bitmap_line[y][x] <<= 1;
- bdf_bitmap_line[y][x] |= bdf_bitmap_line[y][x + 1] >> 7;
- }
- }
- void bdf_DeleteFirstRow(void) {
- int m = (bdf_char_width + 7) / 8;
- int x, y;
- for (y = 0; y + 1 < bdf_char_height; y++)
- for (x = 0; x < m; x++)
- bdf_bitmap_line[y][x] = bdf_bitmap_line[y + 1][x];
- }
- void bdf_ReduceGlyph(void) {
- while (bdf_char_width > 0) {
- if (bdf_IsColZero(bdf_char_width - 1) == 0)
- break;
- bdf_char_width--;
- }
- while (bdf_char_height > 0) {
- if (bdf_IsRowZero(bdf_char_height - 1) == 0)
- break;
- bdf_line_bm_line--;
- bdf_char_height--;
- bdf_char_y++;
- }
- while (bdf_IsColZero(0) != 0 && bdf_char_width > 0) {
- bdf_DeleteFirstCol();
- bdf_char_x++;
- bdf_char_width--;
- }
- while (bdf_char_height > 0) {
- if (bdf_IsRowZero(0) == 0)
- break;
- bdf_DeleteFirstRow();
- bdf_line_bm_line--;
- bdf_char_height--;
- }
- }
- int bdf_GetXYVal(int x, int y) {
- int byte, bit;
- if (x < 0) return 0;
- if (y < 0) return 0;
- if (x >= bdf_char_width) return 0;
- if (y >= bdf_char_height) return 0;
- byte = x >> 3;
- bit = 7 - (x & 7);
- if ((bdf_bitmap_line[y][byte] & (1 << bit)) != 0) return 1;
- return 0;
- }
- void bdf_aa_Clear(void) {
- int x, y;
- for (y = 0; y < BDF_MAX_HEIGHT + 2 * BDF_AA_OFFSET; y++)
- for (x = 0; x < (20 + 2 * BDF_AA_OFFSET) * 8; x++)
- bdf_aa_bitmap_line[y][x] = 0;
- }
- void bdf_aa_SetXYVal(int x, int y, int val) {
- bdf_aa_bitmap_line[y][x] = val;
- }
- int bdf_aa_matrix[9] = {
- 1, 3, 1,
- 3, 4, 3,
- 1, 3, 1
- };
- int bdf_aa_sum = 20;
- int bdf_aa_gray_levels = 4;
- void bdf_aa_Do(void) {
- int x, y, val, sx, sy, sum, gray;
- bdf_aa_Clear();
- for (y = 0; y < bdf_char_height + 2 * BDF_AA_OFFSET; y++)
- for (x = 0; x < bdf_char_width + 2 * BDF_AA_OFFSET; x++) {
- if (bdf_GetXYVal(x - BDF_AA_OFFSET, y - BDF_AA_OFFSET) == 0) {
- sum = 0;
- for (sy = -BDF_AA_OFFSET; sy <= BDF_AA_OFFSET; sy++)
- for (sx = -BDF_AA_OFFSET; sx <= BDF_AA_OFFSET; sx++) {
- val = bdf_GetXYVal(x + sx - BDF_AA_OFFSET, y + sy - BDF_AA_OFFSET);
- val *= bdf_aa_matrix[(sy + BDF_AA_OFFSET) * (2 * BDF_AA_OFFSET + 1) + sx + BDF_AA_OFFSET];
- sum += val;
- }
- if (sum <= 5)
- gray = 0;
- else
- gray = (sum * (bdf_aa_gray_levels - 1) + (bdf_aa_sum / 2)) / bdf_aa_sum;
- if (gray >= bdf_aa_gray_levels)
- gray = bdf_aa_gray_levels - 1;
- }
- else {
- gray = bdf_aa_gray_levels - 1;
- }
- bdf_aa_SetXYVal(x, y, gray);
- }
- }
- void bdf_aa_Show(void) {
- int x, y;
- if (bdf_font_format == 2) {
- for (y = 0; y < bdf_char_height + 2 * BDF_AA_OFFSET; y++) {
- for (x = 0; x < bdf_char_width + 2 * BDF_AA_OFFSET; x++)
- switch (bdf_aa_bitmap_line[y][x]) {
- case 0: printf("."); break;
- case 1: printf("-"); break;
- case 2: printf("+"); break;
- case 3: printf("#"); break;
- }
- printf("\n");
- }
- }
- }
- void bdf_aa_ClearDoShow(void) {
- bdf_aa_Do();
- bdf_aa_Show();
- }
- void bdf_ReadLine(const char *s) {
-
- if (p_first_char(s) == 0) return;
- if (p_skip_space() == 0) return;
- if (bdf_line_state == BDF_LINE_STATE_KEYWORDS) {
- p_get_identifier();
- if (strcmp(p_buf, "COPYRIGHT") == 0) {
- p_get_any();
- strcpy(bdf_copyright, p_buf);
- }
- else if (strcmp(p_buf, "FONT") == 0) {
-
- p_get_identifier_with_blank();
- strcpy(bdf_font, p_buf);
- }
- else if (strcmp(p_buf, "SIZE") == 0) {
- bdf_font_size = p_get_val();
- }
- else if (strcmp(p_buf, "ENCODING") == 0) {
- bdf_encoding = map_UnicodeToU8G(p_get_val());
- bdf_StoreFilePos(bdf_encoding, bdf_last_line_start_pos);
- }
- else if (strcmp(p_buf, "DWIDTH") == 0) {
- bdf_delta_x = p_get_val();
- bdf_delta_y = p_get_val();
- }
- else if (strcmp(p_buf, "FONTBOUNDINGBOX") == 0) {
- bdf_font_width = p_get_val();
- bdf_font_height = p_get_val();
- bdf_font_x = p_get_val();
- bdf_font_y = p_get_val();
- }
- else if (strcmp(p_buf, "BBX") == 0) {
- bdf_char_width = p_get_val();
- bdf_char_height = p_get_val();
- bdf_char_x = p_get_val();
- bdf_char_y = p_get_val();
- bdf_char_ascent = bdf_char_height + bdf_char_y;
-
- }
- else if (strcmp(p_buf, "CHARS") == 0) {
- if (bdf_delta_x < 0)
- bdf_delta_x = 0;
- if (bdf_delta_x_default < 0)
- bdf_delta_x_default = bdf_delta_x;
- }
- else if (strcmp(p_buf, "STARTCHAR") == 0) {
- if (bdf_delta_x_default < 0)
- bdf_delta_x_default = 0;
- bdf_delta_x = bdf_delta_x_default;
- }
- else if (strcmp(p_buf, "BITMAP") == 0) {
- bdf_line_state = BDF_LINE_STATE_BITMAP;
- bdf_line_bm_line = 0;
- }
- }
- else if (bdf_line_state == BDF_LINE_STATE_BITMAP) {
- if (strncmp(s, "ENDCHAR", 7) == 0) {
- bdf_ReduceGlyph();
- bdf_PutGlyph();
- bdf_line_state = BDF_LINE_STATE_KEYWORDS;
- bdf_line_bm_line = 0;
- }
- else if (bdf_requested_encoding == bdf_encoding) {
- int i = 0;
- for (;;) {
- if (p_current_char < '0') break;
- bdf_bitmap_line[bdf_line_bm_line][i] = p_get_hex_byte();
- i++;
- }
- bdf_line_bm_line++;
- assert(bdf_line_bm_line < BDF_MAX_HEIGHT);
- }
- }
- }
- int bdf_ReadFP(FILE *fp) {
- static char bdf_line[BDF_LINE_MAX];
- bdf_is_put_glyph_completed = 0;
- for (;;) {
- bdf_last_line_start_pos = ftell(fp);
- if (fgets(bdf_line, BDF_LINE_MAX - 1, fp) == NULL)
- break;
- bdf_ReadLine(bdf_line);
- if (bdf_is_put_glyph_completed != 0)
- break;
- }
- return 1;
- }
- int bdf_ReadFile(const char *filename, int encoding) {
- int r;
- FILE *fp;
- fp = fopen(filename, "rb");
- if (fp != NULL) {
- bdf_SetFilePos(fp, encoding);
- r = bdf_ReadFP(fp);
- fclose(fp);
- return r;
- }
- return 0;
- }
- void bdf_GenerateFontData(const char *filename, int begin, int end) {
- bdf_state = BDF_STATE_FONT_DATA;
- bdf_ReadFile(filename, -1);
-
- data_Put(bdf_font_format);
- data_Put(bdf_font_width);
- data_Put(bdf_font_height);
- data_Put(bdf_font_x);
- data_Put(bdf_font_y);
- data_Put(bdf_capital_A_height > 0 ? bdf_capital_A_height : bdf_capital_1_height);
- data_Put(0);
- data_Put(0);
- data_Put(0);
- data_Put(0);
- data_Put(begin);
- data_Put(end);
- data_Put(0);
- data_Put(0);
- data_Put(0);
- data_Put(0);
- data_Put(0);
- }
- void bdf_GenerateGlyph(const char *filename, int encoding) {
- bdf_ClearGlyphBuffer();
- bdf_requested_encoding = encoding;
- bdf_state = BDF_STATE_ENCODING;
- bdf_ReadFile(filename, encoding);
- }
- void bdf_Generate(const char *filename, int begin, int end) {
- int i;
- int last_valid_encoding;
- bdf_encoding_65_pos = 0;
- bdf_encoding_97_pos = 0;
- bdf_InitFilePos();
- bdf_ResetMax();
- bdf_info[0] = '\0';
- bdf_font[0] = '\0';
- bdf_copyright[0] = '\0';
- bdf_GenerateFontData(filename, begin, end);
- for (i = begin; i <= end; i++) {
- if (i == 65) bdf_encoding_65_pos = data_pos;
- if (i == 97) bdf_encoding_97_pos = data_pos;
- bdf_is_encoding_successfully_done = 0;
- if (bdf_IsEncodingAvailable(i))
- bdf_GenerateGlyph(filename, i);
- if (bdf_is_encoding_successfully_done == 0)
- data_Put(255);
- if (bdf_is_encoding_successfully_done != 0)
- last_valid_encoding = i;
- }
-
- data_buf[5] = bdf_capital_A_height > 0 ? bdf_capital_A_height : bdf_capital_1_height;
- data_buf[6] = (bdf_encoding_65_pos >> 8);
- data_buf[7] = (bdf_encoding_65_pos & 255);
- data_buf[8] = (bdf_encoding_97_pos >> 8);
- data_buf[9] = (bdf_encoding_97_pos & 255);
- data_buf[12] = bdf_lower_g_descent;
- data_buf[13] = bdf_char_max_ascent;
- data_buf[14] = bdf_char_min_y;
- data_buf[15] = bdf_char_xascent;
- data_buf[16] = bdf_char_xdescent;
- if (0) data_buf[11] = last_valid_encoding;
- }
- void bdf_WriteC(const char *outname, const char *fontname) {
- int capital_ascent;
- FILE *out_fp;
- out_fp = fopen(outname, "wb");
- assert( out_fp != NULL );
- capital_ascent = bdf_capital_A_height > 0 ? bdf_capital_A_height : bdf_capital_1_height;
- fprintf(out_fp, "/*\n");
- fprintf(out_fp, " Fontname: %s\n", bdf_font);
- fprintf(out_fp, " Copyright: %s\n", bdf_copyright);
- fprintf(out_fp, " Capital A Height: %d, '1' Height: %d\n", bdf_capital_A_height, bdf_capital_1_height);
- fprintf(out_fp, " Calculated Max Values w=%2d h=%2d x=%2d y=%2d dx=%2d dy=%2d ascent=%2d len=%2d\n",
- bdf_char_max_width, bdf_char_max_height, bdf_char_max_x, bdf_char_max_y, bdf_delta_max_x, bdf_delta_max_y,
- bdf_char_max_ascent, bdf_glyph_data_max_len);
- fprintf(out_fp, " Font Bounding box w=%2d h=%2d x=%2d y=%2d\n",
- bdf_font_width, bdf_font_height, bdf_font_x, bdf_font_y);
- fprintf(out_fp, " Calculated Min Values x=%2d y=%2d dx=%2d dy=%2d\n",
- bdf_char_min_x, bdf_char_min_y, bdf_delta_min_x, bdf_delta_min_y);
- fprintf(out_fp, " Pure Font ascent =%2d descent=%2d\n", capital_ascent, bdf_lower_g_descent);
- fprintf(out_fp, " X Font ascent =%2d descent=%2d\n", bdf_char_xascent, bdf_char_xdescent);
- fprintf(out_fp, " Max Font ascent =%2d descent=%2d\n", bdf_char_max_ascent, bdf_char_min_y);
- fprintf(out_fp, "*/\n");
- fprintf(out_fp, "const u8g_fntpgm_uint8_t %s[%d] U8G_FONT_SECTION(\"%s\") = {\n", fontname, data_pos, fontname);
- fprintf(out_fp, " ");
- data_Write(out_fp, " ");
- fprintf(out_fp, "};\n");
- #ifndef BDF2U8G_COMPACT_OUTPUT
- fprintf(out_fp, "%s\n", bdf_info);
- #endif
- fclose(out_fp);
- }
- int ga_argc;
- char **ga_argv;
- void ga_remove_arg(void) {
- if (ga_argc == 0) return;
- ga_argc--;
- ga_argv++;
- }
- int ga_is_arg(char opt) {
- if (ga_argc == 0) return 0;
- if (ga_argv[0] == NULL) return 0;
- if (ga_argv[0][0] != '-') return 0;
- if (ga_argv[0][1] != opt) return 0;
- ga_remove_arg();
- return 1;
- }
- int main(int argc, char **argv) {
- int lower_page = 0;
- int upper_page = 1;
- int mapping_shift = 0;
- int upper_mapping_shift = 0;
- int begin = 32;
- int end = 255;
- if (argc < 4) {
- printf("bdf to u8glib font format converter v" BDF2U8G_VERSION "\n");
- printf("%s [-l page] [-u page] [-s shift] [-S upper-shift] [-b begin] [-e end] [-f format] fontfile fontname outputfile\n", argv[0]);
- return 1;
- }
- ga_argc = argc;
- ga_argv = argv;
- ga_remove_arg();
- for (;;) {
- if (ga_is_arg('l')) {
- lower_page = atoi(ga_argv[0]);
- ga_remove_arg();
- }
- else if (ga_is_arg('u')) {
- upper_page = atoi(ga_argv[0]);
- ga_remove_arg();
- }
- else if (ga_is_arg('s')) {
- mapping_shift = atoi(ga_argv[0]);
- ga_remove_arg();
- }
- else if (ga_is_arg('S')) {
- upper_mapping_shift = atoi(ga_argv[0]);
- ga_remove_arg();
- }
- else if (ga_is_arg('b')) {
- begin = atoi(ga_argv[0]);
- ga_remove_arg();
- }
- else if (ga_is_arg('e')) {
- end = atoi(ga_argv[0]);
- ga_remove_arg();
- }
- else if (ga_is_arg('f')) {
- bdf_font_format = atoi(ga_argv[0]);
- ga_remove_arg();
- }
- else {
- break;
- }
- }
- printf("encoding range %d..%d\n", begin, end);
- data_Init();
- map_Init();
- map_UpperLowerPage(lower_page, upper_page, mapping_shift, upper_mapping_shift);
-
- bdf_Generate(ga_argv[0], begin, end);
- bdf_WriteC(ga_argv[2], ga_argv[1]);
- printf("input file '%s'\n", ga_argv[0]);
- printf("u8g font name '%s'\n", ga_argv[1]);
- printf("output file '%s'\n", ga_argv[2]);
- return 0;
- }
|