public virtual void MeasureWidths(byte[] s, int len, TextPositions positions, int enc) { // A bug in GDI+ means that the string has to be broken up into blocks // of 32 or less. int maxMeasurePerCall = 32; try { String sAll = System.Text.Encoding.UTF8.GetString(s, 0, len); int sAllLength = sAll.Length; for (int start = 0; start < sAllLength; start += maxMeasurePerCall) { int sLength = sAllLength - start; if (sLength > maxMeasurePerCall) { sLength = maxMeasurePerCall; } CharacterRange [] characterRanges = new CharacterRange[sLength]; for (int i = 0; i < sLength; i++) { // Another problem with measuring trailing spaces means // that this doesn't work with a range containing a single space. if (sAll[start + i] == ' ') { characterRanges[i] = new CharacterRange(start, i + 1); } else { characterRanges[i] = new CharacterRange(start + i, 1); } } sf.SetMeasurableCharacterRanges(characterRanges); RectangleF layoutRect = new RectangleF(0.0f, 0.0f, float.MaxValue, float.MaxValue); Region[] regions = new Region[sLength]; regions = g.MeasureCharacterRanges(sAll, fontHandle, layoutRect, sf); for (int i = 0; i < sLength; i++) { char ch = sAll[start + i]; float xOfCharRight = (regions[i].GetBounds(g).Right); int xPixel = (int)(xOfCharRight); positions.Add(xPixel); if (ch >= 0x80) { positions.Add(xPixel); } if (ch >= 0x800) { positions.Add(xPixel); } } } } catch (System.IO.IOException) { System.Console.Out.WriteLine("Failed to convert"); } }
public virtual void xMeasureWidths(byte[] s, int len, TextPositions positions, int enc) { try { String sg = System.Text.Encoding.UTF8.GetString(s, 0, len); int sLength = sg.Length; SizeF sz = g.MeasureString(sg, fontHandle, 2000, sf); float maxWidth = sz.Width; float x = 0; float [] xPositions = new float[sLength]; for (int i = 0; i < sLength; i++) { char ch = sg[i]; sz = g.MeasureString(new String(ch, 1), fontHandle, 2000, sf); x += sz.Width; xPositions[i] = x; } float ratio = maxWidth / x; for (int i = 0; i < sLength; i++) { char ch = sg[i]; int xPixel = (int)(xPositions[i] * ratio); positions.Add(xPixel); if (ch >= 0x80) { positions.Add(xPixel); } if (ch >= 0x800) { positions.Add(xPixel); } } } catch (System.IO.IOException) { System.Console.Out.WriteLine("Failed to convert"); } }