Scribus Forums

Scribus => Scripts and Plugins => Topic started by: fredzannarbor on February 05, 2022, 02:37:02 AM

Title: how to align an image frame relative to margins in scripter?
Post by: fredzannarbor on February 05, 2022, 02:37:02 AM
Hi,

I want to align an image frame centered relative the page margins in scripter, just as I would using Align & Distribute in the UI.  How to accomplish?
Title: Re: how to align an image frame relative to margins in scripter?
Post by: Nermander on February 05, 2022, 09:34:49 AM
I have no idea how to do it in the scripter, but I assume that if you can get the positions of margins and the size of the frame you can calculate the necessary numbers and change the position of the frame to those numbers.

That is kind of what the Align & Distribute does for you, but I think when using the scripter you have to code these calculations yourself.

(I have done a lot of similar calculations, not in the Scribus scripter though, but in my perl scripts for doing impositions of postscript files that I used when I wanted to typeset books from Project Gutenberg to use when learning bookbinding. There is an old wiki page about these here https://wiki.scribus.net/canvas/How_to_make_impositions_with_pstops )
Title: Re: how to align an image frame relative to margins in scripter?
Post by: RobSay on February 05, 2022, 03:06:00 PM
There aren't any direct alignment methods as per the UI options. As Nermander says - you can use getPageMargins and related methods - but then you have to calculate the desired position for the object and set this. It is also possible to use guides in the same way but I just used the calcs off page or margin as needed.

Note that if you are using facing pages with inner and outer margins then getPageMargins will return a tuple of (top,inner,outer,bottom) which is actually really useful - but you still need to know whether you are on a right or left page as the calcs are different:

It's fairly easy to write a function for 'align to margins' for a given item:
Pseudo-code to align at margin top left:

# assume left page and use outer margin
x = getPageMargins()[2]
if getPageType() == 2 :
  # 2 is a RIGHT PAGE so use inner margin
  x = getPageMargins()[1]

y = getPageMargins()[0]

moveObjectAbs(x,y,"myObjectName")


Centre alignment requires a little more maths:

viewport_width= PageWidth - (inner + outer)
leftpage_target_x = outer + (viewport_width-frame_width)/2
rightpage_target_x = inner + (viewport_width-frame_width)/2


Distribute functions are a little more complex - but do-able if you really need it.)