App-Music-ChordPro

 view release on metacpan or  search on metacpan

lib/ChordPro/res/abc/abc2svg/fit2box-1.js  view on Meta::CPAN

		cl = sv.match(/class="([^"]+)"/) // "
//console.log("- sv  ====\n"+sv+"\n      ====\n  cl:"+cl)
		if (!tit			// the first SVG is the tune header
		 || cl[1] == "header"
		 || cl[1] == "footer") {
			hh += +v[2]
			if (cl[1] != "header"
			 && cl[1] != "footer")
				tit = 1
			continue
		}
		if (+v[1] > w)
			w = +v[1]		// max width (thanks to trimsvg)
		h += +v[2]			// whole height
	}
//console.log("-- box:"+wb+"x"+hb+" w:"+w.toFixed(2)+" marg:"+marg.toFixed(2)
//+" h:"+h.toFixed(2)+" hh:"+hh.toFixed(2))

	sc = (hb - hh) / h			// height scale

//fixme: magic value!
	w += 24
	v = (wb - marg * 2) / w			// width scale
//console.log("     scw:"+v.toFixed(3)+" sch:"+sc.toFixed(3))

	if (v <= sc) {
		sc = v					// width constraint
	} else {					// height constraint
		v = Math.round((wb - w * sc) / 2)	// margins
		if (v < marg)
			marg = v
	}

	setparm("pagewidth", wb)
	setparm("leftmargin", marg.toFixed(0))	// restore the margins
	setparm("rightmargin", marg.toFixed(0))
	setparm("pagescale", sc)
	setparm("stretchstaff", 1)
	setparm("stretchlast", 1)
	cfmt.fullsvg = ""
	cfmt.trimsvg = 0

	// do the last generation
//console.log("---\n"+f.slice(0, 500)+"\n---")
//console.log("-> "+wb+" "+hb+" sc:"+sc.toFixed(3)+" marg:"+marg)
	mus.tunes.shift()			// remove the tune class
	user.img_out = io			// restore the normal output
	if (abc2svg.fit2box.otosvg) {		// restore the tosvg function
		mus.tosvg = abc2svg.fit2box.otosvg
		abc2svg.fit2box.otosvg = null
	}
	mus.tosvg(fn, f)
	abc2svg.fit2box.on = 0
    }, // do_fit()

    tosvg: function(of, fn, file, bol, eof) {
    var	parse = this.get_parse()

	parse.fname = fn
	parse.file = bol ? file.slice(bol) : file
	parse.eol = 0

	abc2svg.fit2box.on = 1
	abc2svg.fit2box.do_fit(this)
    }, // tosvg()

    // get a formatting parameter
    set_fmt: function(of, cmd, parm) {
	if (cmd != "fit2box")
		return of(cmd, parm)
	if (abc2svg.fit2box.on)
		return
	abc2svg.fit2box.on = 1
	if (!parm) {					// stop fit2box
		if (abc2svg.fit2box.otosvg) {		// restore the tosvg function
			this.tosvg = abc2svg.fit2box.otosvg
			abc2svg.fit2box.otosvg = null
		}
		return
	}

    var	cfmt = this.cfmt(),
	parse = this.get_parse(),
	f = parse.file

	cfmt.fit2box = parm.split(/\s+/)

	// if no tune yet, change the generation function
	if (f.indexOf("X:") < 0) {
		if (!abc2svg.fit2box.otosvg) {
			abc2svg.fit2box.otosvg = this.tosvg
			this.tosvg = abc2svg.fit2box.tosvg.bind(this, this.tosvg)
		}
		return
	}

	// do the fit2box generation now
	parse.file = parse.file.slice(parse.eol)
	parse.eol = 0
	abc2svg.fit2box.do_fit(this)
	parse.file = f
	parse.eol = parse.file.length - 2	// stop the current parsing in tosvg()
    }, // set_fmt()

    set_hooks: function(abc) {
	abc.set_format = abc2svg.fit2box.set_fmt.bind(abc, abc.set_format)
    } // set_hooks()
} // fit2box

if (!abc2svg.mhooks)
	abc2svg.mhooks = {}
abc2svg.mhooks.fit2box = abc2svg.fit2box.set_hooks



( run in 0.831 second using v1.01-cache-2.11-cpan-524268b4103 )