Specifying alternate glyphs for potentially unrepresented unicode code point - web

Unicode has a lot of fancy code points that can be used to do things like change the direction text is formatted, include inband protocol information and other things.
I want to find out if there is a way of specifying, as part of a unicode string, a 'fallback' glyph or set of glyphs where an uncommon unicode code point is unrepresented in the font being used to display the text?
Example:
"æ<character to signify use following as alternate if previous character unavailable>ae<character to specify end of alternate>"
I've had a look through some of these code points at unicode.org and tried some different searches on SO without success. Some of the topics I've looked at:
Is there a way to programatically determine if a font file has a specific Unicode Glyph? (Windows/C#)
How to map code points to unicode characters depending on the font used? (Java)

Related

How can I change the font sizing within multiple lines of a string in Lua? (WoW Lua Question)

I am currently beginning to write functions for WoW WeakAura addons that use the WoW Lua in order to create the addon. I am trying to change the fonts within the printed string so that I have a Header essentially.
The code is essentially as follows:
return string.format("|cFFFFFFFFHeader Text|r\nRegular Text|r\nMore Regular Text|r\nEven More Regular Text")
The current printing returns all of the text the same size, but I just want to make the "Header Text" a size larger. I've found how to modify the colors, but I have yet to find anything similar for font size. Can someone assist?

Extracting paragraph styles from a DOCX in Python3

I have been trying to solve this problem for a while in Python3.
I normally use to extract some information from DOCX documents, by using python-docx library.
from docx.document import Document
from docx import Document
document = Document("test.docx")
for paragraph in document.paragraphs:
for run in paragraph.runs:
print(run.font.name)
#returns None
So, as you can see from the above code, this is a very simple python-docx code to extract some information. I can access some properties such as; font name, size, outline levels, etc.
However, all of these properties are returning None. Because they haven't been explicitly defined.
I have checked StackOverflow for a similar problem and found these.
Extracting word document with styles associated to the content
How to get actual style of text in word document using python docx
In the documentation, it also says, if it returns None, then it's the Default style, that is inherited.
Also tried some XML parsing, but could not reach the desired parameters:
words = document._element.xpath('//w:r')
WORD_NAMESPACE = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
PARA = WORD_NAMESPACE + 'p'
for elem in document.element.getiterator():
if elem.tag == WORD_NAMESPACE + 'p':
for i, child in enumerate(elem.getchildren()):
if child.tag == WORD_NAMESPACE + 'pPr':
...
# No idea how to access, all the styles with which
# tags etc.
How do we extract these default styles too? I would want to extract, the indentation levels, bold, italic, font name, size, etc properties from a DOCX. What could be the alternative ways. I want to solve it in Python3.
I believe what you're trying to discover is the effective style, which is to say, after the style hierarchy for this run has been traversed, which character formatting attribute has which of its possible values for this particular run.
This is a non-trivial problem. The style hierarchy work similarly to Cascading Style Sheets (CSS) in that formatting attributes can be set at various levels and the "closest" setting wins. At the same time, a run can have no assigned character formatting or style in which case the "farthest away" or "default of last resort" determines its character formatting.
In order to compute the effective formatting you need to traverse the style hierarchy level by level until you find the setting you're looking for (like say font.name). To do that, you need to know what the style hierarchy is for a particular run and then have access to each level of it.
So that's a pretty big ask. Roughly, just to give an idea, the style hierarchy would typically be:
character formatting applied directly to the run, like bold, italic, font name and size.
character formatting applied using a character style
character formatting applied using a character style attached to a paragraph style.
character formatting associated with the default paragraph style.
an assigned document-level default
A client-internal final fallback, client being something like Word, OpenOffice, etc.
There are exceptions when the text appears in a table and a table-style may fit in that hierarchy somewhere. I expect there are others as well.
There's no API support for this in python-docx at the moment and I haven't seen any successful implementations of it. I don't believe it is impossible to get a pretty useful implementation, it's just hard enough that most folks find some way to avoid it.

Partial ligature selection with DirectWrite

Using HitTestTextPosition style API from IDWriteTextLayout I did not managed to handle properly text positions inside "ti", "ffi" or other ligatures with fonts like Calibri. It always returns position after or before ligature not inside like t|i or f|f|i.
What is the recommended way to do a caret movement inside ligatures with DirectWrite API?
There... is no "inside" position if you have GSUB replacements turned on?
Opentype GSUB ligatures are single glyph replacements for codepoint sequences, rather than being "several glyphs, smushed together". They are literally distinct, single glyphs, with single bounding boxes, and a single left and right side bearing for cursor placement/alignment. If you have the text A + E and the font has a ligature replacement that turns it into Ӕ then with ligatures enabled there really are only two cursor positions in that code sequence: |Ӕ and Ӕ|. You can't place the cursor "in the middle", because there is no "middle"; it's a single, atomic, indivisible element.
The same goes for f. ligatures like ff, fi, fl, ffi, ffl, or ſt: these are single glyphs once shaped with GSUB turned on. This is in fact what's supposed to happen: having GSUB ligatures enabled means you expressly want text to be presented—for all intents and purposes—as having atomic glyphs for many-to-one substitutions, like turning the full phrase "صلى الله عليه وعلى آله وسلم‎", as well as variations of that, into the single glyph ﷺ.
If you want to work with the base codepoint sequences (so that if you have a text with f + f + i it doesn't turn that into ffi) you will need to load the font with the liga OpenType feature disabled.
The text editors I know of use the simple hack of (1) dividing the width of the glyph cluster by the number of code points within the cluster (excluding any zero width combining marks), rather than use the GDEF caret positioning information. This includes even Word, which you can tell if you look closely enough below. It's not precise, but since it's simple and close enough at ordinary reading sizes, it's what many do:
(2) I've heard that some may (but don't know which) also use the original glyph advances of the unshaped characters (pre-ligation) and scale them proportionally to the ligature cluster width.
(3) Some text editors may use the GDEF table, but I never knew of any for sure (possibly Adobe In-Design?).
The most challenging aspect of using methods 2 or 3 with IDWriteTextLayout is that accessing the corresponding IDWriteFontFace in that run requires quite the indirection because the specific IDWriteFontFace used (after resolving font family name+WWS+variable font axes) is stored in the layout but not publicly accessible via any "getter" API. The only way you can extract them is by "drawing" the glyph runs via IDWriteTextLayout::Draw into a user-defined IDWriteTextRenderer interface to record all the DWRITE_GLYPH_RUN::fontFace's. Then you could call IDWriteFontFace::GetDesignGlyphAdvances on the code points or IDWriteFontFace::TryGetFontTable to read the OpenType GDEF table (which is complex to read). It's a lot of work, and that's because...
The official PadWrite example has the same issue
IDWriteTextLayout was designed for displaying text rather than editing it. It has some functionality for hit-testing which is useful if you want to display an underlined link in a paragraph and test for it being clicked (in which case the ligature would be whole anyway within a word), or if you want to draw some decorations around some text, but it wasn't really intended for the full editing experience, which includes caret navigation. It was always intended that actual text editing engines (e.g. those used in Word, PowerPoint, OpenOffice, ...) would call the lower level API's, which they do.
The PadWrite sample I wrote is a little misleading because although it supports basic editing, that was just so you can play around with the formatting and see how things worked. It had a long way to go before it could really be an interactive editor. For one (the big one), it completely recreated the IDWriteTextLayout each edit, which is why the sample only presented a few paragraphs of text, because a full editor with several pages of text would want to incrementally update the text. I don't work on that team anymore, but I've thought of creating a DWrite helper library on GitHub to fill in some hindsight gaps, and if I ever did, I'd probably just ... use method 1 :b.

Truetype font spacing?

I'm in the process of vectorizing true type fonts to render them as Direct3D primitives. So far I've successfully managed to extract the glyphs using GetGlyphOutline, and render them as linelist primitives. Anyway, on to the spacing...
How can I find out the spacing between characters, or how/where is it determined?
Clearly the spaces vary with non-monospace fonts.
Is there a GDI+ or other windows function call to determine the spacing?
You are referring to the advance width of the glyphs (stored in the hmtx table of the font, or vmtx for vertical text). Via GDI, which it sounds like you are using rather than DirectWrite, you can use:
GetCharABCWidthsI if you already have an array of glyph ids. This approach can support any character in Unicode including extended CJK and newer additions like Egyptian heiroglyphics, not just the lower basic multilingual plane.
GetCharWidth32 for character widths - limited to the basic plane, but the simple option.
Call GetGlyphOutline with GGO_METRICS and use gmCellIncX. This returns more fields than you're probably interested in though.
Kerning is an additional optional adjustment to the nominal advance, such as in the word "AVATAR" where 'A' and 'V' would be shifter closer for aesthetic purposes.
I don't know anything about direct3d, but if your just after the metrics they are stored in the typefaces kerning table; GetKerningPairs will tell you the correct placement for sets of character pairs.

Laying out graphics in RTF

I'm interested how to construct certain kinds of layout in RTF documents, ideally using techniques that do not depend only on the most recent RTF standards, and that are "native", i.e., they do not involve embedding other representations, like picture files. In particular:
In Postscript and DVI, I can specify a coordinate at any time that the next text will be printed at: can this be done with RTF?
Can RTF compose characters through overstriking?
Can lines, outline boxes and filled boxes be drawn, with their geometry specified either absolutely, or relative to text?
You can use the \pvpg \phpg \posx123 \posy123 construct after
you start a paragraph with \pard to position it relative to the top left of the page. See: http://biblioscape.com/rtf15_spec.htm#Heading39
Yes, but it's rather involved, and I think it was only introduced in RTF 1.5. See the drawing objects section of the spec. Here is a basic example of drawing a box (I'm not sure it's entirely valid but it should give you an idea of how to work with drawing objects):
{\rtf1\ansi\deff0
{\pard {\*\do
\dobxcolumn \dobypara
\dprect \dpx0 \dpy0 \dpxsize1000 \dpysize1000 \dplinew25
}\par}
}
If you're doing any work with RTF it's worth picking up O'Reilly's RTF Pocket Guide.
I don't believe this is possible. You'd need to use tabs and newlines to get the text where you want it.
Not really, unless \strike and \strikedl count.
http://www.biblioscape.com/rtf15_spec.htm#Heading52 says drawing objects are an option, and so is inserting images, but neither are really "native", both being absent in the first RTF specs. (And the latter is a bad choice for i.e. just a line.)

Resources