|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object | +--gnu.java.awt.font.opentype.truetype.VirtualMachine
A virtual machine for interpreting TrueType bytecodes.
Lack of Thread Safety: The virtual machine is intentionally not safe to access from multiple concurrent threads. Synchronization needs to be performed externally. Usually, the font has already obtained a lock before calling the scaler, which in turn calls the VM. It would be wasteful to acquire additional locks for the VM.
Implementation Status: The current implementation can execute pre-programs of fonts, but it does not yet actually move any points. Control flow and arithmeti instructions are implemented, but most geometric instructions are not working yet. So, the VirtualMachine class is currently a no-op. However, not very much is missing. You are more than welcome to complete the implementation.
Patents: Apple Computer holds three United States Patents for the mathematical algorithms that are used by TrueType instructions. The monopoly granted by these patents will expire in October 2009. Before the expiration date, a license must be obtained from Apple Computer to use the patented technology inside the United States. For other countries, different dates might apply, or no license might be needed.
The default build of this class does not use the patented algorithms. If you have obtained a license from Apple, or if the patent protection has expired, or if no license is required for your contry, you can set a flag in the source file which will enable the use of the patented mathematical algorithms.
The relevant patents are listed subsequently.
| Field Summary | |
private boolean |
antialiased
Indicates whether or not scan-line conversion will use anti-aliasing (with gray levels). |
private int |
cachedPixelsPerEM
A cache for the number of pixels per EM. |
private java.nio.ShortBuffer |
controlValueTable
The original Control Value Table, sometimes abbreviated as CVT. |
private int[] |
cvt
The scaled values inside the control value table. |
private int |
cvtCutIn
|
private int |
deltaBase
|
private int |
deltaShift
|
private java.awt.geom.AffineTransform |
deviceTransform
|
private short |
dualX
|
private short |
dualY
|
private int |
engineCompensation
A value that is used by rounding operations to compensate for dot gain. |
private boolean |
executeGlyphInstructions
Indicates whether or not the instructions that are associated with individual glyphs shall be executed. |
private java.nio.ByteBuffer[] |
fdefBuffer
fdefBuffer[i] is the buffer that contains the TrueType instructions of function #i. |
private int[] |
fdefEntryPoint
fdefEntryPoint[i] is the position in fdefBuffer[i] where the first TrueType instruction after the FDEF is located. |
private java.nio.ByteBuffer |
fontProgram
The contents of the font’s fpgm table, or
null after the font program has been executed once. |
private short |
freeX
|
private short |
freeY
|
private Zone |
glyphZone
|
private boolean |
ignoreCVTProgram
Indicates whether to ignore any modifications to the control value table that the font’s pre-program might have performed. |
private static java.lang.String[] |
INST_NAME
|
private int |
loop
|
private int |
maxStackElements
The maximum number of stack elements. |
private int |
minimumDistance
|
private int |
numTwilightPoints
The number of points in the Twilight Zone. |
private static short |
ONE_214
The value 1 in 2-dot-14 fixed notation. |
private static boolean |
PATENTED_HINTING
Indicates whether or not to perform hinting operations that are protected by a number of US patents, two of which will expire on October 13, 2009, and one of which will expire on October 27, 2009. |
private int |
pointSize
The current point size of the scaled font. |
private java.nio.ByteBuffer |
preProgram
The prep table of the font, which contains a program
that is executed whenever the point size or the device transform
have changed. |
private short |
projX
|
private short |
projY
|
private int |
roundPeriod
The length of the space between rounded values. |
private int |
roundPhase
The offset of the rounded values from multiples of roundPeriod. |
private int |
roundThreshold
|
private int |
rp0
|
private int |
rp1
|
private int |
rp2
|
private int |
scaleX
|
private int |
scaleY
|
private boolean |
scanControl
|
private int |
scanType
|
private int |
shearX
|
private int |
shearY
|
private int |
singleWidthValue
|
private int |
sp
The current stack pointer of the virtual machine. |
private int[] |
stack
The stack. |
private int[] |
storage
The storage area of the virtual machine. |
private static boolean |
TRACE_EXECUTION
Indicates whether the execution of the Virtual Machine is traced to System.out. |
private Zone |
twilightZone
|
private int |
unitsPerEm
The number of font units per EM. |
private Zone |
zp0
|
private Zone |
zp1
|
private Zone |
zp2
|
| Constructor Summary | |
(package private) |
VirtualMachine(int unitsPerEm,
java.nio.ByteBuffer maxp,
java.nio.ByteBuffer controlValueTable,
java.nio.ByteBuffer fontProgram,
java.nio.ByteBuffer preProgram)
Constructs a new Virtual Machine for executing TrueType instructions. |
| Method Summary | |
private int |
applyCTM_x(int x,
int y)
|
private int |
applyCTM_y(int x,
int y)
|
private void |
deltaC(int[] pairs,
int offset,
int numPairs,
int base)
Implements the DELTAC instructions. |
private void |
dumpInstruction(java.nio.ByteBuffer inst)
Writes a textual description of the current TrueType instruction, including the top stack elements, to System.out. |
private void |
dumpVectors()
|
private void |
execute(java.nio.ByteBuffer instructions,
int pos)
Executes a stream of TrueType instructions. |
private boolean |
executeInstruction(java.nio.ByteBuffer inst)
Executes a single TrueType instruction. |
private int |
funitsToPixels(int funits)
Scales a value from font unites to pixels. |
private int |
getDualProjection(int x,
int y)
Projects the specified vector along the current dual projection vector. |
private int |
getFreeDotProj()
|
private static java.lang.String |
getHex(byte b)
|
private static java.lang.String |
getHex(short b)
|
private static int |
getInstructionLength(int opcode)
Returns the number of bytes that a TrueType instruction occupies. |
private static char |
getNibble(int i,
int rightShift)
|
private int |
getOriginalProjection(Zone zone,
int point)
|
private int |
getPixelsPerEM()
Returns how many pixels there are per EM, in direction of the current projection vector. |
private int |
getProjection(int x,
int y)
Projects the specified vector along the current projection vector. |
private int |
getProjection(Zone zone,
int point)
|
private Zone |
getZone(int zoneNumber)
|
private void |
handleIP()
|
private void |
handleISECT(int a0,
int a1,
int b0,
int b1,
int p)
|
private void |
handleMDAP(int point,
boolean round)
|
private void |
handleMIAP(int cvtIndex,
int point,
boolean round)
|
private void |
handleMIRP(int bcode,
int point,
int cvtIndex)
|
private boolean |
isRotated()
Determines whether the current glyph is rotated. |
private boolean |
isStretched()
Determines whether the current glyph is stretched. |
private void |
movePoint(Zone zone,
int point,
int distance)
|
private static int |
muldiv(int a,
int b,
int c)
|
private static int |
nround(int distance,
int compensation)
|
private void |
reloadControlValueTable()
Reloads the control value table and scales each entry from font units to pixel values. |
private void |
resetGraphicsState()
Sets the graphics state to default values. |
private int |
round(int distance,
int compensation)
|
private void |
setDualVector(short x,
short y)
|
private void |
setFreedomVector(short x,
short y)
|
private void |
setProjectionVector(short x,
short y)
|
private void |
setRoundingMode(int period,
int mode)
Sets the rounding mode. |
boolean |
setup(double pointSize,
java.awt.geom.AffineTransform deviceTransform,
boolean antialiased)
Sets up the virtual machine for the specified parameters. |
private static void |
skipAfter(java.nio.ByteBuffer inst,
int opcode1,
int opcode2,
int illegalCode1,
int illegalCode2,
boolean handleNestedIfClauses)
Skips any instructions until the specified opcode has been encoutered. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
private static final boolean PATENTED_HINTING
private static final boolean TRACE_EXECUTION
private static final short ONE_214
private final int[] storage
private int[] stack
sp[0] gets used before sp[1].
private final int maxStackElements
private int sp
private java.nio.ByteBuffer[] fdefBuffer
private int[] fdefEntryPoint
private java.nio.ShortBuffer controlValueTable
null.
private int[] cvt
private int engineCompensation
private java.nio.ByteBuffer fontProgram
fpgm table, or
null after the font program has been executed once.
private java.nio.ByteBuffer preProgram
prep table of the font, which contains a program
that is executed whenever the point size or the device transform
have changed. This program is called pre-program because it gets
executed before the instructions of the individual glyphs. If
the font does not contain a pre-program, the value of this field
is null.
private int numTwilightPoints
private int pointSize
private java.awt.geom.AffineTransform deviceTransform
private int scaleX
private int scaleY
private int shearX
private int shearY
private boolean antialiased
GETINFO instruction, and some
programs may behave differently according to this setting.
private int cvtCutIn
private int deltaBase
private int deltaShift
private short freeX
private short freeY
private int loop
private int minimumDistance
private short projX
private short projY
private short dualX
private short dualY
private int rp0
private int rp1
private int rp2
private boolean scanControl
private int scanType
private int singleWidthValue
private Zone zp0
private Zone zp1
private Zone zp2
private Zone twilightZone
private Zone glyphZone
private boolean executeGlyphInstructions
private boolean ignoreCVTProgram
private int roundPeriod
private int roundPhase
roundPeriod.
private int roundThreshold
private int cachedPixelsPerEM
getPixelsPerEM()private int unitsPerEm
private static final java.lang.String[] INST_NAME
| Constructor Detail |
VirtualMachine(int unitsPerEm,
java.nio.ByteBuffer maxp,
java.nio.ByteBuffer controlValueTable,
java.nio.ByteBuffer fontProgram,
java.nio.ByteBuffer preProgram)
throws java.awt.FontFormatException
unitsPerEm - the number of font units in one typographic
em.preProgram - the prep table of the font, which
contains a program that is executed whenever the point size or
the device transform have changed. This program is called
pre-program because it gets executed before the instructions of
the individual glyphs. If the font does not contain a
pre-program, pass null.| Method Detail |
private void resetGraphicsState()
private void reloadControlValueTable()
private int funitsToPixels(int funits)
public boolean setup(double pointSize,
java.awt.geom.AffineTransform deviceTransform,
boolean antialiased)
pointSize - the point size of the scaled font.deviceTransform - an affine transformation which gets
applied in addition to scaling by pointSize. Font
programs can separately inquire about the point size. For this
reason, it is not recommended to pre-multiply the point size to
the device transformation.antialiased - true if the scan-line conversion
algorithm will use gray levels to give a smoother appearance,
false otherwise. Font programs can ask for this
value with the GETINFO instruction, and some
programs may behave differently according to this setting.
private void execute(java.nio.ByteBuffer instructions,
int pos)
private void dumpInstruction(java.nio.ByteBuffer inst)
System.out.
This is useful for debugging.
inst - the instruction stream, positioned at the current
instruction.
private static char getNibble(int i,
int rightShift)
private static java.lang.String getHex(byte b)
private static java.lang.String getHex(short b)
private static void skipAfter(java.nio.ByteBuffer inst,
int opcode1,
int opcode2,
int illegalCode1,
int illegalCode2,
boolean handleNestedIfClauses)
inst - the current instruction stream. After the call,
the position of inst is right after the first
occurence of opcode.opcode1 - the opcode for which to look.opcode2 - another opcode for which to look. Pass -1
if only opcode1 would terminate skipping.illegalCode1 - an opcode that must not be encountered
while skipping. Pass -1 if any opcode is acceptable.illegalCode2 - another opcode that must not be encountered
while skipping. Pass -1 to perform no check.handleNestedIfClauses - true to handle
nested IF [ELSE] EIF clauses, false
to ignore them. From the TrueType specification document,
one would think that nested if clauses would not be valid,
but they do appear in some fonts.
java.lang.IllegalStateException - if illegalCode1 or
illegalCode2 has been encountered while skipping.private static int getInstructionLength(int opcode)
opcode - the instruction.
NPUSHB and NPUSHW, where
the instruction length depends on the first operand byte, the
result is -1.private boolean executeInstruction(java.nio.ByteBuffer inst)
true if another instruction shall be
executed in the same call frame; false if the
current call frame shall be popped.
private void setRoundingMode(int period,
int mode)
period - the grid period in fixed-point notation, such as
Fixed.ONE for the SROUND instruction or
sqrt(2)/2 for the S45ROUND instruction.mode - a byte whose bits are set according to the TrueType
specification for SROUND and S45ROUND parameters.
private void deltaC(int[] pairs,
int offset,
int numPairs,
int base)
pairs - the delta table. Because the delta table is on
the stack, callers usually just want to pass the stack array.offset - the offset of the first pair in pairs.numPairs - the number of pairs.base - 0 for DELTAC1, 16 for DELTAC2,
or 32 for DELTAC2.DELTAC1, DELTAC2, and DELTAC3private Zone getZone(int zoneNumber)
private int getProjection(int x,
int y)
x - the x component of the input vector, in 26.6 fixed-point
notation.y - the y component of the input vector, in 26.6 fixed-point
notation.
private int getDualProjection(int x,
int y)
x - the x component of the input vector, in 26.6 fixed-point
notation.y - the y component of the input vector, in 26.6 fixed-point
notation.
private int getProjection(Zone zone,
int point)
private int getOriginalProjection(Zone zone,
int point)
private void handleISECT(int a0,
int a1,
int b0,
int b1,
int p)
private static int muldiv(int a,
int b,
int c)
private int getFreeDotProj()
private void movePoint(Zone zone,
int point,
int distance)
private void dumpVectors()
private void handleIP()
private void handleMDAP(int point,
boolean round)
private void handleMIAP(int cvtIndex,
int point,
boolean round)
private void handleMIRP(int bcode,
int point,
int cvtIndex)
private int round(int distance,
int compensation)
private static int nround(int distance,
int compensation)
private boolean isRotated()
false if the shearing factors for the
x and y axes are zero; true if they
are non-zero.private boolean isStretched()
false if the scaling factors for the
x and y axes are are equal; true if
they differ.private int getPixelsPerEM()
private void setProjectionVector(short x,
short y)
private void setFreedomVector(short x,
short y)
private void setDualVector(short x,
short y)
private int applyCTM_x(int x,
int y)
private int applyCTM_y(int x,
int y)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||