Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Inserting Text After Character Does Not Take Those Text Attributes

Participant ,
Sep 04, 2021 Sep 04, 2021

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:

 

  1.  Insert text after first char of replaced text.
  2. Delete first char of replaced text.
  3. Delete remainder of of replaced text.

 

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.2021-09-04_18-14-23.png

 

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.2021-09-04_18-21-09.png

 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.2021-09-04_18-24-37.png

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); 
}

 

 

TOPICS
Bug, SDK

Views

54

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Sep 06, 2021 Sep 06, 2021
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...

Likes

Translate

Translate
Adobe Community Professional ,
Sep 05, 2021 Sep 05, 2021

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"

Mike Witherell

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 05, 2021 Sep 05, 2021

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 ?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Sep 06, 2021 Sep 06, 2021

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;

 

 

PickUpFormattingofSingleCharacter.PNG

 

Regards,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 07, 2021 Sep 07, 2021

Copy link to clipboard

Copied

LATEST

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines