Пример #1
0
        internal void Build(RichTextBox rtb, bool bBracesBalanced)
        {
            if (rtb == null)
            {
                throw new ArgumentNullException("rtb");
            }

            RichTextBox rtbOp   = CreateOpRtb(rtb);
            string      strText = m_sb.ToString();

            Dictionary <char, string> dEnc = new Dictionary <char, string>();

            if (MonoWorkarounds.IsRequired(586901))
            {
                StringBuilder sbEnc = new StringBuilder();
                for (int i = 0; i < strText.Length; ++i)
                {
                    char ch = strText[i];
                    if ((int)ch <= 255)
                    {
                        sbEnc.Append(ch);
                    }
                    else
                    {
                        string strCharEnc;
                        if (!dEnc.TryGetValue(ch, out strCharEnc))
                        {
                            strCharEnc = RtfbTag.GenerateRandomIdCode();
                            dEnc[ch]   = strCharEnc;
                        }
                        sbEnc.Append(strCharEnc);
                    }
                }
                strText = sbEnc.ToString();
            }

            rtbOp.Text = strText;
            Debug.Assert(rtbOp.Text == strText);             // Test committed

            if (m_fDefault != null)
            {
                rtbOp.SelectAll();
                rtbOp.SelectionFont = m_fDefault;
            }

            string strRtf = rtbOp.Rtf;

            rtbOp.Dispose();

            foreach (KeyValuePair <char, string> kvpEnc in dEnc)
            {
                strRtf = strRtf.Replace(kvpEnc.Value,
                                        StrUtil.RtfEncodeChar(kvpEnc.Key));
            }
            foreach (RtfbTag rTag in m_vTags)
            {
                strRtf = strRtf.Replace(rTag.IdCode, rTag.RtfCode);
            }

            if (bBracesBalanced && MonoWorkarounds.IsRequired(2449941153U))
            {
                strRtf = Regex.Replace(strRtf,
                                       @"(\\)(\{[\u0020-\u005B\u005D-z\w\s]*?)(\})", "$1$2$1$3");
            }

            rtb.Rtf = strRtf;
        }
Пример #2
0
        internal bool Build(RichTextBox rtb, bool bBracesBalanced,
                            ref string strLastRtf)
        {
            if (rtb == null)
            {
                throw new ArgumentNullException("rtb");
            }

            string strText = m_sb.ToString();
            bool   bURtf   = MayGetURtf(rtb);

            // Workaround for encoding bugs in Windows and Mono;
            // Windows: https://sourceforge.net/p/keepass/bugs/1780/
            // Mono: https://bugzilla.novell.com/show_bug.cgi?id=586901
            Dictionary <char, string> dEnc = new Dictionary <char, string>();

            if (bURtf || MonoWorkarounds.IsRequired(586901))
            {
                StringBuilder sbEnc = new StringBuilder();

                for (int i = 0; i < strText.Length; ++i)
                {
                    char ch = strText[i];

                    if (ch <= '\u00FF')
                    {
                        sbEnc.Append(ch);
                    }
                    else
                    {
                        string strCharEnc;
                        if (!dEnc.TryGetValue(ch, out strCharEnc))
                        {
                            strCharEnc = RtfbTag.GenerateRandomIdCode();
                            dEnc[ch]   = strCharEnc;
                        }

                        sbEnc.Append(strCharEnc);
                    }
                }

                strText = sbEnc.ToString();
            }

            string strRtf;

            using (RichTextBox rtbOp = CreateOpRtb(rtb))
            {
                string strFlt = StrUtil.RtfFilterText(strText);
                rtbOp.Text = strFlt;
                Debug.Assert(rtbOp.Text == strFlt);                 // Test committed

                if (m_fDefault != null)
                {
                    rtbOp.SelectAll();
                    rtbOp.SelectionFont = m_fDefault;
                }

                strRtf = rtbOp.Rtf;
            }

            foreach (KeyValuePair <char, string> kvpEnc in dEnc)
            {
                strRtf = strRtf.Replace(kvpEnc.Value,
                                        StrUtil.RtfEncodeChar(kvpEnc.Key));
            }
            foreach (RtfbTag rTag in g_vTags)
            {
                strRtf = strRtf.Replace(rTag.IdCode, rTag.RtfCode);
            }

            if (bBracesBalanced && MonoWorkarounds.IsRequired(2449941153U))
            {
                strRtf = Regex.Replace(strRtf,
                                       @"(\\)(\{[\u0020-\u005B\u005D-z\w\s]*?)(\})", "$1$2$1$3");
            }

            strRtf = StrUtil.RtfFix(strRtf);

            if (strRtf == strLastRtf)
            {
                return(false);
            }
            rtb.Rtf    = strRtf;
            strLastRtf = strRtf;
            return(true);
        }