From a2ddd049367c9f7cfdee3f4b983f7334ee3def5b Mon Sep 17 00:00:00 2001 From: Michael Stangl Date: Fri, 17 Aug 2012 03:04:15 +0200 Subject: [PATCH] - Skalierung gemacht --- bin/generationlib | 9 +-- cfg/punkteraetsel.cfg | 11 ++-- in/font.csv | 2 +- in/test.csv | 13 ++-- libs/piccreate.py | 147 +++++++++++++++++++++++++++++++++--------- 5 files changed, 132 insertions(+), 50 deletions(-) diff --git a/bin/generationlib b/bin/generationlib index 7bf8468..742cb8d 100755 --- a/bin/generationlib +++ b/bin/generationlib @@ -39,7 +39,6 @@ function checkqueue { do if [ ! -d /proc/$PID ] ; then regeneratequeue # at least one PID has finished - echo $PID break fi done @@ -47,10 +46,7 @@ function checkqueue { function cfg_parser { INI_FILE=$1 SECTION=$2 - echo $1 - echo $2 eval `sed -n -e "/^\[$SECTION\]/,/^\s*\[/{/^[^;].*\=.*/p;}" < $INI_FILE`>> trr.txt - echo 'helololo' } function get_filepath { # probiere alle möglichen Kombinationen wo das file stehen könnte je @@ -139,19 +135,16 @@ check_params echo $INPUT_FILE for i in `ls $INPUT_FILE| grep .svg ` do - echo $i - echo $CHECKFILE ID=`echo "$i" | sed -e 's/;.*//g'` PIC_NAME=`echo "$i" | sed -e 's/.svg/.jpg/g'` FILE_PATH=$INPUT_FILE/$PIC_NAME if [ -s $FILE_PATH ] && [ $CHECKFILE -eq 1 ] then - echo $PIC_NAME exists! + echo $PIC_NAME exists else RESIZE="${width}x${height}" SWITCHES="-density $density $INPUT_FILE/$ID -resize $RESIZE $INPUT_FILE/$PIC_NAME" CMDLINE="$CMD $SWITCHES" - echo $CMDLINE (echo $ID; $CMDLINE) & PID=$! diff --git a/cfg/punkteraetsel.cfg b/cfg/punkteraetsel.cfg index 05e9441..3970b35 100644 --- a/cfg/punkteraetsel.cfg +++ b/cfg/punkteraetsel.cfg @@ -31,16 +31,19 @@ content=${name} # Ab dem Punkt wird zu schreiben begonnen x=0 y=0 -length=200 +height=850 +width=2700 # Fontname und Fonttyp fontname=ausgangsschrift glyph_space=20 counter_replace=True #align=center,left,right -align=left +align=center +yalign=center +autoscale=True [jpgs] -width=1900 -height=700 +width=2776 +height=898 format=jpg density=300 diff --git a/in/font.csv b/in/font.csv index e4bb2d2..02f4ea4 100644 --- a/in/font.csv +++ b/in/font.csv @@ -76,7 +76,7 @@ z;z Z;Z plus;+ scharfs;ß -slash; +slash;/ space; minus;- komma;, diff --git a/in/test.csv b/in/test.csv index f25e8c2..2303e92 100644 --- a/in/test.csv +++ b/in/test.csv @@ -1,9 +1,6 @@ Kennummer;Name -a-z;mnopqrstuvwxyzabcdefghijkl -A-Z;ABCDEFGHIJKLMNOPQRSTUVWXYZ -ü;:.:.:.:...---++!!&& -001;Michael -yxelbüxel;Eva -063453;Nicola Jane -333;& -12;ä Ä é è ëïóöÖß/-,:!'a&b+úüÜ +001;Au +002;Rau +003;Lux +004;May +005;Johnson-Kronh-Wattenberg diff --git a/libs/piccreate.py b/libs/piccreate.py index 95552c0..b446971 100644 --- a/libs/piccreate.py +++ b/libs/piccreate.py @@ -21,19 +21,34 @@ def create_picture_frame( config ): params = dict() params['width'] = config.get('globals', 'width') params['height'] = config.get('globals', 'height') + params['background'] = config.get('globals', 'background') #echo '' begintpl = ''' + xmlns:svg="http://www.w3.org/2000/svg" + xmlns="http://www.w3.org/2000/svg" + xmlns:xlink="http://www.w3.org/1999/xlink" + version="1.1" + width="${width}" + height="${height}" + > ''' + imagetpl = ''' + + ''' svgbegin = Template( begintpl ).substitute(params) + # falls ein Hintergrundbild verwendet werden soll, dann das genauso gross + # machen... + try: + background = config.get('globals', 'background') + font_path = os.path.expandvars(os.path.join('$PICCREATE_BACKGROUNDS', background )) + params['background'] = os.path.normpath(font_path) + svgbegin = svgbegin + Template( imagetpl ).substitute(params) + except ConfigParser.NoOptionError: + # dann eben kein Hintergrundbild + pass svgend = '' return svgbegin, svgend @@ -41,13 +56,76 @@ def write_font_line( content, font, params ): ''' erzeugt den SVG Teil des Bildes aus Einzelkomponenten ''' w = Word(content, font, params) - return w.GetSVG() + wordsvg = w.GetSVG() + + # falls das Wort nun skaliert werden soll + if params.has_key('scale'): + params['xscale'] = params['scale'] + params['yscale'] = params['scale'] + if not params.has_key('xscale'): + params['xscale'] = 1.0 + if not params.has_key('yscale'): + params['yscale'] = 1.0 + + pic_width = float(params['width']) + pic_height = float(params['height']) + + if params.has_key('autoscale'): + wordlength = w.calc_length() + wordheight = w.calc_height() + #print wordlength, wordheight + #print pic_width, pic_height + fw = pic_width / wordlength + fh = pic_height / wordheight + #print fw, fh + if fw < fh: scale_faktor = fw + else: scale_faktor = fh + params['xscale'] = scale_faktor + params['yscale'] = scale_faktor + else: + scale_faktor = 1.0 + + # berechne immer den richtigen Startpunkt, je nach Bündigkeit + if params.has_key('align'): + xstart = float(params['x']) + if params['align'] == 'left': + x = xstart + elif params['align'] == 'center': + word_length = w.calc_length() + x = xstart * scale_faktor + (pic_width - word_length * scale_faktor)/2.0 + else: + word_length = w.calc_length() + x = xstart * scale_faktor + pic_width - word_length * scale_faktor + params['xstart'] = x + else: + params['xstart'] = params['x'] + + if params.has_key('yalign'): + ystart = float(params['y']) + if params['yalign'] == 'top': + y = 0.0 + elif params['yalign'] == 'center': + word_height = w.calc_height() + y = ystart * scale_faktor + (pic_height - word_height * scale_faktor)/2.0 + elif params['yalign'] == 'bottom': + y = ystart * scale_faktor + pic_height - word_height * scale_faktor + else: + y = ystart + params['ystart'] = y + else: + params['ystart'] = params['y'] + + scalebegin = ''' + ''' + scaleend = ''' ''' + svg = Template( scalebegin ).substitute(params) + wordsvg + scaleend + return svg def write_random_font_line( config, fontinfos, data ): ''' erzeugt den SVG Teil des Bildes aus einem Hintergrundbild und mehreren in Einzelbereichen zufällig platzierten Einzelbildern ''' # TODO Bild aus Zufallspositionen von Einzelkomponenten - pass + return class Word: """Berechnet die x/y Daten jedes Buchstabn zu einem Wort bzw. mehreren @@ -59,22 +137,23 @@ class Word: def __repr__(self): return self.line def calc_length(self): - deltax= 0.0 + glyph_space = float(self.params['glyph_space']) + deltax = 0.0 for c in self.line: g = self.font.GlyphGet(c) - deltax = g.GetLength() + deltax + deltax = g.GetLength() + deltax + glyph_space return deltax + def calc_height(self): + max_y = 0.0 + for c in self.line: + g = self.font.GlyphGet(c) + new_y = g.GetHeight() + if new_y > max_y: + max_y = new_y + return max_y def GetSVG(self): params = self.params - # berechne immer den richtigen Startpunkt, je nach Bündigkeit - if params['align'] == 'left': - x = float(params['x']) - elif params['align'] == 'center': - word_length = self.calc_length() - x = float(params['x'] + (params['length'] - word_length)/2.0) - else: - word_length = self.calc_length() - x = float(params['x'] + word_length - params['length']) + x = float(params['x']) y = params['y'] glyph_space = float(params['glyph_space']) @@ -91,7 +170,7 @@ class Word: # diese durch den aktuellen Zählerstand ersetzen if params['counter_replace']: xn = g.GetAttribute('npattern') - for i in range(1,int(xn)+1): + for i in range(1, int(xn)+1): counter = counter + 1 content['x'+str(i)] = counter svgstr = svgstr + g.GetSVG(content) @@ -110,7 +189,7 @@ class Glyph: def GetLength(self): return float(self.GetAttribute('xstep')) def GetHeight(self): - return float(self.GetAttribute('ystep')) + return float(self.GetAttribute('ymax')) def GetSVG(self, content): t = Template(self.template) svg = t.substitute(content) @@ -177,8 +256,8 @@ def create_picture(config, fontinfos, data, pic_path): svgstr = '' - begin, end = create_picture_frame( config ) - svgstr = svgstr + begin + picbegin, picend = create_picture_frame( config ) + svgstr = svgstr + picbegin # gehe alle Zeilen die im Bild enthalten sein müssen durch lines = config.get('globals', 'lines').split(',') @@ -201,16 +280,26 @@ def create_picture(config, fontinfos, data, pic_path): font = fontinfos[fontname] # hol dir die Eckdaten für die Zeile - # x, y, align, length - params = dict(config.items(linename)) + # x, y, align, width, height + lineparams = dict(config.items(linename)) - picstr = write_font_line( content, font, params ) + # falls nix angegeben, dann aus hol defaults aus globalen daten + if not lineparams.has_key('width'): + lineparams['width'] = config.get('globals', 'width') + if not lineparams.has_key('height'): + lineparams['height'] = config.get('globals', 'height') + if not lineparams.has_key('x'): + lineparams['x'] = 0.0 + if not lineparams.has_key('y'): + lineparams['y'] = 0.0 + + picstr = write_font_line( content, font, lineparams ) if picstr: svgstr = svgstr + picstr - randstr = write_random_font_line( content, font, params) + randstr = write_random_font_line( content, font, lineparams) if randstr: svgstr = svgstr + randstr - svgstr = svgstr + end + svgstr = svgstr + picend if options.dry: print svgstr