Exemplo n.º 1
0
        public void Background_RadialGradientBrushTest()
        {
            var style = new StyleDefn();

            style.Background.ImageSource = "radial-gradient(red, green)";

            var target = style.CreateBackgroundBrush();

            Assert.IsNotNull(target);
            Assert.IsInstanceOfType(target, typeof(PDFGradientRadialBrush));
            PDFGradientRadialBrush gradient = (PDFGradientRadialBrush)target;

            Assert.AreEqual(2, gradient.Colors.Length);
            Assert.AreEqual(false, gradient.Repeating);
            Assert.AreEqual(FillType.Pattern, gradient.FillStyle);

            style = new StyleDefn();
            style.Background.ImageSource = "repeating-radial-gradient(circle, red 10%, green 15%, yellow 25%)";

            target = style.CreateBackgroundBrush();

            Assert.IsNotNull(target);
            Assert.IsInstanceOfType(target, typeof(PDFGradientRadialBrush));
            gradient = (PDFGradientRadialBrush)target;
            Assert.AreEqual(RadialShape.Circle, gradient.Shape);
            Assert.AreEqual(3, gradient.Colors.Length);
            Assert.AreEqual(true, gradient.Repeating);
            Assert.AreEqual(FillType.Pattern, gradient.FillStyle);
        }
Exemplo n.º 2
0
        public void IsCatchAllStyleTest()
        {
            StyleDefn target   = new StyleDefn();
            bool      expected = true;
            bool      actual;

            actual = target.IsCatchAllStyle();
            Assert.AreEqual(expected, actual);

            target.AppliedClass = "AclassName";
            expected            = false;
            actual = target.IsCatchAllStyle();
            Assert.AreEqual(expected, actual);

            target           = new StyleDefn();
            target.AppliedID = "AnId";
            expected         = false;
            actual           = target.IsCatchAllStyle();
            Assert.AreEqual(expected, actual);

            target             = new StyleDefn();
            target.AppliedType = typeof(Scryber.Components.Label);
            expected           = false;
            actual             = target.IsCatchAllStyle();
            Assert.AreEqual(expected, actual);


            target.AppliedType = null;
            expected           = true;
            actual             = target.IsCatchAllStyle();
            Assert.AreEqual(expected, actual);
        }
        public void StylePriorityCompound_Test()
        {
            StyleDefn defn;
            Document  doc = new Document();

            doc.ElementName = "doc:Document";

            defn              = new StyleDefn();
            defn.Match        = "doc:Div.red"; //higher priority
            defn.Border.Color = "#00FFFF";
            defn.Margins.Top  = 20;
            defn.Margins.Left = 20;
            doc.Styles.Add(defn);

            defn              = new StyleDefn();
            defn.Match        = "doc:Page.green .red"; //highest style priority
            defn.Border.Color = "#FF0000";
            defn.Border.Width = 3;
            defn.Margins.Left = 30;
            doc.Styles.Add(defn);

            defn              = new StyleDefn();
            defn.Match        = "doc:Page.blue .red"; //should be ignored
            defn.Border.Color = "#FFFFFF";
            defn.Border.Width = 10;
            defn.Margins.Left = 10;
            doc.Styles.Add(defn);

            defn              = new StyleDefn(); //Base applied style
            defn.Match        = "doc:Div";
            defn.Margins.Left = 10;
            defn.Margins.All  = 20;
            defn.Fill.Color   = "#00FF00";
            defn.Border.Color = "#0000FF"; //Overriden by both

            doc.Styles.Add(defn);


            Page pg = new Page();

            pg.ElementName = "doc:Page";
            pg.StyleClass  = "green";
            doc.Pages.Add(pg);

            Div div = new Div();

            div.ElementName = "doc:Div";
            div.StyleClass  = "red";
            div.BorderWidth = 5; //Highest priority
            pg.Contents.Add(div);

            var style = div.GetAppliedStyle();

            Assert.AreEqual(5, style.Border.Width.PointsValue, "Div border width should be 5");
            Assert.AreEqual((PDFColor)"#FF0000", style.Border.Color, "Fill colour should be green from the doc:Div style");

            Assert.AreEqual(20, style.Margins.Top.PointsValue, "Top margin should be 30 from 'doc:Div.red'");
            Assert.AreEqual(30, style.Margins.Left.PointsValue, "Left margin should be 10 from 'doc:Div'");
            Assert.AreEqual(20, style.Margins.Bottom.PointsValue, "Bottom should be 20 from 'doc:Div' Margins.All");
        }
