OPTIMISATION OF AUTOMATIC VARIABLE GRAPHIC LAYOUT AND IMPOSITION

Original scientific paper The benefit of automation in graphic prepress lies primarily in an increase of efficiency in the production of layout and imposition of repetitive tasks by shortening lead time. Another benefit is cost reduction, since more work can be done with fewer staff members. One way of automating is by using scripts. Scripts can be written to execute just one task, complex scripts can perform multiple tasks and some scripts automate the entire publishing process. With scripts that execute complex tasks execution period can be time consuming. This paper describes the script that was made to automate variable data layout, imposition and to number digital printing. Quantitative research was carried out in this paper to determine achieved automation and time reduction by using automated processes. This paper also deals with written script optimization in order to shorten the time of its execution and to identify factors that affect the script execution time.


Introduction
Automatic layout and composition technology is of great value to end-to-end digital publishing solutions because it can relieve or eliminate the bottleneck of creating documents composed of highly customized text and image contents.It is also a very challenging technical problem since it involves 2D optimization of positions and dimensions of multiple types of contents: images, texts and vector graphics [1].
There has been extensive research in this area.One of the earliest efforts may be attributed to the Juno-2 constraint-based drawing editor, developed by Heydon and Nelson in the early 1990s [2].Jacobs et al. [3] introduced an adaptive document layout system that automatically selects the best template for given contents.Purvis et al. [4] formalized the creation of personalized documents as a multi-objective optimization problem and used a genetic algorithm to automatically assemble such documents.Constrained satisfaction, learning and genetic algorithms are other techniques that have been explored in automated layout systems [5,6].Johari et al. [7] created a specialized pagination and layout system for yellow pages.Berkner et al. [8] introduced a method to intelligently scale picture and text portions of an image by utilizing information available in the JPEG2000 file.There were also propositions on image layout algorithms to automate the design of photo albums [9,10].
Many researchers in the field of automated layout find problematic the fact that many layouts are created manually by a designer [4,5,11,12].This has led to research in automated layout of information.
The most common technique for doing automated layout is the use of templates.There are also some available software solutions which automate the data flow and creation of page layout according to rules or templates which are produced manually.There are also solutions based on scripts or plug-ins for DTP (Desktop publishing) software.
Some advantages of automatic publishing are: -The production will increase by shortening lead time [12].-The production cost will decrease since more work can be done with fewer working forces.-There is a potential for automatic generation of personal documents by using digital presses [11].-Styling a large amount of text and flowing content into templates are examples of work that should be automated since this kind of work is highly repetitive, can easily fail, and is tiresome to be carried out manually.-Automation can be set on in non-working time of the company [13].
Criteria for job selection in automatic publishing: -The design is rule or template based with simple or regular design and layout -The material consists of many pages -The material is frequently published with data originating from a database, or they can be made by a script -The work is tedious to be done manually [13].
In this paper authors decided to investigate a different approach to automation.Instead of automation of graphic layout in general, the decision has been made to automate only certain parts of the layout process by identifying jobs that were suitable for automation with scripting.Technical Gazette 23, 1(2016), 91-98 The goal of the research was to prove that with new proposed models, which use scripting languages, graphic layout can be optimised by increasing speed, reliability and automation level of the process.Hypothesis was that by analysing graphic prepress it is possible to detect jobs suitable for automation by means of scripting.Jobs that need a lot of manual labour and that are repetitive can be optimally automated.
Automation does not always pay-off, since it requires development of many scripts [12].
Decision on job selection was made by observing graphic prepress processes.Criteria for job selection were as follows: the job had to be a repetitive task (not creative) and written script could be used for different jobs of the same nature.
This project aimed at analysing the problem in its real context.The aim was also to give recommendations of practical value.Selected job was a production of automatic variable graphic layout, imposition and numbering.Created automation script was tested and optimised to determine factors that affect script time.
Variable data printing is produced by commercial tools that suffice the needs of digital printing but most of them are either expensive or they are linked to a certain digital printer machine producer.Acquiring such tools for digital printers is often an expense that is difficult to remand.Avoiding such costs can be done by implementing new models [14].Scripting is an option that could be used to implement new models for printing on demand.
By using scripts in DTP programs graphic prepress can be automated and time savings can be obtained [15].It is known that certain tasks in graphic layout can be solved by scripting, but it is not known how to use such technologies optimally and whether larger time savings can be achieved by optimising written scripts.Assumption is made that by optimising written scripts larger time savings can be made.
Results from this paper give knowledge that can increase usage of scripting technologies, as well as guidelines on script optimisation.
For short scripts optimisation is not necessary but for scripts that execute with longer time it is advisable to optimise them.With script optimisation execution time can be significantly reduced.Operator waiting time is thus also reduced, and the operator is able to continue working in the program accomplishing other necessary tasks.

