예제 #1
0
static void do_glyphs(deark *c, lctx *d)
{
	struct de_bitmap_font *font = NULL;
	de_byte *font_data = NULL;
	de_int64 i;
	de_int64 glyph_rowspan;

	font = de_create_bitmap_font(c);
	font->has_nonunicode_codepoints = 1;
	font->nominal_width = (int)d->glyph_width;
	font->nominal_height = (int)d->glyph_height;
	font->num_chars = d->num_glyphs; // This may increase later
	glyph_rowspan = (d->glyph_width+7)/8;

	d->num_chars_alloc = d->num_glyphs;
	if(d->read_extra_codepoints)
		d->num_chars_alloc += MAX_EXTRA_CODEPOINTS;

	d->index_of_first_extra_codepoint = d->num_glyphs;
	d->num_extra_codepoints = 0;

	font->char_array = de_malloc(c, d->num_chars_alloc * sizeof(struct de_bitmap_font_char));

	font_data = de_malloc(c, d->font_data_size);
	de_read(font_data, d->headersize, d->font_data_size);

	for(i=0; i<d->num_chars_alloc; i++) {
		font->char_array[i].width = font->nominal_width;
		font->char_array[i].height = font->nominal_height;
		font->char_array[i].rowspan = glyph_rowspan;
		if(i<d->num_glyphs)
			font->char_array[i].codepoint_nonunicode = (de_int32)i;
		else
			font->char_array[i].codepoint_nonunicode = DE_INVALID_CODEPOINT;
		font->char_array[i].codepoint_unicode = DE_INVALID_CODEPOINT;
		if(i<d->num_glyphs)
			font->char_array[i].bitmap = &font_data[i*d->bytes_per_glyph];
	}

	if(d->has_unicode_table) {
		if(d->version==2)
			do_psf2_unicode_table(c, d, font);
		else
			do_psf1_unicode_table(c, d, font);
	}

	if(d->num_extra_codepoints>0) {
		font->num_chars = d->index_of_first_extra_codepoint + d->num_extra_codepoints;
		de_dbg(c, "codepoints aliases: %d\n", (int)d->num_extra_codepoints);
		de_dbg(c, "total characters: %d\n", (int)font->num_chars);
	}

	de_font_bitmap_font_to_image(c, font, NULL, 0);

	if(font) {
		de_free(c, font->char_array);
		de_destroy_bitmap_font(c, font);
	}
	de_free(c, font_data);
}
예제 #2
0
파일: unifont.c 프로젝트: jsummers/deark
static void de_run_unifont_hex(deark *c, de_module_params *mparams)
{
	struct de_bitmap_font *font = NULL;
	i64 char_array_numalloc = 0;
	char linebuf[256];
	struct de_linereader *lr = NULL;
	int ok = 0;

	font = de_create_bitmap_font(c);
	font->has_unicode_codepoints = 1;
	font->prefer_unicode = 1;
	font->nominal_height = 16;
	char_array_numalloc = 1024;
	font->char_array = de_mallocarray(c, char_array_numalloc, sizeof(struct de_bitmap_font_char));

	lr = de_linereader_create(c, c->infile);

	while(de_linereader_readnextline(c, lr, linebuf, sizeof(linebuf), 0)) {
		i64 idx;
		struct de_bitmap_font_char *ch;
		i64 fdata_len;
		char *dptr; // Pointer into linebuf, to the char after the ":"

		if(font->num_chars>=17*65536) goto done;

		idx = font->num_chars;
		if(idx >= char_array_numalloc) {
			i64 new_numalloc = char_array_numalloc*2;
			font->char_array = de_reallocarray(c, font->char_array,
				char_array_numalloc, sizeof(struct de_bitmap_font_char),
				new_numalloc);
			char_array_numalloc = new_numalloc;
		}
		ch = &font->char_array[idx];

		dptr = de_strchr(linebuf, ':');
		if(!dptr) goto done;
		*dptr = '\0';
		dptr++;

		fdata_len = (i64)de_strlen(dptr);
		ch->codepoint_unicode = (i32)de_strtoll(linebuf, NULL, 16);
		if(ch->codepoint_unicode<0 || ch->codepoint_unicode>=17*65536) goto done;

		ch->width = (int)((fdata_len/32)*8);
		ch->height = 16;
		de_dbg2(c, "char[%d] U+%04X %d"DE_CHAR_TIMES"%d",
			(int)font->num_chars, (unsigned int)ch->codepoint_unicode,
			ch->width, ch->height);
		if(ch->width<8 || ch->width>32) goto done;
		ch->rowspan = (ch->width+7)/8;
		ch->bitmap = de_malloc(c, ch->rowspan * ch->height);
		decode_fontdata(c, dptr, ch);

		font->num_chars++;
		if(ch->width > font->nominal_width) {
			font->nominal_width = ch->width;
		}
	}

	de_dbg(c, "number of characters: %d", (int)font->num_chars);
	if(font->num_chars<1) goto done;
	if(font->nominal_width<1) goto done;

	de_font_bitmap_font_to_image(c, font, NULL, 0);
	ok = 1;

done:
	if(!ok) {
		de_err(c, "Error parsing HEX font file (offset %"I64_FMT")", lr->f_pos);
	}
	de_linereader_destroy(c, lr);
	if(font) {
		if(font->char_array) {
			i64 k;
			for(k=0; k<font->num_chars; k++) {
				de_free(c, font->char_array[k].bitmap);
			}
			de_free(c, font->char_array);
		}
		font->char_array = NULL;
		de_destroy_bitmap_font(c, font);
	}
}
예제 #3
0
// create bitmap_font object
static void do_make_image(deark *c, lctx *d)
{
	struct de_bitmap_font *font = NULL;
	de_int64 i;
	de_int64 pos;

	font = de_create_bitmap_font(c);

	font->has_nonunicode_codepoints = 1;
	if(d->encoding!=DE_ENCODING_UNKNOWN)
		font->has_unicode_codepoints = 1;
	font->prefer_unicode = 0;

	font->nominal_width = (int)d->nominal_char_width;
	font->nominal_height = (int)d->char_height;
	font->num_chars = d->num_chars_stored;
	font->char_array = de_malloc(c, font->num_chars * sizeof(struct de_bitmap_font_char));

	for(i=0; i<d->num_chars_stored; i++) {
		de_int64 char_width;
		de_int64 char_offset;
		de_int32 char_index;
		de_int64 num_tiles;
		de_int64 tile;
		de_int64 row;

		pos = d->hdrsize + d->char_entry_size*i;
		char_width = de_getui16le(pos);
		if(d->char_entry_size==6)
			char_offset = de_getui32le(pos+2);
		else
			char_offset = de_getui16le(pos+2);
		de_dbg2(c, "char[%d] width=%d offset=%d\n", (int)(d->first_char + i), (int)char_width, (int)char_offset);

		num_tiles = (char_width+7)/8;

		if(i == d->num_chars_stored-1) {
			// Arbitrarily put the "absolute space" char at codepoint 256,
			// and U+2002 EN SPACE (best I can do).
			font->char_array[i].codepoint_nonunicode = 256;
			font->char_array[i].codepoint_unicode = 0x2002;
		}
		else {
			char_index = (de_int32)d->first_char + (de_int32)i;

			font->char_array[i].codepoint_nonunicode = char_index;

			if(font->has_unicode_codepoints) {
				if(char_index<32 && d->dfCharSet==0) {
					// This kind of font usually doesn't have glyphs below 32.
					// If it does, assume that they are VT100 line drawing characters.
					font->char_array[i].codepoint_unicode =
						de_char_to_unicode(c, 95+char_index, DE_ENCODING_DEC_SPECIAL_GRAPHICS);
				}
				else {
					font->char_array[i].codepoint_unicode =
						de_char_to_unicode(c, char_index, d->encoding);
				}
			}
		}

		font->char_array[i].width = (int)char_width;
		font->char_array[i].height = (int)d->char_height;
		font->char_array[i].rowspan = num_tiles;
		font->char_array[i].bitmap = de_malloc(c, d->char_height * num_tiles);

		for(row=0; row<d->char_height; row++) {
			for(tile=0; tile<num_tiles; tile++) {
				font->char_array[i].bitmap[row * font->char_array[i].rowspan + tile] =
					de_getbyte(char_offset + tile*d->char_height + row);
			}
		}
	}

	de_font_bitmap_font_to_image(c, font, d->fi, 0);

	if(font) {
		if(font->char_array) {
			for(i=0; i<font->num_chars; i++) {
				de_free(c, font->char_array[i].bitmap);
			}
			de_free(c, font->char_array);
		}
		de_destroy_bitmap_font(c, font);
	}
}