Beispiel #1
0
        public override void ExportThread()
        {
            if (File.Exists(m_sExportFile))
            {
                try
                {
                    File.Delete(m_sExportFile);
                }
                catch (Exception)
                {
                    ProgressReporter.AddIssue("Failed to delete PDF before export: {0}".FormatString(m_sExportFile));
                }

                if (File.Exists(m_sExportFile))
                {
                    DisplayError();
                    ProgressReporter.Shutdown();
                    return;
                }
            }

#if !MONO_BUILD
            Bitmap zBuffer = null;
#endif

            var progressLayoutIdx = ProgressReporter.GetProgressIndex(ProgressName.LAYOUT);
            var progressCardIdx   = ProgressReporter.GetProgressIndex(ProgressName.CARD);

            ProgressReporter.ProgressReset(progressLayoutIdx, 0, ExportLayoutIndices.Length, 0);

            // always add a new page initially (necessary for ConfigurePointSizes)
            AddInitialPage();

            Type zLastExporterType = null;

            foreach (var nIdx in ExportLayoutIndices)
            {
                ChangeExportLayoutIndex(nIdx);
                m_zExportData.Deck = CurrentDeck;
                if (CurrentDeck.EmptyReference)
                {
                    // empty reference layouts are not exported
                    ProgressReporter.ProgressStep(progressLayoutIdx);
                    continue;
                }

                ProgressReporter.ProgressReset(progressCardIdx, 0, CurrentDeck.CardCount, 0);

                var rectCrop = CurrentDeck.CardLayout.getExportCropDefinition();

                // each layout has its own row export style (potentially)
                var zRowExporter = GetRowExporter();

                // necessary tracking for which index of the layout is to be exported (this is NOT necessarily the index of the card due to the page back functionality)
                var nNextExportIndex = 0;

                // reconfigure the page per layout
                ConfigurePointSizes(CurrentDeck.CardLayout, rectCrop);

                if (nIdx == 0)
                {
                    // only the page has been setup at this point, not the x-position
                    zRowExporter.SetupNewRowXPosition(nNextExportIndex);
                }
                else
                {
                    // after the first layout always evaluate whether a new page should be added
                    // if the exporter type changes start a new page
                    if (CardMakerSettings.PrintLayoutsOnNewPage || zLastExporterType != zRowExporter.GetType())
                    {
                        AddPage(zRowExporter, nNextExportIndex);
                    }
                    else if (zRowExporter.IsLayoutForcedToNewRow() || zRowExporter.IsRowFull())
                    {
                        MoveToNextRow(zRowExporter, nNextExportIndex);
                    }
                }

                zLastExporterType = zRowExporter.GetType();

                // When moving to the next row it should be a completely empty row. In the case of nesting only 2 layouts deep is supported at this
                // time - the first layout and one additional. Also note this is applied AFTER the above decisions above about moving to the next row.
                m_zExportData.NextRowYAdjust = Math.Max(m_zExportData.NextRowYAdjust, m_zExportData.LayoutPointHeight + m_zExportData.BufferY);

#if !MONO_BUILD
                zBuffer?.Dispose();
                zBuffer = createExportBuffer(CurrentDeck.CardLayout, rectCrop);

                var fOriginalXDpi = zBuffer.HorizontalResolution;
                var fOriginalYDpi = zBuffer.VerticalResolution;
#endif

                foreach (var nCardIdx in GetExportIndices())
                {
                    CurrentDeck.CardPrintIndex = nCardIdx;

#if MONO_BUILD
                    // mono build won't support the optimization so re-create the buffer
                    Bitmap zBuffer = createExportBuffer(CurrentDeck.CardLayout, rectCrop);
#else
                    // minor optimization, reuse the same bitmap (for drawing sake the DPI has to be reset)
                    zBuffer.SetResolution(fOriginalXDpi, fOriginalYDpi);
#endif
                    // Draw the image into the buffer
                    if (nCardIdx == -1)
                    {
                        Graphics.FromImage(zBuffer).FillRectangle(Brushes.White, 0, 0, zBuffer.Width, zBuffer.Height);
                        // note: some oddities were observed where the buffer was not flood filling
                    }
                    else
                    {
                        CardRenderer.DrawPrintLineToGraphics(Graphics.FromImage(zBuffer), -rectCrop.X, -rectCrop.Y, true);
                        // if cropping the border needs to be drawn to the cropped size
                        if (rectCrop != Rectangle.Empty)
                        {
                            CardRenderer.DrawBorder(Graphics.FromImage(zBuffer), 0, 0, zBuffer.Width, zBuffer.Height, CurrentDeck.CardLayout, true);
                        }
                    }

                    // apply any export rotation
                    ProcessRotateExport(zBuffer, CurrentDeck.CardLayout, false);

                    // before rendering to the PDF bump the DPI to the desired value
                    zBuffer.SetResolution(CurrentDeck.CardLayout.dpi, CurrentDeck.CardLayout.dpi);

                    var xImage = XImage.FromGdiPlusImage(zBuffer);

                    // before drawing make sure there is space (will move to next row/page)
                    EvaluateDrawLocation(nNextExportIndex, zRowExporter);

                    m_zPageGfx.DrawImage(xImage, m_zExportData.DrawX, m_zExportData.DrawY);

                    // any next row movement is dealt with independent of this call
                    zRowExporter.MoveXToNextColumnPosition();

                    // undo any export rotation
                    ProcessRotateExport(zBuffer, CurrentDeck.CardLayout, true);

                    nNextExportIndex++;

                    ProgressReporter.ProgressStep(progressCardIdx);
                }
                ProgressReporter.ProgressStep(progressLayoutIdx);
            }

#if !MONO_BUILD
            zBuffer?.Dispose();
#endif

            try
            {
                m_zDocument.Save(m_sExportFile);
                ProgressReporter.ThreadSuccess = true;
            }
            catch (Exception ex)
            {
                DisplayError(ex.Message);
                ProgressReporter.ThreadSuccess = false;
            }

            ProgressReporter.Shutdown();
        }
