public override void ResetParser() { this._resultHtmlDoc = null; this.openEltStack.Clear(); this.curHtmlNode = null; this.curAttr = null; this.curTextNode = null; this.parseState = 0; this.textSnapshot = null; }
public override void ResetParser() { _resultHtmlDoc = null; _openEltStack.Clear(); _curHtmlNode = null; _curAttr = null; _curTextNode = null; _parseState = 0; _textSnapshot = null; }
/// <summary> /// parse to htmldom /// </summary> /// <param name="stbuilder"></param> internal void Parse(TextSnapshot textSnapshot, WebDocument htmldoc, DomElement currentNode) { this.textSnapshot = textSnapshot; //1. lex lexer.BeginLex(); //2. mini parser this.curHtmlNode = currentNode; this._resultHtmlDoc = htmldoc; lexer.Analyze(textSnapshot); lexer.EndLex(); }
MemBitmap ParseAndRenderSvg(System.Text.StringBuilder svgContent) { //---------- //copy from HtmlRenderer's SvgViewer demo //---------- var docBuilder = new VgDocBuilder(); var parser = new SvgParser(docBuilder); TextSnapshot textSnapshot = new TextSnapshot(svgContent.ToString()); parser.ParseDocument(textSnapshot); VgVisualDocBuilder builder = new VgVisualDocBuilder(); VgVisualElement vgVisElem = builder.CreateVgVisualDoc(docBuilder.ResultDocument, _vgDocHost).VgRootElem; PixelFarm.CpuBlit.VertexProcessing.Q1RectD bounds = vgVisElem.GetRectBounds(); float actualXOffset = (float)-bounds.Left; float actualYOffset = (float)-bounds.Bottom; int bmpW = (int)Math.Round(bounds.Width); int bmpH = (int)Math.Round(bounds.Height); if (bmpW == 0 || bmpH == 0) { return(null); } MemBitmap memBitmap = new MemBitmap(bmpW, bmpH); using (Tools.BorrowAggPainter(memBitmap, out var p)) using (Tools.More.BorrowVgPaintArgs(p, out var paintArgs)) { float orgX = p.OriginX; float orgY = p.OriginY; p.SetOrigin(actualXOffset, actualYOffset); p.Clear(PixelFarm.Drawing.Color.White); p.FillColor = PixelFarm.Drawing.Color.Black; double prevStrokeW = p.StrokeWidth; vgVisElem.Paint(paintArgs); p.StrokeWidth = prevStrokeW; //restore p.SetOrigin(orgX, orgY); //restore } return(memBitmap); }
public virtual void ParseDocument(TextSnapshot textSnapshot) { _textSnapshot = textSnapshot; OnBegin(); //reset _openEltStack.Clear(); _waitingAttrName = null; _currentNodeName = null; _parseState = 0; // _myXmlLexer.BeginLex(); _myXmlLexer.Analyze(textSnapshot); _myXmlLexer.EndLex(); OnFinish(); }
public override void Analyze(TextSnapshot textSnapshot) { #if DEBUG dbug_OnStartAnalyze(); #endif this.textSnapshot = textSnapshot; char[] sourceBuffer = TextSnapshot.UnsafeGetInternalBuffer(textSnapshot); int lim = sourceBuffer.Length; char strEscapeChar = '"'; int currentState = 0; //----------------------------- for (int i = 0; i < lim; i++) { char c = sourceBuffer[i]; #if DEBUG dbug_currentLineCharIndex++; dbugReportChar(c, currentState); #endif switch (currentState) { default: { //??? } break; case 0: //from content mode { if (c == '<') { //flush existing content //switch to content tag mode FlushExisingBuffer(i, XmlLexerEvent.FromContentPart); currentState = 1; //not need whitespace in this mode } else { //in content mode AppendBuffer(c, i); } } break; case 1: { //after open angle switch (c) { case '!': { currentState = 11; //<! } break; case '?': { //process instruction currentState = 8; } break; case ':': { //shold not occurs currentState = 4; } break; case '/': { //close tag RaiseStateChanged(XmlLexerEvent.VisitOpenSlashAngle, i, 1); currentState = 5; //collect node name } break; default: { currentState = 5; //clear prev buffer //then start collect node name AppendBuffer(c, i); } break; } } break; case 2: { //inside comment node if (c == '-') { if (i < lim - 2) { if (sourceBuffer[i + 1] == '-' && sourceBuffer[i + 2] == '>') { //end comment node FlushExisingBuffer(i, XmlLexerEvent.CommentContent); i += 2; currentState = 0; continue; } } } //skip all comment content ? AppendBuffer(c, i); } break; case 5: { //inside open angle //name collecting //terminate with... switch (c) { case '/': { currentState = 7; } break; case '>': { FlushExisingBuffer(i, XmlLexerEvent.NodeNameOrAttribute); RaiseStateChanged(XmlLexerEvent.VisitCloseAngle, i, 1); //flush currentState = 0; //goto content mode } break; case ':': { //flush node name FlushExisingBuffer(i, XmlLexerEvent.NamePrefix); //start new node name } break; case ' ': { //flush node name FlushExisingBuffer(i, XmlLexerEvent.NodeNameOrAttribute); } break; case '=': { //flush name FlushExisingBuffer(i, XmlLexerEvent.Attribute); RaiseStateChanged(XmlLexerEvent.VisitAttrAssign, i, 1); //start collect value of attr } break; case '"': { //start string escap with " currentState = 6; strEscapeChar = '"'; } break; case '\'': { //start string escap with ' currentState = 6; strEscapeChar = '\''; } break; default: { //else collect //flush nodename if (char.IsWhiteSpace(c)) { FlushExisingBuffer(i, XmlLexerEvent.NodeNameOrAttribute); } else { AppendBuffer(c, i); } } break; } } break; case 6: { //collect string if (c == strEscapeChar) { //stop string escape //flush FlushExisingBuffer(i, XmlLexerEvent.AttributeValueAsLiteralString); currentState = 5; } else { AppendBuffer(c, i); } } break; case 7: { //after / //must be > if (c == '>') { FlushExisingBuffer(i, XmlLexerEvent.NodeNameOrAttribute); RaiseStateChanged(XmlLexerEvent.VisitCloseSlashAngle, i, 1); currentState = 0; } else { //error ? } } break; case 8: { //enter processing instruction if (c == '?') { //exit currentState = 9; } } break; case 9: { if (c == '>') { //flush xml processing instruction FlushExisingBuffer(i, XmlLexerEvent.ProcessInstructionContent); currentState = 0; //back to content mode } } break; case 10: { //unknown tag //exit from this tag when found > if (c == '>') { currentState = 0; } } break; case 11: { //open_angle, exlcimation switch (c) { case '-': { //looking for next char if (i < lim) { if (sourceBuffer[i + 1] == '-') { i++; //consume currentState = 2; continue; } else { //unknown tag? currentState = 10; } } } break; case '[': { // <![ // currentState = 10; //not implement,just skip } break; default: { //doc type? if (char.IsLetter(sourceBuffer[i + 1])) { RaiseStateChanged(XmlLexerEvent.VisitOpenAngleExclimation, i, 2); AppendBuffer(c, i); currentState = 5; } else { currentState = 10; //not implement, just skip } } break; } } break; } } #if DEBUG dbug_OnFinishAnalyze(); #endif }
public TextSource(char[] textBuffer) { _actualSnapshot = new TextSnapshot(textBuffer); }
public static string GetInternalText(TextSource textsource) { return(new string(TextSnapshot.UnsafeGetInternalBuffer(textsource._actualSnapshot))); }
// From DynamicParserLanguageServiceItem SnapshotSpan GetCurrentSpan(ITextSnapshot snapshot, SourceSpan span) { ITextSnapshot currentSnapshot = this.bufferView.TextBuffer.CurrentSnapshot; // TODO (dougwa): deal with line/column spans TextSnapshot cloneSnapshot = new TextSnapshot(snapshot, (TextBufferClone)this.bufferView.TextBuffer); SnapshotSpan oldSpan = new SnapshotSpan(cloneSnapshot, span.Start.Index, span.Length); SnapshotSpan newSpan = oldSpan.TranslateTo(currentSnapshot, SpanTrackingMode.EdgePositive); return newSpan; }
public virtual void Analyze(TextSnapshot textSnapshot) { }
public static char[] DecodeHtml(TextSnapshot source, int startIndex, int decLength) { return(DecodeHtml(TextSnapshot.UnsafeGetInternalBuffer(source), startIndex, decLength)); }
public static void ParseAndRenderSvg(PixelFarm.Drawing.SvgBmpBuilderReq req) { //---------- //copy from HtmlRenderer's SvgViewer demo //---------- var docBuilder = new VgDocBuilder(); var parser = new SvgParser(docBuilder); TextSnapshot textSnapshot = new TextSnapshot(req.SvgContent.ToString()); parser.ParseDocument(textSnapshot); VgVisualDocBuilder builder = new VgVisualDocBuilder(); VgVisualElement vgVisElem = builder.CreateVgVisualDoc(docBuilder.ResultDocument, _vgDocHost).VgRootElem; PixelFarm.CpuBlit.VertexProcessing.Q1RectD bounds = vgVisElem.GetRectBounds(); float actualXOffset = (float)-bounds.Left; float actualYOffset = (float)-bounds.Bottom; //original svg width, height int bmpW = (int)Math.Round(bounds.Width); int bmpH = (int)Math.Round(bounds.Height); if (bmpW == 0 || bmpH == 0) { return; } //scale svg to specific size float scale_w = req.ExpectedWidth / bmpW; //at this point, we have 2 options //1) create bitmap with original svg size and scale it down to expected size //2) scale svg to expected size and create a bitmap //we choose 2) int new_w = (int)Math.Round(bmpW * scale_w); int new_h = (int)Math.Round(bmpH * scale_w); MemBitmap memBitmap = new MemBitmap(new_w, new_h); using (Tools.BorrowAggPainter(memBitmap, out var p)) using (Tools.More.BorrowVgPaintArgs(p, out VgPaintArgs paintArgs)) { //pass by Affine tx = Affine.NewScaling(scale_w); paintArgs._currentTx = tx; float orgX = p.OriginX; float orgY = p.OriginY; p.SetOrigin(actualXOffset * scale_w, actualYOffset * scale_w); p.Clear(req.DefaultBgColor); p.FillColor = PixelFarm.Drawing.Color.Black; double prevStrokeW = p.StrokeWidth; vgVisElem.Paint(paintArgs); p.StrokeWidth = prevStrokeW; //restore p.SetOrigin(orgX, orgY); //restore } #if DEBUG //memBitmap.SaveImage("svg.png"); #endif req.Output = memBitmap; }