protected double CulcRenderWidth(string text, ItemFont itemFont) { double width = 0; ushort glyphIndex; int glyphType; for (int i = 0; i < text.Length; i++) { width += itemFont.GlyphWidth(text, ref i, out glyphIndex, out glyphType); } return(width); }
protected double RenderText(List <Tuple <Brush, GlyphRun> > textDrawList, String text, ItemFont itemFont, double fontSize, Rect drawRect, double marginLeft, double margintTop, Brush fontColor, bool nowrap = false) { double lineHeight = ViewUtil.CulcLineHeight(fontSize); double x0 = drawRect.Left + marginLeft; double xMax = drawRect.Right; double y0 = drawRect.Top + margintTop - (lineHeight - fontSize);//行間オフセット double yMax = nowrap == true ? y0 : drawRect.Bottom; double y = y0; var getRenderHeight = new Func <double>(() => y - y0); for (int i = 0; i < text.Length;) { var glyphIndexes = new List <ushort>(); var advanceWidths = new List <double>(); int currentType = 0; double x = x0; double x1 = x0;//currentTypeの書き出しx座標 y += lineHeight; var AddGlyphRun = new Action(() => { if (glyphIndexes.Count == 0) { return; } var origin = new Point(Math.Round(x1 * m.M11) / m.M11 - selfLeft, Math.Round(y * m.M22) / m.M22 - selfTop); textDrawList.Add(new Tuple <Brush, GlyphRun>(fontColor, new GlyphRun(itemFont.GlyphType[currentType], 0, false, fontSize, glyphIndexes, origin, advanceWidths, null, null, null, null, null, null))); }); for (; i < text.Length && text[i] != '\r' && text[i] != '\n'; i++) { //この辞書検索が負荷の大部分を占めているのでテーブルルックアップする ushort glyphIndex; int glyphType; double width = itemFont.GlyphWidth(text, ref i, out glyphIndex, out glyphType) * fontSize; if (x + width > xMax) { AddGlyphRun(); if (y >= yMax) { return(getRenderHeight()); //次の行無理 } //次の行へ glyphIndexes = new List <ushort>(); advanceWidths = new List <double>(); x = x1 = x0; y += lineHeight; } else if (glyphType != currentType) { //フォントが変わった AddGlyphRun(); glyphIndexes = new List <ushort>(); advanceWidths = new List <double>(); x1 = x; } currentType = glyphType; glyphIndexes.Add(glyphIndex); advanceWidths.Add(width); x += width; } AddGlyphRun(); if (y >= yMax) { return(getRenderHeight()); //次の行無理 } i = text.IndexOf('\n', i); i = i < 0 ? text.Length : i + 1; } return(getRenderHeight()); }