1 Reply Latest reply on Aug 8, 2012 4:54 AM by MOMIR ZECEVIC

    How can we get the font names of each text layer in a document?

    poortip87 Newcomer

      Hi All!


      I wanted to know how we can get the names of fonts applied in each text layer in a psd file. I tried the following,


      int32 numLayers = 0;


      // calculate number of text layers.

      error = PIUGetInfo(classTextLayer, keyCount, &numLayers, NULL);


      for (int i = numLayers - 1; i > 0; --i)


           char* fontName = new char[100];

           int32 len = 100;


           // get font name of each layer by index.

           error = PIUGetInfoByIndex(i, classTextLayer, keyFontName, fontName, &len);



      Suppose I have a document that has 2 text layers and 2 solid color layers as shown in the screen shot. Note that this document does not have a background layer. So, number of layers are 4.



      If layer selection is at one of the solid color layers, then I get number of layers as 0. But, if the layer selection is at any of the text layers, I get number of layers as 4. Why don't I get the number of layers as 2 as I requested info of text layers in the statement,  PIUGetInfo(classTextLayer, keyCount, &numLayers, NULL).


      Also, I get error = -25922 at PIUGetInfoByIndex(i, classTextLayer, keyFontName, fontName, &len),

      which is errReferenceNotFoundDefine.


      How can I go about this?



        • 1. Re: How can we get the font names of each text layer in a document?
          MOMIR ZECEVIC Newcomer



          I usualy "remember" stati in which image is and then start to work on it. That allowes me to iterate trough layers when pulling info or performing an action and then set the document (in previous state, with changes that user wantsof course). Using that aproach i am acessing layers using index but i am also sure that layer i am working on is curently active layer.




          Getting layer numbers in a document:


          SPErr AMAutoPlugin::GetLayNum()



              SPErr error = kSPNoError;


              int32 numLayers;


              PIActionDescriptor result = NULL;

              PIActionReference reference = NULL;




              error = sPSActionReference->Make(&reference);

              if (error)goto returnError;


              error = sPSActionReference->PutEnumerated(reference,classDocument,typeOrdinal,enumTarget);

              if (error) goto returnError;


              error = sPSActionControl->Get(&result, reference);   

              if (error) goto returnError;


              error = sPSActionDescriptor->GetInteger(result, keyNumberOfLayers,&numLayers);

              if (error) goto returnError;


              stDocData.iNumLayers=numLayers;// this is just my global



              if (result != NULL)sPSActionDescriptor->Free(result);

              if (reference != NULL) sPSActionReference->Free(reference);

              return error;





          Here is a part of code (i hope that you will manage to understand it since i have cut out lots of

          code that is not needed in order to shorten the post) that fetch font names.


          I used this aproach since i have to get much more detailed info about text layers (not prsented here)

          I suppose that bu using aproprite PIUGetInfoByIndex routines you can make it much shorter.




          SPErr AMAutoPlugin::GetTextLayerAttributes(int index, TxtLayerInfo* info)




          // To clarify

          // "info" is my structure that i fill with data i need you can use somethong that suites you





              uint32 stringLength=0;

              uint32 STRL=0;


              char fontName[255]="";

              char fontStyle[255]="";

              char fontPSname[255]="";



              Boolean hasKey =false;   



              SPErr error = kSPNoError;

              DescriptorTypeID runtimeKeyID;

              DescriptorTypeID runtimeClassID;

              DescriptorTypeID runtimeTypeID;

              DescriptorTypeID runtimeUnitID;

              DescriptorTypeID runtimeEnumID;


              DescriptorClassID descClass;

              DescriptorEnumID enumValue;


              PIActionList listTextStyle=NULL;

              PIActionDescriptor descLayer = NULL;//*

              PIActionDescriptor descPosition = NULL;

              PIActionDescriptor descTextLayer = NULL;

              PIActionDescriptor descTextStyle = NULL;

              PIActionDescriptor descFont = NULL;

              PIActionDescriptor descFontColor= NULL;

              PIActionDescriptor descClickPoint=NULL;

              PIActionDescriptor desclayerLocking = NULL;

              PIActionDescriptor descEFFECTS = NULL;



              error = sPSActionDescriptor->Make(&descLayer);

              if (error) goto returnError;


              error = sPSActionDescriptor->Make(&descTextLayer);

              if (error) goto returnError;

              error = sPSActionDescriptor->Make(&descTextStyle);

              if (error) goto returnError;

              error = sPSActionList->Make(&listTextStyle);

              if (error) goto returnError;

              error = sPSActionDescriptor->Make(&descFont);

              if (error) goto returnError;





              error = PIUGetInfoByIndex(index,classLayer,0,&descLayer,NULL);

              if (error) goto returnError;




              error = PIUGetSingleItemFromDescriptor(descLayer,keyText,&descTextLayer,&descClass);

              if (error) goto returnError;





              //get font descriptor

              error = sPSActionDescriptor->GetList(descTextLayer, keyTextStyleRange, &listTextStyle);

              if (error) goto returnError;

              error = sPSActionList->GetObject(listTextStyle, 0,&descClass, &descTextStyle);

              if (error) goto returnError;

              error = PIUGetSingleItemFromDescriptor(descTextStyle,keyTextStyle,&descFont,&descClass);

              if (error) goto returnError;



              //font size

              error = sPSActionDescriptor->GetFloat(descFont, keySizeKey, &dFontSize);

              if (error) goto returnError;



              //font name

              error = sPSActionDescriptor->GetStringLength(descFont, keyFontName, &stringLength);

              if (error) goto returnError;


              error = sPSActionDescriptor->GetString(descFont, keyFontName, fontName, stringLength);

              if (error) goto returnError;


              info->sFName= fontName;





               // FONT PS NAME


              error = sPSActionControl->StringIDToTypeID("fontPostScriptName", &runtimeKeyID);

              if (error) goto returnError; 

              error = sPSActionDescriptor->GetStringLength(descFont, runtimeKeyID, &STRL);

              if (error) goto returnError;


              error = sPSActionDescriptor->GetString(descFont, runtimeKeyID, fontPSname, STRL);

              if (error) goto returnError;





              //font name style

              error = sPSActionDescriptor->GetStringLength(descFont, keyFontStyleName, &stringLength);

              if (error) goto returnError;


              error = sPSActionDescriptor->GetString(descFont, keyFontStyleName, fontStyle, stringLength);

              if (error) goto returnError;


              info->sFStyle= fontStyle;






          Hope that this helps,