<commit_before>from Foundation import *
from AppKit import *

"""
Common glyph drawing functions for all views. Notes:
- all drawing is done in font units
- the scale argument is the factor to scale a glyph unit to a view unit
- the rect argument is the rect that the glyph is being drawn in
"""

"""
setLayer_drawingAttributes_(layerName, attributes)

showGlyphFill
showGlyphStroke
showGlyphOnCurvePoints
showGlyphStartPoints
showGlyphOffCurvePoints
showGlyphPointCoordinates
showGlyphAnchors
showGlyphImage
showGlyphMargins
showFontVerticalMetrics
showFontVerticalMetricsTitles
showFontPostscriptBlues
showFontPostscriptFamilyBlues
"""

# ------
# Colors
# ------

defaultColors = dict(

    # General
    # -------

    background=NSColor.whiteColor(),

    # Font
    # ----

    # vertical metrics
    fontVerticalMetrics=NSColor.colorWithCalibratedWhite_alpha_(.4, .5),

    fontPostscriptBlues=NSColor.colorWithCalibratedRed_green_blue_alpha_(.5, .7, 1, .3),
    fontPostscriptFamilyBlues=NSColor.colorWithCalibratedRed_green_blue_alpha_(1, 1, .5, .3),

    # Glyph
    # -----

    # margins
    glyphMarginsFill=NSColor.colorWithCalibratedWhite_alpha_(.5, .11),
    glyphMarginsStroke=NSColor.colorWithCalibratedWhite_alpha_(.7, .5),

    # contour fill
    glyphContourFill=NSColor.colorWithCalibratedRed_green_blue_alpha_(0, 0, 0, 1),

    # contour stroke
    glyphContourStroke=NSColor.colorWithCalibratedRed_green_blue_alpha_(0, 0, 0, 1),

    # component fill
    glyphComponentFill=NSColor.colorWithCalibratedRed_green_blue_alpha_(0, 0, 0, 1),

    # component stroke
    glyphComponentStroke=NSColor.colorWithCalibratedRed_green_blue_alpha_(0, 0, 0, 1),

    # points
    glyphPoints=NSColor.colorWithCalibratedRed_green_blue_alpha_(.6, .6, .6, 1),

    # anchors
    glyphAnchor=NSColor.colorWithCalibratedRed_green_blue_alpha_(1, .2, 0, 1),

)

def colorToNSColor(color):
    r, g, b, a = color
    return NSColor.colorWithCalibratedRed_green_blue_alpha_(r, g, b, a)

def getDefaultColor(name):
    return defaultColors[name]

# ----------
# Primitives
# ----------

def drawFilledRect(rect):
    context = NSGraphicsContext.currentContext()
    context.setShouldAntialias_(False)
    NSRectFillUsingOperation(rect, NSCompositeSourceOver)
    context.setShouldAntialias_(True)

def drawFilledOval(rect):
    path = NSBezierPath.bezierPath()
    path.appendBezierPathWithOvalInRect_(rect)
    path.fill()

def drawLine((x1, y1), (x2, y2), lineWidth=1.0):
    turnOffAntiAliasing = False
    if x1 == x2 or y1 == y2:
        turnOffAntiAliasing = True
    if turnOffAntiAliasing:
        context = NSGraphicsContext.currentContext()
        context.setShouldAntialias_(False)
    path = NSBezierPath.bezierPath()
    path.moveToPoint_((x1, y1))
    path.lineToPoint_((x2, y2))
    if turnOffAntiAliasing and lineWidth == 1.0:
        lineWidth = 0.001
    path.setLineWidth_(lineWidth)
    path.stroke()
    if turnOffAntiAliasing:
        context.setShouldAntialias_(True)

def drawTextAtPoint(text, pt, scale, attributes={}, xAlign="left", yAlign="bottom", flipped=False):
    text = NSAttributedString.alloc().initWithString_attributes_(text, attributes)
    if xAlign != "left" or yAlign != "bottom":
        width, height = text.size()
        width *= scale
        height *= scale
        x, y = pt
        f = 1
        if flipped:
            f = -1
        if xAlign == "center":
            x -= width / 2
        elif xAlign == "right":
            x -= width
        if yAlign == "center":
            y -= height / 2 * f
        elif yAlign == "top":
            y -= height * f
        pt = (x, y)
    context = NSGraphicsContext.currentContext()
    context.saveGraphicsState()
    transform = NSAffineTransform.transform()
    transform.translateXBy_yBy_(pt[0], pt[1])
    if flipped:
        s = -scale
    else:
        s = scale
    transform.scaleXBy_yBy_(scale, s)
    transform.concat()
    text.drawAtPoint_((0, 0))
    context.restoreGraphicsState()

# ----
# Font
# ----

# Vertical Metrics

def drawFontVerticalMetrics(glyph, scale, rect, drawLines=True, drawText=True, color=None, backgroundColor=None):
    font = glyph.font
    if font is None:
        return
    if color is None:
        color = getDefaultColor("fontVerticalMetrics")
    if backgroundColor is None:
        backgroundColor = getDefaultColor("background")
    color.set()
    # gather y positions
    toDraw = (
        ("Descender", "descender"),
        ("X Height", "xHeight"),
        ("Cap Height", "capHeight"),
        ("Ascender", "ascender")
    )
    toDraw = [(name, getattr(font.info, attr)) for name, attr in toDraw if getattr(font.info, attr) is not None]
    toDraw.append(("Baseline", 0))
    positions = {}
    for name, position in toDraw:<commit_msg>Adds a flipped=False flag to drawFontVerticalMetrics that was lost somehow.<commit_after>from Foundation import *
from AppKit import *

