⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftoldmac.c

📁 Demo for Free type 2.2.1
💻 C
📖 第 1 页 / 共 2 页
字号:
  printf("           --font-listing-api={quickdraw_old|quickdraw|ats}\n" );
  printf("           specify API to list system font\n" );
  printf("\n" );
  printf("           --font-resolve-api={quickdraw|ats}\n" );
  printf("           specify API to find fontfile by fontname\n" );
  printf("\n" );
  printf("           --auto-suffix\n" );
  printf("           old QuickDraw API cannot list available style suffixes,\n" );
  printf("           this option adds Bold and Italic suffixes automatically.\n" );
  printf("\n" );
}


void
verifyFMOutput( FMOutput*  fmout )
{
  OSErr    err;
  Handle   font_handle;
  short    rsrcID;
  ResType  rsrcType;
  Str255   rsrcName;



#ifdef VERBOSE
  printf( "FMOutput.boldPixels   0x%02x\n", fmout->boldPixels );
  printf( "FMOutput.italicPixels 0x%02x\n", fmout->italicPixels );
  printf( "FMOutput.ulOffset     0x%02x\n", fmout->ulOffset );
  printf( "FMOutput.ulShadow     0x%02x\n", fmout->ulShadow );
  printf( "FMOutput.ulThick      0x%02x\n", fmout->ulThick );
  printf( "FMOutput.shadowPixels 0x%02x\n", fmout->shadowPixels );
  printf( "FMOutput.extra        0x%02x\n", fmout->extra );
  printf( "FMOutput.curStyle     0x%02x\n", fmout->curStyle );
#else
  printf( "FMOutput.widMax:%d\n", fmout->widMax );
#endif

  font_handle = fmout->fontHandle,
  GetResInfo( font_handle, &rsrcID, &rsrcType, rsrcName );
  err = ResError();
  if ( 0 != err )
    printf( "could not get resource info for handle 0x%08x, err=%d\n",
            (int)(fmout->fontHandle), err );
  else
    printf( "resource: ID=0x%04x, Type=%c%c%c%c, name=%s\n",
             rsrcID,
             (int)MAX( (rsrcType >> 24) & 0xFF, 0x20 ),
             (int)MAX( (rsrcType >> 16) & 0xFF, 0x20 ),
             (int)MAX( (rsrcType >>  8) & 0xFF, 0x20 ),
             (int)MAX( (rsrcType >>  0) & 0xFF, 0x20 ),
             rsrcName[0] > 0 ? rsrcName + 1 : '\0' );
}


FT_OldMac_Err
resolveToolBoxQuickDrawFontName( const char*  font_name )
{
#if !defined( HAVE_QUICKDRAW_CARBON ) || ( QUICKDRAW_CARBON == 0 )
  printf( "cannot check [%s] by Carbon QuickDraw\n", font_name );
  return FT_OldMac_Err_Unimplemented;
#else
  Str255     familyName;
  SInt16     familyID;
  FMInput    lookup_inst;
  FMOutput*  lookup_rslt;


  ft_strncpy( (char*)familyName + 1, (char*)font_name, 254 );
  familyName[0] = strlen( (char *)familyName + 1 );
  GetFNum( familyName, &familyID );
  if ( 0 > familyID )
  {
    printf( "familyName %s is unresolvable\n", familyName + 1 );
    return FT_OldMac_Err_Unresolvable;
  }
  else if ( 0 == familyID )
    return FT_OldMac_Err_PseudoFontName;

  printf( "familyName %s:%d -> familyID 0x%04x: -> ",
           familyName + 1, familyName[0],
           familyID );

  bzero( &lookup_inst, sizeof( FMInput ) );
  lookup_inst.family   = familyID;
  lookup_inst.size     = 14;
  lookup_inst.face     = 0;
  lookup_inst.needBits = FALSE;
  lookup_inst.device   = 0;
  lookup_inst.numer.v  = 1;
  lookup_inst.numer.h  = 1;
  lookup_inst.denom.v  = 1;
  lookup_inst.denom.h  = 1;

  lookup_rslt = FMSwapFont( &lookup_inst );
  if ( NULL == lookup_rslt )
  {
    printf( "FMSwapFont returns NULL (unresolved)\n" );
    return FT_OldMac_Err_UnswappableFontID;
  }
  else
    verifyFMOutput( lookup_rslt );
  return FT_OldMac_Err_Ok;
#endif
}


void
test_face_quickdraw( char*       face_name,
                     FT_Library  library    )
{
  FSSpec   spec;
  UInt8    font_file_path[1024];
  FT_Long  face_index; 
  FT_Face  face;


  printf( "Lookup [%s]...", face_name );
  if ( 0 != FT_GetFile_From_Mac_Name( face_name, &spec, &face_index ) )
  {
    printf( "FreeType could not find font file\n" );
    return;
  }

  ftmac_FSpMakePath( &spec, font_file_path, 1024 );
  printf( "Font file found [%s], face #%d...", font_file_path, (int)face_index );
  if ( 0 != FT_New_Face_From_FSSpec( library, &spec, face_index, &face ) )
  {
    printf( "FreeType could not load font file\n" );
    return;
  }
  printf( "Ok\n" );

  num_opened_faces ++;
  dump_face_info( face );
  FT_Done_Face( face );
}


