[Ipe-discuss] ipelet: insert EPS/PDF/IPE figure

Zhengdao Wang zhengdao at iastate.edu
Wed Nov 17 00:46:06 CET 2010


I am sharing with you an Ipelet that I just put together
that performs the following:

1. Take a EPS/PDF/Ipe file, and take the first page of it.

2. Make a Ipe grouped object out of the page, and insert
   it on the current Ipe page at the center.

In the case where the figure has been produced by Ipe (but
must end with .eps or .ipe), then it should behave just like
Open the file and then Copy/Paste.

However, in other cases (EPS or PDF), all text will be
converted to paths first. This removes all fonts, but
produces a more faithful insert in Ipe. To enable such
conversion, the program depends on Ghostscript.

Ideally, this should work like the "Insert Image" ipelet
supplied with the Ipe package, but for EPS/PDF/Ipe files.

My intention was to use it to prepare presentations, so that
figures from other sources can be inserted easily.

I hope some of you find it useful.

Zhengdao
-------------- next part --------------
-----------------------------------------------------------------------
-- epspdfipe: ipelet for drawing editor Ipe to insert EPS/PDF/IPE files
-----------------------------------------------------------------------
--[[

   This file is an extension of the drawing editor Ipe (ipe7.sourceforge.net)

   Copyright (c) 2010 Zhengdao Wang

   This file can be distributed and modified under the terms of the GNU General
   Public License as published by the Free Software Foundation; either version
   3, or (at your option) any later version.

   This file is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
   details.

   You can find a copy of the GNU General Public License at
   "http://www.gnu.org/copyleft/gpl.html", or write to the Free
   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   1) Only the first page is included, even if there are multiple pages.
   2) The content will be grouped into one 'group' object in Ipe.
   3) The object will be inserted at the center of the current Ipe page.
   4) For files not parseable by Ipe, fonts will be converted to outlines.
      As such, it works best for figures that have not much text. Otherwise
	  the file can get big, and Ipe can get slow.
   5) It depends on availability of ghostscript and pdftoipe.
   6) It has not been fully tested. Absolutely NO WARRANTY.
--]]

label = "Insert EPS/PDF/IPE"

about = [[
Insert EPS/PDF/IPE files. All text converted to outlines (paths) for EPS/PDF.
]]

function run(model)
	-- get filename
	--
	dir=dir or "."

	local file=ipeui.fileDialog(nil, "open", "Import EPS/PDF File",
		"* (*.eps *.pdf *.ipe);; EPS (*.eps);;PDF (*.pdf);;IPE (*.ipe)",
		dir, nil)
	if not file then return end
	dir=string.gsub(file, "(.*/)(.*)", "%1")

	local tmpeps="/tmp/ipelet-eps.eps"
	local tmppdf="/tmp/ipelet-eps.pdf"
	local tmpipe="/tmp/ipelet-eps.ipe"

	-- convert to an Ipe file if not already one
	local format= ipe.fileFormat(file)
	if format~="xml" and format ~="eps" then
		-- convert file to IPE, all texts to outlines (paths)
		if string.sub(file:lower(),-3)=="eps" then
			command="eps2eps"
		else
			command="pdf2ps"
		end
		local ret
		ret=_G.os.execute(command ..
			" -q -dNOCACHE -dFirstPage=1 -dLastPage=1 " ..
			file .. " " .. tmpeps)
		if ret~=0 then
			model:warning ("fail to convert text to outline")
			return
		end

		ret=_G.os.execute("ps2pdf -q " .. tmpeps .. " " .. tmppdf)
		if ret~=0 then
			model:warning ("fail to convert to intermediate PDF")
			return
		end

		ret=_G.os.execute("pdftoipe " .. tmppdf .. " "
			.. tmpipe..">/dev/null 2>/dev/null")
		if ret~=0 then
			model:warning ("fail to convert to intermediate IPE")
			return
		end
		file=tmpipe
	end

	-- load the doc
	local doc = assert(ipe.Document(file))

	local layout = model.doc:sheets():find("layout")
	local fs = layout.framesize

	-- take first page from file and insert on the current IPE
	-- one group object of the page content
	for i,p in doc:pages() do
		local box = ipe.Rect()
		for i,obj,sel,layer in p:objects() do
			box:add(p:bbox(i))
		end

		local nx = (fs.x - box:width()) / 2
		local ny = (fs.y - box:height()) / 2
		local trans = ipe.Vector(nx, ny) - box:bottomLeft()
		local m = ipe.Translation(trans)
		for j = 1,#p do
			p:transform(j, m)
		end

		local elements={}
		for i,obj,sel,layer in p:objects() do
			elements[#elements+1]=obj
		end

		local group = ipe.Group(elements)
		model:creation("create graph", group)
		break
	end

	-- remove tmp files
	files={tmpeps, tmppdf, tmpipe}
	for i=1,3 do
		if ipe.fileExists(files[i]) then
			_G.os.execute("rm " .. files[i])
		end
	end
end


More information about the Ipe-discuss mailing list