HZK16 Chinese character 16*16 dot matrix font library usage and example program

tags: Unicode character table  unicode  Chinese font library  HZK16  16 by 16 dot matrix font

Foreword: Recently there was a project about Thai text recognition and printing. I have only heard of "Savadika" -_-!! for the understanding of Thai before, so I learned the typographic specifications and unicode coding of Thai in the first two days, and then I started to learn the principle of text dot matrix printing and code writing. Today I learned how to use the 16*16 dot matrix font library of Chinese characters.

First of all thanks hereGo wandering with herThe article of the big guy (the id of the big guy is a girlfriend, I envy o.o), the introduction of the article is clear, easy to understand, and the code explanation is also very detailed.

principle:

HZK16 font library is a 16×16 dot matrix font library that meets the GB2312 national standard. GB2312-80 of HZK16 supports 6763 Chinese characters and 682 symbols. Among them, there are 3755 first-level Chinese characters arranged in phonetic order, and there are 3008 second-level Chinese characters arranged according to radicals.

The 16×16 Chinese characters in the HZK16 font library require a total of 256 points to display, which means that 32 bytes are needed to display a common Chinese character. We can't use so many Chinese fonts in some applications, so we can extract only part of the font for our own use.

We know that a GB2312 Chinese character is encoded by two bytes, the range is0xA1A1~0xFEFEA1-A9Is the symbol area,B0-F7It is the Chinese character area. Each area has 94 characters (note: this is only the permitted range of encoding, and may not have font correspondence, for example, the symbol area has many encoding blank areas)

The following takes the Chinese character "I" as an example to introduce how to find its corresponding 32-byte font data in the HZK16 file.

As mentioned earlier, a Chinese character occupies two bytes, the first byte of the two is the area code of the Chinese character, and the last byte is the bit number of the character. Among them, each area records 94 Chinese characters, and the bit number is the position of the character in the area. So to find the location of "I" in the hzk16 library, you must get its area code and bit code.

  • Area code: the first byte of Chinese characters-0xA0, because the Chinese character code starts from the 0xA0 area, so the front of the file starts from the 0xA0 area, and the relative area code must be calculated
  • Bit code: the second byte of Chinese characters-0xA0

In this way, we can get the absolute offset position of Chinese characters in HZK16:offset = (94*(area code-1)+(bit code-1))*32

annotation:

  • The area code minus 1 is because the array starts with 0 and the area code bit number starts with 1.
  • (94*(area code-1)+bit number-1) Is the number of bytes occupied by a Chinese font
  • The last multiplied by 32 is because the Chinese character library should record the font information of the word with 32 bytes of information from this position (mentioned earlier that a Chinese character must have 32 bytes to display)

Icon:

The dot matrix output of "I" is shown below:

Therefore, the storage sequence of "I" in HZK16*16 dot matrix font library is (stored line by line, 16 lines in total, 2 bytes per line, 32 bytes in total):

The output of the program should look like this:

Source code example:

version 1

#include <stdio.h>

int main(void)
{
	FILE* fd = NULL;
	int i, j, k, offset;
	int flag;
	unsigned char buffer[32];
	unsigned char word[3] = "I";
	unsigned char key[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };

	fd = fopen("hzk16", "rb");
	if (fd == NULL)
	{
		fprintf(stderr, "error hzk16\n");
		return 1;
	}

	offset = (94 * (unsigned int)(word[0] - 0xa0 - 1) + (word[1] - 0xa0 - 1)) * 32;
	fseek(fd, offset, SEEK_SET);
	fread(buffer, 1, 32, fd);
	for (k = 0; k<32; k++){
		printf("%02X ", buffer[k]);
	}
	printf("\n");
	for (k = 0; k<16; k++)
	{
		for (j = 0; j<2; j++)
		{
			for (i = 0; i<8; i++)
			{
				flag = buffer[k * 2 + j] & key[i];
				printf("%s", flag ? "●" : "○");
			}
		}
		printf("\n");
	}

	
	fclose(fd);
	fd = NULL;
	return 0;
}