Scripting in DTP programs
DTP programs that are used today facilitate the production of graphic layout for print preparation and epublications.One of the implemented features is also the ability to write scripts.A script is a series of statements that instruct an application to perform a set of tasks.
Adobe InDesign is one of the standard and most popular DTP software for professional use today.Scriptable Adobe applications support several scripting languages.In Mac OS AppleScript and JavaScript can be used and in Windows VBScript (Visual Basic or VBA) and JavaScript [16].Adobe provides an extended implementation of JavaScript, called ExtendScript that is used by many Adobe applications that provide a scripting interface.In addition to implementing the JavaScript language according to the ECMA JavaScript specification, ExtendScript provides certain additional features and utilities [17].The ExtendScript interpreter conforms to the current, ECMA 262 standard for JavaScript.All language features of JavaScript 1.5 are supported [18].
A problem of JavaScript programs is slow execution speed.That is because JavaScript programs are usually executed by interpreters and JavaScript has many dynamic features which must be checked at runtime.Performance improvement through the use of code optimization is an important method for making JavaScript a proper choice for building high quality software.Because a JavaScript statement executes many machine instructions, slight changes of JavaScript source code can greatly improve the performance.Code optimization can be statically applied by using source level transformation.JavaScript compilers can also adopt code optimization to generate faster target code.Even JavaScript interpreters can utilize code optimization techniques at runtime [19].
Guidelines for writing JavaScript code [20] are known and for writing efficient code one should adhere to them.But with ExtendScript the situation is different.ExtendScript uses JavaScript syntax but there are additional features and utilities that change the language.There is insufficient literature on optimising ExtendScript code even from the manufacturer Adobe System Incorporated.Information is unavailable on Adobe ExtendScript engine interpretation.
The original script that was written for testing had long execution time.Hypothesis was that by altering script code execution time could be improved and tests have been carried out to determine factors that could optimize execution time of ExtendScript code.

Script used for measurements
Script used for testing was a script written to automate production of variable graphic layout, imposition and numbering for digital printing.The final product of the script is a collection of pages, with an imposed PDF document, which has placed numeration on it.Script is used for preparation of sheets for digital printing of numerated tickets, vouchers, coupons and other similar numerated materials.
Operation of the script is as follows: a) Determining imposition sheet size and a PDF document that will be imposed (user input).b) Setup of imposed item information: width, height, bleeds size, number of numerated blocks per item, leading zero number for number creation (user input).c) Positioning blocks for numeration on the imported item (user action).d) Imposition setup by choosing one of the two optionswith item rotation for 90 degrees or without (user choice).
e) Imposition and drawing crop marks (script) f) Numeration setup by choosing if the numbers are incremented over the sheet or through imposed sheets (for numerated blocks of materials) and the range of numbers (user choice).g) Numeration (script).
Script execution process can be seen on Fig. 1.Fig. 2 shows the final product after script execution.

Time measurements
Time measurements were conducted by a script with "$.hiresTimer" -High resolution timer option which gives time in microseconds [21].
Time given in this paper is average time derived from 5 conducted measurements.Script time is given as imposition, numeration and idle time.
All steps of script execution except numeration and imposition are user influenced so when determining script optimization time they were not measured.User time was only included in the final comparison of manual and script time.
Idle time is time that passes after script execution until the moment when the operator can continue his operation in the program (it includes screen redraw and other tasks conducted by the program after script execution).Idle time is also measured by the script with event listeners.InDesign idle tasks are executed when there are no other tasks in the application processing order to be executed [22].
Script and manual layout time were all measured by doing the same imposition with one PDF document.Because the script is dynamic and execution depends on user selection all measurements were done with this fixed parameters: imposed sheet size 450×320 mm; PDF document size 2,32 MB; PDF imposed item size on the sheet 85×54 mm, bleeds 3 mm, imposition option set to head up -20 items per sheet, no leading zeroes, numeration options set to increment through the imposed sheets with numeration range from 1 to 1000.

