I have a problem on Roboto font where the Arabic language is not displayed correctly, on the fonts I'm not very prepared but I think it does not support Arabic unicode.
My project deals with a material theme for swing and therefore I would not change fonts for the Arabic language support so I thought that when the theme is loaded I check for a keyboard layaut and if that language is not supported by a robot then change font to loading, this is the only solution I could find but the solution does not work for devices without a keyboard.
Now I would like to ask you two questions:
Is it possible to find the roboto font that supports Arabic and the languages of that lineage there?
Is there any better solution than mine if I need to change font for support?
---------- Problem Load Font to GraphicsEnvironment -------
I have rewritten fonts of different types and now through a method written by me I go to set the font according to the style and the support of the String but I get this problem when I go to render the look and feel
static {
try {
//Noto sans
Font black = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Black.ttf"));
Font blackItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-BlackItalic.ttf"));
Font bold = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Bold.ttf"));
Font boldItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-BoldItalic.ttf"));
Font italic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Italic.ttf"));
Font light = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Light.ttf"));
Font lightItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-LightItalic.ttf"));
Font medium = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Medium.ttf"));
Font mediumItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-MediumItalic.ttf"));
Font regular = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Regular.ttf"));
Font thin = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-Thin.ttf"));
Font thinItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/NotoSans/NotoSans-ThinItalic.ttf"));
//Register font
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
graphicsEnvironment.registerFont(black);
graphicsEnvironment.registerFont(blackItalic);
graphicsEnvironment.registerFont(bold);
graphicsEnvironment.registerFont(boldItalic);
graphicsEnvironment.registerFont(italic);
graphicsEnvironment.registerFont(light);
graphicsEnvironment.registerFont(lightItalic);
graphicsEnvironment.registerFont(medium);
graphicsEnvironment.registerFont(mediumItalic);
graphicsEnvironment.registerFont(regular);
graphicsEnvironment.registerFont(thin);
graphicsEnvironment.registerFont(thinItalic);
black = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Black.ttf"));
//blackItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/Roboto/Roboto-BlackItalic.ttf"));
bold = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Bold.ttf"));
boldItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-BoldItalic.ttf"));
italic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Italic.ttf"));
light = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Light.ttf"));
lightItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-LightItalic.ttf"));
medium = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Medium.ttf"));
mediumItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-MediumItalic.ttf"));
regular = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Regular.ttf"));
thin = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-Thin.ttf"));
thinItalic = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/fonts/Roboto/Roboto-ThinItalic.ttf"));
//register font
graphicsEnvironment.registerFont(black);
//graphicsEnvironment.registerFont(blackItalic);
graphicsEnvironment.registerFont(bold);
graphicsEnvironment.registerFont(boldItalic);
graphicsEnvironment.registerFont(italic);
graphicsEnvironment.registerFont(light);
graphicsEnvironment.registerFont(lightItalic);
graphicsEnvironment.registerFont(medium);
graphicsEnvironment.registerFont(mediumItalic);
graphicsEnvironment.registerFont(regular);
graphicsEnvironment.registerFont(thin);
graphicsEnvironment.registerFont(thinItalic);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FontFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Font getFont(String textCompatibily, String tipeFont){
String familyFontRoboto = "Roboto";
String familyFontNotoSans = "Noto Sans";
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
//System.out.println(familyFontRoboto + " " + tipeFont);
for(Font font : graphicsEnvironment.getAllFonts()){
if(font.canDisplayUpTo(textCompatibily) < 0){
if(font.getName().equals(familyFontRoboto + " " + tipeFont)){
System.out.println("Roboto ok");
System.out.println(font.toString());
return font;
}
}
}
for(Font font : graphicsEnvironment.getAllFonts()){
if(font.canDisplayUpTo(textCompatibily) < 0){
if(font.getName().equals(familyFontNotoSans + " " + tipeFont)){
System.out.println("noto sans ok");
return font;
}
}
}
System.out.print("ops");
return graphicsEnvironment.getAllFonts()[0]; // Noto sans dovrebbe coprire tutto, altrimenti questa cosa si allunga
}
setting font on the button
Font font = MaterialFonts.getFont(button.getText(), "Black");
button.setFont (font);
problem rendering is
enter image description here
'Roboto' is owned by Google... It doesn't support Arabic ... Google uses a fallback font named 'noto font' where 'roboto' isn't supported...
Related
I know that many people may have asked this question before. I've read almost all of them`but it couldn't help me solve my problem.
I'm using iText java library to generate a Persian PDF. I'm using the following
how to use PdfWriter.RUN_DIRECTION_RTL
code:
String ruta = txtruta.getText();
String contenido= txtcontenido.getText();
try {
FileOutputStream archivo = new FileOutputStream(ruta+".pdf");
Document doc = new Document(PageSize.A4,50,50,50,50);
PdfWriter.getInstance(doc, archivo);
doc.open();
BaseFont bfComic = BaseFont.createFont("D:\\Font\\B Lotus.ttf", BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
Font font = new Font(bfComic, 12,Font.NORMAL);
doc.add(new Paragraph(contenido,font));
doc.close();
JOptionPane.showMessageDialog(null,"ok");
} catch (Exception e) {
System.out.println("Eroor"+e);
}
Output:
Problem
Document.add() doesn't support RTL text. You'll have to use ColumnText.setRunDirection or PdfPTable.setRunDirection.
I haven't worked with Persian language. But, I think your problem will be with the font (B Lotus.ttf) you used. In most of times using a registered Unicode font may solve the problem. Try again using a different font.
Also you can RTL a text phrase using following code.
PdfPCell pdfCell = new PdfPCell(new Phrase(contenido, myUnicodePersianFont));
pdfCell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
You will find out a similar question here.
I succeeded
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser dlg = new JFileChooser();
int option = dlg.showSaveDialog(this);
if(option==JFileChooser.APPROVE_OPTION){
File f = dlg.getSelectedFile();
txtaddress.setText(f.toString());
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String ruta = txtaddress.getText();
String con= content.getText();
try {
FileOutputStream archivo = new FileOutputStream(ruta+".pdf");
Document doc = new Document(PageSize.A4,50,50,50,50);
PdfWriter Writer = PdfWriter.getInstance(doc, archivo);
doc.open();
LanguageProcessor al = new ArabicLigaturizer();
Writer.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
BaseFont bfComic = BaseFont.createFont("D:\\Font\\titr.ttf", BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
Font font = new Font(bfComic, 12,Font.NORMAL);
Paragraph p = new Paragraph(al.process(con),font);
p.setAlignment(Element.ALIGN_RIGHT);
doc.add(p);
doc.close();
JOptionPane.showMessageDialog(null,"Yes");
} catch (Exception e) {
System.out.println("Eroor"+e);
}
}
I need to use a particular font to make a document because it contains specia charactesrs such as "Đ" that are not supported by the normal fonts iText comes with.
So, I made this:
BaseFont CROACIA = BaseFont.createFont("C:\\FreeSans.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
Font CROATA = new Font(CROACIA, 12);
It works fine and my "Đ" problem is solved, the thing is that I cant set it to be bold
I tried to make a different font with "BOLD" setting like this_
Font CROATABOLD = new Font(CROACIA, 12, BOLD);
The code does not seems erroneous but when I apply it to a paragraph it just not work, the font seems as normal as usual
FreeSans and FreeSansBold are different fonts of the same family. You provide a path to the FreeSans.ttf font program and as a result iText can use the regular font in the FreeSans family. If you want to use the bold font, you need to provide a path to FreeSansBold.ttf which is a different font program for a font in the same family.
This is shown in the FreeSansBold example:
public static final String FONT = "resources/fonts/FreeSans.ttf";
public static final String FONTBOLD = "resources/fonts/FreeSansBold.ttf";
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bf, 12);
Paragraph p = new Paragraph("FreeSans regular: \u0110", font);
document.add(p);
BaseFont bfbold = BaseFont.createFont(FONTBOLD, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font bold = new Font(bfbold, 12);
p = new Paragraph("FreeSans bold: \u0110", bold);
document.add(p);
document.close();
}
We have two different fonts FreeSans.ttf and FreeSansBold.ttf of the same family. One is the regular font; the other one is the bold font. If you look at the document properties of the result, free_sans_bold.pdf, you clearly see that two different fonts are at play:
I'm using PDFBox to fill form fields in a pdf using below code:
PDField nameField = form.getField("name");
if(null != nameField){
nameField.setValue(data.get("name")); // data is a hashmap
nameField.setReadonly(true);
}
The problem is, if the text is long it doesn't split to multiple lines, even though I have enabled the "multi-line" option for the field in the pdf. Do I have to do anything from the code as well to enable this?
Thanks.
Remember
Setting the ressources for the fonts to be used into the TextField.
Associating the ressources with the PDAccroform of the PDDocument.
Getting a widget for the PDTextField.
Getting a rectangle for the Widget.
Setting the width and the height of the rectangle of the widget.
It would solve it. In my case, I have a height of 20 for a non multiline text and another of 80 for a multiline textfield.You can see them being the last argument of the PDRectangle constructor. The PDRectangle class is used to specify the position and the dimension of the widget that sets it's rectangle to it. The texfield widget will appear as specified by the PDRectangle.
public static PDTextField addTextField(PDDocument pdDoc,PDAcroForm pda,String value,
String default_value,Boolean multiline,float txtfieldsyposition,float pagesheight)
{
int page = (int) (txtfieldsyposition/pagesheight);
if(page+1> pdDoc.getNumberOfPages())
{
ensurePageCapacity(pdDoc,page+1);//add 1 page to doc if needed
}
PDTextField pdtff = new PDTextField(pda);
PDFont font = new PDType1Font(FontName.TIMES_ROMAN);
String appearance = "/TIMES 10 Tf 0 0 0 rg";
try
{
PDFont font_ = new PDType1Font(FontName.HELVETICA);
PDResources resources = new PDResources();
resources.put(COSName.getPDFName("Helv"), font_);
resources.put(COSName.getPDFName("TIMES"), font);
pda.setDefaultResources(resources);
org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget widget = pdtff.getWidgets().get(0);
PDRectangle rect = null;
if(!multiline)
rect = new PDRectangle(80, (pagesheight - (txtfieldsyposition % pagesheight)), 450, 20);
else
rect = new PDRectangle(80,(pagesheight-(txtfieldsyposition%pagesheight)),450,80);
PDPage pd_page = pdDoc.getPage(page);
System.out.println(pd_page.getBBox().getHeight());
widget.setRectangle(rect);
widget.setPage(pd_page);
PDAppearanceCharacteristicsDictionary fieldAppearance = new PDAppearanceCharacteristicsDictionary(new COSDictionary());
fieldAppearance.setBorderColour(new PDColor(new float[]{0,0,0}, PDDeviceRGB.INSTANCE));
fieldAppearance.setBackground(new PDColor(new float[]{255,255,255}, PDDeviceRGB.INSTANCE));
widget.setAppearanceCharacteristics(fieldAppearance);
widget.setPrinted(true);
pd_page.getAnnotations().add(widget);
System.out.println("before appearance " +pdtff.getDefaultAppearance());
pdtff.setDefaultAppearance(appearance);
System.out.println("after appearance "+pdtff.getDefaultAppearance());
if(multiline)
{
pdtff.setMultiline(true);
}
pdtff.setDefaultValue("");
pdtff.setValue(value.replaceAll("\u202F"," "));
pdtff.setPartialName( page +""+(int)txtfieldsyposition);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
return pdtff;
}
I am using custom font in my Swing application and loading it like this:
URL l = ClassLoader.class.getResource("/resource/template/CarroisGothic-Regular.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, l.openStream());
font = font.deriveFont(Font.PLAIN, 13);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
UIManager.put("ComboBox.font", font);
It works fine on systems set where regional settings/language is set to English but for other languages (Russian for example) it displays garbage characters.
This is how I am displaying name of months:
String[] months = new DateFormatSymbols().getMonths();
for (int i = 0; i < months.length && i < 12; i++) {
cmbMonth.addItem(months[i]);
if(i == Calendar.getInstance().get(Calendar.MONTH) + 1) {
cmbMonth.setSelectedIndex(i);
}
}
The font names defined in your markup would be directing the system's cache of fonts and not your application's,this is the reason why your application isn't displaying the desired font(s).
you should try and install the font locally on the machine and use it in your application.
I want to write text on the screen for my game, for things like fps, random text for items and stuff. How can I write that text?
Is it possible without the Basic Game class? Isn't there a command like this g.drawString("Hello World", 100, 100);?
Update: this answer is now outdated, and does not work at all with the latest versions of LWJGL. Until I update this answer fully, I recommend that you look here: https://jvm-gaming.org/t/lwjgl-stb-bindings/54537
You could use the TrueType fonts feature in the Slick-util library.
Using a common font is easy, just create the font like this:
TrueTypeFont font;
Font awtFont = new Font("Times New Roman", Font.BOLD, 24); //name, style (PLAIN, BOLD, or ITALIC), size
font = new TrueTypeFont(awtFont, false); //base Font, anti-aliasing true/false
If you want to load the font from a .ttf file, it's a little more tricky:
try {
InputStream inputStream = ResourceLoader.getResourceAsStream("myfont.ttf");
Font awtFont = Font.createFont(Font.TRUETYPE_FONT, inputStream);
awtFont = awtFont.deriveFont(24f); // set font size
font = new TrueTypeFont(awtFont, false);
} catch (Exception e) {
e.printStackTrace();
}
After you have successfully created a TrueTypeFont, you can draw it like this:
font.drawString(100, 50, "ABC123", Color.yellow); //x, y, string to draw, color
For more information, you can look at the documentation for TrueTypeFont and java.awt.Font, and the Slick-Util tutorial I got most of this code from.
Try Making a BufferedImage of required size. Then get its Graphics and draw a String. Then Convert it to a ByteBuffer and render it in OpenGL.
String text = "ABCD";
int s = 256; //Take whatever size suits you.
BufferedImage b = new BufferedImage(s, s, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = b.createGraphics();
g.drawString(text, 0, 0);
int co = b.getColorModel().getNumComponents();
byte[] data = new byte[co * s * s];
b.getRaster().getDataElements(0, 0, s, s, data);
ByteBuffer pixels = BufferUtils.createByteBuffer(data.length);
pixels.put(data);
pixels.rewind();
Now pixels contains the required Image data you need to draw.
Use GL11.glTexImage2D() function to draw the byte buffer.