Get Unicode character by glyph index in CTFontRef or CGFontRef

CTFontRef provides a great way, for example, CTFontGetGlyphsForCharactersto display character (s) in glyph (s). My question is, is there any method for inverted mapping? That is, can I get characters (s) using the specified characters? Since I found that there is CTFontCopyCharacterSetto get all supported characters, I think there will be some good solutions.

+3
source share
2 answers

I think you may need to analyze the font mapping tables yourself. You can access tables with CGFontCopyTableForTag(); the table you are in is the table " cmap", the format of which is described here:

http://www.microsoft.com/typography/otspec/cmap.htm

and also here:

http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html

Unfortunately, as you discover by reading them, the business mapping of characters to glyphs is definitely not trivial, and in addition, any font can have more than one mapping table (i.e. the set of characters that use a given character may depend on which format of the mapping table you or the selector choose).

, , OpenType AAT, , , , . OpenType AAT Unicode ( ).

+3

TL;DR: CTFont/CTFontRef/CTGlyph - CTLine CTRun; , String- > Glyph.

, . , . . "" , . "" "".

(?) , , , , ​​ . - , - , , . , . ( : - Swift API- Core)

import CoreText
import AppKit

func main(argc: Int, argv: [String])
{
    var stringAttributes: [String: AnyObject] = [:]
    var fontName = "Zapfino"
    var fUseLigatures = false

    var fontNameIndex = 0
    if argc > 1
    {
        if argv[1] == "/lig"
        {
            fUseLigatures = true;
            if (argc > 2) { fontNameIndex = 3 }
        }
        else { fontNameIndex = 2 }
    }

    if fontNameIndex > 0 { fontName = argv[fontNameIndex] }

    if let font = NSFont(name:fontName, size:24.0)
        { stringAttributes[NSFontAttributeName] = font }

    stringAttributes[NSLigatureAttributeName] = fUseLigatures ? 2 : 0

    let string = NSAttributedString(
    string:"This is \(fontName)!",
    attributes: stringAttributes)

    let line = CTLineCreateWithAttributedString(string) // CTLine

    let runs = CTLineGetGlyphRuns(line) // CTRun[]
    let nsRuns:Array<AnyObject> = runs as Array<AnyObject>
    assert(nsRuns.count == 1)

    let run = nsRuns[0] as! CTRun

    let glyphCount = CTRunGetGlyphCount(run)
    println("String: \(string.string)")
    println("\tStrLen: \(count(string.string)), Count Of Glyphs: \(glyphCount)");

    let clusters = UnsafeMutablePointer<CFIndex>.alloc(glyphCount)

    CTRunGetStringIndices(run, CFRange(location:0, length:glyphCount), clusters)

    for var idx = 0; idx < glyphCount; idx++
    {
        let idxString = clusters[idx];
        println("Glyph @ \(idx) maps to String @ \(idxString)")
    }
}

main(Process.arguments.count, Process.arguments)

, /lig , :

    String: This is Zapfino!
        StrLen: 16, Count Of Glyphs: 16
Glyph @ 0 maps to String @ 0
Glyph @ 1 maps to String @ 1
Glyph @ 2 maps to String @ 2
Glyph @ 3 maps to String @ 3
Glyph @ 4 maps to String @ 4
Glyph @ 5 maps to String @ 5
Glyph @ 6 maps to String @ 6
Glyph @ 7 maps to String @ 7
Glyph @ 8 maps to String @ 8
Glyph @ 9 maps to String @ 9
Glyph @ 10 maps to String @ 10
Glyph @ 11 maps to String @ 11
Glyph @ 12 maps to String @ 12
Glyph @ 13 maps to String @ 13
Glyph @ 14 maps to String @ 14
Glyph @ 15 maps to String @ 15
joes-mac: Tue Apr 14, 10:26:00
~/Source/FontGlyph/./main /lig
String: This is Zapfino!
        StrLen: 16, Count Of Glyphs: 7
Glyph @ 0 maps to String @ 0
Glyph @ 1 maps to String @ 2
Glyph @ 2 maps to String @ 4
Glyph @ 3 maps to String @ 5
Glyph @ 4 maps to String @ 7
Glyph @ 5 maps to String @ 8
Glyph @ 6 maps to String @ 15

Ligature, , 1 1. : enter image description here

+3

Source: https://habr.com/ru/post/1791655/


All Articles