TextJustifyRules.cs
//
// This code is part of Document Solutions for Imaging demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.Drawing;
using System.IO;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Imaging;
namespace GcPdfWeb.Samples.Basics
{
// TBD:
public class TextJustifyRules
{
public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] _ = null)
{
var arialbd = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "arialbd.ttf"));
var arialuni = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "arialuni.ttf"));
var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
using var g = bmp.CreateGraphics(Color.White);
var tl = g.CreateTextLayout();
//tl.LineBreakingRules = LineBreakingRules.Unicode;
//tl.WordBoundaryRules = WordBoundaryRules.Unicode;
//tl.TextExtensionStrategy = TextExtensionStrategy.Default;
tl.TextAlignment = TextAlignment.Distributed;
tl.JustifiedSpaceExtension = 0f;
tl.JustifiedTextExtension = 20f;
var tf = new TextFormat { FontSize = 26f, Font = arialuni };
var tfInfo = new TextFormat { FontSize = 11f, Font = arialbd };
float marginx = 260, marginy = 36;
tl.MaxWidth = pixelSize.Width - marginx * 2;
var text = "abcdefg!1010101010abc;999999本列島で使され99 555";
float DrawText(TextLayout tl, float y)
{
var pt = new PointF(marginx, y + 20);
tl.Append(text, tf);
tl.PerformLayout(true);
var rc = new RectangleF(pt, new SizeF(tl.ContentWidth, tl.ContentHeight));
g.FillRectangle(rc, Color.PaleGoldenrod);
g.DrawString($"LineBreakingRules.{tl.LineBreakingRules}, TextExtensionStrategy.{tl.TextExtensionStrategy}:",
tfInfo, new PointF(marginx / 2f, y));
g.DrawTextLayout(tl, pt);
tl.Clear();
return rc.Bottom + 16;
}
float y = marginy, dy = marginy * 3.5f;
y = DrawText(tl, y);
tl.TextExtensionStrategy = TextExtensionStrategy.EastAsianExcel;
y = DrawText(tl, y);
tl.TextExtensionStrategy = TextExtensionStrategy.Excel;
y = DrawText(tl, y);
tl.LineBreakingRules = LineBreakingRules.Simplified;
tl.WordBoundaryRules = WordBoundaryRules.Simplified;
tl.TextExtensionStrategy = TextExtensionStrategy.Default;
y = DrawText(tl, y);
tl.TextExtensionStrategy = TextExtensionStrategy.EastAsianExcel;
y = DrawText(tl, y);
tl.TextExtensionStrategy = TextExtensionStrategy.Excel;
y = DrawText(tl, y);
return bmp;
}
}
}