// Copyright 2017-2022 The MathJax Consortium
// SPDX-License-Identifier: Apache-2.0
// This file is a Go translation and modification of MathJax 3.2.2.

// Package layout implements MathJax's common TeX layout rules.
package layout import // BigDimen is MathJax's sentinel for an unset or effectively infinite // dimension. const BigDimen = 1_000_000 // FullWidth is the percentage-width marker used by MathJax tables. const FullWidth = "100%" // BBox is MathJax's common-output bounding box. Dimensions are in ems. // L and R are space outside W, RScale is relative to the parent, IC is the // italic correction, Skew is the accent skew, and DX is the combining-accent // offset. type BBox struct { W, H, D float64 Scale float64 RScale float64 L, R float64 PWidth string IC float64 Skew float64 DX float64 } // NewBBox returns the initialized zero-width box used by MathJax. An omitted // height or depth is represented by -BigDimen. func (, , float64) *BBox { return &BBox{W: , H: , D: , Scale: 1, RScale: 1} } // ZeroBBox returns a fully specified zero box. func () *BBox { return NewBBox(0, 0, 0) } // EmptyBBox returns a box whose height and depth have not been set. func () *BBox { return NewBBox(0, -BigDimen, -BigDimen) } // Empty prepares b for Append and Combine operations. func ( *BBox) () *BBox { .W = 0 .H = -BigDimen .D = -BigDimen return } // Clean replaces unset dimensions with zero. func ( *BBox) () { if .W == -BigDimen { .W = 0 } if .H == -BigDimen { .H = 0 } if .D == -BigDimen { .D = 0 } } // Rescale multiplies the three geometric dimensions. It intentionally does // not modify spaces or scale metadata, matching MathJax 3.2.2. func ( *BBox) ( float64) { .W *= .H *= .D *= } // Combine expands b to include child at offset (x,y). func ( *BBox) ( *BBox, , float64) { := .RScale // JavaScript rounds each product before adding the offset. Keep those // rounding points when Go may otherwise fuse the multiply and addition. := + jscompat.NumberStep(*(.W+.L+.R)) := + jscompat.NumberStep(*.H) := jscompat.NumberStep(*.D) - if > .W { .W = } if > .H { .H = } if > .D { .D = } } // Append places child after the current width and expands height and depth. func ( *BBox) ( *BBox) { := .RScale // The source's width increment also rounds before the compound addition. .W += jscompat.NumberStep( * (.W + .L + .R)) if *.H > .H { .H = * .H } if *.D > .D { .D = * .D } } // UpdateFrom overwrites b's geometric dimensions and copies a non-empty // percentage width. func ( *BBox) ( *BBox) { .H = .H .D = .D .W = .W if .PWidth != "" { .PWidth = .PWidth } } // Clone returns an independent copy. func ( *BBox) () *BBox { if == nil { return nil } := * return & }