Exemplo n.º 1
0
struct rtgui_font *fnt_font_create(const char* filename, const char* font_family)
{
	int fd = -1;
	rt_uint32_t index;
	struct rtgui_font *font = RT_NULL;
	struct fnt_font *fnt = RT_NULL;
	struct fnt_header *fnt_header;

	fd = open(filename, O_RDONLY, 0);
	if (fd < 0)
	{
		goto __exit;
	}

	font = (struct rtgui_font*) rtgui_malloc (sizeof(struct rtgui_font));
	if (font == RT_NULL) goto __exit;
	fnt = (struct fnt_font*) rtgui_malloc (sizeof(struct fnt_font));
	if (fnt == RT_NULL) goto __exit;
	rt_memset(fnt, 0x00, sizeof(struct fnt_font));
	font->data = (void*)fnt;

	fnt_header = &(fnt->header);
	if (readstr(fd, fnt_header->version, 4) != 4) goto __exit;
	if (!readshort(fd, &fnt_header->max_width)) goto __exit;
	if (!readshort(fd, &fnt_header->height)) goto __exit;
	if (!readshort(fd, &fnt_header->ascent)) goto __exit;
	if (!readshort(fd, &fnt_header->depth)) goto __exit;

	if (!readlong(fd, &fnt_header->first_char)) goto __exit;
	if (!readlong(fd, &fnt_header->default_char)) goto __exit;
	if (!readlong(fd, &fnt_header->size)) goto __exit;
	if (!readlong(fd, &fnt_header->nbits)) goto __exit;
	if (!readlong(fd, &fnt_header->noffset)) goto __exit;
	if (!readlong(fd, &fnt_header->nwidth)) goto __exit;

	fnt->bits = (MWIMAGEBITS*) rtgui_malloc (fnt_header->nbits * sizeof(MWIMAGEBITS));
	if (fnt->bits == RT_NULL) goto __exit;
	/* read data */
	if (readstr(fd, &(fnt->bits[0]), fnt_header->nbits) != fnt_header->nbits) goto __exit;

	/* check boundary */
	if (fnt_header->nbits & 0x01)
	{
		rt_uint16_t pad;
		readshort(fd, &pad);
		pad = pad; /* skip warning */
	}
	
	if (fnt_header->noffset != 0)
	{
		fnt->offset = rtgui_malloc (fnt_header->noffset * sizeof(rt_uint32_t));
		if (fnt->offset == RT_NULL) goto __exit;

		for (index = 0; index < fnt_header->noffset; index ++)
		{
			if (!readshort(fd, &(fnt->offset[index]))) goto __exit;
		}
	}

	if (fnt_header->nwidth != 0)
	{
		fnt->width = rtgui_malloc (fnt_header->nwidth * sizeof(rt_uint8_t));
		if (fnt->width == RT_NULL) goto __exit;

		for (index = 0; index < fnt_header->nwidth; index ++)
		{
			if (!readbyte(fd, &(fnt->width[index]))) goto __exit;
		}
	}

	close(fd);

	font->family = rt_strdup(font_family);
	font->height = fnt->header.height;
	font->refer_count = 0;
	font->engine = &fnt_font_engine;
	
	/* add to system */
	rtgui_font_system_add_font(font);

	return font;

__exit:
	if (fd >= 0) close(fd);
	if (fnt != RT_NULL)
	{
		if (fnt->bits != RT_NULL) rtgui_free(fnt->bits);
		if (fnt->offset != RT_NULL) rtgui_free(fnt->offset);
		if (fnt->width != RT_NULL) rtgui_free(fnt->width);

		rtgui_free(fnt);
	}

	if (font != RT_NULL)
	{
		rtgui_free(font);
	}
	return RT_NULL;
}
Exemplo n.º 2
0
rtgui_font_t* rtgui_freetype_font_create(const char* filename, int bold, int italic, rt_size_t size)
{
	FT_Error err = 0;
	struct rtgui_font* font;

	font = (struct rtgui_font*) rtgui_malloc (sizeof(struct rtgui_font));
	if (font != RT_NULL)
	{
		struct rtgui_freetype_font* freetype;

		freetype = (struct rtgui_freetype_font*) rtgui_malloc (sizeof(struct rtgui_freetype_font));
		if (freetype == RT_NULL)
		{
			rt_free(font);
			font = RT_NULL;
		}
		else
		{
			err = FT_Init_FreeType(&freetype->library);
			if((err = FT_New_Face(freetype->library, filename, 0, &freetype->face)))
			{
				FT_Done_FreeType(freetype->library);

				rt_free(font);
				font = RT_NULL;
			}
			else
			{
				err = FT_Select_Charmap(freetype->face, ft_encoding_unicode);
				if(err)
				{
					err = FT_Select_Charmap(freetype->face, ft_encoding_latin_1 );
				}

				err = FT_Set_Pixel_Sizes(freetype->face, 0, size);
				if (err != 0)
				{
					rtgui_free(font);
					font = RT_NULL;

					FT_Done_FreeType(freetype->library);
					rtgui_free(freetype);

					return RT_NULL;
				}

				freetype->bold = bold;
				freetype->italic = italic;

				rt_kprintf("fonfile:%s\n", filename);
				rt_kprintf("font family_name:%s\n", freetype->face->family_name);
				rt_kprintf("font style_name:%s\n", freetype->face->style_name);

				/* set user data */
				font->data = freetype;
				font->family = rt_strdup(freetype->face->family_name);
				font->height = (rt_uint16_t)size;
				font->refer_count = 0;
				font->engine = &freetype_font_engine;

				/* add to system */
				rtgui_font_system_add_font(font);
			}
		}
	}

	return font;
}