Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,23 @@
*/
public class GlyphLayoutFontLoaderAwt
{
protected final GlyphLayoutProcessorAwt glyphLayoutProcessor;

/**
* Mapping from PDFBox font to AWT font
*/
private final Map<PDType0Font, Font> awtFontMap = new ConcurrentHashMap<>();

public GlyphLayoutFontLoaderAwt(GlyphLayoutProcessorAwt glyphLayoutProcessor) {
this.glyphLayoutProcessor = glyphLayoutProcessor;
}

/**
* Loads the AWT font needed for layout
*
* @param pdDocument document
* @param inputStream of the font
* @return pdType0Font PDFBox font
* @return PDType0Font PDFBox font
* @throws IOException if font can not be loaded
* @throws FontFormatException if the font is bad
*/
Expand All @@ -70,7 +75,7 @@ public PDType0Font loadFont(PDDocument pdDocument, InputStream inputStream)
* @param inputStream of the font
* @param embedSubset True if the font will be subset before embedding. Set this to false when
* creating a font for AcroForm.
* @return pdType0Font PDFBox font
* @return PDType0Font PDFBox font
* @throws IOException if font can not be loaded
* @throws FontFormatException if the font is bad
*/
Expand All @@ -86,7 +91,7 @@ public PDType0Font loadFont(PDDocument pdDocument, InputStream inputStream, bool
* @param pdDocument document
* @param inputStream of the font
* @param fontOptions options for font
* @return pdType0Font PDFBox font
* @return PDType0Font PDFBox font
* @throws IOException if font can not be loaded
* @throws FontFormatException if the font is bad
*/
Expand All @@ -104,7 +109,7 @@ public PDType0Font loadFont(PDDocument pdDocument, InputStream inputStream, Font
* @param embedSubset True if the font will be subset before embedding. Set this to false when
* creating a font for AcroForm.
* @param fontOptions Options for font
* @return pdType0Font PDFBox font
* @return PDType0Font PDFBox font
* @throws IOException if font can not be loaded
* @throws FontFormatException if the font is bad
*/
Expand All @@ -117,6 +122,7 @@ public PDType0Font loadFont(PDDocument pdDocument, InputStream inputStream, bool
try (ByteArrayInputStream bais = new ByteArrayInputStream(inputStream.readAllBytes()))
{
PDType0Font pdType0Font = PDType0Font.load(pdDocument, bais, embedSubset);
pdType0Font.setGlyphLayoutProcessor(glyphLayoutProcessor);
bais.reset();
loadAwtFont(pdType0Font, bais, fontOptions);
return pdType0Font;
Expand Down Expand Up @@ -155,7 +161,8 @@ protected void loadAwtFont(PDType0Font pdType0Font, InputStream inputStream, Fon
*/
public boolean supportsFont(PDFont font)
{
return awtFontMap.containsKey(font);
return (font instanceof PDType0Font)
&& awtFontMap.containsKey((PDType0Font)font);
}

/**
Expand All @@ -164,7 +171,7 @@ public boolean supportsFont(PDFont font)
* @param font PDFBox font
* @return AWT font if available
*/
public Font getAwtFont(PDType0Font font)
protected Font getAwtFont(PDType0Font font)
{
return awtFontMap.get(font);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
*/
public class GlyphLayoutProcessorAwt extends AbstractGlyphLayoutProcessor implements GlyphLayoutProcessorInterface
{

private final GlyphLayoutFontLoaderAwt glyphLayoutFontLoaderAwt;

/**
Expand All @@ -56,7 +55,7 @@ public class GlyphLayoutProcessorAwt extends AbstractGlyphLayoutProcessor implem
*/
public GlyphLayoutProcessorAwt()
{
this.glyphLayoutFontLoaderAwt = new GlyphLayoutFontLoaderAwt();
this.glyphLayoutFontLoaderAwt = new GlyphLayoutFontLoaderAwt(this);
}

/**
Expand Down Expand Up @@ -211,6 +210,7 @@ protected float getStringWidthUni(PDType0Font font, float fontSize, String text,
return (float) rect.getWidth();
}


/**
* Shows a text using glyph positioning (if needed) This text must have a uniform run direction.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
/**
* Examples for ligatures and kerning
* See <a href="https://issues.apache.org/jira/browse/PDFBOX-4951">PDFBOX-4951</a>
*
* <p>
* The default processing of GlyphLayoutProcessor is with ligatures and kerning disabled.
* You can enable ligatures and kerning using FontOptions, see below.
*
Expand All @@ -52,8 +52,8 @@ class GlyphLayoutLigaturesAndKerningTest extends TestBase
/**
* Check that missing glyph is caught like in main pdfbox.
*
* @throws IOException
* @throws FontFormatException
* @throws IOException if an I/O error occurs
* @throws FontFormatException if the font contains errors
*/
@Test
void testMissingGlyph() throws IOException, FontFormatException
Expand Down Expand Up @@ -152,6 +152,11 @@ void testLigaturesAndKerning() throws IOException, FontFormatException, URISynta
float f3 = glyphLayoutProcessor.getStringWidth(dejavuFont, fontSize, DEJAVU_STRING);
float f4 = glyphLayoutProcessor.getStringWidth(dejavuLigKernFont, fontSize, DEJAVU_STRING);

float f5 = glyphLayoutProcessor.getStringWidth(dejavuLigKernFont, fontSize, DEJAVU_STRING+" ");
float f6 = glyphLayoutProcessor.getStringWidth(dejavuLigKernFont, fontSize, DEJAVU_STRING);
System.out.printf("mit leerz %f ohne %f%n", f5, f6);


// equality is expected here, but this shows that the ordinary getStringWidth() isn't helpful
assertEquals(f1, f2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.fop.fonts.FontUris;
import org.apache.fop.fonts.MultiByteFont;

import org.apache.pdfbox.pdmodel.GlyphLayoutProcessorInterface;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
Expand All @@ -50,18 +51,28 @@
*/
public class GlyphLayoutFontLoaderFop
{
private final GlyphLayoutProcessorInterface glyphLayoutProcessor;

/**
* Mapping from PDFBox font to AWT font
*/
private final Map<PDType0Font, MultiByteFont> fopFontMap = new ConcurrentHashMap<>();

/**
* Creates a GlyphLayoutFontLoaderFop
* @param glyphLayoutProcessor the corresponding glyph layout processor
*/
public GlyphLayoutFontLoaderFop(GlyphLayoutProcessorInterface glyphLayoutProcessor) {
this.glyphLayoutProcessor = glyphLayoutProcessor;
}

/**
* Loads the AWT font needed for layout
*
* @param pdDocument document
* @param inputStream of the font
* @param embedSubset
* @param embedSubset True if the font will be subset before embedding. Set this to false when
* creating a font for AcroForm.
* @return pdType0Font PDFBox font
* @throws IOException if font can not be loaded
*/
Expand All @@ -75,6 +86,7 @@ public PDType0Font loadFont(PDDocument pdDocument, InputStream inputStream, bool
try (ByteArrayInputStream bais = new ByteArrayInputStream(inputStream.readAllBytes()))
{
pdType0Font = PDType0Font.load(pdDocument, bais, embedSubset);
pdType0Font.setGlyphLayoutProcessor(glyphLayoutProcessor);
bais.reset();
loadFopFont(pdType0Font, bais);
}
Expand Down Expand Up @@ -143,15 +155,15 @@ public boolean supportsFont(PDFont font)
* @param font PDFBox font
* @return fop font if available
*/
public MultiByteFont getFopFont(PDType0Font font)
protected MultiByteFont getFopFont(PDType0Font font)
{
return fopFontMap.get(font);
}

/**
* Resolver needed to construct a FOP MultibyteFont
*/
static class FopInputStreamResourceResolver implements ResourceResolver
protected static class FopInputStreamResourceResolver implements ResourceResolver
{
private final InputStream inputStream;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class GlyphLayoutProcessorFop extends AbstractGlyphLayoutProcessor implem
*/
public GlyphLayoutProcessorFop()
{
this.glyphLayoutFontLoaderFop = new GlyphLayoutFontLoaderFop();
this.glyphLayoutFontLoaderFop = new GlyphLayoutFontLoaderFop(this);
}

/**
Expand Down Expand Up @@ -100,6 +100,7 @@ public boolean supportsFont(PDFont font)
return glyphLayoutFontLoaderFop.supportsFont(font);
}


/**
* Loads the font needed for layout
*
Expand Down Expand Up @@ -139,7 +140,7 @@ protected float getStringWidthUni(PDType0Font font, float fontSize, String text,
{
TextAndGpa textAndGpa = computeGlyphsAndPositions(font, fontSize, text, bidiLevel);
// PDType0Font.getStringWidth returns glyph widths in 1000-units. Convert to user space using font matrix and fontSize
float raw = font.getStringWidth(textAndGpa.getText());
float raw = font.getStringWidthBasic(textAndGpa.getText());
float scaleX = font.getFontMatrix().getScaleX();
return raw * scaleX * fontSize;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,14 @@ protected abstract float getStringWidthUni(PDType0Font font, float fontSize, Str
* @param fontSize font size
* @param text text
* @return string width
* @throws IllegalArgumentException if the font is not supported or glyphs are missing
*/
public float getStringWidth(PDType0Font font, float fontSize, String text) throws IOException
{
if (!supportsFont(font))
{
throw new IllegalArgumentException("font must be supported by the GlyphLayoutProcessor");
}
float width = 0f;
List<TextAndBidiLevel> textAndBidiLevels = doBidiSplittingAndReordering(text);
for (TextAndBidiLevel textAndBidiLevel: textAndBidiLevels)
Expand Down Expand Up @@ -112,11 +117,15 @@ protected abstract void showTextUni(ContentStreamForGlyphLayoutInterface content
* @param text text to show
*
* @throws IOException if an I/O exception occurs
* @throws IllegalArgumentException if glyphs are missing
* @throws IllegalArgumentException if the font is not supported or glyphs are missing
*/
public void showText(ContentStreamForGlyphLayoutInterface contentStream, PDType0Font font, float fontSize, String text)
throws IOException
{
if (!supportsFont(font))
{
throw new IllegalArgumentException("font must be supported by the GlyphLayoutProcessor");
}
List<TextAndBidiLevel> textAndBidiLevels = doBidiSplittingAndReordering(text);
for (TextAndBidiLevel textAndBidiLevel: textAndBidiLevels)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,22 @@ public void showText(String text) throws IOException
}
else
{
showTextInternal(text);
writeBytes(ASCII_SPACE);
writeOperator(OperatorName.SHOW_TEXT);
showTextBasic(text);
}
}

/**
* Shows the text
*
* @param text text to be shown
* @throws IOException if an I/O exception occurs
*/
protected void showTextBasic(String text) throws IOException {
showTextInternal(text);
writeBytes(ASCII_SPACE);
writeOperator(OperatorName.SHOW_TEXT);
}

/**
* Shows the glyphs for the given glyph codes - only for PDType0Font
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.InputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import org.apache.logging.log4j.Logger;
Expand All @@ -40,6 +41,7 @@
import org.apache.pdfbox.io.RandomAccessRead;
import org.apache.pdfbox.io.RandomAccessReadBuffer;
import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
import org.apache.pdfbox.pdmodel.GlyphLayoutProcessorInterface;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.ResourceCache;
import org.apache.pdfbox.util.Matrix;
Expand All @@ -64,6 +66,8 @@ public class PDType0Font extends PDFont implements PDVectorFont
private boolean isDescendantCJK;
private PDCIDFontType2Embedder embedder;
private TrueTypeFont ttf;
private GlyphLayoutProcessorInterface glyphLayoutProcessor;


/**
* Constructor for reading a Type0 font from a PDF file.
Expand Down Expand Up @@ -755,4 +759,41 @@ public CmapLookup getCmapLookup()
return cmapLookup;
}

/**
*
* @param glyphLayoutProcessor
*/
public void setGlyphLayoutProcessor(GlyphLayoutProcessorInterface glyphLayoutProcessor) {
Objects.requireNonNull(glyphLayoutProcessor, "glyphLayoutProcessor must be not null");
this.glyphLayoutProcessor = glyphLayoutProcessor;
}

/**
* Returns the width of the given Unicode string.
* If a glyph layout processor is set, it is used to compute the string width
*
* @param text The text to get the width of.
* @return The width of the string in 1/1000 units of text space.
* @throws IOException If there is an error getting the width information.
* @throws IllegalArgumentException if a character isn't supported by the font.
*/
@Override
public float getStringWidth(String text) throws IOException
{
return glyphLayoutProcessor != null && glyphLayoutProcessor.supportsFont(this)
? glyphLayoutProcessor.getStringWidth((PDType0Font) this, 1.0f, text) / getFontMatrix().getScaleX()
: getStringWidthBasic(text);
}
/**
* Returns the width of the given Unicode string.
*
* @param text The text to get the width of.
* @return The width of the string in 1/1000 units of text space.
* @throws IOException If there is an error getting the width information.
* @throws IllegalArgumentException if a character isn't supported by the font.
*/
public float getStringWidthBasic(String text) throws IOException
{
return super.getStringWidth(text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.pdfbox.pdmodel.interactive;

import org.apache.pdfbox.pdmodel.GlyphLayoutProcessorInterface;
import org.apache.pdfbox.pdmodel.font.PDFont;

/**
Expand All @@ -38,7 +39,12 @@ public class AppearanceStyle
* Defaulting to 1.2*fontSize to match Acrobats default.
*/
private float leading = 14.4f;


/**
* Glyph layout processor
*/
private GlyphLayoutProcessorInterface glyphLayoutProcessor;

/**
* Get the font used for text formatting.
*
Expand Down Expand Up @@ -99,4 +105,5 @@ public void setLeading(float leading)
{
this.leading = leading;
}

}
Loading