Skip to main content
Rick E Johnson
Inspiring
February 7, 2023
Answered

Preserving accent characters in text handling

  • February 7, 2023
  • 1 reply
  • 439 views

How do you transfer the contents of ai::UnicodeString and std::string variables when their contents include extended accented characters? For example say a spot color has this name:

 

     Maße

 

If I have a reference to it and get its name like so:

 

err = sAICustomColor->GetCustomColorName(cColor, cName);
std::string curStr = cName.as_Platform();

 

I get this:

 

    Ma√üe

 

or this:

 

    Ma\xa7e

 

If I put the ai::UnicodeString into a text frame's text range, I can get its contents correctly as a CORE art object.

 

ai::UnicodeString spot_string; // this is already populated with extended-character encoded text
const ai::UnicodeString::UTF16Char* utf16buffer;
hdi::core::LayerUP artLayer = HDI_CORE_ILLUSTRATOR->currentDocument()->firstLayer();
bool wasLocked = artLayer->locked();
bool wasVisible = artLayer->visible();
artLayer->setLocked(false);
artLayer->setVisible(true);
hdi::core::ArtSP tmpArt = hdi::core::draw::text("", hdi::core::ArtboardPoint(0,0), "", 8, hdi::core::JustifyLeft, hdi::core::ArtColor::black(),artLayer->group().get());
AIArtHandle tmpTextFrame = tmpArt->aiArtHandle();
TextRangeRef tempRange = NULL;
AIErr error = sAITextFrame->GetATETextRange(tmpTextFrame, &tempRange);
ITextRange irange(tempRange);
irange.Remove();
ai::UnicodeString::size_type tmpsz = sAIUnicodeString->UTF_16(spot_name, utf16buffer);
irange.InsertAfter(utf16buffer, tmpsz);

std::string OK_spot_name = tmpArt->text()->textRange()->contents(); // <-------- this is where the correct text gets assigned

tmpArt->dispose();
artLayer->setLocked(wasLocked);
artLayer->setVisible(wasVisible);

 

Of course, creating and deleting temporary text objects and the many lines of code needed to accomplish that seems a pretty roundabout method for something that ought to be fairly simple. Is there a straightforward way to copy text from an ai::UnicodeString to std::string and reliably preserve non-ascii accent characters?

 

Any suggestions would be much appreciated!

This topic has been closed for replies.
Correct answer Rick E Johnson

I got an answer on this from Garrett Walbridge at Hot Door. CORE uses std::string with UTF-8 encoding almost everywhere, which works great. When I use suites not included in CORE, there are apparently some simple steps to transfer the text more seamlessly between it and the SDK. He says:

 

"If you already have an ai::UnicodeString object, you need only call its as_UTF8() method to acquire a std::string in UTF-8 encoding.

 

If you have a UTF-8 encoded std::string and want to ensure proper construction of a new ai::UnicodeString object with it, pass it to the UnicodeString constructor followed by the kAIUTF8CharacterEncoding constant to indicate the encoding."

1 reply

Rick E Johnson
Rick E JohnsonAuthorCorrect answer
Inspiring
February 14, 2023

I got an answer on this from Garrett Walbridge at Hot Door. CORE uses std::string with UTF-8 encoding almost everywhere, which works great. When I use suites not included in CORE, there are apparently some simple steps to transfer the text more seamlessly between it and the SDK. He says:

 

"If you already have an ai::UnicodeString object, you need only call its as_UTF8() method to acquire a std::string in UTF-8 encoding.

 

If you have a UTF-8 encoded std::string and want to ensure proper construction of a new ai::UnicodeString object with it, pass it to the UnicodeString constructor followed by the kAIUTF8CharacterEncoding constant to indicate the encoding."