Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using SharpDX;
- using SharpDX.Direct2D1;
- using SharpDX.DirectWrite;
- namespace NovelReader.DirectX
- {
- public class TextRenderer : Component
- {
- DeviceManager deviceManager;
- Brush sceneColorBrush;
- TextFormat textFormat;
- RectangleF drawRect;
- ResourceFontLoader customFontLoader;
- FontCollection fontCollection;
- RenderingParams renderingParams;
- public TextFormat TextFormat
- {
- get { return this.textFormat; }
- }
- public TextRenderer(int width, int height)
- {
- this.drawRect = new RectangleF(0, 0, width, height);
- }
- public virtual void Initialize(DeviceManager deviceManager)
- {
- this.deviceManager = deviceManager;
- this.sceneColorBrush = new SolidColorBrush(deviceManager.ContextDirect2D, Color.Black);
- this.customFontLoader = new ResourceFontLoader(deviceManager.FactoryDirectWrite);
- this.fontCollection = new FontCollection(deviceManager.FactoryDirectWrite, this.customFontLoader, this.customFontLoader.Key);
- this.renderingParams = new RenderingParams(deviceManager.FactoryDirectWrite,
- 1.8f, 1.5f, 0.0f, PixelGeometry.Bgr, RenderingMode.CleartypeNatural);
- // IPA Pææ
- //this.textFormat = new TextFormat(deviceManager.FactoryDirectWrite, "IPA Pææ", this.fontCollection, FontWeight.Normal, FontStyle.Normal, FontStretch.Normal, 20);
- this.textFormat = new TextFormat(deviceManager.FactoryDirectWrite, "Arial", 15);
- this.textFormat.TextAlignment = TextAlignment.Center;
- }
- public virtual void Render(TargetBase target)
- {
- DeviceContext context2D = target.DeviceManager.ContextDirect2D;
- context2D.BeginDraw();
- context2D.TextAntialiasMode = TextAntialiasMode.Cleartype;
- context2D.AntialiasMode = AntialiasMode.PerPrimitive;
- context2D.TextRenderingParams = this.renderingParams;
- context2D.Clear(Color.White);
- DrawText(context2D, "This is test string for vertical.", 15, 15, 10, 5);
- context2D.EndDraw();
- }
- void DrawText(DeviceContext context2D, string text, int cWidth, int cHeight, int lineLength, int lineMax)
- {
- RectangleF cRect = new RectangleF(0, 0, cWidth, cHeight);
- int index = 0;
- int line = 0;
- foreach (char c in text)
- {
- if (index > lineLength)
- {
- line++;
- index = 0;
- }
- if (line >= lineMax)
- {
- break;
- }
- cRect.Top = cHeight * index;
- cRect.Left = cWidth * line;
- cRect.Right = cRect.Left + cWidth;
- cRect.Bottom = cRect.Top + cHeight;
- context2D.DrawText(c.ToString(), this.textFormat, cRect, this.sceneColorBrush);
- index++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement