예제 #1
0
        /// <summary>
        /// Maps a unicode to the index of the corresponding glyph.
        /// See OpenType spec "cmap - Character To Glyph Index Mapping Table / Format 4: Segment mapping to delta values"
        /// for details about this a little bit strange looking algorithm.
        /// </summary>
        public int CharCodeToGlyphIndex(char value)
        {
            try
            {
                CMap4 cmap     = FontFace.cmap.cmap4;
                int   segCount = cmap.segCountX2 / 2;
                int   seg;
                for (seg = 0; seg < segCount; seg++)
                {
                    if (value <= cmap.endCount[seg])
                    {
                        break;
                    }
                }
                Debug.Assert(seg < segCount);

                if (value < cmap.startCount[seg])
                {
                    return(0);
                }

                if (cmap.idRangeOffs[seg] == 0)
                {
                    return((value + cmap.idDelta[seg]) & 0xFFFF);
                }

                int idx = cmap.idRangeOffs[seg] / 2 + (value - cmap.startCount[seg]) - (segCount - seg);
                Debug.Assert(idx >= 0 && idx < cmap.glyphCount);

                return(cmap.glyphIdArray[idx] == 0 ? 0 : (cmap.glyphIdArray[idx] + cmap.idDelta[seg]) & 0xFFFF);
            }
            catch
            {
                GetType();
                throw;
            }
        }