Java: Determine logical font from a font name - java

I'm working on a RESTful service in Java. Part of this is to take data that is stored in a database that contains a font name (e.g. Arial) and return JSON beans to a client that contains not only the font name to use, but the logical font that it belongs to (e.g. serif, monospace, etc...).
I've looked at using the java.awt.Font class to try and get this information, but when i call getName(), I always only ever get back the original font name (Arial). What I want is to get the logical font that this font belongs to (Sans-Serif).
Here is an example of what I've tried:
Font[] tfonts;
tfonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for (int i = 0; i < tfonts.length; i++) {
System.out.print(tfonts[i].getFontName() + " : ");
System.out.print(tfonts[i].getFamily() + " : ");
System.out.print(tfonts[i].getName());
System.out.println();
}
The output of this is basically:
Times New Roman : Times New Roman : Times New Roman
It is surprising that the root logical font is not found anywhere here. I was hoping to see Serif in here somewhere. I've even tried inspecting the output of the getAttributes() call, and don't see this information there.
The reason I want to do this, we have data objects that contain a field of display fonts that were created from fonts available on a user's PC. We are sending these objects to other platforms (namely iOS) where these fonts may not actually exist. What I'd like to do is send "Serif" or something in its place, and let the iOS clients pick a suitable Serif font. But I cannot seem to find a way to get back to the logical font for a given named font.
I appreciate any help anyone can provide.

Related

PDFlib copy page and use font

The PDFlib example search and replace text copies pages and pastes rectangles and text.
Instead of loading a font from my hard disk (like it is done in the example with int font = p.load_font(REPLACEMENT_FONT, "unicode", "");) I'd like to use the original font from the source document.
How can I achieve this?
What I tried is this:
When using int font = 0 (which is equivalent to the value of tet.fontid in line 244), PDFlib throws an exception like this:
com.pdflib.PDFlibException: Option 'font' has bad font handle 0
at com.pdflib.pdflib.PDF_fit_textline(Native Method)
at com.pdflib.pdflib.fit_textline(pdflib.java:1086)
What could work (and what I'm also not able to get to run)
Maybe I could read the fonts in the target document. Reading fonts in source document is feasible with this: (int) lib.pcos_get_number(pdiHandle, "length:fonts");. Trying to read the fonts in target document with (int) lib.pcos_get_number(outputPdfHandle, "length:fonts"); (with outputPdfHandle = p.begin_document(outfilename, "") from example line 560) throws exception
com.pdflib.PDFlibException: Handle parameter or option of type 'PDI document' has bad value 1
at com.pdflib.pdflib.PDF_pcos_get_number(Native Method)
at com.pdflib.pdflib.pcos_get_number(pdflib.java:1539)
It is not possible to use a font from a document imported via PDI to create text in an output document. In theory the idea sounds attractive to access the font data from the input document via pCOS functions. One could think that it should be possible to reassemble the font data into for example a valid TrueType font that then can be loaded via the PDFlib load_font() function.
But that is not possible for the following reasons:
The font data that is stored in a PDF document is not the complete data that is stored in a TrueType font. Important TrueType tables are missing and cannot be reconstructed from the font data in the PDF file.
A font in a PDF file is almost always a subset that contains only the glyphs that are actually used in the document. So even if it would be possible to use the font data from the input document, you could use only glyphs from the subset to create new text in an output document.
Also the fontid value provided by TET cannot be used as a font handle when creating new output via PDFlib. The fontid value is the index in the pCOS pseudo object array fonts[], and it is totally unrelated to any handles used to create new output via the PDFlib API.

Java: using deriveFont does not change font size

I am loading a custom font (from ttf file) into my project, and using deriveFont(float f) to change the size. However, the size is not actually being set (stuck at 1). Here is my code:
public static void main(String[] args) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
Font mont =
Font.createFont(
Font.TRUETYPE_FONT,
new File(System.getProperty("user.dir") + "/data/Montserrat-MediumItalic.ttf"))
.deriveFont(20f);
ge.registerFont(mont);
Arrays.stream(ge.getAllFonts())
.filter(font -> font.getFontName().contains("Mont"))
.forEach(font -> System.out.println(font.getFontName() + ", Size: " + font.getSize()));
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
}
output:
Montserrat Medium Italic, Size: 1
note: replacing font.getSize() with font.getSize2D prints 1.0.
New: Using decode:
I am now using this
Font test = Font.decode("Montserrat Medium Italic-ITALIC-20");
(fixed class not loading)
Update 2:
this line:
Font mont = Font.createFont(Font.ITALIC, new File(System.getProperty("user.dir") + "/data/Montserrat-MediumItalic.ttf"));
throws IllegalArgumentException: font format not recognized
However, the size is not actually being set (stuck at 1).
This seems unlikely to be the case. I asked for direct confirmation in a comment on the question ("What mont.getSize() return?" -- oops, what embarrassingly bad grammar), but so far you have not answered. I am reasonably confident that if you check, you will see that mont.getSize() evaluates to the size you requested.
An alternative explanation for your observed behavior is readily available. You are using GraphicsEnvironment.getAllFonts() to report on the registered fonts, but according to its documentation, this method
Returns an array containing a one-point size instance of all fonts
available in this GraphicsEnvironment.
(Emphasis added.)
Another answer and especially comments on it suggest that the Font objects returned by GraphicsEnvironment.getAllFonts() might differ in other ways, too, from corresponding Font instances passed to GraphicsEnvironment.registerFont(). Although such variations are not documented as far as I can see, they are consistent with the intended usage of Font objects obtained from a GE, as the getAllFonts() docs describe:
Typical usage would be to allow a user to select a particular font. Then, the application can size the font and set various font attributes by calling the deriveFont method on the chosen instance.
They go on to say that
If a font in this GraphicsEnvironment has multiple programmable variations, only one instance of that Font is returned in the array, and other variations must be derived by the application.
I'm not positive that "multiple programmable variations" means attributes that can be modified when you derive one Font object from another (for then what font wouldn't have programmable variations?), but it is clear that getAllFonts() is not a mechanism for reading back the exact Font objects previously presented to GraphicsEnvironment.registerFont(). Those objects might not even be retained as such.
On the other hand, you can perhaps be relieved that you are not responsible for registering all the different font variations you may want in advance.
I think I know what is the problem. When you call ge.registerFont(mont), it does exactly that, it registers the underlying font, without the modifications to the font object. The deriveFont() function only changes the state of the current font object, but cannot modify the actual font. When you register a font, it is registered with the size of 1. If you print the size of all the other fonts, you will find that they also have the default value of 1. I do not think that you can register a font with a custom default size, or override the default size of Font.getFont(). When you get a font by using Font.getFont(), it will always have the default size of 12 (from the specification).
If you need to have a the font specially formatted, I would suggest creating a static class variable:
Font MontMediumItalic_20;
Then load the font once, either in a resource loader, or the constructor, and apply all the modifications to it.
Alternatively, you can also use Font.decode()
Please let me know if you need any help.

