Ejemplo n.º 1
0
 public void VisitChunk(IChunkVisitor visitor, OutputLocation location, IList <Chunk> chunks, StringBuilder output)
 {
     if (location == OutputLocation.UsingNamespace)
     {
         output.AppendLine("//this was a test");
     }
 }
Ejemplo n.º 2
0
 public NoUniqueIdsTagHelperCodeRenderer(
     IChunkVisitor bodyVisitor,
     CSharpCodeWriter writer,
     CodeGeneratorContext context)
     : base(bodyVisitor, writer, context)
 {
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Instantiates a new <see cref="CSharpTagHelperCodeRenderer"/>.
        /// </summary>
        /// <param name="bodyVisitor">The <see cref="IChunkVisitor"/> used to render chunks found in the body.</param>
        /// <param name="writer">The <see cref="CSharpCodeWriter"/> used to write code.</param>
        /// <param name="context">A <see cref="CodeGeneratorContext"/> instance that contains information about
        /// the current code generation process.</param>
        public CSharpTagHelperCodeRenderer(
            IChunkVisitor bodyVisitor,
            CSharpCodeWriter writer,
            CodeGeneratorContext context)
        {
            if (bodyVisitor == null)
            {
                throw new ArgumentNullException(nameof(bodyVisitor));
            }

            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            _bodyVisitor = bodyVisitor;
            _writer = writer;
            _context = context;
            _tagHelperContext = context.Host.GeneratedClassContext.GeneratedTagHelperContext;
            _designTimeMode = context.Host.DesignTimeMode;

            _literalBodyVisitor = new CSharpLiteralCodeVisitor(this, writer, context);
            _attributeCodeVisitor = new TagHelperAttributeCodeVisitor(writer, context);
            AttributeValueCodeRenderer = new TagHelperAttributeValueCodeRenderer();
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Read the PNG from the file path.
 /// </summary>
 /// <param name="filePath">The path to the PNG file to open.</param>
 /// <param name="chunkVisitor">Optional: A visitor which is called whenever a chunk is read by the library.</param>
 /// <remarks>This will open the file to obtain a <see cref="FileStream"/> so will lock the file during reading.</remarks>
 /// <returns>The <see cref="Png"/> data from the file.</returns>
 public static Png Open(string filePath, IChunkVisitor chunkVisitor = null)
 {
     using (var fileStream = File.OpenRead(filePath))
     {
         return(Open(fileStream, chunkVisitor));
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Read the PNG image from the bytes.
 /// </summary>
 /// <param name="bytes">The bytes of the PNG data to be read.</param>
 /// <param name="chunkVisitor">Optional: A visitor which is called whenever a chunk is read by the library.</param>
 /// <returns>The <see cref="Png"/> data from the bytes.</returns>
 public static Png Open(byte[] bytes, IChunkVisitor chunkVisitor = null)
 {
     using (var memoryStream = new MemoryStream(bytes))
     {
         return(PngOpener.Open(memoryStream, chunkVisitor));
     }
 }
        /// <summary>
        /// Instantiates a new <see cref="CSharpTagHelperCodeRenderer"/>.
        /// </summary>
        /// <param name="bodyVisitor">The <see cref="IChunkVisitor"/> used to render chunks found in the body.</param>
        /// <param name="writer">The <see cref="CSharpCodeWriter"/> used to write code.</param>
        /// <param name="context">A <see cref="CodeGeneratorContext"/> instance that contains information about
        /// the current code generation process.</param>
        public CSharpTagHelperCodeRenderer(
            IChunkVisitor bodyVisitor,
            CSharpCodeWriter writer,
            CodeGeneratorContext context)
        {
            if (bodyVisitor == null)
            {
                throw new ArgumentNullException(nameof(bodyVisitor));
            }

            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            _bodyVisitor      = bodyVisitor;
            _writer           = writer;
            _context          = context;
            _tagHelperContext = context.Host.GeneratedClassContext.GeneratedTagHelperContext;
            _designTimeMode   = context.Host.DesignTimeMode;

            _literalBodyVisitor        = new CSharpLiteralCodeVisitor(this, writer, context);
            _attributeCodeVisitor      = new TagHelperAttributeCodeVisitor(writer, context);
            AttributeValueCodeRenderer = new TagHelperAttributeValueCodeRenderer();
        }
Ejemplo n.º 7
0
 public TrackingUniqueIdsTagHelperCodeRenderer(
     IChunkVisitor bodyVisitor,
     CSharpCodeWriter writer,
     CodeBuilderContext context)
     : base(bodyVisitor, writer, context)
 {
 }
Ejemplo n.º 8
0
        public IImage ReadImage(string path)
        {
            IChunkVisitor chunkVisitor = null;
            Stream        stream       = File.OpenRead(path);

            return(ReadImage(stream, new PngReaderSettings()
            {
                ChunkVisitor = chunkVisitor
            }));
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Instantiates a new <see cref="CSharpTagHelperCodeRenderer"/>.
 /// </summary>
 /// <param name="bodyVisitor">The <see cref="IChunkVisitor"/> used to render chunks found in the body.</param>
 /// <param name="writer">The <see cref="CSharpCodeWriter"/> used to write code.</param>
 /// <param name="context">A <see cref="CodeBuilderContext"/> instance that contains information about
 /// the current code generation process.</param>
 public CSharpTagHelperCodeRenderer([NotNull] IChunkVisitor bodyVisitor,
                                    [NotNull] CSharpCodeWriter writer,
                                    [NotNull] CodeBuilderContext context)
 {
     _bodyVisitor               = bodyVisitor;
     _writer                    = writer;
     _context                   = context;
     _tagHelperContext          = context.Host.GeneratedClassContext.GeneratedTagHelperContext;
     AttributeValueCodeRenderer = new TagHelperAttributeValueCodeRenderer();
 }
Ejemplo n.º 10
0
 public void VisitChunk(IChunkVisitor visitor, OutputLocation location, IList<Chunk> body, StringBuilder output)
 {
     switch (location)
     {
         case OutputLocation.ClassMembers:
             output
                 .Append("global::System.Action __target_")
                 .Append(_targetExtensionCount)
                 .AppendLine(";");
             break;
     }
     visitor.Accept(body);
 }
Ejemplo n.º 11
0
 public void VisitChunk(IChunkVisitor visitor, OutputLocation location, IList <Chunk> body, StringBuilder output)
 {
     if (location == OutputLocation.ClassMembers)
     {
         foreach (var snippet in body.OfType <CodeStatementChunk>().SelectMany(chunk => chunk.Code))
         {
             snippet.Value = snippet.Value.Replace("@class", "class");
         }
         var source    = new SourceWriter(new StringWriter(output));
         var generator = new GeneratedCodeVisitor(source, new Dictionary <string, object>(), NullBehaviour.Strict);
         generator.Accept(body);
     }
 }
Ejemplo n.º 12
0
 public void VisitChunk(IChunkVisitor visitor, OutputLocation location, IList <Chunk> body, StringBuilder output)
 {
     switch (location)
     {
     case OutputLocation.ClassMembers:
         output
         .Append("global::System.Action __target_")
         .Append(_targetExtensionCount)
         .AppendLine(";");
         break;
     }
     visitor.Accept(body);
 }
Ejemplo n.º 13
0
 public void VisitChunk(IChunkVisitor visitor, OutputLocation location, IList<Chunk> body, StringBuilder output)
 {
     if (location == OutputLocation.ClassMembers)
     {
         foreach (var snippet in body.OfType<CodeStatementChunk>().SelectMany(chunk => chunk.Code))
         {
             snippet.Value = snippet.Value.Replace("@class", "class");
         }
         var source = new SourceWriter(new StringWriter(output));
         var generator = new GeneratedCodeVisitor(source, new Dictionary<string, object>(), NullBehaviour.Strict);
         generator.Accept(body);
     }
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Instantiates a new <see cref="CSharpTagHelperCodeRenderer"/>.
        /// </summary>
        /// <param name="bodyVisitor">The <see cref="IChunkVisitor"/> used to render chunks found in the body.</param>
        /// <param name="writer">The <see cref="CSharpCodeWriter"/> used to write code.</param>
        /// <param name="context">A <see cref="CodeGeneratorContext"/> instance that contains information about
        /// the current code generation process.</param>
        public CSharpTagHelperCodeRenderer(
            [NotNull] IChunkVisitor bodyVisitor,
            [NotNull] CSharpCodeWriter writer,
            [NotNull] CodeGeneratorContext context)
        {
            _bodyVisitor = bodyVisitor;
            _writer = writer;
            _context = context;
            _tagHelperContext = context.Host.GeneratedClassContext.GeneratedTagHelperContext;
            _designTimeMode = context.Host.DesignTimeMode;

            _literalBodyVisitor = new CSharpLiteralCodeVisitor(this, writer, context);
            AttributeValueCodeRenderer = new TagHelperAttributeValueCodeRenderer();
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Instantiates a new <see cref="CSharpTagHelperCodeRenderer"/>.
        /// </summary>
        /// <param name="bodyVisitor">The <see cref="IChunkVisitor"/> used to render chunks found in the body.</param>
        /// <param name="writer">The <see cref="CSharpCodeWriter"/> used to write code.</param>
        /// <param name="context">A <see cref="CodeGeneratorContext"/> instance that contains information about
        /// the current code generation process.</param>
        public CSharpTagHelperCodeRenderer(
            [NotNull] IChunkVisitor bodyVisitor,
            [NotNull] CSharpCodeWriter writer,
            [NotNull] CodeGeneratorContext context)
        {
            _bodyVisitor      = bodyVisitor;
            _writer           = writer;
            _context          = context;
            _tagHelperContext = context.Host.GeneratedClassContext.GeneratedTagHelperContext;
            _designTimeMode   = context.Host.DesignTimeMode;

            _literalBodyVisitor        = new CSharpLiteralCodeVisitor(this, writer, context);
            AttributeValueCodeRenderer = new TagHelperAttributeValueCodeRenderer();
        }
Ejemplo n.º 16
0
        public void VisitChunk(IChunkVisitor visitor, OutputLocation location, IList <Chunk> body, StringBuilder output)
        {
            if (location == OutputLocation.RenderMethod)
            {
                output.AppendFormat("RenderComponent(\"{0}\", new System.Collections.Generic.Dictionary<string,object> {{", node.Name);

                var delimiter = "";
                foreach (var attribute in node.Attributes)
                {
                    var code = attribute.AsCode();
                    output.AppendFormat("{2}{{\"{0}\",{1}}}", attribute.Name, code, delimiter);
                    delimiter = ", ";
                }
                output.AppendLine("}, new System.Action(delegate {");
                visitor.Accept(body); //only append body if there are no sections
                output.AppendLine("}),");

                output.AppendLine("new System.Collections.Generic.Dictionary<string,System.Action> {");
                foreach (var section in sectionsChunks)
                {
                    output.Append("{\"")
                    .Append(section.Key)
                    .AppendLine("\", new System.Action(delegate {");

                    foreach (var attr in sectionsAttributes[section.Key])
                    {
                        output.Append("var ")
                        .Append(attr.Name)
                        .Append("=(")
                        .Append(attr.AsCode())
                        .Append(")ViewData[\"")
                        .Append(attr.Name)
                        .AppendLine("\"];");
                    }
                    visitor.Accept(section.Value);
                    output.AppendLine("})},");
                }
                output.AppendLine("});");
            }
            else
            {
                visitor.Accept(body);
                foreach (var sectionChunks in sectionsChunks.Values)
                {
                    visitor.Accept(sectionChunks);
                }
            }
        }
Ejemplo n.º 17
0
        public void VisitChunk(IChunkVisitor visitor, OutputLocation location, IList<Chunk> body, StringBuilder output)
        {
            if (location == OutputLocation.RenderMethod)
            {
                output.AppendFormat("RenderComponent(\"{0}\", new System.Collections.Generic.Dictionary<string,object> {{", node.Name);

                var delimiter = "";
                foreach (var attribute in node.Attributes)
                {
                    var code = attribute.AsCode();
                    output.AppendFormat("{2}{{\"{0}\",{1}}}", attribute.Name, code, delimiter);
                    delimiter = ", ";
                }
                output.AppendLine("}, new System.Action(delegate {");
                visitor.Accept(body); //only append body if there are no sections
                output.AppendLine("}),");

                output.AppendLine("new System.Collections.Generic.Dictionary<string,System.Action> {");
                foreach (var section in sectionsChunks)
                {
                    output.Append("{\"")
                        .Append(section.Key)
                        .AppendLine("\", new System.Action(delegate {");

                    foreach (var attr in sectionsAttributes[section.Key])
                    {
                        output.Append("var ")
                            .Append(attr.Name)
                            .Append("=(")
                            .Append(attr.AsCode())
                            .Append(")ViewData[\"")
                            .Append(attr.Name)
                            .AppendLine("\"];");
                    }
                    visitor.Accept(section.Value);
                    output.AppendLine("})},");
                }
                output.AppendLine("});");
            }
            else
            {
                visitor.Accept(body);
                foreach(var sectionChunks in sectionsChunks.Values)
                    visitor.Accept(sectionChunks);
            }
        }
            public TrackingUniqueIdsTagHelperCodeRenderer(
                IChunkVisitor bodyVisitor,
                CSharpCodeWriter writer,
                CodeGeneratorContext context)
                : base(bodyVisitor, writer, context)
            {

            }
 public void VisitChunk(IChunkVisitor visitor, OutputLocation location, IList<Chunk> body, StringBuilder output)
 {
     visitor.Accept(body);
 }
Ejemplo n.º 20
0
        public static Png Open(Stream stream, IChunkVisitor chunkVisitor = null)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (!stream.CanRead)
            {
                throw new ArgumentException($"The provided stream of type {stream.GetType().FullName} was not readable.");
            }

            var validHeader = HasValidHeader(stream);

            if (!validHeader.IsValid)
            {
                throw new ArgumentException($"The provided stream did not start with the PNG header. Got {validHeader}.");
            }

            var crc         = new byte[4];
            var imageHeader = ReadImageHeader(stream, crc);

            var hasEncounteredImageEnd = false;

            Palette palette = null;

            using (var output = new MemoryStream())
            {
                using (var memoryStream = new MemoryStream())
                {
                    while (TryReadChunkHeader(stream, out var header))
                    {
                        if (hasEncounteredImageEnd)
                        {
                            throw new InvalidOperationException($"Found another chunk {header} after already reading the IEND chunk.");
                        }

                        var bytes = new byte[header.Length];
                        var read  = stream.Read(bytes, 0, bytes.Length);
                        if (read != bytes.Length)
                        {
                            throw new InvalidOperationException($"Did not read {header.Length} bytes for the {header} header, only found: {read}.");
                        }

                        if (header.IsCritical)
                        {
                            switch (header.Name)
                            {
                            case "PLTE":
                                if (header.Length % 3 != 0)
                                {
                                    throw new InvalidOperationException($"Palette data must be multiple of 3, got {header.Length}.");
                                }

                                palette = new Palette(bytes);

                                break;

                            case "IDAT":
                                memoryStream.Write(bytes, 0, bytes.Length);
                                break;

                            case "IEND":
                                hasEncounteredImageEnd = true;
                                break;

                            default:
                                throw new NotSupportedException($"Encountered critical header {header} which was not recognised.");
                            }
                        }

                        read = stream.Read(crc, 0, crc.Length);
                        if (read != 4)
                        {
                            throw new InvalidOperationException($"Did not read 4 bytes for the CRC, only found: {read}.");
                        }

                        var result    = (int)Crc32.Calculate(Encoding.ASCII.GetBytes(header.Name), bytes);
                        var crcActual = (crc[0] << 24) + (crc[1] << 16) + (crc[2] << 8) + crc[3];

                        if (result != crcActual)
                        {
                            throw new InvalidOperationException($"CRC calculated {result} did not match file {crcActual} for chunk: {header.Name}.");
                        }

                        chunkVisitor?.Visit(stream, imageHeader, header, bytes, crc);
                    }

                    memoryStream.Flush();
                    memoryStream.Seek(2, SeekOrigin.Begin);

                    using (var deflateStream = new DeflateStream(memoryStream, CompressionMode.Decompress))
                    {
                        deflateStream.CopyTo(output);
                        deflateStream.Close();
                    }
                }

                var bytesOut = output.ToArray();

                var(bytesPerPixel, samplesPerPixel) = Decoder.GetBytesAndSamplesPerPixel(imageHeader);

                bytesOut = Decoder.Decode(bytesOut, imageHeader, bytesPerPixel, samplesPerPixel);

                return(new Png(imageHeader, new RawPngData(bytesOut, bytesPerPixel, imageHeader.Width, imageHeader.InterlaceMethod, palette, imageHeader.ColorType)));
            }
        }
Ejemplo n.º 21
0
 public void VisitChunk(IChunkVisitor visitor, OutputLocation location, IList<Chunk> chunks, StringBuilder output)
 {
     if (location == OutputLocation.UsingNamespace)
         output.AppendLine("//this was a test");
 }
Ejemplo n.º 22
0
 public void VisitChunk(IChunkVisitor visitor, OutputLocation location, IList <Chunk> body, StringBuilder output)
 {
     visitor.Accept(body);
 }
Ejemplo n.º 23
0
 public static Png Open(Stream stream, IChunkVisitor chunkVisitor = null) => Open(stream, new PngOpenerSettings
 {
     ChunkVisitor = chunkVisitor
 });
Ejemplo n.º 24
0
 public void VisitChunk(IChunkVisitor visitor, OutputLocation location, IList<Chunk> body, StringBuilder output)
 {
     //when we need to accept chunks?
     if (location == OutputLocation.RenderMethod)
         visitor.Accept(_mChunks);
 }
Ejemplo n.º 25
0
                public NoUniqueIdsTagHelperCodeRenderer(IChunkVisitor bodyVisitor,
                                                        CSharpCodeWriter writer,
                                                        CodeBuilderContext context)
                    : base(bodyVisitor, writer, context)
                {

                }
Ejemplo n.º 26
0
 /// <summary>
 /// Read the PNG image from the stream.
 /// </summary>
 /// <param name="stream">The stream containing PNG data to be read.</param>
 /// <param name="chunkVisitor">Optional: A visitor which is called whenever a chunk is read by the library.</param>
 /// <returns>The <see cref="Png"/> data from the stream.</returns>
 public static Png Open(Stream stream, IChunkVisitor chunkVisitor = null)
 => PngOpener.Open(stream, chunkVisitor);