示例#1
0
        protected override void Apply(TextLayout layout, bool enable, int slice)
        {
            var res = layout.SetFontStyle(this.style[slice], new TextRange(ffrom[slice], fto[slice]));

            layout.SetFontWeight(this.w[slice], new TextRange(ffrom[slice], fto[slice]));
            layout.SetFontStretch(this.s[slice], new TextRange(ffrom[slice], fto[slice]));
        }
示例#2
0
        public D2D_TextElements CreateNewTextElements(UText t)
        {
            var textLayout = new TextLayout(dwFact, t.text, new TextFormat(
                                                dwFact,
                                                t.font.name,
                                                t.font.bold ? FontWeight.Bold : FontWeight.Normal,
                                                t.font.italic ? FontStyle.Italic : FontStyle.Normal,
                                                TranslateFontSize(t.font.size))
            {
                ParagraphAlignment = Translate(t.valign),
                TextAlignment      = Translate(t.halign),
                WordWrapping       = t.wrapped ? WordWrapping.Wrap : WordWrapping.NoWrap
            }, t.width, t.height);

            // Set font ranges... textLayout just created, dont worry about any leftover ranges.
            foreach (var sr in t.SafeGetStyleRanges)
            {
                var tr = new TextRange(sr.start, sr.length);
                if (sr.fontOverride != null)
                {
                    UFont ft = (UFont)sr.fontOverride;
                    textLayout.SetFontFamilyName(ft.name, tr);
                    textLayout.SetFontSize(TranslateFontSize(ft.size), tr);
                    textLayout.SetFontStyle(ft.italic ? FontStyle.Italic : FontStyle.Normal, tr);
                    textLayout.SetFontWeight(ft.bold ? FontWeight.Bold : FontWeight.Normal, tr);
                }
                if (sr.fgOverride != null || sr.bgOverride != null)
                {
                    ClientTextEffect cte = new ClientTextEffect();
                    if (sr.fgOverride != null)
                    {
                        cte.fgBrush = CreateBrush(sr.fgOverride);
                    }
                    if (sr.bgOverride != null)
                    {
                        cte.bgBrush = CreateBrush(sr.bgOverride);
                    }
                    textLayout.SetDrawingEffect(cte, tr);
                }
            }

            // Set renderer with a default brush
            var def = new USolidBrush()
            {
                color = new Color(0)
            };
            var textRenderer = new D2D_ClientTextRenderer(realRenderer.renderTarget, new ClientTextEffect()
            {
                fgBrush = CreateBrush(def)
            });

            var ret = new D2D_TextElements(textLayout, textRenderer);

            realRenderer.renderTarget.Disposed += new EventHandler <EventArgs>((o, e) => t.Invalidate());
            return(ret);
        }
示例#3
0
        int DrawLinePortion(Context cr, ChunkStyle style, TextLayout layout, DocumentLine line, int visualOffset, int logicalLength)
        {
            int logicalColumn    = line.GetLogicalColumn(editor, visualOffset);
            int logicalEndColumn = logicalColumn + logicalLength;
            int visualEndOffset  = line.GetVisualColumn(editor, logicalEndColumn);

            int visualLength = visualEndOffset - visualOffset;

            int indexOffset = visualOffset - 1;

            layout.SetFontStyle(style.FontStyle, indexOffset, visualLength);
            layout.SetFontWeight(style.FontWeight, indexOffset, visualLength);
            if (style.Underline)
            {
                layout.SetUnderline(indexOffset, visualLength);
            }
            layout.SetForeground(style.Foreground, indexOffset, visualLength);

            return(visualEndOffset);
        }
示例#4
0
        /// <summary>
        /// 取得字符对应的宽度
        /// </summary>
        /// <param name="character"></param>
        /// <returns></returns>
        private float GetCharacterWidth(Character character)
        {
            DWriteFactory dwFactory  = new DWriteFactory(SharpDX.DirectWrite.FactoryType.Shared);
            Vector2       offset     = new Vector2(0.0f, 0.0f);
            TextFormat    textFormat = new TextFormat(dwFactory,
                                                      character.font.FontFamily.Name,
                                                      character.font.Bold ? SharpDX.DirectWrite.FontWeight.Bold : SharpDX.DirectWrite.FontWeight.Regular,
                                                      character.font.Italic ? SharpDX.DirectWrite.FontStyle.Italic : SharpDX.DirectWrite.FontStyle.Normal,
                                                      character.font.Size);
            TextLayout textLayout = new TextLayout(dwFactory,
                                                   "好" + character.content,
                                                   textFormat,
                                                   this.pageWidth,
                                                   this.pageHeight);

            textLayout.SetUnderline(character.font.Underline, new TextRange(0, character.content.Length));
            textLayout.SetFontStyle(character.font.Italic ? FontStyle.Italic : FontStyle.Normal, new TextRange(0, character.content.Length));
            textLayout.SetFontWeight(character.font.Bold ? FontWeight.Bold : FontWeight.Normal, new TextRange(0, character.content.Length));
            ClusterMetrics[] clusterMetrics = textLayout.GetClusterMetrics();

            float result          = 0.0f;
            float prefixCharWidth = clusterMetrics[0].Width;

            foreach (ClusterMetrics v in clusterMetrics)
            {
                result += v.Width;
            }

            dwFactory.Dispose();
            textFormat.Dispose();
            textLayout.Dispose();
            dwFactory  = null;
            textFormat = null;
            textLayout = null;

            return(result - prefixCharWidth);
        }
示例#5
0
        public virtual void Texts(Xwt.Drawing.Context ctx, double x, double y)
        {
            ctx.Save();

            ctx.Translate(x, y);

            ctx.SetColor(Colors.Black);

            var col1 = new Rectangle();
            var col2 = new Rectangle();

            var text = new TextLayout();

            text.Font = this.Font.WithSize(24);
            Console.WriteLine(text.Font.Size);

            // first text
            text.Text = "Lorem ipsum dolor sit amet,";
            var size1 = text.GetSize();

            col1.Width   = size1.Width;
            col1.Height += size1.Height + 10;
            ctx.DrawTextLayout(text, 0, 0);


            // proofing width; test should align with text above
            ctx.SetColor(Colors.DarkMagenta);
            text.Text  = "consetetur sadipscing elitr, sed diam nonumy";
            text.Width = col1.Width;
            var size2 = text.GetSize();

            ctx.DrawTextLayout(text, 0, col1.Bottom);
            col1.Height += size2.Height + 10;

            ctx.SetColor(Colors.Black);

            // proofing scale, on second col
            ctx.Save();
            ctx.SetColor(Colors.Red);
            col2.Left = col1.Right + 10;

            text.Text = "eirmod tempor invidunt ut.";

            var scale = 1.2;

            text.Width = text.Width / scale;
            var size3 = text.GetSize();

            col2.Height = size3.Height * scale;
            col2.Width  = size3.Width * scale + 5;
            ctx.Scale(scale, scale);
            ctx.DrawTextLayout(text, col2.Left / scale, col2.Top / scale);
            ctx.Restore();

            // proofing heigth, on second col
            ctx.Save();
            ctx.SetColor(Colors.DarkCyan);
            text.Text = "Praesent ac lacus nec dolor pulvinar feugiat a id elit.";
            var size4 = text.GetSize();

            text.Height   = size4.Height / 2;
            text.Trimming = TextTrimming.WordElipsis;
            ctx.DrawTextLayout(text, col2.Left, col2.Bottom + 5);

            ctx.SetLineWidth(1);
            ctx.SetColor(Colors.Blue);
            ctx.Rectangle(new Rectangle(col2.Left, col2.Bottom + 5, text.Width, text.Height));
            ctx.Stroke();
            ctx.Restore();

            // drawing col line
            ctx.SetLineWidth(1);

            ctx.SetColor(Colors.Black.WithAlpha(.5));
            ctx.MoveTo(col1.Right + 5, col1.Top);
            ctx.LineTo(col1.Right + 5, col1.Bottom);
            ctx.Stroke();
            ctx.MoveTo(col2.Right + 5, col2.Top);
            ctx.LineTo(col2.Right + 5, col2.Bottom);
            ctx.Stroke();
            ctx.SetColor(Colors.Black);

            // proofing rotate, and printing size to see the values
            ctx.Save();

            text.Font = this.Font.WithSize(10);
            text.Text = string.Format("Size 1 {0}\r\nSize 2 {1}\r\nSize 3 {2} Scale {3}",
                                      size1, size2, size3, scale);
            text.Width  = -1;            // this clears textsize
            text.Height = -1;
            ctx.Rotate(5);
            // maybe someone knows a formula with angle and textsize to calculyte ty
            var ty = 30;

            ctx.DrawTextLayout(text, ty, col1.Bottom + 10);

            ctx.Restore();

            // scale example here:

            ctx.Restore();

            TextLayout tl0 = new TextLayout(this);

            tl0.Font = this.Font.WithSize(10);
            tl0.Text = "This text contains attributes.";
            tl0.SetUnderline(0, "This".Length);
            tl0.SetForeground(new Color(0, 1.0, 1.0), "This ".Length, "text".Length);
            tl0.SetBackground(new Color(0, 0, 0), "This ".Length, "text".Length);
            tl0.SetFontWeight(FontWeight.Bold, "This text ".Length, "contains".Length);
            tl0.SetFontStyle(FontStyle.Italic, "This text ".Length, "contains".Length);
            tl0.SetStrikethrough("This text contains ".Length, "attributes".Length);

            ctx.DrawTextLayout(tl0, col2.Left, col2.Bottom + 100);


            // Text boces

            y = 180;

            // Without wrapping

            TextLayout tl = new TextLayout(this);

            tl.Text = "Stright text";
            DrawText(ctx, tl, ref y);

            // With wrapping

            tl       = new TextLayout(this);
            tl.Text  = "The quick brown fox jumps over the lazy dog";
            tl.Width = 100;
            DrawText(ctx, tl, ref y);

            // With blank lines

            tl       = new TextLayout(this);
            tl.Text  = "\nEmpty line above\nLine break above\n\nEmpty line above\n\n\nTwo empty lines above\nEmpty line below\n";
            tl.Width = 200;
            DrawText(ctx, tl, ref y);
        }
