示例#1
0
	public void buildRichTextRuns(LinkedList pStyles, LinkedList cStyles, String RunRawText){

        // Handle case of no current style, with a default
        if(pStyles.Count == 0 || cStyles.Count == 0) {
            _rtRuns = new RichTextRun[1];
            _rtRuns[0] = new RichTextRun(this, 0, RunRawText.Length);
        } else {
            // Build up Rich Text Runs, one for each
            //  character/paragraph style pair
            Vector rtrs = new Vector();

            int pos = 0;

            int curP = 0;
            int curC = 0;
            int pLenRemain = -1;
            int cLenRemain = -1;

            // Build one for each run with the same style
            while(pos <= RunRawText.Length && curP < pStyles.Count && curC < cStyles.Count) {
                // Get the Props to use
                TextPropCollection pProps = (TextPropCollection)pStyles.Get(curP);
                TextPropCollection cProps = (TextPropCollection)cStyles.Get(curC);

                int pLen = pProps.GetCharactersCovered();
                int cLen = cProps.GetCharactersCovered();

                // Handle new pass
                bool freshSet = false;
                if(pLenRemain == -1 && cLenRemain == -1) { freshSet = true; }
                if(pLenRemain == -1) { pLenRemain = pLen; }
                if(cLenRemain == -1) { cLenRemain = cLen; }

                // So we know how to build the eventual run
                int RunLen = -1;
                bool pShared = false;
                bool cShared = false;

                // Same size, new styles - neither shared
                if(pLen == cLen && freshSet) {
                    RunLen = cLen;
                    pShared = false;
                    cShared = false;
                    curP++;
                    curC++;
                    pLenRemain = -1;
                    cLenRemain = -1;
                } else {
                    // Some sharing

                    // See if we are already in a shared block
                    if(pLenRemain < pLen) {
                        // Existing shared p block
                        pShared = true;

                        // Do we end with the c block, or either side of it?
                        if(pLenRemain == cLenRemain) {
                            // We end at the same time
                            cShared = false;
                            RunLen = pLenRemain;
                            curP++;
                            curC++;
                            pLenRemain = -1;
                            cLenRemain = -1;
                        } else if(pLenRemain < cLenRemain) {
                            // We end before the c block
                            cShared = true;
                            RunLen = pLenRemain;
                            curP++;
                            cLenRemain -= pLenRemain;
                            pLenRemain = -1;
                        } else {
                            // We end after the c block
                            cShared = false;
                            RunLen = cLenRemain;
                            curC++;
                            pLenRemain -= cLenRemain;
                            cLenRemain = -1;
                        }
                    } else if(cLenRemain < cLen) {
                        // Existing shared c block
                        cShared = true;

                        // Do we end with the p block, or either side of it?
                        if(pLenRemain == cLenRemain) {
                            // We end at the same time
                            pShared = false;
                            RunLen = cLenRemain;
                            curP++;
                            curC++;
                            pLenRemain = -1;
                            cLenRemain = -1;
                        } else if(cLenRemain < pLenRemain) {
                            // We end before the p block
                            pShared = true;
                            RunLen = cLenRemain;
                            curC++;
                            pLenRemain -= cLenRemain;
                            cLenRemain = -1;
                        } else {
                            // We end after the p block
                            pShared = false;
                            RunLen = pLenRemain;
                            curP++;
                            cLenRemain -= pLenRemain;
                            pLenRemain = -1;
                        }
                    } else {
                        // Start of a shared block
                        if(pLenRemain < cLenRemain) {
                            // Shared c block
                            pShared = false;
                            cShared = true;
                            RunLen = pLenRemain;
                            curP++;
                            cLenRemain -= pLenRemain;
                            pLenRemain = -1;
                        } else {
                            // Shared p block
                            pShared = true;
                            cShared = false;
                            RunLen = cLenRemain;
                            curC++;
                            pLenRemain -= cLenRemain;
                            cLenRemain = -1;
                        }
                    }
                }

                // Wind on
                int prevPos = pos;
                pos += RunLen;
                // Adjust for end-of-run extra 1 length
                if(pos > RunRawText.Length) {
                    RunLen--;
                }

                // Save
                RichTextRun rtr = new RichTextRun(this, prevPos, RunLen, pProps, cProps, pShared, cShared);
                rtrs.Add(rtr);
            }

            // Build the array
            _rtRuns = new RichTextRun[rtrs.Count];
            rtrs.copyInto(_rtRuns);
        }

    }