# -*- coding: UTF-8 -*-
""" capellaScript -- (c) Paul Villiger
>>> SMuFL_Gallery

Mit diesem Plugin können Zeichen aus einem SMuFL Font in die Galerie übernommen werden.|

Weitere Informationen sowie die Zeichenbeschreibung befinden sich hier: | 
https://w3c.github.io/smufl/latest/index.html (englisch)

<<<

History: 24.02.26 - Erstausgabe
         26.02.26 - Übersetzung EN - NL (WW)

<?xml version="1.0" encoding="UTF-8"?>
<info>
  <lang id="de">
    <title>Aus SMuFL in Galerie</title>
    <descr>
      <p>Mit diesem Plugin können Zeichen aus einem SMuFL Font in die Galerie übernommen werden.</p>
    </descr>
  </lang>
  <lang id="nl">
    <title>Van SMuFL naar Galerie</title>
    <descr>
      <p>Met deze plugin kunnen tekens uit een SMuFL-lettertype in de Galerie worden overgenomen</p>
    </descr>
  </lang>
  <lang id="en">
    <title>From SMuFL to Gallery</title>
    <descr>
      <p>This plugin enables taking over characters from a SMuFL font into the Gallery</p>
    </descr>
  </lang>
</info>
"""
english = {
    'title'     :'From SMuFL to Gallery',     
    'fontHeight':'Font Height',
    'fontName'  :'Font Name',
    'selFrom'   :'Start Range',
    'selTo'     :'End Range',
    'hexExpl'   :'Hex - input 4 characters, e.g.',
    'example1'  :'   Arrows:      eb60 .. eb8f',
    'example2'  :'   Fingering: e8a0 .. e8c9',
    'error'     :'Fehler',
    'falseInput':'False Input %s %s',
    'help'      :'Further information and description of characters are to be found here: \
\nhttps://w3c.github.io/smufl/latest/index.html'
}
german = {
    'title'     :'Aus SMuFL in Galerie',     
    'fontHeight':'Font Grösse',
    'fontName'  :'Font Name',
    'selFrom'   :'Bereich Anfang',
    'selTo'     :'Berreich Ende',
    'hexExpl'   :'Hex - Eingabe 4-stellig, z.B.',
    'example1'  :'   Pfeile eb60 .. eb8f',
    'example2'  :'   Griffe e8a0 .. e8c9',
    'error'     :'Fehler',
    'falseInput':'Falsche Eingabe %s %s',
    'help'      :'Weitere Informationen sowie die Zeichenbeschreibung befinden sich hier: \
\nhttps://w3c.github.io/smufl/latest/index.html (englisch)'
}
dutch = {
    'title'     :'Uit SMuFL naar Galerie',     
    'fontHeight':'Font Grootte',
    'fontName'  :'Font Naam',
    'selFrom'   :'Begin Selectie',
    'selTo'     :'Einde Selectie',
    'hexExpl'   :'Hex - invoer 4 tekens, b.v.',
    'example1'  :'   Pijlen: eb60 .. eb8f',
    'example2'  :'   Grepen: e8a0 .. e8c9',
    'error'     :'Fout',
    'falseInput':'Verkeerde invoer %s %s',
    'help'      :'Meer informatie en beschrijving van tekens kan men hier vinden: \
\nhttps://w3c.github.io/smufl/latest/index.html (Engels)'
}

try:
    setStringTable(
        ("en", english),
        ("de", german),
        ("nl", dutch))
except:
    def tr(s):
        return german[s]




#-----------------------------------------------------------    
import xml.dom.minidom, codecs, new
from xml.dom.minidom import parseString, NodeList, Node, Element


doc = parseString('<score/>')

def gotoChild(self, name, new=False):
    newEl = None
    if new:
        pass
    else:
        for child in self.childNodes:
            if child.nodeType == child.ELEMENT_NODE and child.tagName == name:
                newEl = child
                break
    if newEl == None:
        newEl = doc.createElement(name)
        self.appendChild(newEl)
    return newEl
Node.gotoChild = new.instancemethod(gotoChild,None,Node)


fontHeight = Edit('18',width = 10)
fontName   = Edit('Bravura', width = 10)
selectFrom = Edit('', width = 10)
selectTo   = Edit('', width = 10)

dlg = Dialog (tr('title'),
              VBox([
                  HBox([Label(tr('fontHeight'), width = 10), fontHeight, Label('', width = 5)]),
                  HBox([Label(tr('fontName'), width = 10), fontName]),
                  HBox([Label(tr('selFrom'), width = 10), selectFrom, Label('   hex') ]),
                  HBox([Label(tr('selTo'), width = 10), selectTo, Label('   hex') ]),
                  HBox([Label('')]),
                  HBox([Label(tr('hexExpl'))]),
                  HBox([Label(tr('example1'))]),
                  HBox([Label(tr('example2'))]),
                  
                  ], padding = 6), help=tr('help')
              )

ok = True
if dlg.run():
    fontHeight = fontHeight.value()
    fontName = fontName.value()
    try:
        sFrom = eval('0x' + selectFrom.value() )
        sTo = eval('0x' + selectTo.value() )
    except:
        messageBox(tr('error'), tr('falseInput') % (selectFrom.value(), selectTo.value() ) )
        ok = False

    if sFrom > sTo and ok:
        ( selectTo, selectFrom ) = ( sFrom, sTo )
    else:
        ( selectTo, selectFrom ) = ( sTo, sFrom )
        
else:
    ok = False
                

def changeDoc(score):

    gallery = score.gotoChild('gallery')
    for i in range(selectFrom, selectTo):
        drawObj = gallery.gotoChild('drawObj', True)
        drawObj.setAttribute('name', str(hex(i)))
        text = drawObj.gotoChild('text')
        text.setAttribute('x',str(0.1875))
        text.setAttribute('y',str(-3.5))
        font = text.gotoChild('font')
        font.setAttribute('face', fontName)
        font.setAttribute('height', fontHeight)
        content = text.gotoChild('content')
        textNode = doc.createTextNode(unichr(i))
        content.appendChild(textNode)
        basic = drawObj.gotoChild('basic')
        basic.setAttribute('placement','auto')
        basic.setAttribute('placementHint','musicSymbol')
        
        

# Hauptprogramm:

from caplib.capDOM import ScoreChange
import tempfile

class newScoreChange(ScoreChange):
    def changeScore(self, score):
        changeDoc(score)

if activeScore() and ok:
    activeScore().registerUndo("SMuFL_Gallery")
    tempInput = tempfile.mktemp('.capx')
    tempOutput = tempfile.mktemp('.capx')
    activeScore().write(tempInput)
    
    newScoreChange(tempInput, tempOutput)

    activeScore().read(tempOutput)
    os.remove(tempInput)
    os.remove(tempOutput)
