Ejemplo n.º 1
0
 public override void Run()
 {
     ICollection<double> chapters = this.Theory.Chapters;
     StripCanvasSize scs = this.Manager.GenerateStripCanvasSize (chapters.Count);
     double dw = scs.CanvasSize.Width;
     double dh = scs.CanvasSize.Height;
     using (PdfSurface surface = new PdfSurface (this.Manager.OutputFile, scs.TotalWidth, scs.TotalHeight)) {
         using (Context ctx = new Context (surface)) {
             int index = 0x00;
             IPoint3 p;
             foreach (double chapter in chapters) {
                 p = scs.GetCanvasOffset (index);
                 ctx.Save ();
                 ctx.Translate (p.X, p.Y);
                 ctx.Rectangle (0.0d, 0.0d, dw, dh);
                 ctx.Stroke ();
                 this.Theory.Time = chapter;
                 CairoEngine engine = new CairoEngine (this.Theory);
                 engine.Context = ctx;
                 engine.Process ();
                 ctx.Restore ();
                 index++;
             }
         }
     }
 }
Ejemplo n.º 2
0
    public static void Write(string fileName)
    {
        Diagram diagram = Program.Load(fileName);
        string pdfName = System.IO.Path.ChangeExtension(fileName, "pdf");

        if(diagram != null)
        {
            try
            {
                using(PdfSurface pdfs = new PdfSurface(pdfName, 595, 842))
                {
                    using(Context cr = new Context(pdfs))
                    {
                        DiagramRenderer dr = new DiagramRenderer(cr, 0, 0, diagram);

                        cr.LineWidth = 1.0;
                        cr.Color = new Color(0.0, 0.0, 0.0);
                        dr.Render();
                        pdfs.SetSize(dr.width, dr.height);
                        dr.Draw();

                        cr.ShowPage();
                    }
                }

                Console.WriteLine("Wrote {0}", pdfName);
            }
            catch
            {
                Console.WriteLine("Error writing {0}", pdfName);
            }
        }
    }
Ejemplo n.º 3
0
 public static void ExportToPdf(this Report report ,string path)
 {
     using (PdfSurface pdfSurface = new PdfSurface (
         path,report.WidthWithMargins,report.HeightWithMargins)) {
         Cairo.Context cr = new Cairo.Context (pdfSurface);
         cr.Translate(report.Margin.Left,report.Margin.Top);
         ReportRenderer renderer = new ReportRenderer (){ Context = cr};
         renderer.RegisterRenderer (typeof(TextBlock), new TextBlockRenderer ());
         renderer.RegisterRenderer (typeof(Line), new LineRenderer ());
         renderer.RegisterRenderer (typeof(Image), new ImageRenderer (){ PixbufRepository = new PixbufRepository(report.ResourceRepository)});
         SectionRenderer sr = new SectionRenderer();
         renderer.RegisterRenderer(typeof(ReportHeaderSection), sr);
         renderer.RegisterRenderer(typeof(ReportFooterSection), sr);
         renderer.RegisterRenderer(typeof(DetailSection), sr);
         renderer.RegisterRenderer(typeof(PageHeaderSection), sr);
         renderer.RegisterRenderer(typeof(PageFooterSection), sr);
         MonoReports.Model.Engine.ReportEngine engine = new MonoReports.Model.Engine.ReportEngine (report,renderer);
         engine.Process ();
         for (int i = 0; i < report.Pages.Count; ++i) {
             renderer.RenderPage (report.Pages [i]);
             cr.ShowPage ();
         }
         pdfSurface.Finish ();
         (cr as IDisposable).Dispose ();
     }
 }
Ejemplo n.º 4
0
 public override void Run()
 {
     StripCanvasSize scs = this.Manager.GenerateStripCanvasSize (0x01);
     using (PdfSurface surface = new PdfSurface (this.Manager.OutputFile, scs.TotalWidth, scs.TotalHeight)) {
         using (Context ctx = new Context (surface)) {
             IPoint3 p = scs.GetCanvasOffset (0x00);
             ctx.Save ();
             ctx.Translate (p.X, p.Y);
             this.Theory.Time = this.Manager.Time;
             CairoEngine engine = new CairoEngine (this.Theory);
             engine.Context = ctx;
             engine.Process ();
             ctx.Restore ();
         }
     }
 }
