![]() |
Handwriting recognition (experimental) | ![]() |
TeXmacs contains the beginning of a recognizer for handwritten characters: a widget in which strokes are drawn with the mouse or a pen, a dialog which learns characters from examples, and a matcher which compares a drawing with the learned examples. It is not connected to the editor: the result of a recognition is only printed on the console, and the dialog is opened by calling (learn-glyphs) by hand. The drawing widget exists in the Widkit ports (X11, SDL and Qtwk, with ink_wk_widget) and in Vue (vue_ink_widget_rep in
vue_widget.cpp
, which passes the strokes as the X11 one does); the Qt and Cocoa ports return an empty widget (ink_widget in
qt_widget.cpp:643
and
ns_widget.mm
), and Qt only says so on the debug output when qt debugging is on.
Graphics/Handwriting/poly_line.hpp,
poly_line.cpp
Points, polylines and contours (lists of strokes), their geometry, normalization, vertex detection and invariants.
learn_handwriting.cpp,
recognize_handwriting.cpp
The table of learned glyphs and the matcher.
smoothen.cpp
simplify, which removes superfluous points of a stroke; it is not called anywhere.
Plugins/Widkit/Misc/ink_widget.cpp
The drawing widget of the Widkit ports.
utils/handwriting/handwriting.scm
The Scheme side: storage of the examples and the learning dialog.
A point is an array<double>, a stroke (poly_line) an array of points and a glyph (contours) an array of strokes. The ink widget of tm-widget records one stroke per press and drag of the left button; the right button erases the strokes within a few pixels of the pointer (
ink_widget.cpp
). When a stroke ends, and after an erasure, commit passes all the strokes to the Scheme callback as a list of lists of coordinate pairs. Leaving the widget to the left clears the drawing and passes an empty list; leaving it to the right passes #t and clears the drawing.
To compare glyphs, invariants (
poly_line.cpp
) first normalizes a glyph into the unit square and then describes it by:
a discrete part: the number of strokes and, at level 1, for each stroke the number of entries returned by vertices, that is its sharp turns plus its two endpoints;
a continuous part: 21 points sampled at equal distances along each stroke and, at level 1, the positions of these vertices along the stroke (as fractions of its length, weighted by 2.5).
register_glyph stores both levels of invariants of an example with its name. recognize_glyph splits a drawing into characters, taking each stroke together with the following ones until a stroke lies entirely to the right of the bounding box of the previous one, unless it lies entirely above it (attached), and for each character looks for the learned examples with the same number of strokes and the same discrete invariants at level 1. Among these, the example with the smallest Euclidean distance between the continuous invariants wins. If there is none at level 1, level 2 (without vertices) is tried. The names of the winners are concatenated; a character without any match contributes nothing.
The matcher can be tried without the widget, from Scheme, with glyph-register and glyph-recognize, which take glyphs as lists of strokes of (x y) pairs. On 2026-10-06, after learning a vertical line as l and a closed loop as o, slightly different drawings were recognized correctly, and two vertical strokes side by side gave ll.
learn-glyphs (
handwriting.scm
) shows a widget with the drawing area, a Learn menu which learns the last drawing as a lowercase or uppercase Latin letter, a digit or a lowercase Greek letter, and a button to recognize it. Both act on the last list of strokes passed by the widget (last-glyph) and do nothing when it is empty or #t. The examples are kept in a hash table from names to lists of glyphs and saved with save-object in ~/.TeXmacs/system/glyphs.scm; they are loaded and registered with glyph-register before the first learning or recognition. The path is written literally, so it ignores $TEXMACS_HOME_PATH: a private home set for tests, or the home directory of TeXmacs on Windows, which is not ~/.TeXmacs, are not used (issue #305 of mgubi/texmacs).