Measuring automation
Automation was measured as number of clicks and keystrokes with a program WhatPulse 2.3.1 (http://www.whatpulse.org/).The numbers were compared between jobs being carried out as manual process and executed by the script.

Results and discussion 4.1 Execution time of the original script
Testing options; number of numerated blocks per item on the imposition: 1, 2, 3, 4 and 5. From the results in Tab. 1 it can be concluded that the imposition time is short and that by adding numeration frames it doesn't increase significantly.Numeration and idle time are high and both of them should be optimized.

Testing doScript method by setting UndoModes
Testing options; number of numerated blocks per item on the imposition: 2.
One of the script optimizations that Adobe advises is using doScript method and its optional parameter UndoModes which determines how the program will manage its Undo option while executing a script.
Adobe claims: "InDesign gives you the ability to undo almost every action, but this comes at a price: for almost every action you make, InDesign writes to disk.Tools presented by the user interface used for normal operation do not present any problem.For scripts, which can perform thousands of actions in the time a human being can blink, the constant disk access can be a serious drag on performance.The doScript method offers a way around this bottleneck performance by providing two parameters that control the way that scripts are executed relative to InDesign's Undo behaviour." [22] "UndoModes" [22] options in Table 2 and Fig. 3   From Tab. 2 and Fig. 3 it can be concluded that the claims found in Adobe documentation in [22] are not true.While using UndoModes parameter in doScript method execution time was not optimized.Instead, execution time was increased by 3÷4 times.In scenarios 4. UndoModes .scriptRequest:Undo each script action as a separate event and 5. UndoModes parameter is not set time is the same as in the original script.This is because scripts execute the same way.The predefined option of doScript method is to write all Undo steps.

Using linked instead of separate text frames
Testing options; number of numerated blocks per item on the imposition: 1, 2, 3, 4 and 5.
Original script uses separate text frames for number placements.To determine if linked frames have an effect on script execution time the script was changed to use linked frames while placing numbers in text frames.
Original script execution while numerating: The beginning of the numeration process is when imposition is created on the master page with all text frames placed on items (inserted PDF document).While adding new pages to the document, imposition will get copied on new pages and text frames are accessible by overriding them from the master page.
Numerating starts by adding necessary pages to the document.Number of pages is derived from user input of numeration range and items per sheet.As it is mentioned in 3.3 Time measurements numerating is done by placing first number on the first page and second number on the second page in the same text frame from master page.Numeration option is set to increment through the imposed sheets (as opposed to numeration incrementing consecutively on a single page).Script uses 2 nested for loops to accomplish that.Outer loop is used to loop through frames on the page and inner is used to loop through the pages.In the inner loop selected text frame gets overridden and created number is placed in the frame.No linking of the frames is done.

Changes in the original script:
This script uses extra for loops.First for loop is used to override all text frames in the document.Then it uses extra for loop for linking text frames with consecutive numbering through the pages.Final for loop is used to place all the numbers at once in one linked story.This for loop has a nested loop for creating consecutive numbers that will get placed in the outer loop.

Original script Linked frames
Tab. 3 and Fig. 4 show results after conducting the tests.From the results shown in Fig. 4 and by comparing time values in Tab. 1 and Tab. 3 it can be concluded that using linked frames instead of separate frames for text placement is better.Script using linked frames is in average 2,4 times quicker i.e. total time of script execution is more than halved.The greatest reduction in time was with idle time.Reasons for that could be the size of the file and Adobe InDesign script execution optimization for using linked text frames.Comparison of file sizes of original script and linked frames script is given in Tab. 4 and Fig. 5.

Using everyItem() for retrieval of data in Collection objects
Testing options; number of numerated blocks per item on the imposition: 1, 2, 3, 4 and 5.
Time measurements in this test were done on prepared documents with 50 pages.The same master pages were used like in the previous tests; with number of frames varying from 1÷5 per PDF item.Script was only unlocking, overriding, frames from the master.Used scripts are shown in Fig. 6.Fig. 7 shows results of time measurements.From the tests a conclusion can be derived that if a script should change properties of all items in a Collection script will execute in less time if everyItem() is used.From Fig. 7 it can be concluded that if a Collection object is big (it has a greater number of objects in it) the difference in execution time is also bigger as opposed to working with single object access.From this test it can be also concluded that the number of DOM accesses also has great impact on execution time.By using variables instead of extra DOM accesses execution time can be reduced.

Testing the execution speed of while and for loops
Tests to determine the execution speed of while and for loops were done with a specially written script for the tests.Used script can be seen in Fig. 8 and results are shown in Fig. 9.  From the results it can be concluded that for loop executes in shorter time than while loop.Execution time of while loop is dependent on the way the loop executes, if it executes by incrementing or decrementing the variable in the condition.If the while loop uses decrementing it will execute in shorter time.These tests of possible optimization of the code were a JavaScript optimization.The script did not have any access to the DOM.Time savings were relatively small, except in the last example with 100.000.000iterations of the loops, a situation that is not so common.By applying this optimization to the script really small time savings can be obtained.Because of that it can be concluded that by optimizing functions or tasks that do not access the DOM larger time savings in script execution time can't be obtained.

Final script optimisation
Testing options; number of numerated blocks per item on the imposition: 1, 2, 3, 4 and 5.
To prepare the script for use all the conclusions from tests and literature research were implemented in the final script.These optimizations were performed: Managing variables scope; When JavaScript code is being executed, execution context is created.The execution context (sometimes called the scope) defines the environment in which code is to be executed.A global execution context is created upon script start, and additional execution contexts are created as functions are executed, ultimately creating an execution context stack where the topmost context is the active one.
Identifier resolution performance is directly related to the number of objects to search in the scope chain.The farther up the scope chain an identifier exists, the longer the search goes on and the longer it takes to access that variable.If scopes are not managed properly, they can negatively affect the execution time of the script.[23] Because of the above mentioned reasons all variables used in the script were created as local variables as to shorten the script execution time because of execution context and smaller scope chain while resolving identifiers.

Working with Collection objects;
JavaScript regards everyItem() as a single object, even though any property or method being invoked on that entity sends a command that multiple actual objects will receive.This mechanism is known as a verb-first command: instead of invoking the specified method on each receiver a single method performs the action (or verb) on any number of objects [24].

Saving resolved references to the DOM in variables;
Script was changed to save in local variables resolved references to the DOM that will be used multiple times.Thus once resolved references do not have to get resolved multiple times and by saving resolved references number of DOM accesses is smaller [24].
The best option for working with Collection object references was function getElements().This function resolves references to Collection objects and saves them to a field.

Using only for loops;
Final script uses only for loops that were optimized not to access DOM while testing conditions.

Doing script tasks with DOM access locality
One of the biggest optimizations in script execution time was gained by rewriting the code in a way that it utilizes DOM access locality.The principle behind this optimization is shown in Figure 10.
In the script used for testing this was done by joining 2 for loops that were used in the linked frames script.In the linked frames script first for loop was used only to override frames on pages by using everyItem() and the second for loop was used for linking frames.When this two for loops were joined in one optimal script execution and significant time savings were gained.This is shown on Fig. 11.
From the above statement it can be concluded that when using DOM access time and space locality in a way that all tasks are performed on near objects in the DOM, or on objects that were recently selected, great time savings can be obtained.Non-optimized way of accomplishing tasks would be to solve tasks one at a time on every object as it can be seen in Fig. 10.By optimization, the script execution time can be shortened from 3 to 4 times.With longer scripts greater time savings can be achieved by optimizing.From Tab. 5 and Fig. 12 it can be concluded that by optimizing ExtendScript code significant time savings can be gained, especially for scripts that execute with long time.
Table 6 shows results after script optimization.Total time for all measurements was reduced by average 70 %, that is optimized script runs in only 30 % of original script time.
This proves the hypothesis that by optimising written scripts larger time savings can be made.
In these final tests comparison between manual and script job execution is made.For determining automation the number of clicks and keystrokes necessary for job execution were counted.The numbers were compared between jobs carried out as manual process and when executed by the script.Tab.7 shows test results.The results were varying around 96 % for user input reduction and automation level was quite high.
The results supported the hypothesis that by analysing graphic prepress it is possible to detect jobs suitable for automation by means of scripting.Jobs that need a lot of manual labour and that are repetitive can be optimally automated.
Comparison between measured time of manual and script execution are given in Tab. 8.
This proves the former hypothesis that by using scripting savings in operational time can be obtained.Results show that the manual time of job production can be reduced for about 94 % by using script automation.This statement is only true when time to write and optimize scripts is not added to the production time.Time for script creation can be dismissed if the scripts are written for repetitive jobs and on different jobs of the same nature.
It is important to mention that manual time and user input by clicks and keystrokes are highly dependent on the program operator and his experience and knowledge in program and layout process.Results given in this paper are obtained by 5 measurements from 5 different experienced operators.Single results for one job can vary more than 50 % in click and keystroke count and 30 % in time measurements.

Conclusion
With scripting graphic prepress processes can be automated and time savings in layout processes can be achieved.By identifying jobs that are suitable for automation with scripting and automating only parts of the layout processes high automation level in graphic prepress can be obtained.Scripts should be written so that they can be used in different jobs of the same nature.
Scripts used for graphic prepress automation can execute with long time.While the script is running the operator cannot operate the program and execute other manual or script tasks.When scripts have long execution time they should be optimized.By script execution optimization in certain situations smaller document sizes can be achieved.
Tests conducted in this paper show that script optimization can lead to high time savings in script execution time (in the tested example time saving were 70 % better than the original script time).Greatest optimizations can be obtained when optimizing parts of the script that access the DOM.Higher time savings are obtained with long run scripts.
From conducted tests following conclusions and guidelines for script optimization have been made; -When using UndoModes parameter of doScript method script execution time can be prolonged, so time tests should be made before using this option.-When working with a lot of frames on pages it is desirable to work with linked frames instead of separate frames.This ensures better script execution time and smaller files sizes.-Working with Collection objects is recommended whenever possible (using everyItem() to set the properties of Collection objects) -DOM accesses should be as little as possible, because they take longer time to execute.-For loops with fixed conditions (do not access the DOM in every condition check) should be preferred.-All variables should be set to local variables as to shorten the script execution time because of execution context and smaller scope chain while resolving identifiers.-Saving resolved references into local variables (preferred usage of getElements() method or saving other references in variables and reusing them when necessary).-When using DOM access time and space locality should be used; tasks should be performed on near objects in the DOM, or on objects that were recently selected.It is better to select one object from DOM and execute all tasks on that object than executing one task at a time on all objects.

Figure 1 Figure 2
Figure 1 Steps in script execution process

Figure 4
Figure 4 Comparison of script execution time for original and linked frames script

Figure 5
Figure 5 Comparison of file sizes in MB of InDesign documents produced by original and linked frames script

Figure 6
Figure 6 Scripts used for tests on working with Collection objects

Figure 7
Figure 7 Execution time comparison of scripts using everyItem() to work with Collection objects, and scripts that use single and extra DOM object access

Figure 8
Figure 8 Scripts used for tests on for and while loop execution speeds

Figure 10 Figure 11
Figure 10 Principle for optimising script execution time with DOM access locality

Table 1
Original script execution time in seconds are: 1. UndoModes.autoUndo-add no events to the Undo queue; 2. UndoModes.entireScript-put a single event in the Undo queue; 3. UndoModes.fastEntireScript-put a single event in the Undo queue; 4. UndoModes .scriptRequest:Undo each script action as a separate event; 5. UndoModes parameter is not set 6. Original script without doScript method.

Table 2
Script execution time in seconds measured while varying values of UndoModes parameter of doScript method

Table 3
Script execution time in seconds measured for a script using liked frames

Table 4
File sizes in MB of InDesign documents (extension .indd)produced by original and linked frames script

Table 5
Final script execution time in seconds

Table 6
Total time savings with script optimisation

Table 7
Script and manual job execution; difference between user inputs by clicks and key strokes

Table 8
Script and manual job execution; difference between average total time of job execution in seconds