Partial ligature selection with DirectWrite - opentype

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.

Related

DirectWrite painting ligatures with multiple colors?

I'm using Microsoft's Cascadia Code font and drawing text with DirectWrite using ID2D1RenderTarget::DrawTextLayout, with individual glyphs colored using IDWriteTextLayout::SetDrawingEffect(CreateSolidColorBrush(...)).
Cascadia Code has a ligature for "greater than or equal to" (=>) that draws as a single arrow glyph. If I have half of the glyph selected, using HitTestTextRange to paint the background highlights the correct area, but setting the selected character's drawingEffect to a different color than the unselected side doesn't work. The entire glyph is painted using the drawing effect from the second character, resulting on one side drawing as white-on-white or black-on-blue. GetClusterMetrics returns two separate clusters for it. Conversely, if I have some Arabic text like ممم, which presumably uses a substituted font, it draws as a ligature, but the individual characters within it will draw as different colors.
Is implementing IDWriteTextRenderer the best way to handle this situation or is there an easier one?
Also, comparing Cascadia Code's supported ligatures in Notepad or Visual Studio Code against my app shows that most ligatures are drawn like they should be, but a handful of them aren't. -~ (minus tilde) draws as a single symbol in Notepad, VS Code, my app, and the Windows SDK's PadWrite sample app, but /\ only draws as an inverted V in the first two. The "infinite arrows" like >==>==> are similarly broken up. Is there a setting I need to use to enable all of them?

Minimise width of the multi-line text in Flutter

I have a variable-width container (screen width) and fixed left and right margins. Remaining area gives me a constraint for maximum text width, where I'm placing text of variable content (multiple languages). I want text to fit into that width without truncation, wrapping if needed. Text is relatively short - it may fit into single line, likely to fit into 2 lines and will fit into 3 lines for sure.
I want to avoid cases like (2) - where text wraps into long and a short line. It does not look nice. Instead I would like it to wrap more evenly, like in case (3).
For the demo purposes I've hardcoded hand-picked margins, but I want solution to work automatically for any text content and any container width.
Is it achievable using built-in Flutter widgets, or I need to implement something custom, similar to https://pub.dartlang.org/packages/auto_size_text?

How can I draw to an XY position in Emacs?

I wanted to allow the Emacs cursor to move around freely outside of actual text (similar to virtualedit=all in Vim).
"Oh," I thought, "I'll just keep track of a virtual cursor and draw it to the screen myself."
But it turns out the actual native C drawing routines (such as draw_glyphs) seem to refer back to the buffer contents to decide what to draw (I could be wrong though).
My next idea was to make a giant overlay of all spaces so I'd have complete freedom where to put stuff. But an overlay only goes over ranges of actual text, so again, this does not seem to give me what I'm looking for.
Is this a reasonable goal without hacking the C code?
I believe the writeable area of a window is intrinsically limited to the buffer with which it is associated, i.e. you have to draw in an area where buffer content exists.
(One example of this limitation is the impossibility of drawing a vertical guide line in the 80th column to help the user identify long lines; currently the best possible implementation of such a feature is to highlight the "overflow" of each too-long line.)
You can do the same as what artist-mode does without adding spaces to the buffer:
when trying to place the cursor after the end of the line, just use an overlay with an after-string property which adds the spaces in the display without modifying the buffer.
Have a look at "artist-mode" (M-xartist-modeRET) - it allows you to draw in Emacs.
From the function documentation: "Artist lets you draw lines, squares, rectangles and poly-lines, ellipses and circles with your mouse and/or keyboard."
You can look at popup.el from the auto-complete package, which can pop up tooltips and menus and such at any position, including positions outside the contents of the buffer. Maybe that will show you how you can do it.

How to display conjuncted letters [Bengali Language] using LWUIT in mobile?

I have been trying to develop a simple J2ME application using LWUIT in Bengali language. However, because of heavy usage of vowels as conjuncted letters in Bengali language, I am facing some problems with LWUIT.
For example let us say, “X” is a consonant letter and “#” works as a vowel in Bengali; now they are combined together when needed becoming a conjuncted format “X#”.
Using LWUIT, when I add such vowels and try to display them as the conjuncted format with a consonant in a real application, they are combined with their previous letter (which is in a consecutive order) as defined in the charset. Although interestingly, in the LWUIT designer display/preview, the characters appear correctly.
For details, kindly download this document here (http://dibbaa.com/lwuit/doc/lwuit.doc) and see the real-life examples.
I will appreciate if anybody can help me out on this. Just let me know how can I set LWUIT framework in such a way so that it doesn’t combine the letters as they defined in the charset by consecutive order while painting them.
I have used LWUIT version 1.3 and font “KarnaphuliP.ttf” for my application.
Thanks
I know nothing about Bangali, and I cannot find the font you mentioned. But I managed to get an alternative font for recreating the problem:"Bengali-Progoty.TTF" (which, unfortunately, is not a bit similar to yours). You can get the font here:Bengali-Progoty.TTF.
Those vowels are special, in that their width are zero ,and I bet their origin point is the right-top point, instead of left-top. This way, vowels can be drawn on top of other characters preceding them.
When lwuit designer generates bitmap font, it draws every character (What I mean is, unicode character) onto a big bitmap, calculates the width of current character, add that width to current offset, and draws the next character. As a vowel has a width of zero, it will be combined into the last non-vowel character preceding it.
To solve this problem, you can either switch to unicode font (Bangali has a place in unicode), or you can stick to the current font and do some customization work to the font generation process.
1 Create your own class overriding the EditorFont class in lwuit's editor.jar.
2 Override EditorFont#getBitmapFont() method, do your own drawing of every character. You can test if any character is a vowel, and if so, draw it with a preceding space.
3 Override the FontTask Ant task provided in lwuit's editor.jar.
4 Override the FontTask#addToResources() method, insert your own EditorFont instance instead of the original one.
5 Override the LWUITTask class, add an AddXXX method to support your overriden FontTask.
6 Build a resource using ant, and use your own version of LWUITTask and FontTask instead of the original version.
7 As vowels have become regular characters, they will take up the same space as other characters and cannot be drawn on top of other characters any more. You have to draw them on top of other characters manually. The com.sun.lwuit.CustomFont class may have to be overriden in order to draw these vowels correctly.
Given the complexity introduced, I highly recommend switching to unicode font. But as I have said, I know nothing about Bangali and cannot tell if it is adequate to use a unicode font. Maybe you have to do it the hard way after all.
Good luck.
I think it may be better for you to implement your own Virtual keyboard. There is a sample in the LWUIT developer guide and demo. I am sure if u invest time in that it will come in handy later on.

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