示例#6
0
        public override BuiltRichText BuildText(IList <RichTextNode> nodes, int width, float sizeMultiplier = 1)
        {
            var paragraphs = new List <List <RichTextNode> >();

            paragraphs.Add(new List <RichTextNode>());
            int first = 0;

            while (nodes[first] is RichTextParagraphNode && first < nodes.Count)
            {
                first++;
            }
            DWrite.TextAlignment ta = CastAlignment((nodes[first] as RichTextTextNode).Alignment);
            paragraphs[paragraphs.Count - 1].Add(nodes[first]);
            foreach (var node in nodes.Skip(first + 1))
            {
                if (node is RichTextParagraphNode)
                {
                    paragraphs.Add(new List <RichTextNode>());
                }
                else
                {
                    var n     = (RichTextTextNode)node;
                    var align = CastAlignment(n.Alignment);
                    if (align != ta && paragraphs[paragraphs.Count - 1].Count > 0)
                    {
                        paragraphs.Add(new List <RichTextNode>());
                    }
                    paragraphs[paragraphs.Count - 1].Add(node);
                    ta = align;
                }
            }
            string lastFont = null;
            float  lastSize = 0;
            //Format text
            var layouts = new List <TextLayout>();

            for (int j = 0; j < paragraphs.Count; j++)
            {
                var p       = paragraphs[j];
                var builder = new StringBuilder();
                foreach (var n in p)
                {
                    builder.Append(((RichTextTextNode)n).Contents);
                }
                var layout = new TextLayout(
                    dwFactory,
                    builder.ToString(),
                    new TextFormat(
                        dwFactory,
                        string.IsNullOrEmpty(lastFont) ? "Arial" : lastFont,
                        lastSize > 0 ? lastSize : 12
                        ),
                    width,
                    float.MaxValue
                    );
                if (p.Count > 0)
                {
                    layout.TextAlignment = CastAlignment(((RichTextTextNode)p[0]).Alignment);
                }
                int startIdx = 0;
                foreach (var n in p)
                {
                    var text  = (RichTextTextNode)n;
                    var range = new TextRange(startIdx, text.Contents.Length);
                    if (text.Bold)
                    {
                        layout.SetFontWeight(FontWeight.Bold, range);
                    }
                    if (text.Italic)
                    {
                        layout.SetFontStyle(FontStyle.Italic, range);
                    }
                    if (text.Underline)
                    {
                        layout.SetUnderline(true, range);
                    }
                    if (!string.IsNullOrEmpty(text.FontName))
                    {
                        layout.SetFontFamilyName(text.FontName, range);
                        lastFont = text.FontName;
                    }
                    if (text.FontSize > 0)
                    {
                        layout.SetFontSize(text.FontSize, range);
                        lastSize = text.FontSize;
                    }
                    layout.SetDrawingEffect(new ColorDrawingEffect(text.Color), range);
                    startIdx += text.Contents.Length;
                }
                layouts.Add(layout);
            }
            //Return
            var built = new DirectWriteBuiltText(this)
            {
                Layout = layouts, Width = width
            };

            built.CacheQuads();
            return(built);
        }