"""
Common glyph drawing functions for all views. Notes:
- all drawing is done in font units
- the scale argument is the factor to scale a glyph unit to a view unit
- the rect argument is the rect that the glyph is being drawn in
"""

"""
setLayer_drawingAttributes_(layerName, attributes)

showGlyphFill
showGlyphStroke
showGlyphOnCurvePoints
showGlyphStartPoints
showGlyphOffCurvePoints
showGlyphPointCoordinates
showGlyphAnchors
showGlyphImage
showGlyphMargins
showFontVerticalMetrics
showFontVerticalMetricsTitles
showFontPostscriptBlues
showFontPostscriptFamilyBlues
"""

# ------
# Colors
# ------

defaultColors = dict(

    # General
    # -------

    background=NSColor.whiteColor(),

    # Font
    # ----

    # vertical metrics
    fontVerticalMetrics=NSColor.colorWithCalibratedWhite_alpha_(.4, .5),

    fontPostscriptBlues=NSColor.colorWithCalibratedRed_green_blue_alpha_(.5, .7, 1, .3),
    fontPostscriptFamilyBlues=NSColor.colorWithCalibratedRed_green_blue_alpha_(1, 1, .5, .3),

    # Glyph
    # -----

    # margins
    glyphMarginsFill=NSColor.colorWithCalibratedWhite_alpha_(.5, .11),
    glyphMarginsStroke=NSColor.colorWithCalibratedWhite_alpha_(.7, .5),

    # contour fill
    glyphContourFill=NSColor.colorWithCalibratedRed_green_blue_alpha_(0, 0, 0, 1),

    # contour stroke
    glyphContourStroke=NSColor.colorWithCalibratedRed_green_blue_alpha_(0, 0, 0, 1),

    # component fill
    glyphComponentFill=NSColor.colorWithCalibratedRed_green_blue_alpha_(0, 0, 0, 1),

    # component stroke
    glyphComponentStroke=NSColor.colorWithCalibratedRed_green_blue_alpha_(0, 0, 0, 1),

    # points
    glyphPoints=NSColor.colorWithCalibratedRed_green_blue_alpha_(.6, .6, .6, 1),

    # anchors
    glyphAnchor=NSColor.colorWithCalibratedRed_green_blue_alpha_(1, .2, 0, 1),

)

def colorToNSColor(color):
    r, g, b, a = color
    return NSColor.colorWithCalibratedRed_green_blue_alpha_(r, g, b, a)

def getDefaultColor(name):
    return defaultColors[name]

# ----------
# Primitives
# ----------

def drawFilledRect(rect):
    context = NSGraphicsContext.currentContext()
    context.setShouldAntialias_(False)
    NSRectFillUsingOperation(rect, NSCompositeSourceOver)
    context.setShouldAntialias_(True)

def drawFilledOval(rect):
    path = NSBezierPath.bezierPath()
    path.appendBezierPathWithOvalInRect_(rect)
    path.fill()

def drawLine((x1, y1), (x2, y2), lineWidth=1.0):
    turnOffAntiAliasing = False
    if x1 == x2 or y1 == y2:
        turnOffAntiAliasing = True
    if turnOffAntiAliasing:
        context = NSGraphicsContext.currentContext()
        context.setShouldAntialias_(False)
    path = NSBezierPath.bezierPath()
    path.moveToPoint_((x1, y1))
    path.lineToPoint_((x2, y2))
    if turnOffAntiAliasing and lineWidth == 1.0:
        lineWidth = 0.001
    path.setLineWidth_(lineWidth)
    path.stroke()
    if turnOffAntiAliasing:
        context.setShouldAntialias_(True)

def drawTextAtPoint(text, pt, scale, attributes={}, xAlign="left", yAlign="bottom", flipped=False):
    text = NSAttributedString.alloc().initWithString_attributes_(text, attributes)
    if xAlign != "left" or yAlign != "bottom":
        width, height = text.size()
        width *= scale
        height *= scale
        x, y = pt
        f = 1
        if flipped:
            f = -1
        if xAlign == "center":
            x -= width / 2
        elif xAlign == "right":
            x -= width
        if yAlign == "center":
            y -= height / 2 * f
        elif yAlign == "top":
            y -= height * f
        pt = (x, y)
    context = NSGraphicsContext.currentContext()
    context.saveGraphicsState()
    transform = NSAffineTransform.transform()
    transform.translateXBy_yBy_(pt[0], pt[1])
    if flipped:
        s = -scale
    else:
        s = scale
    transform.scaleXBy_yBy_(scale, s)
    transform.concat()
    text.drawAtPoint_((0, 0))
    context.restoreGraphicsState()

# ----
# Font
# ----

# Vertical Metrics

def drawFontVerticalMetrics(glyph, scale, rect, drawLines=True, drawText=True, color=None, backgroundColor=None, flipped=False):
    font = glyph.font
    if font is None:
        return
    if color is None:
        color = getDefaultColor("fontVerticalMetrics")
    if backgroundColor is None:
        backgroundColor = getDefaultColor("background")
    color.set()
    # gather y positions
    toDraw = (
        ("Descender", "descender"),
        ("X Height", "xHeight"),
        ("Cap Height", "capHeight"),
        ("Ascender", "ascender")
    )
    toDraw = [(name, getattr(font.info, attr)) for name, attr in toDraw if getattr(font.info, attr) is not None]
    toDraw.append(("Baseline", 0))
    positions = {}
    for name, position in toDraw:
{'fraction_non_alphanumeric': 0.1119519021457448, 'fraction_numerical': 0.014097646936871567, 'mean_word_length': 3.253968253968254, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1664989', 'n_tokens_mistral': 3206, 'n_tokens_neox': 3015, 'n_words': 689}