public override void RenderPage(XGraphics gfx) { base.RenderPage(gfx); string facename = "Times"; XFont fontR = new XFont(facename, 40); XFont fontB = new XFont(facename, 40, XFontStyle.Bold); XFont fontI = new XFont(facename, 40, XFontStyle.Italic); XFont fontBI = new XFont(facename, 40, XFontStyle.Bold | XFontStyle.Italic); //gfx.DrawString("Hello", this.properties.Font1.Font, this.properties.Font1.Brush, 200, 200); double x = 80; XPen pen = XPens.SlateBlue; gfx.DrawLine(pen, x, 100, x, 600); gfx.DrawLine(pen, x - 50, 200, 400, 200); gfx.DrawLine(pen, x - 50, 300, 400, 300); gfx.DrawLine(pen, x - 50, 400, 400, 400); gfx.DrawLine(pen, x - 50, 500, 400, 500); double lineSpace = fontR.GetHeight(gfx); int cellSpace = fontR.FontFamily.GetLineSpacing(fontR.Style); int cellAscent = fontR.FontFamily.GetCellAscent(fontR.Style); int cellDescent = fontR.FontFamily.GetCellDescent(fontR.Style); double cyAscent = lineSpace * cellAscent / cellSpace; XFontMetrics metrics = fontR.Metrics; XSize size; gfx.DrawString("Times 40", fontR, this.properties.Font1.Brush, x, 200); size = gfx.MeasureString("Times 40", fontR); gfx.DrawLine(this.properties.Pen3.Pen, x, 200, x + size.Width, 200); gfx.DrawString("Times bold 40", fontB, this.properties.Font1.Brush, x, 300); size = gfx.MeasureString("Times bold 40", fontB); //gfx.DrawLine(this.properties.Pen3.Pen, x, 300, x + size.Width, 300); gfx.DrawString("Times italic 40", fontI, this.properties.Font1.Brush, x, 400); size = gfx.MeasureString("Times italic 40", fontI); //gfx.DrawLine(this.properties.Pen3.Pen, x, 400, x + size.Width, 400); gfx.DrawString("Times bold italic 40", fontBI, this.properties.Font1.Brush, x, 500); size = gfx.MeasureString("Times bold italic 40", fontBI); //gfx.DrawLine(this.properties.Pen3.Pen, x, 500, x + size.Width, 500); #if true___ // Check Malayalam XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always); XFont Kartika = new XFont("Kartika", 20, XFontStyle.Regular, options); XFont AnjaliOldLipi = new XFont("AnjaliOldLipi", 20, XFontStyle.Regular, options); gfx.DrawString("മകനെ ഇത് ഇന്ത്യയുടെ ഭൂപടം", Kartika, this.properties.Font1.Brush, x, 600); gfx.DrawString("മകനെ ഇത് ഇന്ത്യയുടെ ഭൂപടം", AnjaliOldLipi, this.properties.Font1.Brush, x, 650); #endif }
void RenderMeasureText(XGraphics gfx) { gfx.TranslateTransform(15, 20); XFontStyle style = XFontStyle.Regular; XFont font = new XFont("Times New Roman", 95, style); string text = "Hello"; double x = 20, y = 100; XSize size = gfx.MeasureString(text, font); double lineSpace = font.GetHeight(gfx); int cellSpace = font.FontFamily.GetLineSpacing(style); int cellAscent = font.FontFamily.GetCellAscent(style); int cellDescent = font.FontFamily.GetCellDescent(style); int cellLeading = cellSpace - cellAscent - cellDescent; // Get effective ascent double ascent = lineSpace * cellAscent / cellSpace; gfx.DrawRectangle(XBrushes.Bisque, x, y - ascent, size.Width, ascent); // Get effective descent double descent = lineSpace * cellDescent / cellSpace; gfx.DrawRectangle(XBrushes.LightGreen, x, y, size.Width, descent); // Get effective leading double leading = lineSpace * cellLeading / cellSpace; gfx.DrawRectangle(XBrushes.Yellow, x, y + descent, size.Width, leading); // Draw text half transparent XColor color = XColors.DarkSlateBlue; color.A = 0.6; gfx.DrawString(text, font, new XSolidBrush(color), x, y); }
/// <summary> /// Renders the content of the page. /// </summary> public void Render(XGraphics xGraphics) { const double textPossitionX = 50; double textPossitionY = 100; const string fontFamilyName = "Times"; const int fontSizeHeader = 18; const int fontSizeText = 12; XFont fontHeader = new XFont(fontFamilyName, fontSizeHeader, XFontStyle.Bold); XFont fontText = new XFont(fontFamilyName, fontSizeText); XFont fontItalic = new XFont(fontFamilyName, fontSizeText, XFontStyle.BoldItalic); double lineSpacing = fontText.GetHeight(xGraphics); xGraphics.DrawString("This is Header", fontHeader, XBrushes.Black, textPossitionX, textPossitionX); textPossitionY += lineSpacing; xGraphics.DrawString("This is normal.", fontText, XBrushes.Black, textPossitionX, textPossitionY); textPossitionY += lineSpacing; xGraphics.DrawString("This is Italic.", fontItalic, XBrushes.Black, textPossitionX, textPossitionY); textPossitionY += lineSpacing*3; //Bitmap bitmap = (Bitmap)Image.FromFile(@"E:\temp\klicaj.jpeg"); XImage xImage = null; if (item != null) { if (item.Image != null) { MemoryStream memoryStream = new MemoryStream(item.Image); Bitmap bitmap = (Bitmap) Image.FromStream(memoryStream, true, true); xImage = XImage.FromGdiPlusImage(bitmap); } } if (xImage != null) { XRect rcImage = new XRect(100, textPossitionY, 100, 100*Math.Sqrt(2)); xGraphics.DrawImage(xImage, rcImage); } XGraphicsState state = xGraphics.Save(); xGraphics.Restore(state); }
public static void SaveTest() { XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always); initialize(); PdfPage page = _document.AddPage(); XGraphics X = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend); page.Size = PageSize.A4; double width = page.Width; double height = page.Height; double top = _margins.Top; double bottom = _margins.Bottom; double left = _margins.Left; double right = _margins.Right*2; double y = top; double x = left; XFont titleFont = new XFont("Calibri", 14F, XFontStyle.Bold,options); XFont fontW = new XFont("Calibri", 100F, XFontStyle.Regular, options); XFont fontQ = new XFont("Calibri", 12F, XFontStyle.Regular,options); XFont fontA = new XFont("Calibri", 11F, XFontStyle.Regular, options); var img = new Bitmap(1, 1); XGraphics E = XGraphics.FromGraphics(Graphics.FromImage(img), new XSize()); X.DrawStringML(_title, titleFont, XBrushes.Black, x, ref y, width - right); y += titleFont.GetHeight() + 20; X.DrawString("Date: " + _date, fontQ, Brushes.Black, width - right - 40, y); X.DrawString("First Name: ___________________", fontQ, Brushes.Black, x, y); y += fontQ.GetHeight() + 5; X.DrawString("Last Name: ___________________", fontQ, Brushes.Black, x, y); y += fontQ.GetHeight() + 5; X.DrawString("Class: ________", fontQ, Brushes.Black, x, y); y += fontQ.GetHeight() + 5; X.DrawString("Points:________", fontQ, Brushes.Black, x, y); y += fontQ.GetHeight() + 35; X.DrawString("Choose the correct answer. There might be more than one correct answers.", fontQ, Brushes.Black, x, y); y += fontQ.GetHeight() + 20; for (int i = 0; i < _test.Count; i++) { string question = (i + 1) + ". " + _test[i].question; //Ipologismos gia allagi selidas double tempY = y; E.DrawStringML(question, fontQ, Brushes.Black, x, ref tempY, width - right); if (tempY > height - bottom) { watermarkprint(X, page, fontW); page = _document.AddPage(); page.Size = PageSize.A4; X = XGraphics.FromPdfPage(page); y = top; } //Prints Questions X.DrawStringML(question, fontQ, XBrushes.Black, x, ref y, width - right); for (int k = 0; k < _test[i].anwsers.Count; k++) { string answer = _test[i].anwsers[k].text; //ipologismos gia allagi selida tempY = y; E.DrawStringML(answer, fontQ, Brushes.Black, x, ref tempY, width - right); if (tempY > height-bottom) { watermarkprint(X, page, fontW); page = _document.AddPage(); page.Size = PageSize.A4; X = XGraphics.FromPdfPage(page); y = top; } y += 3; //Edw tipwnei apantisi X.DrawRectangle(new Pen(Color.Black), x + 15, y, 20, 20); if (_solved && _test[i].anwsers[k].correct) { var cube = new Bitmap(20, 20); Graphics C = Graphics.FromImage(cube); C.FillRectangle(Brushes.Black, 0, 0, 20, 20); X.DrawImage(cube, x+15, y,20,20); } y += 12; X.DrawStringML(answer, fontA, Brushes.Black, x + 45, ref y, width - right); } y += 30; } watermarkprint(X,page,fontW); _document.Save(_savePath); Process.Start(_savePath); }
private static void DrawStringML(this XGraphics G, string Text, XFont font, XBrush brush, double x, ref double y, double mX) { string[] words = Text.Split(' '); double tempX = x; double totalSpace = mX - x; double measureWord = font.GetHeight(G); double tempWordWidth = 0; foreach (string word in words) { tempWordWidth = G.MeasureString(word, font).Width; measureWord += tempWordWidth; if (measureWord > totalSpace) { y += font.GetHeight(G); tempX = x; measureWord = tempWordWidth; } G.DrawString(word, font, brush, tempX, y); tempX += tempWordWidth+4; } y += font.GetHeight(G); }
/// <summary> /// Renders the content of the page. /// </summary> public void Render(XGraphics gfx) { XRect rect; XPen pen; double x = 50, y = 100; XFont fontH1 = new XFont("Times", 18, XFontStyle.Bold); XFont font = new XFont("Times", 12); XFont fontItalic = new XFont("Times", 12, XFontStyle.BoldItalic); double ls = font.GetHeight(gfx); // Draw some text gfx.DrawString("Create PDF on the fly with PDFsharp", fontH1, XBrushes.Black, x, x); gfx.DrawString("With PDFsharp you can use the same code to draw graphic, " + "text and images on different targets.", font, XBrushes.Black, x, y); y += ls; gfx.DrawString("The object used for drawing is the XGraphics object.", font, XBrushes.Black, x, y); y += 2 * ls; // Draw an arc pen = new XPen(XColors.Red, 4); pen.DashStyle = XDashStyle.Dash; gfx.DrawArc(pen, x + 20, y, 100, 60, 150, 120); // Draw a star XGraphicsState gs = gfx.Save(); gfx.TranslateTransform(x + 140, y + 30); for (int idx = 0; idx < 360; idx += 10) { gfx.RotateTransform(10); gfx.DrawLine(XPens.DarkGreen, 0, 0, 30, 0); } gfx.Restore(gs); // Draw a rounded rectangle rect = new XRect(x + 230, y, 100, 60); pen = new XPen(XColors.DarkBlue, 2.5); XColor color1 = XColor.FromKnownColor(KnownColor.DarkBlue); XColor color2 = XColors.Red; XLinearGradientBrush lbrush = new XLinearGradientBrush(rect, color1, color2, XLinearGradientMode.Vertical); gfx.DrawRoundedRectangle(pen, lbrush, rect, new XSize(10, 10)); // Draw a pie pen = new XPen(XColors.DarkOrange, 1.5); pen.DashStyle = XDashStyle.Dot; gfx.DrawPie(pen, XBrushes.Blue, x + 360, y, 100, 60, -130, 135); // Draw some more text y += 60 + 2 * ls; gfx.DrawString("With XGraphics you can draw on a PDF page as well as " + "on any System.Drawing.Graphics object.", font, XBrushes.Black, x, y); y += ls * 1.1; gfx.DrawString("Use the same code to", font, XBrushes.Black, x, y); x += 10; y += ls * 1.1; gfx.DrawString("• draw on a newly created PDF page", font, XBrushes.Black, x, y); y += ls; gfx.DrawString("• draw above or beneath of the content of an existing PDF page", font, XBrushes.Black, x, y); y += ls; gfx.DrawString("• draw in a window", font, XBrushes.Black, x, y); y += ls; gfx.DrawString("• draw on a printer", font, XBrushes.Black, x, y); y += ls; gfx.DrawString("• draw in a bitmap image", font, XBrushes.Black, x, y); x -= 10; y += ls * 1.1; gfx.DrawString("You can also import an existing PDF page and use it like " + "an image, e.g. draw it on another PDF page.", font, XBrushes.Black, x, y); y += ls * 1.1 * 2; gfx.DrawString("Imported PDF pages are neither drawn nor printed; create a " + "PDF file to see or print them!", fontItalic, XBrushes.Firebrick, x, y); y += ls * 1.1; gfx.DrawString("Below this text is a PDF form that will be visible when " + "viewed or printed with a PDF viewer.", fontItalic, XBrushes.Firebrick, x, y); y += ls * 1.1; XGraphicsState state = gfx.Save(); XRect rcImage = new XRect(100, y, 100, 100 * Math.Sqrt(2)); gfx.DrawRectangle(XBrushes.Snow, rcImage); gfx.DrawImage(XPdfForm.FromFile("../../../../../PDFs/SomeLayout.pdf"), rcImage); gfx.Restore(state); }
private static void CreateReceiveInventoryStagingReport(string path, int totalproduct) { XFont fontH1 = new XFont("Arial", 18, XFontStyle.Bold); XFont fontH2 = new XFont("Arial", 16, XFontStyle.Italic); XFont font = new XFont("Arial", 12); using (PdfDocument document = new PdfDocument()) { document.Info.Title = "Fox One POS Stage Received Inventory Report"; PdfPage page = document.AddPage(); double x = 50; double y = 100; using (XGraphics gfx = XGraphics.FromPdfPage(page)) { double ls = font.GetHeight(gfx); double lsH1 = fontH1.GetHeight(gfx); double lsH2 = fontH2.GetHeight(gfx); using (XImage img = XImage.FromFile(Configuration.LogoFile)) { double width = img.PixelWidth * 72 / img.HorizontalResolution; double height = img.PixelHeight * 72 / img.HorizontalResolution; gfx.DrawImage(img, x, y, width, height); y += height; } gfx.DrawString(Configuration.Current.BusinessName + " Stage Received Inventory Report", fontH1, XBrushes.Black, x, y); y += lsH1; gfx.DrawString("Station: " + Configuration.Current.StationID, fontH2, XBrushes.Black, x, y); y += lsH2; gfx.DrawString(DateTime.Now.ToString(), fontH2, XBrushes.Black, x, y); y += lsH2 * 2; gfx.DrawString("Total product received: " + totalproduct.ToString(), font, XBrushes.Black, x, y); } document.Save(path); } }
VerticalLineInfo CalcVerticalInfo(XFont font) { ParagraphFormat paragraphFormat = this.paragraph.Format; LineSpacingRule spacingRule = paragraphFormat.LineSpacingRule; XUnit lineHeight = 0; XUnit descent = FontHandler.GetDescent(font); descent = Math.Max(this.currentVerticalInfo.descent, descent); XUnit singleLineSpace = font.GetHeight(); RenderInfo imageRenderInfo = this.CurrentImageRenderInfo; if (imageRenderInfo != null) singleLineSpace = singleLineSpace - FontHandler.GetAscent(font) + imageRenderInfo.LayoutInfo.ContentArea.Height; XUnit inherentLineSpace = Math.Max(this.currentVerticalInfo.inherentlineSpace, singleLineSpace); switch (spacingRule) { case LineSpacingRule.Single: lineHeight = singleLineSpace; break; case LineSpacingRule.OnePtFive: lineHeight = 1.5 * singleLineSpace; break; case LineSpacingRule.Double: lineHeight = 2.0 * singleLineSpace; break; case LineSpacingRule.Multiple: lineHeight = this.paragraph.Format.LineSpacing * singleLineSpace; break; case LineSpacingRule.AtLeast: lineHeight = Math.Max(singleLineSpace, paragraph.Format.LineSpacing); break; case LineSpacingRule.Exactly: lineHeight = new XUnit(paragraph.Format.LineSpacing); inherentLineSpace = paragraph.Format.LineSpacing.Point; break; } lineHeight = Math.Max(this.currentVerticalInfo.height, lineHeight); if (this.MaxElementHeight > 0) lineHeight = Math.Min(this.MaxElementHeight - Renderer.Tolerance, lineHeight); return new VerticalLineInfo(lineHeight, descent, inherentLineSpace); }
/// <summary> /// Calculates the offset for BaseLine positioning simulation: /// In GDI we have only Near, Center and Far as LineAlignment and no BaseLine. For XLineAlignment.BaseLine StringAlignment.Near is returned. /// We now return the negative drawed ascender height. /// This has to be added to the LayoutRect/Origin before each _gdipPath.AddString(). /// </summary> /// <param name="family"></param> /// <param name="style"></param> /// <param name="emSize"></param> /// <param name="format"></param> /// <returns></returns> private float SimulateBaselineOffset(XFontFamily family, XFontStyle style, double emSize, XStringFormat format) { XFont font = new XFont(family.Name, emSize, style); if (format.LineAlignment == XLineAlignment.BaseLine) { double lineSpace = font.GetHeight(); int cellSpace = font.FontFamily.GetLineSpacing(font.Style); int cellAscent = font.FontFamily.GetCellAscent(font.Style); int cellDescent = font.FontFamily.GetCellDescent(font.Style); double cyAscent = lineSpace * cellAscent / cellSpace; cyAscent = lineSpace * font.CellAscent / font.CellSpace; return (float)-cyAscent; } return 0; }
/// <summary> /// Adds a text string to this path. /// </summary> public void AddString(string s, XFontFamily family, XFontStyle style, double emSize, XPoint origin, XStringFormat format) { try { #if CORE DiagnosticsHelper.HandleNotImplemented("XGraphicsPath.AddString"); #endif #if GDI if (family.GdiFamily == null) throw new NotFiniteNumberException(PSSR.NotImplementedForFontsRetrievedWithFontResolver(family.Name)); PointF p = origin.ToPointF(); p.Y += SimulateBaselineOffset(family, style, emSize, format); try { Lock.EnterGdiPlus(); _gdipPath.AddString(s, family.GdiFamily, (int)style, (float)emSize, p, format.RealizeGdiStringFormat()); } finally { Lock.ExitGdiPlus(); } #endif #if WPF if (family.WpfFamily == null) throw new NotFiniteNumberException(PSSR.NotImplementedForFontsRetrievedWithFontResolver(family.Name)); #if !SILVERLIGHT XFont font = new XFont(family.Name, emSize, style); double x = origin.X; double y = origin.Y; double lineSpace = font.GetHeight(); double cyAscent = lineSpace * font.CellAscent / font.CellSpace; double cyDescent = lineSpace * font.CellDescent / font.CellSpace; Typeface typeface = FontHelper.CreateTypeface(family.WpfFamily, style); FormattedText formattedText = FontHelper.CreateFormattedText(s, typeface, emSize, WpfBrushes.Black); switch (format.Alignment) { case XStringAlignment.Near: // nothing to do, this is the default //formattedText.TextAlignment = TextAlignment.Left; break; case XStringAlignment.Center: formattedText.TextAlignment = TextAlignment.Center; break; case XStringAlignment.Far: formattedText.TextAlignment = TextAlignment.Right; break; } switch (format.LineAlignment) { case XLineAlignment.Near: //y += cyAscent; break; case XLineAlignment.Center: // TODO use CapHeight. PDFlib also uses 3/4 of ascent y += -lineSpace / 2; //-formattedText.Baseline + (cyAscent * 2 / 4); break; case XLineAlignment.Far: y += -formattedText.Baseline - cyDescent; break; case XLineAlignment.BaseLine: y -= formattedText.Baseline; break; } Geometry geo = formattedText.BuildGeometry(new XPoint(x, y)); _pathGeometry.AddGeometry(geo); #else // AG-HACK throw new InvalidOperationException("Silverlight cannot create geometry of glyphs."); // TODO: Get the outline directly from the font. #endif #endif } catch { throw; } }
public override void RenderPage(XGraphics gfx) { base.RenderPage(gfx); string facename = "Times"; float size = 24; XFont fontR = new XFont(facename, size, this.properties.Font1.Style); fontR = this.properties.Font1.Font; //XFont fontB = new XFont(facename, size, XFontStyle.Bold); //XFont fontI = new XFont(facename, size, XFontStyle.Italic); //XFont fontBI = new XFont(facename, size, XFontStyle.Bold | XFontStyle.Italic); //gfx.DrawString("Hello", this.properties.Font1.Font, this.properties.Font1.Brush, 200, 200); float x0 = 80; float x1 = 520; float y0 = 80; float y1 = 760 / 2; RectangleF rect = new RectangleF(x0, y0, x1 - x0, y1 - y0); XPen pen = XPens.SlateBlue; gfx.DrawRectangle(pen, rect); gfx.DrawLine(pen, (x0 + x1) / 2, y0, (x0 + x1) / 2, y1); gfx.DrawLine(pen, x0, (y0 + y1) / 2, x1, (y0 + y1) / 2); XSolidBrush brush = this.properties.Font1.Brush; XStringFormat format = new XStringFormat(); double lineSpace = fontR.GetHeight(gfx); int cellSpace = fontR.FontFamily.GetLineSpacing(fontR.Style); int cellAscent = fontR.FontFamily.GetCellAscent(fontR.Style); int cellDescent = fontR.FontFamily.GetCellDescent(fontR.Style); double cyAscent = lineSpace * cellAscent / cellSpace; gfx.DrawString("TopLeft", fontR, brush, rect, format); format.Alignment = XStringAlignment.Center; gfx.DrawString("TopCenter", fontR, brush, rect, format); format.Alignment = XStringAlignment.Far; gfx.DrawString("TopRight", fontR, brush, rect, format); format.LineAlignment= XLineAlignment.Center; format.Alignment = XStringAlignment.Near; gfx.DrawString("CenterLeft", fontR, brush, rect, format); format.Alignment = XStringAlignment.Center; gfx.DrawString("Center", fontR, brush, rect, format); format.Alignment = XStringAlignment.Far; gfx.DrawString("CenterRight", fontR, brush, rect, format); format.LineAlignment= XLineAlignment.Far; format.Alignment = XStringAlignment.Near; gfx.DrawString("BottomLeft", fontR, brush, rect, format); format.Alignment = XStringAlignment.Center; gfx.DrawString("BottomCenter", fontR, brush, rect, format); format.Alignment = XStringAlignment.Far; gfx.DrawString("BottomRight", fontR, brush, rect, format); // format.LineAlignment= XLineAlignment.Center; // format.Alignment = XStringAlignment.Center; // gfx.DrawString("CenterLeft", fontR, brush, rect, format); // gfx.DrawString("Times 40", fontR, this.properties.Font1.Brush, x, 200); // gfx.DrawString("Times bold 40", fontB, this.properties.Font1.Brush, x, 300); // gfx.DrawString("Times italic 40", fontI, this.properties.Font1.Brush, x, 400); // gfx.DrawString("Times bold italic 40", fontBI, this.properties.Font1.Brush, x, 500); }
public void Render(XGraphics gfx) { XPen pen; double x = 50; XFont fontH1 = new XFont("Times", 18, XFontStyle.Bold); XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always); XFont font = new XFont("Times", 12, XFontStyle.Regular, options); XFont fontItalic = new XFont("Times", 12, XFontStyle.BoldItalic); double ls = font.GetHeight(gfx); pen = new XPen(XColors.Black, 0.5); pen.DashStyle = XDashStyle.Solid; double tablePositionX = 15, tablePositionY = 30, tablePositionX2 = gfx.PageSize.Width - tablePositionX, tablePositionY2 = tablePositionY + 30; // draw plan title string title = LoadedPlan.Name + " " + LoadedPlan.Departament.Name + " " + LoadedPlan.Faculty.Name + " " + LoadedPlan.StudiesType.Name; System.Drawing.Font ff = new Font("Times", 12); Size titleSize = System.Windows.Forms.TextRenderer.MeasureText(title, ff); gfx.DrawString(title, font, XBrushes.Black, x, 20); double fullLength = 0; string legend = "Kolorem czerwonym oznaczono przedmioty obieralne, kolorem czarnym - przedmioty obowiązkowe"; Size legendSize = System.Windows.Forms.TextRenderer.MeasureText(legend, ff); if (LoadedPlan.SubjectsDatas.Count > 0) { // draw table headers gfx.DrawLine(pen, tablePositionX, tablePositionY, tablePositionX, tablePositionY2); gfx.DrawLine(pen, tablePositionX + 30, tablePositionY, tablePositionX + 30, tablePositionY2); tablePositionY += 11; gfx.DrawString("Lp.", font, XBrushes.Black, tablePositionX + 2, tablePositionY); gfx.DrawString("Przedmiot", font, XBrushes.Black, tablePositionX + 32, tablePositionY); Size nameLength = System.Windows.Forms.TextRenderer.MeasureText("Przedmiot", ff); List<Semester> semesters = new List<Semester>(); List<SubjectTypesData> subjectTypes = new List<SubjectTypesData>(); Size semNameLength = new Size(0, 0); foreach (SubjectsData sd in LoadedPlan.SubjectsDatas) { bool semOnList = false; foreach (Semester s in semesters) { if (s.SemesterID == sd.SemesterID) semOnList = true; } foreach (SubjectTypesData st in sd.SubjectTypesDatas) { bool stOnList = false; foreach (SubjectTypesData std in subjectTypes) if (st.SubjectTypeID == std.SubjectTypeID) stOnList = true; if (!stOnList) subjectTypes.Add(st); } if (!semOnList) { semesters.Add(sd.Semester); Size tmp = System.Windows.Forms.TextRenderer.MeasureText(sd.Semester.Name, ff); if (tmp.Width > semNameLength.Width) semNameLength = tmp; } } semesters = semesters.OrderBy(c => c.Semester1).ToList(); double oldX = tablePositionX; LoadedPlan.SubjectsDatas.OrderBy(c => c.Semester.Semester1); List<string> subjectNames = new List<string>(); foreach (SubjectsData sd in LoadedPlan.SubjectsDatas) { bool isOnList = false; foreach (string name in subjectNames) if (name.Equals(sd.Subject.Name)) isOnList = true; if (!isOnList) { Size tmp = System.Windows.Forms.TextRenderer.MeasureText(sd.Subject.Name, ff); subjectNames.Add(sd.Subject.Name); if (tmp.Width > nameLength.Width) nameLength.Width = tmp.Width; } } // -32 tablePositionX += nameLength.Width + 34; tablePositionY -= 11; gfx.DrawLine(pen, tablePositionX, tablePositionY, tablePositionX, tablePositionY2); double semLength = 15 * (subjectTypes.Count + 1); double diff = 0; if (semLength + 40 < semNameLength.Width) diff = semNameLength.Width - semLength - 40; else if (semLength >= semNameLength.Width) diff = 40; else diff = 40; semLength += diff; foreach (Semester s in semesters) { gfx.DrawString(s.Name, font, XBrushes.Black, tablePositionX + 2, tablePositionY + 11); foreach (SubjectTypesData st in subjectTypes) { gfx.DrawString(st.SubjectType.Name.Substring(0, 1), font, XBrushes.Black, tablePositionX + 2, tablePositionY2 - 2); tablePositionX += 15; gfx.DrawLine(pen, tablePositionX, tablePositionY2 - 15, tablePositionX, tablePositionY2); } gfx.DrawString("E", font, XBrushes.Black, tablePositionX + 2, tablePositionY2 - 2); tablePositionX += 15; gfx.DrawLine(pen, tablePositionX, tablePositionY2 - 15, tablePositionX, tablePositionY2); gfx.DrawString("ECTS", font, XBrushes.Black, tablePositionX + 2, tablePositionY2 - 2); tablePositionX += diff; gfx.DrawLine(pen, tablePositionX, tablePositionY, tablePositionX, tablePositionY2); //if (tempX > 0) // tablePositionX = tempX + 15; } tablePositionX = oldX; double oldY = tablePositionY2; double oldY2 = tablePositionY2 + 15; int lp = 1; fullLength = nameLength.Width + (semLength * semesters.Count) + 49; gfx.DrawLine(pen, tablePositionX, 30, fullLength, 30); gfx.DrawLine(pen, tablePositionX, tablePositionY2, fullLength, tablePositionY2); gfx.DrawLine(pen, nameLength.Width + 49, tablePositionY2 - 15, fullLength, tablePositionY2 - 15); nameLength.Width += 47; int nonSpecCount = 0; foreach (string name in subjectNames) { foreach (SubjectsData sd in LoadedPlan.SubjectsDatas) { if (sd.Subject.Name.Equals(name) && sd.SpecializationsData == null) { nonSpecCount++; int offset = 0; tablePositionY = tablePositionY2; tablePositionY2 += 15; gfx.DrawLine(pen, tablePositionX, tablePositionY, tablePositionX, tablePositionY2); offset += 2; gfx.DrawString(lp.ToString(), font, XBrushes.Black, tablePositionX + offset, tablePositionY + 11); gfx.DrawLine(pen, tablePositionX, tablePositionY2, fullLength, tablePositionY2); offset += 28; gfx.DrawLine(pen, tablePositionX + 30, tablePositionY, tablePositionX + offset, tablePositionY2); lp++; offset += 2; if (sd.IsElective) gfx.DrawString(name, font, XBrushes.Red, tablePositionX + offset, tablePositionY + 11); else gfx.DrawString(name, font, XBrushes.Black, tablePositionX + offset, tablePositionY + 11); gfx.DrawLine(pen, nameLength.Width + 2, tablePositionY, nameLength.Width + 2, tablePositionY2); int no = 0; foreach (Semester s in semesters) { if (s.Name.Equals(sd.Semester.Name)) { foreach (SubjectTypesData std in sd.SubjectTypesDatas) { int stno = 0; foreach (SubjectTypesData std2 in subjectTypes) { if (std.SubjectType.Name.Equals(std2.SubjectType.Name)) gfx.DrawString(std.Hours.ToString(), font, XBrushes.Black, (nameLength.Width + 4) + (no * semLength) + (15 * stno), tablePositionY + 11); stno++; } } if (sd.IsExam) // -53 gfx.DrawString("x", font, XBrushes.Black, (nameLength.Width + 4) + ((no + 1) * (semLength) - 13 - diff), tablePositionY + 11); // -38 gfx.DrawString(sd.Ects.ToString(), font, XBrushes.Black, (nameLength.Width + 4) + ((no + 1) * (semLength) + 2 - diff), tablePositionY + 11); } no++; } } } } // draw grid tablePositionX = oldX + 32 + nameLength.Width; for (int i = 0; i < nonSpecCount; i++) { for (int j = 0; j < semesters.Count; j++) { for (int k = 0; k < subjectTypes.Count; k++) { gfx.DrawLine(pen, (nameLength.Width + 15) + 2 + (j * semLength) + (15 * k), oldY, (nameLength.Width + 15) + 2 + (j * semLength) + (15 * k), oldY2); } gfx.DrawLine(pen, (nameLength.Width + 2) + ((j + 1) * (semLength) - diff), oldY, (nameLength.Width + 2) + ((j + 1) * (semLength) - diff), oldY2); gfx.DrawLine(pen, (nameLength.Width + 2) + ((j + 1) * semLength), oldY, (nameLength.Width + 2) + ((j + 1) * semLength), oldY2); } oldY = oldY2; oldY2 += 15; } List<string> specializations = new List<string>(); foreach (SubjectsData sd in LoadedPlan.SubjectsDatas) { if (sd.SpecializationsData != null) { bool wasOnList = false; foreach (string spec in specializations) if (spec.Equals(sd.SpecializationsData.Specialization.Name)) wasOnList = true; if (!wasOnList) specializations.Add(sd.SpecializationsData.Specialization.Name); } } oldY2 += 30; foreach (string specialization in specializations) { lp = 1; tablePositionY += 45; tablePositionY2 = tablePositionY; oldY = tablePositionY; oldY2 = tablePositionY2; tablePositionX = 15; gfx.DrawString(specialization, font, XBrushes.Black, 15, tablePositionY-5); gfx.DrawLine(pen, tablePositionX, tablePositionY, fullLength, tablePositionY); int specCount = 1; foreach (string name in subjectNames) { foreach (SubjectsData sd in LoadedPlan.SubjectsDatas) { if (sd.Subject.Name.Equals(name) && sd.SpecializationsData != null && sd.SpecializationsData.Specialization.Name.Equals(specialization)) { specCount++; int offset = 0; tablePositionY = tablePositionY2; tablePositionY2 += 15; gfx.DrawLine(pen, tablePositionX, tablePositionY, tablePositionX, tablePositionY2); offset += 2; gfx.DrawString(lp.ToString(), font, XBrushes.Black, tablePositionX + offset, tablePositionY + 11); gfx.DrawLine(pen, tablePositionX, tablePositionY2, fullLength, tablePositionY2); offset += 28; gfx.DrawLine(pen, tablePositionX + 30, tablePositionY, tablePositionX + offset, tablePositionY2); lp++; offset += 2; if (sd.SpecializationsData.IsElective) gfx.DrawString(name, font, XBrushes.Red, tablePositionX + offset, tablePositionY + 11); else gfx.DrawString(name, font, XBrushes.Black, tablePositionX + offset, tablePositionY + 11); gfx.DrawLine(pen, nameLength.Width + 2, tablePositionY, nameLength.Width + 2, tablePositionY2); int no = 0; foreach (Semester s in semesters) { if (s.Name.Equals(sd.Semester.Name)) { foreach (SubjectTypesData std in sd.SubjectTypesDatas) { int stno = 0; foreach (SubjectTypesData std2 in subjectTypes) { if (std.SubjectType.Name.Equals(std2.SubjectType.Name)) gfx.DrawString(std.Hours.ToString(), font, XBrushes.Black, (nameLength.Width + 4) + (no * semLength) + (15 * stno), tablePositionY + 11); stno++; } } if (sd.IsExam) // -53 gfx.DrawString("x", font, XBrushes.Black, (nameLength.Width + 4) + ((no + 1) * (semLength) - 13 - diff), tablePositionY + 11); // -38 gfx.DrawString(sd.Ects.ToString(), font, XBrushes.Black, (nameLength.Width + 4) + ((no + 1) * (semLength) + 2 - diff), tablePositionY + 11); } no++; } } } } tablePositionX = oldX + 32 + nameLength.Width; for (int i = 0; i < specCount; i++) { for (int j = 0; j < semesters.Count; j++) { for (int k = 0; k < subjectTypes.Count; k++) gfx.DrawLine(pen, (nameLength.Width + 15) + 2 + (j * semLength) + (15 * k), oldY, (nameLength.Width + 15) + 2 + (j * semLength) + (15 * k), oldY2); gfx.DrawLine(pen, (nameLength.Width + 2) + ((j + 1) * (semLength) - diff), oldY, (nameLength.Width + 2) + ((j + 1) * (semLength) - diff), oldY2); gfx.DrawLine(pen, (nameLength.Width + 2) + ((j + 1) * semLength), oldY, (nameLength.Width + 2) + ((j + 1) * semLength), oldY2); } oldY = oldY2; oldY2 += 15; } } // draw legend tablePositionX = 15; tablePositionY += 50; foreach (SubjectTypesData std in subjectTypes) { gfx.DrawString(std.SubjectType.Name.Substring(0, 1) + " - " + std.SubjectType.Name, font, XBrushes.Black, tablePositionX, tablePositionY); tablePositionY += 15; } gfx.DrawString("E - Egzamin", font, XBrushes.Black, tablePositionX, tablePositionY); gfx.DrawString(legend, font, XBrushes.Black, tablePositionX, tablePositionY+15); } if (titleSize.Width > fullLength) if (titleSize.Width > legendSize.Width) this.Width = titleSize.Width + 30; else this.Width = legendSize.Width + 30; else if (fullLength> legendSize.Width) this.Width = fullLength + 30; else this.Width = legendSize.Width + 30; }
/// <summary> /// Draws the specified text string. /// </summary> public void DrawString(string s, XFont font, XBrush brush, XRect layoutRectangle, XStringFormat format) { if (s == null) throw new ArgumentNullException("s"); if (font == null) throw new ArgumentNullException("font"); if (brush == null) throw new ArgumentNullException("brush"); if (format.LineAlignment == XLineAlignment.BaseLine && layoutRectangle.Height != 0) throw new InvalidOperationException("DrawString: With XLineAlignment.BaseLine the height of the layout rectangle must be 0."); if (s.Length == 0) return; if (format == null) format = XStringFormat.Default; if (this.drawGraphics) { RectangleF rect = layoutRectangle.ToRectangleF(); if (format.LineAlignment == XLineAlignment.BaseLine) { // TODO optimze double lineSpace = font.GetHeight(this); int cellSpace = font.FontFamily.GetLineSpacing(font.Style); int cellAscent = font.FontFamily.GetCellAscent(font.Style); int cellDescent = font.FontFamily.GetCellDescent(font.Style); double cyAscent = lineSpace * cellAscent / cellSpace; cyAscent = lineSpace * font.cellAscent / font.cellSpace; rect.Offset(0, (float)-cyAscent); } this.gfx.DrawString(s, font.RealizeGdiFont(), brush.RealizeGdiBrush(), rect, format != null ? format.RealizeGdiStringFormat() : null); } if (this.renderer != null) this.renderer.DrawString(s, font, brush, layoutRectangle, format); }
/// <summary> /// Adds a text string to this path. /// </summary> public void AddString(string s, XFontFamily family, XFontStyle style, double emSize, XRect layoutRect, XStringFormat format) { if (s == null) throw new ArgumentNullException("s"); if (family == null) throw new ArgumentNullException("family"); if (format.LineAlignment == XLineAlignment.BaseLine && layoutRect.Height != 0) throw new InvalidOperationException("DrawString: With XLineAlignment.BaseLine the height of the layout rectangle must be 0."); if (s.Length == 0) return; if (format == null) format = XStringFormats.Default; XFont font = new XFont(family.Name, emSize, style); #if GDI && !WPF RectangleF rc = layoutRect.ToRectangleF(); if (format.LineAlignment == XLineAlignment.BaseLine) { double lineSpace = font.GetHeight(); int cellSpace = font.FontFamily.GetLineSpacing(font.Style); int cellAscent = font.FontFamily.GetCellAscent(font.Style); int cellDescent = font.FontFamily.GetCellDescent(font.Style); double cyAscent = lineSpace * cellAscent / cellSpace; cyAscent = lineSpace * font.cellAscent / font.cellSpace; rc.Offset(0, (float)-cyAscent); } //this.gfx.DrawString(text, font.RealizeGdiFont(), brush.RealizeGdiBrush(), rect, // format != null ? format.RealizeGdiStringFormat() : null); this.gdipPath.AddString(s, family.gdiFamily, (int)style, (float)emSize, rc, format.RealizeGdiStringFormat()); #endif #if WPF && !GDI // Just a first sketch, but currently we do not need it and there is enough to do... double x = layoutRect.X; double y = layoutRect.Y; //double lineSpace = font.GetHeight(this); //double cyAscent = lineSpace * font.cellAscent / font.cellSpace; //double cyDescent = lineSpace * font.cellDescent / font.cellSpace; //double cyAscent = family.GetCellAscent(style) * family.GetLineSpacing(style) / family.getl; //fontlineSpace * font.cellAscent / font.cellSpace; //double cyDescent =family.GetCellDescent(style); // lineSpace * font.cellDescent / font.cellSpace; double lineSpace = font.GetHeight(); double cyAscent = lineSpace * font.cellAscent / font.cellSpace; double cyDescent = lineSpace * font.cellDescent / font.cellSpace; bool bold = (style & XFontStyle.Bold) != 0; bool italic = (style & XFontStyle.Italic) != 0; bool strikeout = (style & XFontStyle.Strikeout) != 0; bool underline = (style & XFontStyle.Underline) != 0; Typeface typeface = FontHelper.CreateTypeface(family, style); FormattedText formattedText = new FormattedText(s, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, emSize, System.Windows.Media.Brushes.Black); switch (format.Alignment) { case XStringAlignment.Near: // nothing to do, this is the default //formattedText.TextAlignment = TextAlignment.Left; break; case XStringAlignment.Center: x += layoutRect.Width / 2; formattedText.TextAlignment = TextAlignment.Center; break; case XStringAlignment.Far: x += layoutRect.Width; formattedText.TextAlignment = TextAlignment.Right; break; } //if (PageDirection == XPageDirection.Downwards) //{ switch (format.LineAlignment) { case XLineAlignment.Near: //y += cyAscent; break; case XLineAlignment.Center: // TODO use CapHeight. PDFlib also uses 3/4 of ascent y += -formattedText.Baseline + (cyAscent * 2 / 4) + layoutRect.Height / 2; break; case XLineAlignment.Far: y += -formattedText.Baseline - cyDescent + layoutRect.Height; break; case XLineAlignment.BaseLine: y -= formattedText.Baseline; break; } //} //else //{ // // TODOWPF // switch (format.LineAlignment) // { // case XLineAlignment.Near: // //y += cyDescent; // break; // case XLineAlignment.Center: // // TODO use CapHeight. PDFlib also uses 3/4 of ascent // //y += -(cyAscent * 3 / 4) / 2 + rect.Height / 2; // break; // case XLineAlignment.Far: // //y += -cyAscent + rect.Height; // break; // case XLineAlignment.BaseLine: // // nothing to do // break; // } //} //if (bold && !descriptor.IsBoldFace) //{ // // TODO: emulate bold by thicker outline //} //if (italic && !descriptor.IsBoldFace) //{ // // TODO: emulate italic by shearing transformation //} if (underline) { //double underlinePosition = lineSpace * realizedFont.FontDescriptor.descriptor.UnderlinePosition / font.cellSpace; //double underlineThickness = lineSpace * realizedFont.FontDescriptor.descriptor.UnderlineThickness / font.cellSpace; //DrawRectangle(null, brush, x, y - underlinePosition, width, underlineThickness); } if (strikeout) { //double strikeoutPosition = lineSpace * realizedFont.FontDescriptor.descriptor.StrikeoutPosition / font.cellSpace; //double strikeoutSize = lineSpace * realizedFont.FontDescriptor.descriptor.StrikeoutSize / font.cellSpace; //DrawRectangle(null, brush, x, y - strikeoutPosition - strikeoutSize, width, strikeoutSize); } //this.dc.DrawText(formattedText, layoutRectangle.Location.ToPoint()); //this.dc.DrawText(formattedText, new System.Windows.Point(x, y)); Geometry geo = formattedText.BuildGeometry(new Point(x, y)); this.pathGeometry.AddGeometry(geo); #endif }
public void DrawString(XGraphics gfx, string text, XFont font, XBrush brush, XRect layoutRectangle, XStringFormat format) { double x = layoutRectangle.X; double y = layoutRectangle.Y; double lineSpace = font.GetHeight(gfx); double cyAscent = lineSpace * font.cellAscent / font.cellSpace; double cyDescent = lineSpace * font.cellDescent / font.cellSpace; bool bold = (font.Style & XFontStyle.Bold) != 0; bool italic = (font.Style & XFontStyle.Italic) != 0; bool strikeout = (font.Style & XFontStyle.Strikeout) != 0; bool underline = (font.Style & XFontStyle.Underline) != 0; //FormattedText formattedText = new FormattedText(text, new CultureInfo("en-us"), // WPFHACK // FlowDirection.LeftToRight, font.typeface, font.Size, brush.RealizeWpfBrush()); TextBlock textBlock = FontHelper.CreateTextBlock(text, null, font.Size, brush.RealizeWpfBrush()); Canvas.SetLeft(textBlock, x); Canvas.SetTop(textBlock, y); //formattedText.SetTextDecorations(TextDecorations.OverLine); switch (format.Alignment) { case XStringAlignment.Near: // nothing to do, this is the default //formattedText.TextAlignment = TextAlignment.Left; break; case XStringAlignment.Center: x += layoutRectangle.Width / 2; textBlock.TextAlignment = TextAlignment.Center; break; case XStringAlignment.Far: x += layoutRectangle.Width; textBlock.TextAlignment = TextAlignment.Right; break; } if (gfx.PageDirection == XPageDirection.Downwards) { switch (format.LineAlignment) { case XLineAlignment.Near: //y += cyAscent; break; //case XLineAlignment.Center: // // TODO use CapHeight. PDFlib also uses 3/4 of ascent // y += -formattedText.Baseline + (cyAscent * 1 / 3) + layoutRectangle.Height / 2; // //y += -formattedText.Baseline + (font.Size * font.Metrics.CapHeight / font.unitsPerEm / 2) + layoutRectangle.Height / 2; // break; //case XLineAlignment.Far: // y += -formattedText.Baseline - cyDescent + layoutRectangle.Height; // break; //case XLineAlignment.BaseLine: // y -= formattedText.Baseline; // break; } } else { // TODOWPF: make unit test switch (format.LineAlignment) { case XLineAlignment.Near: //y += cyDescent; break; case XLineAlignment.Center: // TODO use CapHeight. PDFlib also uses 3/4 of ascent //y += -(cyAscent * 3 / 4) / 2 + rect.Height / 2; break; case XLineAlignment.Far: //y += -cyAscent + rect.Height; break; case XLineAlignment.BaseLine: // nothing to do break; } } //if (bold && !descriptor.IsBoldFace) //{ // // TODO: emulate bold by thicker outline //} //if (italic && !descriptor.IsBoldFace) //{ // // TODO: emulate italic by shearing transformation //} //if (underline) //{ // formattedText.FontStyle.SetTextDecorations(TextDecorations.Underline); // //double underlinePosition = lineSpace * realizedFont.FontDescriptor.descriptor.UnderlinePosition / font.cellSpace; // //double underlineThickness = lineSpace * realizedFont.FontDescriptor.descriptor.UnderlineThickness / font.cellSpace; // //DrawRectangle(null, brush, x, y - underlinePosition, width, underlineThickness); //} //if (strikeout) //{ // formattedText.SetTextDecorations(TextDecorations.Strikethrough); // //double strikeoutPosition = lineSpace * realizedFont.FontDescriptor.descriptor.StrikeoutPosition / font.cellSpace; // //double strikeoutSize = lineSpace * realizedFont.FontDescriptor.descriptor.StrikeoutSize / font.cellSpace; // //DrawRectangle(null, brush, x, y - strikeoutPosition - strikeoutSize, width, strikeoutSize); //} //formattedText _canvas.Children.Add(textBlock); }
/// <summary> /// Draws the specified text string. /// </summary> public void DrawString(string text, XFont font, XBrush brush, XRect layoutRectangle, XStringFormat format) { if (text == null) throw new ArgumentNullException("text"); if (font == null) throw new ArgumentNullException("font"); if (brush == null) throw new ArgumentNullException("brush"); if (format != null && format.LineAlignment == XLineAlignment.BaseLine && layoutRectangle.Height != 0) throw new InvalidOperationException("DrawString: With XLineAlignment.BaseLine the height of the layout rectangle must be 0."); if (text.Length == 0) return; if (format == null) format = XStringFormats.Default; if (this.drawGraphics) { #if GDI if (this.targetContext == XGraphicTargetContext.GDI) { RectangleF rect = layoutRectangle.ToRectangleF(); if (format.LineAlignment == XLineAlignment.BaseLine) { double lineSpace = font.GetHeight(this); int cellSpace = font.FontFamily.GetLineSpacing(font.Style); int cellAscent = font.FontFamily.GetCellAscent(font.Style); int cellDescent = font.FontFamily.GetCellDescent(font.Style); double cyAscent = lineSpace * cellAscent / cellSpace; cyAscent = lineSpace * font.cellAscent / font.cellSpace; rect.Offset(0, (float)-cyAscent); } this.gfx.DrawString(text, font.RealizeGdiFont(), brush.RealizeGdiBrush(), rect, format != null ? format.RealizeGdiStringFormat() : null); } #endif #if WPF if (this.targetContext == XGraphicTargetContext.WPF) { #if !SILVERLIGHT double x = layoutRectangle.X; double y = layoutRectangle.Y; double lineSpace = font.GetHeight(this); double cyAscent = lineSpace * font.cellAscent / font.cellSpace; double cyDescent = lineSpace * font.cellDescent / font.cellSpace; bool bold = (font.Style & XFontStyle.Bold) != 0; bool italic = (font.Style & XFontStyle.Italic) != 0; bool strikeout = (font.Style & XFontStyle.Strikeout) != 0; bool underline = (font.Style & XFontStyle.Underline) != 0; //FormattedText formattedText = new FormattedText(text, new CultureInfo("en-us"), // WPFHACK // FlowDirection.LeftToRight, font.typeface, font.Size, brush.RealizeWpfBrush()); FormattedText formattedText = FontHelper.CreateFormattedText(text, font.typeface, font.Size, brush.RealizeWpfBrush()); //formattedText.SetTextDecorations(TextDecorations.OverLine); switch (format.Alignment) { case XStringAlignment.Near: // nothing to do, this is the default //formattedText.TextAlignment = TextAlignment.Left; break; case XStringAlignment.Center: x += layoutRectangle.Width / 2; formattedText.TextAlignment = TextAlignment.Center; break; case XStringAlignment.Far: x += layoutRectangle.Width; formattedText.TextAlignment = TextAlignment.Right; break; } if (PageDirection == XPageDirection.Downwards) { switch (format.LineAlignment) { case XLineAlignment.Near: //y += cyAscent; break; case XLineAlignment.Center: // TODO use CapHeight. PDFlib also uses 3/4 of ascent y += -formattedText.Baseline + (cyAscent * 1 / 3) + layoutRectangle.Height / 2; //y += -formattedText.Baseline + (font.Size * font.Metrics.CapHeight / font.unitsPerEm / 2) + layoutRectangle.Height / 2; break; case XLineAlignment.Far: y += -formattedText.Baseline - cyDescent + layoutRectangle.Height; break; case XLineAlignment.BaseLine: y -= formattedText.Baseline; break; } } else { // TODOWPF: make unit test switch (format.LineAlignment) { case XLineAlignment.Near: //y += cyDescent; break; case XLineAlignment.Center: // TODO use CapHeight. PDFlib also uses 3/4 of ascent //y += -(cyAscent * 3 / 4) / 2 + rect.Height / 2; break; case XLineAlignment.Far: //y += -cyAscent + rect.Height; break; case XLineAlignment.BaseLine: // nothing to do break; } } //if (bold && !descriptor.IsBoldFace) //{ // // TODO: emulate bold by thicker outline //} //if (italic && !descriptor.IsBoldFace) //{ // // TODO: emulate italic by shearing transformation //} if (underline) { formattedText.SetTextDecorations(TextDecorations.Underline); //double underlinePosition = lineSpace * realizedFont.FontDescriptor.descriptor.UnderlinePosition / font.cellSpace; //double underlineThickness = lineSpace * realizedFont.FontDescriptor.descriptor.UnderlineThickness / font.cellSpace; //DrawRectangle(null, brush, x, y - underlinePosition, width, underlineThickness); } if (strikeout) { formattedText.SetTextDecorations(TextDecorations.Strikethrough); //double strikeoutPosition = lineSpace * realizedFont.FontDescriptor.descriptor.StrikeoutPosition / font.cellSpace; //double strikeoutSize = lineSpace * realizedFont.FontDescriptor.descriptor.StrikeoutSize / font.cellSpace; //DrawRectangle(null, brush, x, y - strikeoutPosition - strikeoutSize, width, strikeoutSize); } //this.dc.DrawText(formattedText, layoutRectangle.Location.ToPoint()); this.dc.DrawText(formattedText, new System.Windows.Point(x, y)); #else dc.DrawString(this, text, font, brush, layoutRectangle, format); #endif } #endif } if (this.renderer != null) this.renderer.DrawString(text, font, brush, layoutRectangle, format); }
/// <summary> /// Adds a text string to this path. /// </summary> public void AddString(string s, XFontFamily family, XFontStyle style, double emSize, XRect layoutRect, XStringFormat format) { if (s == null) throw new ArgumentNullException("s"); if (family == null) throw new ArgumentNullException("family"); if (format == null) format = XStringFormats.Default; if (format.LineAlignment == XLineAlignment.BaseLine && layoutRect.Height != 0) throw new InvalidOperationException( "DrawString: With XLineAlignment.BaseLine the height of the layout rectangle must be 0."); if (s.Length == 0) return; XFont font = new XFont(family.Name, emSize, style); #if CORE DiagnosticsHelper.HandleNotImplemented("XGraphicsPath.AddString"); #endif #if (GDI || CORE_) && !WPF //Gfx.DrawString(text, font.Realize_GdiFont(), brush.RealizeGdiBrush(), rect, // format != null ? format.RealizeGdiStringFormat() : null); if (family.GdiFamily == null) throw new NotFiniteNumberException(PSSR.NotImplementedForFontsRetrievedWithFontResolver(family.Name)); RectangleF rect = layoutRect.ToRectangleF(); rect.Offset(new PointF(0, SimulateBaselineOffset(family, style, emSize, format))); try { Lock.EnterGdiPlus(); _gdipPath.AddString(s, family.GdiFamily, (int)style, (float)emSize, rect, format.RealizeGdiStringFormat()); } finally { Lock.ExitGdiPlus(); } #endif #if WPF && !GDI if (family.WpfFamily == null) throw new NotFiniteNumberException(PSSR.NotImplementedForFontsRetrievedWithFontResolver(family.Name)); #if !SILVERLIGHT // Just a first sketch, but currently we do not need it and there is enough to do... double x = layoutRect.X; double y = layoutRect.Y; //double lineSpace = font.GetHeight(this); //double cyAscent = lineSpace * font.cellAscent / font.cellSpace; //double cyDescent = lineSpace * font.cellDescent / font.cellSpace; //double cyAscent = family.GetCellAscent(style) * family.GetLineSpacing(style) / family.getl; //fontlineSpace * font.cellAscent / font.cellSpace; //double cyDescent =family.GetCellDescent(style); // lineSpace * font.cellDescent / font.cellSpace; double lineSpace = font.GetHeight(); double cyAscent = lineSpace * font.CellAscent / font.CellSpace; double cyDescent = lineSpace * font.CellDescent / font.CellSpace; bool bold = (style & XFontStyle.Bold) != 0; bool italic = (style & XFontStyle.Italic) != 0; bool strikeout = (style & XFontStyle.Strikeout) != 0; bool underline = (style & XFontStyle.Underline) != 0; Typeface typeface = FontHelper.CreateTypeface(family.WpfFamily, style); //FormattedText formattedText = new FormattedText(s, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, emSize, WpfBrushes.Black); FormattedText formattedText = FontHelper.CreateFormattedText(s, typeface, emSize, WpfBrushes.Black); switch (format.Alignment) { case XStringAlignment.Near: // nothing to do, this is the default //formattedText.TextAlignment = TextAlignment.Left; break; case XStringAlignment.Center: x += layoutRect.Width / 2; formattedText.TextAlignment = TextAlignment.Center; break; case XStringAlignment.Far: x += layoutRect.Width; formattedText.TextAlignment = TextAlignment.Right; break; } //if (PageDirection == XPageDirection.Downwards) //{ switch (format.LineAlignment) { case XLineAlignment.Near: //y += cyAscent; break; case XLineAlignment.Center: // TO/DO use CapHeight. PDFlib also uses 3/4 of ascent //y += -formattedText.Baseline + (cyAscent * 2 / 4) + layoutRect.Height / 2; // GDI seems to make it this simple: // TODO: Check WPF's vertical alignment and make all implementations fit. $MaOs y += layoutRect.Height / 2 - lineSpace / 2; break; case XLineAlignment.Far: y += -formattedText.Baseline - cyDescent + layoutRect.Height; break; case XLineAlignment.BaseLine: y -= formattedText.Baseline; break; } //} //else //{ // // TODOWPF // switch (format.LineAlignment) // { // case XLineAlignment.Near: // //y += cyDescent; // break; // case XLineAlignment.Center: // // TODO use CapHeight. PDFlib also uses 3/4 of ascent // //y += -(cyAscent * 3 / 4) / 2 + rect.Height / 2; // break; // case XLineAlignment.Far: // //y += -cyAscent + rect.Height; // break; // case XLineAlignment.BaseLine: // // nothing to do // break; // } //} //if (bold && !descriptor.IsBoldFace) //{ // // TODO: emulate bold by thicker outline //} //if (italic && !descriptor.IsItalicFace) //{ // // TODO: emulate italic by shearing transformation //} if (underline) { //double underlinePosition = lineSpace * realizedFont.FontDescriptor.descriptor.UnderlinePosition / font.cellSpace; //double underlineThickness = lineSpace * realizedFont.FontDescriptor.descriptor.UnderlineThickness / font.cellSpace; //DrawRectangle(null, brush, x, y - underlinePosition, width, underlineThickness); } if (strikeout) { //double strikeoutPosition = lineSpace * realizedFont.FontDescriptor.descriptor.StrikeoutPosition / font.cellSpace; //double strikeoutSize = lineSpace * realizedFont.FontDescriptor.descriptor.StrikeoutSize / font.cellSpace; //DrawRectangle(null, brush, x, y - strikeoutPosition - strikeoutSize, width, strikeoutSize); } //dc.DrawText(formattedText, layoutRectangle.Location.ToPoint()); //dc.DrawText(formattedText, new SysPoint(x, y)); Geometry geo = formattedText.BuildGeometry(new Point(x, y)); _pathGeometry.AddGeometry(geo); #else // AG-HACK throw new InvalidOperationException("Silverlight cannot create geometry of glyphs."); // TODO: no, yagni #endif #endif }
/// <summary> /// Resembles the DrawString function of GDI+. /// </summary> public void DrawString(XGraphics gfx, string text, XFont font, XBrush brush, XRect layoutRectangle, XStringFormat format) { double x = layoutRectangle.X; double y = layoutRectangle.Y; double lineSpace = font.GetHeight(); //old: font.GetHeight(gfx); double cyAscent = lineSpace * font.CellAscent / font.CellSpace; double cyDescent = lineSpace * font.CellDescent / font.CellSpace; bool bold = (font.Style & XFontStyle.Bold) != 0; bool italic = (font.Style & XFontStyle.Italic) != 0; bool strikeout = (font.Style & XFontStyle.Strikeout) != 0; bool underline = (font.Style & XFontStyle.Underline) != 0; //Debug.Assert(font.GlyphTypeface != null); TextBlock textBlock = new TextBlock(); //FontHelper.CreateTextBlock(text, font.GlyphTypeface, font.Size, brush.RealizeWpfBrush()); if (layoutRectangle.Width > 0) textBlock.Width = layoutRectangle.Width; switch (format.Alignment) { case XStringAlignment.Near: textBlock.TextAlignment = TextAlignment.Left; break; case XStringAlignment.Center: textBlock.TextAlignment = TextAlignment.Center; break; case XStringAlignment.Far: textBlock.TextAlignment = TextAlignment.Right; break; } if (gfx.PageDirection == XPageDirection.Downwards) { switch (format.LineAlignment) { case XLineAlignment.Near: //y += cyAscent; break; case XLineAlignment.Center: // TODO use CapHeight. PDFlib also uses 3/4 of ascent y += (layoutRectangle.Height - textBlock.ActualHeight) / 2; //y += -formattedText.Baseline + (font.Size * font.Metrics.CapHeight / font.unitsPerEm / 2) + layoutRectangle.Height / 2; break; case XLineAlignment.Far: y += layoutRectangle.Height - textBlock.ActualHeight; //y -= textBlock.ActualHeight; //-formattedText.Baseline - cyDescent + layoutRectangle.Height; break; case XLineAlignment.BaseLine: //#if !WINDOWS_PHONE y -= textBlock.BaselineOffset; //#else // // No BaselineOffset in Silverlight WP yet. // //y -= textBlock.BaselineOffset; //#endif break; } } else { throw new NotImplementedException("XPageDirection.Downwards"); } //if (bold && !descriptor.IsBoldFace) //{ // // TODO: emulate bold by thicker outline //} //if (italic && !descriptor.IsBoldFace) //{ // // TODO: emulate italic by shearing transformation //} if (underline) textBlock.TextDecorations = TextDecorations.Underline; // No strikethrough in Silverlight //if (strikeout) //{ // formattedText.SetTextDecorations(TextDecorations.Strikethrough); // //double strikeoutPosition = lineSpace * realizedFont.FontDescriptor.descriptor.StrikeoutPosition / font.cellSpace; // //double strikeoutSize = lineSpace * realizedFont.FontDescriptor.descriptor.StrikeoutSize / font.cellSpace; // //DrawRectangle(null, brush, x, y - strikeoutPosition - strikeoutSize, width, strikeoutSize); //} Canvas.SetLeft(textBlock, x); Canvas.SetTop(textBlock, y); ActiveCanvas.Children.Add(textBlock); }
internal static double GetSubSuperScaling(XFont font) { return 0.8 * GetAscent(font) / font.GetHeight(); }