Ejemplo n.º 1
0
        public OutputCol NewCol()
        {
            Point offset = new Point();

            offset.Y = absolutePad;
            offset.X = absolutePad + myCol.Sum((c) => absoluteSpace + c.getArea.Width);

            OutputCol col = new OutputCol(colSize, offset);

            myCol.Add(col);
            // Console.WriteLine("New COL");
            return(col);
        }
Ejemplo n.º 2
0
        public List <OutputPage> saveCahpterFragmentsInto_PNG_LTR(
            List <PageFragmnet> allFragments, string prefix, string outputFolder, DrawMock mock = null)
        {
            bool dryRun = (mock != null);

            Size      virtualPage   = new Size(pageSize.Width, (int)(pageSize.Height * (1 - repeatColumnPercent * 0.01f)));
            int       repeatHeight  = pageSize.Height - virtualPage.Height;
            Rectangle virtualRepeat = new Rectangle(0, 0, virtualPage.Width, repeatHeight);

            // Split to parts
            List <OutputPage> outputPages = new List <OutputPage>();
            int fragmentIndex             = 0;

            while (fragmentIndex < allFragments.Count)
            {
                //Console.WriteLine("New Page");
                OutputPage face = new OutputPage(columnCount, virtualPage, padPercent, spacePercent);
                outputPages.Add(face);

                for (int c = 0; c < columnCount && fragmentIndex < allFragments.Count; c++)
                {
                    OutputCol col       = face.NewCol();
                    bool      firstFrag = true;
                    while (!col.isFull() && fragmentIndex < allFragments.Count)
                    {
                        col.addFragment(allFragments[fragmentIndex], firstFrag);
                        firstFrag = false;
                        if (allFragments[fragmentIndex].isFull())
                        {
                            fragmentIndex++;
                        }
                    }
                }
            }

            if (dryRun)
            {
                var jsonString = JsonConvert.SerializeObject(
                    outputPages, Formatting.Indented,
                    new JsonConverter[] { new StringEnumConverter() });
                Console.WriteLine(jsonString);
            }

            // Width bigger just because we dont know col width yet.
            Bitmap repeatCol = new Bitmap(virtualPage.Width, repeatHeight);

            using (var rG = System.Drawing.Graphics.FromImage(repeatCol))
            {
                rG.FillRectangle(Brushes.Gray, virtualRepeat);

                // Print to file
                int  colUniqueIndex = 0;
                int  pageIndex      = 0;
                bool hasRepeat      = false;
                foreach (var page in outputPages)
                {
                    log.i("Creating new page..");
                    using (Bitmap faceBit = dryRun ? new Bitmap(1, 1) : new Bitmap(pageSize.Width, pageSize.Height))
                    {
                        using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(faceBit))
                        {
                            // White background
                            if (!dryRun)
                            {
                                g.FillRectangle(Brushes.White, new Rectangle(0, 0, pageSize.Width, pageSize.Height));
                            }

                            Rectangle lastPartialShiftedTarget = new Rectangle(0, 0, 0, 0);
                            foreach (var col in page.GetCols)
                            {
                                log.i("Creating new cols..");

                                // Handle also the shifting:
                                Rectangle actualFullCol = shiftYRectangle(substractYRectangle(col.getArea, repeatHeight), repeatHeight);

                                if (dryRun)
                                {
                                    mock?.setSize(colUniqueIndex, col.getArea);
                                }
                                else
                                {
                                    g.DrawRectangle(borderPen, actualFullCol); // Col border
                                }
                                if (hasRepeat)
                                {
                                    // Print repeat from last col
                                    g.DrawImage(
                                        repeatCol,
                                        new Rectangle(actualFullCol.Location, new Size(actualFullCol.Width, repeatHeight))
                                        , virtualRepeat, GraphicsUnit.Pixel);
                                }

                                foreach (var part in col.getPrintSources)
                                {
                                    if (dryRun)
                                    {
                                        mock?.draw(colUniqueIndex, part.PartialTarget);
                                    }
                                    else
                                    {
                                        log.i("Creating frag from '" + part.filename + "'");
                                        using (Image fragSource = Bitmap.FromFile(part.filename))
                                        {
                                            // Shifted by substracting Y
                                            lastPartialShiftedTarget = shiftYRectangle(part.PartialTarget, repeatHeight);
                                            g.DrawImage(fragSource, lastPartialShiftedTarget, part.PartialSource, GraphicsUnit.Pixel);
                                            hasRepeat = true;
                                        }
                                    }
                                }

                                // Save last repeat:
                                Rectangle repeatTarget = lastPartialShiftedTarget;
                                repeatTarget.Y      = lastPartialShiftedTarget.Y + lastPartialShiftedTarget.Height - repeatHeight;
                                repeatTarget.Height = repeatHeight;
                                rG.DrawImage(faceBit, virtualRepeat, repeatTarget, GraphicsUnit.Pixel);
                                colUniqueIndex++;
                            }
                        }

                        if (!dryRun)
                        {
                            string saveFile = Path.Combine(outputFolder, string.Format("{0}page{1:0000}.png", prefix, pageIndex));
                            log.i("Saving to '" + saveFile + "'");
                            faceBit.Save(saveFile);
                        }

                        pageIndex++;
                    }
                }

                repeatCol.Dispose();
                return(outputPages);
            }
        }