public override void RecalcLayout() { base.RecalcLayout(); Font font = NumIndicator.AbsFont; FontFamily family = new FontFamily(font.Name); int descent = family.GetCellDescent(font.Style); float descentPixel = font.Size * descent / family.GetEmHeight(font.Style); _Descent = (int)(descentPixel / 2) + 1; }
void ObtainFontMetrics(Graphics g) { string infoString = ""; // enough space for one line of output int ascent; // font family ascent in design units float ascentPixel; // ascent converted to pixels int descent; // font family descent in design units float descentPixel; // descent converted to pixels int lineSpacing; // font family line spacing in design units float lineSpacingPixel; // line spacing converted to pixels FontStyle fontStyle = FontStyle.Regular; //fontStyle = FontStyle.Italic | FontStyle.Bold; FontFamily fontFamily = new FontFamily("arial"); //fontFamily = FontFamily.GenericSansSerif; Font font = new Font( fontFamily, 16, fontStyle, GraphicsUnit.Pixel); PointF pointF = new PointF(10, 10); SolidBrush solidBrush = new SolidBrush(Color.Black); // Display the font size in pixels. infoString = "font family : " + font.FontFamily.Name + " " + fontStyle + "."; g.DrawString(infoString, font, solidBrush, pointF); // Move down one line. pointF.Y += font.Height; // Display the font size in pixels. infoString = "font.Size returns " + font.Size + "."; g.DrawString(infoString, font, solidBrush, pointF); // Move down one line. pointF.Y += font.Height; // Display the font family em height in design units. infoString = "fontFamily.GetEmHeight() returns " + fontFamily.GetEmHeight(fontStyle) + "."; g.DrawString(infoString, font, solidBrush, pointF); // Move down two lines. pointF.Y += 2 * font.Height; // Display the ascent in design units and pixels. ascent = fontFamily.GetCellAscent(fontStyle); // 14.484375 = 16.0 * 1854 / 2048 ascentPixel = font.Size * ascent / fontFamily.GetEmHeight(fontStyle); infoString = "The ascent is " + ascent + " design units, " + ascentPixel + " pixels."; g.DrawString(infoString, font, solidBrush, pointF); // Move down one line. pointF.Y += font.Height; // Display the descent in design units and pixels. descent = fontFamily.GetCellDescent(fontStyle); // 3.390625 = 16.0 * 434 / 2048 descentPixel = font.Size * descent / fontFamily.GetEmHeight(fontStyle); infoString = "The descent is " + descent + " design units, " + descentPixel + " pixels."; g.DrawString(infoString, font, solidBrush, pointF); // Move down one line. pointF.Y += font.Height; // Display the line spacing in design units and pixels. lineSpacing = fontFamily.GetLineSpacing(fontStyle); // 18.398438 = 16.0 * 2355 / 2048 lineSpacingPixel = font.Size * lineSpacing / fontFamily.GetEmHeight(fontStyle); infoString = "The line spacing is " + lineSpacing + " design units, " + lineSpacingPixel + " pixels."; g.DrawString(infoString, font, solidBrush, pointF); title = "ObtainFontMetrics"; }
/// <summary> /// Connects the specifications of a font from XFont to a real glyph type face. /// </summary> void Initialize() { XFontMetrics fm = null; #if DEBUG___ FontData[] fontDataArray = FontDataStock.Global.GetFontDataList(); if (fontDataArray.Length > 0) { //// GetType(); ////#if GDI //// var x = XPrivateFontCollection.global.GlobalPrivateFontCollection; //// families = x.Families; //// bool fff = families[0].IsStyleAvailable(System.Drawing.FontStyle.Regular); //// fff.GetType(); //// this.font = new Font(families[0].Name, 12, System.Drawing.FontStyle.Regular, GraphicsUnit.Pixel); //// this.font = new Font("Oblivious", 12, System.Drawing.FontStyle.Regular, GraphicsUnit.Pixel); //// this.font = new Font(families[0], 12, System.Drawing.FontStyle.Regular, GraphicsUnit.Pixel); //// System.Drawing.FontFamily f = new System.Drawing.FontFamily(families[0].Name); //// f.GetType(); ////#endif } #endif #if GDI if (this.font == null) { if (this.gdifamily != null) { this.font = new Font(this.gdifamily, (float)this.emSize, (System.Drawing.FontStyle) this.style, GraphicsUnit.World); this.familyName = this.gdifamily.Name; // Do we need this??? } else { // First check private fonts this.font = XPrivateFontCollection.TryFindPrivateFont(this.familyName, this.emSize, (System.Drawing.FontStyle) this.style) ?? new Font(this.familyName, (float)this.emSize, (System.Drawing.FontStyle) this.style, GraphicsUnit.World); } #if DEBUG // new Font returns MSSansSerif if the requested font was not found ... //Debug.Assert(this.familyName == this.font.FontFamily.Name); #endif } fm = Metrics; System.Drawing.FontFamily fontFamily = this.font.FontFamily; this.unitsPerEm = fm.UnitsPerEm; System.Drawing.FontFamily fontFamily2 = this.font.FontFamily; this.cellSpace = fontFamily2.GetLineSpacing(font.Style); //Debug.Assert(this.cellSpace == fm.Ascent + Math.Abs(fm.Descent) + fm.Leading, "Value differs from information retrieved from font image."); this.cellAscent = fontFamily.GetCellAscent(font.Style); #pragma warning disable 1030 #warning delTHHO //!!!delTHHO 14.08.2008 Debug.Assert(this.cellAscent == fm.Ascent, "Value differs from information retrieved from font image."); //Debug.Assert(this.cellAscent == fm.Ascent, "Value differs from information retrieved from font image."); this.cellDescent = fontFamily.GetCellDescent(font.Style); #if DEBUG int desc = Math.Abs(fm.Descent); if (this.cellDescent != desc) { Debug.Assert(false, "Value differs from information retrieved from font image."); } #endif #endif #if WPF #if !SILVERLIGHT if (this.family == null) { Debug.Assert(this.typeface == null); this.typeface = XPrivateFontCollection.TryFindTypeface(Name, this.style, out this.family); #if true if (this.typeface != null) { GlyphTypeface glyphTypeface; ICollection <Typeface> list = this.family.GetTypefaces(); foreach (Typeface tf in list) { if (!tf.TryGetGlyphTypeface(out glyphTypeface)) { Debugger.Break(); } } if (!this.typeface.TryGetGlyphTypeface(out glyphTypeface)) { throw new InvalidOperationException(PSSR.CannotGetGlyphTypeface(Name)); } } #endif } if (this.family == null) { this.family = new System.Windows.Media.FontFamily(Name); } if (typeface == null) { this.typeface = FontHelper.CreateTypeface(this.family, style); } fm = Metrics; Debug.Assert(this.unitsPerEm == 0 || this.unitsPerEm == fm.UnitsPerEm); this.unitsPerEm = fm.UnitsPerEm; //Debug.Assert(this.cellSpace == 0 || this.cellSpace == fm.Ascent + Math.Abs(fm.Descent) + fm.Leading); this.cellSpace = fm.Ascent + Math.Abs(fm.Descent) + fm.Leading; Debug.Assert(this.cellAscent == 0 || this.cellAscent == fm.Ascent); this.cellAscent = fm.Ascent; Debug.Assert(this.cellDescent == 0 || this.cellDescent == Math.Abs(fm.Descent)); this.cellDescent = Math.Abs(fm.Descent); #else if (fm != null) { fm.GetType(); } #endif #endif }
public MetricsInfo GetMetricInfo(int character, GraphicsContext context, DeviceDescription description) { var svgContext = _mapper.MapGraphicsContextToSvg(new Point(), 0, context); var s = new string(new[] { (char)character }); var family = new FontFamily(svgContext.Font.Family); var style = GetStyle(context.FontFace); return new MetricsInfo { Ascent = family.GetCellAscent(style), Descent = family.GetCellDescent(style), Width = MeasureWidth(s, context, description), }; }
protected void uiButtonApply_Click(object sender, EventArgs e) { try { MailMessage msg = new MailMessage(); msg.IsBodyHtml = true; System.Drawing.Graphics myGraphic = null; string[] images = uiRadioButtonListLayouts.SelectedValue.Split('#'); System.Drawing.Image imgSelectedLayout; imgSelectedLayout = System.Drawing.Image.FromFile(Server.MapPath("~/" + images[0])); // add layout as attach msg.Attachments.Add(new Attachment(Server.MapPath("~/" + images[0]))); System.Drawing.Color backcolor = System.Drawing.ColorTranslator.FromHtml(uiHiddenFieldColor.Value); System.Drawing.Brush b = new System.Drawing.SolidBrush(backcolor); Cards card = new Cards(); card.LoadByPrimaryKey(CardID); // add Main card info msg.Body += "<br /> =========================================================== <br />"; msg.Body += " ========================= Card info ================== <br />"; msg.Body += " Card Name : " + card.CardNameEng; msg.Body += "<br /> Card Price : " + card.PriceNow; msg.Body += "<br /> Card Path : " + FullPath + " > " + card.CardNameEng; // add background color msg.Body += "<br /> =========================================================== <br />"; msg.Body += " ========================= Background color ================== <br />"; msg.Body += "Background Color : " + uiHiddenFieldColor.Value + "<br />"; System.Drawing.Bitmap backgroundcolor = new System.Drawing.Bitmap(imgSelectedLayout); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(backgroundcolor); g.FillRectangle(b, 0, 0, imgSelectedLayout.Width, imgSelectedLayout.Height); g.Save(); string backpath = Guid.NewGuid().ToString(); backgroundcolor.Save(Server.MapPath("~/images/UserOrders/" + backpath + "_1.jpeg")); System.Drawing.Image GeneratedImg;// =new Image.FromFile(ImageBack); // draw background GeneratedImg = System.Drawing.Image.FromFile(Server.MapPath("~/images/UserOrders/" + backpath + "_1.jpeg")); myGraphic = System.Drawing.Graphics.FromImage(GeneratedImg); myGraphic.SmoothingMode = SmoothingMode.HighQuality; msg.Body += "<br /> =========================================================== <br />"; msg.Body += " ========================= Images Info ================== <br />"; //draw user uploaded images foreach (DataListItem dli in uiDataListImages.Items) { if (dli.ItemType == ListItemType.Item || dli.ItemType == ListItemType.AlternatingItem) { FileUpload imgfile = (FileUpload)dli.FindControl("uiFileUploadImg"); HiddenField hfPath = (HiddenField)dli.FindControl("uiHiddenFieldPath"); HiddenField hf = (HiddenField)dli.FindControl("uiHiddenFieldImgID"); HiddenField contrast = (HiddenField)dli.FindControl("uiHiddenFieldContrast"); HiddenField brightness = (HiddenField)dli.FindControl("uiHiddenFieldBrightness"); HiddenField rotate = (HiddenField)dli.FindControl("uiHiddenFieldRotate"); DropDownList style = (DropDownList)dli.FindControl("uiDropDownListStyle"); System.Web.UI.WebControls.Image current = (System.Web.UI.WebControls.Image)dli.FindControl("uiImageCurrent"); CardImages img = new CardImages(); img.LoadByPrimaryKey(Convert.ToInt32(hf.Value)); string path = hfPath.Value; if (imgfile.HasFile) { path = "images/UserOrders/cardImages/" + Guid.NewGuid().ToString() + imgfile.FileName; imgfile.SaveAs(Server.MapPath("~/" + path)); hfPath.Value = path; current.ImageUrl = path; current.Visible = true; } if (!string.IsNullOrEmpty(path)) { System.Drawing.Image currentCardImage; WebClient webclient = new WebClient(); System.IO.Stream s = webclient.OpenRead(ConfigurationManager.AppSettings["appHome"] + "ImageOnDemand.ashx?url=" + path + "&brightness=" + brightness.Value + "&contrast=" + contrast.Value + "&" + style.SelectedValue); currentCardImage = System.Drawing.Image.FromStream(s); if (float.Parse(rotate.Value) > 0) { myGraphic.TranslateTransform((float)img.ImgWidth / 2, (float)img.ImgHieght / 2); myGraphic.RotateTransform(float.Parse(rotate.Value)); myGraphic.DrawImageUnscaled(currentCardImage, img.PosX, img.PosY, img.ImgWidth, img.ImgHieght); myGraphic.RotateTransform(-float.Parse(rotate.Value)); myGraphic.TranslateTransform(-(float)img.ImgWidth / 2, -(float)img.ImgHieght / 2); } else { myGraphic.DrawImageUnscaled(currentCardImage, img.PosX, img.PosY, img.ImgWidth, img.ImgHieght); } // add image uploaded by user as attachment msg.Attachments.Add(new Attachment(Server.MapPath("~/" + path))); msg.Body += "Image " + (dli.ItemIndex + 1).ToString() + " : <br />"; msg.Body += "contrust : " + contrast.Value + "<br />"; msg.Body += "brightness : " + brightness.Value + " <br />"; msg.Body += "rotaion : " + rotate.Value + " <br />"; msg.Body += "style : " + style.SelectedValue.Substring(0, style.SelectedValue.IndexOf("=")) + " <br />"; } } } myGraphic.ResetTransform(); // draw layout //myGraphic.DrawImageUnscaled(imgSelectedLayout, 0, 0,imgSelectedLayout.Width, imgSelectedLayout.Height); myGraphic.DrawImage(imgSelectedLayout, 0, 0, imgSelectedLayout.Width, imgSelectedLayout.Height); myGraphic.Save(); // draw strings myGraphic.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; msg.Body += "<br /> =========================================================== <br />"; msg.Body += " ========================= Text Info ================== <br />"; foreach (DataListItem dli in uiDataListCardText.Items) { if (dli.ItemType == ListItemType.Item || dli.ItemType == ListItemType.AlternatingItem) { HiddenField hf = (HiddenField)dli.FindControl("uiHiddenFieldTextID"); HiddenField hfc = (HiddenField)dli.FindControl("uiHiddenFieldMyColor"); DropDownList ddlfont = (DropDownList)dli.FindControl("uiDropDownListFonts"); DropDownList ddlfontsize = (DropDownList)dli.FindControl("uiDropDownListFontSize"); CardText text = new CardText(); TextBox tb = (TextBox)dli.FindControl("uiTextBoxText"); text.LoadByPrimaryKey(Convert.ToInt32(hf.Value)); System.Drawing.Color fontcolor = System.Drawing.ColorTranslator.FromHtml(hfc.Value); System.Drawing.Brush fontbrush = new System.Drawing.SolidBrush(fontcolor); FontFamily family = new FontFamily(ddlfont.SelectedItem.Text); float fontsize = (Convert.ToInt32(ddlfontsize.SelectedItem.Text) * family.GetEmHeight(FontStyle.Regular)) / family.GetCellDescent(FontStyle.Regular); myGraphic.DrawString(tb.Text, new Font(ddlfont.SelectedItem.Text, fontsize, FontStyle.Italic), fontbrush, new RectangleF(text.PosX, text.PosY, text.Width, text.Height)); msg.Body += text.TextLabel + " : " + tb.Text + "<br />"; msg.Body += "font : " + ddlfont.SelectedItem.Text + "<br />"; msg.Body += "font size : " + ddlfontsize.SelectedItem.Text + " <br />"; msg.Body += "color : " + hfc.Value + " <br />"; } } myGraphic.Save(); string newpath = Guid.NewGuid().ToString(); // save generated img GeneratedImg.Save(Server.MapPath("~/images/UserOrders/" + newpath + "_2.jpeg"), System.Drawing.Imaging.ImageFormat.Jpeg); uiImageMain.ImageUrl = "~/images/UserOrders/" + newpath + "_2.jpeg"; // add final image as attachment msg.Attachments.Add(new Attachment(Server.MapPath("~/images/UserOrders/" + newpath + "_2.jpeg"))); myGraphic.Dispose(); UserPayement temp = new UserPayement(); temp.AddNew(); temp.CardID = CardID; temp.CardDesign = "images/UserOrders/" + newpath + "_2.jpeg"; temp.CardCount = 1; try { temp.GetColumn("ItemPrice"); } catch (Exception) { temp.AddColumn("ItemPrice", Type.GetType("System.Double")); } temp.SetColumn("ItemPrice", card.PriceNow); Session["UserPayment"] = temp; uipanelError.Visible = false; OrderMail = msg; } catch (Exception ex) { uipanelError.Visible = true; } }
public void FontFamily_String () { HostIgnoreList.CheckTest ("MonoTests.System.Drawing.FontFamilyTest.FontFamily_String"); FontFamily ff = new FontFamily (name); CheckMono (ff); FontStyle style = FontStyle.Bold; Assert.AreEqual (ff.Name, ff.GetName (0), "GetName"); Assert.IsTrue ((ff.GetCellAscent (style) > 0), "GetCellAscent"); Assert.IsTrue ((ff.GetCellDescent (style) > 0), "GetCellDescent"); Assert.IsTrue ((ff.GetEmHeight (style) > 0), "GetEmHeight"); Assert.IsTrue ((ff.GetLineSpacing (style) > 0), "GetLineSpacing"); Assert.IsTrue (ff.IsStyleAvailable (style), "IsStyleAvailable"); }