Exemplo n.º 4
0
        public void MergeIntoTest()
        {
            Scryber.Components.Label lbl = new Scryber.Components.Label();
            lbl.StyleClass = "myclass";
            lbl.ID         = "myId";

            StyleDefn target = new StyleDefn();

            target.Background.Color = Scryber.Drawing.PDFColors.Aqua;

            Style style = new Style();

            style.Background.Color = Scryber.Drawing.PDFColors.Blue;

            //matched should have aqua applied, non-matching should stay blue


            //match applied type
            target.AppliedType = typeof(Scryber.Components.Label);
            target.MergeInto(style, lbl, ComponentState.Normal);

            style.Flatten();
            Assert.AreEqual(Scryber.Drawing.PDFColors.Aqua, style.Background.Color);

            //non-matching applied type
            target.AppliedType = typeof(Scryber.Components.TableCell);
            style = new Style();
            style.Background.Color = Scryber.Drawing.PDFColors.Blue;

            target.MergeInto(style, lbl, ComponentState.Normal);
            Assert.AreEqual(Scryber.Drawing.PDFColors.Blue, style.Background.Color);
        }
        public void NestedClassMatch_Test()
        {
            int priority;

            var defn = new StyleDefn();

            defn.Match = ".red .blue";

            var div1 = new Div();

            div1.StyleClass = "red";

            var div2 = new Div();

            div2.StyleClass = "blue";
            div1.Contents.Add(div2);

            var result = defn.IsMatchedTo(div2, out priority);

            Assert.IsTrue(result, "The inner div was not matched as expected");


            //Switch the parent to .green and it should not match

            div1.StyleClass = "green";
            result          = defn.IsMatchedTo(div2, out priority);
            Assert.IsFalse(result, "The inner div was matched - not as expected");
        }
Exemplo n.º 6
0
        public void ParseCSSWithVariables()
        {
            //1. Initial to make sure it is parsed, but should not be used

            string cssWithVariable = @"

                :root{
                    color: #00FF00;
                    --main-color: #FF0000;
                }

                .other{
                    color: var(--main-color);
                }";

            using (var doc = BuildDocumentWithStyles(cssWithVariable))
            {
                //Check that the variable is there.
                Assert.AreEqual(2, doc.Styles.Count);
                StyleDefn defn = doc.Styles[0] as StyleDefn;

                Assert.IsTrue(defn.HasVariables);
                Assert.AreEqual(1, defn.Variables.Count);
                Assert.AreEqual("--main-color", defn.Variables["--main-color"].CssName);
                Assert.AreEqual("main-color", defn.Variables["--main-color"].NormalizedName);
                Assert.AreEqual("#FF0000", defn.Variables["--main-color"].Value);

                //Should not be applied
                var applied = doc.GetAppliedStyle();
                Assert.AreEqual("rgb(0,255,0)", applied.Fill.Color.ToString());
            }
        }
        public void MultiClassIsMatch_Test()
        {
            int priority;

            var defn = new StyleDefn();

            defn.Match = "doc:Div.red";


            var div = new Div();

            div.StyleClass  = "red blue";
            div.ElementName = "doc:Div";
            var result = defn.IsMatchedTo(div, out priority);

            Assert.IsTrue(result, "The definition did not match the component");

            div.StyleClass = "blue red";
            result         = defn.IsMatchedTo(div, out priority);
            Assert.IsTrue(result, "The definition did not match the component");

            div.StyleClass = "blue red green";
            result         = defn.IsMatchedTo(div, out priority);
            Assert.IsTrue(result, "The definition did not match the component");
        }
        public void ItemTest()
        {
            StyleCollection target = new StyleCollection();
            StyleDefn       defn   = new StyleDefn();

            defn.Border.Color = PDFColors.Red;
            defn.Border.Width = 10;
            target.Add(defn);

            StyleDefn defn2 = new StyleDefn();

            defn2.Border.Color = PDFColors.Gray;
            defn2.Border.Width = 2;
            target.Add(defn2);

            int       index    = 0;
            StyleBase expected = defn;
            StyleBase actual   = target[index];

            Assert.AreEqual(expected, actual);

            index    = 1;
            expected = defn2;
            actual   = target[index];
            Assert.AreEqual(expected, actual);

            expected      = defn;
            target[index] = defn;
            actual        = target[index];
            Assert.AreEqual(expected, actual);
        }
        private void InitStylesDocument(StylesDocument target)
        {
            StyleDefn defn = new StyleDefn();

            defn.AppliedType  = typeof(Label);
            defn.AppliedClass = "sea";

            defn.Border.Color    = PDFColors.Red;
            defn.Border.Width    = 10;
            defn.Font.FontFamily = (PDFFontSelector)"Helvetica";
            target.Styles.Add(defn);

            StyleDefn defn2 = new StyleDefn();

            defn2.AppliedClass = "sea";

            defn2.Border.Color        = PDFColors.Gray;
            defn2.Columns.ColumnCount = 3;
            target.Styles.Add(defn2);

            StyleDefn defn3 = new StyleDefn();

            defn3.AppliedClass = "other";
            defn3.Border.Width = 20;
            defn3.Stroke.Color = PDFColors.Aqua;
            target.Styles.Add(defn3);
        }
        private StyleDefn CreateAlternateStyle()
        {
            StyleDefn style = new StyleDefn();

            style.Padding.All          = 10;
            style.Background.Color     = Scryber.Drawing.PDFColors.Yellow;
            style.Background.FillStyle = Scryber.Drawing.FillType.Pattern;
            return(style);
        }
