Class PDFGlyphVector
- java.lang.Object
-
- org.faceless.pdf2.PDFGlyphVector
-
public final class PDFGlyphVector extends Object
A PDFGlyphVector is a sequence of glyphs in a particular font. It's a very low-level class and most likely theLayoutBox
is a better option, but if there's a requirement to get text onto the page as fast as possible without any layout then this class can be used. A quick example:PDFStyle style = ... PDFGlyphVector vector = style.createGlyphVector("Hello World", Locale.getDefault()); canvas.drawGlyphVector(vector, x, y);
-
-
Field Summary
Fields Modifier and Type Field Description static int
FLAG_BREAKPOINT
A parameter togetFlag(int)
which indicates whether there is a theoretical word breakpoint before this glyph.static int
FLAG_CJKSCRIPT
A parameter togetFlag(int)
which returns whether the glyph is in a run of Han, Hangul, Hiragana, Katakana or Bopomofo glyphs.static int
FLAG_COLR
Return true if this particular glyph is a color glyph using the Microsoft "COLR" method of creating colored glyphs - stacking outlines in different colors.static int
FLAG_EXPLICIT
A parameter to thegetFlag(int)
which returns true if the glyph used is not the same glyph that you would get by default without any form of OpenType or complex layout.static int
FLAG_LEFTJOIN
A parameter togetFlag(int)
which returns whether the glyph is cursively joined to the leftstatic int
FLAG_LIGATURE
A parameter togetFlag(int)
which returns whether the glyph is a ligature composed of 2 (or more) base characters.static int
FLAG_MATHEXTENDED
A parameter togetFlag(int)
which returns whether the glyph is a vertically "extended shape" mathematical glyph, meaning super/subscripts should be positioned relative to the glyph bounding-box, not the nominal sizestatic int
FLAG_PNG
Return true if this particular glyph is a color glyph using the Google "CBDT" method of creating colored glyphs - essentially, a PNG bitmap image.static int
FLAG_RIGHTJOIN
A parameter togetFlag(int)
which returns whether the glyph is cursively joined to the rightstatic int
FLAG_SMALLCAPS
A parameter togetFlag(int)
which returns whether the glyph was replaced with a synthesized "small-caps" version of the original.static int
FLAG_SVG
Return true if this particular glyph is a color glyph using an SVG outline.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
getActualText()
Get the "ActualText" this PDFGlyphVector represents.float
getAdjacentPosition(PDFGlyphVector gv, float y)
Given a second PDFGlyphVector and the difference in baselines between this glyph vector and the second, return the horizontal kerning between this PDFGlyphVector and the second so that the two are adjacent.double
getAdvance(int off)
Return the horizontal advance for the glyph at the specified indexint
getCID(int off)
Return the glyph id of the glyph at the specified indexint
getFirstIndex(int off)
Return the first index into the String returned fromgetText()
of the glyph at the specified offset.boolean
getFlag(int glyph, int flag)
Return whether the specified glyph has the specifiedflag
set.AffineTransform
getGlyphTransform(int off)
Return the AffineTransform previously applied to the glyph viasetGlyphTransform(int, java.awt.geom.AffineTransform)
, or null if no transform was applied.int
getLastIndex(int off)
Return the last index into the String returned fromgetText()
of the glyph at the specified offset.byte
getLevel()
Returns the Bidi level of this PDFGlyphVector, as specified in the constructorLocale
getLocale()
Return the initial Locale that was specified when the PDFGlyphVector was created.double
getLSB(int off)
Return the left-side bearing of the glyph at the specified indexint
getNumGlyphs()
Returns the number of glyphs in this PDFGlyphVectorint
getNumSpaces()
Returns the number of space characters in this PDFGlyphVector.Shape
getOutline()
Get the Outline of this PDFGlyphVector as a Shape.Shape
getOutline(int index, boolean boundsOnly)
Get the Outline of a single glyph from this PDFGlyphVector as a Shape.Point2D
getPosition(int off)
Return the start position of the glyph at the specified index, relative to the start position of the start of the text.PDFStyle
getStyle()
Returns the PDFStyle of this PDFGlyphVectorString
getText()
Return the text this PDFGlyphVector was originally created from.int
getTextLength()
Return the number of characters from the original String this PDFGlyphVector represents.int
getTextOffset()
Return the initial text offset that was specified when the PDFGlyphVector was created.float
getWidth()
Returns the width in points of this sequence.float
getWidth(int first, boolean firstStart, int last, boolean lastStart)
Return the width from the specified edges of the first glyph to the last glyph.float
getWidth(int first, int last)
Return the width from the start of the first glyph to the end of the last glyph.void
kern(int off, float kern)
Add or remove whitespace at a particular point in the PDFGlyphVector.void
restorePositions()
Restore the positions of all the glyphs in the PDFGlyphVector to that saved by an earlier call tosavePositions()
.void
savePositions()
Save the positions of all the glyphs in the PDFGlyphVector.void
setActualText(String actualtext)
Set the "ActualText" this PDFGlyphVector represents.void
setGlyphTransform(int off, AffineTransform transform)
Set an AffineTransform on an individual glyph.void
setStyle(PDFStyle style)
Override the style originally used to create this PDFGlyphVector.PDFGlyphVector[]
splitAt(int headlen, int tailstart)
Split this PDFGlyphVector at the specified point, returning an array of two new PDFGlyphVectors containing the two halves.float
stretch(float chardelta, float spacedelta)
Stretch or squash this GlyphVector by adding track kerning between each glyph.String
toString()
-
-
-
Field Detail
-
FLAG_LEFTJOIN
public static final int FLAG_LEFTJOIN
A parameter togetFlag(int)
which returns whether the glyph is cursively joined to the left- Since:
- 2.23.4
- See Also:
- Constant Field Values
-
FLAG_RIGHTJOIN
public static final int FLAG_RIGHTJOIN
A parameter togetFlag(int)
which returns whether the glyph is cursively joined to the right- Since:
- 2.23.4
- See Also:
- Constant Field Values
-
FLAG_LIGATURE
public static final int FLAG_LIGATURE
A parameter togetFlag(int)
which returns whether the glyph is a ligature composed of 2 (or more) base characters. This returns false for a base character combined with an accent- Since:
- 2.23.4
- See Also:
- Constant Field Values
-
FLAG_EXPLICIT
public static final int FLAG_EXPLICIT
A parameter to thegetFlag(int)
which returns true if the glyph used is not the same glyph that you would get by default without any form of OpenType or complex layout. This can be useful to determine if the PDFGlyphVector could be recreated without any OpenType layout, or if the Unicode value of the Glyph could be reasonably inferred from the Glyph ID.- Since:
- 2.24.1
- See Also:
- Constant Field Values
-
FLAG_MATHEXTENDED
public static final int FLAG_MATHEXTENDED
A parameter togetFlag(int)
which returns whether the glyph is a vertically "extended shape" mathematical glyph, meaning super/subscripts should be positioned relative to the glyph bounding-box, not the nominal size- Since:
- 2.24.1
- See Also:
- Constant Field Values
-
FLAG_CJKSCRIPT
public static final int FLAG_CJKSCRIPT
A parameter togetFlag(int)
which returns whether the glyph is in a run of Han, Hangul, Hiragana, Katakana or Bopomofo glyphs.- Since:
- 2.24.2
- See Also:
- Constant Field Values
-
FLAG_SMALLCAPS
public static final int FLAG_SMALLCAPS
A parameter togetFlag(int)
which returns whether the glyph was replaced with a synthesized "small-caps" version of the original.- Since:
- 2.24.2
- See Also:
- Constant Field Values
-
FLAG_BREAKPOINT
public static final int FLAG_BREAKPOINT
A parameter togetFlag(int)
which indicates whether there is a theoretical word breakpoint before this glyph. This will be false for combining marks and glyphs that combine with a previous glyph to form a syllable in scripts like Devanagai.- Since:
- 2.24.4
- See Also:
- Constant Field Values
-
FLAG_COLR
public static final int FLAG_COLR
Return true if this particular glyph is a color glyph using the Microsoft "COLR" method of creating colored glyphs - stacking outlines in different colors.- Since:
- 2.24.4
- See Also:
- Constant Field Values
-
FLAG_PNG
public static final int FLAG_PNG
Return true if this particular glyph is a color glyph using the Google "CBDT" method of creating colored glyphs - essentially, a PNG bitmap image.- Since:
- 2.24.4
- See Also:
- Constant Field Values
-
FLAG_SVG
public static final int FLAG_SVG
Return true if this particular glyph is a color glyph using an SVG outline. This is not currently supported by the PDF API.- Since:
- 2.24.4
- See Also:
- Constant Field Values
-
-
Method Detail
-
splitAt
public PDFGlyphVector[] splitAt(int headlen, int tailstart)
Split this PDFGlyphVector at the specified point, returning an array of two new PDFGlyphVectors containing the two halves.- Parameters:
headlen
- the length (the number of glyphs) of this item after the split, from1 .. getNumGlyphs()
tailstart
- the index of the first glyph of the tail, fromheadlen .. getNumGlyphs()
. If the resulting PDFGlyphVector would contain zero glyphs, no tail is created and this method returnsnull
as the second argument to the array- Since:
- 2.22
-
getLevel
public byte getLevel()
Returns the Bidi level of this PDFGlyphVector, as specified in the constructor- Since:
- 2.22
-
getText
public String getText()
Return the text this PDFGlyphVector was originally created from. If the PDFGlyphVector was created from a split, this will be the same String as the source PDFGlyphVector.- Since:
- 2.23.4
-
getTextOffset
public int getTextOffset()
Return the initial text offset that was specified when the PDFGlyphVector was created.- Since:
- 2.23.4
-
getStyle
public PDFStyle getStyle()
Returns the PDFStyle of this PDFGlyphVector
-
getLocale
public Locale getLocale()
Return the initial Locale that was specified when the PDFGlyphVector was created.- Since:
- 2.23.4
-
setStyle
public void setStyle(PDFStyle style)
Override the style originally used to create this PDFGlyphVector. This can be used to change the color or other non-font related data, but the font should not be changed. Doing so will likely cause errors- Since:
- 2.23
-
getNumGlyphs
public int getNumGlyphs()
Returns the number of glyphs in this PDFGlyphVector
-
getNumSpaces
public int getNumSpaces()
Returns the number of space characters in this PDFGlyphVector. Space characters are defined to be "word-separators" from css-text, (space and non-breaking space, as well as U+1361, U+10100, U+10101, U+1039F and U+1091F).- Since:
- 2.22
-
savePositions
public void savePositions()
Save the positions of all the glyphs in the PDFGlyphVector. The positions can be restored later by callingrestorePositions()
. This can be used to undo the effects of akern(int, float)
orstretch(float, float)
operation.- Since:
- 2.23
-
restorePositions
public void restorePositions()
Restore the positions of all the glyphs in the PDFGlyphVector to that saved by an earlier call tosavePositions()
. This can be used to undo the effects of akern(int, float)
orstretch(float, float)
operation.- Since:
- 2.23
-
getTextLength
public int getTextLength()
Return the number of characters from the original String this PDFGlyphVector represents. If every character can be displayed using this font, the returned value will be the same as the length of the String supplied to
PDFStyle.createGlyphVector()
. If no characters could be displayed, this method returns 0, and any range in between is possible too.A convenience method that is identical to
getLastIndex(getNumGlyphs() - 1) + 1
- Since:
- 2.22
-
getFlag
public boolean getFlag(int glyph, int flag)
Return whether the specified glyph has the specifiedflag
set. If multiple flags are combined with a logical-or, returns true if any of the flags are set.- Parameters:
glyph
- the glyph number from 0..getNumGlyphs()-1
flag
- theflag
- Returns:
- true if the flag is set on that glyph
-
getWidth
public float getWidth()
Returns the width in points of this sequence.
-
getWidth
public float getWidth(int first, int last)
Return the width from the start of the first glyph to the end of the last glyph. This excludes any kerning applied after the final glyph. SogetWidth() == getWidth(0, getNumGlyphs() - 1) + final_kern
. Since 2.23.3 simply callsgetWidth(first, true, last, false)
- Parameters:
first
- the index of the first glyph in the PDFGlyphVector to measure, inclusivelast
- the index of the last glyph in the PDFGlyphVector to measure, inclusive- Since:
- 2.22
-
getWidth
public float getWidth(int first, boolean firstStart, int last, boolean lastStart)
Return the width from the specified edges of the first glyph to the last glyph. If either "start" parameter is true, the measurement is made to the start edge of that glyph - the left edge for LTR content, the right for RTL. If the "start" parameter is false, the measurement is made to the end edge. This is the start of the glyph plus the advance, and excludes any kerning or justification applied between the trailing edge of the glyph and the start of the next one, if any. This results in the following equivalences with other methods:
getWidth() == getWidth(0, true, getNumGlyphs(), true)
getWidth(a, b) == getWidth(a, true, b, false)
getAdvance(0) == getWidth(0, true, 0, false)
- Parameters:
first
- the index of the first glyph in the PDFGlyphVector to measure, inclusivefirstStart
- if true, the start edge of the first glyph is used. If false, the end edge is usedlast
- the index of the last glyph in the PDFGlyphVector to measure, inclusive- Throws:
IllegalArgumentException
- if first > last, if first < 0, if last > getNumGlyphs() or if (last == getNumGlyphs() and lastStart == false)- Since:
- 2.23.3
-
getFirstIndex
public int getFirstIndex(int off)
Return the first index into the String returned fromgetText()
of the glyph at the specified offset. Glyphs may represent more than one character - for example, if the "f" and "i" characters were combined into an "fi" ligature then this method would return the index of "f" in the original String.- Parameters:
off
- the logical glyph index, from 0..getNumGlyphs()
-1 inclusive- Since:
- 2.22
-
getLastIndex
public int getLastIndex(int off)
Return the last index into the String returned fromgetText()
of the glyph at the specified offset. Glyphs may represent more than one character - for example, if the "f" and "i" characters were combined into an "fi" ligature then this method would return the index of "i" in the original String.- Parameters:
off
- the logical glyph index, from 0..getNumGlyphs()
-1 inclusive- Since:
- 2.22
-
getCID
public int getCID(int off)
Return the glyph id of the glyph at the specified index- Parameters:
off
- the logical glyph index, from 0..getNumGlyphs()
-1 inclusive- Since:
- 2.22.2
-
getAdvance
public double getAdvance(int off)
Return the horizontal advance for the glyph at the specified index- Parameters:
off
- the logical glyph index, from 0..getNumGlyphs()
-1 inclusive- Since:
- 2.22.2
-
getLSB
public double getLSB(int off)
Return the left-side bearing of the glyph at the specified index- Parameters:
off
- the logical glyph index, from 0..getNumGlyphs()
-1 inclusive- Since:
- 2.24.2
-
getPosition
public Point2D getPosition(int off)
Return the start position of the glyph at the specified index, relative to the start position of the start of the text. Note that this does not include any transformation set on the glyph withsetGlyphTransform(int, java.awt.geom.AffineTransform)
- Parameters:
off
- the logical glyph index, from 0..getNumGlyphs()
-1 inclusive- Since:
- 2.22.2
-
stretch
public float stretch(float chardelta, float spacedelta)
Stretch or squash this GlyphVector by adding track kerning between each glyph. Space is considered to be added after a glyph, which is only significant if the PDFGlyphVector is split after being stretched. Calling this method more than once will add the values to the adjustments set by any previous calls tostretch(float, float)
orkern(int, float)
, not replace them.- Parameters:
chardelta
- the number of points to add to the overall width of this PDFGlyhVector, evenly distributed over every glyph.spacedelta
- the number of points to add to the overall width of this PDFGlyhVector, added to spaces only.- Returns:
- the new width of this PDFGlyphVector, which would normally be
getWidth() + chardelta + spacedelta
. The only time that won't be the case is if(spacedelta != 0 && getNumSpaces() == 0) || getNumGlyphs() == 0
- Since:
- 2.22
-
kern
public void kern(int off, float kern)
Add or remove whitespace at a particular point in the PDFGlyphVector. The specified amount of space is added to the glyph after the specified index - a positive value moves glyphs apart, a negative value moves them closer together. Calling this method more than once will add the value to adjustment set by previous calls tostretch(float, float)
orkern(int, float)
, not replace them. Kerned value are considered to be part of the glyph and will be maintained if the PDFGlyphVector is split into two parts.- Parameters:
off
- the logical glyph index to add the space to, from 0..getNumGlyphs()
exclusivekern
- the amount of space to add after this glyph, in points.- Since:
- 2.22
-
getAdjacentPosition
public float getAdjacentPosition(PDFGlyphVector gv, float y)
Given a second PDFGlyphVector and the difference in baselines between this glyph vector and the second, return the horizontal kerning between this PDFGlyphVector and the second so that the two are adjacent.
In almost all cases this is simply 0 - the exception is when OpenType "math" layout is in use.
For example, given two glyphvectors, one representing the base and the other a superscript, here's how to position them relative to eachother.
PDFGlyphVector base = ... PDFGlyphVector super = ... float superscriptOffset = baseFontSize * 0.5f; // +ve moves up float x = base.getAdjacentPosition(super, superscriptOffset); canvas.drawGlyphVector(base, 0, 0); canvas.drawGlyphVector(super, x, superscriptOffset);
If the "y" value passed in is NaN, the returned value will instead show the top accent position of the final glyph - the top accent attachment position, less half the width of the other PDFGlyphVector. For example, here is how to correctly position a value above the integral sign, which is often sloped to the right.
PDFGlyphVector integral = big.createGlyphVector("∫", null); PDFGlyphVector super = small.createGlyphVector("n", null); float x = base.getAdjacentPosition(super, Float.NaN); canvas.drawGlyphVector(base, 0, 0); canvas.drawGlyphVector(super, x, big.getFontSize() * ascent);
- Parameters:
gv
- the other PDFGlyphVector. If null, only the "italicsCorrection" or "topAccentAttachmentPoint" value on this font is returned, depending on whether y < 0 or y != yy
- the difference from this objects baseline to the baseline of "gv". A positive value means "gv" is a superscript of this PDFGlyphVector, a negative value means its a subscript (and the returned value will include "italicsCorrection"). A value of zero means the baseline is the same, in which case the returned value is the same asgetWidth()
. A value of Float.NaN will request the "top accent position" of the final glyph in this object.- Returns:
- the X position to place the second PDFGlyphVector, relative to the start of this one
- Since:
- 2.24.1
-
setGlyphTransform
public void setGlyphTransform(int off, AffineTransform transform)
Set an AffineTransform on an individual glyph. This method will adjust the glyph position but have no effect on any surrounding glyphs, which will be positioned as normal.- Parameters:
off
- the logical glyph index, from 0..getNumGlyphs()
-1 inclusivetransform
- the AffineTransform to apply- Since:
- 2.23.1
-
getGlyphTransform
public AffineTransform getGlyphTransform(int off)
Return the AffineTransform previously applied to the glyph viasetGlyphTransform(int, java.awt.geom.AffineTransform)
, or null if no transform was applied.- Parameters:
off
- the logical glyph index, from 0..getNumGlyphs()
-1 inclusive- Returns:
- the glyph transform
- Since:
- 2.23.1
-
getOutline
public Shape getOutline()
Get the Outline of this PDFGlyphVector as a Shape. The Shape is in PDF coordinates, so (0,0) is the left-most edge of the glyph at the baseline and positive values extend up. To render to a bitmap, it needs to be flipped vertically. The returned Shape will include any glyph transformations set by
setGlyphTransform(int, java.awt.geom.AffineTransform)
.If we cannot retrieve the outline (for example if the font is not embedded) then this method returns null.
- Since:
- 2.23.1
-
getOutline
public Shape getOutline(int index, boolean boundsOnly)
Get the Outline of a single glyph from this PDFGlyphVector as a Shape. The same description that applies to
getOutline()
applies here, except only a single glyph is returned.If the rectangular bounds is requested, a Shape is always returned - although it may not be rectangular (if the glyph is transformed or italicized). If the exact shape is requested, and we cannot retrieve the outline (for example, if the font is not embedded) then this method returns null.
- Parameters:
index
- the glyph index, from 0..getNumGlyphs() - 1boundsOnly
- if true, only the rectangular bounds of the shape is considered - if false, the exact shape is returned.- Since:
- 2.25
-
getActualText
public String getActualText()
Get the "ActualText" this PDFGlyphVector represents. This is possibly a subtext of the originaltext
.- Since:
- 2.23.4
-
setActualText
public void setActualText(String actualtext)
Set the "ActualText" this PDFGlyphVector represents. This method can be called to change the value if the logical value of the text is not represented by the text the glyphs were created from - the specified value will be stored in the document structure and used for text extraction- Parameters:
actualtext
- the text value that should be stored in the Document Structure- Since:
- 2.23.4
-
-