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

     1  /*
     2   * This file is subject to the terms and conditions defined in
     3   * file 'LICENSE.md', which is part of this source code package.
     4   */
     5  
     6  package textencoding
     7  
     8  import "github.com/unidoc/unidoc/common"
     9  
    10  func glyphToRune(glyph string, glyphToRuneMap map[string]rune) (rune, bool) {
    11  	ucode, found := glyphToRuneMap[glyph]
    12  	if found {
    13  		return ucode, true
    14  	}
    15  
    16  	//common.Log.Debug("Glyph->Rune ERROR: Unable to find glyph %s", glyph)
    17  	return 0, false
    18  }
    19  
    20  func runeToGlyph(ucode rune, runeToGlyphMap map[rune]string) (string, bool) {
    21  	glyph, found := runeToGlyphMap[ucode]
    22  	if found {
    23  		return glyph, true
    24  	}
    25  
    26  	//common.Log.Debug("Rune->Glyph ERROR: Unable to find rune %v", ucode)
    27  	return "", false
    28  }
    29  
    30  func splitWords(raw string, encoder TextEncoder) []string {
    31  	runes := []rune(raw)
    32  
    33  	words := []string{}
    34  
    35  	startsAt := 0
    36  	for idx, code := range runes {
    37  		glyph, found := encoder.RuneToGlyph(code)
    38  		if !found {
    39  			common.Log.Debug("Glyph not found for code: %s\n", string(code))
    40  			continue
    41  		}
    42  
    43  		if glyph == "space" {
    44  			word := runes[startsAt:idx]
    45  			words = append(words, string(word))
    46  			startsAt = idx + 1
    47  		}
    48  	}
    49  
    50  	word := runes[startsAt:]
    51  	if len(word) > 0 {
    52  		words = append(words, string(word))
    53  	}
    54  
    55  	return words
    56  }