'''
Pango text provider
===================

'''

__all__ = ('LabelPango', )

from kivy.compat import PY2
from kivy.core.text import LabelBase
from kivy.core.text._text_pango import (KivyPangoRenderer, kpango_get_extents,
                                        kpango_get_metrics)


class LabelPango(LabelBase):

    # This is a hack to avoid dealing with glib attrs to configure layout,
    # we just create markup out of the options and let pango set attrs
    def _pango_markup(self, text):
        markup = (text.replace('&', '&amp;')
                      .replace('<', '&lt;')
                      .replace('>', '&gt;'))
        options = self.options
        tags = []
        if options['bold']:
            markup = '<b>{}</b>'.format(markup)
        if options['underline']:
            markup = '<u>{}</u>'.format(markup)
        if options['strikethrough']:
            markup = '<s>{}</s>'.format(markup)
        if options['font_hinting'] == 'mono':
            markup = '<tt>{}</tt>'.format(markup)

        # FIXME: does this do the right thing? .. don't see much w/roboto
        weight_attr = ''
        if options['font_hinting'] in ('light', 'normal'):
            weight_attr = ' weight="{}"'.format(options['font_hinting'])

        return '<span font="{}"{}>{}</span>'.format(
                int(self.options['font_size']),
                weight_attr,
                markup)

    def get_extents(self, text):
        if not text:
            return (0, 0)
        w, h = kpango_get_extents(self, self._pango_markup(text))
        return (w, h)

    def get_ascent(self):
        return kpango_get_metrics(self)[0]

    def get_descent(self):
        return kpango_get_metrics(self)[1]

    def _render_begin(self):
        self._rdr = KivyPangoRenderer(self._size[0], self._size[1])

    def _render_text(self, text, x, y):
        self._rdr.render(self, self._pango_markup(text), x, y)

    def _render_end(self):
        imgdata = self._rdr.get_ImageData()
        del self._rdr
        return imgdata

@@ -917,302 +917,225 @@
 up)%0A
-        if options%5B'font_hinting'%5D == 'mono':%0A            markup = '%3Ctt%3E%7B%7D%3C/tt%3E'.format(markup)%0A%0A        # FIXME: does this do the right thing? .. don't see much w/roboto%0A        weight_attr = ''%0A        if options%5B'font_hinting'%5D in ('light', 'normal'):%0A            weight_attr = ' weight
+%0A        span_attrs = ''%0A        if options%5B'font_features'%5D:%0A            span_attrs += 'font_features=%22%7B%7D%22 '.format(options%5B'font_features'%5D)%0A        if options%5B'text_language'%5D:%0A            span_attrs += 'lang
 =%22%7B%7D%22
+ 
 '.fo
@@ -1152,20 +1152,21 @@
 ns%5B'
-font_hinting
+text_language
 '%5D)%0A
@@ -1192,16 +1192,21 @@
 pan font
+_size
 =%22%7B%7D%22%7B%7D%3E
@@ -1292,14 +1292,12 @@
     
-weight
+span
 _att

Create Pango markup for font_features and text_language