Beispiel #1
0
        private void OnCoverChanged(CoverChangedArgs e)
        {
            if (CoverChanged != null)
            {
                CoverChanged(this, e);
                lblStatus.Text = e.ChangedCoverFile ? e.Message + SourceMessage : e.Message;

                //Update the new DefaultImage
                DefaultImage = e.Cover;
                BackupCover  = e.Cover;
            }
        }
        private bool FixHtml(CoverChangedArgs e)
        {
            bool FixedCoverWidth = false;
            PreserveAspectRatio = e.PreserveAspectRatio;

            string HtmlHeigth = ImageNode.GetAttributeValue("height", "");
            string HtmlWidth = ImageNode.GetAttributeValue("width", "");
            string HeigthValue = e.Heigth.ToString();
            string WidthValue = e.Width.ToString();

            //string SVGAspectValue = "xMidYMid meet";
            string SVGAspectValue = PreserveAspectRatio ? "xMidYMid meet" : "none";
            //string SVGHeigthValue = PreserveAspectRatio ? "100%" : "800";
            //string SVGWidthValue = PreserveAspectRatio ? "100%" : "600";
            string SVGHeigthValue = "100%";
            string SVGWidthValue = "100%";
            HtmlAttribute SVGAspectAttri = ImageNode.ParentNode.Attributes["preserveAspectRatio"];
            HtmlAttribute SVGHeigthAttri = ImageNode.ParentNode.Attributes["height"];
            HtmlAttribute SVGWidthAttri = ImageNode.ParentNode.Attributes["width"];

            HtmlNode body = ImageNode.OwnerDocument.DocumentNode.SelectSingleNode("//body");
            HtmlNodeCollection css = ImageNode.OwnerDocument.DocumentNode.SelectNodes("//head/link");
            HtmlNode BodyStyle = ImageNode.OwnerDocument.DocumentNode.SelectSingleNode("//head/style");
            string styleValue = "margin:0; padding: 0; border-width: 0";
            HtmlAttribute styleAttri = body.Attributes["style"];
            HtmlAttribute classAttri = body.Attributes["class"];

            if (HtmlHeigth != HeigthValue | HtmlWidth != WidthValue | (SVGAspectAttri != null && SVGAspectAttri.Value != SVGAspectValue) | (SVGHeigthAttri != null && SVGHeigthAttri.Value != SVGHeigthValue) | (SVGWidthAttri != null && SVGWidthAttri.Value != SVGWidthValue) | !ImageIsSVG | (styleAttri != null && styleAttri.Value != styleValue) | styleAttri == null | css != null | BodyStyle != null)
            {
                try
                {
                    ImageNode.SetAttributeValue("height", HeigthValue);
                    ImageNode.SetAttributeValue("width", WidthValue);

                    if (ImageIsSVG)
                    {
                        ImageNode.ParentNode.SetAttributeValue("preserveAspectRatio", SVGAspectValue);
                        ImageNode.ParentNode.Attributes["viewBox"].Value = "0 0 " + e.Width.ToString() + " " + e.Heigth.ToString();
                        ImageNode.ParentNode.SetAttributeValue("height", SVGHeigthValue);
                        ImageNode.ParentNode.SetAttributeValue("width", SVGWidthValue);

                        if (css != null)
                            css.ToList().ForEach(x => x.Remove());//Remove any css or xgpt file
                        if (BodyStyle != null)
                            BodyStyle.Remove();//Remove the style element
                        if (classAttri != null)
                            classAttri.Remove();//Delete the class attribute because it is no longer needed
                        body.SetAttributeValue("style", styleValue);//replace the style class with a style Attribute

                    } else
                    {

                        //If file is not SVG base Convert it and rerun the Fix
                        string SVGhtml = @"<svg xmlns=""http://www.w3.org/2000/svg"" xmlns:xlink=""http://www.w3.org/1999/xlink"" height=""800"" preserveAspectRatio=""none"" version=""1.1"" viewBox=""0 0 600 800"" width=""100%"">
              <image height=""800"" width=""100%"" xlink:href=""" + this.OriginalImageURL + @"""></image>
            </svg>";
                        string newHtml = ImageNode.OwnerDocument.DocumentNode.OuterHtml.Replace(ImageNode.OwnerDocument.DocumentNode.SelectSingleNode("//body").InnerHtml, SVGhtml);

                        //Reload ImageNode
                        HtmlDocument newDoc = new HtmlDocument();
                        newDoc.OptionDefaultStreamEncoding = Encoding.UTF8;
                        newDoc.LoadHtml(newHtml);
                        ImageNode = newDoc.DocumentNode.SelectSingleNode("//image");
                        ImageIsSVG = true;

                        FixHtml(e);
                    }

                    FixedCoverWidth = true;
                } catch (Exception) { }
            }

            return FixedCoverWidth;
        }
        private void ExportNewCover(object sender, CoverChangedArgs e)
        {
            using (new HourGlass())
            {
                if (e.Cover != null && ImageNode != null)
                {
                    bool ChangedCoverFile = false;

                    #region Replace Existing File with the New One if it is different
                    if (!ImageCompare(e.Cover, BookImage))
                    {
                        fileOutName = Zip.GetFilePathInsideZip(ImageURL);
                        fileOutStream = e.Cover.ToStream(GetImageType(BookImage));
                        UpdateZip();
                        SaveOpfFixToFile();
                        e.Message = SaveMessage;
                        ChangedCoverFile = true;
                    } else
                    {
                        e.Message = "File has not changed, Aborting";
                    }
                    #endregion

                    //Make sure it is scaled to fit
                    bool FixedCoverWidth = FixHtml(e);

                    if (FixedCoverWidth)
                    {
                        MyHtmlDoc.fileOutStream = MyHtmlDoc.TidyHtml(ImageNode.OwnerDocument.DocumentNode.OuterHtml).ToStream();
                        MyHtmlDoc.UpdateZip();

                        if (PreserveAspectRatio)
                            e.Message = !ChangedCoverFile ? "File has not changed, But Fixing the dimensions" : e.Message;
                        else
                            e.Message = !ChangedCoverFile ? "File has not changed, But making sure that it is scaled to fit" : e.Message;
                    }

                    #region If guide is empty add it
                    if (IsGuideEmpty)
                    {
                        MyOPFDoc.AddCoverRef(CoverFile);
                        e.Message = !ChangedCoverFile && !FixedCoverWidth ? "File has not changed, But fixing missing Cover Tag in guide" : e.Message;
                    }
                    #endregion

                    //Check to see if the cover file is the first
                    if (!Variables.MassUpdate)
                        CheckPositionOfCover();

                    //Update the stream and BookImage with the new default
                    GetImage();

                    e.ChangedCoverFile = ChangedCoverFile;
                } else
                {
                    e.Message = "Cover is Empty, Aborting";
                }
            }
        }
