This program includes a basic parser for SVG texts generated by Inkscape. It removes the transformation matrix that is applied to each text line by simply applying the matrix's transformation to the coordinates of the first item on that line. For simplicity, and because the y-coordinate of the first item is always negative in these instances, I only look for the negative value. Inkscape uses a full matrix just to convert a negative y-coordinate to a positive one, so my task is to extract the y-translation from the matrix, eliminate the matrix, and then add this value to the negative y-coordinate of the first element in the line.
By doing this, the matrix effectively becomes an identity matrix, a fact that allows us to remove it. Inkscape usually creates one text item per line, where this item has only one y-coordinate and a series of x-coordinates, one for each character in the text. This setup is commonly used for text justification.
In some versions of Illustrator, text justification can be problematic. When converting files to SVG, a line that appeared perfectly justified on-screen might occasionally split the last word, leaving a hyphen at the end of one line and the remainder of the word on the next. This disrupts the intended formatting where words were carefully chosen to fit neatly within the paragraph. What was once beautiful and harmonious can end up looking awkward and unappealing.
A simple solution to this issue is to convert the entire file in Illustrator to PDF, then import that PDF into Inkscape and save it as an SVG. However, Inkscape tends to be overly verbose and uses internal variables containing the word "inkscape" throughout. The ideal approach here would be to locate the problematic line in the Inkscape-generated file, copy it along with all lines affected by the problem, and replace the corresponding lines in the Illustrator-generated file. However, it's often much simpler to delete the entire paragraph or set of paragraphs from the Illustrator file and replace them with the text from the Inkscape file.
Newer versions of Inkscape have streamlined this process significantly. The only peculiar aspect is the application of one matrix per line solely for a single y-coordinate translation. SVGinktxt is used to automatically strip these matrices from the text items copied from the Inkscape-generated file. The resulting output can then be manually pasted into the appropriate place in the Illustrator-generated file.
The primary focus of this program isn't its use case, but rather how the parser was developed. It uses a methodology that makes it resemble a BNF grammar, achieved through simple programming rules.
The parser was implemented using a new concept I'm experimenting with. It's a generalized "early return"-based parser development methodology where "if" commands never have "else" clauses.
I use only early return functions to parse particular elements. Inside these functions, I employ loops where "if" commands handle specific situations, and "continue" commands allow me to exit these "if" blocks and continue the loop, thus avoiding "else" clauses while mimicking the logic of early returns within loops.
It's widely recognized that at the assembly level, both "if" statements and "else" clauses are implemented with "goto" instructions. Essentially, "else" clauses are just less obvious forms of "goto", while "continue" commands are explicit "goto"s that jump to the next iteration of a loop. There's no inherent drawback to using "continue" in this way; in fact, it can lead to a parser structure that's markedly different. This approach can result in code that's more linear or less nested, potentially enhancing readability or simplifying maintenance.
The code becomes more readable and begins to resemble a BNF grammar, which is typically used to define languages and parsers. This similarity suggests that the code not only becomes easier to understand and write but might also execute faster due to its structure.