Skip to content

Commit

Permalink
Add flag to inhibit FOP glyph substitution (#904)
Browse files Browse the repository at this point in the history
* add flag to inhibit FOP glyph substitution

* add Javadoc parameter description

* make text rendering options object mutable

as requested at #904 (review)

* transfer text rendering options to PdfDocument
  • Loading branch information
dadza committed Nov 1, 2023
1 parent 80f6935 commit 1d970b9
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 13 deletions.
55 changes: 49 additions & 6 deletions openpdf/src/main/java/com/lowagie/text/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ private static String getVersionNumber() {
protected int chapternumber = 0;

/**
* The default language of the document. Can be set to values like "en_US".
* This language is used in {@link FopGlyphProcessor} to determine which glyphs are to be substituted.
* The default "dflt" means that all glyphs which can be replaced will be substituted.
* Text rendering options, including the default language of the document and a flag
* to enable font glyph substitution (if FOP is available)
*
* @since 3.1.15
*/
String documentLanguage = "dflt";
TextRenderingOptions textRenderingOptions = new TextRenderingOptions();

// constructor

Expand Down Expand Up @@ -958,7 +958,7 @@ public boolean isMarginMirroring() {
* @param documentLanguage the wanted language
*/
public void setDocumentLanguage(String documentLanguage) {
this.documentLanguage = documentLanguage;
textRenderingOptions.setDocumentLanguage(documentLanguage);
}

/**
Expand All @@ -970,6 +970,49 @@ public void setDocumentLanguage(String documentLanguage) {
* @return the current document language
*/
public String getDocumentLanguage() {
return documentLanguage;
return textRenderingOptions.getDocumentLanguage();
}

/**
* Set a flag that determine whether glyph substion is enabled when FOP is available.
*
* @param glyphSubstitutionEnabled the glyph substitution enabled flag
* @see FopGlyphProcessor
* @see #setDocumentLanguage(String)
*/
public void setGlyphSubstitutionEnabled(boolean glyphSubstitutionEnabled) {
textRenderingOptions.setGlyphSubstitutionEnabled(glyphSubstitutionEnabled);
}

/**
* Returns the glyph substitution enabled flag.
*
* @return the glyph substitution enabled flag
* @see #setGlyphSubstitutionEnabled(boolean)
*/
public boolean isGlyphSubstitutionEnabled() {
return textRenderingOptions.isGlyphSubstitutionEnabled();
}

/**
* Sets the text rendering options.
*
* @param textRenderingOptions the text rendering options
* @see #setDocumentLanguage(String)
* @see Document#setGlyphSubstitutionsEnabled(boolean)
*/
public void setTextRenderingOptions(TextRenderingOptions textRenderingOptions) {
this.textRenderingOptions = textRenderingOptions == null ? new TextRenderingOptions() : textRenderingOptions;
}

/**
* Gets the text rendering options.
*
* @return the text rendering options
* @see #getDocumentLanguage()
* @see #isGlyphSubstitutionEnabled()
*/
public TextRenderingOptions getTextRenderingOptions() {
return textRenderingOptions;
}
}
86 changes: 86 additions & 0 deletions openpdf/src/main/java/com/lowagie/text/TextRenderingOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.lowagie.text;

import com.lowagie.text.pdf.FopGlyphProcessor;

/**
* Text rendering options, including the default language of the document and a flag
* to enable font glyph substitution (if FOP is available).
*
* @author Lucian Chirita (lucianc@users.sourceforge.net)
* @see Document#setTextRenderingOptions(TextRenderingOptions)
* @since 3.1.15
*/
public class TextRenderingOptions {

public static final String DOCUMENT_LANGUAGE_DEFAULT = "dflt";

/**
* The default language of the document. Can be set to values like "en_US".
* This language is used in {@link FopGlyphProcessor} to determine which glyphs are to be substituted.
* The default "dflt" means that all glyphs which can be replaced will be substituted.
*
*/
private String documentLanguage;

private boolean glyphSubstitutionEnabled;

/**
* Creates a text rendering options instance with the default options: glyph substitution enabled
* and "dflt" as document language.
*/
public TextRenderingOptions() {
this(DOCUMENT_LANGUAGE_DEFAULT, true);
}

/**
* Creates a text rendering options instance.
*
* @param documentLanguage the wanted language
* @param glyphSubstitutionEnabled whether glyph substitution is enabled
*/
public TextRenderingOptions(String documentLanguage, boolean glyphSubstitutionEnabled) {
this.documentLanguage = documentLanguage;
this.glyphSubstitutionEnabled = glyphSubstitutionEnabled;
}

/**
* Sets the default language of the document.
*
* @param documentLanguage the document language
* @see #getDocumentLanguage()
*/
public void setDocumentLanguage(String documentLanguage) {
this.documentLanguage = documentLanguage;
}

/**
* The default language of the document. Can be set to values like "en_US". This language is used in
* FopGlyphProcessor to determine which glyphs are to be substituted.
* <P/>
* The default "dflt" means that all glyphs which can be replaced will be substituted.
*
* @return the current document language
*/
public String getDocumentLanguage() {
return documentLanguage;
}

/**
* Sets the font glyph substitution enabled flag.
*
* @param glyphSubstitutionEnabled whether glyph substitution is enabled
*/
public void setGlyphSubstitutionEnabled(boolean glyphSubstitutionEnabled) {
this.glyphSubstitutionEnabled = glyphSubstitutionEnabled;
}

/**
* Returns the glyph substitution enabled flag.
*
* @return the glyph substitution enabled flag
* #see {@link Document#setGlyphSubstitutionEnabled(boolean)}
*/
public boolean isGlyphSubstitutionEnabled() {
return glyphSubstitutionEnabled;
}
}
8 changes: 5 additions & 3 deletions openpdf/src/main/java/com/lowagie/text/pdf/FontDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

import com.lowagie.text.Utilities;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.TextRenderingOptions;

/**
* Each font in the document will have an instance of this class
Expand Down Expand Up @@ -172,7 +173,7 @@ BaseFont getBaseFont() {
* @param text the text to convert
* @return the conversion
*/
byte[] convertToBytes(String text, String language) {
byte[] convertToBytes(String text, TextRenderingOptions options) {
byte[] b = null;
switch (fontType) {
case BaseFont.FONT_TYPE_T3:
Expand Down Expand Up @@ -217,9 +218,10 @@ byte[] convertToBytes(String text, String language) {
}
else {
String fileName = ((TrueTypeFontUnicode)getBaseFont()).fileName;
if (FopGlyphProcessor.isFopSupported() && (fileName!=null && fileName.length()>0
if (options.isGlyphSubstitutionEnabled() && FopGlyphProcessor.isFopSupported()
&& (fileName!=null && fileName.length()>0
&&( fileName.contains(".ttf") || fileName.contains(".TTF")))){
return FopGlyphProcessor.convertToBytesWithGlyphs(ttu,text,fileName,longTag,language);
return FopGlyphProcessor.convertToBytesWithGlyphs(ttu,text,fileName,longTag, options.getDocumentLanguage());
}else {
return convertToBytesWithGlyphs(text);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,7 @@ private void showText2(String text) {
if (state.fontDetails == null) {
throw new NullPointerException(MessageLocalization.getComposedMessage("font.and.size.must.be.set.before.writing.any.text"));
}
byte[] b = state.fontDetails.convertToBytes(text, getPdfDocument().getDocumentLanguage());
byte[] b = state.fontDetails.convertToBytes(text, getPdfDocument().getTextRenderingOptions());
escapeString(b, content);
}

Expand Down
2 changes: 1 addition & 1 deletion openpdf/src/main/java/com/lowagie/text/pdf/PdfCopy.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public String toString() {
*/
public PdfCopy(Document document, OutputStream os) throws DocumentException {
super(new PdfDocument(), os);
this.document.setDocumentLanguage(document.getDocumentLanguage());
this.document.setTextRenderingOptions(document.getTextRenderingOptions());
document.addDocListener(pdf);
pdf.addWriter(this);
indirectMap = new HashMap<>();
Expand Down
4 changes: 2 additions & 2 deletions openpdf/src/main/java/com/lowagie/text/pdf/PdfWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ protected PdfWriter(PdfDocument document, OutputStream os) {
public static PdfWriter getInstance(Document document, OutputStream os)
throws DocumentException {
PdfDocument pdf = new PdfDocument();
pdf.setDocumentLanguage(document.getDocumentLanguage());
pdf.setTextRenderingOptions(document.getTextRenderingOptions());
document.addDocListener(pdf);
PdfWriter writer = new PdfWriter(pdf, os);
pdf.addWriter(writer);
Expand All @@ -624,7 +624,7 @@ public static PdfWriter getInstance(Document document, OutputStream os)
public static PdfWriter getInstance(Document document, OutputStream os, DocListener listener)
throws DocumentException {
PdfDocument pdf = new PdfDocument();
pdf.setDocumentLanguage(document.getDocumentLanguage());
pdf.setTextRenderingOptions(document.getTextRenderingOptions());
pdf.addDocListener(listener);
document.addDocListener(pdf);
PdfWriter writer = new PdfWriter(pdf, os);
Expand Down

0 comments on commit 1d970b9

Please sign in to comment.