Beispiel #4
0
        private void ExportNewCover(object sender, CoverChangedArgs e)
        {
            using (new HourGlass())
            {
                if (e.Cover != null && ImageNode != null)
                {
                    bool ChangedCoverFile = false;

                    #region Replace Existing File with the New One if it is different
                    if (!ImageCompare(e.Cover, BookImage))
                    {
                        fileOutName   = Zip.GetFilePathInsideZip(ImageURL);
                        fileOutStream = e.Cover.ToStream(GetImageType(BookImage));
                        UpdateZip();
                        SaveOpfFixToFile();
                        e.Message        = SaveMessage;
                        ChangedCoverFile = true;
                    }
                    else
                    {
                        e.Message = "File has not changed, Aborting";
                    }
                    #endregion

                    //Make sure it is scaled to fit
                    bool FixedCoverWidth = FixHtml(e);

                    if (FixedCoverWidth)
                    {
                        MyHtmlDoc.fileOutStream = MyHtmlDoc.TidyHtml(ImageNode.OwnerDocument.DocumentNode.OuterHtml).ToStream();
                        MyHtmlDoc.UpdateZip();

                        if (PreserveAspectRatio)
                        {
                            e.Message = !ChangedCoverFile ? "File has not changed, But Fixing the dimensions" : e.Message;
                        }
                        else
                        {
                            e.Message = !ChangedCoverFile ? "File has not changed, But making sure that it is scaled to fit" : e.Message;
                        }
                    }

                    #region If guide is empty add it
                    if (IsGuideEmpty)
                    {
                        MyOPFDoc.AddCoverRef(CoverFile);
                        e.Message = !ChangedCoverFile && !FixedCoverWidth ? "File has not changed, But fixing missing Cover Tag in guide" : e.Message;
                    }
                    #endregion

                    //Check to see if the cover file is the first
                    if (!Variables.MassUpdate)
                    {
                        CheckPositionOfCover();
                    }

                    //Update the stream and BookImage with the new default
                    GetImage();

                    e.ChangedCoverFile = ChangedCoverFile;
                }
                else
                {
                    e.Message = "Cover is Empty, Aborting";
                }
            }
        }