Ejemplo n.º 5
0
		public static void Main(string[] args)
		{
			// call the snippets
			Snippets snip = new Snippets();
			Surface surface = new PdfSurface("snippets.pdf", IMAGE_WIDTH, IMAGE_WIDTH);
			Context cr = new Context(surface);

			foreach (string snippet in Snippets.snippets)
			{
				cr.Save();
				Snippets.InvokeSnippet(snip, snippet, cr, IMAGE_WIDTH, IMAGE_HEIGHT);
				cr.ShowPage ();
				cr.Restore();
			}
			surface.Finish ();
		}
Ejemplo n.º 6
0
        public static bool GeneratePdf(Game [] games, int games_page, string file)
        {
            int columns, rows;
            switch (games_page) {
            case 1:
                columns = 1;
                rows = 1;
                break;
            case 2:
                columns = 2;
                rows = 1;
                break;
            case 4:
                columns = 2;
                rows = 2;
                break;
            default:
                throw new InvalidOperationException ("Invalid games per page value");
            }

            try {

                PdfSurface pdf = new PdfSurface (file, page_width * columns, page_height * rows);

                if (pdf.Status != Status.Success)
                    return false;

                CairoContextEx cr = new CairoContextEx (pdf, "sans 12", 72);

                GenerateQuestions (cr, games, columns, rows);
                GenerateAnswers (cr, games, columns, rows);

                pdf.Finish ();
                ((IDisposable)cr).Dispose();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
Ejemplo n.º 7
0
 private void tool_saveaspdf(object sender, EventArgs e)
 {
     using(FileChooserDialog fcd_pdf = new FileChooserDialog("PDF File",this,FileChooserAction.Save)) {
         FileFilter ff = new FileFilter();
         ff.AddPattern("*.pdf");
         ff.Name = "PDF file";
         fcd_pdf.AddFilter(ff);
         fcd_pdf.AddButton("OK",ResponseType.Ok);
         int result = fcd_pdf.Run();
         if(result == (int) ResponseType.Ok) {
             PointD siz = this.sketchpad.MeasureSize();
             using(PdfSurface pdfs = new PdfSurface(fcd_pdf.Filename,siz.X,siz.Y)) {
                 using(Context ctx = new Context(pdfs)) {
                     this.sketchpad.PaintContext(ctx,(int) Math.Ceiling(siz.X),(int) Math.Ceiling(siz.Y));
                 }
             }
         }
         fcd_pdf.HideAll();
     }
 }
Ejemplo n.º 8
0
 public CairoGraphics(string pdfFile, double width, double height)
 {
     Surface surf = new PdfSurface(pdfFile, width, height);
     cr = new Context(surf);
 }
Ejemplo n.º 9
0
        public void Export(string filename, ExportFormat format)
        {
            this.UnselectAll();

            double leftMost   = double.MaxValue,
                   rightMost  = 0,
                   topMost 	  = double.MaxValue,
                   bottomMost = 0;

            Shape leftLayer   = new Shape(),
                  rightLayer  = new Shape(),
                  topLayer    = new Shape(),
                  bottomLayer = new Shape();

            foreach(Shape l in this)
            {
                if(l.Position.X + l.Boundaries.Width / 2 > rightMost)
                {
                    rightMost =  l.Position.X + l.Boundaries.Width / 2;
                    rightLayer = l;
                }
                if(l.Position.X - l.Boundaries.Width / 2 < leftMost)
                {
                    leftMost = l.Position.X - l.Boundaries.Width / 2;
                    leftLayer = l;
                }
                if((l.Position.Y - l.Boundaries.Height / 2) < topMost)
                {
                    topMost = l.Position.Y - l.Boundaries.Height / 2;
                    topLayer = l;
                }
                if(l.Position.Y + l.Boundaries.Height > bottomMost)
                {
                    bottomMost = l.Position.Y + l.Boundaries.Height;
                    bottomLayer = l;
                }
            }

            Size size;

            //TODO: to keep or not to keep the 1px border, that is the question.
            size.Width = (rightLayer.Position.X + rightLayer.Boundaries.Width / 2) -
                         (leftLayer.Position.X - leftLayer.Boundaries.Width / 2) + 1;

            size.Height = (bottomLayer.Position.Y + bottomLayer.Boundaries.Height / 2) -
                          (topLayer.Position.Y - topLayer.Boundaries.Height / 2) + 1;

            Surface surface;

            //TODO: each surface is handling the size differently... BUT WHY?
            switch(format)
            {
                case ExportFormat.SVG:
                    surface = new SvgSurface(filename, size.Width, size.Height);
                    break;
                case ExportFormat.PDF:
                    surface = new PdfSurface(filename, size.Width, size.Height);
                    break;
                case ExportFormat.PNG:
                    surface = new ImageSurface(Format.ARGB32,
                                           	   (int)size.Width,
                                               (int)size.Height);
                    break;
                default:
                    surface = new SvgSurface(filename, size.Width, size.Height);
                    break;
            }

            Cairo.Context c = new Context(surface);

            c.Translate(-leftLayer.Position.X + leftLayer.Boundaries.Width / 2,
                        -(topLayer.Position.Y - topLayer.Boundaries.Height / 2));

            this.Draw(c);

            if(format == ExportFormat.PNG)
                surface.WriteToPng(filename);
            surface.Finish();

            c.Target.Dispose();

            ((IDisposable) c).Dispose ();
        }
Ejemplo n.º 10
0
        public static void ExportToPdf(this Report report ,string path)
        {
            report.EvalDataSource();
            double unitMultiplier = CairoExtensions.UnitMultiplier;
            double realFontMultiplier = CairoExtensions.RealFontMultiplier;
            ReportRenderer renderer = new ReportRenderer ();
            renderer.ResolutionX = 72;

            using (PdfSurface pdfSurface = new PdfSurface (
                path,report.WidthWithMargins * renderer.UnitMultipilier,
                report.HeightWithMargins * renderer.UnitMultipilier)) {

                Cairo.Context cr = new Cairo.Context (pdfSurface);
                cr.Antialias = Antialias.None;
                renderer.Context = cr;
                renderer.RegisterRenderer (typeof(TextBlock), new TextBlockRenderer ());
                renderer.RegisterRenderer (typeof(Line), new LineRenderer ());
                PixbufRepository pbr = new PixbufRepository();
                pbr.Report = report;

                renderer.RegisterRenderer (typeof(Image), new ImageRenderer (){PixbufRepository = pbr});
                SectionRenderer sr = new SectionRenderer();
                renderer.RegisterRenderer(typeof(ReportHeaderSection), sr);
                renderer.RegisterRenderer(typeof(ReportFooterSection), sr);
                renderer.RegisterRenderer(typeof(DetailSection), sr);
                renderer.RegisterRenderer(typeof(PageHeaderSection), sr);
                renderer.RegisterRenderer(typeof(PageFooterSection), sr);

                MonoReports.Model.Engine.ReportEngine engine = new MonoReports.Model.Engine.ReportEngine (report,renderer);
                engine.Process ();

                Cairo.Context cr1 = new Cairo.Context (pdfSurface);
                renderer.Context = cr1;
                cr1.Translate(report.Margin.Left * renderer.UnitMultipilier,report.Margin.Top * renderer.UnitMultipilier);
                engine.RenderPages(renderer,report);
                pdfSurface.Finish ();
                (cr as IDisposable).Dispose ();
                (cr1 as IDisposable).Dispose ();
            }

            CairoExtensions.UnitMultiplier = unitMultiplier;
            CairoExtensions.RealFontMultiplier = realFontMultiplier;
        }
Ejemplo n.º 11
0
        /*
        [XmlIgnore]
        public FontFamily font;
        */
        public void InternalRegenerateCloud()
        {
            r = new Random();
            int id = 0;
            double minSize = Entries.Min(e => e.SizeValue);
            double maxSize = Entries.Max(e => e.SizeValue);
            double range = Math.Max(0.00001, maxSize - minSize);

            _source = new ImageSurface(Format.Argb32,ActualWidth,ActualHeight);
            pixelss = _source.Data;
            g = new Context(_source);
            g.Color = new Color(1.0,1.0,1.0);
            //g.Rectangle(0.0,0.0,ActualWidth,ActualHeight);
            g.Paint();

            g.SelectFontFace(fontname,FontSlant.Normal,FontWeight.Bold);
            g.SetFontSize(100.0);
            TextExtents te = g.TextExtents("x");
            double areaPerLetter = te.Width;

            strides = _source.Stride;
            int arraySize = (int)((ActualWidth / 4) + 2) * (int)((ActualHeight / 4) + 2);
            _mapping = new int[arraySize];
            for (int i = 0; i < arraySize; i++) _mapping[i] = -1;

            if(svgfn != null) {
                svgs = new SvgSurface(svgfn,ActualWidth,ActualHeight);
                svg = new Context(svgs);
                svg.SelectFontFace(fontname,FontSlant.Normal,FontWeight.Bold);
            }
            if(pdffn != null) {
                pdfs = new PdfSurface(pdffn,ActualWidth,ActualHeight);
                pdf = new Context(pdfs);
                pdf.SelectFontFace(fontname,FontSlant.Normal,FontWeight.Bold);
            }

            double fontMultiplier;

            if(CoverRatio > 0.0) {
                double totalsize = 0.0;
                MinFontSize = 24.0; // use big fonts for testing
                if(FontMaxRatio < 1.0 || FontMaxRatio > 500.0) FontMaxRatio = 6.0;
                MaxFontSize = FontMaxRatio*MinFontSize;
                fontMultiplier = (MaxFontSize - MinFontSize) / range;
                foreach(WordCloudEntry e in Entries) {
                    float fontsize = (float)(((e.SizeValue - minSize) * fontMultiplier) + MinFontSize);
                    g.SetFontSize(fontsize);
                    TextExtents te1 = g.TextExtents(e.Word);
                    totalsize += te1.Width*te1.Height;
                }
                double actualsize = ActualHeight*ActualWidth;
                double ratio1 = totalsize / actualsize; //this should be == CoverRatio
                //we assume that totalsize ~ MinFontSize^2 in this case
                MinFontSize = MinFontSize*Math.Sqrt(CoverRatio / ratio1);
                MaxFontSize = FontMaxRatio*MinFontSize;
                fontMultiplier = (MaxFontSize - MinFontSize) / range;
                LargestSizeWidthProportion = 0.9;
            }

            double targetWidth = Math.Max(ActualWidth, ActualHeight) * LargestSizeWidthProportion;
            WordCloudEntry od = Entries.OrderByDescending(e => (e.SizeValue - minSize) * e.Word.Length).First();
            double maxFontSize;
            if(MaxFontSize > 0.0 && CoverRatio <= 0.0) {
                maxFontSize = MaxFontSize;
                if(maxFontSize < MinFontSize) maxFontSize = MinFontSize*2.7;
            }
            else {
                g.SetFontSize(100.0);
                TextExtents te2 = g.TextExtents(od.Word);
                maxFontSize = 97.0*targetWidth / te2.Width;
                //maxFontSize = Math.Max(MinFontSize * 2.7, 100.0 / ((od.Word.Length * areaPerLetter) / targetWidth));
                if(maxFontSize > MinFontSize + 100) maxFontSize = MinFontSize + 100;
            }
            if(CoverRatio > 0.0) {
                if(maxFontSize < MaxFontSize) MaxFontSize = maxFontSize;
                fontMultiplier = (maxFontSize - MinFontSize) / range;
            }
            else fontMultiplier = (maxFontSize - MinFontSize) / range;

            var points = new[]
                             {
                                 new Point((int) (ActualWidth/2), (int) (ActualHeight/2)),
                                 new Point((int) (ActualWidth/4), (int) (ActualHeight/4)),
                                 new Point((int) (ActualWidth/4), (int) (3*ActualHeight/2)),
                                 new Point((int) (3*ActualWidth/4), (int) (ActualHeight/2)),
                                 new Point((int) (3*ActualWidth/4), (int) (3*ActualHeight/4))
                             };

            int currentPoint = 0;
            foreach (WordCloudEntry e in Entries.OrderByDescending(e => e.SizeValue))
            {
            again:
                double position = 0.0;
                Point centre = points[currentPoint];

                double angle = 0.0;
                if(vratio > 0.0)
                {
                    if(r.NextDouble() < vratio) angle = 90.0;
                }
                float fontsize = (float)(((e.SizeValue - minSize) * fontMultiplier) + MinFontSize);
                ImageSurface bm;
            imgretry:
                bm = CreateImage(e.Word,fontsize,e.Color,angle);
                // test if it fits
                if(angle == 90.0) {
                    if(bm.Height > ActualHeight) {
                        if(ActualWidth > ActualHeight) {
                            angle = 0.0;
                            bm.Destroy();
                            bm = null;
                            goto imgretry;
                        }
                        // crop the end
                        ImageSurface bm2 = new ImageSurface(Format.Argb32,bm.Width,(int)(ActualHeight*0.95));
                        Context g2 = new Context(bm2);
                        g2.SetSource(bm,0,ActualHeight-bm.Height);
                        g2.Paint();
                        ((IDisposable)g2).Dispose();
                        bm.Destroy();
                        bm = bm2;
                    }
                }
                else {
                    if(bm.Width > ActualWidth) {
                        if(ActualHeight > ActualWidth) {
                            angle = 90.0;
                            bm.Destroy();
                            bm = null;
                            goto imgretry;
                        }
                        // crop the end
                        ImageSurface bm2 = new ImageSurface(Format.Argb32,(int)(ActualWidth*0.95),bm.Height);
                        Context g2 = new Context(bm2);
                        g2.SetSource(bm,0,0);
                        g2.Paint();
                        ((IDisposable)g2).Dispose();
                        bm.Destroy();
                        bm = bm2;
                    }
                }
                //WordBitmap bm2 = new WordBitmap(bm);
                Dictionary<Point, List<Point>> lst = CreateCollisionList(bm);
                bool collided = true;
                do
                {
                    Point spiralPoint = GetSpiralPoint(position);
                    int offsetX = (bm.Width / 2);
                    int offsetY = (bm.Height / 2);
                    var testPoint = new Point((int)(spiralPoint.X + centre.X - offsetX), (int)(spiralPoint.Y + centre.Y - offsetY));
                    if (position > (2 * Math.PI) * 580)
                    {
                        //bm2.Dispose();
                        if (++currentPoint >= points.Length)
                            goto done;
              					goto again;
                    }
                    int cols = CountCollisions(testPoint, lst);
                    if (cols == 0)
                    {
                    tryagain:
                        double oldY = testPoint.Y;
                        if (Math.Abs(testPoint.X + offsetX - centre.X) > 10)
                        {
                            if (testPoint.X + offsetX < centre.X)
                            {
                                do
                                {
                                    testPoint.X += 2;
                                } while (testPoint.X + offsetX < centre.X && CountCollisions(testPoint, lst) == 0);
                                testPoint.X -= 2;
                            }
                            else
                            {
                                do
                                {
                                    testPoint.X -= 2;
                                } while (testPoint.X + offsetX > centre.X && CountCollisions(testPoint, lst) == 0);
                                testPoint.X += 2;
                            }
                        }
                        if (Math.Abs(testPoint.Y + offsetY - centre.Y) > 10)
                        {
                            if (testPoint.Y + offsetY < centre.Y)
                            {
                                do
                                {
                                    testPoint.Y += 2;
                                } while (testPoint.Y + offsetY < centre.Y && CountCollisions(testPoint, lst) == 0);
                                testPoint.Y -= 2;
                            }
                            else
                            {
                                do
                                {
                                    testPoint.Y -= 2;
                                } while (testPoint.Y + offsetY > centre.Y && CountCollisions(testPoint, lst) == 0);
                                testPoint.Y += 2;
                            }
                            if (testPoint.Y != oldY)
                                goto tryagain;
                        }

                        collided = false;
                        CopyBits(testPoint, bm, lst, Entries.IndexOf(e));
                        Console.Error.WriteLine("id: {0}, word: {1}, score: {7}, fontsize: {8}," +
                            "x: {2}, y: {3}, w: {4}, h: {5}, angle: {6}",id,e.Word,testPoint.X,testPoint.Y,
                                             bm.Width,bm.Height,angle,e.SizeValue,fontsize);
                        id++;
                        bm.Destroy();
                        if(svg != null) {
                            svg.SetFontSize(fontsize);
                            TextExtents te2 = svg.TextExtents(e.Word);
                            svg.Save();

                            if(angle == 90.0) {
                                svg.MoveTo(testPoint.X-te2.YBearing,testPoint.Y+te2.Width+te2.XBearing);
                                svg.Rotate(-0.5*Math.PI);
                            }
                            else {
                                svg.MoveTo(testPoint.X-te2.XBearing,testPoint.Y-te2.YBearing);
                            }
                            svg.Color = e.Color;
                            svg.ShowText(e.Word);
                            svg.Restore();
                        }
                        if(pdf != null) {
                            pdf.SetFontSize(fontsize);
                            TextExtents te2 = pdf.TextExtents(e.Word);
                            pdf.Save();

                            if(angle == 90.0) {
                                pdf.MoveTo(testPoint.X-te2.YBearing,testPoint.Y+te2.Width+te2.XBearing);
                                pdf.Rotate(-0.5*Math.PI);
                            }
                            else {
                                pdf.MoveTo(testPoint.X-te2.XBearing,testPoint.Y-te2.YBearing);
                            }
                            pdf.Color = e.Color;
                            pdf.ShowText(e.Word);
                            pdf.Restore();
                        }
                    }
                    else
                    {
                        if (cols <= 2)
                        {
                            position += (2 * Math.PI) / 100;
                        }
                        else

                            position += (2 * Math.PI) / 40;
                    }
                } while (collided);
            }
            done:
            ((IDisposable)g).Dispose();
            if(svgfn != null) ((IDisposable)svg).Dispose();
            if(pdffn != null) ((IDisposable)pdf).Dispose();
            Console.Error.WriteLine("# {0} words placed",id);
        }