from __future__ import print_function, division, absolute_import
from fontTools.misc.py23 import *
from fontTools.misc import sstruct
from fontTools.misc.textTools import readHex
from fontTools.ttLib import TTLibError
from . import DefaultTable

# Apple's documentation of 'meta':
# https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6meta.html

META_HEADER_FORMAT = """
    > # big endian
    version:     L
    flags:       L
    dataOffset:  L
    numDataMaps: L
"""

# According to Apple's spec, the dataMaps entries contain a dataOffset
# that is documented as "Offset from the beginning of the data section
# to the data for this tag". However, this is *not* the case with
# the fonts that Apple ships pre-installed on MacOS X Yosemite 10.10.4,
# and it also does not reflect how Apple's ftxdumperfuser tool is parsing
# the 'meta' table (tested ftxdumperfuser build 330, FontToolbox.framework
# build 187). Instead of what is claimed in the spec, the data maps contain
# a dataOffset relative to the very beginning of the 'meta' table.
# The dataOffset field of the 'meta' header apparently gets ignored.

DATA_MAP_FORMAT = """
    > # big endian
    tag:         4s
    dataOffset:  L
    dataLength:  L
"""


class table__m_e_t_a(DefaultTable.DefaultTable):
    def __init__(self, tag="meta"):
        DefaultTable.DefaultTable.__init__(self, tag)
        self.data = {}

    def decompile(self, data, ttFont):
        headerSize = sstruct.calcsize(META_HEADER_FORMAT)
        header = sstruct.unpack(META_HEADER_FORMAT, data[0 : headerSize])
        if header["version"] != 1:
            raise TTLibError("unsupported 'meta' version %d" %
                             header["version"])
        dataMapSize = sstruct.calcsize(DATA_MAP_FORMAT)
        for i in range(header["numDataMaps"]):
            dataMapOffset = headerSize + i * dataMapSize
            dataMap = sstruct.unpack(
                DATA_MAP_FORMAT,
                data[dataMapOffset : dataMapOffset + dataMapSize])
            tag = dataMap["tag"]
            offset = dataMap["dataOffset"]
            self.data[tag] = data[offset : offset + dataMap["dataLength"]]

    def compile(self, ttFont):
        keys = sorted(self.data.keys())
        headerSize = sstruct.calcsize(META_HEADER_FORMAT)
        dataOffset = headerSize + len(keys) * sstruct.calcsize(DATA_MAP_FORMAT)
        header = sstruct.pack(META_HEADER_FORMAT, {
                "version": 1,
                "flags": 0,
                "dataOffset": dataOffset,
                "numDataMaps": len(keys)
        })
        dataMaps = []
        dataBlocks = []
        for tag in keys:
            data = self.data[tag]
            dataMaps.append(sstruct.pack(DATA_MAP_FORMAT, {
                "tag": tag,
                "dataOffset": dataOffset,
                "dataLength": len(data)
            }))
            dataBlocks.append(data)
            dataOffset += len(data)
        return bytesjoin([header] + dataMaps + dataBlocks)

    def toXML(self, writer, ttFont, progress=None):
        for tag in sorted(self.data.keys()):
            writer.begintag("hexdata", tag=tag)
            writer.newline()
            writer.dumphex(self.data[tag])
            writer.endtag("hexdata")
            writer.newline()

    def fromXML(self, name, attrs, content, ttFont):
        if name == "hexdata":
            self.data[attrs["tag"]] = readHex(content)
        else:
            raise TTLibError("can't handle '%s' element" % name)

@@ -3092,16 +3092,309 @@
 eys()):%0A
+            if tag in %5B%22dlng%22, %22slng%22%5D:%0A                writer.begintag(%22text%22, tag=tag)%0A                writer.newline()%0A                writer.write(self.data%5Btag%5D)%0A                writer.newline()%0A                writer.endtag(%22text%22)%0A                writer.newline()%0A            else:%0A    
         
@@ -3437,32 +3437,36 @@
 ag)%0A            
+  
+  
 writer.newline()
@@ -3458,32 +3458,36 @@
 riter.newline()%0A
+    
             writ
@@ -3517,32 +3517,36 @@
 g%5D)%0A            
+ 
+   
 writer.endtag(%22h
@@ -3554,16 +3554,20 @@
 xdata%22)%0A
+    
         
@@ -3726,16 +3726,145 @@
 ontent)%0A
+        elif name == %22text%22 and attrs%5B%22tag%22%5D in %5B%22dlng%22, %22slng%22%5D:%0A            self.data%5Battrs%5B%22tag%22%5D%5D = strjoin(content).strip()%0A
         

handle dlng and slng as text