Init() public method

public Init ( InputMeta meta ) : void
meta InputMeta
return void
Ejemplo n.º 1
0
        public void ReadAll()
        {
            if (Meta.ReadInt() != unchecked ((int)0x9AC6CDD7))
            {
                throw new DocumentException("Not a placeable windows metafile");
            }
            Meta.ReadWord();
            _left           = Meta.ReadShort();
            _top            = Meta.ReadShort();
            _right          = Meta.ReadShort();
            _bottom         = Meta.ReadShort();
            _inch           = Meta.ReadWord();
            _state.ScalingX = (_right - _left) / (float)_inch * 72f;
            _state.ScalingY = (_bottom - _top) / (float)_inch * 72f;
            _state.OffsetWx = _left;
            _state.OffsetWy = _top;
            _state.ExtentWx = _right - _left;
            _state.ExtentWy = _bottom - _top;
            Meta.ReadInt();
            Meta.ReadWord();
            Meta.Skip(18);

            int tsize;
            int function;

            Cb.SetLineCap(1);
            Cb.SetLineJoin(1);
            for (; ;)
            {
                var lenMarker = Meta.Length;
                tsize = Meta.ReadInt();
                if (tsize < 3)
                {
                    break;
                }

                function = Meta.ReadWord();
                switch (function)
                {
                case 0:
                    break;

                case META_CREATEPALETTE:
                case META_CREATEREGION:
                case META_DIBCREATEPATTERNBRUSH:
                    _state.AddMetaObject(new MetaObject());
                    break;

                case META_CREATEPENINDIRECT:
                {
                    var pen = new MetaPen();
                    pen.Init(Meta);
                    _state.AddMetaObject(pen);
                    break;
                }

                case META_CREATEBRUSHINDIRECT:
                {
                    var brush = new MetaBrush();
                    brush.Init(Meta);
                    _state.AddMetaObject(brush);
                    break;
                }

                case META_CREATEFONTINDIRECT:
                {
                    var font = new MetaFont();
                    font.Init(Meta);
                    _state.AddMetaObject(font);
                    break;
                }

                case META_SELECTOBJECT:
                {
                    var idx = Meta.ReadWord();
                    _state.SelectMetaObject(idx, Cb);
                    break;
                }

                case META_DELETEOBJECT:
                {
                    var idx = Meta.ReadWord();
                    _state.DeleteMetaObject(idx);
                    break;
                }

                case META_SAVEDC:
                    _state.SaveState(Cb);
                    break;

                case META_RESTOREDC:
                {
                    var idx = Meta.ReadShort();
                    _state.RestoreState(idx, Cb);
                    break;
                }

                case META_SETWINDOWORG:
                    _state.OffsetWy = Meta.ReadShort();
                    _state.OffsetWx = Meta.ReadShort();
                    break;

                case META_SETWINDOWEXT:
                    _state.ExtentWy = Meta.ReadShort();
                    _state.ExtentWx = Meta.ReadShort();
                    break;

                case META_MOVETO:
                {
                    var y = Meta.ReadShort();
                    var p = new System.Drawing.Point(Meta.ReadShort(), y);
                    _state.CurrentPoint = p;
                    break;
                }

                case META_LINETO:
                {
                    var y = Meta.ReadShort();
                    var x = Meta.ReadShort();
                    var p = _state.CurrentPoint;
                    Cb.MoveTo(_state.TransformX(p.X), _state.TransformY(p.Y));
                    Cb.LineTo(_state.TransformX(x), _state.TransformY(y));
                    Cb.Stroke();
                    _state.CurrentPoint = new System.Drawing.Point(x, y);
                    break;
                }

                case META_POLYLINE:
                {
                    _state.LineJoinPolygon = Cb;
                    var len = Meta.ReadWord();
                    var x   = Meta.ReadShort();
                    var y   = Meta.ReadShort();
                    Cb.MoveTo(_state.TransformX(x), _state.TransformY(y));
                    for (var k = 1; k < len; ++k)
                    {
                        x = Meta.ReadShort();
                        y = Meta.ReadShort();
                        Cb.LineTo(_state.TransformX(x), _state.TransformY(y));
                    }
                    Cb.Stroke();
                    break;
                }

                case META_POLYGON:
                {
                    if (IsNullStrokeFill(false))
                    {
                        break;
                    }

                    var len = Meta.ReadWord();
                    var sx  = Meta.ReadShort();
                    var sy  = Meta.ReadShort();
                    Cb.MoveTo(_state.TransformX(sx), _state.TransformY(sy));
                    for (var k = 1; k < len; ++k)
                    {
                        var x = Meta.ReadShort();
                        var y = Meta.ReadShort();
                        Cb.LineTo(_state.TransformX(x), _state.TransformY(y));
                    }
                    Cb.LineTo(_state.TransformX(sx), _state.TransformY(sy));
                    StrokeAndFill();
                    break;
                }

                case META_POLYPOLYGON:
                {
                    if (IsNullStrokeFill(false))
                    {
                        break;
                    }

                    var numPoly = Meta.ReadWord();
                    var lens    = new int[numPoly];
                    for (var k = 0; k < lens.Length; ++k)
                    {
                        lens[k] = Meta.ReadWord();
                    }

                    for (var j = 0; j < lens.Length; ++j)
                    {
                        var len = lens[j];
                        var sx  = Meta.ReadShort();
                        var sy  = Meta.ReadShort();
                        Cb.MoveTo(_state.TransformX(sx), _state.TransformY(sy));
                        for (var k = 1; k < len; ++k)
                        {
                            var x = Meta.ReadShort();
                            var y = Meta.ReadShort();
                            Cb.LineTo(_state.TransformX(x), _state.TransformY(y));
                        }
                        Cb.LineTo(_state.TransformX(sx), _state.TransformY(sy));
                    }
                    StrokeAndFill();
                    break;
                }

                case META_ELLIPSE:
                {
                    if (IsNullStrokeFill(_state.LineNeutral))
                    {
                        break;
                    }

                    var b = Meta.ReadShort();
                    var r = Meta.ReadShort();
                    var t = Meta.ReadShort();
                    var l = Meta.ReadShort();
                    Cb.Arc(_state.TransformX(l), _state.TransformY(b), _state.TransformX(r), _state.TransformY(t), 0, 360);
                    StrokeAndFill();
                    break;
                }

                case META_ARC:
                {
                    if (IsNullStrokeFill(_state.LineNeutral))
                    {
                        break;
                    }

                    var yend   = _state.TransformY(Meta.ReadShort());
                    var xend   = _state.TransformX(Meta.ReadShort());
                    var ystart = _state.TransformY(Meta.ReadShort());
                    var xstart = _state.TransformX(Meta.ReadShort());
                    var b      = _state.TransformY(Meta.ReadShort());
                    var r      = _state.TransformX(Meta.ReadShort());
                    var t      = _state.TransformY(Meta.ReadShort());
                    var l      = _state.TransformX(Meta.ReadShort());
                    var cx     = (r + l) / 2;
                    var cy     = (t + b) / 2;
                    var arc1   = GetArc(cx, cy, xstart, ystart);
                    var arc2   = GetArc(cx, cy, xend, yend);
                    arc2 -= arc1;
                    if (arc2 <= 0)
                    {
                        arc2 += 360;
                    }

                    Cb.Arc(l, b, r, t, arc1, arc2);
                    Cb.Stroke();
                    break;
                }

                case META_PIE:
                {
                    if (IsNullStrokeFill(_state.LineNeutral))
                    {
                        break;
                    }

                    var yend   = _state.TransformY(Meta.ReadShort());
                    var xend   = _state.TransformX(Meta.ReadShort());
                    var ystart = _state.TransformY(Meta.ReadShort());
                    var xstart = _state.TransformX(Meta.ReadShort());
                    var b      = _state.TransformY(Meta.ReadShort());
                    var r      = _state.TransformX(Meta.ReadShort());
                    var t      = _state.TransformY(Meta.ReadShort());
                    var l      = _state.TransformX(Meta.ReadShort());
                    var cx     = (r + l) / 2;
                    var cy     = (t + b) / 2;
                    var arc1   = GetArc(cx, cy, xstart, ystart);
                    var arc2   = GetArc(cx, cy, xend, yend);
                    arc2 -= arc1;
                    if (arc2 <= 0)
                    {
                        arc2 += 360;
                    }

                    var ar = PdfContentByte.BezierArc(l, b, r, t, arc1, arc2);
                    if (ar.Count == 0)
                    {
                        break;
                    }

                    var pt = (float[])ar[0];
                    Cb.MoveTo(cx, cy);
                    Cb.LineTo(pt[0], pt[1]);
                    for (var k = 0; k < ar.Count; ++k)
                    {
                        pt = (float[])ar[k];
                        Cb.CurveTo(pt[2], pt[3], pt[4], pt[5], pt[6], pt[7]);
                    }
                    Cb.LineTo(cx, cy);
                    StrokeAndFill();
                    break;
                }

                case META_CHORD:
                {
                    if (IsNullStrokeFill(_state.LineNeutral))
                    {
                        break;
                    }

                    var yend   = _state.TransformY(Meta.ReadShort());
                    var xend   = _state.TransformX(Meta.ReadShort());
                    var ystart = _state.TransformY(Meta.ReadShort());
                    var xstart = _state.TransformX(Meta.ReadShort());
                    var b      = _state.TransformY(Meta.ReadShort());
                    var r      = _state.TransformX(Meta.ReadShort());
                    var t      = _state.TransformY(Meta.ReadShort());
                    var l      = _state.TransformX(Meta.ReadShort());
                    var cx     = (r + l) / 2;
                    var cy     = (t + b) / 2;
                    var arc1   = GetArc(cx, cy, xstart, ystart);
                    var arc2   = GetArc(cx, cy, xend, yend);
                    arc2 -= arc1;
                    if (arc2 <= 0)
                    {
                        arc2 += 360;
                    }

                    var ar = PdfContentByte.BezierArc(l, b, r, t, arc1, arc2);
                    if (ar.Count == 0)
                    {
                        break;
                    }

                    var pt = (float[])ar[0];
                    cx = pt[0];
                    cy = pt[1];
                    Cb.MoveTo(cx, cy);
                    for (var k = 0; k < ar.Count; ++k)
                    {
                        pt = (float[])ar[k];
                        Cb.CurveTo(pt[2], pt[3], pt[4], pt[5], pt[6], pt[7]);
                    }
                    Cb.LineTo(cx, cy);
                    StrokeAndFill();
                    break;
                }

                case META_RECTANGLE:
                {
                    if (IsNullStrokeFill(true))
                    {
                        break;
                    }

                    var b = _state.TransformY(Meta.ReadShort());
                    var r = _state.TransformX(Meta.ReadShort());
                    var t = _state.TransformY(Meta.ReadShort());
                    var l = _state.TransformX(Meta.ReadShort());
                    Cb.Rectangle(l, b, r - l, t - b);
                    StrokeAndFill();
                    break;
                }

                case META_ROUNDRECT:
                {
                    if (IsNullStrokeFill(true))
                    {
                        break;
                    }

                    var h = _state.TransformY(0) - _state.TransformY(Meta.ReadShort());
                    var w = _state.TransformX(Meta.ReadShort()) - _state.TransformX(0);
                    var b = _state.TransformY(Meta.ReadShort());
                    var r = _state.TransformX(Meta.ReadShort());
                    var t = _state.TransformY(Meta.ReadShort());
                    var l = _state.TransformX(Meta.ReadShort());
                    Cb.RoundRectangle(l, b, r - l, t - b, (h + w) / 4);
                    StrokeAndFill();
                    break;
                }

                case META_INTERSECTCLIPRECT:
                {
                    var b = _state.TransformY(Meta.ReadShort());
                    var r = _state.TransformX(Meta.ReadShort());
                    var t = _state.TransformY(Meta.ReadShort());
                    var l = _state.TransformX(Meta.ReadShort());
                    Cb.Rectangle(l, b, r - l, t - b);
                    Cb.EoClip();
                    Cb.NewPath();
                    break;
                }

                case META_EXTTEXTOUT:
                {
                    var y     = Meta.ReadShort();
                    var x     = Meta.ReadShort();
                    var count = Meta.ReadWord();
                    var flag  = Meta.ReadWord();
                    var x1    = 0;
                    var y1    = 0;
                    var x2    = 0;
                    var y2    = 0;
                    if ((flag & (MetaFont.ETO_CLIPPED | MetaFont.ETO_OPAQUE)) != 0)
                    {
                        x1 = Meta.ReadShort();
                        y1 = Meta.ReadShort();
                        x2 = Meta.ReadShort();
                        y2 = Meta.ReadShort();
                    }
                    var text = new byte[count];
                    int k;
                    for (k = 0; k < count; ++k)
                    {
                        var c = (byte)Meta.ReadByte();
                        if (c == 0)
                        {
                            break;
                        }

                        text[k] = c;
                    }
                    string s;
                    try
                    {
                        s = EncodingsRegistry.Instance.GetEncoding(1252).GetString(text, 0, k);
                    }
                    catch
                    {
                        s = System.Text.Encoding.ASCII.GetString(text, 0, k);
                    }
                    OutputText(x, y, flag, x1, y1, x2, y2, s);
                    break;
                }

                case META_TEXTOUT:
                {
                    var count = Meta.ReadWord();
                    var text  = new byte[count];
                    int k;
                    for (k = 0; k < count; ++k)
                    {
                        var c = (byte)Meta.ReadByte();
                        if (c == 0)
                        {
                            break;
                        }

                        text[k] = c;
                    }
                    string s;
                    try
                    {
                        s = EncodingsRegistry.Instance.GetEncoding(1252).GetString(text, 0, k);
                    }
                    catch
                    {
                        s = System.Text.Encoding.ASCII.GetString(text, 0, k);
                    }
                    count = (count + 1) & 0xfffe;
                    Meta.Skip(count - k);
                    var y = Meta.ReadShort();
                    var x = Meta.ReadShort();
                    OutputText(x, y, 0, 0, 0, 0, 0, s);
                    break;
                }

                case META_SETBKCOLOR:
                    _state.CurrentBackgroundColor = Meta.ReadColor();
                    break;

                case META_SETTEXTCOLOR:
                    _state.CurrentTextColor = Meta.ReadColor();
                    break;

                case META_SETTEXTALIGN:
                    _state.TextAlign = Meta.ReadWord();
                    break;

                case META_SETBKMODE:
                    _state.BackgroundMode = Meta.ReadWord();
                    break;

                case META_SETPOLYFILLMODE:
                    _state.PolyFillMode = Meta.ReadWord();
                    break;

                case META_SETPIXEL:
                {
                    var color = Meta.ReadColor();
                    var y     = Meta.ReadShort();
                    var x     = Meta.ReadShort();
                    Cb.SaveState();
                    Cb.SetColorFill(color);
                    Cb.Rectangle(_state.TransformX(x), _state.TransformY(y), .2f, .2f);
                    Cb.Fill();
                    Cb.RestoreState();
                    break;
                }

                case META_DIBSTRETCHBLT:
                case META_STRETCHDIB:
                {
                    var rop = Meta.ReadInt();
                    if (function == META_STRETCHDIB)
                    {
                        /*int usage = */
                        Meta.ReadWord();
                    }
                    var srcHeight  = Meta.ReadShort();
                    var srcWidth   = Meta.ReadShort();
                    var ySrc       = Meta.ReadShort();
                    var xSrc       = Meta.ReadShort();
                    var destHeight = _state.TransformY(Meta.ReadShort()) - _state.TransformY(0);
                    var destWidth  = _state.TransformX(Meta.ReadShort()) - _state.TransformX(0);
                    var yDest      = _state.TransformY(Meta.ReadShort());
                    var xDest      = _state.TransformX(Meta.ReadShort());
                    var b          = new byte[(tsize * 2) - (Meta.Length - lenMarker)];
                    for (var k = 0; k < b.Length; ++k)
                    {
                        b[k] = (byte)Meta.ReadByte();
                    }

                    try
                    {
                        var inb = new MemoryStream(b);
                        var bmp = BmpImage.GetImage(inb, true, b.Length);
                        Cb.SaveState();
                        Cb.Rectangle(xDest, yDest, destWidth, destHeight);
                        Cb.Clip();
                        Cb.NewPath();
                        bmp.ScaleAbsolute(destWidth * bmp.Width / srcWidth, -destHeight * bmp.Height / srcHeight);
                        bmp.SetAbsolutePosition(xDest - destWidth * xSrc / srcWidth, yDest + destHeight * ySrc / srcHeight - bmp.ScaledHeight);
                        Cb.AddImage(bmp);
                        Cb.RestoreState();
                    }
                    catch
                    {
                        // empty on purpose
                    }
                    break;
                }
                }
                Meta.Skip((tsize * 2) - (Meta.Length - lenMarker));
            }
            _state.Cleanup(Cb);
        }
