#
#		Python GUI - Fonts - Macintosh
#

import sys
from Carbon.Qd import GetPort, SetPort, TextWidth, TextFont, TextFace, TextSize, GetFontInfo
from Carbon.Fm import GetFNum, GetFontName, GetDefFontSize
from MacDummy import _mac_dummy_port
from GFonts import Font as GFont

class Font(GFont):

    def _from_mac_port(cls, port):
        mac_font = port.GetPortTextFont()
        mac_size = port.GetPortTextSize()
        mac_face = port.GetPortTextFace()
        return cls._from_mac_specs(mac_font, mac_size, mac_face)
    
    _from_mac_port = classmethod(_from_mac_port)
    
    def _from_mac_specs(cls, mac_font, mac_size, mac_face):
        font = cls.__new__(cls)
        font._set_mac_specs(mac_font, mac_size, mac_face)
        return font
    
    _from_mac_specs = classmethod(_from_mac_specs)

    def __init__(self, family, size = GetDefFontSize(), style = []):
        mac_font = GetFNum(family)
        mac_face = 0
        if 'bold' in style:
            mac_face |= 1
        if 'italic' in style:
            mac_face |= 2
        self._set_mac_specs(mac_font, size, mac_face)
    
    def get_family(self):
        return GetFontName(self._mac_font)
    
    def get_size(self):
        return self._mac_size
    
    def get_style(self):
        style = []
        mac_face = self._mac_face
        if mac_face & 1:
            style.append('bold')
        if mac_face & 2:
            style.append('italic')
        return style
    
    def get_ascent(self):
        return self._mac_get_info()[0]
    
    def get_descent(self):
        return self._mac_get_info()[1]
    
    def get_height(self):
        info = self._mac_get_info()
        return info[0] + info[1]
    
    def width(self, s, start = 0, end = sys.maxint):
        old_port = GetPort()
        import Windows
        SetPort(_mac_dummy_port)
        self._mac_set_font()
        width = TextWidth(s, 0, len(s))
        SetPort(old_port)
        return width

    def x_to_pos(self, string, x):
        """Given a number of pixels measured from the left of
        the given string, returns the nearest inter-character position. 
        Returns 0 if x is negative, and len(string) if x is beyond the 
        right end of the string."""
        import Windows
        old_port = GetPort()
        SetPort(_mac_dummy_port)
        d = 0
        i = 0
        imax = len(string)
        while i < imax:
            w = TextWidth(string, i, 1)
            if x < d + w / 2:
                break
            d += w
            i += 1
        SetPort(old_port)
        return i

    def _set_mac_specs(self, mac_font, mac_size, mac_face):
        self._mac_font = mac_font
        self._mac_size = mac_size
        self._mac_face = mac_face
        self._mac_info = None
    
    def _mac_get_info(self):
        info = self._mac_info
        if not info:
            import Windows
            old_port = GetPort()
            SetPort(_mac_dummy_port)
            self._mac_set_font()
            info = GetFontInfo()
            SetPort(old_port)
            self._mac_info = info
        return info
    
    def _mac_set_font(self):
        TextFont(self._mac_font)
        TextFace(self._mac_face)
        TextSize(self._mac_size)


