From Scribus Wiki
Revision as of 13:49, 6 November 2016 by Gpittman (talk | contribs) (→‎
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
This article is part of the Scripts series.

Here is a script coming out of a discussion on the mailing list. The question was how to search and replace double spaces with single spaces. This led to various suggestions, a simple one being to use the Search/Replace function in Scribus.

Some side comments in the thread were about the propriety of double versus single spaces between sentences. What you can easily find in a web search is that, prior to the invention of typewriters, publishers came up with their own conventions or styles as to dealing with spacing and other typographic features. One convention was to use an em space between sentences. When typewriters came along, presumably in a form of emulation, it was recommended to put two spaces between sentences in a paragraph.

Currently, it has become a much more common practice, especially outside the US, and certainly on the internet to only use single spaces, thus there is some utility of the following script. If you truly wanted to have em spaces between sentences, you might want to add logic that also includes converting single spaces between sentences to em spaces. This is a bit tricky, since then you must begin with the ends of sentences, which would include periods, question marks, exclamation points, and maybe even quotation marks. So a period followed by a space converts the space to em space, then any subsequent space would be deleted.

Here is the script, and after it you can see the outcome after using it.

# -*- coding: utf-8 -*-
# File: - convert double spaces to emspace or single space
# © 2014.11.08 Gregory Pittman
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
You must have a document open, and a text frame selected.
There is one dialog, which asks you if you wish to replace double spaces
with an em space. The default choice is Yes, any other input causes a double
space to convert to a single space.

import scribus
if scribus.haveDoc():
    c = 0
    scribus.messageBox('Usage Error', 'You need a Document open', scribus.ICON_NONE, scribus.BUTTON_OK)
if scribus.selectionCount() == 0:
    scribus.messageBox('Scribus - Usage Error',
        "There is no object selected.\nPlease select a text frame and try again.",
        scribus.ICON_WARNING, scribus.BUTTON_OK)
if scribus.selectionCount() > 1:
    scribus.messageBox('Scribus - Usage Error',
        "You have more than one object selected.\nPlease select one text frame and try again.", scribus.ICON_WARNING, scribus.BUTTON_OK)
textbox = scribus.getSelectedObject()
pageitems = scribus.getPageItems()
boxcount = 1
for item in pageitems:
    if (item[0] == textbox):
        if (item[1] != 4):
            scribus.messageBox('Scribus - Usage Error', "This is not a textframe. Try again.", scribus.ICON_WARNING, scribus.BUTTON_OK)
contents = scribus.getTextLength(textbox)

emspace = u"\u2003"
replace_char = " "

selection = scribus.valueDialog("Replace Character", 'Replace with em space?\n Anything but Yes will use normal space', 'Yes')
if (selection == 'Yes'):
  replace_char = emspace
prevchar = ''
while 1:
    if ((c == contents) or (c > contents)): break
    if ((c + 1) > contents - 1):
        nextchar = 'emspace'
        scribus.selectText(c+1, 1, textbox)
        nextchar = scribus.getText(textbox)
    scribus.selectText(c, 1, textbox)
    char = scribus.getText(textbox)
    if (len(char) != 1):
        c += 1

    if (char == ' '):

	if (prevchar == ' '):
	    scribus.selectText(c-1, 2, textbox)
	    scribus.insertText(replace_char, c-1, textbox)
	    char = replace_char
	    c += 1

	c += 1
    prevchar = char
    contents = scribus.getTextLength(textbox)
scribus.messageBox("Finished", "That should do it!",scribus.ICON_NONE, scribus.BUTTON_OK)


Look at this output from the script applied to some sample text. This is DejaVu Serif Book. It's worth mentioning that a number of fonts do not have an em space, so what you see in Scribus is the small rectangle indicating a missing glyph.

3kinds of spacing.png

There are four sentences here, so 3 instances of spacing between them. Before Being we see a single space. Before Fifty this is an em space, and finally before At we have a double space, keeping in mind this is a proportional font.

My personal opinion is that an em space is aesthetically much too wide for modern typography, and if anything may interfere with reading a bit – see how that Fifty seems to just hang out there on the end of that line? Comparing the single and double space in this proportional font, while it isn't an obviously unattractive appearance that two spaces have, there does not seem to be any aesthetic benefit of having the extra space, and I don't see that a single space seems to crowd the sentences together.

So, having gone through this exercise, you may prefer to use this script only for changing double spaces to a single. In that case you might just delete or comment out the following lines for the valueDialog, and maybe name the script

selection = scribus.valueDialog("Replace Character", 'Replace with em space?\n Anything but Yes will use normal space', 'Yes')
if (selection == 'Yes'):
  replace_char = emspace

Like others in this family of text-processing scripts, you can Undo the effects, but you have to click Undo once for each instance that was changed.