private void ArrangeText() { try { fAcceptFontChange = false; fHeights.Clear(); Graphics gfx = CreateGraphics(); //gfx.TextRenderingHint = TextRenderingHint.AntiAlias; try { int xPos = 0; int yPos = 0; int xMax = 0; int lineHeight = 0; string text = fLines.Text; Font defFont = this.Font; SizeF csz = this.ClientSize; SizeF zerosz = new SizeF(0f, 0f); var parser = new BBTextParser(AppHost.GfxProvider, defFont.SizeInPoints, new ColorHandler(fLinkColor), new ColorHandler(ForeColor)); parser.ParseText(fChunks, text); int line = -1; int chunksCount = fChunks.Count; int k = 0; while (k < chunksCount) { BBTextChunk chunk = fChunks[k]; bool recalcChunk = false; if (line != chunk.Line) { line = chunk.Line; if (line > 0) { yPos += lineHeight; fHeights.Add(lineHeight); } xPos = 0; lineHeight = 0; } int prevX = xPos; int prevY = yPos; string chunkStr = chunk.Text; if (!string.IsNullOrEmpty(chunkStr)) { using (var font = new Font(defFont.Name, chunk.Size, (sdFontStyle)chunk.Style, defFont.Unit)) { SizeF strSize = gfx.MeasureString(chunkStr, font, zerosz, fStrFormat); if (fWordWrap) { int wBound = xPos + 2 * fBorderWidth; if (wBound + strSize.Width > csz.Width) { int lastIndex = chunkStr.Length - 1; while (true) { int spPos = chunkStr.LastIndexOf(' ', lastIndex); if (spPos <= 0) { // the beginning of the chunk is reached and there are no more words to carry chunk.Text = chunkStr.Substring(0, lastIndex + 1); strSize = gfx.MeasureString(chunkStr, font, zerosz, fStrFormat); // the current chunk still does not fit into the area if (wBound + strSize.Width > csz.Width) { // this is not the only chunk on this line if (k > 0 && fChunks[k - 1].Line == chunk.Line) { // transfer the current chunk to the next line // and recount it again at the next iteration ShiftChunks(k, chunksCount); recalcChunk = true; } } break; } string newChunk = chunkStr.Substring(0, spPos); strSize = gfx.MeasureString(newChunk, font, zerosz, fStrFormat); if (wBound + strSize.Width < csz.Width) { var secondPart = chunk.Clone(); secondPart.Text = chunkStr.Substring(spPos + 1); secondPart.Line += 1; fChunks.Insert(k + 1, secondPart); chunksCount += 1; // shift next chunks ShiftChunks(k + 2, chunksCount); chunk.Text = newChunk; break; } else { lastIndex = spPos - 1; } } } } chunk.Width = (int)strSize.Width; xPos += chunk.Width; if (xMax < xPos) { xMax = xPos; } int h = (int)strSize.Height; if (lineHeight < h) { lineHeight = h; } } if (!string.IsNullOrEmpty(chunk.URL)) { chunk.LinkRect = ExtRect.CreateBounds(prevX, prevY, xPos - prevX, lineHeight); } } if (!recalcChunk) { k++; } } fTextSize = new ExtSize(xMax + 2 * fBorderWidth, yPos + 2 * fBorderWidth); } finally { gfx.Dispose(); fAcceptFontChange = true; AdjustViewport(fTextSize); } } catch (Exception ex) { Logger.LogWrite("HyperView.ArrangeText(): " + ex.Message); } }
private void ArrangeText() { try { fAcceptFontChange = false; fHeights.Clear(); try { int xPos = 0; int yPos = 0; int xMax = 0; int lineHeight = 0; string text = fLines.Text.Trim(); if (!string.IsNullOrEmpty(text)) { Font defFont = this.Font; var parser = new BBTextParser(AppHost.GfxProvider, defFont.Size, new ColorHandler(fLinkColor), new ColorHandler(TextColor)); parser.ParseText(fChunks, text); int line = -1; int chunksCount = fChunks.Count; for (int k = 0; k < chunksCount; k++) { BBTextChunk chunk = fChunks[k]; if (line != chunk.Line) { line = chunk.Line; if (line > 0) { yPos += lineHeight; fHeights.Add(lineHeight); } xPos = 0; lineHeight = 0; } int prevX = xPos; int prevY = yPos; if (!string.IsNullOrEmpty(chunk.Text)) { using (var font = new Font(defFont.FamilyName, chunk.Size, (sdFontStyle)chunk.Style)) { SizeF strSize = font.MeasureString(chunk.Text); chunk.Width = (int)strSize.Width; xPos += chunk.Width; if (xMax < xPos) { xMax = xPos; } int h = (int)strSize.Height; if (lineHeight < h) { lineHeight = h; } } if (!string.IsNullOrEmpty(chunk.URL)) { chunk.LinkRect = ExtRect.CreateBounds(prevX, prevY, xPos - prevX, lineHeight); } } } } fTextSize = new ExtSize(xMax + 2 * fBorderWidth, yPos + 2 * fBorderWidth); } finally { fAcceptFontChange = true; SetImageSize(fTextSize); } } catch (Exception ex) { Logger.LogWrite("HyperView.ArrangeText(): " + ex.Message); } }
private void ArrangeText() { try { fAcceptFontChange = false; fHeights.Clear(); Graphics gfx = CreateGraphics(); try { int xPos = 0; int yPos = 0; int xMax = 0; int lineHeight = 0; string text = fLines.Text; Font defFont = this.Font; var parser = new BBTextParser(AppHost.GfxProvider, defFont.SizeInPoints, new ColorHandler(fLinkColor), new ColorHandler(ForeColor)); parser.ParseText(fChunks, text); int line = -1; int chunksCount = fChunks.Count; for (int k = 0; k < chunksCount; k++) { BBTextChunk chunk = fChunks[k]; if (line != chunk.Line) { line = chunk.Line; if (line > 0) { yPos += lineHeight; fHeights.Add(lineHeight); } xPos = 0; lineHeight = 0; } if (!string.IsNullOrEmpty(chunk.Text)) { using (var font = new Font(defFont.Name, chunk.Size, (sdFontStyle)chunk.Style, defFont.Unit)) { SizeF strSize = gfx.MeasureString(chunk.Text, font); chunk.Width = (int)strSize.Width; xPos += chunk.Width; if (xMax < xPos) { xMax = xPos; } int h = (int)strSize.Height; if (lineHeight < h) { lineHeight = h; } } } } fTextSize = new ExtSize(xMax + 2 * fBorderWidth, yPos + 2 * fBorderWidth); } finally { gfx.Dispose(); fAcceptFontChange = true; AdjustViewport(fTextSize); } } catch (Exception ex) { Logger.LogWrite("HyperView.ArrangeText(): " + ex.Message); } }
private void ArrangeText() { try { SuspendLayout(); fAcceptFontChange = false; fHeights.Clear(); try { int xPos = 0; int yPos = 0; int xMax = 0; int lineHeight = 0; string text = fLines.Text; Font defFont = this.Font; float maxWidth = this.ClientSize.Width - (2 * fBorderWidth); text = SysUtils.StripHTML(text); var parser = new BBTextParser(AppHost.GfxProvider, defFont.Size, new ColorHandler(fLinkColor), new ColorHandler(TextColor)); parser.ParseText(fChunks, text); int line = -1; int chunksCount = fChunks.Count; int k = 0; while (k < chunksCount) { BBTextChunk chunk = fChunks[k]; bool recalcChunk = false; if (line != chunk.Line) { line = chunk.Line; if (line > 0) { yPos += lineHeight; fHeights.Add(lineHeight); } xPos = 0; lineHeight = 0; } int prevX = xPos; int prevY = yPos; string chunkStr = chunk.Text; if (!string.IsNullOrEmpty(chunkStr)) { using (var font = new Font(defFont.FamilyName, chunk.Size, (EDFontStyle)chunk.Style)) { SizeF strSize = font.MeasureString(chunkStr); if (fWordWrap && xPos + strSize.Width > maxWidth) { int lastPos = 0, prevPos = 0; string tempStr, prevStr = string.Empty; int sliceType = -1; while (true) { tempStr = GetSlice(chunkStr, ref lastPos, ref sliceType); strSize = font.MeasureString(tempStr); if (xPos + strSize.Width <= maxWidth) { prevStr = tempStr; prevPos = lastPos; } else { if (sliceType == 0) { // first word if (xPos == 0) { string tail = chunkStr.Substring(lastPos); SplitChunk(chunk, k, tempStr, tail, ref chunksCount); } else { ShiftChunks(k, chunksCount); recalcChunk = true; } break; } else if (sliceType == 1 || sliceType == 2) { // middle or tail word string tail = chunkStr.Substring(prevPos); SplitChunk(chunk, k, prevStr, tail, ref chunksCount); break; } else if (sliceType == 3) { // one first and last word, nothing to do break; } } } } strSize = font.MeasureString(chunk.Text); chunk.Width = (int)strSize.Width; xPos += chunk.Width; if (xMax < xPos) { xMax = xPos; } int h = (int)strSize.Height; if (lineHeight < h) { lineHeight = h; } } if (!string.IsNullOrEmpty(chunk.URL)) { chunk.LinkRect = ExtRect.CreateBounds(prevX, prevY, xPos - prevX, lineHeight); } } if (!recalcChunk) { k++; } } fTextSize = new ExtSize(xMax + 2 * fBorderWidth, yPos + 2 * fBorderWidth); } finally { fAcceptFontChange = true; SetImageSize(fTextSize); ResumeLayout(); } } catch (Exception ex) { Logger.WriteError("HyperView.ArrangeText()", ex); } }