When a teacher at a technical college forces students to write notes by hand, it turns out something like this:
This is the result of the work of the program that generates the handwriting text in the user's handwriting. It can change the thickness of the pen and the color of the paste, write letters together or separately, supports writing in many different languages and is potentially able to wrap words through syllables into many of them. Written in C ++ / Qt, there are versions for Windows and Linux. Further there will be a small analysis of a handwritten letter, a description of various ways to imitate it, analysis of the most interesting moments of the program and a link to the repository.
Why is software generation of individual handwritten writing - is it difficult? The fact is that a handwritten letter is very changeable, there is no such hard predestination, as in printing. For a realistic imitation of a handwritten letter, at least these are its features:
All this is influenced by the manner of writing, the type of writing material, the substrate, the speed of writing, the prevailing hand, the mood and well-being of the scribe, and a whole lot more. That is, you need not only to take into account a huge bunch of factors, but also to allow the user of the program to customize them at his discretion.
In practice, it is rarely necessary to take all this into account. For example, for a small handwritten print on a card or invitation, use the usual handwritten font OpenType. However, in certain conditions it may be suitable for the abstract. On the web, you can find instructions on how to create an individual font yourself and print them from MS Word, and for English-speaking users there are whole generators of handwritten fonts.
But with this method, plausibility suffers: the characters will be exactly the same, the strings are perfectly smooth. There will be no chance, vyvlaglaznosti and chaos, which in my experience are characteristic of the vast majority of abstracts, and even for a neat summary such perfection is unnatural.
The basic idea is this: we take a printed text, for each character we take a handwritten glyph and place it on the virtual sheet of paper in the right place. At the same time, for the same character, the glyphs should be several, and the specific one should be chosen randomly, otherwise there will be the same problems as the OpenType fonts. Then it will be possible to make various distortions both on the whole sheet of paper, and for a single line or character. Probably, it will be possible to distort the glyphs in such a way that from one entered glyph you can immediately get a ready-made set.
All the same can be done with ligatures and, accordingly, with combinations of symbols, in order to take into account the influence of the adjacent letters on each other. Spoiler: unfortunately, my program does not support ligatures and cannot distort glyphs.
I am not the first to decide that software generation is a good idea, and I wrote my own program for creating manuscripts. I know the Bruise and Handwriter programs, as well as the Scribe service. They, however, have certain disadvantages that led me to write my own program: Bruise, for example, has only one glyph per character and some problems with usability, Handwriter is paid, and the Scribe does not allow to create your own font.
Since we are talking about an individual letter, it is necessary to provide the opportunity to hammer in our glyphs into the program. A fairly obvious solution is to allow the user to print a template to fill in and then take the glyphs from the scan of the filled template. However, for this you need to solve the following tasks:
The difficulty is not just to do it, but to do it qualitatively for countless scanners with different characteristics, scanning parameters and different paper. I decided that I should not take up such a voluminous task.
So my program uses vector graphics. The font creation algorithm is now like this: the user opens his favorite vector editor, draws the necessary glyphs (preferably using a tablet), saves them, and then loads them into the program. The disadvantages of this solution are as follows:
But different bonuses appear:
The characters are different: some are strictly within the line, others stand for its edge, and others lie on the upper or lower border. The protruding parts of the characters in this case may be above the adjacent characters or under them. Therefore, to correctly place a character, you need to know which part of it will be located within the line and how other characters should be placed relative to it.
The yellow rectangle is just that part of the character that is within the string.
In the font editor, you can just set the position of the character relative to the line and other characters, indicating the borders of the character with a yellow frame. And here, of course, symbols and specific glyphs are aligned.
In addition to the yellow frame, two round pointers are also noticeable in the font editor. These are the points where connecting lines will come from neighboring letters. Yes, for consistent writing, the words next to each other are simply joined by straight lines. In theory, it would be better to use splines, but if you write letters without long tails, given that the program will draw tails for the user, it will not be thrown into the eyes.
You can specify all this manually for each character - you can shoot yourself. We must somehow simplify the process of creating your own font.
First, there is an automatic loading of glyphs. When saving a glyph from your favorite vector editor, it is enough to name the file by a specific pattern, and the program will assign it to the desired character. The pattern is as follows: the symbol itself, then, if necessary, the number; You can separate them with underscores. And, since Windows does not see a big difference between upper and lower case letters, for the capital letters one should add the prefix “UP_”. True, this trick does not work with all the characters, because not everything can be used in the file name, so instead of forbidden characters you can write their name.
Secondly, with a continuous writing, the connecting line, as a rule, enters the beginning of the first line of the letter, and leaves the end of the last line. Since, as a rule, vector editors save information about lines in the sequence in which the lines were drawn, we know which line was drawn first and which line was last. That is, the program can take most of the work on the placement of round pointers. And takes.
And third, the program can also indicate the yellow rectangle itself, simply by placing it along the boundaries of the glyph. This is often wrong, but at least some percentage of correctly supplied data will already make life easier for the user.
In order to properly break up words into syllables, the P. Christ's algorithm is used in the modification of Dymchenko and Varsanofyev. In short: using regular expressions, two groups of letters are described, between which there should be a hyphen. Then, using a consistent application of these rules, a specific word is divided into syllables, the hyphen nearest to the edge is selected, and the entire part of the word right from the hyphen is transferred to a new line.
Such rules already exist for the Russian language. They are not perfectly accurate, but supposedly cover 99% of all transfers. Also, I believe, they can be developed for some other languages. But not for everyone. For example, in English, to translate words by syllables would require a much more complicated algorithm, since words are carried by sound rather than by writing.
Here are the rules for the Russian language:
Here L is any letter, G is a vowel, C is a consonant, X is a letter from the “y” set. To allow users to change the rules without modifying the source code, I moved them into a separate file:
I allowed myself to modify the original rules somewhat in order to prevent the separation of one letter from the word.
Vector graphics make it possible to change the parameters of lines, in particular, to round their edges and smooth corners, so why not take advantage of this? In addition, I see no reason to somehow limit the user to the ability to customize the parameters of the sheet and font. A rough idea of the basic settings can be obtained by looking at the screenshot:
Clickable:
As promised - link to the repository: https://github.com/aizenbit/Scribbler
Source: https://habr.com/ru/post/316404/
All Articles