Ejemplo n.º 1
0
        double DrawText(Graphics graphics,
                        string font, string text,
                        double x, double y, double height,
                        Color color)
        {
            Color outlineColor = Color.Gray;

            if (color == Color.Black &&
                m_InvertCheckBox.IsChecked)
            {
                color = Color.White;
            }

            SimpleFont.GlyphRenderType gren = SimpleFont.GlyphRenderType.Outline;

            double scale_x = 100;

            if (m_AltNETTypeFontManager.LoadFont(font, gren))
            {
                m_AltNETTypeFontManager.Height  = height;
                m_AltNETTypeFontManager.Width   = height * scale_x;
                m_AltNETTypeFontManager.Hinting = m_HintingCheckBox.IsChecked;

                int text_index = 0;
                int text_len   = text.Length;

                double start_x  = x;
                bool   fHinting = m_HintingCheckBox.IsChecked;
                bool   fKerning = m_KerningCheckBox.IsChecked;
                double interval = m_IntervalSlider.Value;
                double italic   = m_AuxItalicSlider.Value;
                double weight   = m_AuxWeightSlider.Value;
                double width    = m_WidthSlider.Value;
                m_WeightedTransformedFlattenGlyph.Weight = -weight * height / 15;
                bool fWeightedTransformedFlattenGlyph = System.Math.Abs(weight) > 0.05;

                bool   fFill     = m_FillCheckBox.IsChecked;
                bool   fOutline  = m_OutlineCheckBox.IsChecked;
                double thickness = m_OutlineThicknessSlider.Value;

                while (text_index < text_len)
                {
                    if (text[text_index] == '\n')
                    {
                        x  = start_x;
                        y += height * 1.25;

                        text_index++;
                        continue;
                    }

                    SimpleFontCacheManager.GlyphCache glyph = m_AltNETTypeFontManager.GetGlyph(text[text_index]);
                    if (glyph != null)
                    {
                        if (fKerning)
                        {
                            m_AltNETTypeFontManager.AddKerning(ref x, ref y);
                        }

                        if (glyph.data_type == SimpleFont.GlyphDataType.Outline)
                        {
                            m_FlattenGlyph.Geometry = glyph.m_Data as Geometry;

                            double ty = fHinting ? System.Math.Floor(y + 0.5) : y;
                            m_mtx = Matrix4.Identity;
                            m_mtx.Scale(width / scale_x, 1, MatrixOrder.Append);
                            m_mtx.Multiply(Matrix4.CreateSkewing(-italic / 3, 0), MatrixOrder.Append);
                            m_mtx.Translate(start_x + x / scale_x, ty, MatrixOrder.Append);
                            m_TransformedFlattenGlyph.Transform = m_mtx;

                            Geometry geometry;
                            if (fWeightedTransformedFlattenGlyph)
                            {
                                m_WeightedTransformedFlattenGlyph.Modified();
                                geometry = m_WeightedTransformedFlattenGlyph;
                            }
                            else
                            {
                                geometry = m_TransformedFlattenGlyph;
                            }

                            if (fFill)
                            {
                                graphics.FillGeometry(color, geometry);
                            }
                            if (fOutline)
                            {
                                graphics.DrawGeometry(outlineColor, geometry, thickness);
                            }
                        }

                        // increment pen position
                        x += glyph.advance_x + interval * scale_x;
                        y += glyph.advance_y;
                    }

                    text_index++;
                }

                y += height * 1.6;
            }

            return(y);
        }
        double DrawText(Graphics graphics,
                        string font, string text,
                        double x, double y, double height,
                        Color color,
                        SimpleFont.GlyphRenderType glyphRenderType)
        {
            if (color == Color.Black &&
                m_InvertCheckBox.IsChecked)
            {
                color = Color.White;
            }


            if (m_AltNETTypeFontManager.LoadFont(font, glyphRenderType))
            {
                m_AltNETTypeFontManager.Hinting = m_HintingCheckBox.IsChecked;
                m_AltNETTypeFontManager.Height  = m_HeightSlider.Value;
                m_AltNETTypeFontManager.Width   = m_WidthSlider.Value;
                m_AltNETTypeFontManager.FlipY   = true;

                double start_x  = x;
                bool   fHinting = m_HintingCheckBox.IsChecked;
                bool   fKerning = m_KerningCheckBox.IsChecked;

                double thickness = m_OutlineThicknessSlider.Value * height / 72;

                int len = text.Length;
                for (int i = 0; i < len; i++)
                {
                    char CharValue = text[i];

                    if (CharValue == '\n')
                    {
                        x  = start_x;
                        y += height * 1.25;

                        continue;
                    }


                    SimpleFontCacheManager.GlyphCache glyph = m_AltNETTypeFontManager.GetGlyph(CharValue);
                    if (glyph != null)
                    {
                        if (fKerning)
                        {
                            m_AltNETTypeFontManager.AddKerning(ref x, ref y);
                        }


                        double tx = fHinting ? System.Math.Floor(x + 0.5) : x;
                        double ty = fHinting ? System.Math.Floor(y + 0.5) : y;


                        switch (glyph.data_type)
                        {
                        case SimpleFont.GlyphDataType.Mono:
                        case SimpleFont.GlyphDataType.Gray8:

                            Bitmap bitmap = glyph.m_Data as Bitmap;
                            if (bitmap != null)
                            {
                                BitmapData bitmapData = bitmap.LockBits(ImageLockMode.WriteOnly);
                                if (bitmapData != null)
                                {
                                    byte[] data   = bitmapData.Scan0;
                                    int    stride = bitmapData.Stride;
                                    int    h      = bitmap.PixelHeight;
                                    int    w      = bitmap.PixelWidth;

                                    byte r = color.R;
                                    byte g = color.G;
                                    byte b = color.B;

                                    for (int by = 0; by < h; by++)
                                    {
                                        int index = by * stride;
                                        for (int bx = 0; bx < w; bx++, index += 4)
                                        {
                                            data[index]     = r;
                                            data[index + 1] = g;
                                            data[index + 2] = b;
                                        }
                                    }

                                    graphics.DrawImage(bitmap, tx + glyph.bounds.Left, ty - glyph.bounds.Top);

                                    bitmap.UnlockBits(bitmapData);
                                }
                            }

                            break;

                        case SimpleFont.GlyphDataType.Outline:
                            m_FlattenGlyph.Geometry = glyph.m_Data as Geometry;
                            if (m_FlattenGlyph.Geometry != null)
                            {
                                m_mtx = Matrix4.Identity;
                                m_mtx.Translate(x, ty, MatrixOrder.Append);
                                m_TransformedFlattenGlyph.Transform = m_mtx;

                                graphics.DrawGeometry(color, m_TransformedFlattenGlyph, thickness);
                            }

                            break;
                        }

                        // increment pen position
                        x += glyph.advance_x;
                        y += glyph.advance_y;
                    }
                }

                y += height * 1.5;
            }

            return(y);
        }