Advertisement
kkc0923

TextRenderer

Nov 25th, 2012
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using SharpDX;
  7. using SharpDX.Direct2D1;
  8. using SharpDX.DirectWrite;
  9.  
  10. namespace NovelReader.DirectX
  11. {
  12.     public class TextRenderer : Component
  13.     {
  14.         DeviceManager deviceManager;
  15.         Brush sceneColorBrush;
  16.         TextFormat textFormat;
  17.         RectangleF drawRect;
  18.         ResourceFontLoader customFontLoader;
  19.         FontCollection fontCollection;
  20.         RenderingParams renderingParams;
  21.  
  22.         public TextFormat TextFormat
  23.         {
  24.             get { return this.textFormat; }
  25.         }
  26.  
  27.         public TextRenderer(int width, int height)
  28.         {
  29.             this.drawRect = new RectangleF(0, 0, width, height);
  30.         }
  31.  
  32.         public virtual void Initialize(DeviceManager deviceManager)
  33.         {
  34.             this.deviceManager = deviceManager;
  35.             this.sceneColorBrush = new SolidColorBrush(deviceManager.ContextDirect2D, Color.Black);
  36.            
  37.             this.customFontLoader = new ResourceFontLoader(deviceManager.FactoryDirectWrite);
  38.             this.fontCollection = new FontCollection(deviceManager.FactoryDirectWrite, this.customFontLoader, this.customFontLoader.Key);
  39.  
  40.             this.renderingParams = new RenderingParams(deviceManager.FactoryDirectWrite,
  41.                 1.8f, 1.5f, 0.0f, PixelGeometry.Bgr, RenderingMode.CleartypeNatural);
  42.  
  43.             // IPA P明朝
  44.             //this.textFormat = new TextFormat(deviceManager.FactoryDirectWrite, "IPA P明朝", this.fontCollection, FontWeight.Normal, FontStyle.Normal, FontStretch.Normal, 20);
  45.             this.textFormat = new TextFormat(deviceManager.FactoryDirectWrite, "Arial", 15);
  46.             this.textFormat.TextAlignment = TextAlignment.Center;
  47.            
  48.         }
  49.  
  50.         public virtual void Render(TargetBase target)
  51.         {
  52.             DeviceContext context2D = target.DeviceManager.ContextDirect2D;
  53.  
  54.             context2D.BeginDraw();
  55.  
  56.             context2D.TextAntialiasMode = TextAntialiasMode.Cleartype;
  57.             context2D.AntialiasMode = AntialiasMode.PerPrimitive;
  58.  
  59.             context2D.TextRenderingParams = this.renderingParams;
  60.            
  61.             context2D.Clear(Color.White);
  62.  
  63.             DrawText(context2D, "This is test string for vertical.", 15, 15, 10, 5);
  64.  
  65.             context2D.EndDraw();
  66.         }
  67.  
  68.         void DrawText(DeviceContext context2D, string text, int cWidth, int cHeight, int lineLength, int lineMax)
  69.         {
  70.             RectangleF cRect = new RectangleF(0, 0, cWidth, cHeight);
  71.             int index = 0;
  72.             int line = 0;
  73.  
  74.             foreach (char c in text)
  75.             {
  76.                 if (index > lineLength)
  77.                 {
  78.                     line++;
  79.                     index = 0;
  80.                 }
  81.  
  82.                 if (line >= lineMax)
  83.                 {
  84.                     break;
  85.                 }
  86.                 cRect.Top = cHeight * index;
  87.                 cRect.Left = cWidth * line;
  88.  
  89.                 cRect.Right = cRect.Left + cWidth;
  90.                 cRect.Bottom = cRect.Top + cHeight;
  91.                
  92.                 context2D.DrawText(c.ToString(), this.textFormat, cRect, this.sceneColorBrush);
  93.  
  94.                 index++;
  95.             }
  96.            
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement