How do you draw a particular glyph using Java on Android? - java

I have seen http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/font/GlyphVector.html but I don't know how you would use it to display a glyph on the screen. Lets say you want to print glyph number 1042 (likely to be different in each font and unlikely (but possible) to be the same as Unicode 1042) to the screen. How do you go from the number to the character on screen? Is GlyphVector the way to go or is there a better method?

That GlyphVector class is not available on Android. There don’t seem to be any public API calls in Android graphics that allow access to font glyphs without going through the Unicode encoding.
Your obvious option would seem to be direct parsing of the TrueType font file. Perhaps find some library somewhere that has been ported to Android, or could be so ported (Freetype?).

Related

Respecting Font Ligatures in Java/Swing

I've run into a problem that has me really scratching my head when dealing with Swing objects in java (and JFX as well, but I'll worry about that later...).
Here is the code that I am using to open fonts in my program. It's pretty standard.
public static Font getFontFromFile(String filePath) throws FontFormatException, IOException {
File fontFile = new File(filePath);
return Font.createFont(Font.TRUETYPE_FONT, fontFile);
}
My problem is that when I actually go to use these fonts, ligatures within them are not respected. One of the core features of my program allows users to load custom fonts, many of which have specialized ligatures. These are not being respected, and I'm not sure what I am doing wrong. If the user types two characters that should reduce to a single ligature, the second character just appears normally, with no transformation taking place. When I load the fonts into any other program or text editor I'm seeing the ligatures be respected exactly as I would anticipate.
I've stripped down code where I'm actually setting the font in a few places to the absolute bare bones and I'm seeing the (wrong) behavior even in places where I'm doing something as simple as:
jTextField.setFont(myFont);
Is there some setting on import that I am missing? Or that needs to be globally flipped on the 2D graphics object? Any assistance would be really appreciated. Thanks,
FOUND IT.
Ok. So. This one was frustrating, as I had been correctly setting the TextAttributes, while loading fonts via a method which is plagued by a long standing bug... Ligatures are supposed to be represented in JTextField and JTextArea objects, but under certain circumstances, they will not be. There are two things to keep in mind.
There is an old bug (https://bugs.openjdk.java.net/browse/JDK-8139741) which prevents ligatures from being respected when using the pattern
// BROKEN PATTERN
Font myFont = new Font(<FONT_FAMILY_NAME>, Font.PLAIN, 72);
Instead, the font ligatures will only be respected if it is loaded from a binary location:
// WORKING PATTERN
File myFontFile = new File(<FILE_LOCATION>);
Font fixed = Font.createFont(Font.TRUETYPE_FONT, myFontFile);
Once the font is loaded from binary, ligatures must be set via TextAttributes like so:
Map attributes = fixed.getAttributes();
attributes.put(TextAttribute.LIGATURES, TextAttribute.LIGATURES_ON);
fixed = fixed.deriveFont(attributes);
When loading fonts directly from the OS, I've taken to finding the actual font file on the system and loading it as a binary. It's cumbersome, but it functions.
At least with respect to Java2D, I read this - see Figure 4-13 as suggesting that Java will simply take the list of code points provided and render the glyph for each without combining to make ligatures.
Back before JavaFX 2, there used to be a boolean ligatures property on javafx.scene.text.Font that claimed to control whether ligatures were used, but that doesn't appear to be there anymore.
Historically (it's been at least 7 years) I've had to parse TT/OT fonts for ligatures and do the combining manually (and sometimes even pair kerning).

how to visualize control characters

Modern text editors like Notepad++ can visualize control characters like CR, LF, STX, ETX, EOT. I have started to wonder how text editors visualize these characters so neatly.
Note: I am familiar with how encodings and character sets work. And I'm also familiar with the reason why these characters exist.
Some ideas:
Does it apply a special font for these specific characters ?
i.e. a font which contains a representation of all characters.
Or does it use an advanced text-field control/gui-component that renders (i.e. draws) them on the canvas ?
Or does it just replace the characters ? (e.g. replacing a 0x0D with unicode character 0x240D i.e. ␍)
This seems to be the easiest. But then how does it preserve the fact that copying the text still keeps the original text.
The reason for my question: I would like to create a java application that does the same thing.
The 3rd idea should be true, you can replace the charaters with unicode control pictures using a proper font.
There are some inherent problems with assigning glyphs ('images') to Control Codes; most have to do with the case that they already have a particular use! For example, if you send a Tab code to your display, you'd typically expect the cursor to move by a certain number of positions and not to see a character ○ pop up.
Also, typically, fonts use Unicode as their native encoding. Unicode does not allow a glyph to be assigned to the control codes:
Sixty-five code points (U+0000–U+001F and U+007F–U+009F) are reserved as control codes (https://en.wikipedia.org/wiki/Unicode)
There is an 'alias' sort of set defined: U+2400 to U+241F for 0x00 to 0x1f, U+2420 "␠" for "symbol for space", and U+2421 "␡" for "symbol for Delete" (your #3) but then you need to make sure the user has a font that contains these glyphs.
The most configurable way is to 'manually' draw whatever you like. This means you can use any font you want (without the need for a special font), and character replacement is not necessary (only the drawing code need to filter out 'specials'). A drawback, though, is that you are also in charge of drawing regular text.
If that is overkill or you don't have sufficient control over the text draw area, you can simply use different foreground and background colors for the control characters only. This is a screenshot of a quick-and-dirty hex viewer I wrote a while ago – I only change the colors here, but I could have written out custom text for all as well.
For a good overview of what it takes, see James Brown's Design & Implementation of a Win32 Text Editor; it focuses on using Win32 API calls but there is a lot of background as well. Drawing neat Control Codes is addressed in the section Enhanced Drawing & Painting.

Detect Large and Small font sizes of Tesseract OCR Java implementation

Is it possible to OCR a picture and identify different sizes of fonts in the picture using Tesseract OCR. If yes, do I need to use any other 3rd party library or can I use pure Java. For an example,
I want to detect the headline and the content of a newspaper by using the font size.
Any help regarding this matter would be appreciated.
You can use ResultIterator.WordFontAttributes API method (example in Java using Tess4J) to retrieve font information, including font name and size, of the recognized text.
The hOCR output of Tesseract includes the bounding boxes of lines and words which can be used to determine size and it can be configured to also include the point size of the font in the output by turning on the hocr_font_info config variable.

Defining new Characters in Java (Android)

I am looking to apply an alphabet that does not exist in the UTF-Setup currently applied to all of my view elements and Swing components. To display these new characters, would I have to simply have each as its own image and then present the images adjacent to one another as in a character-like pattern, or is there any method by which to import letters from pictures to be added to something like a text area upon acting on a button?
Basically, if I have a pictograph system, may I import these images as characters, or would I have to maintain them as pictures?
To give some specificity, picture, Klingon writing or Dragon language, something that certainly is not defined in the standard packages of Character sets.
Thank you!
Best way I can think of to do this is to create some font file (.ttf, .otf, etc.) representing your special alphabet and then proceed to follow the instructions in this answer here.
Downside is, there really isn't any easy way to create font files. Usually it involves many hours manually tracing symbols using a vector graphic editor and compiling those to a font file.
If your characters are already vector images, then most of the work will have already been done.

How to use fonts in opengl in java?

The title pretty much sums it up - its the easiest thing in the
world in C++ and Windows, but Java seems to struggle with this issue.
I would like to stay away from solutions involving loading bitmaps of fonts, and instead try go for a native truetype font loader, if possible. Kerning and antialiasing is quite important in my application.
JOGL has a class, TextRenderer, which should do what you want. It accepts a Java "Font" object, and I think you can specify any ttf file for such objects. It does also support antialiasing, I'm not sure what kerning is but probably that too. :)
Unfortunately the JOGL docs are currently... missing. Dunno where they went, hopefully they'll be back soon (in a day or two). Otherwise I would link you to the documentation for the class. Sorry!
Use the JOGL/Java 2D bridge: render the font on a texture.
Check out "Java 2D and JOGL"[1]
[1]: http://weblogs.java.net/blog/campbell/archive/2007/01/java_2d_and_jog.html ""
Another thing you can do (e.g. if you want full lighting functionality for the text) is construct a GlyphVector from the font and string and use a GLUtessellator to render it as a set of triangles.
JOGL is pretty good. I've used Processing (processing.org) before that renders nice text. Yes, C++ and OpenGL under Windows does seem easy as the Font management is different (Linux and MacOSX = Much harder to do text with OpenGL I've found)

Categories