Skip to content

Commit

Permalink
newpaint: fixed issue with lineHeight overwriting outs; working but n…
Browse files Browse the repository at this point in the history
…ot working on web
  • Loading branch information
rcoreilly committed Mar 8, 2025
1 parent 3579df7 commit a6b70a5
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion text/shaped/shapers/shapedgt/shaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (nl *nilLogger) Printf(format string, args ...any) {}
// NewShaper returns a new text shaper.
func NewShaper() shaped.Shaper {
sh := &Shaper{}
sh.fontMap = fontscan.NewFontMap(nil) // &nilLogger{}
sh.fontMap = fontscan.NewFontMap(&nilLogger{})
// TODO(text): figure out cache dir situation (especially on mobile and web)
str, err := os.UserCacheDir()
if errors.Log(err) != nil {
Expand Down
6 changes: 3 additions & 3 deletions text/shaped/shapers/shapedgt/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ func (sh *Shaper) WrapLines(tx rich.Text, defSty *rich.Style, tsty *text.Style,
tsty.FontSize.Dots = 16
}

lht := sh.lineHeight(defSty, tsty, rts) // note: this overwrites output buffer so must do outs after!
txt := tx.Join()
outs := sh.ShapeTextOutput(tx, tsty, rts, txt)
return sh.WrapLinesOutput(outs, txt, tx, defSty, tsty, rts, size)
return sh.WrapLinesOutput(outs, txt, tx, defSty, tsty, lht, rts, size)
}

func (sh *Shaper) WrapLinesOutput(outs []shaping.Output, txt []rune, tx rich.Text, defSty *rich.Style, tsty *text.Style, rts *rich.Settings, size math32.Vector2) *shaped.Lines {
func (sh *Shaper) WrapLinesOutput(outs []shaping.Output, txt []rune, tx rich.Text, defSty *rich.Style, tsty *text.Style, lht float32, rts *rich.Settings, size math32.Vector2) *shaped.Lines {

fsz := tsty.FontSize.Dots
dir := shaped.GoTextDirection(rich.Default, tsty)

lht := sh.lineHeight(defSty, tsty, rts)
lns := &shaped.Lines{Source: tx, Color: tsty.Color, SelectionColor: tsty.SelectColor, HighlightColor: tsty.HighlightColor, LineHeight: lht}

lgap := lns.LineHeight - (lns.LineHeight / tsty.LineSpacing) // extra added for spacing
Expand Down
2 changes: 1 addition & 1 deletion text/shaped/shapers/shapedjs/font.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ func SetFontStyle(ctx js.Value, fn *text.Font, tsty *text.Style, lineHeight floa
// font: font-style font-variant font-weight font-size/line-height font-family
parts := []string{fsty.Slant.String(), "normal", fmt.Sprintf("%g", fsty.Weight.ToFloat32()), fsz, fam}
fspec := strings.Join(parts, " ")
fmt.Println("fspec:", fspec)
// fmt.Println("fspec:", fspec)
ctx.Set("font", fspec)
}
10 changes: 6 additions & 4 deletions text/shaped/shapers/shapedjs/shaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (sh *Shaper) Shape(tx rich.Text, tsty *text.Style, rts *rich.Settings) []sh
// use slices.Clone if needed longer than that.
func (sh *Shaper) ShapeAdjust(tx rich.Text, tsty *text.Style, rts *rich.Settings, txt []rune) []shaped.Run {
return sh.AdjustRuns(sh.ShapeText(tx, tsty, rts, txt), tx, tsty, rts)
// return sh.ShapeText(tx, tsty, rts, txt)
}

// AdjustRuns adjusts the given run metrics based on the html measureText results.
Expand Down Expand Up @@ -98,6 +99,7 @@ func (sh *Shaper) WrapLines(tx rich.Text, defSty *rich.Style, tsty *text.Style,
tsty.FontSize.Dots = 16
}

lht := sh.lineHeight(defSty, tsty, rts) // note: this overwrites output buffer so must do outs after!
txt := tx.Join()
outs := sh.ShapeTextOutput(tx, tsty, rts, txt)
for oi := range outs {
Expand All @@ -107,7 +109,7 @@ func (sh *Shaper) WrapLines(tx rich.Text, defSty *rich.Style, tsty *text.Style,
fnt := text.NewFont(sty, tsty)
sh.AdjustOutput(out, fnt, tx, tsty, rts)
}
lns := sh.WrapLinesOutput(outs, txt, tx, defSty, tsty, rts, size)
lns := sh.WrapLinesOutput(outs, txt, tx, defSty, tsty, lht, rts, size)
// todo: adjust again?
return lns
}
Expand All @@ -116,12 +118,12 @@ func (sh *Shaper) WrapLines(tx rich.Text, defSty *rich.Style, tsty *text.Style,
func (sh *Shaper) AdjustOutput(out *shaping.Output, fnt *text.Font, tx rich.Text, tsty *text.Style, rts *rich.Settings) {
rng := textpos.Range{out.Runes.Offset, out.Runes.Offset + out.Runes.Count}
si, sn, ri := tx.Index(rng.Start)
sty, stx := tx.Span(si)
_, stx := tx.Span(si)
ri -= sn
fmt.Println(string(stx))
// fmt.Println(string(stx))
rtx := stx[ri : ri+rng.Len()]
SetFontStyle(ctx, fnt, tsty, 0)
fmt.Println("si:", si, ri, sty, string(rtx))
// fmt.Println("si:", si, ri, sty, string(rtx))

spm := MeasureText(ctx, string(rtx))
msz := spm.FontBoundingBoxAscent + spm.FontBoundingBoxDescent
Expand Down

0 comments on commit a6b70a5

Please sign in to comment.