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

     1  // +build unidev
     2  
     3  package main
     4  
     5  // Utility to generate static maps of glyph <-> character codes for text encoding.
     6  
     7  import (
     8  	"bufio"
     9  	"flag"
    10  	"fmt"
    11  	"io"
    12  	"os"
    13  	"strings"
    14  )
    15  
    16  func main() {
    17  
    18  	encodingfile := flag.String("encodingfile", "", "Encoding glyph list file")
    19  	method := flag.String("method", "charcode-to-glyph", "charcode-to-glyph/glyph-to-charcode")
    20  
    21  	flag.Parse()
    22  
    23  	if len(*encodingfile) == 0 {
    24  		fmt.Printf("Please specify an encoding file, see -h for options\n")
    25  		os.Exit(1)
    26  	}
    27  
    28  	var err error
    29  	switch *method {
    30  	case "charcode-to-glyph":
    31  		err = charcodeToGlyphListPath(*encodingfile)
    32  	case "glyph-to-charcode":
    33  		err = glyphToCharcodeListPath(*encodingfile)
    34  	default:
    35  		fmt.Printf("Unsupported method, see -h for options\n")
    36  		os.Exit(1)
    37  	}
    38  
    39  	if err != nil {
    40  		fmt.Printf("Error: %v\n", err)
    41  		os.Exit(1)
    42  	}
    43  }
    44  
    45  func charcodeToGlyphListPath(filename string) error {
    46  	f, err := os.Open(filename)
    47  	if err != nil {
    48  		return err
    49  	}
    50  	defer f.Close()
    51  
    52  	reader := bufio.NewReader(f)
    53  
    54  	index := -1
    55  	for {
    56  		line, err := reader.ReadString('\n')
    57  		if err != nil {
    58  			if err == io.EOF {
    59  				break
    60  			}
    61  			return err
    62  		}
    63  
    64  		line = strings.Trim(line, " \r\n")
    65  
    66  		//fmt.Printf("%s\n", line)
    67  
    68  		parts := strings.Split(line, " ")
    69  		for _, part := range parts {
    70  			index++
    71  			if part == "notdef" {
    72  				continue
    73  			}
    74  			fmt.Printf("\t%d: \"%s\",\n", index, part)
    75  		}
    76  	}
    77  
    78  	return nil
    79  }
    80  
    81  func glyphToCharcodeListPath(filename string) error {
    82  	f, err := os.Open(filename)
    83  	if err != nil {
    84  		return err
    85  	}
    86  	defer f.Close()
    87  
    88  	reader := bufio.NewReader(f)
    89  
    90  	index := -1
    91  	for {
    92  		line, err := reader.ReadString('\n')
    93  		if err != nil {
    94  			if err == io.EOF {
    95  				break
    96  			}
    97  			return err
    98  		}
    99  
   100  		line = strings.Trim(line, " \r\n")
   101  
   102  		//fmt.Printf("%s\n", line)
   103  
   104  		parts := strings.Split(line, " ")
   105  		for _, part := range parts {
   106  			index++
   107  			if part == "notdef" {
   108  				continue
   109  			}
   110  			fmt.Printf("\t\"%s\": %d,\n", part, index)
   111  		}
   112  	}
   113  
   114  	return nil
   115  }