Beispiel #5
0
        private bool FixHtml(CoverChangedArgs e)
        {
            bool FixedCoverWidth = false;

            PreserveAspectRatio = e.PreserveAspectRatio;

            string HtmlHeigth  = ImageNode.GetAttributeValue("height", "");
            string HtmlWidth   = ImageNode.GetAttributeValue("width", "");
            string HeigthValue = e.Heigth.ToString();
            string WidthValue  = e.Width.ToString();

            //string SVGAspectValue = "xMidYMid meet";
            string SVGAspectValue = PreserveAspectRatio ? "xMidYMid meet" : "none";
            //string SVGHeigthValue = PreserveAspectRatio ? "100%" : "800";
            //string SVGWidthValue = PreserveAspectRatio ? "100%" : "600";
            string        SVGHeigthValue = "100%";
            string        SVGWidthValue  = "100%";
            HtmlAttribute SVGAspectAttri = ImageNode.ParentNode.Attributes["preserveAspectRatio"];
            HtmlAttribute SVGHeigthAttri = ImageNode.ParentNode.Attributes["height"];
            HtmlAttribute SVGWidthAttri  = ImageNode.ParentNode.Attributes["width"];

            HtmlNode           body       = ImageNode.OwnerDocument.DocumentNode.SelectSingleNode("//body");
            HtmlNodeCollection css        = ImageNode.OwnerDocument.DocumentNode.SelectNodes("//head/link");
            HtmlNode           BodyStyle  = ImageNode.OwnerDocument.DocumentNode.SelectSingleNode("//head/style");
            string             styleValue = "margin:0; padding: 0; border-width: 0";
            HtmlAttribute      styleAttri = body.Attributes["style"];
            HtmlAttribute      classAttri = body.Attributes["class"];

            if (HtmlHeigth != HeigthValue | HtmlWidth != WidthValue | (SVGAspectAttri != null && SVGAspectAttri.Value != SVGAspectValue) | (SVGHeigthAttri != null && SVGHeigthAttri.Value != SVGHeigthValue) | (SVGWidthAttri != null && SVGWidthAttri.Value != SVGWidthValue) | !ImageIsSVG | (styleAttri != null && styleAttri.Value != styleValue) | styleAttri == null | css != null | BodyStyle != null)
            {
                try
                {
                    ImageNode.SetAttributeValue("height", HeigthValue);
                    ImageNode.SetAttributeValue("width", WidthValue);

                    if (ImageIsSVG)
                    {
                        ImageNode.ParentNode.SetAttributeValue("preserveAspectRatio", SVGAspectValue);
                        ImageNode.ParentNode.Attributes["viewBox"].Value = "0 0 " + e.Width.ToString() + " " + e.Heigth.ToString();
                        ImageNode.ParentNode.SetAttributeValue("height", SVGHeigthValue);
                        ImageNode.ParentNode.SetAttributeValue("width", SVGWidthValue);

                        if (css != null)
                        {
                            css.ToList().ForEach(x => x.Remove());//Remove any css or xgpt file
                        }
                        if (BodyStyle != null)
                        {
                            BodyStyle.Remove();//Remove the style element
                        }
                        if (classAttri != null)
                        {
                            classAttri.Remove();                     //Delete the class attribute because it is no longer needed
                        }
                        body.SetAttributeValue("style", styleValue); //replace the style class with a style Attribute
                    }
                    else
                    {
                        //If file is not SVG base Convert it and rerun the Fix
                        string SVGhtml = @"<svg xmlns=""http://www.w3.org/2000/svg"" xmlns:xlink=""http://www.w3.org/1999/xlink"" height=""800"" preserveAspectRatio=""none"" version=""1.1"" viewBox=""0 0 600 800"" width=""100%"">
      <image height=""800"" width=""100%"" xlink:href=""" + this.OriginalImageURL + @"""></image>
    </svg>";
                        string newHtml = ImageNode.OwnerDocument.DocumentNode.OuterHtml.Replace(ImageNode.OwnerDocument.DocumentNode.SelectSingleNode("//body").InnerHtml, SVGhtml);

                        //Reload ImageNode
                        HtmlDocument newDoc = new HtmlDocument();
                        newDoc.OptionDefaultStreamEncoding = Encoding.UTF8;
                        newDoc.LoadHtml(newHtml);
                        ImageNode  = newDoc.DocumentNode.SelectSingleNode("//image");
                        ImageIsSVG = true;

                        FixHtml(e);
                    }

                    FixedCoverWidth = true;
                } catch (Exception) { }
            }

            return(FixedCoverWidth);
        }
        private void OnCoverChanged(CoverChangedArgs e)
        {
            if (CoverChanged != null)
            {
                CoverChanged(this, e);
                lblStatus.Text = e.ChangedCoverFile ? e.Message + SourceMessage : e.Message;

                //Update the new DefaultImage
                DefaultImage = e.Cover;
                BackupCover = e.Cover;
            }
        }