■ 폰트 메트릭을 구하는 방법을 보여준다.
▶ VerticalAlignment.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
namespace TestProject { /// <summary> /// 수직 정렬 /// </summary> public enum VerticalAlignment { /// <summary> /// 상단 /// </summary> Top, /// <summary> /// 중간 /// </summary> Middle, /// <summary> /// 하단 /// </summary> Bottom, } } |
▶ DrawingContextExtensions.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
using System.Globalization; using System.Linq; using System.Windows; using System.Windows.Media; namespace TestProject { /// <summary> /// 그리기 컨텍스트 확장 /// </summary> public static class DrawingContextExtension { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 문자열 그리기 - DrawString(drawingContext, text, fontName, emSize, brush, originPoint, verticalAlignment, horizontalAlignment) /// <summary> /// 문자열 그리기 /// </summary> /// <param name="drawingContext">그리기 컨텍스트</param> /// <param name="text">텍스트</param> /// <param name="fontName">폰트명</param> /// <param name="emSize">EM 크기</param> /// <param name="brush">브러시</param> /// <param name="originPoint">원점 포인트</param> /// <param name="verticalAlignment">수직 정렬</param> /// <param name="horizontalAlignment">수평 정렬</param> public static void DrawString(this DrawingContext drawingContext, string text, string fontName, double emSize, Brush brush, Point originPoint, VerticalAlignment verticalAlignment, TextAlignment horizontalAlignment) { Typeface typeface = new Typeface(fontName); FormattedText formattedText = new FormattedText ( text, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, typeface, emSize, brush ); formattedText.TextAlignment = horizontalAlignment; if(verticalAlignment == VerticalAlignment.Middle) { originPoint.Y -= formattedText.Height / 2; } else if(verticalAlignment == VerticalAlignment.Bottom) { originPoint.Y -= formattedText.Height; } drawingContext.DrawText(formattedText, originPoint); } #endregion #region 문자열 그리기 - DrawString(drawingContext, text, angle, fontName, emSize, brush, originPoint, textAlignment, verticalAlignment, horizontalAlignment) /// <summary> /// 문자열 그리기 /// </summary> /// <param name="drawingContext">그리기 컨텍스트</param> /// <param name="text">텍스트</param> /// <param name="angle">각도</param> /// <param name="fontName">폰트명</param> /// <param name="emSize">EM 크기</param> /// <param name="brush">브러시</param> /// <param name="originPoint">원점 포인트</param> /// <param name="textAlignment">텍스트 정렬</param> /// <param name="verticalAlignment">수직 정렬</param> /// <param name="horizontalAlignment">수평 정렬</param> public static void DrawString(this DrawingContext drawingContext, string text, double angle, string fontName, double emSize, Brush brush, Point originPoint, TextAlignment textAlignment, VerticalAlignment verticalAlignment, TextAlignment horizontalAlignment) { Typeface typeface = new Typeface(fontName); FormattedText formattedText = new FormattedText ( text, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, typeface, emSize, brush ); formattedText.TextAlignment = textAlignment; double width = formattedText.Width - formattedText.OverhangLeading; double height = formattedText.Height; TranslateTransform translateTransform1 = new TranslateTransform(); translateTransform1.Y = -height / 2; if((textAlignment == TextAlignment.Left) || (textAlignment == TextAlignment.Justify)) { translateTransform1.X = -width / 2; } else if(textAlignment == TextAlignment.Right) { translateTransform1.X = width / 2; } else { translateTransform1.X = 0; } RotateTransform rotateTransform = new RotateTransform(angle); Rect rectangle = new Rect(0, 0, width, height); if(textAlignment == TextAlignment.Center) { rectangle.X -= width / 2; } else if(textAlignment == TextAlignment.Right) { rectangle.X -= width; } Rect rotatedRectangle = rotateTransform.TransformBounds(rectangle); TranslateTransform translateTransfor2 = new TranslateTransform(originPoint.X, originPoint.Y); if(horizontalAlignment == TextAlignment.Left) { translateTransfor2.X += rotatedRectangle.Width / 2; } else if(horizontalAlignment == TextAlignment.Right) { translateTransfor2.X -= rotatedRectangle.Width / 2; } if(verticalAlignment == VerticalAlignment.Top) { translateTransfor2.Y += rotatedRectangle.Height / 2; } else if(verticalAlignment == VerticalAlignment.Bottom) { translateTransfor2.Y -= rotatedRectangle.Height / 2; } drawingContext.PushTransform(translateTransfor2); drawingContext.PushTransform(rotateTransform); drawingContext.PushTransform(translateTransform1); drawingContext.DrawText(formattedText, new Point(0, 0)); drawingContext.Pop(); drawingContext.Pop(); drawingContext.Pop(); } #endregion #region 다각형 그리기 - DrawPolygon(drawingContext, brush, pen, pointArray, fillRule) /// <summary> /// 다각형 그리기 /// </summary> /// <param name="drawingContext">그리기 컨텍스트</param> /// <param name="brush">브러시</param> /// <param name="pen">펜</param> /// <param name="pointArray">포인트 배열</param> /// <param name="fillRule">채우기 규칙</param> public static void DrawPolygon(this DrawingContext drawingContext, Brush brush, Pen pen, Point[] pointArray, FillRule fillRule) { drawingContext.DrawPolygonOrPolyline(brush, pen, pointArray, fillRule, true); } #endregion #region 다각선 그리기 - DrawPolyline(drawingContext, brush, pen, pointArray, fillRule) /// <summary> /// 다각선 그리기 /// </summary> /// <param name="drawingContext">그리기 컨텍스트</param> /// <param name="brush">브러시</param> /// <param name="pen">펜</param> /// <param name="pointArray">포인트 배열</param> /// <param name="fillRule">채우기 규칙</param> public static void DrawPolyline(this DrawingContext drawingContext, Brush brush, Pen pen, Point[] pointArray, FillRule fillRule) { drawingContext.DrawPolygonOrPolyline(brush, pen, pointArray, fillRule, false); } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 다각형 또는 다각선 그리기 - DrawPolygonOrPolyline(drawingContext, brush, pen, pointArray, fillRule, drawPolygon) /// <summary> /// 다각형 또는 다각선 그리기 /// </summary> /// <param name="drawingContext">그리기 컨텍스트</param> /// <param name="brush">브러시</param> /// <param name="pen">펜</param> /// <param name="pointArray">포인트 배열</param> /// <param name="fillRule">채우기 규칙</param> /// <param name="drawPolygon">다각형 그리기 여부</param> private static void DrawPolygonOrPolyline(this DrawingContext drawingContext, Brush brush, Pen pen, Point[] pointArray, FillRule fillRule, bool drawPolygon) { StreamGeometry streamGeometry = new StreamGeometry(); streamGeometry.FillRule = fillRule; using(StreamGeometryContext context = streamGeometry.Open()) { context.BeginFigure(pointArray[0], true, drawPolygon); context.PolyLineTo(pointArray.Skip(1).ToArray(), true, false); } drawingContext.DrawGeometry(brush, pen, streamGeometry); } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="폰트 메트릭 구하기" Background="Transparent"> <Grid Name="mainGrid"> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
using System.Windows; using System.Windows.Media; using System.Globalization; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 렌더링시 처리하기 - OnRender(drawingContext) /// <summary> /// 렌더링시 처리하기 /// </summary> /// <param name="drawingContext">그리기 컨텍스트</param> protected override void OnRender(DrawingContext drawingContext) { Rect backgroundRectangle = new Rect(0, 0, ActualWidth, ActualHeight); drawingContext.DrawRectangle(Brushes.White, null, backgroundRectangle); double emSize = 200; FontFamily fontFamily = new FontFamily("Times New Roman"); Typeface typeface = new Typeface("Times New Roman Bold"); FormattedText formattedText = new FormattedText ( "MgfSj", new CultureInfo("en-us"), FlowDirection.LeftToRight, typeface, emSize, Brushes.Black ); Point originPoint = new Point(100, 20); drawingContext.DrawText(formattedText, originPoint); drawingContext.DrawEllipse(Brushes.Yellow, new Pen(Brushes.Black, 1), originPoint, 4, 4); double topY; double baselineY; double capsY; double lowercaseY; double descentY; double bottomY; double originX; double startX; double endX; double rightX; GetTextMetrics ( originPoint.X, originPoint.Y, formattedText, typeface, emSize, out topY, out baselineY, out capsY, out lowercaseY, out descentY, out bottomY, out originX, out startX, out endX, out rightX ); const string FONT_NAME = "Times New Roman Bold"; emSize = 14; Pen horizontalLinePen = new Pen(Brushes.Red, 1); horizontalLinePen.DashStyle = new DashStyle(new double[] { 5, 5 }, 0); double minimumX = 10; double maximumX = this.mainGrid.ActualWidth - 10; double[] yValueArray = { topY, capsY, lowercaseY, baselineY, descentY, bottomY }; string[] yLabelArray = { "y top", "y caps", "y lowercase", "y baseline", "y descent", "" }; const double GAP = 3; for(int i = 0; i < yValueArray.Length; i++) { drawingContext.DrawLine ( horizontalLinePen, new Point(minimumX, yValueArray[i]), new Point(maximumX, yValueArray[i]) ); drawingContext.DrawString ( yLabelArray[i], FONT_NAME, emSize, Brushes.Black, new Point(minimumX, yValueArray[i] - GAP), TestProject.VerticalAlignment.Bottom, TextAlignment.Left ); } DrawText ( drawingContext, typeface, TextAlignment.Left, TestProject.VerticalAlignment.Top, 14, Brushes.Black, "y bottom", minimumX, bottomY ); Pen verticalLinePen = new Pen(Brushes.Green, 1); verticalLinePen.DashStyle = new DashStyle(new double[] { 5, 5 }, 0); double minimumY = 10; double maximumY = mainGrid.ActualHeight - 10; double[] xValueArray = { originX, startX, endX, rightX }; foreach(double xValue in xValueArray) { drawingContext.DrawLine ( verticalLinePen, new Point(xValue, minimumY), new Point(xValue, maximumY) ); } drawingContext.DrawString ( "x origin", -90, FONT_NAME, emSize, Brushes.Black, new Point(originX - GAP, maximumY), TextAlignment.Left, TestProject.VerticalAlignment.Bottom, TextAlignment.Right ); drawingContext.DrawString ( "x start", -90, FONT_NAME, emSize, Brushes.Black, new Point(startX, maximumY), TextAlignment.Left, TestProject.VerticalAlignment.Bottom, TextAlignment.Left ); drawingContext.DrawString ( "x end", -90, FONT_NAME, emSize, Brushes.Black, new Point(endX - GAP, maximumY), TextAlignment.Left, TestProject.VerticalAlignment.Bottom, TextAlignment.Right ); drawingContext.DrawString ( "x right", -90, FONT_NAME, emSize, Brushes.Black, new Point(rightX, maximumY), TextAlignment.Left, TestProject.VerticalAlignment.Bottom, TextAlignment.Left ); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 텍스트 메트릭 구하기 - GetTextMetrics(x, y, formattedText, typeface, emSize, topY, baselineY, capsY, lowerCaseY, descentY, bottomY, originX, startX, endX, rightX) /// <summary> /// 텍스트 메트릭 구하기 /// </summary> /// <param name="x">X 좌표</param> /// <param name="y">Y 좌표</param> /// <param name="formattedText">포맷 텍스트</param> /// <param name="typeface">타입 페이스</param> /// <param name="emSize">EM 크기</param> /// <param name="topY">상단 Y</param> /// <param name="baselineY">베이스 라인 Y</param> /// <param name="capsY">캡스 Y</param> /// <param name="lowerCaseY">소문자 Y</param> /// <param name="descentY">문자 하강 Y</param> /// <param name="bottomY">하단 Y</param> /// <param name="originX">원점 X 좌표</param> /// <param name="startX">시작 X 좌표</param> /// <param name="endX">정려 X 좌표</param> /// <param name="rightX">오른쪽 X 좌표</param> private void GetTextMetrics ( double x, double y, FormattedText formattedText, Typeface typeface, double emSize, out double topY, out double baselineY, out double capsY, out double lowerCaseY, out double descentY, out double bottomY, out double originX, out double startX, out double endX, out double rightX ) { topY = y; bottomY = topY + formattedText.Height; baselineY = topY + formattedText.Baseline; capsY = baselineY - typeface.CapsHeight * emSize; lowerCaseY = baselineY - typeface.XHeight * emSize; descentY = bottomY + formattedText.OverhangAfter; originX = x; startX = originX + formattedText.OverhangLeading; rightX = originX + formattedText.Width; endX = rightX - formattedText.OverhangTrailing; } #endregion #region 텍스트 그리기 - DrawText(drawingContext, typeface, horizontalAlignment, verticalAlignment, emSize, brush, text, x, y) /// <summary> /// 텍스트 그리기 /// </summary> /// <param name="drawingContext">그리기 컨텍스트</param> /// <param name="typeface">타입 페이스</param> /// <param name="horizontalAlignment">수평 정렬</param> /// <param name="verticalAlignment">수직 정렬</param> /// <param name="emSize">EM 크기</param> /// <param name="brush">브러시</param> /// <param name="text">텍스트</param> /// <param name="x">X 좌표</param> /// <param name="y">Y 좌표</param> private void DrawText(DrawingContext drawingContext, Typeface typeface, TextAlignment horizontalAlignment, VerticalAlignment verticalAlignment, double emSize, Brush brush, string text, double x, double y) { FormattedText formattedText = new FormattedText ( text, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, typeface, emSize, brush ); formattedText.TextAlignment = horizontalAlignment; Point originPoint = new Point(x, y); if(verticalAlignment == TestProject.VerticalAlignment.Middle) { originPoint.Y -= formattedText.Height / 2; } else if(verticalAlignment == TestProject.VerticalAlignment.Bottom) { originPoint.Y -= formattedText.Height; } drawingContext.DrawText(formattedText, originPoint); } #endregion } } |