detecting the text of hand written fonts

I need to detect the text on the product
I need to extract the text "GOLD WINNER".
I tried OCR (tesseract) but it didn't work.
I can tell you two way for solution;
You can use opencv solutions like SURF/SIFT, or free ones ORB etc.
Features2D + Homography to find a known object
You can try to find font name of your target image and create a new language data using tesseract.TrainingTesseract

iText: Compare font after processing string using FontSelector

tl;dr:
In iText, is there a way to access the font's name, or figure out the language of a font that has been applied to a Phrase from a FontSelector?
This question is in relation to an issue we've been having for the printwikipedia project --- github -- issue .
I have a body of text that I do not have control over coming in to a FontSelector to be processed. Some of that text is in Arabic and some is in Hebrew and I am trying to figure out the best way of detecting the type of font in order to have it print correctly as seen here: http://developers.itextpdf.com/question/how-create-persian-content-pdf
using
pdfCell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
After running the strings through the FontSelector to become Phrases and then placing them within the cell formatted with the code above all the text ends up on the right.
Formatting of this text is very important so I cannot have all of my text aligned to the right but only when it is meant to be read as right to left. So what I believe should be the best course of action is to detect the font that has been applied to the Phrase and then alter the cell if necessary.
public FontSelector fs = new FontSelector();
// add a whole lot of fonts to fs
// incoming line of some sort of text
Phrase ph = fs.process(line);
System.out.println(ph.getFont().toString());
The above code will output some extremely varied results. Pretty much a new id for every font created for each piece of text. I can't figure out a way to compare a font that exists in the fontselector object with a font that has been applied to the incoming text.
Is this the best method to figure out what fonts are in a phrase?
How can I access the font's name, or figure out the language of a font that has been applied to a Phrase from a FontSelector?
I went through the itext documentation a bit more carefully and discovered my answer.
Phrases are made up of Chunks which contain the fonts. If one simple does a .getChunks() command on the output phrase and then iterators over the chunks they can then compare the fonts by doings a .getFont() on the chunks in the arraylist and proceeding to apply whatever styles you wish from there.

Syntax for generating Barcode using PCL syntax

Iam searching for a PCL syntax to generate and print the BARCODE. If anyone having any information about the same, please help me. I have tried googling it, but didnt find anything.
First you need a barcode font. Once you have this simply move the cursor to the spot you want (in the example 300 x 300 in whatever unit of measure you've defined), and then call the barocde and put your text down. A simple exmample might look like htis:
<ESC>*p300x300Y<ESC>*c100D1234567890
This assumes that you have embedded the barcode as part of the print job and assigned the numeric 1000 to the font call. Just search the web for "pcl barcode font" and you will find many sites that sell these fonts and provide instructions on how to call. If the printer has barcode fonts embedded, try printing a font list and it should provide the escape sequence you need to call.

Categories

Resources