from __future__ import print_function, division, absolute_import

from fontTools.misc.py23 import *

from ufo2ft.featureCompiler import FeatureCompiler
from ufo2ft.kernFeatureWriter import KernFeatureWriter
from ufo2ft.markFeatureWriter import MarkFeatureWriter
from ufo2ft.outlineCompiler import OutlineOTFCompiler, OutlineTTFCompiler
from ufo2ft.postProcessor import PostProcessor


__version__ = "0.6.2"


def compileOTF(ufo, outlineCompilerClass=OutlineOTFCompiler,
               featureCompilerClass=FeatureCompiler,
               kernWriterClass=KernFeatureWriter, markWriterClass=MarkFeatureWriter,
               glyphOrder=None, useProductionNames=True, optimizeCFF=True,
               roundTolerance=None, removeOverlaps=False):
    """Create FontTools CFF font from a UFO.

    *removeOverlaps* performs a union operation on all the glyphs' contours.

    *optimizeCFF* sets whether the CFF table should be subroutinized.

    *roundTolerance* (float) controls the rounding of point coordinates.
      It is defined as the maximum absolute difference between the original
      float and the rounded integer value.
      By default, all floats are rounded to integer (tolerance 0.5); a value
      of 0 completely disables rounding; values in between only round floats
      which are close to their integral part within the tolerated range.
    """

    outlineCompiler = outlineCompilerClass(
        ufo, glyphOrder, roundTolerance=roundTolerance,
        removeOverlaps=removeOverlaps)
    otf = outlineCompiler.compile()

    featureCompiler = featureCompilerClass(
        ufo, otf, kernWriterClass=kernWriterClass, markWriterClass=markWriterClass,
        mtiFeatures=_getMtiFeatures(ufo))
    featureCompiler.compile()

    postProcessor = PostProcessor(otf, ufo)
    otf = postProcessor.process(useProductionNames, optimizeCFF)

    return otf


def compileTTF(ufo, outlineCompilerClass=OutlineTTFCompiler,
               featureCompilerClass=FeatureCompiler,
               kernWriterClass=KernFeatureWriter, markWriterClass=MarkFeatureWriter,
               glyphOrder=None, useProductionNames=True,
               convertCubics=True, cubicConversionError=None,
               reverseDirection=True, removeOverlaps=False):
    """Create FontTools TrueType font from a UFO.

    *removeOverlaps* performs a union operation on all the glyphs' contours.

    *convertCubics* and *cubicConversionError* specify how the conversion from cubic
    to quadratic curves should be handled.
    """
    outlineCompiler = outlineCompilerClass(
        ufo, glyphOrder, convertCubics=convertCubics,
        cubicConversionError=cubicConversionError,
        reverseDirection=reverseDirection,
        removeOverlaps=removeOverlaps)
    otf = outlineCompiler.compile()

    featureCompiler = featureCompilerClass(
        ufo, otf, kernWriterClass=kernWriterClass, markWriterClass=markWriterClass,
        mtiFeatures=_getMtiFeatures(ufo))
    featureCompiler.compile()

    postProcessor = PostProcessor(otf, ufo)
    otf = postProcessor.process(useProductionNames)

    return otf


def _getMtiFeatures(ufo):
    features = {}
    prefix = "com.github.googlei18n.ufo2ft.mtiFeatures/"
    for fileName in ufo.data.fileNames:
        if fileName.startswith(prefix) and fileName.endswith(".mti"):
            content = tounicode(ufo.data[fileName], encoding="utf-8")
            features[fileName[len(prefix):-4]] = content
    return features if len(features) > 0 else None

@@ -94,16 +94,81 @@
 port *%0A%0A
+from ufo2ft.preProcessor import OTFPreProcessor, TTFPreProcessor%0A
 from ufo
@@ -482,24 +482,74 @@
 pileOTF(ufo,
+ preProcessorClass=OTFPreProcessor,%0A              
  outlineComp
@@ -1471,16 +1471,127 @@
 %0A    %22%22%22
+%0A    preProcessor = preProcessorClass(ufo, removeOverlaps=removeOverlaps)%0A    glyphSet = preProcessor.process()
 %0A%0A    ou
@@ -1633,32 +1633,62 @@
 s(%0A        ufo, 
+glyphSet=glyphSet, glyphOrder=
 glyphOrder, roun
@@ -1682,16 +1682,24 @@
 phOrder,
+%0A       
  roundTo
@@ -1724,47 +1724,8 @@
 ance
-,%0A        removeOverlaps=removeOverlaps
 )%0A  
@@ -2106,16 +2106,66 @@
 TTF(ufo,
+ preProcessorClass=TTFPreProcessor,%0A              
  outline
@@ -2789,72 +2789,93 @@
     
-outlineCompiler = outlineCompilerClass(%0A        ufo, glyphOrder,
+preProcessor = preProcessorClass(%0A        ufo, removeOverlaps=removeOverlaps,%0A       
  con
@@ -2909,21 +2909,16 @@
        c
-ubicC
 onversio
@@ -2991,47 +2991,146 @@
 tion
-,
+)
 %0A    
-    removeOv
+glyphSet = preProcessor.process()%0A%0A    outlineCompiler = outlineCompil
 er
+C
 la
-ps=removeOverlaps
+ss(%0A        ufo, glyphSet=glyphSet, glyphOrder=glyphOrder
 )%0A  

add preProcessorClass argument to compile{OTF,TTF}