📄 shape.cpp
字号:
b->m_bitPos = 0;
b->m_bitBuf = 0;
}
static inline U8 GetByte(struct BitParser *b)
{
U8 v;
v = *b->ptr++;
return v;
}
static inline U16 GetWord(struct BitParser *b)
{
U8 *s;
U16 v;
s = b->ptr;
v = s[0] | ((U16) s[1] << 8);
b->ptr = s + 2;
return v;
}
static inline U32 GetDWord(struct BitParser *b)
{
U32 v;
U8 * s = b->ptr;
v = (U32) s[0] | ((U32) s[1] << 8) |
((U32) s[2] << 16) | ((U32) s [3] << 24);
b->ptr = s + 4;
return v;
}
static inline U32 GetBit (struct BitParser *b)
{
U32 v;
S32 m_bitPos = b->m_bitPos;
U32 m_bitBuf = b->m_bitBuf;
if (m_bitPos == 0) {
m_bitBuf = (U32)(*b->ptr++) << 24;
m_bitPos = 8;
}
v = (m_bitBuf >> 31);
m_bitPos--;
m_bitBuf <<= 1;
b->m_bitPos = m_bitPos;
b->m_bitBuf = m_bitBuf;
return v;
}
static inline U32 GetBits (struct BitParser *b, int n)
{
U32 v;
S32 m_bitPos = b->m_bitPos;
U32 m_bitBuf = b->m_bitBuf;
if (n == 0)
return 0;
while (m_bitPos < n) {
m_bitBuf |= (U32)(*b->ptr++) << (24 - m_bitPos);
m_bitPos += 8;
}
v = m_bitBuf >> (32 - n);
m_bitBuf <<= n;
m_bitPos -= n;
b->m_bitPos = m_bitPos;
b->m_bitBuf = m_bitBuf;
return v;
}
// Get n bits from the string with sign extension.
static inline S32 GetSBits (struct BitParser *b,S32 n)
{
// Get the number as an unsigned value.
S32 v = (S32) GetBits(b,n);
// Is the number negative?
if (v & (1L << (n - 1)))
{
// Yes. Extend the sign.
v |= -1L << n;
}
return v;
}
/************************************************************************/
static void GetMatrix(BitParser *b, Matrix* mat)
{
InitBits(b);
// Scale terms
if (GetBit(b))
{
int nBits = (int) GetBits(b,5);
mat->a = (float)(GetSBits(b,nBits))/(float)0x10000;
mat->d = (float)(GetSBits(b,nBits))/(float)0x10000;
}
else
{
mat->a = mat->d = 1.0;
}
// Rotate/skew terms
if (GetBit(b))
{
int nBits = (int)GetBits(b,5);
mat->c = (float)(GetSBits(b,nBits))/(float)0x10000;
mat->b = (float)(GetSBits(b,nBits))/(float)0x10000;
}
else
{
mat->b = mat->c = 0.0;
}
// Translate terms
int nBits = (int) GetBits(b,5);
mat->tx = GetSBits(b,nBits);
mat->ty = GetSBits(b,nBits);
}
static FillStyleDef * ParseFillStyle(ShapeParser *shape, long *n, long getAlpha)
{
BitParser *b = &shape->bit_parser;
FillStyleDef *defs;
U16 i = 0;
// Get the number of fills.
U16 nFills = GetByte(b);
// Do we have a larger number?
if (nFills == 255)
{
// Get the larger number.
nFills = GetWord(b);
}
*n = nFills;
defs = new FillStyleDef[ nFills ];
if (defs == NULL) return NULL;
// Get each of the fill style.
for (i = 0; i < nFills; i++)
{
U16 fillStyle = GetByte(b);
defs[i].type = (FillType) fillStyle;
if (fillStyle & 0x10)
{
defs[i].type = (FillType) (fillStyle & 0x12);
// Get the gradient matrix.
GetMatrix(b,&(defs[i].matrix));
// Get the number of colors.
defs[i].gradient.nbGradients = GetByte(b);
// Get each of the colors.
for (U16 j = 0; j < defs[i].gradient.nbGradients; j++)
{
defs[i].gradient.ratio[j] = GetByte(b);
defs[i].gradient.color[j].red = GetByte(b);
defs[i].gradient.color[j].green = GetByte(b);
defs[i].gradient.color[j].blue = GetByte(b);
if (getAlpha) {
defs[i].gradient.color[j].alpha = GetByte(b);
} else {
defs[i].gradient.color[j].alpha = ALPHA_OPAQUE;
}
}
}
else if (fillStyle & 0x40)
{
defs[i].type = (FillType) (fillStyle & 0x41);
// Get the bitmapId
defs[i].bitmap = (Bitmap *)shape->dict->getCharacter(GetWord(b));
// Get the bitmap matrix.
GetMatrix(b,&(defs[i].matrix));
}
else
{
defs[i].type = (FillType) 0;
// A solid color
defs[i].color.red = GetByte(b);
defs[i].color.green = GetByte(b);
defs[i].color.blue = GetByte(b);
if (getAlpha) {
defs[i].color.alpha = GetByte(b);
} else {
defs[i].color.alpha = ALPHA_OPAQUE;
}
}
}
return defs;
}
static LineStyleDef * ParseLineStyle(ShapeParser *shape, long *n, long getAlpha)
{
BitParser *b = &shape->bit_parser;
LineStyleDef *defs,*def;
FillStyleDef *f;
long i;
// Get the number of lines.
U16 nLines = GetByte(b);
// Do we have a larger number?
if (nLines == 255)
{
// Get the larger number.
nLines = GetWord(b);
}
*n = nLines;
defs = new LineStyleDef[ nLines ];
if (defs == NULL) return NULL;
// Get each of the line styles.
for (i = 0; i < nLines; i++)
{
def=&defs[i];
def->width = GetWord(b);
def->color.red = GetByte(b);
def->color.green = GetByte(b);
def->color.blue = GetByte(b);
if (getAlpha) {
def->color.alpha = GetByte(b);
} else {
def->color.alpha = ALPHA_OPAQUE;
}
f=&def->fillstyle;
f->type = f_Solid;
f->color = def->color;
if (shape->cxform) {
f->color = shape->cxform->getColor(f->color);
}
f->color.pixel = shape->gd->allocColor(f->color);
}
return defs;
}
/* 0 = end of shape */
static int ParseShapeRecord(ShapeParser *shape, ShapeRecord *sr, long getAlpha)
{
BitParser *b = &shape->bit_parser;
// Determine if this is an edge.
BOOL isEdge = (BOOL) GetBit(b);
if (!isEdge)
{
// Handle a state change
U16 flags = (U16) GetBits(b,5);
// Are we at the end?
if (flags == 0)
{
// End of shape
return 0;
}
sr->type = shapeNonEdge;
sr->flags = (ShapeFlags)flags;
// Process a move to.
if (flags & flagsMoveTo)
{
U16 nBits = (U16) GetBits(b,5);
sr->x = GetSBits(b,nBits);
sr->y = GetSBits(b,nBits);
}
// Get new fill info.
if (flags & flagsFill0)
{
sr->fillStyle0 = GetBits(b,shape->m_nFillBits);
}
if (flags & flagsFill1)
{
sr->fillStyle1 = GetBits(b,shape->m_nFillBits);
}
// Get new line info
if (flags & flagsLine)
{
sr->lineStyle = GetBits(b,shape->m_nLineBits);
}
// Check to get a new set of styles for a new shape layer.
if (flags & flagsNewStyles)
{
FillStyleDef *fillDefs;
LineStyleDef *lineDefs;
long n;
// Parse the style.
fillDefs = ParseFillStyle(shape, &n, getAlpha);
if (fillDefs == NULL) return 0;
sr->newFillStyles = fillDefs;
sr->nbNewFillStyles = n;
lineDefs = ParseLineStyle(shape, &n, getAlpha);
if (lineDefs == NULL) return 0;
sr->newLineStyles = lineDefs;
sr->nbNewLineStyles = n;
InitBits(b); // Bug !
// Reset.
shape->m_nFillBits = (U16) GetBits(b,4);
shape->m_nLineBits = (U16) GetBits(b,4);
}
//if (flags & flagsEndShape)
//printf("\tEnd of shape.\n\n");
return flags & flagsEndShape ? 0 : 1;
}
else
{
if (GetBit(b))
{
sr->type = shapeLine;
// Handle a line
U16 nBits = (U16) GetBits(b,4) + 2; // nBits is biased by 2
// Save the deltas
if (GetBit(b))
{
// Handle a general line.
sr->dX = GetSBits(b,nBits);
sr->dY = GetSBits(b,nBits);
}
else
{
// Handle a vert or horiz line.
if (GetBit(b))
{
// Vertical line
sr->dY = GetSBits(b,nBits);
sr->dX = 0;
}
else
{
// Horizontal line
sr->dX = GetSBits(b,nBits);
sr->dY = 0;
}
}
}
else
{
sr->type = shapeCurve;
// Handle a curve
U16 nBits = (U16) GetBits(b,4) + 2; // nBits is biased by 2
// Get the control
sr->ctrlX = GetSBits(b,nBits);
sr->ctrlY = GetSBits(b,nBits);
// Get the anchor
sr->anchorX = GetSBits(b,nBits);
sr->anchorY = GetSBits(b,nBits);
}
return 1;
}
}
static void drawShape(GraphicDevice *gd, Matrix *matrix1, Cxform *cxform, Shape *shape,
ShapeAction shapeAction, void *id,ScanLineFunc scan_line_func)
{
LineStyleDef *l;
FillStyleDef *f0;
FillStyleDef *f1;
ShapeRecord sr1,*sr = &sr1;
int firstPoint;
long lastX,lastY;
LineStyleDef *curLineStyle;
long curNbLineStyles;
FillStyleDef *curFillStyle;
long curNbFillStyles;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -