示例#1
0
def test_textInfo(text, shouldApplyBiDi, baseLevel, baseDirection, alignment,
                  segments):
    ti = TextInfo(text)
    ti.shouldApplyBiDi = shouldApplyBiDi
    assert baseLevel == ti.baseLevel
    assert baseDirection == ti.baseDirection
    assert alignment == ti.suggestedAlignment
    assert segments == ti.segments
示例#2
0
 def __init__(self, text, style):
     self.text_info = TextInfo(text)
     self.text = text
     self.setStyle(style)
     if self.style.lang:
         self.text_info.languageOverride = self.style.lang
     self.resetGlyphRun()
示例#3
0
async def test_verticalGlyphMetricsFromUFO():
    fontPath = getFontPath('MutatorSansBoldWideMutated.ufo')
    numFonts, opener, getSortInfo = getOpener(fontPath)
    font = opener(fontPath, 0)
    await font.load(None)
    textInfo = TextInfo("ABCDE")
    textInfo.directionOverride = "TTB"
    glyphs = font.getGlyphRunFromTextInfo(textInfo)
    ax = [g.ax for g in glyphs]
    ay = [g.ay for g in glyphs]
    dx = [g.dx for g in glyphs]
    dy = [g.dy for g in glyphs]
    expectedAX = [0, 0, 0, 0, 0]
    expectedAY = [-1022, -1000, -1000, -1000, -1000]
    expectedDX = [-645, -635, -687, -658, -560]
    expectedDY = [-822, -800, -800, -800, -800]
    assert expectedAX == ax
    assert expectedAY == ay
    assert expectedDX == dx
    assert expectedDY == dy
示例#4
0
async def test_getGlyphRunFromTextInfo(text, expectedGlyphNames, expectedPositions):
    fontPath = getFontPath('IBMPlexSansArabic-Regular.ttf')
    numFonts, opener, getSortInfo = getOpener(fontPath)
    font = opener(fontPath, 0)
    await font.load(None)
    textInfo = TextInfo(text)
    glyphs = font.getGlyphRunFromTextInfo(textInfo)
    glyphNames = [g.name for g in glyphs]
    positions = [g.pos for g in glyphs]
    assert expectedGlyphNames == glyphNames
    assert expectedPositions == positions
示例#5
0
 def __init__(self, text, styles):
     self.text_info = TextInfo(text)
     self.strings = []
     self.segment_data = []
     for segmentText, segmentScript, segmentBiDiLevel, firstCluster in self.text_info._segments:
         self.strings.append(
             StyledString(segmentText, styles[segmentScript]))
         cluster_data = []
         for index, char in enumerate(segmentText, firstCluster):
             cluster_data.append(
                 dict(index=index,
                      char=char,
                      unicode=f"U+{ord(char):04X}",
                      unicodeName=unicodedata.name(char, "?"),
                      script=segmentScript,
                      bidiLevel=segmentBiDiLevel,
                      dir=["LTR", "RTL"][segmentBiDiLevel % 2]))
         self.segment_data.append(cluster_data)
示例#6
0
async def test_mapGlyphsToChars():
    text = "عربي بِّ"
    fontPath = getFontPath('Amiri-Regular.ttf')
    numFonts, opener, getSortInfo = getOpener(fontPath)
    font = opener(fontPath, 0)
    await font.load(None)
    textInfo = TextInfo(text)
    glyphs = font.getGlyphRunFromTextInfo(textInfo)
    charIndices = []
    for glyphIndex in range(len(glyphs)):
        charIndices.append(glyphs.mapGlyphsToChars({glyphIndex}))
    expectedCharIndices = [{7}, {6}, {5}, {4}, {3}, {2}, {1}, {0}]
    assert expectedCharIndices == charIndices
    glyphIndices = []
    for charIndex in range(len(text)):
        glyphIndices.append(glyphs.mapCharsToGlyphs({charIndex}))
    expectedGlyphIndices = [{7}, {6}, {5}, {4}, {3}, {2}, {1}, {0}]
    assert expectedGlyphIndices == glyphIndices
示例#7
0
async def test_colrV1Font():
    fontPath = getFontPath("more_samples-glyf_colr_1.ttf")
    numFonts, opener, getSortInfo = getOpener(fontPath)
    font = opener(fontPath, 0)
    await font.load(None)
    textInfo = TextInfo("c")
    glyphs = font.getGlyphRunFromTextInfo(textInfo)
    glyphNames = [g.name for g in glyphs]
    glyphDrawing, *_ = font.getGlyphDrawings(glyphNames, True)
    boundingBox = glyphDrawing.bounds
    assert (100, 0, 900, 1000) == boundingBox
    surface = CoreGraphicsPixelSurface(boundingBox)
    context = NSGraphicsContext.graphicsContextWithCGContext_flipped_(
        surface.context, False)
    savedContext = NSGraphicsContext.currentContext()
    try:
        NSGraphicsContext.setCurrentContext_(context)
        glyphDrawing.draw(glyphs.colorPalette, (0, 0, 0, 1))
    finally:
        NSGraphicsContext.setCurrentContext_(savedContext)
示例#8
0
def test_textInfo(org, bidi, dirOverride, result, runLengths, dir, align):
    ti = TextInfo(org)
    ti.shouldApplyBiDi = bidi
    ti.directionOverride = dirOverride
    assert ti.text == result
    assert ti.directionForShaper == dir