[
https://issues.apache.org/jira/browse/PDFBOX-4379?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16690795#comment-16690795
]
Darren Croft commented on PDFBOX-4379:
--------------------------------------
Thanks for looking at this. It is still confusing to me. In a few simple tests,
hasGlyph did return true for ASCII characters that are in a font and false when
an emoji is not. It just doesn't tell me when an emoji is in the font.
Here is an expanded test that shows what I mean. hasGlyph appears to do what I
want for normal characters but from what you're saying, I shouldn't depend on
it....
Thanks!
{code:java}
import java.io.IOException;
import java.io.InputStream;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
/**
*
* @author darren
*/
public class FontTest {
public static void main(String[] args) throws IOException {
try {
PDDocument doc = new PDDocument();
PDPage page = new PDPage(PDRectangle.LETTER);
doc.addPage(page);
InputStream in = FontTest.class.getResourceAsStream("/Symbola_hint.ttf");
PDType0Font emojiFont = PDType0Font.load(doc, in);
in = FontTest.class.getResourceAsStream("/Quicksand-Bold.ttf");
PDType0Font font = PDType0Font.load(doc, in);
//create a test string that has normal characters and an emoji
int emojiCodepoint=0x1F609;
String testString="Start " + new String(Character.toChars(emojiCodepoint)) +
" End";
PDPageContentStream stream = new PDPageContentStream(doc, page);
stream.beginText();
stream.setLeading(12 * 1.2f);
//Loop through each character. Use normal font if it contains glyph
otherwise use symbol font
final int length = testString.length();
for (int offset = 0; offset < length;) {
//check if glyph is in main font
final int codepoint = testString.codePointAt(offset);
if (font.hasGlyph(codepoint)) {
stream.setFont(font, 12);
} else {
//check if glyph is in the emojifont
if (emojiFont.hasGlyph(codepoint)) {
System.out.println("has Glyph");
} else {
System.out.println("no Glyph");
}
stream.setFont(emojiFont, 12);
}
stream.showText(new String(Character.toChars(codepoint)));
offset += Character.charCount(codepoint);
}
stream.endText();
stream.close();
doc.save("emojiTest.pdf");
} catch (Exception exception) {
System.out.println(exception.getMessage());
exception.printStackTrace();
}
}
}
{code}
> hasGlyph returns false when a two character glyph does exist
> ------------------------------------------------------------
>
> Key: PDFBOX-4379
> URL: https://issues.apache.org/jira/browse/PDFBOX-4379
> Project: PDFBox
> Issue Type: Bug
> Components: FontBox
> Affects Versions: 2.0.12
> Reporter: Darren Croft
> Priority: Major
> Attachments: Symbola_hint.ttf
>
>
> The following codes runs without exception and adds the glyph to the page,
> but it prints "no glyph" to standard output. It should print "has glyph" on
> standard output.
> {code:java}
> public class FontTest {
> public static void main(String[] args) throws IOException {
> try {
> PDDocument doc = new PDDocument();
> PDPage page = new PDPage(PDRectangle.LETTER);
> doc.addPage(page);
> InputStream in =
> FontTest.class.getResourceAsStream("/Symbola_hint.ttf");
> PDType0Font emojiFont = PDType0Font.load(doc, in);
> PDPageContentStream stream = new PDPageContentStream(doc, page);
> stream.beginText();
> stream.setFont(emojiFont, 12);
> stream.setLeading(12 * 1.2f);
> int codepoint=0x1F609;
> if(emojiFont.hasGlyph(codepoint)) {
> System.out.println("has Glyph");
> } else {
> System.out.println("no Glyph");
> }
> String s=new String(Character.toChars(codepoint));
> stream.showText(s);
> stream.close();
> doc.save("emojiTest.pdf");
> } catch (Exception exception) {
> System.out.println(exception.getMessage());
> exception.printStackTrace();
> }
> }
> }
> {code}
>
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]