package textmeasure

import (
	

	
	
)

type atlasGlyphMetrics struct {
	rune    rune
	frame   fixed.Rectangle26_6
	advance fixed.Int26_6
}

// Ruler faces have immutable metrics. Measure each glyph once, then search for
// the packing width using bounds alone. Only the chosen width needs a mapping.
// Arbitrary font.Face implementations retain makeSquareMapping's original
// method-call sequence, including repeated metric queries during the search.
func makeMetricsSquareMapping( font.Face,  []rune,  fixed.Int26_6) (map[rune]fixedGlyph, fixed.Rectangle26_6) {
	 := make([]atlasGlyphMetrics, 0, len())
	for ,  := range  {
		, ,  := .GlyphBounds()
		if ! {
			continue
		}
		 = append(, atlasGlyphMetrics{
			rune: ,
			frame: fixed.Rectangle26_6{
				Min: fixed.P(.Min.X.Floor(), .Min.Y.Floor()),
				Max: fixed.P(.Max.X.Ceil(), .Max.Y.Ceil()),
			},
			advance: ,
		})
	}
	 := .Metrics()
	 := .Ascent + .Descent
	 := sort.Search(int(fixed.I(1024*1024)), func( int) bool {
		 := arrangeAtlasGlyphs(, , fixed.Int26_6(), , nil)
		return .Max.X-.Min.X >= .Max.Y-.Min.Y
	})
	 := make(map[rune]fixedGlyph, len())
	 := arrangeAtlasGlyphs(, , fixed.Int26_6(), , )
	return , 
}

func arrangeAtlasGlyphs( []atlasGlyphMetrics, , ,  fixed.Int26_6,  map[rune]fixedGlyph) fixed.Rectangle26_6 {
	 := fixed.Rectangle26_6{}
	 := fixed.P(0, 0)
	for ,  := range  {
		 := .frame
		.X -= .Min.X
		 = .Add()
		if  != nil {
			[.rune] = fixedGlyph{dot: , frame: , advance: .advance}
		}
		 = .Union()
		.X = .Max.X
		.X += 
		.X = fixed.I(.X.Ceil())
		if .Max.X >=  {
			.X = 0
			.Y += 
			.Y += 
			.Y = fixed.I(.Y.Ceil())
		}
	}
	return 
}