Duality.Resources.Font.UpdateKerningData C# (CSharp) Method

UpdateKerningData() public method

Updates this Fonts kerning sample data.
public UpdateKerningData ( ) : void
return void
        public void UpdateKerningData()
        {
            if (this.kerning)
            {
                int kerningSamples = (this.Ascent + this.Descent) / 4;
                int[] kerningY;
                if (kerningSamples <= 6)
                {
                    kerningSamples = 6;
                    kerningY = new int[] {
                        this.BaseLine - this.Ascent,
                        this.BaseLine - this.BodyAscent,
                        this.BaseLine - this.BodyAscent * 2 / 3,
                        this.BaseLine - this.BodyAscent / 3,
                        this.BaseLine,
                        this.BaseLine + this.Descent};
                }
                else
                {
                    kerningY = new int[kerningSamples];
                    int bodySamples = kerningSamples * 2 / 3;
                    int descentSamples = (kerningSamples - bodySamples) / 2;
                    int ascentSamples = kerningSamples - bodySamples - descentSamples;

                    for (int k = 0; k < ascentSamples; k++)
                        kerningY[k] = this.BaseLine - this.Ascent + k * (this.Ascent - this.BodyAscent) / ascentSamples;
                    for (int k = 0; k < bodySamples; k++)
                        kerningY[ascentSamples + k] = this.BaseLine - this.BodyAscent + k * this.BodyAscent / (bodySamples - 1);
                    for (int k = 0; k < descentSamples; k++)
                        kerningY[ascentSamples + bodySamples + k] = this.BaseLine + (k + 1) * this.Descent / descentSamples;
                }

                int[] c = new int[3];
                for (int i = 0; i < SupportedChars.Length; ++i)
                {
                    Pixmap.Layer glyphTemp = this.GetGlyphBitmap(SupportedChars[i]);

                    this.glyphs[i].kerningSamplesLeft	= new int[kerningY.Length];
                    this.glyphs[i].kerningSamplesRight	= new int[kerningY.Length];

                    if (SupportedChars[i] != ' ')
                    {
                        int pxIndex;
                        // Left side samples
                        for (int sampleIndex = 0; sampleIndex < this.glyphs[i].kerningSamplesLeft.Length; sampleIndex++)
                        {
                            this.glyphs[i].kerningSamplesLeft[sampleIndex] = glyphTemp.Width / 2;
                            for (int off = 0; off <= 2; off++)
                            {
                                pxIndex = MathF.Clamp(kerningY[sampleIndex] + off - 1, 0, glyphTemp.Height - 1) * glyphTemp.Width;
                                c[off] = 0;
                                while (glyphTemp.Data[pxIndex + c[off]].A == 0)
                                {
                                    c[off]++;
                                    if (c[off] >= glyphTemp.Width / 2) break;
                                }
                                this.glyphs[i].kerningSamplesLeft[sampleIndex] = Math.Min(this.glyphs[i].kerningSamplesLeft[sampleIndex], c[off]);
                            }
                        }
                        // Right side samples
                        for (int sampleIndex = 0; sampleIndex < this.glyphs[i].kerningSamplesRight.Length; sampleIndex++)
                        {
                            this.glyphs[i].kerningSamplesRight[sampleIndex] = glyphTemp.Width / 2;
                            for (int off = 0; off <= 2; off++)
                            {
                                pxIndex = MathF.Clamp(kerningY[sampleIndex] + off - 1, 0, glyphTemp.Height - 1) * glyphTemp.Width + glyphTemp.Width - 1;
                                c[off] = 0;
                                while (glyphTemp.Data[pxIndex - c[off]].A == 0)
                                {
                                    c[off]++;
                                    if (c[off] >= glyphTemp.Width / 2) break;
                                }
                                this.glyphs[i].kerningSamplesRight[sampleIndex] = Math.Min(this.glyphs[i].kerningSamplesRight[sampleIndex], c[off]);
                            }
                        }
                    }

                    glyphTemp.Dispose();
                }
            }
            else
            {
                for (int i = 0; i < SupportedChars.Length; ++i)
                {
                    this.glyphs[i].kerningSamplesLeft	= null;
                    this.glyphs[i].kerningSamplesRight	= null;
                }
            }
        }