Skip to content

Commit 0640b3c

Browse files
authored
Merge pull request #1679 from cogentcore/contmeta
contmeta: render rest of meta data for content, also super / subscript
2 parents d118d08 + 855ff95 commit 0640b3c

9 files changed

Lines changed: 78 additions & 6 deletions

File tree

content/content.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,14 @@ func (ct *Content) pageMaker(p *tree.Plan, tabIdx int) {
241241
})
242242
})
243243
}
244+
if !ct.inPDFRender && len(ct.current.Page.Affiliations) > 0 {
245+
tree.Add(p, func(w *core.Text) {
246+
w.SetType(core.TextBodyLarge)
247+
w.Updater(func() {
248+
w.SetText(ct.current.Page.Affiliations)
249+
})
250+
})
251+
}
244252
if !ct.inPDFRender && !ct.current.Page.Date.IsZero() {
245253
tree.Add(p, func(w *core.Text) {
246254
w.SetType(core.TextTitleMedium)
@@ -249,6 +257,14 @@ func (ct *Content) pageMaker(p *tree.Plan, tabIdx int) {
249257
})
250258
})
251259
}
260+
if !ct.inPDFRender && len(ct.current.Page.Abstract) > 0 {
261+
tree.Add(p, func(w *core.Text) {
262+
w.SetType(core.TextBodyLarge)
263+
w.Updater(func() {
264+
w.SetText("<b>Abstract:</b> " + ct.current.Page.Abstract)
265+
})
266+
})
267+
}
252268
tree.Add(p, func(w *core.Frame) {
253269
w.Styler(func(s *styles.Style) {
254270
s.Direction = styles.Column

content/examples/basic/content/button.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
+++
22
Categories = ["Widgets"]
33
Authors = "Bea A. Author<sup>1</sup> and Test Ing Name<sup>2</sup>"
4-
Affiliations = "<sup>1</sup>University of Somwhere <sup>2</sup>University of Elsewhere"
4+
Affiliations = "<sup>1</sup>University of Somewhere <sup>2</sup>University of Elsewhere"
55
Abstract = "The button is an essential element of any GUI framework, with the capability of triggering actions of any sort. Actions are very important because they allow people to actually do something."
6+
Date = "2026-05-01"
67
+++
78

89
A **button** is a [[widget]] that a user can click on to trigger a described action. See [[func button]] for a button [[value binding|bound]] to a function. There are various [[#types]] of buttons.

content/page_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package content
66

77
import (
88
"testing"
9+
"time"
910

1011
"cogentcore.org/core/content/bcontent"
1112
"github.com/stretchr/testify/assert"
@@ -14,6 +15,9 @@ import (
1415
func TestNewPage(t *testing.T) {
1516
pg, err := bcontent.NewPage(exampleContent, "button.md")
1617
assert.NoError(t, err)
18+
dstr := "2026-05-01"
19+
date, err := time.Parse(time.DateOnly, dstr)
20+
assert.NoError(t, err)
1721
assert.Equal(t, bcontent.Page{
1822
Source: exampleContent,
1923
Filename: "button.md",
@@ -22,7 +26,9 @@ func TestNewPage(t *testing.T) {
2226
Title: "Button",
2327
Categories: []string{"Widgets"},
2428
Authors: "Bea A. Author<sup>1</sup> and Test Ing Name<sup>2</sup>",
25-
Affiliations: "<sup>1</sup>University of Somwhere <sup>2</sup>University of Elsewhere",
29+
Affiliations: "<sup>1</sup>University of Somewhere <sup>2</sup>University of Elsewhere",
2630
Abstract: "The button is an essential element of any GUI framework, with the capability of triggering actions of any sort. Actions are very important because they allow people to actually do something.",
31+
DateString: dstr,
32+
Date: date,
2733
}, *pg)
2834
}

docs/content/text.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,13 @@ core.NewText(b).SetText("Hello,\n\tworld!").Styler(func(s *styles.Style) {
4343
s.Font.Family = rich.Monospace
4444
})
4545
```
46+
47+
The `Type` of text provides predefined size values according to the Material 3 standards, with the default being `TextBodyLarge`:
48+
49+
```Go
50+
for _, typ := range core.TextTypesValues() {
51+
s := strcase.ToSentence(typ.String())
52+
core.NewText(b).SetType(typ).SetText(s)
53+
}
54+
```
55+

paint/pdf/pdf_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func TestText(t *testing.T) {
9898
prv := UseStandardFonts()
9999
sh := shaped.NewShaper()
100100

101-
src := "PDF can put <b>HTML</b> <br>formatted Text where you <i>want</i>"
101+
src := "PDF can put <b>HTML</b> <br>formatted Text where you <i>want</i> <br>including<sup>1</sup> and sub<sub>script</sub>"
102102
rsty := &sty.Font
103103
tsty := &sty.Text
104104

paint/pdf/text.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ func (r *PDF) textRunRegions(m math32.Matrix2, run *shapedgt.Run, ln *shaped.Lin
122122
func (r *PDF) textRun(style *styles.Paint, m math32.Matrix2, run *shapedgt.Run, ln *shaped.Line, lns *shaped.Lines, runes []rune, clr image.Image, off math32.Vector2) {
123123
// dir := run.Direction
124124
region := run.Runes()
125-
offTrans := math32.Translate2D(off.X, off.Y)
126125
rbb := run.MaxBounds.Translate(off)
127126
fill := clr
128127
if run.FillColor != nil {
@@ -131,14 +130,24 @@ func (r *PDF) textRun(style *styles.Paint, m math32.Matrix2, run *shapedgt.Run,
131130
fsz := math32.FromFixed(run.Size)
132131
lineW := max(fsz/16, 1) // 1 at 16, bigger if biggerr
133132
if run.Math.Path != nil {
134-
r.w.PushTransform(offTrans)
133+
r.w.PushTransform(math32.Translate2D(off.X, off.Y))
135134
psty := *style
136135
psty.Stroke.Color = run.StrokeColor
137136
psty.Fill.Color = fill
138137
r.Path(*run.Math.Path, &psty, math32.Identity2())
139138
r.w.PopStack()
140139
return
141140
}
141+
sty := run.Font.Style(&style.Text)
142+
// https://en.wikipedia.org/wiki/Subscript_and_superscript: latex does -.14 sub, .25 super
143+
// others often use -.33 and .33
144+
if sty.Special == rich.Super {
145+
off.X += 0.05 * fsz // tends to be cramped otherwise
146+
off.Y -= 0.25 * fsz
147+
} else if sty.Special == rich.Sub {
148+
off.X += 0.05 * fsz // tends to be cramped otherwise
149+
off.Y += 0.14 * fsz
150+
}
142151

143152
idm := math32.Identity2()
144153
if run.Decoration.HasFlag(rich.Underline) || run.Decoration.HasFlag(rich.DottedUnderline) {
@@ -160,7 +169,7 @@ func (r *PDF) textRun(style *styles.Paint, m math32.Matrix2, run *shapedgt.Run,
160169
}
161170
}
162171

163-
r.w.StartTextObject(offTrans)
172+
r.w.StartTextObject(math32.Translate2D(off.X, off.Y))
164173
r.setTextStyle(&run.Font, style, fill, run.StrokeColor, math32.FromFixed(run.Size), lns.LineHeight)
165174
raw := string(runes[region.Start:region.End])
166175
r.w.WriteText(raw)

paint/renderers/htmlcanvas/text.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ func (rs *Renderer) TextRun(ctx *render.Context, run *shapedgt.Run, ln *shaped.L
120120
rs.setTransform(ctx)
121121
return
122122
}
123+
sty := run.Font.Style(&ctx.Style.Text)
124+
// https://en.wikipedia.org/wiki/Subscript_and_superscript: latex does -.14 sub, .25 super
125+
// others often use -.33 and .33
126+
if sty.Special == rich.Super {
127+
off.Y -= 0.25 * fsz
128+
} else if sty.Special == rich.Sub {
129+
off.Y += 0.14 * fsz
130+
}
123131

124132
if run.Decoration.HasFlag(rich.Underline) || run.Decoration.HasFlag(rich.DottedUnderline) {
125133
dash := []float32{2, 2}

paint/renderers/rasterx/text.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ func (rs *Renderer) TextRun(ctx *render.Context, run *shapedgt.Run, ln *shaped.L
131131
return
132132
}
133133

134+
sty := run.Font.Style(&ctx.Style.Text)
135+
// https://en.wikipedia.org/wiki/Subscript_and_superscript: latex does -.14 sub, .25 super
136+
// others often use -.33 and .33
137+
if sty.Special == rich.Super {
138+
off.Y -= 0.25 * fsz
139+
} else if sty.Special == rich.Sub {
140+
off.Y += 0.14 * fsz
141+
}
142+
134143
if run.Decoration.HasFlag(rich.Underline) || run.Decoration.HasFlag(rich.DottedUnderline) {
135144
dash := []float32{2, 2}
136145
if run.Decoration.HasFlag(rich.Underline) {

text/shaped/shaped_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,16 @@ func TestGlyphAlign(t *testing.T) {
415415
pc.DrawText(lns, pos)
416416
})
417417
}
418+
419+
func TestGlyphSuperSub(t *testing.T) {
420+
RunTest(t, "supersub", 300, 300, func(pc *paint.Painter, sh Shaper, tsty *text.Style) {
421+
src := "Super<sup>script</sup> and Sub<sub>script</sub>"
422+
sty := rich.NewStyle()
423+
sty.Size = 0.845
424+
tx, err := htmltext.HTMLToRich([]byte(src), sty, nil)
425+
assert.NoError(t, err)
426+
lns := sh.WrapLines(tx, sty, tsty, math32.Vec2(250, 250))
427+
pos := math32.Vec2(10.3, 10.96875)
428+
pc.DrawText(lns, pos)
429+
})
430+
}

0 commit comments

Comments
 (0)