Ejemplo n.º 1
0
    def process(self, text):
        if not text:
            return []
        if self._needsInternalUpdate:
            self._updateEngine()

        # TODO: reuse buffer?
        buf = hb.Buffer.create()
        if isinstance(text, list):
            unicodes = []
            for name in text:
                # TODO: use a dict instead?
                gid = self._glyphOrder.index(name)
                unicodes.append(CH_GID_PREFIX + gid)
            buf.add_codepoints(unicodes, len(unicodes), 0, len(unicodes))
        else:
            buf.add_str(text)
        buf.guess_segment_properties()
        hb.shape(self._hbFont, buf, list(self._fontFeatures.values()))

        glyphRecords = []
        for info, pos in zip(buf.glyph_infos, buf.glyph_positions):
            glyphName = self._glyphOrder[info.codepoint]
            if glyphName not in self.font:
                continue
            record = GlyphRecord()
            record.glyph = self.font[glyphName]
            record.cluster = info.cluster
            record.xOffset = pos.x_offset
            record.yOffset = pos.y_offset
            record.xAdvance = pos.x_advance
            record.yAdvance = pos.y_advance
            glyphRecords.append(record)
        del buf
        return glyphRecords
Ejemplo n.º 2
0
 def _shapeAndSetText(self):
     font = self._font
     records = font.engine.process(self._glyphList)
     if self._shaper == 'compositor':
         records_ = []
         index = 0
         for glyphRecord in records:
             record_ = GlyphRecord()
             record_.glyph = glyph = font[glyphRecord.glyphName]
             record_.cluster = index
             record_.xOffset = glyphRecord.xPlacement
             record_.yOffset = glyphRecord.yPlacement
             record_.xAdvance = glyph.width + glyphRecord.xAdvance
             record_.yAdvance = glyph.height + glyphRecord.yAdvance
             records_.append(record_)
             index += len(glyphRecord.ligatureComponents) or 1
         records = records_
     self.parent().setGlyphRecords(records)