[ 
https://issues.apache.org/jira/browse/PDFBOX-4183?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16428129#comment-16428129
 ] 

vikas kumar jagga commented on PDFBOX-4183:
-------------------------------------------

Elaborating the problem further with code.

 

Currently if we have to print  a text containing mixture of japanese and thai 
fonts eg. {color:#d04437}"平仮名, ひらがな "   {color:#333333}first of all{color} 
{color:#333333}we will load the ttf  font files of both these languages . Below 
is the example.{color}{color}

 

{color:#14892c} url = 
this.getClass().getResource("/fonts/NotoSansCJK-Regular.ttf");{color}
{color:#14892c} loc = url.getPath();{color}
 
{color:#14892c} PDType0Font fontCjk = PDType0Font.load(document, new 
File(loc));  // fontCJK holds japanese fonts{color}
 
{color:#14892c} url = 
this.getClass().getResource("/fonts/Prompt-Regular.ttf");{color}
{color:#14892c} loc = url.getPath();{color}
 
{color:#14892c} PDType0Font fontThai = PDType0Font.load(document, new 
File(loc)); // fontThai holds Thai fonts{color}

 

Now coming to the printing part. To print thai characters we will first set the 
PDType0Font object of Thai ttf file and then to print japanese we have to 
switch the font and call the setFont with PDType0Font  object of japanese ttf 
file. Below is the code in green.


 {color:#d04437}//Setting the Thai font to the Content stream{color}
 {color:#d04437}contents.setFont(fontThai);{color}

{color:#d04437}contents.showText(พยัญชนะ );{color}

 

{color:#d04437}//Setting the Japanese font to the Content stream{color}

{color:#d04437}contents.setFont(fontCjk);{color}
{color:#d04437} contents.showText(平仮名, ひらがな);{color}
 
 What is expected is that these multiple font Objects can be combined into 
single PDType0Font object to avoid language detection. Below is the sample code 
that is expected in place of the code above in red.

{color:#14892c}PDType0Font multiLangFonts;{color}
 
{color:#14892c} multiLangFonts.add  (fontCjk );  // adding japanese fonts{color}
{color:#14892c} multiLangFonts.add ( fontThai ); // adding thai fonts{color}
 
{color:#14892c} PDPageContentStream contents;{color}
 
{color:#14892c} contents.setFont ( multiLangFonts );{color}
 
{color:#14892c} contents.showText ( พยัญชนะ 平仮名, ひらがな );  // both Japanese and 
Thai language fonts got printed at a time without switching the fonts{color}

> To support multi-lingual paragraphs without switching Font objects
> ------------------------------------------------------------------
>
>                 Key: PDFBOX-4183
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-4183
>             Project: PDFBox
>          Issue Type: New Feature
>          Components: FontBox
>            Reporter: vikas kumar jagga
>            Priority: Major
>
> Problem :- If a single paragraph has fonts of different languages we need to 
> detect the language and load the appropriate PDType0Font font file object 
> (holding ttf file) supporting that language fonts.
> PDFbox library should provide a class with addFont() method that can hold 
> multiple PDType0Font objects (holding different ttf files) so that this 
> object holding multiple font files could be loaded in document object. It 
> will reduce the overhead to detect the language and then loading that ttf 
> file object.
> iText provide this feature with FontSelector class. Below is the sample code 
> usage.
> {color:#14892c}import java.awt.Color;{color}
> {color:#14892c}import org.apache.log4j.Logger;{color}
> {color:#14892c}import com.lowagie.text.Font;{color}
> {color:#14892c}import com.lowagie.text.pdf.BaseFont;{color}
> {color:#14892c}import com.lowagie.text.pdf.FontSelector;{color}
> {color:#14892c}public class Fonts {{color}
> {color:#14892c}public static final int STYLE_BOLD = Font.BOLD;{color}
> {color:#14892c} public static final int STYLE_BOLDITALIC = 
> Font.BOLDITALIC;{color}
> {color:#14892c} public static final int STYLE_ITALIC = Font.ITALIC;{color}
> {color:#14892c} public static final int STYLE_NORMAL = Font.NORMAL;{color}
> {color:#14892c} public static final int STYLE_STRIKETHROUGH = 
> Font.STRIKETHRU;{color}
> {color:#14892c} public static final int STYLE_UNDERLINE = 
> Font.UNDERLINE;{color}
> {color:#14892c} public static final BaseFont FONT_NOTO_SANS;{color}
> {color:#14892c} public static final BaseFont FONT_NOTO_SANS_AR; // 
> Arabic{color}
> {color:#14892c} public static final BaseFont FONT_NOTO_SANS_TH; // Thai{color}
> {color:#14892c} public static final BaseFont FONT_NOTO_SANS_CJK; // Chinese, 
> Japanese and Korean{color}
> {color:#14892c}static {{color}
> {color:#14892c}try {{color}
> {color:#14892c} tmpNOTOSANS = 
> BaseFont.createFont("/fonts/NotoSans-Regular.ttf", BaseFont.IDENTITY_H, 
> BaseFont.EMBEDDED);{color}
> {color:#14892c} }{color}
> {color:#14892c} catch (Exception ex) {{color}
>  
> {color:#14892c} }{color}
> {color:#14892c}try {{color}
> {color:#14892c} tmpNOTOSANS_AR = 
> BaseFont.createFont("/fonts/NotoKufiArabic-Regular.ttf", BaseFont.IDENTITY_H, 
> BaseFont.EMBEDDED);{color}
> {color:#14892c} }{color}
> {color:#14892c} catch (Exception ex) {{color}
>  
> {color:#14892c} }{color}
> {color:#14892c}try {{color}
> {color:#14892c} tmpNOTOSANS_TH = 
> BaseFont.createFont("/fonts/NotoSansThai-Regular.ttf", BaseFont.IDENTITY_H, 
> BaseFont.EMBEDDED);{color}
> {color:#14892c} }{color}
> {color:#14892c} catch (Exception ex) {{color}
>  
> {color:#14892c} }{color}
> {color:#14892c}try {{color}
> {color:#14892c} tmpNOTOSANS_CJK = 
> BaseFont.createFont("/fonts/NotoSansCJK-Regular.ttf", BaseFont.IDENTITY_H, 
> BaseFont.NOT_EMBEDDED);{color}
> {color:#14892c} }{color}
> {color:#14892c} catch (Exception ex) {{color}
>  
> {color:#14892c} }{color}
>  
> {color:#14892c} FONT_NOTO_SANS = tmpNOTOSANS;{color}
> {color:#14892c} FONT_NOTO_SANS_AR = tmpNOTOSANS_AR;{color}
> {color:#14892c} FONT_NOTO_SANS_TH = tmpNOTOSANS_TH;{color}
> {color:#14892c} FONT_NOTO_SANS_CJK = tmpNOTOSANS_CJK;{color}
> {color:#14892c} }{color}
> {color:#14892c}public static FontSelector returnFontSelector(float size, int 
> style) {{color}
> {color:#14892c} return returnFontSelector(size, style, null);{color}
> {color:#14892c} }{color}
> {color:#14892c}public static FontSelector returnFontSelector(float size, int 
> style, Color color) {{color}
> {color:#14892c} FontSelector multiFonts = new FontSelector();{color}
>  
> {color:#14892c} multiFonts.addFont(new Font(Fonts.FONT_NOTO_SANS, size, 
> style, color));{color}
> {color:#14892c} multiFonts.addFont(new Font(Fonts.FONT_NOTO_SANS_AR, size, 
> style, color));{color}
> {color:#14892c} multiFonts.addFont(new Font(Fonts.FONT_NOTO_SANS_TH, size, 
> style, color));{color}
> {color:#14892c} multiFonts.addFont(new Font(Fonts.FONT_NOTO_SANS_CJK, size, 
> style, color));{color}
>  
> {color:#14892c} return multiFonts;{color}
> {color:#14892c} }{color}
> {color:#14892c}}{color}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: dev-h...@pdfbox.apache.org

Reply via email to