| 1 | // Package emoji is the list of emoji a page icon may be (columns and |
| 2 | // icons spec §2.2): Unicode's RGI set, read from its emoji-test.txt at |
| 3 | // one version (16.0), vendored beside this file under Unicode's terms |
| 4 | // (https://www.unicode.org/terms_of_use.html). |
| 5 | package emoji |
| 6 | |
| 7 | import ( |
| 8 | "bufio" |
| 9 | "crypto/sha256" |
| 10 | _ "embed" // emoji-test.txt |
| 11 | "encoding/hex" |
| 12 | "encoding/json" |
| 13 | "strings" |
| 14 | ) |
| 15 | |
| 16 | //go:embed emoji-test.txt |
| 17 | var source string |
| 18 | |
| 19 | // Emoji is one fully-qualified RGI emoji, its group and its name, as the |
| 20 | // picker shows them. |
| 21 | type Emoji struct { |
| 22 | Text string `json:"e"` |
| 23 | Group int `json:"g"` |
| 24 | Name string `json:"n"` |
| 25 | } |
| 26 | |
| 27 | // Groups is Unicode's groups, in the file's order, Component left out: |
| 28 | // its swatches are parts of emoji, not emoji. |
| 29 | var Groups []string |
| 30 | |
| 31 | var ( |
| 32 | list []Emoji |
| 33 | // canonical maps every listed form, qualified or not, to its |
| 34 | // fully-qualified one. |
| 35 | canonical = map[string]string{} |
| 36 | pickerJSON []byte |
| 37 | pickerHash string |
| 38 | ) |
| 39 | |
| 40 | func init() { |
| 41 | parse() |
| 42 | encoded, err := json.Marshal(struct { |
| 43 | Groups []string `json:"groups"` |
| 44 | List []Emoji `json:"emoji"` |
| 45 | }{Groups, list}) |
| 46 | if err != nil { |
| 47 | panic(err) |
| 48 | } |
| 49 | pickerJSON = encoded |
| 50 | sum := sha256.Sum256(encoded) |
| 51 | pickerHash = hex.EncodeToString(sum[:])[:16] |
| 52 | } |
| 53 | |
| 54 | // parse reads the file. A line is "code points ; status # emoji E1.0 |
| 55 | // name"; groups are "# group: Name" lines. |
| 56 | func parse() { |
| 57 | scanner := bufio.NewScanner(strings.NewReader(source)) |
| 58 | group, component := -1, false |
| 59 | for scanner.Scan() { |
| 60 | line := scanner.Text() |
| 61 | if name, ok := strings.CutPrefix(line, "# group: "); ok { |
| 62 | component = name == "Component" |
| 63 | if !component { |
| 64 | Groups = append(Groups, name) |
| 65 | group = len(Groups) - 1 |
| 66 | } |
| 67 | continue |
| 68 | } |
| 69 | text, status, name, ok := entry(line) |
| 70 | if !ok || component { |
| 71 | continue |
| 72 | } |
| 73 | switch status { |
| 74 | case "fully-qualified": |
| 75 | list = append(list, Emoji{Text: text, Group: group, Name: name}) |
| 76 | canonical[text] = text |
| 77 | case "minimally-qualified", "unqualified": |
| 78 | // The file lists these straight after their qualified form. |
| 79 | if len(list) > 0 && list[len(list)-1].Name == name { |
| 80 | canonical[text] = list[len(list)-1].Text |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // entry reads one data line: the emoji, its status and its name. |
| 87 | func entry(line string) (text, status, name string, ok bool) { |
| 88 | fields, comment, found := strings.Cut(line, "#") |
| 89 | if !found || strings.TrimSpace(fields) == "" { |
| 90 | return "", "", "", false |
| 91 | } |
| 92 | _, status, found = strings.Cut(fields, ";") |
| 93 | if !found { |
| 94 | return "", "", "", false |
| 95 | } |
| 96 | parts := strings.Fields(comment) |
| 97 | if len(parts) < 3 { |
| 98 | return "", "", "", false |
| 99 | } |
| 100 | return parts[0], strings.TrimSpace(status), strings.Join(parts[2:], " "), |
| 101 | true |
| 102 | } |
| 103 | |
| 104 | // Canonical is text as one fully-qualified RGI emoji, and whether it is |
| 105 | // one: a minimally-qualified or unqualified form becomes the |
| 106 | // fully-qualified one, so every platform draws it as an emoji. Anything |
| 107 | // else, two emoji or text, is not an icon. |
| 108 | func Canonical(text string) (string, bool) { |
| 109 | out, ok := canonical[text] |
| 110 | return out, ok |
| 111 | } |
| 112 | |
| 113 | // Count is how many emoji the list holds. |
| 114 | func Count() int { return len(list) } |
| 115 | |
| 116 | // Picker is the picker's list as JSON, and a hash of it for its URL. |
| 117 | func Picker() ([]byte, string) { return pickerJSON, pickerHash } |
| 118 | |