using Microsoft.Maui.Graphics; using Lucy.ViewModels.Editor; namespace Lucy.Views.Editor…
- User
using Microsoft.Maui.Graphics; using Lucy.ViewModels.Editor;
namespace Lucy.Views.Editor { public class PageDrawable : IDrawable { private readonly PageVM _page;
public PageDrawable(PageVM page) { _page = page; } public void Draw(ICanvas canvas, RectF dirtyRect) { var pageRenderer = new PageRenderer(); pageRenderer.RenderPage(canvas, _page); } }}using Lucy.ViewModels.Blocks; using Lucy.ViewModels.Blocks.Base; using System.Collections.Generic; using System; using System.Linq; using Lucy.ViewModels.Internals; using Lucy.Enumerators; using Lucy.ViewModels.Editor; using Microsoft.Maui.Graphics; using Microsoft.Maui.Graphics.Text; // Add this using directive
namespace Lucy.Views.Editor { /// /// Renders a page onto the ICanvas, drawing all layers and blocks. /// public class PageRenderer { public void RenderPage(ICanvas canvas, PageVM page) { DrawPageBackground(canvas, page); canvas.SaveState();
try { SetContentClipping(canvas, page); DrawGridLines(canvas, page); foreach (var layer in page.Layers.OrderBy(l => l.Order)) { foreach (var block in layer.Blocks) { RenderBlock(canvas, block, page); } } } finally { canvas.RestoreState(); } DrawPageNumber(canvas, page); } private void RenderBlock(ICanvas canvas, BlockVM block, PageVM page) { if (block is TextBlockVM textBlock) { RenderTextBlock(canvas, textBlock, page); } // TODO: Handle other block types (e.g., images) // What Hasn't Been Implemented: Rendering logic for non-text blocks. // Next Steps: Implement rendering methods for ImageBlockVM and other custom blocks. } private void RenderTextBlock(ICanvas canvas, TextBlockVM textBlock, PageVM page) { float x = textBlock.X; float y = textBlock.Y; float maxWidth = page.Width - page.MarginLeft - page.MarginRight - (float)(textBlock.LeftIndentation + textBlock.RightIndentation); // Create a TextLayout var textLayout = new Microsoft.Maui.Graphics.Text.TextLayout { MaxWidth = maxWidth, MaxHeight = page.Height - y - page.MarginBottom, LineHeight = (float)textBlock.LineSpacing, HorizontalAlignment = GetHorizontalAlignment(textBlock.Alignment) }; // Build the text with formatting foreach (var run in textBlock.Runs) { if (run == null || string.IsNullOrEmpty(run.Text)) continue; var fontAttributes = GetFontAttributes(run); var font = new Microsoft.Maui.Graphics.Font(run.FontFamily ?? "Arial", (int)run.FontSize, fontAttributes); textLayout.Append(run.Text, font, run.TextColor); } // Background rectangle covers the entire text block height SizeF textSize = textLayout.GetSize(); RectF backgroundRect = new RectF( x, y, x + maxWidth, y + textSize.Height ); // Background color if (textBlock.BackgroundColour != null) { canvas.FillColor = textBlock.BackgroundColour; // Assuming it's already a Microsoft.Maui.Graphics.Color canvas.FillRectangle(backgroundRect); } // Draw red bounding box around the entire text block. canvas.StrokeColor = Colors.Red; canvas.StrokeSize = 2; canvas.DrawRectangle(backgroundRect); // Draw the text layout canvas.SaveState(); canvas.Translate(x, y); textLayout.Draw(canvas, new PointF(0, 0)); canvas.RestoreState(); } private FontStyleType GetFontAttributes(RunVM run) { if (run.Bold && run.Italic) return FontStyleType.Oblique; // Use Oblique as BoldItalic is not available else if (run.Bold) return FontStyleType.Normal; else if (run.Italic) return FontStyleType.Italic; else return FontStyleType.Normal; } private HorizontalAlignment GetHorizontalAlignment(Alignment alignment) { return alignment switch { Alignment.Left => HorizontalAlignment.Left, Alignment.Centre => HorizontalAlignment.Center, Alignment.Right => HorizontalAlignment.Right, _ => HorizontalAlignment.Left, }; } /// <summary> /// Draws the background and border of the page. /// </summary> private void DrawPageBackground(ICanvas canvas, PageVM page) { var pageRect = new RectF(0, 0, page.Width, page.Height); canvas.FillColor = Colors.White; canvas.FillRectangle(pageRect); canvas.StrokeColor = Colors.Black; canvas.StrokeSize = 2; canvas.DrawRectangle(pageRect); } /// <summary> /// Sets the clipping region to the content area within the margins. /// </summary> private void SetContentClipping(ICanvas canvas, PageVM page) { var clipRect = new RectF( page.MarginLeft, page.MarginTop, page.Width - page.MarginLeft - page.MarginRight, page.Height - page.MarginTop - page.MarginBottom ); canvas.ClipRectangle(clipRect); } /// <summary> /// Draws grid lines within the content area for alignment assistance. /// </summary> private void DrawGridLines(ICanvas canvas, PageVM page) { // Grid settings const int DesiredColumns = 10; const int DesiredRows = 10; float contentLeft = page.MarginLeft; float contentRight = page.Width - page.MarginRight; float contentTop = page.MarginTop; float contentBottom = page.Height - page.MarginBottom; float contentWidth = contentRight - contentLeft; float contentHeight = contentBottom - contentTop; float spacingX = contentWidth / DesiredColumns; float spacingY = contentHeight / DesiredRows; canvas.StrokeColor = Colors.LightGray; canvas.StrokeSize = 1; // Draw vertical grid lines for (int i = 0; i <= DesiredColumns; i++) { float x = contentLeft + i * spacingX; canvas.DrawLine(x, contentTop, x, contentBottom); } // Draw horizontal grid lines for (int i = 0; i <= DesiredRows; i++) { float y = contentTop + i * spacingY; canvas.DrawLine(contentLeft, y, contentRight, y); } } /// <summary> /// Draws the page number at the bottom center of the page. /// </summary> private void DrawPageNumber(ICanvas canvas, PageVM page) { var font = new Microsoft.Maui.Graphics.Font("Arial", 14); canvas.Font = font; canvas.FontColor = Colors.Gray; string pageNumberText = $"Page {page.PageNumber}"; float x = page.Width / 2; float y = page.Height - page.MarginBottom / 2; canvas.DrawString(pageNumberText, x, y, HorizontalAlignment.Center); } ///PLAN ///Since we want to do layer- and grid-based content layouts, do the following. ///Each page is repaginated into PageM (page model) and PageVM (page view model) lists in sequential order. ///Each page has a layer stack; layer stacks contain references to block objects, such as TextBlockVM, ImageBlockVM, etc., and these are shown in a nested list under each layer. ///Lists are ordered top to bottom, with left to right being preferred if there are any clashes. ///When adding document objects, such as lines to separate headers from the document's body, these are added as objects such as LineM/LineVM and so on. ///Make it modular to apply document- or page-wide styles for these objects, allowing for LaTeX-styled templating AND Word-styled WYSIWYG editing. ///Every inserted template object has default values and modified values, and both are stored so a user can reset back to defaults. ///Any changes to an object or layout forces a repagination. ///We can then also allow for a toolbar for objects, such as textblocks, images, lines, etc., whilst also allowing for programmatic insertion. ///If an image is on the same layer as text, then the text can dynamically wrap around it; else, it goes in front or behind text, depending on order in the layer stack. ///Enable virtualisation of each PageM/PageVM to allow for performant rendering, only rendering what we need at the time. ///Templates define default values, meanwhile edits define modified values. ///This would also allow for either per-page or per-document margins, depending on need; it would be best to have a margin UI, with drag controls, that have a toggle for either "document margins" or "page margins". ///To allow for caret editing, a return/new paragraph character creates a new textblock object (for user friendliness, an Enter-inserted textblock would use the layout values of the previous block as its modified values). ///Use the "Locked" bool in each blockvm to determine if a block's attributes can be modified, meaning only default values are used, and they can't have their layout or contents changed. /// ///Would the editor and renderer benefit from being MVVM themselves? To allow for these objects? /// ///Also, for performance's sake, pages should repaginate from the end of the document up to the modified block if editing in the document body. Anything that comes before won't be affected by layout unless the paragraph needs to run onto the next page with the edits, surely? } - Assistant