private int WriteBfrange(OutputStream <ByteArrayOutputStream> stream, IList <Glyph> range)
 {
     if (range.IsEmpty())
     {
         return(0);
     }
     stream.WriteInteger(range.Count);
     stream.WriteString(" beginbfrange\n");
     foreach (Glyph glyph in range)
     {
         String fromTo = CMapContentParser.ToHex(glyph.GetCode());
         stream.WriteString(fromTo);
         stream.WriteString(fromTo);
         stream.WriteByte('<');
         foreach (char ch in glyph.GetChars())
         {
             stream.WriteString(ToHex4(ch));
         }
         stream.WriteByte('>');
         stream.WriteByte('\n');
     }
     stream.WriteString("endbfrange\n");
     range.Clear();
     return(1);
 }
Example #2
0
        /// <summary>Creates a ToUnicode CMap to allow copy and paste from Acrobat.</summary>
        /// <param name="metrics">
        /// metrics[0] contains the glyph index and metrics[2]
        /// contains the Unicode code
        /// </param>
        /// <returns>the stream representing this CMap or <CODE>null</CODE></returns>
        public virtual PdfStream GetToUnicode(Object[] metrics)
        {
            List <int> unicodeGlyphs = new List <int>(metrics.Length);

            for (int i = 0; i < metrics.Length; i++)
            {
                int[] metric = (int[])metrics[i];
                if (fontProgram.GetGlyphByCode(metric[0]).GetChars() != null)
                {
                    unicodeGlyphs.Add(metric[0]);
                }
            }
            if (unicodeGlyphs.Count == 0)
            {
                return(null);
            }
            StringBuilder buf = new StringBuilder("/CIDInit /ProcSet findresource begin\n" + "12 dict begin\n" + "begincmap\n"
                                                  + "/CIDSystemInfo\n" + "<< /Registry (Adobe)\n" + "/Ordering (UCS)\n" + "/Supplement 0\n" + ">> def\n"
                                                  + "/CMapName /Adobe-Identity-UCS def\n" + "/CMapType 2 def\n" + "1 begincodespacerange\n" + "<0000><FFFF>\n"
                                                  + "endcodespacerange\n");
            int size = 0;

            for (int k = 0; k < unicodeGlyphs.Count; ++k)
            {
                if (size == 0)
                {
                    if (k != 0)
                    {
                        buf.Append("endbfrange\n");
                    }
                    size = Math.Min(100, unicodeGlyphs.Count - k);
                    buf.Append(size).Append(" beginbfrange\n");
                }
                --size;
                String fromTo = CMapContentParser.ToHex((int)unicodeGlyphs[k]);
                Glyph  glyph  = fontProgram.GetGlyphByCode((int)unicodeGlyphs[k]);
                if (glyph.GetChars() != null)
                {
                    StringBuilder uni = new StringBuilder(glyph.GetChars().Length);
                    foreach (char ch in glyph.GetChars())
                    {
                        uni.Append(ToHex4(ch));
                    }
                    buf.Append(fromTo).Append(fromTo).Append('<').Append(uni.ToString()).Append('>').Append('\n');
                }
            }
            buf.Append("endbfrange\n" + "endcmap\n" + "CMapName currentdict /CMap defineresource pop\n" + "end end\n");
            PdfStream toUnicode = new PdfStream(PdfEncodings.ConvertToBytes(buf.ToString(), null));

            MakeObjectIndirect(toUnicode);
            return(toUnicode);
        }