- // Get the next color glyph run.
- ColorGlyphRun& colorGlyphRun = *( glyphColorRunsBuffer + index );
- colorGlyphRun.color = colorRun.color;
-
- // Convert the color run index from character to glyph.
- colorGlyphRun.glyphRun.glyphIndex = *( charactersToGlyphBuffer + colorRun.characterRun.characterIndex );
-
- // Get the index to the last character of the run.
- const CharacterIndex lastIndex = colorRun.characterRun.characterIndex + colorRun.characterRun.numberOfCharacters - 1u;
-
- // Calculate the number of glyphs.
- colorGlyphRun.glyphRun.numberOfGlyphs = *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - colorGlyphRun.glyphRun.glyphIndex;
+ if( 0u < colorRun.characterRun.numberOfCharacters )
+ {
+ // Find the color index.
+ const ColorIndex colorIndex = FindColor( colors, colorRun.color );
+
+ // Get the index to the last character of the run.
+ const CharacterIndex lastIndex = colorRun.characterRun.characterIndex + colorRun.characterRun.numberOfCharacters - 1u;
+
+ const GlyphIndex glyphIndex = std::max( startGlyphIndex, *( charactersToGlyphBuffer + colorRun.characterRun.characterIndex ) ) - startGlyphIndex;
+ // Get the number of glyphs of the run.
+ const Length lastGlyphIndexPlusOne = std::min( numberOfNewGlyphs, *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - startGlyphIndex );
+
+ // Set the indices.
+ for( GlyphIndex i = glyphIndex; i < lastGlyphIndexPlusOne; ++i )
+ {
+ *( newColorIndicesBuffer + i ) = colorIndex;
+ }
+ }