public void DrawString(string str, double x, double y, StringAlignment align, Size layoutRect, StringColorType colorType = StringColorType.Forground)
        {
            if (this.render == null || this.render.IsDisposed)
            {
                return;
            }
            float dpix, dpiy;

            D2D.SolidColorBrush brush;
            switch (colorType)
            {
            case StringColorType.Forground:
                brush = this._factory.GetSolidColorBrush(this.Foreground);
                break;

            case StringColorType.LineNumber:
                brush = this._factory.GetSolidColorBrush(this.LineNumber);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            this.GetDpi(out dpix, out dpiy);
            MyTextLayout layout = this._factory.GetTextLayout(str, this.format, (float)layoutRect.Width, (float)layoutRect.Height, dpix, false);

            layout.StringAlignment = align;
            layout.Draw(this.render, (float)x, (float)y, brush);
            layout.Dispose();
        }
 public void SetTextColor(MyTextLayout layout, int start, int length, Color4?color)
 {
     if (color == null)
     {
         return;
     }
     layout.SetDrawingEffect(this._factory.GetSolidColorBrush((Color4)color), new DW.TextRange(start, length));
 }
        public void DrawMarkerEffect(MyTextLayout layout, HilightType type, int start, int length, double x, double y, bool isBold, Color4?effectColor = null)
        {
            if (type == HilightType.None)
            {
                return;
            }

            float thickness = isBold ? BoldThickness : NormalThickness;

            Color4 color;

            if (effectColor != null)
            {
                color = (Color4)effectColor;
            }
            else if (type == HilightType.Select)
            {
                color = this.Hilight;
            }
            else
            {
                color = this.Foreground;
            }

            IMarkerEffecter effecter = null;

            D2D.SolidColorBrush brush = this._factory.GetSolidColorBrush(color);

            if (type == HilightType.Squiggle)
            {
                effecter = new D2DSquilleLineMarker(this.render, brush, this._factory.GetStroke(HilightType.Squiggle), thickness);
            }
            else if (type == HilightType.Select)
            {
                effecter = new HilightMarker(this.render, brush);
            }
            else if (type == HilightType.None)
            {
                effecter = null;
            }
            else
            {
                effecter = new LineMarker(this.render, brush, this._factory.GetStroke(type), thickness);
            }

            if (effecter != null)
            {
                bool isUnderline = type != HilightType.Select;

                DW.HitTestMetrics[] metrics = layout.HitTestTextRange(start, length, (float)x, (float)y);
                foreach (DW.HitTestMetrics metric in metrics)
                {
                    float offset = isUnderline ? metric.Height : 0;
                    effecter.Draw(metric.Left, metric.Top + offset, metric.Width, metric.Height);
                }
            }
        }
        public const int MiniumeWidth = 40;    //これ以上ないと誤操作が起こる

        public void InitTextFormat(string fontName, float fontSize, DW.FontWeight fontWeigth = DW.FontWeight.Normal, DW.FontStyle fontStyle = DW.FontStyle.Normal)
        {
            if (this.format != null)
            {
                this.format.Dispose();
            }

            float dpix, dpiy;

            this.GetDpi(out dpix, out dpiy);

            float fontSizeInDIP = fontSize * (dpix / 72.0f);

            this.format = this._factory.GetTextFormat(fontName, fontSizeInDIP, fontWeigth, fontStyle);
            this.format.WordWrapping     = DW.WordWrapping.NoWrap;
            this.format.ReadingDirection = GetDWRightDirect(_RightToLeft);

            MyTextLayout layout = this._factory.GetTextLayout("0", this.format, float.MaxValue, float.MaxValue, dpix, false);

            layout.RightToLeft = false;
            this.emSize        = new Size(layout.Width, layout.Height);
            layout.Dispose();

            this.TabWidthChar = this.TabWidthChar;

            this.hasCache = false;

            layout             = this._factory.GetTextLayout("+", this.format, float.MaxValue, float.MaxValue, dpix, false);
            layout.RightToLeft = false;
#if METRO
            this.FoldingWidth = Math.Max(D2DRenderCommon.MiniumeWidth, layout.Width);
#else
            this.FoldingWidth = layout.Width;
#endif
            layout.Dispose();

            this._factory.Clear();

            this.OnChangedRenderResource(this, new ChangedRenderRsourceEventArgs(ResourceType.Font));
        }
        public ITextLayout CreateLaytout(string str, SyntaxInfo[] syntaxCollection, IEnumerable <Marker> MarkerRanges, IEnumerable <Selection> SelectRanges, double WrapWidth)
        {
            float dpix, dpiy;

            this.GetDpi(out dpix, out dpiy);

            double layoutWidth = this.TextArea.Width;

            if (WrapWidth != LineToIndexTable.NONE_BREAK_LINE)
            {
                this.format.WordWrapping = DW.WordWrapping.Wrap;
                layoutWidth = WrapWidth;
            }
            else
            {
                this.format.WordWrapping = DW.WordWrapping.NoWrap;
            }

            bool         hasNewLine = str.Length > 0 && str[str.Length - 1] == Document.NewLine;
            MyTextLayout newLayout  = this._factory.GetTextLayout(
                str,
                this.format,
                layoutWidth,
                this.TextArea.Height,
                dpiy,
                hasNewLine && this.ShowLineBreak);

            newLayout.SetLineSpacing(this.emSize.Height);
            newLayout.SetLineBreakBrush(this._factory.GetSolidColorBrush(this.ControlChar));
            if (syntaxCollection != null)
            {
                foreach (SyntaxInfo s in syntaxCollection)
                {
                    D2D.SolidColorBrush brush = this._factory.GetSolidColorBrush(this.Foreground);
                    switch (s.type)
                    {
                    case TokenType.Comment:
                        brush = this._factory.GetSolidColorBrush(this.Comment);
                        break;

                    case TokenType.Keyword1:
                        brush = this._factory.GetSolidColorBrush(this.Keyword1);
                        break;

                    case TokenType.Keyword2:
                        brush = this._factory.GetSolidColorBrush(this.Keyword2);
                        break;

                    case TokenType.Literal:
                        brush = this._factory.GetSolidColorBrush(this.Literal);
                        break;
                    }
                    newLayout.SetDrawingEffect(brush, new DW.TextRange(s.index, s.length));
                }
            }

            if (syntaxCollection != null)
            {
                foreach (SyntaxInfo s in syntaxCollection)
                {
                    D2D.SolidColorBrush brush = null;
                    switch (s.type)
                    {
                    case TokenType.Comment:
                        brush = this._factory.GetSolidColorBrush(this.Comment);
                        break;

                    case TokenType.Keyword1:
                        brush = this._factory.GetSolidColorBrush(this.Keyword1);
                        break;

                    case TokenType.Keyword2:
                        brush = this._factory.GetSolidColorBrush(this.Keyword2);
                        break;

                    case TokenType.Literal:
                        brush = this._factory.GetSolidColorBrush(this.Literal);
                        break;
                    }
                    newLayout.SetDrawingEffect(brush, new DW.TextRange(s.index, s.length));
                }
            }

            if (MarkerRanges != null)
            {
                newLayout.Markers = MarkerRanges.ToArray();
                foreach (Marker sel in newLayout.Markers)
                {
                    if (sel.length == 0 || sel.start == -1)
                    {
                        continue;
                    }
                    if (sel.hilight == HilightType.Url)
                    {
                        newLayout.SetDrawingEffect(this._factory.GetSolidColorBrush(this.Url), new DW.TextRange(sel.start, sel.length));
                    }
                }
            }

            if (SelectRanges != null)
            {
                newLayout.Selects = SelectRanges.ToArray();
                if (this.HilightForeground.Alpha > 0.0)
                {
                    foreach (Selection sel in SelectRanges)
                    {
                        if (sel.length == 0 || sel.start == -1)
                        {
                            continue;
                        }

                        newLayout.SetDrawingEffect(this._factory.GetSolidColorBrush(this.HilightForeground), new DW.TextRange(sel.start, sel.length));
                    }
                }
            }

            this.format.WordWrapping = DW.WordWrapping.NoWrap;

            return(newLayout);
        }
        public void DrawOneLine(Document doc, LineToIndexTable lti, int row, double x, double y, PreDrawOneLineHandler PreDrawOneLine)
        {
            int lineLength = lti.GetLengthFromLineNumber(row);

            if (lineLength == 0 || this.render == null || this.render.IsDisposed)
            {
                return;
            }

            MyTextLayout layout = (MyTextLayout)lti.GetLayout(row);

            if (PreDrawOneLine != null)
            {
                PreDrawOneLine(layout, lti, row, x, y);
            }

            if (layout.Markers != null)
            {
                foreach (Marker sel in layout.Markers)
                {
                    if (sel.length == 0 || sel.start == -1)
                    {
                        continue;
                    }
                    Color4 color = new Color4()
                    {
                        Alpha = sel.color.A, Red = sel.color.R, Blue = sel.color.B, Green = sel.color.G
                    };
                    if (sel.hilight == HilightType.Url)
                    {
                        color = this.Url;
                    }
                    this.DrawMarkerEffect(layout, sel.hilight, sel.start, sel.length, x, y, sel.isBoldLine, color);
                }
            }
            if (layout.Selects != null)
            {
                foreach (Selection sel in layout.Selects)
                {
                    if (sel.length == 0 || sel.start == -1)
                    {
                        continue;
                    }

                    this.DrawMarkerEffect(layout, HilightType.Select, sel.start, sel.length, x, y, false);
                }
            }

            if (this.ShowFullSpace || this.ShowHalfSpace || this.ShowTab)
            {
                string str = lti[row];
                D2D.GeometryRealization geo = null;
                for (int i = 0; i < lineLength; i++)
                {
                    Point pos = new Point(0, 0);
                    if (this.ShowTab && str[i] == '\t')
                    {
                        pos = layout.GetPostionFromIndex(i);
                        geo = this._factory.CreateSymbol(ShowSymbol.Tab, this.format);
                    }
                    else if (this.ShowFullSpace && str[i] == ' ')
                    {
                        pos = layout.GetPostionFromIndex(i);
                        geo = this._factory.CreateSymbol(ShowSymbol.FullSpace, this.format);
                    }
                    else if (this.ShowHalfSpace && str[i] == ' ')
                    {
                        pos = layout.GetPostionFromIndex(i);
                        geo = this._factory.CreateSymbol(ShowSymbol.HalfSpace, this.format);
                    }
                    if (geo != null)
                    {
                        var old_trans = this.render.Transform;
                        this.render.Transform = SharpDX.Matrix3x2.Translation(new Vector2((float)(pos.X + x), (float)(pos.Y + y)));
                        this.render.DrawGeometryRealization(geo, this._factory.GetSolidColorBrush(this.ControlChar));
                        this.render.Transform = old_trans;
                        geo = null;
                    }
                }
            }

            layout.Draw(this.render, (float)x, (float)y, this._factory.GetSolidColorBrush(this.Foreground));
        }