Beispiel #2
0
        public override void ExportThread()
        {
            if (File.Exists(m_sExportFile))
            {
                try
                {
                    File.Delete(m_sExportFile);
                }
                catch (Exception)
                {
                    ProgressReporter.AddIssue("Failed to delete PDF before export: {0}".FormatString(m_sExportFile));
                }

                if (File.Exists(m_sExportFile))
                {
                    DisplayError();
                    ProgressReporter.Shutdown();
                    return;
                }
            }

#if !MONO_BUILD
            Bitmap zBuffer = null;
#endif

            var progressLayoutIdx = ProgressReporter.GetProgressIndex(ProgressName.LAYOUT);
            var progressCardIdx   = ProgressReporter.GetProgressIndex(ProgressName.CARD);

            ProgressReporter.ProgressReset(progressLayoutIdx, 0, ExportLayoutIndices.Length, 0);
            foreach (var nIdx in ExportLayoutIndices)
            {
                ChangeExportLayoutIndex(nIdx);
                if (CurrentDeck.EmptyReference)
                {
                    // empty reference layouts are not exported
                    ProgressReporter.ProgressStep(progressLayoutIdx);
                    continue;
                }

                ProgressReporter.ProgressReset(progressCardIdx, 0, CurrentDeck.CardCount, 0);

                var rectCrop = CurrentDeck.CardLayout.getExportCropDefinition();

                ConfigurePointSizes(CurrentDeck.CardLayout, rectCrop);

                // necessary tracking for which index of the layout is to be exported (this is NOT necessarily the index of the card due to the page back functionality)
                var nNextExportIndex = 0;

                if (0 < nIdx)
                {
                    if (CardMakerSettings.PrintLayoutsOnNewPage)
                    {
                        AddPage();
                    }

                    if (CardMakerSettings.PrintAutoHorizontalCenter ||
                        (m_dDrawX + m_dLayoutPointWidth > m_dPageMarginEndX)) // this is the case where a layout won't fit in the remaining space of the row
                    {
                        MoveToNextRow(nNextExportIndex);
                    }
                }

                // should be adjusted after the above move to next row
                m_dNextRowYAdjust = Math.Max(m_dNextRowYAdjust, m_dLayoutPointHeight + m_dBufferY);

                if (CardMakerSettings.PrintAutoHorizontalCenter)
                {
                    CenterLayoutPositionOnNewRow(nNextExportIndex);
                }

#if !MONO_BUILD
                zBuffer?.Dispose();
                zBuffer = createExportBuffer(CurrentDeck.CardLayout, rectCrop);

                float fOriginalXDpi = zBuffer.HorizontalResolution;
                float fOriginalYDpi = zBuffer.VerticalResolution;
#endif

                foreach (var nCardIdx in GetExportIndices())
                {
                    CurrentDeck.ResetDeckCache();

                    CurrentDeck.CardPrintIndex = nCardIdx;

#if MONO_BUILD
                    // mono build won't support the optimization so re-create the buffer
                    Bitmap zBuffer = createExportBuffer(CurrentDeck.CardLayout, rectCrop);
#endif

#if !MONO_BUILD
                    // minor optimization, reuse the same bitmap (for drawing sake the DPI has to be reset)
                    zBuffer.SetResolution(fOriginalXDpi, fOriginalYDpi);
#endif
                    if (nCardIdx == -1)
                    {
                        Graphics.FromImage(zBuffer).FillRectangle(Brushes.White, 0, 0, zBuffer.Width, zBuffer.Height);
                        // note: some oddities were observed where the buffer was not flood filling
                    }
                    else
                    {
                        CardRenderer.DrawPrintLineToGraphics(Graphics.FromImage(zBuffer), -rectCrop.X, -rectCrop.Y, true);
                        // if cropping the border needs to be drawn to the cropped size
                        if (rectCrop != Rectangle.Empty)
                        {
                            CardRenderer.DrawBorder(Graphics.FromImage(zBuffer), 0, 0, zBuffer.Width, zBuffer.Height, CurrentDeck.CardLayout, true);
                        }
                    }

                    // apply any export rotation
                    ProcessRotateExport(zBuffer, CurrentDeck.CardLayout, false);

                    // before rendering to the PDF bump the DPI to the desired value
                    zBuffer.SetResolution(CurrentDeck.CardLayout.dpi, CurrentDeck.CardLayout.dpi);

                    var xImage = XImage.FromGdiPlusImage(zBuffer);

                    EvaluatePagePosition(nNextExportIndex);

                    m_zPageGfx.DrawImage(xImage, m_dDrawX, m_dDrawY);

                    MoveToNextColumnPosition();

                    // undo any export rotation
                    ProcessRotateExport(zBuffer, CurrentDeck.CardLayout, true);

                    nNextExportIndex++;

                    ProgressReporter.ProgressStep(progressCardIdx);
                }
                ProgressReporter.ProgressStep(progressLayoutIdx);
            }

#if !MONO_BUILD
            zBuffer?.Dispose();
#endif

            try
            {
                m_zDocument.Save(m_sExportFile);
                ProgressReporter.ThreadSuccess = true;
            }
            catch (Exception ex)
            {
                DisplayError(ex.Message);
                ProgressReporter.ThreadSuccess = false;
            }

            ProgressReporter.Shutdown();
        }