งานต้นฉบับสำหรับส่งโรงพิมพ์ สำหรับหน้าที่มีภาพชิดขอบ เราจำเป็นต้องเผื่อขอบให้มากกว่าขนาดกระดาษจริงด้านละ 3 มม. เพื่อกันตัดขอบพลาด ดังนั้นเวลาเตรียมต้นฉบับ ต้องเผื่อขนาดหน้าไว้ +6 มม.เสมอ
เราจะทำ Inkscape Script Extensions ด้วยไพธอน เพื่อใช้ในการนี้ในการรันแปลงไฟล์ภาพทีละหลาย ๆ ไฟล์ได้
สคริปต์ตัวอย่าง จะเป็นสคริปต์ที่ใช้ขยายหน้ากระดาษของ Inkscape ออกด้านละ 3 มม. พร้อมกับขยับวัตถุทุกอย่างขึ้นข้างบนและไปทางขวา ด้านละ 3 มม. (ทั้งนี้เวลาเตรียมงานขั้นต้น เราจะจัดให้ลงหน้าตรงกลาง และเผื่อขนาดภาพพื้นหลังออกไปทางขอบทุกด้าน ซึ่งก็เป็นธรรมชาติของการทำงานอยู่แล้ว)
จะตั้งชื่อส่วนขยายนี้ว่า expand_3mm จะต้องสร้างไฟล์ 2 ไฟล์ คือ Extension description file (.inx) และ Python script file (.py) แล้วใส่ลงในไดเรคทอรี่ ~/.config/inkscape/extensions/ หรือหากต้องการให้ใช้ได้กับทุกคนก็ใส่ลงใน /usr/share/inkscape/extensions
ดังนี้
$ vi ~/.config/inkscape/extensions/expand_3mm.inx
<inkscape-extension>
<_name>Expand 3mm</_name>
<id>org.ekips.filter.expand_3mm</id>
<dependency type="executable" location="extensions">expand_3mm.py</dependency>
<dependency type="executable" location="extensions">inkex.py</dependency>
<dependency type="executable" location="extensions">simpletransform.py</dependency>
<effect>
<object-type>all</object-type>
<effects-menu>
<submenu _name="Example"/>
</effects-menu>
</effect>
<script>
<command reldir="extensions" interpreter="python">expand_3mm.py</command>
</script>
</inkscape-extension>
$ vi ~/.config/inkscape/extensions/expand_3mm.py
#!/usr/bin/env python
# These two lines are only needed if you don't put the script directly into the installation directory
import sys
sys.path.append('/usr/share/inkscape/extensions')
# We will use the inkex module with the predefined Effect base class.
import inkex, simpletransform
class Expand3mmEffect(inkex.Effect):
"""
Example Inkscape effect extension.
"""
def __init__(self):
"""
Constructor.
"""
# Call the base class constructor.
inkex.Effect.__init__(self)
def effect(self):
"""
Effect behaviour.
"""
#MOVE 3mm,3mm
add = 10.629878067 #3mm
transformation = 'translate(%s,%s)' % (add,add,)
transform = simpletransform.parseTransform(transformation)
if self.selected:
for id, node in self.selected.iteritems():
simpletransform.applyTransformToNode(transform, node)
#EXPAND 6mm
svg = self.document.getroot()
width = float(svg.get('width')) + (add * 2) #6mm
height = float(svg.get('height')) + (add * 2) #6mm
svg.set('width','%s' % (width,))
svg.set('height','%s' % (height,))
# Create effect instance and apply it.
effect = Expand3mmEffect()
effect.affect()
# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99
เสร็จแล้ว
ทดสอบว่าสคริปต์ใช้ได้หรือไม่ ด้วยการลองเปิด inkscape ดู จะเห็นอยู่ในเมนู Extensions -> Example -> Expand 3mm
หรือผ่านบรรทัดคำสั่งว่า
$ inkscape --verb-list | grep expand_3mm
จะขึ้นมาว่า
org.ekips.filter.expand_3mm: Expand 3mm org.ekips.filter.expand_3mm.noprefs: Expand 3mm (No preferences)
เปิดไฟล์ demo.svg -> Select All in All Layers -> ขยายขนาด -> บันทึกกลับ -> ปิด inkscape
$ inkscape demo.svg --verb=EditSelectAllInAllLayers --verb=org.ekips.filter.expand_3mm --verb=FileSave --verb=FileClose
เสร็จแล้ว
/usr/share/inkscape/extensions รวมทั้งตัว inkex.py ด้วย