github.com/unidoc/unidoc@v2.2.0+incompatible/pdf/model/textencoding/glyphlist/utils/glyphparser/glyphparser.go (about)

     1  // +build unidev
     2  
     3  package main
     4  
     5  // Utility to generate static maps of glyph <-> rune conversions for a glyphlist.
     6  
     7  import (
     8  	"bufio"
     9  	"errors"
    10  	"flag"
    11  	"fmt"
    12  	"io"
    13  	"os"
    14  	"sort"
    15  	"strings"
    16  )
    17  
    18  func main() {
    19  	glyphlistFile := flag.String("glyphfile", "", "Glyph file to parse")
    20  	method := flag.String("method", "glyph-to-rune", "glyph-to-rune/rune-to-glyph")
    21  
    22  	flag.Parse()
    23  
    24  	if len(*glyphlistFile) == 0 {
    25  		fmt.Printf("Need to specify glyph list file via glyphfile, see -h for options\n")
    26  		os.Exit(1)
    27  	}
    28  
    29  	glyphToUnicodeMap, err := parseGlyphList(*glyphlistFile)
    30  	if err != nil {
    31  		fmt.Printf("Failed: %v\n", err)
    32  		os.Exit(1)
    33  	}
    34  
    35  	switch *method {
    36  	case "glyph-to-rune":
    37  		printGlyphToRuneList(glyphToUnicodeMap)
    38  	case "rune-to-glyph":
    39  		printRuneToGlyphList(glyphToUnicodeMap)
    40  	default:
    41  		fmt.Printf("Unsupported method: %s, see -h for options\n", *method)
    42  	}
    43  
    44  	/*
    45  		glyphs, err := loadGlyphlist("symbol.txt")
    46  		if err != nil {
    47  			fmt.Printf("Failed: %v\n", err)
    48  			os.Exit(1)
    49  		}
    50  		_ = glyphs
    51  	*/
    52  
    53  	//printGlyphList(glyphToUnicodeMap)
    54  	//printEncodingGlyphToRuneMap(glyphs, glyphToUnicodeMap)
    55  	//printEncodingRuneToGlyphMap(glyphs, glyphToUnicodeMap)
    56  
    57  }
    58  
    59  func printGlyphToRuneList(glyphToUnicodeMap map[string]string) {
    60  	keys := []string{}
    61  	for key := range glyphToUnicodeMap {
    62  		keys = append(keys, key)
    63  	}
    64  	sort.Strings(keys)
    65  
    66  	fmt.Printf("var glyphlistGlyphToRuneMap = map[string]rune{\n")
    67  	for _, glyph := range keys {
    68  		ucode := glyphToUnicodeMap[glyph]
    69  		fmt.Printf("\t\"%s\":\t'\\u%s',\n", glyph, strings.ToLower(ucode))
    70  	}
    71  	fmt.Printf("}\n")
    72  }
    73  
    74  func printRuneToGlyphList(glyphToUnicodeMap map[string]string) {
    75  	keys := []string{}
    76  	for key := range glyphToUnicodeMap {
    77  		keys = append(keys, key)
    78  	}
    79  	sort.Strings(keys)
    80  
    81  	uniqueList := map[string]bool{}
    82  
    83  	fmt.Printf("var glyphlistRuneToGlyphMap = map[rune]string{\n")
    84  	for _, glyph := range keys {
    85  		ucode := glyphToUnicodeMap[glyph]
    86  		ucode = strings.ToLower(ucode)
    87  
    88  		_, duplicate := uniqueList[ucode]
    89  		if !duplicate {
    90  			fmt.Printf("\t'\\u%s':\t\"%s\",\n", ucode, glyph)
    91  			uniqueList[ucode] = true
    92  		} else {
    93  			fmt.Printf("//\t'\\u%s':\t\"%s\", // duplicate\n", ucode, glyph)
    94  		}
    95  	}
    96  	fmt.Printf("}\n")
    97  }
    98  
    99  func printEncodingGlyphToRuneMap(glyphs []string, glyphToUnicodeMap map[string]string) {
   100  	fmt.Printf("var nameEncodingGlyphToRuneMap map[string]rune = map[string]rune{\n")
   101  	for _, glyph := range glyphs {
   102  		ucode, has := glyphToUnicodeMap[glyph]
   103  		if has {
   104  			fmt.Printf("\t\"%s\":\t'\\u%s',\n", glyph, strings.ToLower(ucode))
   105  		} else {
   106  			fmt.Printf("'%s' - NOT FOUND\n", glyph)
   107  		}
   108  
   109  	}
   110  	fmt.Printf("}\n")
   111  }
   112  
   113  func printEncodingRuneToGlyphMap(glyphs []string, glyphToUnicodeMap map[string]string) {
   114  	fmt.Printf("var nameEncodingRuneToGlyphMap map[rune]string = map[rune]string{\n")
   115  	for _, glyph := range glyphs {
   116  		ucode, has := glyphToUnicodeMap[glyph]
   117  		if has {
   118  			fmt.Printf("\t'\\u%s':\t\"%s\",\n", strings.ToLower(ucode), glyph)
   119  		} else {
   120  			fmt.Printf("'%s' - NOT FOUND\n", glyph)
   121  		}
   122  
   123  	}
   124  	fmt.Printf("}\n")
   125  }
   126  
   127  func parseGlyphList(filename string) (map[string]string, error) {
   128  	f, err := os.Open(filename)
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  	defer f.Close()
   133  
   134  	reader := bufio.NewReader(f)
   135  
   136  	gmap := map[string]bool{}
   137  	glyphToUnicodeMap := map[string]string{}
   138  
   139  	for {
   140  		line, err := reader.ReadString('\n')
   141  		if err != nil {
   142  			if err == io.EOF {
   143  				break
   144  			}
   145  			return nil, err
   146  		}
   147  
   148  		line = strings.Trim(line, " \r\n")
   149  
   150  		if line[0] == '#' {
   151  			continue
   152  		}
   153  
   154  		parts := strings.Split(line, ";")
   155  		if len(parts) != 2 {
   156  			return nil, errors.New("Invalid part")
   157  		}
   158  
   159  		if len(parts[1]) > 4 {
   160  			subparts := strings.Split(parts[1], " ")
   161  			for _, subpart := range subparts {
   162  				//fmt.Printf("\"%s\": '\\u%s', //%s (non unique)\n", parts[0], parts[1][0:4], parts[1][4:])
   163  				if _, has := gmap[subpart]; !has {
   164  					//fmt.Printf("'\\u%s': \"%s\",\n", subpart, parts[0])
   165  					gmap[subpart] = true
   166  					glyphToUnicodeMap[parts[0]] = subpart
   167  				} else {
   168  					//fmt.Printf("// '\\u%s': \"%s\", (duplicate)\n", subpart, parts[0])
   169  					glyphToUnicodeMap[parts[0]] = subpart
   170  				}
   171  			}
   172  		} else {
   173  			//fmt.Printf("\"%s\": '\\u%s',\n", parts[0], parts[1])
   174  
   175  			if _, has := gmap[parts[1]]; !has {
   176  				//fmt.Printf("'\\u%s': \"%s\",\n", parts[1], parts[0])
   177  				gmap[parts[1]] = true
   178  				glyphToUnicodeMap[parts[0]] = parts[1]
   179  			} else {
   180  				//fmt.Printf("// '\\u%s': \"%s\", (duplicate)\n", parts[1], parts[0])
   181  				glyphToUnicodeMap[parts[0]] = parts[1]
   182  			}
   183  		}
   184  	}
   185  
   186  	return glyphToUnicodeMap, nil
   187  }
   188  
   189  func loadGlyphlist(filename string) ([]string, error) {
   190  	f, err := os.Open(filename)
   191  	if err != nil {
   192  		return nil, err
   193  	}
   194  	defer f.Close()
   195  
   196  	glyphs := []string{}
   197  	reader := bufio.NewReader(f)
   198  
   199  	index := -1
   200  	for {
   201  		line, err := reader.ReadString('\n')
   202  		if err != nil {
   203  			if err == io.EOF {
   204  				break
   205  			}
   206  			return nil, err
   207  		}
   208  
   209  		line = strings.Trim(line, " \r\n")
   210  
   211  		//fmt.Printf("%s\n", line)
   212  
   213  		parts := strings.Split(line, " ")
   214  		for _, part := range parts {
   215  			index++
   216  			if part == "notdef" {
   217  				continue
   218  			}
   219  			//fmt.Printf("%d: \"%s\",\n", index, part)
   220  			glyphs = append(glyphs, part)
   221  		}
   222  	}
   223  
   224  	return glyphs, nil
   225  }