Sublime Text 3插件開發記錄

前言

今天在使用FontForge進行字體裁剪的時候,實現了一次Sublime Text 3插件開發的入門,簡單記錄一下,供以後參考。

插件要點

  1. 開發語言是python,新建代碼文件後存放到指定目錄:%User%/AppData\Roaming\Sublime Text 3\Packages(可通過sublime-preferences-browse packages菜單打開),可以在該目錄下新建目錄,但層級不可太深。
  2. 代碼類的名稱規則:命令名+Command,比如testCommand,後面會貼出示例。
  3. 如果不需要菜單,可通過Ctrl+` 打開sublime命令行窗口,執行命令view.run_command(“命令名”)即可運行。
  4. 如果要綁定菜單,在命令文件同級目錄下,新建文本Main.sublime-menu,文本內容如下:
[
  {
    "id": "tools",
    "children": [
      {
        "id": "CustomTools",
        "caption": "自定義工具",
        "children":[
            {
                "caption": "文本去重",
                "id": "duplicatetextremover",
                "command": "duplicatetextremover"
            },
            {
                "caption": "轉換爲fontforge選擇語句",
                "id": "convert2unicode",
                "command": "convert2unicode"
            }
        ]
      }
    ]
  }
]
  1. 如果菜單沒出來,可以重啓sublime試試, 如果代碼有問題,菜單會置灰不可用。

示例腳本

  1. 字符串去重
import sublime
import sublime_plugin


class duplicatetextremoverCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		endPoint = self.view.size()
		alltext = self.view.substr(sublime.Region(0, endPoint))
		tempText = ""
		for x in alltext:
			if x not in tempText and x != "\n":
				tempText += x
				pass
		self.view.insert(edit, endPoint, "\n\n去重結果:\n" + tempText)
  1. 轉換爲fontforge選擇語句
import sublime
import sublime_plugin


class convert2unicodeCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		endPoint = self.view.size()
		alltext = self.view.substr(sublime.Region(0, endPoint))
		tempText = ""
		for x in alltext:
			if x not in tempText and x != "\n":
				tempText += "SelectMore(" + hex(ord(x)) + ")" + "\n"
				pass
		tempText = tempText.rstrip('\n')
		self.view.insert(edit, endPoint, "\n\n轉換結果:\n" + tempText)

FontForge精簡字體

以上操作都是爲了這一步,太複雜的不會搞,還是在FontForge裏用圖形化界面操作方便些,具體操作步驟:
1.使用FontForge打開字體文件後,點擊“File-Execute Scripte”菜單;
2.在彈窗中粘貼在sublime中得到的命令文本,點擊ok執行後FontForge會選中對應的字形;
3.點擊“Edit-Select-Invert Selection”菜單進行反選;
4.點擊“Encoding-Detach & Remove Glyphs”刪除字形;
5.點擊“Encoding-Remove Unused Slots”;
6.點擊“File-Generate Fonts”生成字體文件即可;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章