Exemple #1
0
        public IEnumerable <BreakSpan> BreakToLineSegments(char[] str, int startAt, int len)
        {
            //user must setup the CustomBreakerBuilder before use
            if (_textBreaker == null)
            {
                _textBreaker = Typography.TextBreak.CustomBreakerBuilder.NewCustomBreaker();
            }
            int cur_startAt = startAt;

            _textBreaker.BreakWords(str, cur_startAt, len);
            foreach (TextBreak.BreakSpan sp in _textBreaker.GetBreakSpanIter())
            {
                //our service select a proper script lang info and add to the breakspan

                //at this point
                //we assume that 1 break span
                //has 1 script lang, and we examine it
                //with sample char
                char sample = str[sp.startAt];

                ScriptLang selectedScriptLang;
                if (sample == ' ')
                {
                    //whitespace
                    selectedScriptLang = _defaultScriptLang;
                }
                else if (char.IsWhiteSpace(sample))
                {
                    //other whitespace
                    selectedScriptLang = _defaultScriptLang;
                }
                else
                {
                    //
                    Typography.OpenFont.ScriptLang scLang;
                    if (Typography.OpenFont.ScriptLangs.TryGetScriptLang(sample, out scLang))
                    {
                        //we should decide to use
                        //current typeface
                        //or ask for alternate typeface
                        //if  the current type face is not support the request scriptLang
                        //
                        selectedScriptLang = scLang;
                    }
                    else
                    {
                        //not found
                        //use default
                        selectedScriptLang = _defaultScriptLang;
                    }
                }

                BreakSpan breakspan = new BreakSpan();
                breakspan.startAt = sp.startAt;
                breakspan.len     = sp.len;
                breakspan.scLang  = selectedScriptLang;
                yield return(breakspan);
            }
        }
Exemple #2
0
        public static void Benchmark()
        {
            Console.OutputEncoding = Encoding.Unicode;
            const int length = 1000000;
            var       s      = new System.Diagnostics.Stopwatch();
            var       b      = new Typography.TextBreak.CustomBreaker(_ => { });

            b.BreakWords("Initialize"); //Don't measure startup costs
            foreach (var c in new[] {
                '0', '3', ' ', 'a', 'r', '#', '.', '%', '\r', '\u3232', '\uFEFF', '0'
            })
            {
                s.Restart();
                b.BreakWords(new string(c, length));
                s.Stop();
                Console.WriteLine("'{0}': {1}", c, s.Elapsed);
                GC.Collect();
            }
        }