/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.editor.richcopy;

import com.intellij.openapi.diagnostic.Logger;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Java logical font names (like 'Monospaced') don't necessarily make sense for other applications, so we try to map those fonts to
 * the corresponding physical font names.
 */
public class FontMapper {
  private static final Logger LOG = Logger.getInstance("#" + FontMapper.class.getName());

  private static final String MAC_OS_FONT_CLASS = "sun.font.CFont";
  private static final String[] logicalFontsToMap = {Font.DIALOG, Font.DIALOG_INPUT, Font.MONOSPACED, Font.SERIF, Font.SANS_SERIF};
  private static final Map<String, String> logicalToPhysicalMapping = new HashMap<String, String>();

  static {
    /*try {
      FontManager fontManager = FontManagerFactory.getInstance();
      for (String logicalFont : logicalFontsToMap) {
        String physicalFont = null;
        Font2D font2D = fontManager.findFont2D(logicalFont, OptionsConstants.DEFAULT_EDITOR_FONT_SIZE, 0);
        if (font2D == null) {
          continue;
        }
        else if (font2D instanceof CompositeFont && ((CompositeFont)font2D).getNumSlots() > 0) {
          physicalFont = ((CompositeFont)font2D).getSlotFont(0).getFamilyName(Locale.getDefault());
        }
        else if (font2D.getClass().getName().equals(MAC_OS_FONT_CLASS)) {
          Field field = Class.forName(MAC_OS_FONT_CLASS).getDeclaredField("nativeFontName");
          field.setAccessible(true);
          physicalFont = (String)field.get(font2D);
        }
        if (physicalFont != null) {
          logicalToPhysicalMapping.put(logicalFont, physicalFont);
        }
      }
    }
    catch (Throwable e) {
      LOG.warn("Failed to determine logical to physical font mappings");
    }*/
  }

  public static String getPhysicalFontName(String logicalFontName) {
    String mapped = logicalToPhysicalMapping.get(logicalFontName);
    return mapped == null ? logicalFontName : mapped;
  }
}

@@ -692,26 +692,133 @@
 er;%0A
-%0Aimport java.awt.*
+import org.jetbrains.annotations.NotNull;%0A%0Aimport java.awt.*;%0Aimport java.lang.reflect.Field;%0Aimport java.lang.reflect.Method
 ;%0Aim
@@ -841,16 +841,41 @@
 ashMap;%0A
+import java.util.Locale;%0A
 import j
@@ -1192,76 +1192,8 @@
 );%0A%0A
-  private static final String MAC_OS_FONT_CLASS = %22sun.font.CFont%22;%0A
   pr
@@ -1437,18 +1437,16 @@
 c %7B%0A    
-/*
 try %7B%0A  
@@ -1449,17 +1449,24 @@
 %7B%0A      
-F
+Object f
 ontManag
@@ -1472,22 +1472,74 @@
 ger 
-fontManager = 
+= null;%0A      try %7B%0A        fontManager = Class.forName(%22sun.font.
 Font
@@ -1552,29 +1552,368 @@
 rFactory
+%22)
 .get
-Instance(
+Method(%22getInstance%22).invoke(null);%0A      %7D%0A      catch (ClassNotFoundException e) %7B%0A        // expected for JRE 1.6. FontManager.findFont2D method is static there, so leaving fontManager value as null will work%0A      %7D%0A      Method findFontMethod = Class.forName(%22sun.font.FontManager%22).getMethod(%22findFont2D%22, String.class, int.class, int.class
 );%0A     
@@ -2004,22 +2004,22 @@
         
-Font2D
+Object
  font2D 
@@ -2025,246 +2025,411 @@
  = f
-ontManager.findFont2D(logicalFont, OptionsConstants.DEFAULT_EDITOR_FONT_SIZE, 0);%0A        if (font2D == null) %7B%0A          continue;%0A        %7D%0A        else if (font2D instanceof CompositeFont && ((CompositeFont)font2D).getNumSlots() %3E 0) %7B
+indFontMethod.invoke(fontManager, logicalFont, Font.PLAIN, 0);%0A        if (font2D == null) %7B%0A          continue;%0A        %7D%0A        String fontClassName = font2D.getClass().getName();%0A        if (%22sun.font.CompositeFont%22.equals(fontClassName)) %7B // Windows and Linux case%0A          Object physicalFontObject = Class.forName(%22sun.font.CompositeFont%22).getMethod(%22getSlotFont%22, int.class).invoke(font2D, 0);
 %0A   
@@ -2455,46 +2455,59 @@
  = (
-(CompositeFont)f
+String)Class.forName(%22sun.font.F
 ont2D
+%22
 ).get
-SlotFont(0).
+Method(%22
 getF
@@ -2515,17 +2515,60 @@
 milyName
-(
+%22, Locale.class).invoke(physicalFontObject, 
 Locale.g
@@ -2612,64 +2612,63 @@
 if (
-font2D.getClass().getName().equals(MAC_OS_FONT_CLASS)) %7B
+%22sun.font.CFont%22.equals(fontClassName)) %7B // MacOS case
 %0A   
@@ -2706,25 +2706,24 @@
 ame(
-MAC_OS_FONT_CLASS
+%22sun.font.CFont%22
 ).ge
@@ -3086,18 +3086,16 @@
 );%0A    %7D
-*/
 %0A  %7D%0A%0A  
@@ -3107,16 +3107,29 @@
 c static
+%0A  @NotNull%0A 
  String 
@@ -3148,16 +3148,25 @@
 ontName(
+@NotNull 
 String l

make FontMapper compile and work with JRE 1.6 (IDEA-67767)