Scribusoid

Author Topic: how to align an image frame relative to margins in scripter?  (Read 318 times)

fredzannarbor

  • Newbie
  • *
  • Posts: 9
  • Karma: 0
  • Scribus User
how to align an image frame relative to margins in scripter?
« on: February 05, 2022, 03: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?

Nermander

  • Hero Member
  • *****
  • Posts: 1522
  • Karma: 19
Re: how to align an image frame relative to margins in scripter?
« Reply #1 on: February 05, 2022, 10: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 )

RobSay

  • Newbie
  • *
  • Posts: 40
  • Karma: 0
  • Scribus User
Re: how to align an image frame relative to margins in scripter?
« Reply #2 on: February 05, 2022, 04: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:
  • Left Page, align to margin: x = Outer
  • Right Page, align to margin: x = Inner

It's fairly easy to write a function for 'align to margins' for a given item:
Pseudo-code to align at margin top left:
Code: [Select]
# 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:
Code: [Select]
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.)