Ejemplo n.º 2
0
        public void ReadAll()
        {
            if (meta.ReadInt() != unchecked ((int)0x9AC6CDD7))
            {
                throw new DocumentException(MessageLocalization.GetComposedMessage("not.a.placeable.windows.metafile"));
            }
            meta.ReadWord();
            left           = meta.ReadShort();
            top            = meta.ReadShort();
            right          = meta.ReadShort();
            bottom         = meta.ReadShort();
            inch           = meta.ReadWord();
            state.ScalingX = (float)(right - left) / (float)inch * 72f;
            state.ScalingY = (float)(bottom - top) / (float)inch * 72f;
            state.OffsetWx = left;
            state.OffsetWy = top;
            state.ExtentWx = right - left;
            state.ExtentWy = bottom - top;
            meta.ReadInt();
            meta.ReadWord();
            meta.Skip(18);

            int tsize;
            int function;

            cb.SetLineCap(1);
            cb.SetLineJoin(1);
            for (;;)
            {
                int lenMarker = meta.Length;
                tsize = meta.ReadInt();
                if (tsize < 3)
                {
                    break;
                }
                function = meta.ReadWord();
                switch (function)
                {
                case 0:
                    break;

                case META_CREATEPALETTE:
                case META_CREATEREGION:
                case META_DIBCREATEPATTERNBRUSH:
                    state.AddMetaObject(new MetaObject());
                    break;

                case META_CREATEPENINDIRECT:
                {
                    MetaPen pen = new MetaPen();
                    pen.Init(meta);
                    state.AddMetaObject(pen);
                    break;
                }

                case META_CREATEBRUSHINDIRECT:
                {
                    MetaBrush brush = new MetaBrush();
                    brush.Init(meta);
                    state.AddMetaObject(brush);
                    break;
                }

                case META_CREATEFONTINDIRECT:
                {
                    MetaFont font = new MetaFont();
                    font.Init(meta);
                    state.AddMetaObject(font);
                    break;
                }

                case META_SELECTOBJECT:
                {
                    int idx = meta.ReadWord();
                    state.SelectMetaObject(idx, cb);
                    break;
                }

                case META_DELETEOBJECT:
                {
                    int idx = meta.ReadWord();
                    state.DeleteMetaObject(idx);
                    break;
                }

                case META_SAVEDC:
                    state.SaveState(cb);
                    break;

                case META_RESTOREDC:
                {
                    int idx = meta.ReadShort();
                    state.RestoreState(idx, cb);
                    break;
                }

                case META_SETWINDOWORG:
                    state.OffsetWy = meta.ReadShort();
                    state.OffsetWx = meta.ReadShort();
                    break;

                case META_SETWINDOWEXT:
                    state.ExtentWy = meta.ReadShort();
                    state.ExtentWx = meta.ReadShort();
                    break;

                case META_MOVETO:
                {
                    int y = meta.ReadShort();
                    System.Drawing.Point p = new System.Drawing.Point(meta.ReadShort(), y);
                    state.CurrentPoint = p;
                    break;
                }

                case META_LINETO:
                {
                    int y = meta.ReadShort();
                    int x = meta.ReadShort();
                    System.Drawing.Point p = state.CurrentPoint;
                    cb.MoveTo(state.TransformX(p.X), state.TransformY(p.Y));
                    cb.LineTo(state.TransformX(x), state.TransformY(y));
                    cb.Stroke();
                    state.CurrentPoint = new System.Drawing.Point(x, y);
                    break;
                }

                case META_POLYLINE:
                {
                    state.LineJoinPolygon = cb;
                    int len = meta.ReadWord();
                    int x   = meta.ReadShort();
                    int y   = meta.ReadShort();
                    cb.MoveTo(state.TransformX(x), state.TransformY(y));
                    for (int k = 1; k < len; ++k)
                    {
                        x = meta.ReadShort();
                        y = meta.ReadShort();
                        cb.LineTo(state.TransformX(x), state.TransformY(y));
                    }
                    cb.Stroke();
                    break;
                }

                case META_POLYGON:
                {
                    if (IsNullStrokeFill(false))
                    {
                        break;
                    }
                    int len = meta.ReadWord();
                    int sx  = meta.ReadShort();
                    int sy  = meta.ReadShort();
                    cb.MoveTo(state.TransformX(sx), state.TransformY(sy));
                    for (int k = 1; k < len; ++k)
                    {
                        int x = meta.ReadShort();
                        int y = meta.ReadShort();
                        cb.LineTo(state.TransformX(x), state.TransformY(y));
                    }
                    cb.LineTo(state.TransformX(sx), state.TransformY(sy));
                    StrokeAndFill();
                    break;
                }

                case META_POLYPOLYGON:
                {
                    if (IsNullStrokeFill(false))
                    {
                        break;
                    }
                    int   numPoly = meta.ReadWord();
                    int[] lens    = new int[numPoly];
                    for (int k = 0; k < lens.Length; ++k)
                    {
                        lens[k] = meta.ReadWord();
                    }
                    for (int j = 0; j < lens.Length; ++j)
                    {
                        int len = lens[j];
                        int sx  = meta.ReadShort();
                        int sy  = meta.ReadShort();
                        cb.MoveTo(state.TransformX(sx), state.TransformY(sy));
                        for (int k = 1; k < len; ++k)
                        {
                            int x = meta.ReadShort();
                            int y = meta.ReadShort();
                            cb.LineTo(state.TransformX(x), state.TransformY(y));
                        }
                        cb.LineTo(state.TransformX(sx), state.TransformY(sy));
                    }
                    StrokeAndFill();
                    break;
                }

                case META_ELLIPSE:
                {
                    if (IsNullStrokeFill(state.LineNeutral))
                    {
                        break;
                    }
                    int b = meta.ReadShort();
                    int r = meta.ReadShort();
                    int t = meta.ReadShort();
                    int l = meta.ReadShort();
                    cb.Arc(state.TransformX(l), state.TransformY(b), state.TransformX(r), state.TransformY(t), 0, 360);
                    StrokeAndFill();
                    break;
                }

                case META_ARC:
                {
                    if (IsNullStrokeFill(state.LineNeutral))
                    {
                        break;
                    }
                    float yend   = state.TransformY(meta.ReadShort());
                    float xend   = state.TransformX(meta.ReadShort());
                    float ystart = state.TransformY(meta.ReadShort());
                    float xstart = state.TransformX(meta.ReadShort());
                    float b      = state.TransformY(meta.ReadShort());
                    float r      = state.TransformX(meta.ReadShort());
                    float t      = state.TransformY(meta.ReadShort());
                    float l      = state.TransformX(meta.ReadShort());
                    float cx     = (r + l) / 2;
                    float cy     = (t + b) / 2;
                    float arc1   = GetArc(cx, cy, xstart, ystart);
                    float arc2   = GetArc(cx, cy, xend, yend);
                    arc2 -= arc1;
                    if (arc2 <= 0)
                    {
                        arc2 += 360;
                    }
                    cb.Arc(l, b, r, t, arc1, arc2);
                    cb.Stroke();
                    break;
                }

                case META_PIE:
                {
                    if (IsNullStrokeFill(state.LineNeutral))
                    {
                        break;
                    }
                    float yend   = state.TransformY(meta.ReadShort());
                    float xend   = state.TransformX(meta.ReadShort());
                    float ystart = state.TransformY(meta.ReadShort());
                    float xstart = state.TransformX(meta.ReadShort());
                    float b      = state.TransformY(meta.ReadShort());
                    float r      = state.TransformX(meta.ReadShort());
                    float t      = state.TransformY(meta.ReadShort());
                    float l      = state.TransformX(meta.ReadShort());
                    float cx     = (r + l) / 2;
                    float cy     = (t + b) / 2;
                    float arc1   = GetArc(cx, cy, xstart, ystart);
                    float arc2   = GetArc(cx, cy, xend, yend);
                    arc2 -= arc1;
                    if (arc2 <= 0)
                    {
                        arc2 += 360;
                    }
                    List <float[]> ar = PdfContentByte.BezierArc(l, b, r, t, arc1, arc2);
                    if (ar.Count == 0)
                    {
                        break;
                    }
                    float[] pt = ar[0];
                    cb.MoveTo(cx, cy);
                    cb.LineTo(pt[0], pt[1]);
                    for (int k = 0; k < ar.Count; ++k)
                    {
                        pt = ar[k];
                        cb.CurveTo(pt[2], pt[3], pt[4], pt[5], pt[6], pt[7]);
                    }
                    cb.LineTo(cx, cy);
                    StrokeAndFill();
                    break;
                }

                case META_CHORD:
                {
                    if (IsNullStrokeFill(state.LineNeutral))
                    {
                        break;
                    }
                    float yend   = state.TransformY(meta.ReadShort());
                    float xend   = state.TransformX(meta.ReadShort());
                    float ystart = state.TransformY(meta.ReadShort());
                    float xstart = state.TransformX(meta.ReadShort());
                    float b      = state.TransformY(meta.ReadShort());
                    float r      = state.TransformX(meta.ReadShort());
                    float t      = state.TransformY(meta.ReadShort());
                    float l      = state.TransformX(meta.ReadShort());
                    float cx     = (r + l) / 2;
                    float cy     = (t + b) / 2;
                    float arc1   = GetArc(cx, cy, xstart, ystart);
                    float arc2   = GetArc(cx, cy, xend, yend);
                    arc2 -= arc1;
                    if (arc2 <= 0)
                    {
                        arc2 += 360;
                    }
                    List <float[]> ar = PdfContentByte.BezierArc(l, b, r, t, arc1, arc2);
                    if (ar.Count == 0)
                    {
                        break;
                    }
                    float[] pt = ar[0];
                    cx = pt[0];
                    cy = pt[1];
                    cb.MoveTo(cx, cy);
                    for (int k = 0; k < ar.Count; ++k)
                    {
                        pt = ar[k];
                        cb.CurveTo(pt[2], pt[3], pt[4], pt[5], pt[6], pt[7]);
                    }
                    cb.LineTo(cx, cy);
                    StrokeAndFill();
                    break;
                }

                case META_RECTANGLE:
                {
                    if (IsNullStrokeFill(true))
                    {
                        break;
                    }
                    float b = state.TransformY(meta.ReadShort());
                    float r = state.TransformX(meta.ReadShort());
                    float t = state.TransformY(meta.ReadShort());
                    float l = state.TransformX(meta.ReadShort());
                    cb.Rectangle(l, b, r - l, t - b);
                    StrokeAndFill();
                    break;
                }

                case META_ROUNDRECT:
                {
                    if (IsNullStrokeFill(true))
                    {
                        break;
                    }
                    float h = state.TransformY(0) - state.TransformY(meta.ReadShort());
                    float w = state.TransformX(meta.ReadShort()) - state.TransformX(0);
                    float b = state.TransformY(meta.ReadShort());
                    float r = state.TransformX(meta.ReadShort());
                    float t = state.TransformY(meta.ReadShort());
                    float l = state.TransformX(meta.ReadShort());
                    cb.RoundRectangle(l, b, r - l, t - b, (h + w) / 4);
                    StrokeAndFill();
                    break;
                }

                case META_INTERSECTCLIPRECT:
                {
                    float b = state.TransformY(meta.ReadShort());
                    float r = state.TransformX(meta.ReadShort());
                    float t = state.TransformY(meta.ReadShort());
                    float l = state.TransformX(meta.ReadShort());
                    cb.Rectangle(l, b, r - l, t - b);
                    cb.EoClip();
                    cb.NewPath();
                    break;
                }

                case META_EXTTEXTOUT:
                {
                    int y     = meta.ReadShort();
                    int x     = meta.ReadShort();
                    int count = meta.ReadWord();
                    int flag  = meta.ReadWord();
                    int x1    = 0;
                    int y1    = 0;
                    int x2    = 0;
                    int y2    = 0;
                    if ((flag & (MetaFont.ETO_CLIPPED | MetaFont.ETO_OPAQUE)) != 0)
                    {
                        x1 = meta.ReadShort();
                        y1 = meta.ReadShort();
                        x2 = meta.ReadShort();
                        y2 = meta.ReadShort();
                    }
                    byte[] text = new byte[count];
                    int    k;
                    for (k = 0; k < count; ++k)
                    {
                        byte c = (byte)meta.ReadByte();
                        if (c == 0)
                        {
                            break;
                        }
                        text[k] = c;
                    }
                    string s;
                    try {
                        s = System.Text.Encoding.GetEncoding(1252).GetString(text, 0, k);
                    }
                    catch  {
                        s = System.Text.ASCIIEncoding.ASCII.GetString(text, 0, k);
                    }
                    OutputText(x, y, flag, x1, y1, x2, y2, s);
                    break;
                }

                case META_TEXTOUT:
                {
                    int    count = meta.ReadWord();
                    byte[] text  = new byte[count];
                    int    k;
                    for (k = 0; k < count; ++k)
                    {
                        byte c = (byte)meta.ReadByte();
                        if (c == 0)
                        {
                            break;
                        }
                        text[k] = c;
                    }
                    string s;
                    try {
                        s = System.Text.Encoding.GetEncoding(1252).GetString(text, 0, k);
                    }
                    catch {
                        s = System.Text.ASCIIEncoding.ASCII.GetString(text, 0, k);
                    }
                    count = (count + 1) & 0xfffe;
                    meta.Skip(count - k);
                    int y = meta.ReadShort();
                    int x = meta.ReadShort();
                    OutputText(x, y, 0, 0, 0, 0, 0, s);
                    break;
                }

                case META_SETBKCOLOR:
                    state.CurrentBackgroundColor = meta.ReadColor();
                    break;

                case META_SETTEXTCOLOR:
                    state.CurrentTextColor = meta.ReadColor();
                    break;

                case META_SETTEXTALIGN:
                    state.TextAlign = meta.ReadWord();
                    break;

                case META_SETBKMODE:
                    state.BackgroundMode = meta.ReadWord();
                    break;

                case META_SETPOLYFILLMODE:
                    state.PolyFillMode = meta.ReadWord();
                    break;

                case META_SETPIXEL:
                {
                    BaseColor color = meta.ReadColor();
                    int       y     = meta.ReadShort();
                    int       x     = meta.ReadShort();
                    cb.SaveState();
                    cb.SetColorFill(color);
                    cb.Rectangle(state.TransformX(x), state.TransformY(y), .2f, .2f);
                    cb.Fill();
                    cb.RestoreState();
                    break;
                }

                case META_DIBSTRETCHBLT:
                case META_STRETCHDIB: {
                    int rop = meta.ReadInt();
                    if (function == META_STRETCHDIB)
                    {
                        /*int usage = */ meta.ReadWord();
                    }
                    int    srcHeight  = meta.ReadShort();
                    int    srcWidth   = meta.ReadShort();
                    int    ySrc       = meta.ReadShort();
                    int    xSrc       = meta.ReadShort();
                    float  destHeight = state.TransformY(meta.ReadShort()) - state.TransformY(0);
                    float  destWidth  = state.TransformX(meta.ReadShort()) - state.TransformX(0);
                    float  yDest      = state.TransformY(meta.ReadShort());
                    float  xDest      = state.TransformX(meta.ReadShort());
                    byte[] b          = new byte[(tsize * 2) - (meta.Length - lenMarker)];
                    for (int k = 0; k < b.Length; ++k)
                    {
                        b[k] = (byte)meta.ReadByte();
                    }
                    try {
                        MemoryStream inb = new MemoryStream(b);
                        Image        bmp = BmpImage.GetImage(inb, true, b.Length);
                        cb.SaveState();
                        cb.Rectangle(xDest, yDest, destWidth, destHeight);
                        cb.Clip();
                        cb.NewPath();
                        bmp.ScaleAbsolute(destWidth * bmp.Width / srcWidth, -destHeight * bmp.Height / srcHeight);
                        bmp.SetAbsolutePosition(xDest - destWidth * xSrc / srcWidth, yDest + destHeight * ySrc / srcHeight - bmp.ScaledHeight);
                        cb.AddImage(bmp);
                        cb.RestoreState();
                    }
                    catch {
                        // empty on purpose
                    }
                    break;
                }
                }
                meta.Skip((tsize * 2) - (meta.Length - lenMarker));
            }
            state.Cleanup(cb);
        }
