def drawStructure(self, W, H, wEff): """ Генерация изображения со структурой :type H: int :type W: int :type wEff: int """ lines = sum(x.repeats * len(x.progressions) for x in self._structure) extraH = settings.STRUCTURE_LINE_SPACE * (lines + len(self._structure) - 1) + settings.STRUCTURE_GROUP_SPACE * ( len(self._structure) - 1 ) lineH = (H - extraH) // (lines + len(self._structure)) maxBarSize = max(progression.length() for section in self._structure for progression in section.progressions) im = Image.new("RGBA", (W, H)) draw = ImageDraw.Draw(im) font = ImageFont.truetype(os.path.join("res", "DejaVuSans.ttf"), int(lineH * 0.9)) draw.setfont(font) yPos = 0 for section in self._structure: title = section.title if section.repeats > 1: title += " x%d" % section.repeats draw.text((4, yPos), title) yPos += lineH + settings.STRUCTURE_LINE_SPACE for progression in section.progressions: xPos = 0 pos = Length(0, 1) for chord in progression.chords: pos += chord.length barX = 2 * pos.count(maxBarSize) * wEff // 3 draw.rectangle([(xPos, yPos), (barX, yPos + lineH)], outline="black", fill=str(chord.color)) xPos = barX xPos = 2 * wEff // 3 title = [ x for x in [progression.title, "x%d" % progression.repeats if progression.repeats > 1 else ""] if x ] draw.text((xPos + 4, int(round(yPos + lineH * 0.05))), " ".join(title)) yPos += settings.STRUCTURE_LINE_SPACE + lineH yPos += settings.STRUCTURE_GROUP_SPACE del draw return im
def processStructure(cls, structureElement, signature): """ Генерация элементов RawChord Необходимо расчитать размер в долях :type structureElement: xml.dom.minidom.Element :type signature: Length :rtype: None """ length = getAttr(structureElement, 'length', True, Length.make_validator(signature)) chord = structureElement.getAttribute('name') if structureElement.tagName == 'chord': return RawChord(chord, length) else: raise ParseException(u"Элемент %s не может содержать элемент %s" % (structureElement.parentNode.tagName, structureElement.tagName))
def processElement(self, element): """ Специфичная для класса обработка элемента Конструрием элементы из аттрибутов элемента :type element: xml.dom.minidom.Element :rtype: None """ id = element.getAttribute('id') signature = getAttr(element, 'signature', False, Length.make_validator(), Length(4,4)) title = element.getAttribute('title') key = getAttr(element, 'key', False, validator_key) chords = [self.processStructure(x, signature) for x in iterElements(element)] if not chords: raise ParseException(u"Последовательность %s пуста" % id) self.validate(id, chords, signature) self.values[id] = Progression(signature, title, chords, key)