14 Replies Latest reply: Sep 13, 2010 1:34 PM by A. Patterson RSS

    Setting the font

    deerowbear Community Member

      I am trying to figure out how to set the font and this is what I have come up with up so far:

       

      AIFontKey fontKey;

      error = sAIFont->FindFont( "Arial", kAIAnyFontTechnology, kRomanAIScript, true, &fontKey );

      if (error) throw (error);

      FontRef fontRef;

      error = sAIFont->FontFromFontKey( fontKey, &fontRef );

      if (error) throw (error);

      IFont iFont( fontRef);

      features.SetFont( iFont);

       

      I am not sure if I am doing this correct. I took most of this code form another post, but was  not sure how to implement the code.

       

      I am trying to set the font and am using the below code to set the font size and the position of the text:

       

      ATE::IParaFeatures justif;

      ATE::ICharFeatures features;

      justif.SetLeadingType(ATE::kRomanLeadingType);

      justif.SetStartIndent(kAIRealZero);

      justif.SetFirstLineIndent(kAIRealZero);

      features.SetTracking(kAIRealZero);

      //Justification.

      justif.SetJustification(kCenterJustify);

      //Size

      features.SetFontSize(10);

      AIArtHandle artGroup = NULL;

      error = sAIArt->GetFirstArtOfLayer(NULL, &artGroup);

      aisdk::check_ai_error(error);

      // Add the new point text item to the layer.

      AITextOrientation orient = kHorizontalTextOrientation;

      AIRealPoint anchor = {this->fEndPoint.h,this->fEndPoint.v-4};

      AIArtHandle textFrame = NULL;

      error = sAITextFrame->NewPointText(kPlaceAboveAll, artGroup, orient, anchor, &textFrame);

      aisdk::check_ai_error(error);

      // Set the contents of the text range.

      TextRangeRef range = NULL;

      error = sAITextFrame->GetATETextRange(textFrame, &range);

      aisdk::check_ai_error(error);

      ITextRange crange(range);

      crange.SetLocalCharFeatures(features);

      crange.SetLocalParaFeatures(justif);

      crange.InsertAfter("X");

       

       

      Any help would help thanks daniel

        • 1. Re: Setting the font
          A. Patterson Community Member

          What behaviour are you seeing? I have this all working in our plugin, so I should be able to help you figure this out, but first I need to know what is & isn't working. Just the font? Everything?

          • 2. Re: Setting the font
            deerowbear Community Member

            When I execute the code, which it compiles, my plugin fails to create a text block. If I comment out this line

             

            AIFontKey fontKey;

            error = sAIFont->FindFont( "Arial", kAIAnyFontTechnology, kRomanAIScript, true, &fontKey );

            if ( error ) throw ( error );

            FontRef fontRef;

            error = sAIFont->FontFromFontKey( fontKey, &fontRef );

            if ( error ) throw ( error );

            IFont iFont( fontRef );

            features.SetFont( iFont );

             

            My plugin creates text that is justified and the correct font size.

             

            I think the error is in FindFont().

             

            I am not sure If passing "Arial" as a parameter is correct???

            • 3. Re: Setting the font
              A. Patterson Community Member

              Arial should be a fine thing to search for, I'm not sure why that would be the problem. Next question though is: where does it fail in that block? Does it throw after the FindFont? Or later on? Also, initialize FontRef to zero and then make sure its not still zero on the other side of the FontFromFontKey() call. Lastly, you can ask the IFont object if its null using IFont::IsNull().


              Basically, let's inspect every variable through that block and see if we can figure out which one's going off the rails. The ATE can be really finicky, and when its gets its back up it blows up at the drop of a hat. It's pretty powerful, but it can be a real pain to work with at times.

              • 4. Re: Setting the font
                A. Patterson Community Member

                Actually, upon reviewing my code a little more, I'm going to change my mind about 'Arial'. I think you may need to be more specific, like 'Arial Narrow' or 'Arial Black' or 'Arial Regular'. FindFont seems to want the PostScript name of the font, and that includes the font style I believe. Try that first.

                • 5. Re: Setting the font
                  deerowbear Community Member

                  I put in this code, and it must be breaking at the FindFont method.

                   

                  If I put the messagebox before the findfont function is called i get feed back, if I do it after the fact I get nothing.

                   

                  Also, I  set the value of fontRef = 0 and changed the first argument for FindFont to "Arial Narrow"

                   

                  It however still errors out. Thanks for helping me out so far. Would you have anymore ideas?

                   

                  AIFontKey fontKey;

                  MessageBox(NULL, (char*)fontKey, "Font Ref", 0);

                  error = sAIFont->FindFont( "Arial Narrow", kAIAnyFontTechnology, kRomanAIScript, true, &fontKey );

                   

                  • 6. Re: Setting the font
                    A. Patterson Community Member

                    It errors out after the FindFont call? You can translate the error code (its a pain) which might shed some light. If you pause on the line after the FindFont call and go to the Watch window (the one where you can enter whatever you want). Enter 'error' and then make sure its showing in hex. Then on another line in the Watch window, type

                     

                    (char)0xAA

                     

                    where AA is the first two hex digits from the error code. This should give you (hopefully) an ASCII character. Repeat this for each pair of hex digits from error. Then reverse the order, it should spell out something. Something like 'PARM' or 'Tgnf' or '!Fnt'. From that, we might be able to deduce what its complaining about.

                    • 7. Re: Setting the font
                      deerowbear Community Member

                      I am not sure about how to get illustrator and the IDE to interact. Can you please give me direction. I know how to add a watch, but if I am executing the code, how does it know to run through illustrator?

                       

                      I tried adding this code, but i still was not getting out any useful data:

                       

                       

                       

                          char buffer[sizeof(long)*8+1];

                          MessageBox(NULL, ltoa(error, buffer, 10), "Font Ref", 0);

                       

                      Thanks daniel

                      • 8. Re: Setting the font
                        A. Patterson Community Member

                        No, if you stick the number into a message box, it won't be very helpful. You need to cast each byte (pair of hex digits) to a character code in order to decode them (and they don't always mean anything). Otherwise you just get a number. Alternately, you could just take that number and punch it into the calculator, convert to hex and then look up each hex pair in an Ascii table.

                         

                        As for connecting it to hte IDE, just specify the Illustrator.exe as the executable to debug. When it starts up, any breakpoints will appear to be 'not available', but at some point during the load the plugin will load & the breakpoints will light up.

                        • 9. Re: Setting the font
                          deerowbear Community Member

                          Is it normal to receive this error:

                          debug_error.JPG

                           

                          I can't seem to be able to add a watch even though the dialog appears. I can't even right click on a variable to watch it...any ideas?

                          • 10. Re: Setting the font
                            A. Patterson Community Member

                            Yeah, that error is normal. Its basically just telling you that you won't be able to debug Illustrator since you don't have the source code. Obviously, that's pretty reasonable

                             

                            As for the watch, I'm talking about the one in the panel, not 'Add Quickwatch' or whatever its called.

                            • 11. Re: Setting the font
                              deerowbear Community Member

                              Okay, I'll try to get the watch to work correctly. It seems to skip over the pause and just launch illustrator. This code actually worked in terms of changing the font, but of course not everyones font number 10 will be the same:

                               

                              FontRef fontRef;

                              AIFontKey fontKey = NULL;

                              //For instance, 10 is a number of the font in the font list.

                              error = sAIFont -> IndexFontList(10, &fontKey );

                              error = sAIFont -> FontFromFontKey ( fontKey, &fontRef );

                              ATE::IFont iFont( fontRef );

                              features.SetFont( iFont );

                              • 12. Re: Setting the font
                                A. Patterson Community Member

                                Heh, well, at least font 10 is working So yeah, its just the font name searching we're figuring out.

                                 

                                As for the debugging, it is just launching Illustrator. Fortunately, Illustrator loads your plugin so that puts you in the debugger too You should be able to add a breakpoint, do whatever it is that runs that code and have it stop.

                                • 13. Re: Setting the font
                                  deerowbear Community Member

                                  Okay, here is what I decided to do...it is lame, but it works. Thanks for the help. : )

                                   

                                  char buf[250], fonttext[500];

                                  short forfont;

                                  long fontCount = 0;

                                  int font_value;

                                  error = sAIFont->CountFonts(&fontCount);

                                  //char buffer[sizeof(long)*8+1];

                                  //MessageBox(NULL, ltoa(fontCount, buffer, 10), "Font Num", 0);

                                  aisdk::check_ai_error(error);

                                  for (int i = 0; i < fontCount; i++)

                                  {

                                  // Report each font name to the log.

                                  AIFontKey fontKey = NULL;

                                  error = sAIFont->IndexFontList(i, &fontKey);

                                  aisdk::check_ai_error(error);

                                   

                                  error = sAIFont->GetUserFontName(fontKey,fonttext,forfont);

                                  if(strcmp(fonttext, "Arial Bold") == 0)

                                  {

                                  font_value = i;

                                  break;

                                  }

                                  else

                                  {

                                  font_value = -1;

                                  }

                                  aisdk::check_ai_error(error);

                                  }

                                  FontRef fontRef;

                                  AIFontKey fontKey = NULL;

                                  //For instance, 10 is a number of the font in the font list.

                                  error = sAIFont -> IndexFontList(font_value, &fontKey );

                                  error = sAIFont -> FontFromFontKey ( fontKey, &fontRef );

                                  ATE::IFont iFont( fontRef );

                                  features.SetFont( iFont );

                                  • 14. Re: Setting the font
                                    A. Patterson Community Member

                                    Ironically, this isn't far off what I do. I actually wrote my own font manager class and I preload it when the plugin loads. The font manager has a list of font family objects, each of which has a list of font styles (like 'Narrow', 'Bold', etc.). Each of these is associated with a font reference, though I use FindFont to look them up. It's possible that 'Arial Narrow' isn't what it wants, but rather 'ArialNarrow' or 'Arial-Narrow' or something else. I'd have to debug into my font manager and watch for Arial to know for sure, since I don't actually care; I just load all fonts -- like you're doing now -- and then I store the names they give. After that, I use that same object to populate combo boxes, etc., and to set fonts.

                                     

                                    So I don't think you're doing must different than me, I just wouldn't suggest right off the bat that someone go to that length One minor suggestion: if you're doing this in a loop, cache that font ref!

                                     

                                    Anyways, glad you got something working. The text engine is a pain, so post any other questions if they come up and I'll try and address them if I can. I'm no text expert by any stretch of the imagination.