Version 2

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    FILE* fphzk = NULL;
    int i, j, k, offset;
    int flag;
    unsigned char buffer[32];
    unsigned char word[5];
    unsigned char key[8] = {
        0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01
    };
    fphzk = fopen("hzk16", "rb");
    if(fphzk == NULL){
        fprintf(stderr, "error hzk16\n");
        return 1;
    }
    while(1){
        printf("Enter the Chinese characters (multiple) to be generated:");
        for(;;){
            fgets((char*)word, 3, stdin);
            if(*word == '\n') 
                break;
            offset = (94*(unsigned int)(word[0]-0xa0-1)+(word[1]-0xa0-1))*32;
            fseek(fphzk, offset, SEEK_SET);
            fread(buffer, 1, 32, fphzk);
            for(k=0; k<16; k++){
                for(j=0; j<2; j++){
                    for(i=0; i<8; i++){
                        flag = buffer[k*2+j]&key[i];
                        printf("%s", flag?"●":"○");
                    }
                }
                printf("\n");
            }
            printf("uchar code key[32] = {");
            for(k=0; k<31; k++){
                printf("0x%02X,", buffer[k]);
            }
            printf("0x%02X};\n", buffer[31]);
            printf("\n");
        }
    }
    fclose(fphzk);
    fphzk = NULL;
    return 0;
}

Version 3

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    FILE* fphzk = NULL;
    int i, j, k, offset;
    int flag;
    unsigned char buffer[32];
    unsigned char word[2] = {0xCE, 0xD2}; // Change to your Chinese character encoding after transcoding
    unsigned char key[8] = { 0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01 };
    fphzk = fopen("hzk16", "rb");
    if(fphzk == NULL){
        fprintf(stderr, "error hzk16\n");
        return 1;
    }

    offset = (94*(unsigned int)(word[0]-0xa0-1)+(word[1]-0xa0-1))*32;
    fseek(fphzk, offset, SEEK_SET);
    fread(buffer, 1, 32, fphzk);
    for(k=0; k<16; k++){
        for(j=0; j<2; j++){
            for(i=0; i<8; i++){
                flag = buffer[k*2+j]&key[i];
                printf("%s", flag?"●":"○");
            }
        }
        printf("\n");
    }

    for(k=0; k<31; k++){
        printf("0x%02X,", buffer[k]);
    }

    printf("\n");

    fclose(fphzk);
    fphzk = NULL;
    return 0;
}

Intelligent Recommendation

Nginx Chinese character becomes 16

Why can't 80% of the code farmers can't do architects? >>>   Problem Description: After receiving the URL GET method with the NGINX of OpenResty, the log of the relevant parameters ...

Python achieves Chinese character dot matrix display

The effect is as follows, there must be HZK16 in the code folder to store GBK character set files Principle: A Chinese character can be displayed normally only when it is displayed in a dot matrix for...

Dot matrix letter library algorithm (Chinese and English)

Due to the high-definition switching business needs of the camera, you need to switch between different sizes; The basic principle is to expand the point of the dot matrix. It is twice that is a point...

Use font HZK16 to drive oled0.96(ssd1306) to display Chinese

Hardware Platform: Friendly Arm Tiny4412 Software platform: Ubuntu16.04 Source code location:https://github.com/lian494362816/Tiny4412/tree/master/SourceCode/Driver/003_spi The focus of this article i...

Electronic Design Tutorial 50: 16*16 LED Dot Matrix Screen Driver-LED Dot Matrix Screen Working Principle

  I tried to cascade the shift register + 38 decoder to achieve 3 control lines and drive 16The effect of 16LED dot matrix screen. This is the second blog about the working principle of LED ...

More Recommendation

How can the dot matrix font make the character display more compact?

Whether the dot matrix font can be displayed compactly depends entirely on the font. 1. Non-monospaced fonts Due to different application scenarios, different requirements? For example: printers have ...

ucGUI/emWin custom Chinese font library (Chinese character font library)

Tool: Use the official font conversion tool ucGUI-FontConvert (mine is v2.16) download link: http://download.csdn.net/download/aeroyoung/10205530 step: 1. Double-click to open the software, as shown i...

Android dot matrix font

1. dot matrix font   The dot matrix font is to divide each Chinese character into 16×16 or 24×24 points, and then use the virtual reality of each point to represent the outline of the...

Use java to read Chinese dot matrix font and display

Use java to read Chinese dot matrix font Chinese dot matrix font principle Specific java code to prepare for the subsequent use (ssd1306) 12864oled (or 12832oled) to display Chinese~ Chinese dot matri...

16. Character

2019 Unicorn Enterprise Heavy Glour Recruitment Python Engineer Standard >>> Character type The computers represent characters as integers, character set definition characters and values ​​ma...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top