6 Replies Latest reply: Feb 24, 2012 8:45 AM by perigee_rde RSS

    Set Color for Text

      Hi,<br /><br />we need something to set the color for text i'm adding. I use the CHMLFiltTextHelper to add text so i decided to add a function AddColor there.<br /><br />so i searched the sdk samples, found some code about setting the color (SnpApplyTextStyleAttributes.cpp) and i wrote this function:<br /><br />>void CHMLFiltTextHelper::AddColor( const double red , const double green , const double blue ) <br />{<br />  this->checkInvariant();<br />     InterfacePtr<ITextAttrUID> textAttrUID_Color(::CreateObject2<ITextAttrUID>(kTextAttrColorBoss));<br />     if (textAttrUID_Color != nil) {<br />       const int32 colorSpace = ( kPMCsCalRGB );     // RGB color space.<br />          ColorArray rgbColor;<br />               <br />>          rgbColor.push_back( red ); // Red (0.0...1.0)<br />          rgbColor.push_back( green ); // Green (0.0...1.0)<br />          rgbColor.push_back( blue ); // Blue (0.0...1.0)<br />               <br />>    InterfacePtr<ICommand> newColorCmd( CmdUtils::CreateCommand( kNewColorCmdBoss ) );<br />               <br />>          if( newColorCmd != nil ) {<br />               InterfacePtr<IColorData> colorData(newColorCmd, UseDefaultIID());<br />               if( colorData ) {<br />b                 // Indesign crashed in the next line<br />                    colorData->SetColorData( rgbColor );<br /><br />>                    ErrorCode error = CmdUtils::ProcessCommand(newColorCmd);<br />                    if (error == kSuccess) {<br />                         const UIDList* colorUIDList = newColorCmd->GetItemList();<br />                         if (colorUIDList != nil) {<br />                              UID colorUID = colorUIDList->First();<br />                              textAttrUID_Color->SetUIDData( colorUID );<br />                              ASSERT(this->fCharAttributeBossList);<br />                              this->fCharAttributeBossList->ApplyAttribute( textAttrUID_Color );<br />                         }<br />                    }<br />               }<br />          }<br />  }<br />}<br /><br />But it doesn't work, InDesign crashed at this line:<br />>                     colorData->SetColorData( rgbColor );<br /><br />What is wrong here?<br /><br />Thanks<br />Klaus<br /><br />PS: The Forum kills all the indents in the code, hope it is readable ..
        • 1. Re: Set Color for Text
          Dirk Becker  Community Member
          I guess (in other words, did not try) the size of the array does not match the requirements of the preset color space. See the comment on SetColorData, even if it is phrased a bit weak "Typically color space is set first ...".

          Dirk
          • 2. Re: Set Color for Text
            Dirk Becker  Community Member
            Regarding indents, see here:
            http://www.adobeforums.com/webx?128@@.3bc170eb
            • 3. Re: Set Color for Text
              Community Member
              I used SetColorData with the ColorSpace <br />i kPMCsCalRGB<br />before - same result.<br /><br />Thanks for the hint with the indent, next try with the code:<br /><pre><br />void CHMLFiltTextHelper::AddColor( const double red , const double green , const double blue ) { <br />  this->checkInvariant(); <br />  InterfacePtr<ITextAttrUID> textAttrUID_Color(::CreateObject2<ITextAttrUID>(kTextAttrColorBoss)); <br />  if (textAttrUID_Color != nil) { <br />    const int32 colorSpace = ( kPMCsCalRGB ); // RGB color space. <br />    ColorArray rgbColor; <br />  <br />    rgbColor.push_back( red ); // Red (0.0...1.0) <br />    rgbColor.push_back( green ); // Green (0.0...1.0) <br />    rgbColor.push_back( blue ); // Blue (0.0...1.0) <br />  <br />    InterfacePtr<ICommand> newColorCmd( CmdUtils::CreateCommand( kNewColorCmdBoss ) ); <br />    if( newColorCmd != nil ) { <br />      InterfacePtr<IColorData> colorData(newColorCmd, UseDefaultIID()); <br />      if( colorData ) { <br />        colorData->SetColorData( rgbColor ); <br />  <br />        ErrorCode error = CmdUtils::ProcessCommand(newColorCmd); if (error == kSuccess) { <br />          const UIDList* colorUIDList = newColorCmd->GetItemList(); <br />          if (colorUIDList != nil) { <br />            UID colorUID = colorUIDList->First(); <br />            textAttrUID_Color->SetUIDData( colorUID ); <br />            ASSERT(this->fCharAttributeBossList); <br />            this->fCharAttributeBossList->ApplyAttribute( textAttrUID_Color ); <br />          } <br />        } <br />      } <br />    } <br />  } <br />}<br /></pre>
              • 4. Re: Set Color for Text
                Community Member
                Trying a different version now:<br /><br /><pre><br />void CHMLFiltTextHelper::AddColor( UID ColorUID )<br />{<br />  do {<br />    this->checkInvariant();<br />    // Our text style will be normal, except<br />    // the text will be colored blue.  Since<br />    // blue is not a normal swatch color, we<br />    // will search for it, and if we don't<br />    // find it, we'll create it.<br />    InterfacePtr<ITextAttrUID> textAttrUID(::CreateObject2<ITextAttrUID>(kTextAttrColorBoss));<br />    if (textAttrUID != nil) {<br />      textAttrUID->SetUIDData( ColorUID );<br />      ASSERT(this->fCharAttributeBossList);<br />      this->fCharAttributeBossList->ApplyAttribute(textAttrUID);<br />    }<br />  } while(kFalse);<br />}<br /></pre><br /><br />but it still doesn't work. Now InDesign crashes when i call <br />i Apply();<br /><br />Any tipps or ideas?
                • 5. Re: Set Color for Text
                  Community Member
                  Nobody tried ever to set the color for Text?
                  • 6. Re: Set Color for Text
                    perigee_rde

                    I may be a little bit late, but try this :

                     

                    ErrorCode MyTextUtils::ApplyTextAttributes(ITextModel * textModel, int32 position, int32 length, UID attributeUID, ClassID classID)

                    {

                      ErrorCode rc = kFailure;

                      do {

                           InterfacePtr<ITextAttrUID> textAttrUID(::CreateObject2<ITextAttrUID>(classID));

                     

                           // Set our color attribute and store it:

                           textAttrUID->SetUIDData(attributeUID);

                     

                           InterfacePtr<ICommand> pApplyTextAttrCmd ( Utils<ITextAttrUtils>()->BuildApplyTextAttrCmd(textModel, position,  length, textAttrUID, kCharAttrStrandBoss));   

                            rc = CmdUtils::ProcessCommand(pApplyTextAttrCmd);

                     

                        } while(false);

                     

                        return rc;              

                    }

                     

                     

                    Use kTextAttrColorBoss for the classID, and the UID of your color for attributeUID