def on_export(self): if not self.check_is_ready(): return where = self.get_font_path() if not where: Message.show_error("请选择字体", Globals.main_window) return chars = self.input_edit.toPlainText() if len(chars) <= 0: Message.show_error("请填写有效的输出字符", Globals.main_window) return output_dir = Globals.config.get(Globals.UserData.output_dir) save_file = Globals.config.get(Globals.UserData.font_save_name) FontFactory().run_with( FontMode.Ttf, { "where": where, "output": output_dir, "save_file": save_file, "max_width": Globals.get_max_width(), "chars": chars, "font_size": Globals.config.get(Globals.UserData.font_size) })
def on_image_changed(self, item): row = item.index().row() old = self.image_atlas[row] new = os.path.join(self.get_image_dir(), item.text() + ".png") try: os.renames(old, new) self.image_atlas[row] = new except Exception as e: Message.show_error(str(e), self) item.setText(os.path.splitext(os.path.basename(old))[0])
def on_show_open_file(msg): if Globals.signal.open_file_trigger and msg: def callback(): MainWindow.open_file(msg) Message.show_choice( msg + "\n\n转换完成!是否打开?", "打开", "关闭", callback )
def __check_textures(self): for file in self.__atlas: base = os.path.splitext(os.path.basename(file)) ext = base[1] if ext.lower() == ".png": image = Image.open(file) if image.width < self.__max_width: self.__textures.append((file, image)) else: self.__invalid_textures.append(file) if len(self.__textures) == 0: Message.show_error("未找到有效的图集", Globals.main_window) return False return True
def check_is_ready(self): if not self.isEnabled() or not self.isVisible(): return False output_dir = Globals.config.get(Globals.UserData.output_dir) if not (os.path.exists(output_dir) and os.path.isdir(output_dir)): Message.show_error("无效的输出目录!", self) return False # save_as_path = os.path.join(output_dir, Globals.config.get(Globals.UserData.font_save_name) + ".png") # if os.path.exists(save_as_path): # Message.show_error("已存在同名文件!", self) # return False return True
def on_start_clicked(self): if not self.check_is_ready(): return if len(self.image_atlas_set) <= 0: Message.show_error("图集目录下未找到有效图集!", self) return output_dir = Globals.config.get(Globals.UserData.output_dir) save_file = Globals.config.get(Globals.UserData.font_save_name) FontFactory.run_with(FontMode.Atlas, { "output": output_dir, "save_file": save_file, "atlas": list(self.image_atlas_set), "max_width": Globals.get_max_width() })
def __merge_textures(self): one_height = int(self.__height / self.__lines) writer = FntWriter() writer.set_font(size=one_height) writer.set_size(self.__width, self.__height, one_height) size = (self.__width, self.__height) channel = (255, 255, 255, 0) merge = Image.new("RGBA", size, channel) x = 0 count = 0 for i, line in enumerate(self.__textures_in_line): for _, v in enumerate(line): y = int(i * one_height) (path, image) = v img = image.crop((0, 0, image.width, image.height)) if x + img.width > self.__max_width: x = 0 region = (x, y, image.width + x, y + image.height) base = os.path.splitext(os.path.basename(path))[0] code = base if len(base) > 1: val = ESCAPE_CHARS.get(base) if val: code = val try: char = FntChar() char.id = ord(code) char.width = image.width char.height = image.height char.x = x char.y = y char.xoffset = 0 alignment = self.__alignment if code in TOP_ALIGNMENT_CHARS: alignment = Alignment.Top elif code in BOTTOM_ALIGNMENT_CHARS: alignment = Alignment.Bottom if alignment == Alignment.Center: char.yoffset = int((one_height - image.height) / 2) elif alignment == Alignment.Top: char.yoffset = 0 else: char.yoffset = int((one_height - image.height)) char.xadvance = char.width char.chnl = 15 writer.add_char(char.text()) merge.paste(img, region) x += img.width count += 1 print("Atlas正在添加字符: %s => %s" % (base, char.id)) except Exception: print("无效的字符: %s" % base) self.__invalid_textures.append(path) if count > 0: writer.set_count(count) merge.save(self.__filename) writer.save(self.__fntname) Globals.signal.open_file_trigger.emit(self.__filename) if len(self.__invalid_textures) > 0: msg = [file + "\n" for file in self.__invalid_textures] Message.show_warning("无效的字符:\n" + "".join(msg), Globals.main_window)
def on_show_msg(self, msg): if Globals.signal.msgbox_trigger and msg: Message.show_info(msg, self)
def on_view_manual(): tail = "" for key, val in ESCAPE_SWAP_CHARS.items(): tail += "\n\t%s\t=>\t%s" % (key, val) Message.show_info(Globals.help + tail, Globals.main_window)