<commit_before>package editor

import (
	"image"
	"image/draw"
	"unicode"

	"sigint.ca/graphics/editor/internal/text"

	"golang.org/x/image/font"
	"golang.org/x/image/math/fixed"
)

const tabwidth = 4

type fontface struct {
	face   font.Face
	height int
}

func mkFont(face font.Face) fontface {
	bounds, _, _ := face.GlyphBounds('|')
	height := int(1.33*float64(bounds.Max.Y>>6-bounds.Min.Y>>6)) + 1
	return fontface{
		face:   face,
		height: height,
	}
}

// draw draws s onto dst starting at pt. It returns the cumulative advance
// in pixels of each glyph.
func (f fontface) draw(dst draw.Image, pt image.Point, l *text.Line, src *image.Uniform) {
	if l.Adv == nil {
		l.Adv = make([]int16, 1, l.RuneCount()+1)
	} else {
		l.Adv = l.Adv[0:1]
	}
	dot := fixed.P(pt.X, pt.Y+f.height-(f.height/4))
	for i, r := range l.String() {
		tab := r == '\t'
		if tab {
			// for the sake of variable-width fonts, try to pick a font with an
			// average width
			r = '_'
		}
		dr, mask, maskp, advance, ok := f.face.Glyph(dot, r)
		if !ok {
			dr, mask, maskp, advance, ok = f.face.Glyph(dot, unicode.ReplacementChar)
			if !ok {
				panic("internal error")
			}
		}
		if tab {
			advance *= tabwidth
		} else {
			draw.DrawMask(dst, dr, src, dr.Min, mask, maskp, draw.Over)
		}
		dot.X += advance
		l.Adv = append(l.Adv, l.Adv[i]+int16(advance>>6))
	}
}

// measure returns the cumulative advance in pixel for each glyph in s,
// beginning from the pixel value start.
func (f fontface) measure(l *text.Line) {
	if l.Adv == nil {
		l.Adv = make([]int16, 1, l.RuneCount()+1)
	} else {
		l.Adv = l.Adv[0:1]
	}
	for i, r := range l.Runes() {
		tab := r == '\t'
		if tab {
			// for the sake of variable-width fonts, try to pick a font with an
			// average width
			r = '_'
		}
		advance, ok := f.face.GlyphAdvance(r)
		if !ok {
			advance, ok = f.face.GlyphAdvance(unicode.ReplacementChar)
			if !ok {
				panic("internal error")
			}
		}
		if tab {
			advance *= tabwidth
		}
		l.Adv = append(l.Adv, l.Adv[i]+int16(advance>>6))
	}
}
<commit_msg>editor: fix botched conversion avoidance attempt from previous commit<commit_after>package editor

import (
	"image"
	"image/draw"
	"unicode"

	"sigint.ca/graphics/editor/internal/text"

	"golang.org/x/image/font"
	"golang.org/x/image/math/fixed"
)

const tabwidth = 4

type fontface struct {
	face   font.Face
	height int
}

func mkFont(face font.Face) fontface {
	bounds, _, _ := face.GlyphBounds('|')
	height := int(1.33*float64(bounds.Max.Y>>6-bounds.Min.Y>>6)) + 1
	return fontface{
		face:   face,
		height: height,
	}
}

// draw draws s onto dst starting at pt. It returns the cumulative advance
// in pixels of each glyph.
func (f fontface) draw(dst draw.Image, pt image.Point, l *text.Line, src *image.Uniform) {
	if l.Adv == nil {
		l.Adv = make([]int16, 1, l.RuneCount()+1)
	} else {
		l.Adv = l.Adv[0:1]
	}
	dot := fixed.P(pt.X, pt.Y+f.height-(f.height/4))
	for i, r := range l.Runes() {
		tab := r == '\t'
		if tab {
			// for the sake of variable-width fonts, try to pick a font with an
			// average width
			r = '_'
		}
		dr, mask, maskp, advance, ok := f.face.Glyph(dot, r)
		if !ok {
			dr, mask, maskp, advance, ok = f.face.Glyph(dot, unicode.ReplacementChar)
			if !ok {
				panic("internal error")
			}
		}
		if tab {
			advance *= tabwidth
		} else {
			draw.DrawMask(dst, dr, src, dr.Min, mask, maskp, draw.Over)
		}
		dot.X += advance
		l.Adv = append(l.Adv, l.Adv[i]+int16(advance>>6))
	}
}

// measure returns the cumulative advance in pixels for each glyph in s.
func (f fontface) measure(l *text.Line) {
	if l.Adv == nil {
		l.Adv = make([]int16, 1, l.RuneCount()+1)
	} else {
		l.Adv = l.Adv[0:1]
	}
	for i, r := range l.Runes() {
		tab := r == '\t'
		if tab {
			// for the sake of variable-width fonts, try to pick a font with an
			// average width
			r = '_'
		}
		advance, ok := f.face.GlyphAdvance(r)
		if !ok {
			advance, ok = f.face.GlyphAdvance(unicode.ReplacementChar)
			if !ok {
				panic("internal error")
			}
		}
		if tab {
			advance *= tabwidth
		}
		l.Adv = append(l.Adv, l.Adv[i]+int16(advance>>6))
	}
}

{'fraction_non_alphanumeric': 0.16328526032754828, 'fraction_numerical': 0.013688584698117819, 'mean_word_length': 2.8676748582230625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2587461', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1626, 'n_words': 436}