/*
 * Copyright 2000-2016 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.vcs.ui;

import com.intellij.openapi.editor.impl.ComplementaryFontsRegistry;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.*;

public class FontUtil {
  @Nullable
  public static Font getFontAbleToDisplay(char c, int size, int style, @NotNull String family) {
    Font font = ComplementaryFontsRegistry.getFontAbleToDisplay(c, size, style, family, null).getFont();
    return font.canDisplay(c) ? font : null;
  }

  @NotNull
  public static String getHtmlWithFonts(@NotNull String input) {
    Font font = UIUtil.getLabelFont();
    return getHtmlWithFonts(input, font.getStyle(), font);
  }

  @NotNull
  public static String getHtmlWithFonts(@NotNull String input, int style, @NotNull Font baseFont) {
    int start = baseFont.canDisplayUpTo(input);
    if (start == -1) return input;

    Font font = null;
    StringBuilder result = new StringBuilder(input.substring(0, start));
    for (int i = start; i < input.length(); i++) {
      char c = input.charAt(i);
      if (baseFont.canDisplay(c)) {
        if (font != null) result.append("</font>");
        result.append(c);
        font = null;
      }
      else if (font != null && font.canDisplay(c)) {
        result.append(c);
      }
      else {
        if (font != null) result.append("</font>");
        font = getFontAbleToDisplay(c, baseFont.getSize(), style, baseFont.getFamily());
        if (font != null) result.append("<font face=\"").append(font.getFamily()).append("\">");
        result.append(c);
      }
    }
    if (font != null) result.append("</font>");

    return result.toString();
  }
}

@@ -676,34 +676,28 @@
 mpl.
-ComplementaryFontsRegistry
+FontFallbackIterator
 ;%0Aim
@@ -775,51 +775,8 @@
 ull;
-%0Aimport org.jetbrains.annotations.Nullable;
 %0A%0Aim
@@ -821,272 +821,8 @@
 l %7B%0A
-  @Nullable%0A  public static Font getFontAbleToDisplay(char c, int size, int style, @NotNull String family) %7B%0A    Font font = ComplementaryFontsRegistry.getFontAbleToDisplay(c, size, style, family, null).getFont();%0A    return font.canDisplay(c) ? font : null;%0A  %7D%0A%0A
   @N
@@ -1195,30 +1195,8 @@
 t;%0A%0A
-    Font font = null;%0A
     
@@ -1240,359 +1240,229 @@
 der(
-input.substring(0, start)
 );%0A
+%0A
     
-for (int i = start; i %3C input.length(); i++) %7B%0A      char c = input.charAt(i
+FontFallbackIterator it = new FontFallbackIterator(
 );%0A
-  
     i
-f (baseFont.canDisplay(c)) %7B%0A        if (font != null) result.append(%22%3C/font%3E%22);%0A        result.append(c);%0A        font = null;%0A      %7D%0A      else if (font != null && font.canDisplay(c)) %7B%0A        result.append(c);%0A      %7D%0A      else
+t.setPreferredFont(baseFont.getFamily(), baseFont.getSize());%0A    it.setFontStyle(style);%0A%0A    it.start(input, 0, input.length());%0A    while (!it.atEnd())
  %7B%0A 
@@ -1470,120 +1470,96 @@
     
-  if (
+Font 
 font 
-!
 = 
-null) result.append(%22%3C/font%3E%22);%0A  
+it.getFont();%0A%0A
       
-font = getFontAbleToDisplay(c, baseFont.getSize(), style, 
+boolean insideFallbackBlock = !font.getFamily().equals(
 base
@@ -1583,35 +1583,50 @@
 ;%0A      
-  
 if (
-font != null)
+insideFallbackBlock) %7B%0A       
  result.
@@ -1687,24 +1687,31 @@
 d(%22%5C%22%3E%22);%0A  
+    %7D%0A%0A
       result
@@ -1722,73 +1722,146 @@
 end(
-c
+input, it.getStart(), it.getEnd()
 );%0A
+%0A
       
-%7D%0A    %7D%0A    if (font != null) result.append(%22%3C/font%3E%22);
+if (insideFallbackBlock) %7B%0A        result.append(%22%3C/font%3E%22);%0A      %7D%0A%0A      it.advance();%0A    %7D
 %0A%0A  

use FontFallbackIterator for details view rendering