void
test_face_ats( char*       face_name,
               FT_Library  library    )
{
#if !defined( HAVE_ATS ) || ( HAVE_ATS == 0 )
  FT_UNUSED( library );
  printf( "cannot check [%s] by ATS\n", face_name );
#else
  FSSpec   spec;
  UInt8    font_file_path[1024];
  FT_Long  face_index; 
  FT_Face  face;


  printf( "Lookup [%s]...", face_name );
  if ( 0 != FT_GetFile_From_Mac_ATS_Name( face_name, &spec, &face_index ) )
  {
    printf( "FreeType could not find font file\n" );
    return;
  }

  ftmac_FSpMakePath( &spec, font_file_path, 1024 );
  printf( "Font file found [%s], face #%d...", font_file_path, (int)face_index );
  if ( 0 != FT_New_Face_From_FSSpec( library, &spec, face_index, &face ) )
  {
    printf( "FreeType could not load font file\n" );
    return;
  }
  printf( "Ok\n" );

  num_opened_faces ++;
  dump_face_info( face );
  FT_Done_Face( face );
#endif
}


void
test_face( char*       face_name,
           FT_Library  library    )
{
  num_scanned_faces ++;
  if ( 0 == ft_strcmp( font_resolve_api, "quickdraw" ) )
    test_face_quickdraw( face_name, library );
  else if ( 0 == ft_strcmp( font_resolve_api, "ats" ) )
    test_face_ats( face_name, library );
  else
  { 
    printf( "invalid api name to resolve [%s]\n", font_resolve_api );
    exit( -1 );
  }
}


void
test_font_list_quickdraw_old( FT_Library  library )
{
  Str255  fmo_family_name;
  char    fmo_face_name[1024];
  int     i;


  for ( i = 0; i < 0x7FFF; i++ )
  {
    GetFontName( i, fmo_family_name );
    if ( 0 < fmo_family_name[0] )
    {
      fmo_family_name[ fmo_family_name[0] + 1 ] = '\0';

      ft_strncpy( (char *)fmo_face_name, (char *)fmo_family_name + 1, 1024 );
      test_face( fmo_face_name, library );

      if ( !auto_suffix )
        continue;

      ft_strncpy( (char *)fmo_face_name, (char *)fmo_family_name + 1, 1024 );
      strncat( (char *)fmo_face_name, " Bold", 1024 );
      printf( "+ " );
      test_face( fmo_face_name, library );

      ft_strncpy( (char *)fmo_face_name, (char *)fmo_family_name + 1, 1024 );
      strncat( (char *)fmo_face_name, " Italic", 1024 );
      printf( "+ " );
      test_face( fmo_face_name, library );

      ft_strncpy( (char *)fmo_face_name, (char *)fmo_family_name + 1, 1024 );
      strncat( (char *)fmo_face_name, " Bold Italic", 1024 );
      printf( "+ " );
      test_face( fmo_face_name, library );
    }
  }
}


char*
make_style_suffix( char*        fm_style_name,
                   FMFontStyle  fm_style      )
{
  fm_style_name[0] = ' ';
  fm_style_name[1] = '\0';
  if ( fm_style & bold      ) strcat( fm_style_name, "Bold " );
  if ( fm_style & italic    ) strcat( fm_style_name, "Italic " );
  if ( fm_style & underline ) strcat( fm_style_name, "Underline " );
  if ( fm_style & outline   ) strcat( fm_style_name, "Outline " );
  if ( fm_style & shadow    ) strcat( fm_style_name, "Shadow " );
  if ( fm_style & condense  ) strcat( fm_style_name, "Condense " );
  if ( fm_style & extend    ) strcat( fm_style_name, "Extend " );
  if ( ft_strlen( fm_style_name ) > 0 )
    fm_style_name[ strlen( fm_style_name) - 1 ] = '\0';

  return fm_style_name;
}