Exemplo n.º 11
0
        public void AppliedTypeTest()
        {
            StyleDefn target   = new StyleDefn();
            Type      expected = typeof(Scryber.Components.TableCell);
            Type      actual;

            target.AppliedType = expected;
            actual             = target.AppliedType;
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 12
0
        public void AppliedStateTest()
        {
            StyleDefn      target   = new StyleDefn();
            ComponentState expected = ComponentState.Down;
            ComponentState actual;

            target.AppliedState = expected;
            actual = target.AppliedState;
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 13
0
        public void AppliedClassTest()
        {
            StyleDefn target   = new StyleDefn();
            string    expected = "myclass";
            string    actual;

            target.AppliedClass = expected;
            actual = target.AppliedClass;
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 14
0
        public void AppliedIDTest()
        {
            StyleDefn target   = new StyleDefn();
            string    expected = "myID";
            string    actual;

            target.AppliedID = expected;
            actual           = target.AppliedID;
            Assert.AreEqual(expected, actual);
        }
        public void MergeIntoTest()
        {
            string         classname = "sea";
            StylesDocument target    = new StylesDocument();

            // same class, same type = applied
            StyleDefn defn = new StyleDefn();

            defn.AppliedType  = typeof(Label);
            defn.AppliedClass = classname;

            defn.Border.Color    = PDFColors.Red;
            defn.Border.Width    = 10;
            defn.Font.FontFamily = (PDFFontSelector)"Helvetica";
            target.Styles.Add(defn);

            // same class no type = applied (lower priority)
            StyleDefn defn2 = new StyleDefn();

            defn2.AppliedClass = classname;

            defn2.Border.Color        = PDFColors.Gray;
            defn2.Columns.ColumnCount = 3;
            target.Styles.Add(defn2);

            // different class = not applied
            StyleDefn defn3 = new StyleDefn();

            defn3.AppliedClass = "other";
            defn3.Border.Width = 20;
            defn3.Stroke.Color = PDFColors.Aqua;
            target.Styles.Add(defn3);

            //same class but different type = not applied
            StyleDefn defn4 = new StyleDefn();

            defn4.AppliedClass    = classname;
            defn4.AppliedType     = typeof(Image);
            defn4.Font.FontFamily = (PDFFontSelector)"Symbol";
            target.Styles.Add(defn4);



            Style actual = new Style();
            Label lbl    = new Label();

            lbl.StyleClass = classname;
            target.MergeInto(actual, lbl, ComponentState.Normal);
            actual.Flatten();

            Assert.AreEqual(PDFColors.Red, actual.Border.Color); //from defn (higher priority than defn2)
            Assert.AreEqual((PDFUnit)10, actual.Border.Width);   // from defn
            Assert.AreEqual(3, actual.Columns.ColumnCount);      //from defn2
            Assert.AreEqual((PDFFontSelector)"Helvetica", actual.Font.FontFamily);
        }
        public void StylePriorityMultiple_Test()
        {
            StyleDefn defn;
            Document  doc = new Document();

            doc.ElementName = "doc:Document";

            defn                  = new StyleDefn();
            defn.Match            = "doc:Div.red"; //higher priority
            defn.Border.LineStyle = LineType.Solid;
            defn.Margins.Top      = 30;
            doc.Styles.Add(defn);

            defn              = new StyleDefn();
            defn.Match        = ".red";
            defn.Border.Color = "#FF0000";
            defn.Border.Width = 3; //overriden by doc:Div.red
            doc.Styles.Add(defn);

            //Default div style (added after to ensure priority overrides order)
            defn                  = new StyleDefn();
            defn.Match            = "doc:Div";
            defn.Margins.Left     = 10;
            defn.Margins.All      = 20;
            defn.Fill.Color       = "#00FF00";
            defn.Border.Color     = "#0000FF";     //Overriden by .red
            defn.Border.Width     = 1;
            defn.Border.LineStyle = LineType.Dash; //Overriden by doc:Div.red
            defn.Border.Dash      = PDFDashes.Dot;
            doc.Styles.Add(defn);


            Page pg = new Page();

            pg.ElementName = "doc:Page";
            pg.StyleClass  = "green";
            doc.Pages.Add(pg);

            Div div = new Div();

            div.ElementName = "doc:Div";
            div.StyleClass  = "red";
            div.BorderWidth = 10; //Highest priority
            pg.Contents.Add(div);

            var style = div.GetAppliedStyle();

            Assert.AreEqual(10, style.Border.Width.PointsValue, "Div border width should be 10");
            Assert.AreEqual(LineType.Solid, style.Border.LineStyle, "LineStyle should be solid from the doc:Div.red style");
            Assert.AreEqual((PDFColor)"#00FF00", style.Fill.Color, "Fill colour should be green from the doc:Div style");

            Assert.AreEqual(30, style.Margins.Top.PointsValue, "Top margin should be 30 from 'doc:Div.red'");
            Assert.AreEqual(10, style.Margins.Left.PointsValue, "Left margin should be 10 from 'doc:Div'");
            Assert.AreEqual(20, style.Margins.Bottom.PointsValue, "Bottom should be 20 from 'doc:Div' Margins.All");
        }
Exemplo n.º 17
0
        public void PDFStyleDefnConstructorTest()
        {
            StyleDefn target = new StyleDefn();

            Assert.IsNotNull(target);
            Assert.AreEqual(false, target.HasValues);
            Assert.IsTrue(string.IsNullOrEmpty(target.AppliedClass));
            Assert.IsTrue(string.IsNullOrEmpty(target.AppliedID));
            Assert.IsNull(target.AppliedType);
            Assert.AreEqual(ComponentState.Normal, target.AppliedState);
        }
Exemplo n.º 18
0
        public void PDFStyleDefnConstructorTest1()
        {
            Type      appliedtype      = typeof(Scryber.Components.Label);
            string    appliedid        = "myid";
            string    appliedclassname = "myclass";
            StyleDefn target           = new StyleDefn(appliedtype, appliedid, appliedclassname);

            Assert.IsNotNull(target);
            Assert.IsFalse(target.HasValues);
            Assert.AreEqual(appliedclassname, target.AppliedClass);
            Assert.AreEqual(appliedid, target.AppliedID);
            Assert.AreEqual(appliedtype, target.AppliedType);
            Assert.AreEqual(ComponentState.Normal, target.AppliedState);
        }
        public void MergeIntoTest()
        {
            StyleCollection target = new StyleCollection();
            StyleDefn       defn   = new StyleDefn();

            defn.Match = "doc:Label.sea";

            defn.Border.Color    = PDFColors.Red;
            defn.Border.Width    = 10;
            defn.Font.FontFamily = (PDFFontSelector)"Helvetica";

            target.Add(defn);

            StyleDefn defn2 = new StyleDefn();

            defn2.Match = ".sea"; // lower priority

            defn2.Border.Color        = PDFColors.Gray;
            defn2.Columns.ColumnCount = 3;
            target.Add(defn2);

            StyleDefn defn3 = new StyleDefn();

            defn3.AppliedClass = "other";
            defn3.Border.Width = 20;
            defn3.Stroke.Color = PDFColors.Aqua;
            target.Add(defn3);

            Label lbl = new Label();

            lbl.ElementName = "doc:Label";
            ComponentState state = ComponentState.Normal;

            Style style = new Style();

            target.MergeInto(style, lbl, state);
            style.Flatten();
            Assert.IsFalse(style.HasValues);//no style class on the label

            lbl.StyleClass = "sea";
            style          = new Style();
            target.MergeInto(style, lbl, state);
            style.Flatten();

            Assert.AreEqual(PDFColors.Red, style.Border.Color);                   //from defn as higher priority
            Assert.AreEqual((PDFUnit)10, style.Border.Width);                     // from defn (defn2 has no width)
            Assert.AreEqual((PDFFontSelector)"Helvetica", style.Font.FontFamily); //from defn
            Assert.AreEqual(3, style.Columns.ColumnCount);                        //from defn2 (lower priority but not set on defn)
            Assert.IsFalse(style.IsValueDefined(StyleKeys.StrokeColorKey));       //defn3 does have a stroke, but shoulld not be included
        }
        public void DocumentPrefixUpperAlpha_Numbered_Test()
        {
            Document doc = new Document();

            //Add a catch all number style definition for upper letter with Prefix
            StyleDefn pgNumStyle = new StyleDefn();

            pgNumStyle.AppliedType = typeof(Document);

            pgNumStyle.PageStyle.NumberStyle = PageNumberStyle.UppercaseLetters;
            doc.Styles.Add(pgNumStyle);


            int totalcount = 10;

            //Add ten pages with default numbering
            for (int i = 0; i < totalcount; i++)
            {
                Page pg = new Page();
                doc.Pages.Add(pg);
                PageNumberLabel lbl = new PageNumberLabel();
                pg.Contents.Add(lbl);
            }


            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                doc.LayoutComplete += Doc_LayoutCompleted;
                doc.SaveAsPDF(ms);
            }

            //Check the numbers
            for (int i = 0; i < totalcount; i++)
            {
                int           pgNum = i + 1;
                PDFLayoutPage lpg   = layout.AllPages[i];

                PDFPageNumberData data = lpg.GetPageNumber();

                //Check the number
                Assert.AreEqual(pgNum, data.PageNumber, "Page Number failed");
                Assert.AreEqual(totalcount, data.LastPageNumber, "Last Page number failed");
                Assert.AreEqual(pgNum, data.GroupNumber, "Group number failed");
                Assert.AreEqual(totalcount, data.GroupLastNumber, "Last Group number failed");

                //Should be upper letter with hash prefix
                string output = ((char)((int)'A' + i)).ToString();
                Assert.AreEqual(output, data.ToString(), "String result failed");
            }
        }
        public void NestedMultipleClassMatch_Test()
        {
            int priority = 0;

            var defn = new StyleDefn();

            defn.Match = "doc:Div.green > doc:Para.green.red, doc:Div.red > doc:Para.blue.green";

            var div1 = new Div();

            div1.ElementName = "doc:Div";
            div1.StyleClass  = "red";

            var div2 = new Div();

            div2.ElementName = "doc:Div";
            div2.StyleClass  = "green";
            div1.Contents.Add(div2);

            var para = new Paragraph();

            para.ElementName = "doc:Para";
            para.StyleClass  = "green red";
            div2.Contents.Add(para);

            //red > bulue and green
            var result = defn.IsMatchedTo(para, out priority);

            Assert.IsTrue(result, "The inner para was not matched as expected");


            //Switch the parent to .red and parents parent to .green and it should match on the second selector
            //As not a direct parent (classes reveresd order)

            div2.StyleClass = "red";
            para.StyleClass = "green blue";

            result = defn.IsMatchedTo(para, out priority);
            Assert.IsTrue(result, "The inner para was not matched as expected");

            //red back as direct parent, but only one class on the para

            div2.StyleClass = "red";
            para.StyleClass = "green";

            result = defn.IsMatchedTo(para, out priority);
            Assert.IsFalse(result, "The inner para was matched - not as expected");
        }
        public void SimpleIsMatch_Test()
        {
            int priority;
            var defn = new StyleDefn();

            defn.Match = PDFStyleMatcher.Parse(".red");


            var div = new Div();

            div.StyleClass = "red";

            var result = defn.IsMatchedTo(div, out priority);

            Assert.IsTrue(result, "The definition did not match the component");
        }
        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        /// Creates a new style with the Background and
        /// </summary>
        /// <returns></returns>
        internal virtual StyleDefn CreatePDFStyleBase()
        {
            StyleDefn style = new StyleDefn();

            style.AppliedClass = "sea";
            style.AppliedID    = "mylabel";
            style.AppliedType  = typeof(Components.Label);

            style.Background.Color = Scryber.Drawing.PDFColors.Aqua;

            style.Border.Width = 4;
            style.Border.Color = Scryber.Drawing.PDFColors.Blue;

            Assert.IsTrue(style.HasValues);
            return(style);
        }
        public void NestedDualClassMatch_Test()
        {
            int priority;

            var defn = new StyleDefn();

            defn.Match = "doc:Div.green > .blue.green";

            var div1 = new Div();

            div1.ElementName = "doc:Div";
            div1.StyleClass  = "red";

            var div2 = new Div();

            div2.ElementName = "doc:Div";
            div2.StyleClass  = "green";
            div1.Contents.Add(div2);

            var para = new Paragraph();

            para.ElementName = "doc:Para";
            para.StyleClass  = "blue green";
            div2.Contents.Add(para);

            var result = defn.IsMatchedTo(para, out priority);

            Assert.IsTrue(result, "The inner para was not matched as expected");


            //Switch the parent to .red and parents parent to .green and it should not match
            //As not a direct parent

            div1.StyleClass = "green";
            div2.StyleClass = "red";

            result = defn.IsMatchedTo(div2, out priority);
            Assert.IsFalse(result, "The inner para was matched - not as expected");

            //Green back as direct parent, but only one class on the para

            div2.StyleClass = "green";
            para.StyleClass = "blue";

            result = defn.IsMatchedTo(div2, out priority);
            Assert.IsFalse(result, "The inner para was matched - not as expected");
        }
        public void DualIsMatch_Test()
        {
            int priority;
            var defn = new StyleDefn();

            defn.Match = PDFStyleMatcher.Parse("doc:Div.red");


            var div = new Div();

            div.ID          = "MyDiv";
            div.StyleClass  = "red";
            div.ElementName = "doc:Div";

            var result = defn.IsMatchedTo(div, out priority);

            Assert.IsTrue(result, "The definition did not match the component");
        }
        public void DeepNestedClassMatch_Test()
        {
            int priority;

            var defn = new StyleDefn();

            defn.Match = "doc:Div#MyDiv doc:Para.blue";

            var div1 = new Div();

            div1.ElementName = "doc:Div";
            div1.ID          = "MyDiv";
            div1.StyleClass  = "red";

            var div2 = new Div();

            div2.ElementName = "doc:Div";
            div2.StyleClass  = "blue";
            div1.Contents.Add(div2);

            var para = new Paragraph();

            para.ElementName = "doc:Para";
            para.StyleClass  = "blue";
            div2.Contents.Add(para);

            var result = defn.IsMatchedTo(para, out priority);

            Assert.IsTrue(result, "The inner para was not matched as expected");

            //Switch the parent to .green and it should not match

            div1.StyleClass = "green";
            result          = defn.IsMatchedTo(div2, out priority);
            Assert.IsFalse(result, "The inner para was matched - not as expected");
        }
Exemplo n.º 27
0
        public void LoadHtmlFromSource()
        {
            var path = System.Environment.CurrentDirectory;

            path = System.IO.Path.Combine(path, "../../../Content/HTML/HtmlFromSource.html");

            using (var doc = Document.ParseDocument(path))
            {
                var defn = new StyleDefn("h1.border");
                defn.Background.Color = (PDFColor)"#FFA";
                defn.Border.Width     = 2;
                defn.Border.Color     = PDFColors.Red;
                defn.Border.LineStyle = LineType.Solid;

                doc.Styles.Add(defn);



                using (var stream = DocStreams.GetOutputStream("HtmlFromSource.pdf"))
                {
                    doc.SaveAsPDF(stream);
                }
            }
        }
        public void DocumentStyle_WithTitle_PageGroup_AndSection_Test()
        {
            // Blank title page, followed by 2 pages with lower roman, 3 pages in the page group,
            // then inner section of lower alpha and back to a page group single page.

            string[] pgNums = new string[] { "", "i", "ii", "1", "2", "3", "e", "f", "g", "4" };

            //set up the styles

            Document doc = new Document();

            //catch all style will be applied to the document
            StyleDefn catchall = new StyleDefn();

            catchall.PageStyle.NumberStyle = PageNumberStyle.LowercaseRoman;
            catchall.AppliedType           = typeof(Document);
            doc.Styles.Add(catchall);

            //style for the page group
            StyleDefn pgGrpStyle = new StyleDefn();

            pgGrpStyle.AppliedType           = typeof(PageGroup);
            pgGrpStyle.PageStyle.NumberStyle = PageNumberStyle.Decimals;
            doc.Styles.Add(pgGrpStyle);

            //style for the section
            StyleDefn sectStyle = new StyleDefn();

            sectStyle.AppliedType                = typeof(Section);
            sectStyle.PageStyle.NumberStyle      = PageNumberStyle.LowercaseLetters;
            sectStyle.PageStyle.NumberStartIndex = 5;
            doc.Styles.Add(sectStyle);

            //build the document pages to match

            //empty title page

            Page title = new Page();

            title.Style.PageStyle.NumberStyle = PageNumberStyle.None;
            doc.Pages.Add(title);

            // 2 lower roman from the document style

            Page pi = new Page();

            doc.Pages.Add(pi);

            Page pii = new Page();

            doc.Pages.Add(pii);

            //page group with 3 pages of decimals

            PageGroup grp = new PageGroup();

            doc.Pages.Add(grp);

            Page pg1 = new Page();

            grp.Pages.Add(pg1);

            Page pg2 = new Page();

            grp.Pages.Add(pg2);

            Page pg3 = new Page();

            grp.Pages.Add(pg3);

            // section of lower alpha with 3 pages of content

            Section sect = new Section();

            grp.Pages.Add(sect);
            sect.Contents.Add(new PageBreak());
            sect.Contents.Add(new PageBreak());

            // final page in the group
            Page pg4 = new Page();

            grp.Pages.Add(pg4);



            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                doc.LayoutComplete += Doc_LayoutCompleted;
                doc.SaveAsPDF(ms);
            }

            Assert.AreEqual(layout.AllPages.Count, pgNums.Length);

            for (int i = 0; i < pgNums.Length; i++)
            {
                PDFLayoutPage lpg = layout.AllPages[i];

                string expected = pgNums[i];
                string actual   = lpg.GetPageNumber().ToString();

                Assert.AreEqual(expected, actual);
                TestContext.WriteLine("Expected '{0}', Actual '{1}'", expected, actual);
            }
        }
        private StyleBase ParseNextStyle()
        {
            if (!this._str.MoveNext())
            {
                return(null);
            }

            var start = this._str.Offset;

            if (start >= this._str.Length)
            {
                return(null);
            }

            StyleBase parsed   = null;
            string    selector = "";

            try
            {
                var next = MoveToNextStyleStart();
                if (next < 0)
                {
                    this._str.Offset = this._str.Length;
                    return(null);
                }

                selector = this._str.Substring(start, next - start).Trim();

                if (this.IsMediaQuery(ref selector))
                {
                    if (this._log.ShouldLog(TraceLevel.Verbose))
                    {
                        this._log.Add(TraceLevel.Verbose, "CSS", "Found media query at rule for " + selector + " parsing inner contents");
                    }

                    var             match    = Selectors.MediaMatcher.Parse(selector);
                    StyleMediaGroup media    = new StyleMediaGroup(match);
                    var             innerEnd = MoveToGroupEnd();
                    if (innerEnd <= next)
                    {
                        return(null);
                    }

                    this.ParseInnerStyles(media, next + 1, innerEnd - (next + 1));
                    parsed           = media;
                    this._str.Offset = innerEnd;
                }
                else if (this.IsPageQuery(ref selector))
                {
                    if (this._log.ShouldLog(TraceLevel.Verbose))
                    {
                        this._log.Add(TraceLevel.Verbose, "CSS", "Found page at rule for " + selector + " parsing inner contents");
                    }

                    var            match    = Selectors.PageMatcher.Parse(selector);
                    StylePageGroup pg       = new StylePageGroup(match);
                    var            innerEnd = MoveToNextStyleEnd();
                    if (innerEnd <= next)
                    {
                        return(null);
                    }

                    string style = this._str.Substring(next + 1, innerEnd - (next + 1));

                    CSSStyleItemReader    reader = new CSSStyleItemReader(style);
                    CSSStyleItemAllParser parser = new CSSStyleItemAllParser();

                    while (reader.ReadNextAttributeName())
                    {
                        parser.SetStyleValue(this._log, pg, reader);
                    }


                    parsed           = pg;
                    this._str.Offset = innerEnd;
                }
                else if (this.IsFontFace(ref selector))
                {
                    if (this._log.ShouldLog(TraceLevel.Verbose))
                    {
                        this._log.Add(TraceLevel.Verbose, "CSS", "Found font-face at rule for " + selector + " parsing inner contents");
                    }

                    StyleFontFace ff       = new StyleFontFace();
                    var           innerEnd = MoveToNextStyleEnd();
                    if (innerEnd <= next)
                    {
                        return(null);
                    }

                    string                style  = this._str.Substring(next + 1, innerEnd - (next + 1));
                    CSSStyleItemReader    reader = new CSSStyleItemReader(style);
                    CSSStyleItemAllParser parser = new CSSStyleItemAllParser();

                    while (reader.ReadNextAttributeName())
                    {
                        parser.SetStyleValue(this._log, ff, reader);
                    }


                    parsed           = ff;
                    this._str.Offset = innerEnd;
                }
                else
                {
                    var end = MoveToNextStyleEnd();
                    if (end < next)
                    {
                        this._str.Offset = this._str.Length;
                        return(null);
                    }

                    selector = this._str.Substring(start, next - start);
                    string style = this._str.Substring(next + 1, end - (next + 1));



                    StyleDefn defn = new StyleDefn();

                    defn.Match = selector;

                    if (this._log.ShouldLog(TraceLevel.Verbose))
                    {
                        this._log.Add(TraceLevel.Verbose, "CSS", "Found css selector " + defn.Match.ToString() + " parsing inner contents");
                    }

                    CSSStyleItemReader    reader = new CSSStyleItemReader(style);
                    CSSStyleItemAllParser parser = new CSSStyleItemAllParser();

                    while (reader.ReadNextAttributeName())
                    {
                        parser.SetStyleValue(this._log, defn, reader);
                    }

                    //success, so we can return
                    parsed           = defn;
                    this._str.Offset = end;
                }
            }
            catch (Exception ex)
            {
                this._log.Add(TraceLevel.Error, "CSS", "Parsing of css failed with message : " + ex.Message, ex);

                if (null != this._owner)
                {
                    this._owner.RegisterParsingError(start, selector, ex);
                }

                this._str.Offset = this._str.Length;
            }


            return(parsed);
        }
        private void InitTraceLogStyles()
        {
            StyleDefn traceSect = new StyleDefn()
            {
                AppliedType = typeof(Section)
            };

            traceSect.Margins.All         = 10;
            traceSect.PageStyle.PaperSize = PaperSize.A4;
            traceSect.Font.FontFamily     = (PDFFontSelector)"Helvetica";
            this.Styles.Add(traceSect);

            StyleDefn h1 = new StyleDefn()
            {
                AppliedType = typeof(Head1)
            };

            h1.Background.Color = new PDFColor(0.6);
            h1.Size.FullWidth   = true;
            h1.Fill.Color       = PDFColors.White;
            h1.Padding.All      = 5;
            this.Styles.Add(h1);

            StyleDefn h3 = new StyleDefn()
            {
                AppliedType = typeof(Head3)
            };

            h3.Background.Color = new PDFColor(0.9);
            h3.Size.FullWidth   = true;
            h3.Padding.All      = 5;
            this.Styles.Add(h3);

            StyleDefn tbl = new StyleDefn()
            {
                AppliedType = typeof(TableGrid)
            };

            tbl.Size.FullWidth = true;
            tbl.Margins.All    = 10;
            tbl.Font.FontSize  = 10;
            this.Styles.Add(tbl);



            //general cell

            StyleDefn tcell = new StyleDefn()
            {
                AppliedType = typeof(TableCell)
            };

            tcell.Border.Width = 1;
            tcell.Border.Color = PDFColors.Gray;
            tcell.Padding.All  = 2;
            this.Styles.Add(tcell);

            StyleDefn tcellhead = new StyleDefn()
            {
                AppliedType = typeof(TableHeaderCell)
            };

            tcellhead.Font.FontBold = true;
            this.Styles.Add(tcellhead);

            StyleDefn tcellnum = new StyleDefn()
            {
                AppliedType  = typeof(TableCell),
                AppliedClass = "number"
            };

            tcellnum.Position.HAlign = HorizontalAlignment.Right;
            this.Styles.Add(tcellnum);


            //log level styles

            StyleDefn logGrid = new StyleDefn()
            {
                AppliedClass = "log-grid"
            };

            logGrid.Font.FontSize = 9;
            this.Styles.Add(logGrid);

            StyleDefn logMsg = new StyleDefn()
            {
                AppliedClass = "Message"
            };

            logMsg.Fill.Color = PDFColors.Green;
            this.Styles.Add(logMsg);

            StyleDefn logErr = new StyleDefn()
            {
                AppliedClass = "Error"
            };

            logErr.Fill.Color       = PDFColors.White;
            logErr.Background.Color = PDFColors.Red;
            this.Styles.Add(logErr);

            StyleDefn logWarn = new StyleDefn()
            {
                AppliedClass = "Warning"
            };

            logWarn.Fill.Color = PDFColors.Red;
            this.Styles.Add(logWarn);

            StyleDefn logVerb = new StyleDefn()
            {
                AppliedClass = "Verbose"
            };

            logVerb.Fill.Color = PDFColors.Black;
            this.Styles.Add(logVerb);

            StyleDefn logDebug = new StyleDefn()
            {
                AppliedClass = "Debug"
            };

            logDebug.Fill.Color      = new PDFColor(0.4, 0.4, 0.4);
            logDebug.Font.FontItalic = true;
            this.Styles.Add(logDebug);
        }