Ejemplo n.º 3
0
        public void ReadAll()
        {
            if (meta.ReadInt() != unchecked((int)0x9AC6CDD7)) {
            throw new DocumentException(MessageLocalization.GetComposedMessage("not.a.placeable.windows.metafile"));
            }
            meta.ReadWord();
            left = meta.ReadShort();
            top = meta.ReadShort();
            right = meta.ReadShort();
            bottom = meta.ReadShort();
            inch = meta.ReadWord();
            state.ScalingX = (float)(right - left) / (float)inch * 72f;
            state.ScalingY = (float)(bottom - top) / (float)inch * 72f;
            state.OffsetWx = left;
            state.OffsetWy = top;
            state.ExtentWx = right - left;
            state.ExtentWy = bottom - top;
            meta.ReadInt();
            meta.ReadWord();
            meta.Skip(18);

            int tsize;
            int function;
            cb.SetLineCap(1);
            cb.SetLineJoin(1);
            for (;;) {
            int lenMarker = meta.Length;
            tsize = meta.ReadInt();
            if (tsize < 3)
                break;
            function = meta.ReadWord();
            switch (function) {
                case 0:
                    break;
                case META_CREATEPALETTE:
                case META_CREATEREGION:
                case META_DIBCREATEPATTERNBRUSH:
                    state.AddMetaObject(new MetaObject());
                    break;
                case META_CREATEPENINDIRECT:
                {
                    MetaPen pen = new MetaPen();
                    pen.Init(meta);
                    state.AddMetaObject(pen);
                    break;
                }
                case META_CREATEBRUSHINDIRECT:
                {
                    MetaBrush brush = new MetaBrush();
                    brush.Init(meta);
                    state.AddMetaObject(brush);
                    break;
                }
                case META_CREATEFONTINDIRECT:
                {
                    MetaFont font = new MetaFont();
                    font.Init(meta);
                    state.AddMetaObject(font);
                    break;
                }
                case META_SELECTOBJECT:
                {
                    int idx = meta.ReadWord();
                    state.SelectMetaObject(idx, cb);
                    break;
                }
                case META_DELETEOBJECT:
                {
                    int idx = meta.ReadWord();
                    state.DeleteMetaObject(idx);
                    break;
                }
                case META_SAVEDC:
                    state.SaveState(cb);
                    break;
                case META_RESTOREDC:
                {
                    int idx = meta.ReadShort();
                    state.RestoreState(idx, cb);
                    break;
                }
                case META_SETWINDOWORG:
                    state.OffsetWy = meta.ReadShort();
                    state.OffsetWx = meta.ReadShort();
                    break;
                case META_SETWINDOWEXT:
                    state.ExtentWy = meta.ReadShort();
                    state.ExtentWx = meta.ReadShort();
                    break;
                case META_MOVETO:
                {
                    int y = meta.ReadShort();
                    System.Drawing.Point p = new System.Drawing.Point(meta.ReadShort(), y);
                    state.CurrentPoint = p;
                    break;
                }
                case META_LINETO:
                {
                    int y = meta.ReadShort();
                    int x = meta.ReadShort();
                    System.Drawing.Point p = state.CurrentPoint;
                    cb.MoveTo(state.TransformX(p.X), state.TransformY(p.Y));
                    cb.LineTo(state.TransformX(x), state.TransformY(y));
                    cb.Stroke();
                    state.CurrentPoint = new System.Drawing.Point(x, y);
                    break;
                }
                case META_POLYLINE:
                {
                    state.LineJoinPolygon = cb;
                    int len = meta.ReadWord();
                    int x = meta.ReadShort();
                    int y = meta.ReadShort();
                    cb.MoveTo(state.TransformX(x), state.TransformY(y));
                    for (int k = 1; k < len; ++k) {
                        x = meta.ReadShort();
                        y = meta.ReadShort();
                        cb.LineTo(state.TransformX(x), state.TransformY(y));
                    }
                    cb.Stroke();
                    break;
                }
                case META_POLYGON:
                {
                    if (IsNullStrokeFill(false))
                        break;
                    int len = meta.ReadWord();
                    int sx = meta.ReadShort();
                    int sy = meta.ReadShort();
                    cb.MoveTo(state.TransformX(sx), state.TransformY(sy));
                    for (int k = 1; k < len; ++k) {
                        int x = meta.ReadShort();
                        int y = meta.ReadShort();
                        cb.LineTo(state.TransformX(x), state.TransformY(y));
                    }
                    cb.LineTo(state.TransformX(sx), state.TransformY(sy));
                    StrokeAndFill();
                    break;
                }
                case META_POLYPOLYGON:
                {
                    if (IsNullStrokeFill(false))
                        break;
                    int numPoly = meta.ReadWord();
                    int[] lens = new int[numPoly];
                    for (int k = 0; k < lens.Length; ++k)
                        lens[k] = meta.ReadWord();
                    for (int j = 0; j < lens.Length; ++j) {
                        int len = lens[j];
                        int sx = meta.ReadShort();
                        int sy = meta.ReadShort();
                        cb.MoveTo(state.TransformX(sx), state.TransformY(sy));
                        for (int k = 1; k < len; ++k) {
                            int x = meta.ReadShort();
                            int y = meta.ReadShort();
                            cb.LineTo(state.TransformX(x), state.TransformY(y));
                        }
                        cb.LineTo(state.TransformX(sx), state.TransformY(sy));
                    }
                    StrokeAndFill();
                    break;
                }
                case META_ELLIPSE:
                {
                    if (IsNullStrokeFill(state.LineNeutral))
                        break;
                    int b = meta.ReadShort();
                    int r = meta.ReadShort();
                    int t = meta.ReadShort();
                    int l = meta.ReadShort();
                    cb.Arc(state.TransformX(l), state.TransformY(b), state.TransformX(r), state.TransformY(t), 0, 360);
                    StrokeAndFill();
                    break;
                }
                case META_ARC:
                {
                    if (IsNullStrokeFill(state.LineNeutral))
                        break;
                    float yend = state.TransformY(meta.ReadShort());
                    float xend = state.TransformX(meta.ReadShort());
                    float ystart = state.TransformY(meta.ReadShort());
                    float xstart = state.TransformX(meta.ReadShort());
                    float b = state.TransformY(meta.ReadShort());
                    float r = state.TransformX(meta.ReadShort());
                    float t = state.TransformY(meta.ReadShort());
                    float l = state.TransformX(meta.ReadShort());
                    float cx = (r + l) / 2;
                    float cy = (t + b) / 2;
                    float arc1 = GetArc(cx, cy, xstart, ystart);
                    float arc2 = GetArc(cx, cy, xend, yend);
                    arc2 -= arc1;
                    if (arc2 <= 0)
                        arc2 += 360;
                    cb.Arc(l, b, r, t, arc1, arc2);
                    cb.Stroke();
                    break;
                }
                case META_PIE:
                {
                    if (IsNullStrokeFill(state.LineNeutral))
                        break;
                    float yend = state.TransformY(meta.ReadShort());
                    float xend = state.TransformX(meta.ReadShort());
                    float ystart = state.TransformY(meta.ReadShort());
                    float xstart = state.TransformX(meta.ReadShort());
                    float b = state.TransformY(meta.ReadShort());
                    float r = state.TransformX(meta.ReadShort());
                    float t = state.TransformY(meta.ReadShort());
                    float l = state.TransformX(meta.ReadShort());
                    float cx = (r + l) / 2;
                    float cy = (t + b) / 2;
                    float arc1 = GetArc(cx, cy, xstart, ystart);
                    float arc2 = GetArc(cx, cy, xend, yend);
                    arc2 -= arc1;
                    if (arc2 <= 0)
                        arc2 += 360;
                    List<float[]> ar = PdfContentByte.BezierArc(l, b, r, t, arc1, arc2);
                    if (ar.Count == 0)
                        break;
                    float[] pt = ar[0];
                    cb.MoveTo(cx, cy);
                    cb.LineTo(pt[0], pt[1]);
                    for (int k = 0; k < ar.Count; ++k) {
                        pt = ar[k];
                        cb.CurveTo(pt[2], pt[3], pt[4], pt[5], pt[6], pt[7]);
                    }
                    cb.LineTo(cx, cy);
                    StrokeAndFill();
                    break;
                }
                case META_CHORD:
                {
                    if (IsNullStrokeFill(state.LineNeutral))
                        break;
                    float yend = state.TransformY(meta.ReadShort());
                    float xend = state.TransformX(meta.ReadShort());
                    float ystart = state.TransformY(meta.ReadShort());
                    float xstart = state.TransformX(meta.ReadShort());
                    float b = state.TransformY(meta.ReadShort());
                    float r = state.TransformX(meta.ReadShort());
                    float t = state.TransformY(meta.ReadShort());
                    float l = state.TransformX(meta.ReadShort());
                    float cx = (r + l) / 2;
                    float cy = (t + b) / 2;
                    float arc1 = GetArc(cx, cy, xstart, ystart);
                    float arc2 = GetArc(cx, cy, xend, yend);
                    arc2 -= arc1;
                    if (arc2 <= 0)
                        arc2 += 360;
                    List<float[]> ar = PdfContentByte.BezierArc(l, b, r, t, arc1, arc2);
                    if (ar.Count == 0)
                        break;
                    float[] pt = ar[0];
                    cx = pt[0];
                    cy = pt[1];
                    cb.MoveTo(cx, cy);
                    for (int k = 0; k < ar.Count; ++k) {
                        pt = ar[k];
                        cb.CurveTo(pt[2], pt[3], pt[4], pt[5], pt[6], pt[7]);
                    }
                    cb.LineTo(cx, cy);
                    StrokeAndFill();
                    break;
                }
                case META_RECTANGLE:
                {
                    if (IsNullStrokeFill(true))
                        break;
                    float b = state.TransformY(meta.ReadShort());
                    float r = state.TransformX(meta.ReadShort());
                    float t = state.TransformY(meta.ReadShort());
                    float l = state.TransformX(meta.ReadShort());
                    cb.Rectangle(l, b, r - l, t - b);
                    StrokeAndFill();
                    break;
                }
                case META_ROUNDRECT:
                {
                    if (IsNullStrokeFill(true))
                        break;
                    float h = state.TransformY(0) - state.TransformY(meta.ReadShort());
                    float w = state.TransformX(meta.ReadShort()) - state.TransformX(0);
                    float b = state.TransformY(meta.ReadShort());
                    float r = state.TransformX(meta.ReadShort());
                    float t = state.TransformY(meta.ReadShort());
                    float l = state.TransformX(meta.ReadShort());
                    cb.RoundRectangle(l, b, r - l, t - b, (h + w) / 4);
                    StrokeAndFill();
                    break;
                }
                case META_INTERSECTCLIPRECT:
                {
                    float b = state.TransformY(meta.ReadShort());
                    float r = state.TransformX(meta.ReadShort());
                    float t = state.TransformY(meta.ReadShort());
                    float l = state.TransformX(meta.ReadShort());
                    cb.Rectangle(l, b, r - l, t - b);
                    cb.EoClip();
                    cb.NewPath();
                    break;
                }
                case META_EXTTEXTOUT:
                {
                    int y = meta.ReadShort();
                    int x = meta.ReadShort();
                    int count = meta.ReadWord();
                    int flag = meta.ReadWord();
                    int x1 = 0;
                    int y1 = 0;
                    int x2 = 0;
                    int y2 = 0;
                    if ((flag & (MetaFont.ETO_CLIPPED | MetaFont.ETO_OPAQUE)) != 0) {
                        x1 = meta.ReadShort();
                        y1 = meta.ReadShort();
                        x2 = meta.ReadShort();
                        y2 = meta.ReadShort();
                    }
                    byte[] text = new byte[count];
                    int k;
                    for (k = 0; k < count; ++k) {
                        byte c = (byte)meta.ReadByte();
                        if (c == 0)
                            break;
                        text[k] = c;
                    }
                    string s;
                    try {
                        s = System.Text.Encoding.GetEncoding(1252).GetString(text, 0, k);
                    }
                    catch  {
                        s = System.Text.ASCIIEncoding.ASCII.GetString(text, 0, k);
                    }
                    OutputText(x, y, flag, x1, y1, x2, y2, s);
                    break;
                }
                case META_TEXTOUT:
                {
                    int count = meta.ReadWord();
                    byte[] text = new byte[count];
                    int k;
                    for (k = 0; k < count; ++k) {
                        byte c = (byte)meta.ReadByte();
                        if (c == 0)
                            break;
                        text[k] = c;
                    }
                    string s;
                    try {
                        s = System.Text.Encoding.GetEncoding(1252).GetString(text, 0, k);
                    }
                    catch {
                        s = System.Text.ASCIIEncoding.ASCII.GetString(text, 0, k);
                    }
                    count = (count + 1) & 0xfffe;
                    meta.Skip(count - k);
                    int y = meta.ReadShort();
                    int x = meta.ReadShort();
                    OutputText(x, y, 0, 0, 0, 0, 0, s);
                    break;
                }
                case META_SETBKCOLOR:
                    state.CurrentBackgroundColor = meta.ReadColor();
                    break;
                case META_SETTEXTCOLOR:
                    state.CurrentTextColor = meta.ReadColor();
                    break;
                case META_SETTEXTALIGN:
                    state.TextAlign = meta.ReadWord();
                    break;
                case META_SETBKMODE:
                    state.BackgroundMode = meta.ReadWord();
                    break;
                case META_SETPOLYFILLMODE:
                    state.PolyFillMode = meta.ReadWord();
                    break;
                case META_SETPIXEL:
                {
                    BaseColor color = meta.ReadColor();
                    int y = meta.ReadShort();
                    int x = meta.ReadShort();
                    cb.SaveState();
                    cb.SetColorFill(color);
                    cb.Rectangle(state.TransformX(x), state.TransformY(y), .2f, .2f);
                    cb.Fill();
                    cb.RestoreState();
                    break;
                }
                case META_DIBSTRETCHBLT:
                case META_STRETCHDIB: {
                    int rop = meta.ReadInt();
                    if (function == META_STRETCHDIB) {
                        /*int usage = */ meta.ReadWord();
                    }
                    int srcHeight = meta.ReadShort();
                    int srcWidth = meta.ReadShort();
                    int ySrc = meta.ReadShort();
                    int xSrc = meta.ReadShort();
                    float destHeight = state.TransformY(meta.ReadShort()) - state.TransformY(0);
                    float destWidth = state.TransformX(meta.ReadShort()) - state.TransformX(0);
                    float yDest = state.TransformY(meta.ReadShort());
                    float xDest = state.TransformX(meta.ReadShort());
                    byte[] b = new byte[(tsize * 2) - (meta.Length - lenMarker)];
                    for (int k = 0; k < b.Length; ++k)
                        b[k] = (byte)meta.ReadByte();
                    try {
                        MemoryStream inb = new MemoryStream(b);
                        Image bmp = BmpImage.GetImage(inb, true, b.Length);
                        cb.SaveState();
                        cb.Rectangle(xDest, yDest, destWidth, destHeight);
                        cb.Clip();
                        cb.NewPath();
                        bmp.ScaleAbsolute(destWidth * bmp.Width / srcWidth, -destHeight * bmp.Height / srcHeight);
                        bmp.SetAbsolutePosition(xDest - destWidth * xSrc / srcWidth, yDest + destHeight * ySrc / srcHeight - bmp.ScaledHeight);
                        cb.AddImage(bmp);
                        cb.RestoreState();
                    }
                    catch {
                        // empty on purpose
                    }
                    break;
                }
            }
            meta.Skip((tsize * 2) - (meta.Length - lenMarker));
            }
            state.Cleanup(cb);
        }