示例#7
0
        public void DrawMessages()
        {
            if (msgs.Count == 0)
            {
                return;
            }

            lastText = text;
            text     = "";

            foreach (OSDMessage msg in msgs)
            {
                text += msg.msg + "\r\n";
            }
            text = text.Trim();

            if (text == "")
            {
                return;
            }
            if (lastText == text && !requiresUpdate)
            {
                msgs.Clear(); Draw(); return;
            }

            Utilities.Dispose(ref layout);
            if (brushes.Count > 0)
            {
                foreach (var brush in brushes)
                {
                    SolidColorBrush tmp = brush;
                    Utilities.Dispose(ref tmp);
                }
                brushes = new List <SolidColorBrush>();
            }

            layout = new TextLayout(renderer.factoryWrite, text, format, hookViewport ? renderer.GetViewport.Width : renderer.HookControl.Width, hookViewport ? renderer.GetViewport.Height : renderer.HookControl.Height);

            // Fix Styling
            int curPos = 0;

            foreach (OSDMessage msg in msgs)
            {
                if (msg.styles == null)
                {
                    curPos += msg.msg.Length + 2; continue;
                }

                foreach (SubStyle style in msg.styles)
                {
                    switch (style.style)
                    {
                    case SubStyles.BOLD:
                        layout.SetFontWeight(FontWeight.Bold, new TextRange(curPos + style.from, style.len));
                        break;

                    case SubStyles.ITALIC:
                        layout.SetFontStyle(FontStyle.Italic, new TextRange(curPos + style.from, style.len));
                        break;

                    case SubStyles.UNDERLINE:
                        layout.SetUnderline(true, new TextRange(curPos + style.from, style.len));
                        break;

                    case SubStyles.STRIKEOUT:
                        layout.SetStrikethrough(true, new TextRange(curPos + style.from, style.len));
                        break;

                    case SubStyles.COLOR:
                        SolidColorBrush brush2dtmp = new SolidColorBrush(renderer.rtv2d, (Color)style.value);
                        brushes.Add(brush2dtmp);
                        layout.SetDrawingEffect(brush2dtmp.NativePointer, new TextRange(curPos + style.from, style.len));

                        break;
                    }
                }

                curPos += msg.msg.Length + 2;
            }

            CreateRectangle();

            msgs.Clear();
            Draw();

            requiresUpdate = false;
        }
