Copy link to clipboard
Copied
Hello,
To replace some text with some other text, so that the replacement text uses thew formatting of the first char of the replaced text, we use the following method:
This should leave us with the replacement text in the text formatting of the first char of the replaced text.
And it does, most of the time, as shown in the example below, where we apply the above algorithm to the text on the left, and we get the text on the right, where [1d] was replaced by 1 and [1MMMM] was replaced by ENERO.
This works well if the first replaced text is at the beginning of the box (as above), or if [1d] is separated from [1MMMM] by a space, or both, as in the examples below.
But, if there is any text before [1d], and there is no space between [1d] and [1MMMM], then the [MMMM] is inserted with the text attributes of the 1 in [1d], as can be seen in the example below.
Below are the relevant parts of the code that implements the above algorithm,
Did anyone ever encounter this bug ? Any suggestions ?
Very best regards,
Olivier
// We want to insert after the first character of the replaced string
// in order to get the attributes of the first character.
// Lets increase the position by 1
TextIndex position = originalPosition;
if (position < (totLenBox-1) )
position++;
ILanguage* language = nil;
// Lets try to insert text and then delete the text to be replaced
uint32 beforeLength = totLenBox;
status = textModel->Insert(position, &data, language);
uint32 lengthAfterInsertChars = textModel->TotalLength();
uint32 numInsertedChars = lengthAfterInsertChars - beforeLength;
//The position to start deleting is the position to insert text + the length of text to replace
TextIndex deletePosition = position + numInsertedChars;
// Since we inserted after the first character of the replaced string
// in order to get the attributes of the first character.
// We need to delete the first character
if (position == originalPosition + 1)
{
// delete minus the first character
status = textModel->Delete(deletePosition, lengthS-1);
// delete the first character
status = textModel->Delete(position - 1, 1);
}
Hi Olivier,
I'm not sure what you intend to do in the sample that goes wrong.
Do you want to change the text from:
[1d][1MMMM]
to:
1 ENERO
?
Then I would remove all the characters after the one that holds the right formatting and assign new contents to the character that is formatted how you like it. In ExtendScript code something like this with your sample:
// Text frame selected:
var story = app.selection[0].parentStory;
var newContents = "1 ENERO";
story.characters.itemByRange(2,-1
...
Copy link to clipboard
Copied
Not sure if this helps, but ...
What is this set to in preferences?
InDesign/Edit > Preferences > Type > "Adjust Spacing Automatically when Cutting and Pasting Words"
Copy link to clipboard
Copied
Thank you very much, Mike.
Unfortunately this made no difference.
It seems like it's a refresh issue.
If my code moves to another box, in between the 2 text replacements, then the bug does not appear.
How can I force the box's internals to update, in between the 2 text replacements ?
Copy link to clipboard
Copied
Hi Olivier,
I'm not sure what you intend to do in the sample that goes wrong.
Do you want to change the text from:
[1d][1MMMM]
to:
1 ENERO
?
Then I would remove all the characters after the one that holds the right formatting and assign new contents to the character that is formatted how you like it. In ExtendScript code something like this with your sample:
// Text frame selected:
var story = app.selection[0].parentStory;
var newContents = "1 ENERO";
story.characters.itemByRange(2,-1).contents = ""; // Empty string
story.characters[1].contents = newContents;
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Thank you very much, Uwe.
For some reason we were not aware of the existence of textModel->Replace() and were using textModel->Insert(), assuming that text placed after a specific char would inherit that character's attributes.
Or, we might have tried to do a full-on replacement, and that also sometime fails to only use the attributes of the first character. Your solution of only replacing the first charcater, and then deleting the others, was the solution.
Thank you very much again,
Olivier