tags.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  2. <script src="https://unpkg.com/papaparse@5.4.1/papaparse.min.js"></script>
  3. <link rel="preconnect" href="https://fonts.googleapis.com">
  4. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  5. <link href="https://cdn.jsdelivr.net/npm/daisyui@4.12.14/dist/full.min.css" rel="stylesheet" type="text/css" />
  6. <script src="https://cdn.tailwindcss.com"></script>
  7. <body>
  8. <div id="app">
  9. <link v-for="family in uniqueFamilies" :href="familyLink(family)" rel="stylesheet">
  10. <!-- Navbar -->
  11. <div style="max-height: 3rem" class="navbar bg-base-100 fixed left-0 top-0 shadow">
  12. <a class="btn btn-ghost btn-sm text-xl">GF Tagger</a>
  13. <ul class="menu menu-horizontal px-1">
  14. <li>
  15. <details>
  16. <summary>File</summary>
  17. <ul class="shadow w-36">
  18. <li><a @click="saveCSV">Export CSV</a></li>
  19. <li><a @click="prCSV">Open PR</a></li>
  20. </ul>
  21. </details>
  22. </li>
  23. <li>
  24. <details>
  25. <summary>Edit</summary>
  26. <ul class="shadow w-56">
  27. <li><a>Insert placeholder tags</a></li>
  28. <li><a>Remove placeholder tags</a></li>
  29. </ul>
  30. </details>
  31. </li>
  32. <li>
  33. <details>
  34. <summary>{{ CurrentCategory }}</summary>
  35. <ul class="shadow">
  36. <div class="cont" style="max-height: 16rem; overflow: scroll">
  37. <li v-for="category in sortedCategories()">
  38. <a @click="CurrentCategory = category">{{ category }}</a>
  39. </li>
  40. </div>
  41. </ul>
  42. </details>
  43. </li>
  44. <li>
  45. <form @submit.prevent="loadCSV">
  46. <div class="join" style="padding-top: 12px;">
  47. <input class="join-item input input-sm input-bordered w-full max-w-xs" v-model="commit" required placeholder="refs/heads/main">
  48. <button class="join-item btn btn-sm">checkout</button>
  49. </div>
  50. </form>
  51. </li>
  52. </ul>
  53. </div>
  54. <!-- Add font panel -->
  55. <div id="panel" class="fixed top-20 right-0 bg-base-100 p-5 shadow">
  56. <div class="panel-tile">
  57. <form @submit.prevent="AddTag">
  58. <div class="label label-xs">
  59. <span class="label-text label-xs">Add Tag</span>
  60. </div>
  61. <div class="join">
  62. <input class="join-item input input-xs input-bordered w-full max-w-xs" v-model="newTag" required placeholder="/Expressive/Funky">
  63. <button class="join-item btn btn-xs">Add</button>
  64. </div>
  65. </form>
  66. <div class="divider"></div>
  67. <form @submit.prevent="AddFamily">
  68. <div class="label lavel-xs">
  69. <span class="label-text label-xs">Add Family</span>
  70. </div>
  71. <div class="join">
  72. <input class="join-item input input-xs input-bordered w-full max-w-xs" list="items" v-model="newFamily" required placeholder="Family Name">
  73. <datalist id="items">
  74. <option v-for="family in uniqueFamilies" :value="family">
  75. </datalist>
  76. <input class="join-item input input-xs input-bordered btn-square" v-model="newWeight" required placeholder="Score">
  77. <button class="join-item btn btn-xs">Add</button>
  78. </div>
  79. </form>
  80. </div>
  81. <div class="divider"></div>
  82. <div class="panel-tile" style="max-height: 100px; overflow: scroll;">
  83. <label>History</label>
  84. <div v-if="isEdited">
  85. <p style="font-size: 10pt;" v-for="item in history">{{ item }}</p>
  86. </div>
  87. <div v-else>
  88. <p style="font-size: 10pt;">No changes</p>
  89. </div>
  90. </div>
  91. </div>
  92. <div v-if="sortedFamilies.length === 0">
  93. <p>No families found for this tag. Please add some</p>
  94. </div>
  95. <div class="mt-20">
  96. <div class="item" v-for="family in sortedFamilies" :key="family.Family">
  97. <family-item :family="family" :ready="ready" @edited="edited" @remove="removeFamily"></family-item>
  98. </div>
  99. </div>
  100. </div>
  101. </body>
  102. <script>
  103. Vue.component('family-item', {
  104. props: ['family', 'ready'],
  105. template: `
  106. <div class="item p-1">
  107. <div class="join">
  108. <b class="pr-2">{{ family.Family }}</b>
  109. <input style="width: 3rem;" class="join-item input input-xs input-bordered btn-square" v-model.lazy="family.Weight" @change="edited" placeholder="family.Weight">
  110. <button class="btn btn-xs join-item pr-2" @click="removeFamily">X</button>
  111. </div>
  112. <div v-if="ready" :style="familyStyle" contenteditable="true">
  113. {{ familyPangram }}
  114. </div>
  115. <div v-else>
  116. Loading...
  117. </div>
  118. <div class="divider"></div>
  119. </div>
  120. `,
  121. methods: {
  122. edited() {
  123. this.$emit('edited', this.family);
  124. },
  125. removeFamily() {
  126. this.$emit('remove', this.family);
  127. }
  128. },
  129. computed: {
  130. familyPangram() {
  131. return this.$root.familyPangram(this.family);
  132. },
  133. familyStyle() {
  134. return `font-family: "${this.family.Family}", "Adobe NotDef"; font-size: 32pt;`;
  135. }
  136. }
  137. });
  138. var app = new Vue({
  139. el: '#app',
  140. data() {
  141. return {
  142. ready: false,
  143. isEdited: false,
  144. commit: "refs/heads/main",
  145. newTag: "",
  146. newFamily: '',
  147. newWeight: '',
  148. CurrentCategory: "/Expressive/Calm",
  149. Categories: new Set(),
  150. Families: [],
  151. Pangrams: new Map([
  152. ["English", "The quick brown fox jumps over the lazy dog."],
  153. ["Greek", "Ζαφείρι δέξου πάγκαλο, βαθῶν ψυχῆς τὸ σῆμα"],
  154. ["Cyrillic", "В чащах юга жил бы цитрус? Да, но фальшивый экземпляр!"],
  155. ["Japanese", "いろはにほへと ちりぬるを わかよたれそ つねならむ うゐのおくやま けふこえて あさきゆめみし ゑひもせす(ん"],
  156. ["Chinese", "視野無限廣,窗外有藍天"],
  157. ["Arabic", "نص حكيم له سر قاطع وذو شأن عظيم مكتوب على ثوب أخضر ومغلف بجلد أزرق"],
  158. ["Hebrew", "שפן אכל קצת גזר בטעם חסה, ודי."],
  159. ["Devanagari", "ऋषियों को सताने वाले दुष्ट राक्षसों के राजा रावण का सर्वनाश करने वाले विष्णुवतार भगवान श्रीराम, अयोध्या के महाराज दशरथ के बड़े सपुत्र थे।"],
  160. ["Bengali", "যেহেতু মানব পরিবারের সকল সদস্যের সমান ও অবিচ্ছেদ্য অধিকারসমূহ"],
  161. ["Gujarati", "કેમ કે માનવકુટુંબના દરેક સભ્યની પરંપરાપ્રાપ્ત પ્રતિષ્ઠાને અને"],
  162. ["Telugu", "మానవకుటంబమునందలి వ్యక్తులందరికిని గల ఆజన్మసిద్ధమైన ప్రతిపత్తిని"],
  163. ["Kannada", "ಎಲ್ಲಾ ಮಾನವರೂ ಸ್ವತಂತ್ರರಾಗಿಯೇ ಜನಿಸಿದ್ದಾರೆ. ಹಾಗೂ ಘನತೆ ಮತ್ತು ಹಕ್ಕುಗಳಲ್ಲಿ"],
  164. ["Khmer", "ដោយយល់ឃើញថា ការទទួលស្គាល់សេចក្ដីថ្លៃថ្នូរជាប់ពីកំណើត និងសិទ្ធិស្មើភាពគ្នា"],
  165. ["Phags Pa", "ꡗ ꡈꡱ ᠂ ꡒ ꡂ ꡈꡞ ᠂ ꡚꡖꡋ ꡈꡞꡋꡨꡖ ꡗꡛꡧꡖ ꡈꡋ ꡈꡱꡨꡖ ꡳꡬꡖ"],
  166. ["Tamil", "மனிதக் குடும்பத்தினைச் சேர்ந்த யாவரதும் உள்ளார்ந்த"],
  167. ]),
  168. FamilyScripts: new Map(),
  169. history: [],
  170. };
  171. },
  172. watch: {
  173. commit(newCommit) {
  174. this.updateURL();
  175. },
  176. CurrentCategory(newCategory) {
  177. this.updateURL();
  178. },
  179. },
  180. created() {
  181. this.loadCSV();
  182. this.loadFamilyPangrams();
  183. },
  184. mounted() {
  185. const urlParams = new URLSearchParams(window.location.search);
  186. const category = urlParams.get('category');
  187. if (category) {
  188. this.CurrentCategory = category;
  189. }
  190. const commit = urlParams.get('commit');
  191. if (commit) {
  192. this.commit = commit;
  193. this.loadCSV();
  194. }
  195. },
  196. computed: {
  197. sortedFamilies() {
  198. let ll = this.Families;
  199. let filtered = ll.filter(family => family["Group/Tag"] === this.CurrentCategory);
  200. filtered.sort(function(a, b) {return b.Weight - a.Weight;});
  201. return filtered;
  202. },
  203. uniqueFamilies() {
  204. return Array.from(new Set(this.Families.map((family) => family.Family)));
  205. }
  206. },
  207. methods: {
  208. sortedCategories() {
  209. return Array.from(this.Categories).sort();
  210. },
  211. updateURL() {
  212. const url = new URL(window.location);
  213. if (this.commit && this.commit !== "refs/heads/main") {
  214. url.searchParams.set('commit', this.commit);
  215. } else {
  216. url.searchParams.delete('commit');
  217. }
  218. if (this.CurrentCategory) {
  219. url.searchParams.set('category', this.CurrentCategory);
  220. } else {
  221. url.searchParams.delete('category');
  222. }
  223. history.pushState(null, '', url);
  224. },
  225. familyPangram(family) {
  226. return this.Pangrams.get(this.FamilyScripts.get(family.Family));
  227. },
  228. edited(family) {
  229. this.isEdited = true;
  230. this.history.push(`* ${family.Family},${family["Group/Tag"]},${family.Weight}`);
  231. },
  232. parseUnicode(str) {
  233. let ranges = str.split(",");
  234. let script = "English";
  235. let scripts = {
  236. "U+600-6FF": "Arabic",
  237. "U+900-97F": "Devanagari",
  238. "U+590-5FF": "Hebrew",
  239. "U+A80-AFF": "Gujarati",
  240. "U+C00-C7F": "Telugu",
  241. "U+C80-CFF": "Kannada",
  242. "U+980-9FE": "Bengali",
  243. "U+1780-17FF": "Khmer",
  244. "U+A840-A877": "Phags Pa",
  245. "U+0B82-0BFA": "Tamil",
  246. }
  247. for (let i = 0; i < ranges.length; i++) {
  248. for (let key in scripts) {
  249. if (ranges[i].includes(key)) {
  250. script = scripts[key];
  251. break;
  252. }
  253. }
  254. }
  255. return script;
  256. },
  257. async loadFamilyPangrams(delay = 1000) {
  258. await document.fonts.ready;
  259. let result = new Map();
  260. let fonts = document.fonts;
  261. fonts.forEach((font) => {
  262. if (!result.has(font.family)) {
  263. result.set(font.family, this.parseUnicode(font.unicodeRange));
  264. }
  265. });
  266. console.log(result.size)
  267. if (result.size < 1000) {
  268. console.log("retry")
  269. setTimeout(() => this.loadFamilyPangrams(), delay);
  270. }
  271. this.FamilyScripts = result;
  272. this.ready = true;
  273. },
  274. familyLink(Family) {
  275. return "https://fonts.googleapis.com/css2?family=" + Family.replace(" ", "+") + "&display=swap"
  276. },
  277. familyCSSClass(Family) {
  278. let cssName = Family.family.replace(" ", "-").toLowerCase();
  279. return `.${cssName} {
  280. font-family: "${Family.family}", sans-serif;
  281. font-weight: 400;
  282. font-style: normal;
  283. }`
  284. },
  285. familySelector(Family) {
  286. let cssName = Family.Family.replace(" ", "-").toLowerCase();
  287. return cssName;
  288. },
  289. familyStyle(Family) {
  290. return `font-family: "${Family.Family}", "Adobe NotDef"; font-size: 32pt;`
  291. },
  292. AddTag() {
  293. this.isEdited = true;
  294. this.Categories.add(this.newTag);
  295. this.history.push(`+ Tag added "${this.newTag}"`);
  296. this.CurrentCategory = this.newTag;
  297. },
  298. AddFamily() {
  299. this.isEdited = true;
  300. let newFamily = { Weight: this.newWeight, Family: this.newFamily, "Group/Tag": this.CurrentCategory }
  301. this.Families.push(newFamily);
  302. this.history.push(`+ ${newFamily.Family},${newFamily["Group/Tag"]},${newFamily.Weight}`);
  303. },
  304. removeFamily(Family) {
  305. this.isEdited = true;
  306. this.Families = this.Families.filter((t) => t !== Family);
  307. this.history.push(`- ${Family.Family},${Family["Group/Tag"]},${Family.Weight}`);
  308. },
  309. familiesToCSV() {
  310. this.Families = this.Families.filter((t) => t.Family !== "");
  311. // The sorting function used is case sensitive.
  312. // This means that "A" will come before "a".
  313. this.Families = Array.from(this.Families).sort((a, b) => {
  314. if (`${a.Family},${a['Group/Tag']}` < `${b.Family},${b['Group/Tag']}`) {
  315. return -1;
  316. }
  317. if (`${a.Family},${a['Group/Tag']}` > `${b.Family},${b['Group/Tag']}`) {
  318. return 1;
  319. }
  320. return 0;
  321. });
  322. // Include a newline at the end to keep Evan's Vim happy.
  323. return Papa.unparse(this.Families,
  324. {
  325. columns: ["Family", "Group/Tag", "Weight"],
  326. skipEmptyLines: true,
  327. header: false
  328. }
  329. ) + "\n";
  330. },
  331. saveCSV() {
  332. let csv = this.familiesToCSV();
  333. const blob = new Blob([csv], { type: 'text/csv' });
  334. const url = URL.createObjectURL(blob);
  335. const a = document.createElement('a');
  336. a.href = url;
  337. a.download = "families.csv";
  338. document.body.appendChild(a);
  339. a.click();
  340. document.body.removeChild(a);
  341. URL.revokeObjectURL(url);
  342. },
  343. prCSV() {
  344. let csv = this.familiesToCSV();
  345. alert("Tag data copied to clipboard. A github pull request page will open in a new tab. Please remove the old data and paste in the new.");
  346. navigator.clipboard.writeText(csv);
  347. window.open("https://github.com/google/fonts/edit/main/tags/all/families.csv")
  348. },
  349. loadCSV() {
  350. if (this.history.length > 0) {
  351. let proceed = confirm("Checking out a new commit will delete any changes you've made. Would you like to continue?")
  352. if (proceed === false) {
  353. return;
  354. }
  355. }
  356. this.history = [];
  357. const csvFilePath = `https://raw.githubusercontent.com/google/fonts/${this.commit}/tags/all/families.csv`; // Update this path to your CSV file
  358. fetch(csvFilePath)
  359. .then(response => {
  360. if (response.status === 404) {
  361. alert(`No data found for commit "${this.commit}". Please input a git commit hash e.g 538d9635c160306b40af31c9a3821c59b285bbff`);
  362. }
  363. return response.text()
  364. })
  365. .then(csvText => {
  366. csvText = "Family,Group/Tag,Weight\r\n" + csvText;
  367. Papa.parse(csvText, {
  368. header: true,
  369. complete: (results) => {
  370. this.Categories = new Set(results.data.map((row) => row["Group/Tag"]));
  371. this.Families = results.data.map((row) => ({
  372. Weight: row.Weight,
  373. Family: row.Family,
  374. "Group/Tag": row["Group/Tag"]
  375. })
  376. );
  377. }
  378. });
  379. })
  380. .catch(error => {
  381. console.error('Error loading CSV file:', error);
  382. });
  383. }
  384. }
  385. } // methods
  386. )
  387. </script>
  388. <style>
  389. @font-face {
  390. font-family: "Adobe NotDef";
  391. src: url(https://cdn.jsdelivr.net/gh/adobe-fonts/adobe-notdef/AND-Regular.ttf);
  392. }
  393. </style>