svghmi/svghmi.py
branchsvghmi
changeset 3211 938b55abe946
parent 3210 0ddefd20ca2b
child 3214 bb314cdfc656
equal deleted inserted replaced
3210:0ddefd20ca2b 3211:938b55abe946
   311             "method":   "_EditPO"
   311             "method":   "_EditPO"
   312         },
   312         },
   313         {
   313         {
   314             "bitmap":    "AddFont",
   314             "bitmap":    "AddFont",
   315             "name":    _("Add Font"),
   315             "name":    _("Add Font"),
   316             "tooltip": _("Add TTF, OTH or WOFF font to be embedded in HMI"),
   316             "tooltip": _("Add TTF, OTF or WOFF font to be embedded in HMI"),
   317             "method":   "_AddFont"
   317             "method":   "_AddFont"
   318         },
   318         },
   319         {
   319         {
   320             "bitmap":    "DelFont",
   320             "bitmap":    "DelFont",
   321             "name":    _("Delete Font"),
   321             "name":    _("Delete Font"),
   397         ret = TranslationToEtree(langs,translated_messages)
   397         ret = TranslationToEtree(langs,translated_messages)
   398 
   398 
   399         self.ProgressEnd("i18n")
   399         self.ProgressEnd("i18n")
   400 
   400 
   401         return ret
   401         return ret
       
   402 
       
   403     def GetFonts(self, _context):
       
   404         project_path = self.CTNPath()
       
   405         fontdir = os.path.join(project_path, "fonts") 
       
   406         css_parts = []
       
   407 
       
   408         for f in sorted(os.listdir(fontdir)):
       
   409             fontfile = os.path.join(fontdir,f)
       
   410             if os.path.isfile(fontfile):
       
   411                 css_parts.append(GetCSSFontFaceFromFontFile(fontfile))
       
   412 
       
   413         return "".join(css_parts)
   402 
   414 
   403     times_msgs = {}
   415     times_msgs = {}
   404     indent = 1
   416     indent = 1
   405     def ProgressStart(self, k, m):
   417     def ProgressStart(self, k, m):
   406         self.times_msgs[k] = (time.time(), m)
   418         self.times_msgs[k] = (time.time(), m)
   453 
   465 
   454                 transform = XSLTransform(os.path.join(ScriptDirectory, "gen_index_xhtml.xslt"),
   466                 transform = XSLTransform(os.path.join(ScriptDirectory, "gen_index_xhtml.xslt"),
   455                               [("GetSVGGeometry", lambda *_ignored:self.GetSVGGeometry()),
   467                               [("GetSVGGeometry", lambda *_ignored:self.GetSVGGeometry()),
   456                                ("GetHMITree", lambda *_ignored:self.GetHMITree()),
   468                                ("GetHMITree", lambda *_ignored:self.GetHMITree()),
   457                                ("GetTranslations", self.GetTranslations),
   469                                ("GetTranslations", self.GetTranslations),
       
   470                                ("GetFonts", self.GetFonts),
   458                                ("ProgressStart", lambda _ign,k,m:self.ProgressStart(str(k),str(m))),
   471                                ("ProgressStart", lambda _ign,k,m:self.ProgressStart(str(k),str(m))),
   459                                ("ProgressEnd", lambda _ign,k:self.ProgressEnd(str(k)))])
   472                                ("ProgressEnd", lambda _ign,k:self.ProgressEnd(str(k)))])
   460 
   473 
   461                 self.ProgressStart("svg", "source SVG parsing")
   474                 self.ProgressStart("svg", "source SVG parsing")
   462 
   475 
   616             self._StartPOEdit(POFile)
   629             self._StartPOEdit(POFile)
   617         else:
   630         else:
   618             self.GetCTRoot().logger.write_error(_("POT file does not exist, add translatable text (label starting with '_') in Inkscape first\n"))
   631             self.GetCTRoot().logger.write_error(_("POT file does not exist, add translatable text (label starting with '_') in Inkscape first\n"))
   619 
   632 
   620     def _AddFont(self):
   633     def _AddFont(self):
   621         pass
   634         dialog = wx.FileDialog(
       
   635             self.GetCTRoot().AppFrame,
       
   636             _("Choose a font"),
       
   637             os.path.expanduser("~"),
       
   638             "",
       
   639             _("Font files (*.ttf;*.otf;*.woff;*.woff2)|*.ttf;*.otf;*.woff;*.woff2"), wx.OPEN)
       
   640 
       
   641         if dialog.ShowModal() == wx.ID_OK:
       
   642             fontfile = dialog.GetPath()
       
   643             if os.path.isfile(fontfile):
       
   644                 familyname, uniquename, formatname, mimetype = GetFontTypeAndFamilyName(fontfile)
       
   645             else:
       
   646                 self.GetCTRoot().logger.write_error(
       
   647                     _('Selected font %s is not a readable file\n')%fontfile)
       
   648                 return
       
   649             if familyname is None or uniquename is None or formatname is None or mimetype is None:
       
   650                 self.GetCTRoot().logger.write_error(
       
   651                     _('Selected font file %s is invalid or incompatible\n')%fontfile)
       
   652                 return
       
   653 
       
   654             project_path = self.CTNPath()
       
   655 
       
   656             fontfname = uniquename + "." + mimetype.split('/')[1]
       
   657             fontdir = os.path.join(project_path, "fonts") 
       
   658             newfontfile = os.path.join(fontdir, fontfname) 
       
   659 
       
   660             if not os.path.exists(fontdir):
       
   661                 os.mkdir(fontdir)
       
   662 
       
   663             shutil.copyfile(fontfile, newfontfile)
       
   664 
       
   665             self.GetCTRoot().logger.write(
       
   666                 _('Added font %s as %s\n')%(fontfile,newfontfile))
   622 
   667 
   623     def _DelFont(self):
   668     def _DelFont(self):
   624         pass
   669         project_path = self.CTNPath()
       
   670         fontdir = os.path.join(project_path, "fonts") 
       
   671         dialog = wx.FileDialog(
       
   672             self.GetCTRoot().AppFrame,
       
   673             _("Choose a font to remove"),
       
   674             fontdir,
       
   675             "",
       
   676             _("Font files (*.ttf;*.otf;*.woff;*.woff2)|*.ttf;*.otf;*.woff;*.woff2"), wx.OPEN)
       
   677         if dialog.ShowModal() == wx.ID_OK:
       
   678             fontfile = dialog.GetPath()
       
   679             if os.path.isfile(fontfile):
       
   680                 if os.path.relpath(fontfile, fontdir) == os.path.basename(fontfile):
       
   681                     os.remove(fontfile) 
       
   682                     self.GetCTRoot().logger.write(
       
   683                         _('Removed font %s\n')%fontfile)
       
   684                 else:
       
   685                     self.GetCTRoot().logger.write_error(
       
   686                         _("Font to remove %s is not in %s\n") % (fontfile,fontdir))
       
   687             else:
       
   688                 self.GetCTRoot().logger.write_error(
       
   689                     _("Font file does not exist: %s\n") % fontfile)
   625         
   690         
   626     def CTNGlobalInstances(self):
   691     def CTNGlobalInstances(self):
   627         # view_name = self.BaseParams.getName()
   692         # view_name = self.BaseParams.getName()
   628         # return [ (view_name + "_" + name, iec_type, "") for name, iec_type in SPECIAL_NODES]
   693         # return [ (view_name + "_" + name, iec_type, "") for name, iec_type in SPECIAL_NODES]
   629         # TODO : move to library level for multiple hmi
   694         # TODO : move to library level for multiple hmi