示例#8
0
        /// <summary>
        /// 从rss生成图像
        /// </summary>
        /// <param name="channel">从rss源获得的数据</param>
        /// <param name="path">保存图像的路径</param>
        private void GetImageFromRss(object obj)
        {
            ImageObj image    = (ImageObj)obj;
            RssInfo  rssInfo  = image.rssInfo;
            RssMedia rssMedia = image.rssMedia;
            string   fileName = "";

            if (rssInfo == null || rssMedia == null)
            {
                return;
            }

            List <Page> pages                  = GetPageListFromRss(rssInfo, rssMedia);
            var         wicFactory             = new ImagingFactory();
            var         d2dFactory             = new SharpDX.Direct2D1.Factory();
            var         dwFactory              = new SharpDX.DirectWrite.Factory();
            var         renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default,
                                                                            new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Unknown),
                                                                            0,
                                                                            0,
                                                                            RenderTargetUsage.None, FeatureLevel.Level_DEFAULT);

            var wicBitmap = new SharpDX.WIC.Bitmap(wicFactory,
                                                   pageWidth,
                                                   pageHeight,
                                                   SharpDX.WIC.PixelFormat.Format32bppBGR,
                                                   BitmapCreateCacheOption.CacheOnLoad);
            var d2dRenderTarget = new WicRenderTarget(d2dFactory,
                                                      wicBitmap,
                                                      renderTargetProperties);

            d2dRenderTarget.AntialiasMode = AntialiasMode.PerPrimitive;
            var solidColorBrush = new SolidColorBrush(d2dRenderTarget,
                                                      new SharpDX.Color(this.backgroundColor.R,
                                                                        this.backgroundColor.G,
                                                                        this.backgroundColor.B,
                                                                        this.backgroundColor.A));
            var textBodyBrush = new SolidColorBrush(d2dRenderTarget,
                                                    new SharpDX.Color(rssMedia.RssBodyProp.
                                                                      TextColor.R,
                                                                      rssMedia.RssBodyProp.TextColor.G,
                                                                      rssMedia.RssBodyProp.TextColor.B,
                                                                      rssMedia.RssBodyProp.TextColor.A));
            var titleColorBrush = new SolidColorBrush(d2dRenderTarget,
                                                      new SharpDX.Color(rssMedia.RssTitleProp.TextColor.R,
                                                                        rssMedia.RssTitleProp.TextColor.G,
                                                                        rssMedia.RssTitleProp.TextColor.B,
                                                                        rssMedia.RssTitleProp.TextColor.A));
            var publishDateColorBrush = new SolidColorBrush(d2dRenderTarget,
                                                            new SharpDX.Color(rssMedia.RssPublishTimeProp.TextColor.R,
                                                                              rssMedia.RssPublishTimeProp.TextColor.G,
                                                                              rssMedia.RssPublishTimeProp.TextColor.B,
                                                                              rssMedia.RssPublishTimeProp.TextColor.A));
            TextLayout textLayout;

            try
            {
                int count = 0;

                foreach (Page page in pages)
                {
                    d2dRenderTarget.BeginDraw();
                    d2dRenderTarget.Clear(new SharpDX.Color(this.backgroundColor.R,
                                                            this.backgroundColor.G,
                                                            this.backgroundColor.B,
                                                            this.backgroundColor.A));

                    foreach (Line line in page.lines)
                    {
                        foreach (Block block in line.content)
                        {
                            TextFormat textFormat = new TextFormat(dwFactory,
                                                                   block.font.FontFamily.Name,
                                                                   block.font.Size);
                            textLayout = new TextLayout(dwFactory,
                                                        block.content,
                                                        textFormat,
                                                        block.width,
                                                        block.height);
                            switch (block.type)
                            {
                            case RssBodyType.Title:
                                textLayout.SetUnderline(rssMedia.RssTitleProp.TextFont.Underline,
                                                        new TextRange(0, block.content.Length));
                                textLayout.SetFontStyle(rssMedia.RssTitleProp.TextFont.Italic ? FontStyle.Italic : FontStyle.Normal,
                                                        new TextRange(0, block.content.Length));
                                textLayout.SetFontWeight(rssMedia.RssTitleProp.TextFont.Bold ? FontWeight.Bold : FontWeight.Normal,
                                                         new TextRange(0, block.content.Length));
                                d2dRenderTarget.DrawTextLayout(new Vector2(block.left, block.top), textLayout, titleColorBrush);
                                break;

                            case RssBodyType.Time:
                                textLayout.SetUnderline(rssMedia.RssPublishTimeProp.TextFont.Underline,
                                                        new TextRange(0, block.content.Length));
                                textLayout.SetFontStyle(rssMedia.RssPublishTimeProp.TextFont.Italic ? FontStyle.Italic : FontStyle.Normal,
                                                        new TextRange(0, block.content.Length));
                                textLayout.SetFontWeight(rssMedia.RssPublishTimeProp.TextFont.Bold ? FontWeight.Bold : FontWeight.Normal,
                                                         new TextRange(0, block.content.Length));
                                d2dRenderTarget.DrawTextLayout(new Vector2(block.left, block.top), textLayout, publishDateColorBrush);
                                break;

                            case RssBodyType.Body:
                                textLayout.SetUnderline(rssMedia.RssBodyProp.TextFont.Underline,
                                                        new TextRange(0, block.content.Length));
                                textLayout.SetFontStyle(rssMedia.RssBodyProp.TextFont.Italic ? FontStyle.Italic : FontStyle.Normal,
                                                        new TextRange(0, block.content.Length));
                                textLayout.SetFontWeight(rssMedia.RssBodyProp.TextFont.Bold ? FontWeight.Bold : FontWeight.Normal,
                                                         new TextRange(0, block.content.Length));
                                d2dRenderTarget.DrawTextLayout(new Vector2(block.left, block.top), textLayout, textBodyBrush);
                                break;

                            default:
                                break;
                            }
                            textFormat.Dispose();
                            textFormat = null;
                        }
                    }

                    d2dRenderTarget.EndDraw();

                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    fileName = string.Format("{0}{1}.jpg", path, DateTime.Now.Ticks.ToString());
                    var stream  = new WICStream(wicFactory, fileName, NativeFileAccess.Write);
                    var encoder = new PngBitmapEncoder(wicFactory);
                    encoder.Initialize(stream);
                    var bitmapFrameEncode = new BitmapFrameEncode(encoder);
                    bitmapFrameEncode.Initialize();
                    bitmapFrameEncode.SetSize(pageWidth, pageHeight);
                    var pixelFormatGuid = SharpDX.WIC.PixelFormat.FormatDontCare;
                    bitmapFrameEncode.SetPixelFormat(ref pixelFormatGuid);
                    bitmapFrameEncode.WriteSource(wicBitmap);
                    bitmapFrameEncode.Commit();
                    encoder.Commit();
                    bitmapFrameEncode.Dispose();
                    encoder.Dispose();
                    stream.Dispose();

                    Console.WriteLine("*********image count is : " + count++);
                    //发送单个图片生成事件
                    if (SingleGenerateCompleteEvent != null)
                    {
                        SingleGenerateCompleteEvent(fileName);
                    }
                }
                //发送生成完成事件
                if (GenerateCompleteEvent != null)
                {
                    GenerateCompleteEvent(path);
                    //停止线程,从字典删除
                    StopGenerate(rssMedia.CachePath);
                }
            }
            catch (ThreadAbortException aborted)
            {
                Trace.WriteLine("rss 图片生成线程终止 : " + aborted.Message);
            }
            catch (Exception ex)
            {
                Trace.WriteLine("rss 图片生成遇到bug : " + ex.Message);
            }
            finally
            {
                wicFactory.Dispose();
                d2dFactory.Dispose();
                dwFactory.Dispose();
                wicBitmap.Dispose();
                d2dRenderTarget.Dispose();
                solidColorBrush.Dispose();
                textBodyBrush.Dispose();
                titleColorBrush.Dispose();
                publishDateColorBrush.Dispose();
                rssInfo.Dispose();
                rssMedia.Dispose();
                wicFactory            = null;
                d2dFactory            = null;
                dwFactory             = null;
                wicBitmap             = null;
                d2dRenderTarget       = null;
                solidColorBrush       = null;
                textBodyBrush         = null;
                titleColorBrush       = null;
                publishDateColorBrush = null;
                rssInfo  = null;
                rssMedia = null;
                pages.Clear();
                pages = null;
            }
        }
示例#9
0
 protected override void Apply(TextLayout layout, bool enable, int slice)
 {
     var res = layout.SetFontStyle(this.style[slice], new TextRange(ffrom[slice], fto[slice]));
     layout.SetFontWeight(this.w[slice], new TextRange(ffrom[slice], fto[slice]));
     layout.SetFontStretch(this.s[slice], new TextRange(ffrom[slice], fto[slice]));
 }
示例#10
0
 protected override void DoApply(TextLayout layout, TextRange range)
 {
     layout.SetFontStyle(this.Style, range);
 }
示例#11
0
 protected override void DoApply(TextLayout layout, TextRange range)
 {
     layout.SetFontStyle(this.Style, range);
 }