package tex
import (
"regexp"
"strings"
)
const dimensionNumber = `([-+]?(?:[.,][0-9]+|[0-9]+(?:[.,][0-9]*)?))`
const dimensionUnit = `(pt|em|ex|mu|px|mm|cm|in|pc)`
var (
dimensionSpace = regexp .MustCompile (`^` + muDimensionSpace + `*` )
dimensionFull = regexp .MustCompile (`^` + muDimensionSpace + `*` + dimensionNumber + muDimensionSpace + `*` + dimensionUnit + muDimensionSpace + `*$` )
dimensionPrefix = regexp .MustCompile (`^` + muDimensionSpace + `*` + dimensionNumber + muDimensionSpace + `*` + dimensionUnit + ` ?` )
)
func (p *parser ) readDimension (name string ) (string , error ) {
missing := func () (string , error ) {
return "" , texError ("MissingDimOrUnits" , "Missing dimension or its units for \\%s" , name )
}
if p .pos >= len (p .source ) {
return missing ()
}
p .pos += len (dimensionSpace .FindString (p .source [p .pos :]))
if p .pos >= len (p .source ) {
return missing ()
}
if p .source [p .pos ] == '{' {
start := p .pos + 1
p .pos ++
depth := 1
for p .pos < len (p .source ) {
switch p .consumeRune () {
case '\\' :
if p .pos < len (p .source ) {
p .consumeRune ()
} else {
p .pos ++
}
case '{' :
depth ++
case '}' :
depth --
if depth == 0 {
match := dimensionFull .FindStringSubmatch (p .source [start : p .pos -1 ])
if match == nil {
return missing ()
}
return dimensionValue (match ), nil
}
}
}
return "" , texError ("MissingCloseBrace" , "Missing close brace" )
}
match := dimensionPrefix .FindStringSubmatch (p .source [p .pos :])
if match == nil {
return missing ()
}
p .pos += len (match [0 ])
return dimensionValue (match ), nil
}
func dimensionValue(match []string ) string {
return normalizeTeXMu (strings .Replace (match [1 ], "," , "." , 1 ) + match [2 ])
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .