Difference between revisions of "Image Placement"

From Wiki
Jump to navigation Jump to search
m (optimations)
(some Lua code)
Line 1: Line 1:
< [[Using–Graphics]] >
+
< [[Using Graphics]] >
  
 
If you’re using ConTeXt for complex layout tasks, you need to calculate with image sizes. Often it makes sense to use Lua functions.
 
If you’re using ConTeXt for complex layout tasks, you need to calculate with image sizes. Often it makes sense to use Lua functions.
Line 53: Line 53:
 
\stoptabulate
 
\stoptabulate
 
</context>
 
</context>
 +
 +
== Calculations in Lua ==
 +
 +
<pre>
 +
TEXpt = 65536 -- sp per pt
 +
TEXptpi = 72.27 -- pt per inch
 +
 +
function sp2mm(n)
 +
-- convert sp into mm
 +
return math.floor(n * 35.28 / TEXpt)/100
 +
end
 +
 +
function glue2num(glue)
 +
-- convert TeX glue (e.g. skips) into dimensions(? or numbers)
 +
return glue.width + (glue.stretch * glue.stretch_order) - (glue.shrink * glue.shrink_order)
 +
end
 +
 +
function ImgSize(resolution)
 +
filename = figures.current().status.fullname -- current image
 +
local pic = img.scan{filename = filename}
 +
local picH = pic.ysize * TEXptpi * TEXpt / resolution  -- picture height in sp
 +
local picW = pic.xsize * TEXptpi * TEXpt / resolution  -- picture width in sp
 +
context("height=" .. picH .. ",width=" .. picW)
 +
end
 +
</pre>
 +
 +
((WORK IN PROGRESS))

Revision as of 20:36, 8 February 2018

< Using Graphics >

If you’re using ConTeXt for complex layout tasks, you need to calculate with image sizes. Often it makes sense to use Lua functions.

Beware, TeX calculates internally in "scaled points" (sp), 1 sp = 1/65536 pt.

Measures in pure ConTeXt

You can get at a picture’s dimensions with one line of TeX code:

\setupexternalfigures[location=default] % necessary to find pictures in tree
\getfiguredimensions[hacker.jpg]

This defines a bunch of variables:

Calculations in Lua

TEXpt = 65536 -- sp per pt
TEXptpi = 72.27 -- pt per inch

function sp2mm(n)
	-- convert sp into mm
	return math.floor(n * 35.28 / TEXpt)/100
end

function glue2num(glue)
	-- convert TeX glue (e.g. skips) into dimensions(? or numbers)
	return glue.width + (glue.stretch * glue.stretch_order) - (glue.shrink * glue.shrink_order)
end

function ImgSize(resolution)
	filename = figures.current().status.fullname -- current image
	local pic = img.scan{filename = filename}
	local picH = pic.ysize * TEXptpi * TEXpt / resolution  -- picture height in sp
	local picW = pic.xsize * TEXptpi * TEXpt / resolution  -- picture width in sp
	context("height=" .. picH .. ",width=" .. picW)
end

((WORK IN PROGRESS))