void
test_font_list_quickdraw( FT_Library  library )
{
#if !defined( HAVE_QUICKDRAW_CARBON ) || ( QUICKDRAW_CARBON == 0 )
  FT_UNUSED( library );
  printf( "cannot get font list by Carbon QuickDraw\n" );
#else
  FMFontFamilyIterator          fm_family_iter;
  FMFontFamily                  fm_family;
  Str255                        fm_family_namestr;
  char                          fm_family_name[1024];

  FMFontFamilyInstanceIterator  fm_font_iter;
  FMFont                        fm_font;
  FMFontStyle                   fm_style;
  FMFontSize                    fm_size;
  char                          fm_style_name[1024];



  if ( 0 != FMCreateFontFamilyIterator( NULL, NULL,
                                        kFMUseGlobalScopeOption,
                                        &fm_family_iter ) )
    return;
  
get_quickdraw_font_family:
  if ( 0 == FMGetNextFontFamily( &fm_family_iter, &fm_family ) )
  {
    if ( 0 == FMCreateFontFamilyInstanceIterator( fm_family, &fm_font_iter ) )
    {
get_quickdraw_font_instance:
      if ( 0 == FMGetNextFontFamilyInstance( &fm_font_iter, &fm_font, &fm_style, &fm_size ) )
      {
        if ( 0 == fm_size )
        {
          FMGetFontFamilyName( fm_family, fm_family_namestr );
          CopyPascalStringToC( fm_family_namestr, fm_family_name );
          strcat( fm_family_name, make_style_suffix( fm_style_name, fm_style ) );
          test_face( fm_family_name, library );
        }
        goto get_quickdraw_font_instance;
      }
      else
      {
        FMDisposeFontFamilyInstanceIterator( &fm_font_iter );
        goto get_quickdraw_font_family;
      }
    }
    else
      goto get_quickdraw_font_family;
  }

  FMDisposeFontFamilyIterator( &fm_family_iter );
  return;
#endif
}


void
test_font_list_ats( FT_Library  library )
{
#if !defined( HAVE_ATS ) || ( HAVE_ATS == 0 )
  FT_UNUSED( library );
  printf( "cannot get font list by ATS\n" );
#else
  ATSFontIterator  ats_font_iter;
  ATSFontRef       ats_font_ref;
  CFStringRef      ats_font_name;
  char             face_name[1024];


  if ( noErr != ATSFontIteratorCreate( kATSFontContextGlobal,
                                       NULL, NULL,
                                       kATSOptionFlagsUnRestrictedScope,
                                       &ats_font_iter ) )
    return;

  while ( noErr == ATSFontIteratorNext( ats_font_iter, &ats_font_ref ) )
  {
    if ( 0 != ATSFontGetName( ats_font_ref, kATSOptionFlagsUnRestrictedScope, &ats_font_name ) )
      continue;

    if ( NULL == ats_font_name )
      continue;

    if ( CFStringGetCString( ats_font_name, face_name, 1024, kCFStringEncodingNonLossyASCII ) )
      test_face( (char *)face_name, library );
  }
  return;
#endif
}


void
test_system_font_list()
{
  FT_Library  library;


  printf( "fontlist is generated by [%s]\n", font_listing_api );
  printf( "fontname is resolved by  [%s]\n", font_resolve_api );

  if ( 0 != FT_Init_FreeType( &library ) )
    return;

  if ( 0 == ft_strcmp( font_listing_api, "quickdraw_old" ) )
    test_font_list_quickdraw_old( library );
  else if ( 0 == ft_strcmp( font_listing_api, "quickdraw" ) )
    test_font_list_quickdraw( library );
  else if ( 0 == ft_strcmp( font_listing_api, "ats" ) )
    test_font_list_ats( library );
  else
  { 
    printf( "invalid api name to list [%s]\n", font_listing_api );
    exit( -1 );
  }

  FT_Done_FreeType( library );
}


int
main( int     argc,
      char**  argv )
{
  int  i;


  num_scanned_fonts = 0;
  num_opened_fonts  = 0;
  num_scanned_faces = 0;
  num_opened_faces  = 0;

  font_listing_api = NULL;
  font_resolve_api = NULL;
  auto_suffix      = FALSE;
  max_face_number  = 0;
  force_scan_face  = FALSE;

  if ( 1 == argc )
    print_help();
  else
  {
    for ( i = 1; i < argc; i++ ) 
    {
      if ( 0 == ft_strcmp( "--help", argv[i] ) )
      {
        print_help();
        exit( 0 );
      }
      else if ( 0 == ft_strncmp( "--font-listing-api=", argv[i], 19 ) )
        font_listing_api = argv[i] + 19;
      else if ( 0 == ft_strncmp( "--font-resolve-api=", argv[i], 19 ) )
        font_resolve_api = argv[i] + 19;
      else if ( 0 == ft_strcmp( "--auto-suffix", argv[i] ) )
        auto_suffix = TRUE;
      else if ( 0 == ft_strncmp( "--max-face-number=", argv[i], 18 ) )
        max_face_number = atoi( argv[i] + 18 );
      else if ( 0 == ft_strcmp( "--force-scan-face", argv[i] ) )
        force_scan_face = TRUE;
    }
  }

  if ( NULL == font_listing_api && NULL == font_resolve_api )
    test_font_files( argc, argv );
  else if ( NULL != font_listing_api && NULL != font_resolve_api )
    test_system_font_list( argc, argv );
  else if ( NULL == font_listing_api )
    printf( "require --font-listing-api to specify how to list system font\n" );
  else if ( NULL == font_resolve_api )
    printf( "require --font-resolve-api to specify how to find file by fontname\n" );

  printf( "\n" );
  printf( "---------------------------------\n" );
  printf( "Summary\n" );
  printf( "Font File opened/scanned = %d/%d\n", num_opened_fonts, num_scanned_fonts );
  printf( "Font Face opened/scanned = %d/%d\n", num_opened_faces, num_scanned_faces );
  exit( 0 );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -