int gr_text(int x, int y, const char *s) { GGLContext *gl = gr_context; GRFont *gfont = gr_font; unsigned off, width, height, font_bitmap_width, n; y -= gfont->ascent; gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); gl->enable(gl, GGL_TEXTURE_2D); while((off = *s)) { if(*((unsigned char*)(s)) < 0x20) { s++; continue; } width = gfont->cwidth; height = gfont->cheight; off = ch_utf8_to_custom(s); if(off >= 96) width *= 2; memcpy(&font_ftex, &gfont->texture, sizeof(font_ftex)); font_bitmap_width = (font.width % (font.cwidth * font_char_per_bitmap)); if(!font_bitmap_width) font_bitmap_width = font.cwidth * font_char_per_bitmap; font_ftex.width = font_bitmap_width; font_ftex.stride = font_bitmap_width; font_ftex.data = font_data[(off < 96) ? (off / font_char_per_bitmap) : ((96 + (off - 96) * 2) / font_char_per_bitmap)]; gl->bindTexture(gl, &font_ftex); if(off >= 96) gl->texCoord2i(gl, ((96 + (off - 96) * 2) * font.cwidth) % (font_char_per_bitmap * font.cwidth) - x, 0 - y); else gl->texCoord2i(gl, (off % font_char_per_bitmap) * width - x, 0 - y); gl->recti(gl, x, y, x + width, y + height); x += width; n = ch_utf8_length(s); if(n <= 0) break; s += n; } return x; }
int str_utf8_length(const char* s) { int n, l, off; n = 0; off = 0; while(*(s + off)) { l = ch_utf8_length(s + off); if (l > 0) { off += l; if (l == 1) n += 1; else n += 2; } } return n; }
int ch_utf8_to_custom(const char* s) { int i, n; struct utf8_to_custom* res; unsigned char name[8]; n = ch_utf8_length(s); if(n <= 0) return 0; if(n == 1) return (*s - 32); for(i = 0; i < n; i++) name[i] = ((unsigned char*)(s))[i]; name[n] = 0; res = in_word_set(name, n); if(res) return res->value; else return 0; }
void main() { char* s = "测test试"; int len; unsigned char *bits, *save; unsigned char *in, data; int sz; short ch; int i, d, n, bmp, pos; sz = 0; bits = malloc(font.width * font.height); save = bits; in = font.rundata; while((data = *in++)) { memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f); bits += (data & 0x7f); sz += (data & 0x7f); } free(save); printf("%d * %d %c= %d(%d)\n", font.width, font.height, ((font.width * font.height == sz) ? '=' : '!'), sz, font.width * font.height); font_bitmap_count = font.width / font.cwidth / font_char_per_bitmap + 1; printf("bitmaps: %d\n", font_bitmap_count); font_data = (void**)malloc(font_bitmap_count * sizeof(void*)); for(n = 0; n < font_bitmap_count; n++) { font_data[n] = malloc(font_char_per_bitmap * font.cwidth * font.cheight); memset(font_data[n], 0, font_char_per_bitmap * font.cwidth * font.cheight); } d = 0; in = font.rundata; while((data = *in++)) { n = data & 0x7f; for(i = 0; i < n; i++, d++) { bmp = d % font.width / (font.cwidth * font_char_per_bitmap); pos = d / font.width * (font.cwidth * font_char_per_bitmap) + (d % (font.cwidth * font_char_per_bitmap)); //printf("x = %d y = %d => b = %d x = %d y = %d\n", d % font.width, d / font.width, bmp, pos % (font.cwidth * font_char_per_bitmap), pos / (font.cwidth * font_char_per_bitmap)); ((unsigned char*)(font_data[bmp]))[pos] = (data & 0x80) ? 0xff : 0; } } printf("%d %d\n", font.width * font.height, d); for(n = 0; n < font_bitmap_count; n++) { free(font_data[n]); //free(font_data); } // 1 0 printf("%d %d\n", ch_test_cjk(s), ch_test_cjk(s + 3)); len = ch_utf8_length(s); // 3 printf("%d\n", len); len = ch_utf8_length(s+3); // 1 printf("%d\n", len); len = str_utf8_length(s); // 8 printf("%s %d\n", s, len); len = ch_utf8_to_custom(s); // ??? printf("%d\n", len); // 84 len = ch_utf8_to_custom(s + 3); printf("%d\n", len); }