private void cmdMeasureString_Click(object sender, EventArgs e) { //How to measure user's string... //this demostrate step-by-step //similar to ... selectedTextPrinter.DrawString(printTextBuffer, x_pos, y_pos); string str = txtInputChar.Text; // Typeface typeface = _basicOptions.Typeface; float fontSizeInPoints = _basicOptions.FontSizeInPoints; var layout = new Typography.TextLayout.GlyphLayout(); layout.Typeface = typeface; layout.ScriptLang = _basicOptions.ScriptLang; layout.PositionTechnique = _basicOptions.PositionTech; layout.EnableLigature = false;// true layout.EnableComposition = true; //3. //3.1 : if you want GlyphPlanList too. //var resultGlyphPlanList = new Typography.TextLayout.GlyphPlanList(); //Typography.TextLayout.MeasuredStringBox box = layout.LayoutAndMeasureString(str.ToCharArray(), 0, str.Length, _basicOptions.FontSizeInPoints, resultGlyphPlanList); //or //3.2 : only MeasuredStringBox Typography.TextLayout.MeasuredStringBox box = layout.LayoutAndMeasureString( str.ToCharArray(), 0, str.Length, fontSizeInPoints); this.lblStringSize.Text = "measure (W,H)= (" + box.width.ToString() + "," + (box.AscendingInPx - box.DescendingInPx) + ") px"; }
public static void MeasureString( this GlyphLayout glyphLayout, char[] textBuffer, int startAt, int len, out MeasuredStringBox strBox, float scale = 1) { //TODO: consider extension method List <GlyphPlan> outputGlyphPlans = glyphLayout._myGlyphPlans; outputGlyphPlans.Clear(); glyphLayout.Layout(textBuffer, startAt, len, outputGlyphPlans); // int j = outputGlyphPlans.Count; Typeface currentTypeface = glyphLayout.Typeface; if (j == 0) { //not scale strBox = new MeasuredStringBox(0, currentTypeface.Ascender * scale, currentTypeface.Descender * scale, currentTypeface.LineGap * scale); } //get last one GlyphPlan lastOne = outputGlyphPlans[j - 1]; strBox = new MeasuredStringBox((lastOne.x + lastOne.advX) * scale, currentTypeface.Ascender * scale, currentTypeface.Descender * scale, currentTypeface.LineGap * scale); }
//static void ConcatMeasureBox(ref float accumW, ref float accumH, ref MeasuredStringBox measureBox) //{ // accumW += measureBox.width; // float h = measureBox.CalculateLineHeight(); // if (h > accumH) // { // accumH = h; // } //} public static MeasuredStringBox LayoutAndMeasureString( this GlyphLayout glyphLayout, char[] textBuffer, int startAt, int len, float fontSizeInPoints, float limitW = -1,//-1 unlimit scaled width (px) bool snapToGrid = true) { //1. unscale layout, in design unit glyphLayout.Layout(textBuffer, startAt, len); //2. scale to specific font size Typeface typeface = glyphLayout.Typeface; float pxscale = typeface.CalculateScaleToPixelFromPointSize(fontSizeInPoints); //.... float scaled_accumX = 0; if (limitW < 0) { //no limit scaled_accumX = MeasureGlyphPlans( glyphLayout, pxscale, snapToGrid); return(new MeasuredStringBox( scaled_accumX, typeface.Ascender, typeface.Descender, typeface.LineGap, typeface.ClipedAscender, typeface.ClipedDescender, pxscale)); } else if (limitW > 0) { scaled_accumX = MeasureGlyphPlanWithLimitWidth( glyphLayout, pxscale, limitW, snapToGrid, out int stopAtChar); var mstrbox = new MeasuredStringBox( scaled_accumX, typeface.Ascender, typeface.Descender, typeface.LineGap, typeface.ClipedAscender, typeface.ClipedDescender, pxscale); mstrbox.StopAt = (ushort)stopAtChar; return(mstrbox); } else { return(new MeasuredStringBox( 0, typeface.Ascender, typeface.Descender, typeface.LineGap, typeface.ClipedAscender, typeface.ClipedDescender, pxscale)); } }