Implementation of FreeType vector font simple display based on embedded Linux __linux

Source: Internet
Author: User
Tags clear screen vector font
introduction of FreeType

FreeType Library is a completely free (open source), high-quality and portable font engine, it provides a unified interface to access a variety of font format files, it is very convenient for us to develop font display related program features. It supports monochrome bitmap, anti-aliasing bitmap rendering. FreeType Library is a highly modular library, although it is developed using ANSI C, but with object-oriented thinking, so freetype users can be flexible to crop it. More information about FreeType can be obtained from the official website of FreeType: https://www.freetype.org/to get more relevant information. II. Basic Development environment

PC Machine: Ubuntu9.10

Cross tool versions: GCC version 4.3.2

Development Board: JZ2440

Linux kernel version: Linux-3.4.10

Freetype version: Freetype-2.4.10

To use the FreeType Vector font Library to develop, you must first download the Vector font library, you can download from the official website: https://www.freetype.org/, or from the information I uploaded click here to download. When the download is complete, compile the vector font library into the cross-compilation tool chain and the root file system of the Development Board (the process can be searched by itself). third, the basic development steps 1, open LCD LCD equipment in order to use the FreeType vector font Library to display text, first of all, and the hardware LCD LCD related to the display of the API interface implementation. 1.1 to open the LCD device, get the relevant parameters, mapping memory to user space This part of the specific code to achieve the following:

/* Open LCD device driver file in readable and writable mode
	/FD_FB = open ("/dev/fb0", O_RDWR);
	if (FD_FB = = 1)
	{
		printf ("Can ' t open/dev/fb0!\n");
		return-1;
	}

	/* Get the variable parameters of the LCD device * *
	ret = IOCTL (FD_FB, Fbioget_vscreeninfo, &var);
	if (ret = = 1)
	{
		printf ("Can ' t ioctl for/dev/fb0!\n");
		return-1;
	}

	/* Get the fixed parameters of the LCD device * *
	ret = IOCTL (FD_FB, Fbioget_fscreeninfo, &fix);
	if (ret = = 1)
	{
		printf ("Can ' t ioctl for/dev/fb0!\n");
		return-1;
	}

	/* Get the relevant video memory information * *
	screen_size = var.xres * var.yres * VAR.BITS_PER_PIXEL/8;
	Line_width  = var.xres * VAR.BITS_PER_PIXEL/8;
	Pixel_width = VAR.BITS_PER_PIXEL/8;

	* * To map the LCD video memory to user space * *
	Fbmem = mmap (NULL, screen_size, Prot_read | Prot_write, map_shared, FD_FB, 0);
	if (Fbmem = = (char *)-1)
	{
		printf ("Mmap for/dev/fb0 error!\n");
		return-1;
	}

	* * The LCD screen is black * * *
	lcd_clear_screen.
1.2 Implement pixel display and clear screen interfaceThe pixel display is the most basic calling interface for all LCD screens, which enables a wide variety of display techniques, and this function is implemented as follows:
/*	display a pixel above the LCD
 *		x: x coordinates
 *		y: Represents the y-coordinate * color		: The color of the pixel display/
void Lcd_put_pixel (int x, int y, int color)
{
	/* get pixel position in memory * *
	unsigned char *pen8 = fbmem + y * line_width + x * pixel_width;
	unsigned short *pen16 = (unsigned short *) Pen8;
	unsigned int *pen32 = (unsigned int *) Pen32;

	int red, green, blue;

	/* To determine the number of digits of a pixel * *
	switch (var.bits_per_pixel)
	{case
		:		//RGB = 565
		{
			Red   = ( Color >>) & 0xFF;
			Green = (color >> 8) & 0xFF;
			Blue  = color & 0xff;

			*pen16 = ((Red >> 3) << 11) | ((green >> 2) << 5) | (Blue >> 3);
			break;
		Case:	
		{
			*pen32 = color;
			break;
		Default:
		{
			printf ("Don ' t support this size of pixel:%d\n", var.bits_per_pixel);
			break;
		}}}

The realization of screen function is also often used, its main role is to screen Qingcheng the same color, convenient text, images and so on display, its specific implementation is as follows:
/	* LCD screen screen
 * Color		: Clear screen color/
void lcd_clear_screen (int color)
{
	memset (fbmem, Color, screen_size);
}
2. Initialize the library

Initializing the library creates an instance of the FreeType library, which is implemented as follows:

/* Initialize FreeType Library *
	/error = Ft_init_freetype (&library);
	if (Error)
	{
		printf ("Ft_init_freetype error!\n");
		return-1;
	}

3. Load font file

Load the font and create a face object that describes the type of loaded font, which is implemented as follows:

/* Open the loaded font file *
 	/error = Ft_new_face (library, argv[1], 0, &face);
  	if (Error)
  	{
		printf ("Ft_new_face error!\n");
		return-1;
	}
ARGV[1]: The name of the incoming font file.

4, set the font size We here through the form of pixels to set the size of the font, set the size of the font for 24*24, specifically implemented as follows:

/* Set the pixel size of the character 24*24 * * *
	error = ft_set_pixel_sizes (face, 0);
	if (Error)
	{
		printf ("ft_set_pixel_sizes error!\n");
		return-1;
	}

5, according to the encoding value of characters, load glyph

Ft_set_transform (Face, 0, &pen);	Set the starting coordinate position of the font

		/* load character encoding, fill face Glyph slot member
		/error = Ft_load_char (face, Wcstr1[i), ft_load_render);
		if (Error)
		{
			printf ("Ft_load_char error!\n");
			return-1;
		}

6. Get the bitmap information of the font and display it through the LCD screen .

The specific implementation of this function is as follows:

	/* LCD display vector font bitmap information
 *		bitmap: Vector bitmap of the font to display
 *		x: Display x coordinates
 *		y: Displayed y-coordinate/
void Lcd_ Draw_bitmap (ft_bitmap* Bitmap, Ft_int x, ft_int y)
{
  	Ft_int  i, J, p, Q;
  	Ft_int  X_max = x + bitmap->width;
  	Ft_int  Y_max = y + bitmap->rows;

	/* Print bitmap information to the screen/for
	(i = x, p = 0; i < X_max i++, p++) {for
		(j = y, q = 0; J < Y_max, J + q++) 
  {
			if ((i > X_max) | | (J > Y_max) | | (I < 0) | | (J < 0))
				Continue;
			if (BITMAP->BUFFER[Q * bitmap->width + p]!= 0)
			{
				Lcd_put_pixel (i, J, white);
			}
			else
			{
				Lcd_put_pixel (i, J, black);
			}
	}}
	

The above simple introduction of the whole FreeType the basic process of development, the complete content can refer to the official website of FreeType: https://www.freetype.org/, can also refer to this article: https://wenku.baidu.com/ View/2d24be10cc7931b765ce155b.html
Appendix: Complete code implementation please download from the link below

http://download.csdn.net/download/tech_pro/9873843


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.