MCPcopy Create free account
hub / github.com/cogentcore/core / SetHTMLPre

Method SetHTMLPre

paint/text.go:594–807  ·  view source on GitHub ↗

note: adding print / log statements to following when inside gide will cause an infinite loop because the console redirection uses this very same code! SetHTMLPre sets preformatted HTML-styled text by decoding all standard inline HTML text style formatting tags in the string and sets the per-charact

(str []byte, font *styles.FontRender, txtSty *styles.Text, ctxt *units.Context, cssAgg map[string]any)

Source from the content-addressed store, hash-verified

592// (including class names) are decoded. Whitespace is decoded as-is,
593// including LF \n etc, except in WhiteSpacePreLine case which only preserves LF's
594func (tr *Text) SetHTMLPre(str []byte, font *styles.FontRender, txtSty *styles.Text, ctxt *units.Context, cssAgg map[string]any) {
595 // errstr := "core.Text SetHTMLPre"
596
597 sz := len(str)
598 tr.Spans = make([]Span, 1)
599 tr.Links = nil
600 if sz == 0 {
601 return
602 }
603 curSp := &(tr.Spans[0])
604 initsz := min(sz, 1020)
605 curSp.Init(initsz)
606
607 font.Font = OpenFont(font, ctxt)
608
609 nextIsParaStart := false
610 curLinkIndex := -1 // if currently processing an <a> link element
611
612 fstack := make([]*styles.FontRender, 1, 10)
613 fstack[0] = font
614
615 tagstack := make([]string, 0, 10)
616
617 tmpbuf := make([]byte, 0, 1020)
618
619 bidx := 0
620 curTag := ""
621 for bidx < sz {
622 cb := str[bidx]
623 ftag := ""
624 if cb == '<' && sz > bidx+1 {
625 eidx := bytes.Index(str[bidx+1:], []byte(">"))
626 if eidx > 0 {
627 ftag = string(str[bidx+1 : bidx+1+eidx])
628 bidx += eidx + 2
629 } else { // get past <
630 curf := fstack[len(fstack)-1]
631 curSp.AppendString(string(str[bidx:bidx+1]), curf.Face.Face, curf.Color, curf.Background, curf.Decoration, font, ctxt)
632 bidx++
633 }
634 }
635 if ftag != "" {
636 if ftag[0] == '/' {
637 etag := strings.ToLower(ftag[1:])
638 // fmt.Printf("%v etag: %v\n", bidx, etag)
639 if etag == "pre" {
640 continue // ignore
641 }
642 if etag != curTag {
643 // log.Printf("%v end tag: %v doesn't match current tag: %v for string\n%v\n", errstr, etag, curTag, string(str))
644 }
645 switch etag {
646 // case "p":
647 // tr.Spans = append(tr.Spans, Span{})
648 // curSp = &(tr.Spans[len(tr.Spans)-1])
649 // nextIsParaStart = true
650 // case "br":
651 // tr.Spans = append(tr.Spans, Span{})

Callers 4

SetHTMLMethod · 0.95
SetHTMLBytesMethod · 0.95
layoutAllLinesMethod · 0.80
layoutLineMethod · 0.80

Calls 13

SetStylePropertiesXMLFunction · 0.92
SubPropertiesFunction · 0.92
OpenFontFunction · 0.85
SetHTMLSimpleTagFunction · 0.85
FontStyleCSSFunction · 0.85
AppendStringMethod · 0.80
AppendRuneMethod · 0.80
SplitMethod · 0.80
SetDecorationMethod · 0.80
SetNewParaMethod · 0.80
InitMethod · 0.65
IndexMethod · 0.45

Tested by

no test coverage detected