From f42e0edd3094808c319a89594ae6f55843a0a3bf Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Wed, 12 Aug 2009 21:19:18 +0200 Subject: [PATCH] finished first prototype of spec editor --- spec/edit_spec.py | 314 ++++++++++++++++++++++++++++++++++++--------------- spec/spec_helper.rb | 5 +- 2 files changed, 230 insertions(+), 89 deletions(-) diff --git a/spec/edit_spec.py b/spec/edit_spec.py index 2ede754..dec96ef 100644 --- a/spec/edit_spec.py +++ b/spec/edit_spec.py @@ -53,10 +53,52 @@ class AreaPlain(AreaCheck): class AreaNotPlain(AreaCheck): pass +checktypes = [PixelColorCheck,PixelBrighterThan,PixelDarkerThan,PixelEqualTo,AreaPlain,AreaNotPlain] + +def convert_to_ppm(pdf): + print pdf + f = os.popen("pdfinfo "+pdf, "rb") + info = f.read() + f.close() + width,heigth = re.compile(r"Page size:\s*([0-9]+) x ([0-9]+) pts").findall(info)[0] + dpi = int(72.0 * 612 / int(width)) + os.system("pdftoppm -r "+str(dpi)+" -f 1 -l 1 "+pdf+" test") + return "test-000001.ppm" + class Model: def __init__(self, filename, checks): self.filename = filename + self.imgfilename = convert_to_ppm(filename) + self.bitmap = wx.Bitmap(self.imgfilename) + self.image = wx.ImageFromBitmap(self.bitmap) + self.width = self.bitmap.GetWidth() + self.height = self.bitmap.GetHeight() self.checks = checks + self.xy2check = {} + self.appendListeners = [] + self.drawModeListeners = [] + self.drawmode = PixelColorCheck + + def setdrawmode(self, mode): + self.drawmode = mode + for f in self.drawModeListeners: + f() + + def find(self, x, y): + return self.xy2check.get((x,y),None) + + def delete(self, check): + i = self.checks.index(check) + del self.checks[i] + del self.xy2check[(check.x,check.y)] + for f in self.appendListeners: + f(check) + + def append(self, check): + self.checks += [check] + self.xy2check[(check.x,check.y)] = check + for f in self.appendListeners: + f(check) @staticmethod def load(filename): @@ -85,19 +127,20 @@ class Model: if filename: raise Exception("can't load multi-file specs (in line %d)" % (nr+1)) filename = m.group(1); + model = Model(filename, []) continue m = r_pixelcolor.match(line) - if m: checks += [PixelColorCheck(int(m.group(1)),int(m.group(2)),int(m.group(3),16))];continue + if m: model.append(PixelColorCheck(int(m.group(1)),int(m.group(2)),int(m.group(3),16)));continue m = r_pixelbrighter.match(line) - if m: checks += [PixelBrighterThan(int(m.group(1)),int(m.group(2)),int(m.group(3)),int(m.group(4)))];continue + if m: model.append(PixelBrighterThan(int(m.group(1)),int(m.group(2)),int(m.group(3)),int(m.group(4))));continue m = r_pixeldarker.match(line) - if m: checks += [PixelDarkerThan(int(m.group(1)),int(m.group(2)),int(m.group(3)),int(m.group(4)))];continue + if m: model.append(PixelDarkerThan(int(m.group(1)),int(m.group(2)),int(m.group(3)),int(m.group(4))));continue m = r_pixelequalto.match(line) - if m: checks += [PixelEqualTo(int(m.group(1)),int(m.group(2)),int(m.group(3)),int(m.group(4)))];continue + if m: model.append(PixelEqualTo(int(m.group(1)),int(m.group(2)),int(m.group(3)),int(m.group(4))));continue m = r_areaplain.match(line) - if m: checks += [AreaPlain(int(m.group(1)),int(m.group(2)),int(m.group(3)),int(m.group(4)))];continue + if m: model.append(AreaPlain(int(m.group(1)),int(m.group(2)),int(m.group(3)),int(m.group(4))));continue m = r_areanotplain.match(line) - if m: checks += [AreaNotPlain(int(m.group(1)),int(m.group(2)),int(m.group(3)),int(m.group(4)))];continue + if m: model.append(AreaNotPlain(int(m.group(1)),int(m.group(2)),int(m.group(3)),int(m.group(4))));continue if r_width.match(line) or r_height.match(line): continue # compatibility if r_describe.match(line) or r_end.match(line) or r_header.match(line): @@ -106,7 +149,7 @@ class Model: raise Exception("invalid file format: can't load this file (in line %d)" % (nr+1)) fi.close() - return Model(filename, checks) + return model def save(self): path = os.path.splitext(self.filename)[0]+".rb" @@ -131,97 +174,39 @@ class Model: fi.write("end\n") fi.close() - -class ImageFrame(wx.Frame): - def __init__(self, application, model): - wx.Frame.__init__(self, None, -1, style = wx.DEFAULT_FRAME_STYLE, pos=(50,50)) - self.application = application +class ZoomWindow(wx.Window): + def __init__(self, parent, model): + wx.Window.__init__(self, parent, pos=(0,0), size=(15*32,15*32)) self.model = model self.Bind(wx.EVT_PAINT, self.OnPaint) - self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouse) - self.bitmap = wx.Bitmap(model.filename) - self.width = self.bitmap.GetWidth() - self.height = self.bitmap.GetHeight() - self.bitmap_x = 500 - self.bitmap_y = 32 - self.SetSize((self.width+self.bitmap_x+32, max(self.height+self.bitmap_y, 15*32))) - self.image = wx.ImageFromBitmap(self.bitmap) self.x = 0 self.y = 0 - self.createToolbar() - - def createToolbar(self): - tsize = (16,16) - self.toolbar = self.CreateToolBar(wx.TB_HORIZONTAL | wx.NO_BORDER | wx.TB_FLAT) - self.toolbar.AddSimpleTool(wx.ID_OPEN, - wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR, tsize), - "Open") - self.toolbar.AddSimpleTool(wx.ID_SAVE, - wx.ArtProvider.GetBitmap(wx.ART_FILE_SAVE, wx.ART_TOOLBAR, tsize), - "Save selected pages") - self.toolbar.AddSimpleTool(wx.ID_PREFERENCES, - wx.ArtProvider.GetBitmap(wx.ART_LIST_VIEW, wx.ART_TOOLBAR, tsize), - "Options") - #self.toolbar.AddSeparator() - self.toolbar.Realize() - def OnMouseClick(self, event): - x = min(max(event.X - self.bitmap_x, 0), self.width-1) - y = min(max(event.Y - self.bitmap_y, 0), self.height-1) - if y not in self.model.pixels: - self.model.pixels[y] = {} - if y in self.model.pixels and x in self.model.pixels[y]: - del self.model.pixels[y][x] - else: - color = self.image.GetRed(x,y)<<16 | self.image.GetGreen(x,y)<<8 | self.image.GetBlue(x,y) - self.model.pixels[y][x] = color + def setpos(self,x,y): + self.x = x + self.y = y self.Refresh() - - def OnMouse(self, event): - if event.LeftIsDown(): - return self.OnMouseClick(event) - lastx = self.x - lasty = self.y - self.x = min(max(event.X - self.bitmap_x, 0), self.width-1) - self.y = min(max(event.Y - self.bitmap_y, 0), self.height-1) - if lastx!=self.x or lasty!=self.y: - self.Refresh() - + def OnPaint(self, event): dc = wx.PaintDC(self) self.Draw(dc) - + def Draw(self,dc=None): if not dc: dc = wx.ClientDC(self) - dc.SetBackground(wx.Brush((0,0,0))) - dc.DrawBitmap(self.bitmap, self.bitmap_x, self.bitmap_y, False) - - for yy,row in self.model.pixels.items(): - for xx in row.keys(): - x = xx+self.bitmap_x - y = yy+self.bitmap_y - l = 0 - for r in range(10): - r = (r+1)*3.141526/5 - dc.DrawLine(x+10*math.sin(l), y+10*math.cos(l), x+10*math.sin(r), y+10*math.cos(r)) - l = r - dc.DrawLine(x,y,x+1,y) - color = (0,255,0) for yy in range(15): y = self.y+yy-8 - marked = self.model.pixels.get(y, {}) for xx in range(15): x = self.x+xx-8 - if 0<=x&1` + `pdfinfo #{@filename}` =~ /Page size:\s*([0-9]+) x ([0-9]+) pts/ + width,height = $1,$2 + dpi = (72.0 * 612 / width.to_i).to_i + output = `pdf2swf -s zoom=#{dpi} --flatten -p #{@page} #{@filename} -o #{@swfname} 2>&1` raise ConversionFailed.new(output,@swfname) unless File.exists?(@swfname) output = `swfrender --legacy #{@swfname} -o #{@pngname} 2>&1` raise ConversionFailed.new(output,@pngname) unless File.exists?(@pngname) -- 1.7.10.4