public static void GenerateGlyphPlans(this GlyphLayout glyphLayout, char[] textBuffer, int startAt, int len, UnscaledGlyphPlanList list) { //generate glyph plan based on its current setting glyphLayout.Layout(textBuffer, startAt, len); ReadOutput(glyphLayout, list); }
public IUnscaledGlyphPlanList StringToGlyphPlanList(string s) { m_layout.Layout(s.ToCharArray(), 0, s.Length); var l = new UnscaledGlyphPlanList(); foreach (var g in m_layout.GetUnscaledGlyphPlanIter()) { l.Append(g); } return(l); }
/// <summary> /// read GlyphPlan latest layout output /// </summary> private static void ReadOutput(GlyphLayout glyphLayout, UnscaledGlyphPlanList outputGlyphPlanList) { Typeface typeface = glyphLayout.Typeface; var glyphPositions = glyphLayout._glyphPositions; //3.read back int finalGlyphCount = glyphPositions.Count; int cx = 0; short cy = 0; PositionTechnique posTech = glyphLayout.PositionTechnique; ushort prev_index = 0; for (int i = 0; i < finalGlyphCount; ++i) { GlyphPos glyphPos = glyphPositions[i]; switch (posTech) { default: throw new NotSupportedException(); case PositionTechnique.None: outputGlyphPlanList.Append(new UnscaledGlyphPlan( 0, glyphPos.glyphIndex, glyphPos.advanceW, cx, cy)); break; case PositionTechnique.OpenFont: outputGlyphPlanList.Append(new UnscaledGlyphPlan( 0, glyphPos.glyphIndex, glyphPos.advanceW, cx + glyphPos.xoffset, (short)(cy + glyphPos.yoffset))); break; case PositionTechnique.Kerning: if (i > 0) { cx += typeface.GetKernDistance(prev_index, glyphPos.glyphIndex); } outputGlyphPlanList.Append(new UnscaledGlyphPlan( 0, prev_index = glyphPos.glyphIndex, glyphPos.advanceW, cx, cy)); break; } cx += glyphPos.advanceW; } }
public static void CopyGlyphPlans(RenderVxFormattedString renderVx, UnscaledGlyphPlanList glyphPlans, float scale) { int n = glyphPlans.Count; //copy var renderVxGlyphPlans = new RenderVxGlyphPlan[n]; float acc_x = 0; float acc_y = 0; float x = 0; float y = 0; float g_x = 0; float g_y = 0; for (int i = 0; i < n; ++i) { UnscaledGlyphPlan glyphPlan = glyphPlans[i]; float ngx = acc_x + (float)Math.Round(glyphPlan.OffsetX * scale); float ngy = acc_y + (float)Math.Round(glyphPlan.OffsetY * scale); //NOTE: // -glyphData.TextureXOffset => restore to original pos // -glyphData.TextureYOffset => restore to original pos //-------------------------- g_x = (float)(x + ngx); //ideal x g_y = (float)(y + ngy); float g_w = (float)Math.Round(glyphPlan.AdvanceX * scale); acc_x += g_w; //g_x = (float)Math.Round(g_x); g_y = (float)Math.Floor(g_y); renderVxGlyphPlans[i] = new RenderVxGlyphPlan( glyphPlan.glyphIndex, g_x, g_y, g_w ); } renderVx.glyphList = renderVxGlyphPlans; }
public void PrepareStringForRenderVx(RenderVxFormattedString renderVx, char[] buffer, int startAt, int len) { int j = buffer.Length; //create temp buffer span that describe the part of a whole char buffer TextBufferSpan textBufferSpan = new TextBufferSpan(buffer, startAt, len); //ask text service to parse user input char buffer and create a glyph-plan-sequence (list of glyph-plan) //with specific request font GlyphPlanSequence glyphPlanSeq = _textServices.CreateGlyphPlanSeq(ref textBufferSpan, font); float scale = _fontAtlas.TargetTextureScale; int recommendLineSpacing = _fontAtlas.OriginalRecommendLineSpacing; //-------------------------- //TODO: //if (x,y) is left top //we need to adjust y again float x = 0; float y = 0; y -= ((recommendLineSpacing) * scale); renderVx.RecommmendLineSpacing = (int)(recommendLineSpacing * scale); // float scaleFromTexture = _finalTextureScale; TextureKind textureKind = _fontAtlas.TextureKind; //-------------------------- //TODO: review render steps //NOTE: // -glyphData.TextureXOffset => restore to original pos // -glyphData.TextureYOffset => restore to original pos // ideal_x = (float)(x + (glyph.x * scale - glyphData.TextureXOffset) * scaleFromTexture); // ideal_y = (float)(y + (glyph.y * scale - glyphData.TextureYOffset + srcRect.Height) * scaleFromTexture); //-------------------------- float g_x = 0; float g_y = 0; int baseY = (int)Math.Round(y); int n = glyphPlanSeq.len; int endBefore = glyphPlanSeq.startAt + n; //*** _glsx.SetAssociatedTextureInfo(_glBmp); // vboBufferList2.Clear(); indexList2.Clear(); float acc_x = 0; float acc_y = 0; for (int i = glyphPlanSeq.startAt; i < endBefore; ++i) { UnscaledGlyphPlanList glyphPlanList = GlyphPlanSequence.UnsafeGetInteralGlyphPlanList(glyphPlanSeq); UnscaledGlyphPlan glyph = glyphPlanList[i]; Typography.Rendering.TextureFontGlyphData glyphData; if (!_fontAtlas.TryGetGlyphDataByGlyphIndex(glyph.glyphIndex, out glyphData)) { //if no glyph data, we should render a missing glyph *** continue; } //-------------------------------------- //TODO: review precise height in float //-------------------------------------- PixelFarm.Drawing.Rectangle srcRect = ConvToRect(glyphData.Rect); float ngx = acc_x + (float)Math.Round(glyph.OffsetX * scale); float ngy = acc_y + (float)Math.Round(glyph.OffsetY * scale); //NOTE: // -glyphData.TextureXOffset => restore to original pos // -glyphData.TextureYOffset => restore to original pos //-------------------------- g_x = (float)(x + (ngx - glyphData.TextureXOffset) * scaleFromTexture); //ideal x g_y = (float)(y + (ngy - glyphData.TextureYOffset + srcRect.Height) * scaleFromTexture); acc_x += (float)Math.Round(glyph.AdvanceX * scale); //g_x = (float)Math.Round(g_x); g_y = (float)Math.Floor(g_y); switch (textureKind) { case TextureKind.Msdf: _glsx.DrawSubImageWithMsdf(_glBmp, ref srcRect, g_x, g_y, scaleFromTexture); break; case TextureKind.StencilGreyScale: //stencil gray scale with fill-color _glsx.DrawGlyphImageWithStecil(_glBmp, ref srcRect, g_x, g_y, scaleFromTexture); break; case TextureKind.Bitmap: _glsx.DrawSubImage(_glBmp, ref srcRect, g_x, g_y, scaleFromTexture); break; case TextureKind.StencilLcdEffect: _glsx.WriteVboToList( vboBufferList2, indexList2, ref srcRect, g_x, g_y, scaleFromTexture); break; } } //--------- DrawingGL.GLRenderVxFormattedString renderVxFormattedString = (DrawingGL.GLRenderVxFormattedString)renderVx; renderVxFormattedString.IndexArray = indexList2.ToArray(); renderVxFormattedString.VertexCoords = vboBufferList2.ToArray(); renderVxFormattedString.VertexCount = indexList2.Count; }
public void DrawString(char[] buffer, int startAt, int len, double x, double y) { _glsx.FontFillColor = painter.FontFillColor; int j = buffer.Length; //create temp buffer span that describe the part of a whole char buffer TextBufferSpan textBufferSpan = new TextBufferSpan(buffer, startAt, len); //ask text service to parse user input char buffer and create a glyph-plan-sequence (list of glyph-plan) //with specific request font GlyphPlanSequence glyphPlanSeq = _textServices.CreateGlyphPlanSeq(ref textBufferSpan, font); float scale = _fontAtlas.TargetTextureScale; int recommendLineSpacing = _fontAtlas.OriginalRecommendLineSpacing; //-------------------------- //TODO: //if (x,y) is left top //we need to adjust y again y -= ((_fontAtlas.OriginalRecommendLineSpacing) * scale); EnsureLoadGLBmp(); // float scaleFromTexture = _finalTextureScale; TextureKind textureKind = _fontAtlas.TextureKind; float g_x = 0; float g_y = 0; int baseY = (int)Math.Round(y); int n = glyphPlanSeq.len; int endBefore = glyphPlanSeq.startAt + n; //------------------------------------- _glsx.LoadTexture1(_glBmp); //------------------------------------- _vboBufferList.Clear(); //clear before use _indexList.Clear(); //clear before use float acc_x = 0; float acc_y = 0; for (int i = glyphPlanSeq.startAt; i < endBefore; ++i) { UnscaledGlyphPlanList glyphPlanList = GlyphPlanSequence.UnsafeGetInteralGlyphPlanList(glyphPlanSeq); UnscaledGlyphPlan glyph = glyphPlanList[i]; Typography.Rendering.TextureFontGlyphData glyphData; if (!_fontAtlas.TryGetGlyphDataByGlyphIndex(glyph.glyphIndex, out glyphData)) { //if no glyph data, we should render a missing glyph *** continue; } //-------------------------------------- //TODO: review precise height in float //-------------------------------------- PixelFarm.Drawing.Rectangle srcRect = ConvToRect(glyphData.Rect); float ngx = acc_x + (float)Math.Round(glyph.OffsetX * scale); float ngy = acc_y + (float)Math.Round(glyph.OffsetY * scale); //NOTE: // -glyphData.TextureXOffset => restore to original pos // -glyphData.TextureYOffset => restore to original pos //-------------------------- g_x = (float)(x + (ngx - glyphData.TextureXOffset) * scaleFromTexture); //ideal x g_y = (float)(y + (ngy - glyphData.TextureYOffset + srcRect.Height) * scaleFromTexture); acc_x += (float)Math.Round(glyph.AdvanceX * scale); //g_x = (float)Math.Round(g_x); g_y = (float)Math.Floor(g_y); switch (textureKind) { case TextureKind.Msdf: _glsx.DrawSubImageWithMsdf(_glBmp, ref srcRect, g_x, g_y, scaleFromTexture); break; case TextureKind.StencilGreyScale: //stencil gray scale with fill-color _glsx.DrawGlyphImageWithStecil(_glBmp, ref srcRect, g_x, g_y, scaleFromTexture); break; case TextureKind.Bitmap: _glsx.DrawSubImage(_glBmp, ref srcRect, g_x, g_y, scaleFromTexture); break; case TextureKind.StencilLcdEffect: _glsx.WriteVboToList( _vboBufferList, _indexList, ref srcRect, g_x, g_y, scaleFromTexture); break; } } //------- //we create vbo first //then render _glsx.DrawGlyphImageWithSubPixelRenderingTechnique3(_vboBufferList.ToArray(), _indexList.ToArray()); }
public override void DrawFromGlyphPlans(UnscaledGlyphPlanList glyphPlanList, int startAt, int len, float x, float y) { UpdateVisualOutputSettings(); //draw data in glyph plan //3. render each glyph float sizeInPoints = this.FontSizeInPoints; float scale = _currentTypeface.CalculateScaleToPixelFromPointSize(sizeInPoints); // _glyphMeshCollections.SetCacheInfo(this.Typeface, sizeInPoints, this.HintTechnique); //this draw a single line text span*** int endBefore = startAt + len; Graphics g = this.TargetGraphics; float acc_x = 0; float acc_y = 0; float g_x = 0; float g_y = 0; for (int i = startAt; i < endBefore; ++i) { UnscaledGlyphPlan glyphPlan = glyphPlanList[i]; //check if we have a cache of this glyph //if not -> create it GraphicsPath foundPath; if (!_glyphMeshCollections.TryGetCacheGlyph(glyphPlan.glyphIndex, out foundPath)) { //if not found then create a new one _currentGlyphPathBuilder.BuildFromGlyphIndex(glyphPlan.glyphIndex, sizeInPoints); _txToGdiPath.Reset(); _currentGlyphPathBuilder.ReadShapes(_txToGdiPath); foundPath = _txToGdiPath.ResultGraphicsPath; //register _glyphMeshCollections.RegisterCachedGlyph(glyphPlan.glyphIndex, foundPath); } //------ //then move pen point to the position we want to draw a glyph float ngx = acc_x + (float)Math.Round(glyphPlan.OffsetX * scale); float ngy = acc_y + (float)Math.Round(glyphPlan.OffsetY * scale); g_x = (x + (ngx)); g_y = (y + (ngy)); acc_x += (float)Math.Round(glyphPlan.AdvanceX * scale); //g_x = (float)Math.Round(g_x); g_y = (float)Math.Floor(g_y); g.TranslateTransform(g_x, g_y); if (FillBackground) { g.FillPath(_fillBrush, foundPath); } if (DrawOutline) { g.DrawPath(_outlinePen, foundPath); } //and then we reset back *** g.TranslateTransform(-g_x, -g_y); } }
void RenderAndShowMeasureBox() { bool flipY = chkFlipY.Checked; //set some Gdi+ props... g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.White); Typography.OpenFont.Typeface typeface = _currentTextPrinter.Typeface; Typography.OpenFont.TypefaceExtension2.UpdateAllCffGlyphBounds(typeface); float pxscale = typeface.CalculateScaleToPixelFromPointSize(_currentTextPrinter.FontSizeInPoints); int lineSpacing = (int)System.Math.Ceiling((double)typeface.CalculateLineSpacing(LineSpacingChoice.TypoMetric) * pxscale); if (flipY) { //credit: //http://stackoverflow.com/questions/1485745/flip-coordinates-when-drawing-to-control g.ScaleTransform(1.0F, -1.0F); // Flip the Y-Axis g.TranslateTransform(0.0F, -500); // Translate the drawing area accordingly } //-------------------------------- //textspan measurement sample //-------------------------------- _currentTextPrinter.HintTechnique = (HintTechnique)lstHintList.SelectedItem; _currentTextPrinter.PositionTechnique = (PositionTechnique)cmbPositionTech.SelectedItem; _currentTextPrinter.UpdateGlyphLayoutSettings(); //render at specific pos float x_pos = 0, y_pos = lineSpacing * 2; char[] textBuffer = txtInputChar.Text.ToCharArray(); //Example 1: this is a basic draw sample _currentTextPrinter.FillColor = Color.Black; _currentTextPrinter.TargetGraphics = g; _currentTextPrinter.DrawString( textBuffer, 0, textBuffer.Length, x_pos, y_pos ); // //-------------------------------------------------- //Example 2: print glyph plan to 'user' list-> then draw it (or hold it/ not draw) //you can create you own class to hold userGlyphPlans.*** //2.1 _reusableUnscaledGlyphPlanList.Clear(); _currentTextPrinter.GenerateGlyphPlan(textBuffer, 0, textBuffer.Length, _reusableUnscaledGlyphPlanList); //2.2 //and we can print the formatted glyph plan later. y_pos -= lineSpacing;//next line _currentTextPrinter.FillColor = Color.Red; _currentTextPrinter.DrawFromGlyphPlans( new GlyphPlanSequence(_reusableUnscaledGlyphPlanList), x_pos, y_pos ); //Example 3: MeasureString UnscaledGlyphPlanList glyphPlans = new UnscaledGlyphPlanList(); _currentTextPrinter.GlyphLayoutMan.GenerateUnscaledGlyphPlans(glyphPlans); MeasuredStringBox strBox = _currentTextPrinter.GlyphLayoutMan.LayoutAndMeasureString( textBuffer, 0, textBuffer.Length, _currentTextPrinter.FontSizeInPoints); int j = glyphPlans.Count; float backup_xpos = x_pos; for (int i = 0; i < j; ++i) { UnscaledGlyphPlan glyphPlan = glyphPlans[i]; Typography.OpenFont.Glyph glyph = typeface.GetGlyphByIndex(glyphPlan.glyphIndex); // Typography.OpenFont.Bounds b = glyph.Bounds; // float xmin = b.XMin * pxscale; float ymin = b.YMin * pxscale; // float xmax = b.XMax * pxscale; float ymax = b.YMax * pxscale; // float glyph_x = x_pos + glyphPlan.OffsetX; g.DrawRectangle(Pens.Red, glyph_x + xmin, y_pos + ymin, xmax - xmin, ymax - ymin); x_pos += glyphPlan.AdvanceX * pxscale; } x_pos = backup_xpos; g.FillRectangle(Brushes.Red, new RectangleF(0, 0, 5, 5));//reference point(0,0) g.FillRectangle(Brushes.Green, new RectangleF(x_pos, y_pos, 3, 3)); float x_pos2 = x_pos + strBox.width + 10; g.DrawRectangle(Pens.Black, x_pos, y_pos + strBox.DescendingInPx, strBox.width, strBox.ClipHeightInPx); g.DrawRectangle(Pens.Red, x_pos, y_pos + strBox.DescendingInPx, strBox.width, strBox.LineSpaceInPx); g.DrawLine(Pens.Blue, x_pos, y_pos, x_pos2, y_pos); //baseline g.DrawLine(Pens.Green, x_pos, y_pos + strBox.DescendingInPx, x_pos2, y_pos + strBox.DescendingInPx); //descending g.DrawLine(Pens.Magenta, x_pos, y_pos + strBox.AscendingInPx, x_pos2, y_pos + strBox.AscendingInPx); //ascending ////------------ ////draw another line (for reference) y_pos -= lineSpacing;//next line _currentTextPrinter.FillColor = Color.Black; _currentTextPrinter.DrawFromGlyphPlans( new GlyphPlanSequence(_reusableUnscaledGlyphPlanList), x_pos, y_pos ); //transform back if (flipY) { g.ScaleTransform(1.0F, -1.0F); // Flip the Y-Axis g.TranslateTransform(0.0F, -500); // Translate the drawing area accordingly } //--------- //txtMsgInfo.Text = "choice:" + choice.ToString() + "=" + lineSpacing.ToString(); }
void RenderAndShowMeasureBox() { bool flipY = chkFlipY.Checked; //set some Gdi+ props... _g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; _g.Clear(Color.White); Typography.OpenFont.Typeface typeface = _currentTextPrinter.Typeface; Typography.OpenFont.Extensions.TypefaceExtensions.UpdateAllCffGlyphBounds(typeface); float pxscale = typeface.CalculateScaleToPixelFromPointSize(_currentTextPrinter.FontSizeInPoints); int lineSpacing = (int)System.Math.Ceiling((double)typeface.CalculateLineSpacing(LineSpacingChoice.TypoMetric) * pxscale); if (flipY) { //credit: //http://stackoverflow.com/questions/1485745/flip-coordinates-when-drawing-to-control _g.ScaleTransform(1.0F, -1.0F); // Flip the Y-Axis _g.TranslateTransform(0.0F, -500); // Translate the drawing area accordingly } //-------------------------------- //textspan measurement sample //-------------------------------- _currentTextPrinter.HintTechnique = (HintTechnique)lstHintList.SelectedItem; _currentTextPrinter.PositionTechnique = (PositionTechnique)cmbPositionTech.SelectedItem; _currentTextPrinter.UpdateGlyphLayoutSettings(); //render at specific pos float x_pos = 0, y_pos = lineSpacing * 2; char[] textBuffer = txtInputChar.Text.ToCharArray(); //Example 1: this is a basic draw sample _currentTextPrinter.FillColor = Color.Black; _currentTextPrinter.TargetGraphics = _g; //_currentTextPrinter.DrawString( // textBuffer, // 0, // textBuffer.Length, // x_pos, // y_pos // ); // //-------------------------------------------------- //Example 2: print glyph plan to 'user' list-> then draw it (or hold it/ not draw) //you can create you own class to hold userGlyphPlans.*** //2.1 if (chkEnableMultiTypefaces.Checked) { _ftmGlyphPlansList.Clear(); _currentTextPrinter.GenerateGlyphPlans(textBuffer, 0, textBuffer.Length, _ftmGlyphPlansList); //2.2 //and we can print the formatted glyph plan later. y_pos -= lineSpacing;//next line _currentTextPrinter.FillColor = Color.Red; _currentTextPrinter.DrawFromFormattedGlyphPlans( _ftmGlyphPlansList, x_pos, y_pos ); //-------------------------------------------------- //Example 3: MeasureString //3.1 use data in formatted_glyph_plan_list to measure string => OK, OR //3.2 use another 'light-weight' text printer to measure string (without drawing actual string) //3.1 _currentTextPrinter.MeasureGlyphPlanList(_ftmGlyphPlansList, out int measureWidth); _g.DrawRectangle(Pens.Red, x_pos, y_pos, measureWidth, 30); float baseline = y_pos; //3.2 draw each glyph bounds { int m = _ftmGlyphPlansList.Count; for (int i = 0; i < m; ++i) { FormattedGlyphPlanSeq seq = _ftmGlyphPlansList[i]; ResolvedFont resolvedFont = seq.ResolvedFont; Typeface localTypeface = resolvedFont.Typeface; pxscale = resolvedFont.GetScaleToPixelFromPointInSize(); GlyphPlanSequence glyph_seq = seq.Seq; var snapToPxScale = new GlyphPlanSequenceSnapPixelScaleLayout(seq.Seq, 0, glyph_seq.Count, pxscale); x_pos += (resolvedFont.WhitespaceWidth * seq.PrefixWhitespaceCount); int nn = 0;/// while (snapToPxScale.Read()) { //float cx = (float)Math.Round(snapToPxScale.ExactX + x_pos); //float cy = (float)Math.Floor(snapToPxScale.ExactY + baseline); UnscaledGlyphPlan glyphPlan = glyph_seq[nn]; Glyph glyph = localTypeface.GetGlyph(glyphPlan.glyphIndex); Bounds b = glyph.Bounds; // float xmin = b.XMin * pxscale; float ymin = b.YMin * pxscale; // float xmax = b.XMax * pxscale; float ymax = b.YMax * pxscale; // float glyph_x = x_pos + (glyphPlan.OffsetX * pxscale); _g.DrawRectangle(Pens.Red, glyph_x + xmin, y_pos + ymin, xmax - xmin, ymax - ymin); x_pos += glyphPlan.AdvanceX * pxscale; nn++; } x_pos += (resolvedFont.WhitespaceWidth * seq.PostfixWhitespaceCount); } } _ftmGlyphPlansList.Clear(); } else { _reusableUnscaledGlyphPlanList.Clear(); _currentTextPrinter.GenerateGlyphPlans(textBuffer, 0, textBuffer.Length, _reusableUnscaledGlyphPlanList); //2.2 //and we can print the formatted glyph plan later. y_pos -= lineSpacing;//next line _currentTextPrinter.FillColor = Color.Red; // GlyphPlanSequence seq = new GlyphPlanSequence(_reusableUnscaledGlyphPlanList); _currentTextPrinter.DrawFromGlyphPlans( seq, x_pos, y_pos ); //-------------------------------------------------- //Example 3: MeasureString float descending_px = _currentTextPrinter.FontDescedingPx; float ascending_px = _currentTextPrinter.FontAscendingPx; float lineSpacing_px = _currentTextPrinter.FontLineSpacingPx; float clippedHeight_px = _currentTextPrinter.FontClipHeightPx; _currentTextPrinter.MeasureGlyphPlanSeq(seq, out int textspanW); UnscaledGlyphPlanList glyphPlans = new UnscaledGlyphPlanList(); _currentTextPrinter.GenerateGlyphPlans(textBuffer, 0, textBuffer.Length, glyphPlans); int j = glyphPlans.Count; float backup_xpos = x_pos; for (int i = 0; i < j; ++i) { UnscaledGlyphPlan glyphPlan = glyphPlans[i]; Glyph glyph = typeface.GetGlyph(glyphPlan.glyphIndex); // Bounds b = glyph.Bounds; // float xmin = b.XMin * pxscale; float ymin = b.YMin * pxscale; // float xmax = b.XMax * pxscale; float ymax = b.YMax * pxscale; // float glyph_x = x_pos + (glyphPlan.OffsetX * pxscale); _g.DrawRectangle(Pens.Red, glyph_x + xmin, y_pos + ymin, xmax - xmin, ymax - ymin); x_pos += glyphPlan.AdvanceX * pxscale; } x_pos = backup_xpos; _g.FillRectangle(Brushes.Red, new RectangleF(0, 0, 5, 5));//reference point(0,0) _g.FillRectangle(Brushes.Green, new RectangleF(x_pos, y_pos, 3, 3)); float x_pos2 = x_pos + textspanW + 10; _g.DrawRectangle(Pens.Black, x_pos, y_pos + descending_px, textspanW, clippedHeight_px); _g.DrawRectangle(Pens.Red, x_pos, y_pos + descending_px, textspanW, lineSpacing_px); _g.DrawLine(Pens.Blue, x_pos, y_pos, x_pos2, y_pos); //baseline _g.DrawLine(Pens.Green, x_pos, y_pos + descending_px, x_pos2, y_pos + descending_px); //descending _g.DrawLine(Pens.Magenta, x_pos, y_pos + ascending_px, x_pos2, y_pos + ascending_px); //ascending ////------------ ////draw another line (for reference) y_pos -= lineSpacing;//next line _currentTextPrinter.FillColor = Color.Black; _currentTextPrinter.DrawFromGlyphPlans( new GlyphPlanSequence(_reusableUnscaledGlyphPlanList), x_pos, y_pos ); } //transform back if (flipY) { _g.ScaleTransform(1.0F, -1.0F); // Flip the Y-Axis _g.TranslateTransform(0.0F, -500); // Translate the drawing area accordingly } //--------- //txtMsgInfo.Text = "choice:" + choice.ToString() + "=" + lineSpacing.ToString(); }
public void CalculateUserCharGlyphAdvancePos(ref TextBufferSpan textBufferSpan, ILineSegmentList lineSegs, RequestFont font, int[] outputUserInputCharAdvance, out int outputTotalW, out int lineHeight) { //layout //from font //resolve for typeface // Typeface typeface = ResolveTypeface(font); _typographyTxtServices.SetCurrentFont(typeface, font.SizeInPoints); MyLineSegmentList mylineSegs = (MyLineSegmentList)lineSegs; float scale = typeface.CalculateScaleToPixelFromPointSize(font.SizeInPoints); outputTotalW = 0; int j = mylineSegs.Count; int pos = 0; //start at 0 _reusableTextBuffer.SetRawCharBuffer(textBufferSpan.GetRawCharBuffer()); for (int i = 0; i < j; ++i) { //userGlyphPlanList.Clear(); //userCharToGlyphMapList.Clear(); //get each segment MyLineSegment lineSeg = mylineSegs.GetSegment(i); //each line seg may has different script lang _typographyTxtServices.CurrentScriptLang = lineSeg.scriptLang; // //CACHING ...., reduce number of GSUB/GPOS // //we cache used line segment for a while //we ask for caching context for a specific typeface and font size GlyphPlanSequence seq = _typographyTxtServices.GetUnscaledGlyphPlanSequence(_reusableTextBuffer, lineSeg.StartAt, lineSeg.Length); UnscaledGlyphPlanList planList = GlyphPlanSequence.UnsafeGetInteralGlyphPlanList(seq); //IMPORTANT //num of glyph may more or less than original user input char buffer // int endAt = seq.startAt + seq.len; int seq_startAt = seq.startAt; //float g_x = 0; //float g_y = 0; //int baseY = (int)Math.Round(y); //int n = glyphPlanSeq.len; //int endBefore = glyphPlanSeq.startAt + n; ////*** //_glsx.SetAssociatedTextureInfo(_glBmp); //List<float> vboBufferList = new List<float>(); //List<ushort> indexList = new List<ushort>(); //for (int i = glyphPlanSeq.startAt; i < endBefore; ++i) //{ // GlyphPlanList glyphPlanList = GlyphPlanSequence.UnsafeGetInteralGlyphPlanList(glyphPlanSeq); // GlyphPlan glyph = glyphPlanList[i]; // Typography.Rendering.TextureFontGlyphData glyphData; // if (!_fontAtlas.TryGetGlyphDataByCodePoint(glyph.glyphIndex, out glyphData)) // { // //if no glyph data, we should render a missing glyph *** // continue; // } // //if (scaleFromTexture != 1) // //{ // //} // //-------------------------------------- // //TODO: review precise height in float // //-------------------------------------- // PixelFarm.Drawing.Rectangle srcRect = ConvToRect(glyphData.Rect); // g_x = (float)(x + (glyph.ExactX * scale - glyphData.TextureXOffset) * scaleFromTexture); //ideal x // g_y = (float)(y + (glyph.ExactY * scale - glyphData.TextureYOffset + srcRect.Height) * scaleFromTexture); // //for sharp glyph // //we adjust g_x,g_y to integer value // //float g_y2 = (float)Math.Floor(g_y); // g_x = (float)Math.Round(g_x); // g_y = (float)Math.Floor(g_y); // switch (textureKind) // { // case TextureKind.Msdf: // _glsx.DrawSubImageWithMsdf(_glBmp, // ref srcRect, // g_x, // g_y, // scaleFromTexture); // break; // case TextureKind.StencilGreyScale: // //stencil gray scale with fill-color // _glsx.DrawGlyphImageWithStecil(_glBmp, // ref srcRect, // g_x, // g_y, // scaleFromTexture); // break; // case TextureKind.Bitmap: // _glsx.DrawSubImage(_glBmp, // ref srcRect, // g_x, // g_y, // scaleFromTexture); // break; // case TextureKind.StencilLcdEffect: // _glsx.WriteVboToList( // vboBufferList, // indexList, // ref srcRect, // g_x, // g_y, // scaleFromTexture); // break; // } //} ////--------- for (int s = seq_startAt; s < endAt; ++s) { UnscaledGlyphPlan glyphPlan = planList[s]; double actualAdvX = glyphPlan.AdvanceX; outputTotalW += outputUserInputCharAdvance[pos + glyphPlan.input_cp_offset] += (int)Math.Round(actualAdvX * scale); } pos += lineSeg.Length; } // lineHeight = (int)Math.Round(typeface.CalculateRecommendLineSpacing() * scale); _reusableTextBuffer.SetRawCharBuffer(null); }
private void cmdMeasureTextSpan_Click(object sender, System.EventArgs e) { //set some Gdi+ props... g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.White); //credit: //http://stackoverflow.com/questions/1485745/flip-coordinates-when-drawing-to-control g.ScaleTransform(1.0F, -1.0F); // Flip the Y-Axis g.TranslateTransform(0.0F, -(float)300); // Translate the drawing area accordingly //-------------------------------- //textspan measurement sample //-------------------------------- _currentTextPrinter.HintTechnique = (HintTechnique)lstHintList.SelectedItem; _currentTextPrinter.PositionTechnique = (PositionTechnique)cmbPositionTech.SelectedItem; //render at specific pos float x_pos = 0, y_pos = 100; char[] textBuffer = txtInputChar.Text.ToCharArray(); //Example 1: this is a basic draw sample _currentTextPrinter.FillColor = Color.Black; _currentTextPrinter.TargetGraphics = g; _currentTextPrinter.DrawString( textBuffer, 0, textBuffer.Length, x_pos, y_pos ); // //-------------------------------------------------- //Example 2: print glyph plan to 'user' list-> then draw it (or hold it/ not draw) //you can create you own class to hold userGlyphPlans.*** //2.1 _reusableUnscaledGlyphPlanList.Clear(); _currentTextPrinter.GenerateGlyphPlan(textBuffer, 0, textBuffer.Length, _reusableUnscaledGlyphPlanList); //2.2 //and we can print the formatted glyph plan later. y_pos -= _currentTextPrinter.FontLineSpacingPx; _currentTextPrinter.FillColor = Color.Red; _currentTextPrinter.DrawFromGlyphPlans( new GlyphPlanSequence(_reusableUnscaledGlyphPlanList), x_pos, y_pos ); //Example 3: MeasureString Typography.OpenFont.Typeface typeface = _currentTextPrinter.Typeface; UnscaledGlyphPlanList userGlyphPlans = new UnscaledGlyphPlanList(); _currentTextPrinter.GlyphLayoutMan.GenerateUnscaledGlyphPlans(userGlyphPlans); MeasuredStringBox strBox = new MeasuredStringBox(); throw new System.NotSupportedException(); //_currentTextPrinter.GlyphLayoutMan.LayoutAndMeasureString( // textBuffer, 0, textBuffer.Length, // _currentTextPrinter.FontSizeInPoints, // true, // userGlyphPlans); float x_pos2 = x_pos + strBox.width + 10; g.DrawRectangle(Pens.Red, x_pos, y_pos + strBox.descending, strBox.width, strBox.CalculateLineHeight()); g.DrawLine(Pens.Blue, x_pos, y_pos, x_pos2, y_pos); //baseline g.DrawLine(Pens.Green, x_pos, y_pos + strBox.descending, x_pos2, y_pos + strBox.descending); //descending g.DrawLine(Pens.Magenta, x_pos, y_pos + strBox.ascending, x_pos2, y_pos + strBox.ascending); //ascending Typography.OpenFont.TypefaceExtension2.UpdateAllCffGlyphBounds(typeface); float pxscale = typeface.CalculateScaleToPixelFromPointSize(_currentTextPrinter.FontSizeInPoints); int j = userGlyphPlans.Count; for (int i = 0; i < j; ++i) { UnscaledGlyphPlan glyphPlan = userGlyphPlans[i]; Typography.OpenFont.Glyph glyph = typeface.GetGlyphByIndex(glyphPlan.glyphIndex); // Typography.OpenFont.Bounds b = glyph.Bounds; // float xmin = b.XMin * pxscale; float ymin = b.YMin * pxscale; // float xmax = b.XMax * pxscale; float ymax = b.YMax * pxscale; // float glyph_x = x_pos + glyphPlan.OffsetX; g.DrawRectangle(Pens.Red, glyph_x + xmin, y_pos + ymin, xmax - xmin, ymax - ymin); } //------------ _currentTextPrinter.FillColor = Color.Black; //transform back g.ScaleTransform(1.0F, -1.0F); // Flip the Y-Axis g.TranslateTransform(0.0F, -(float)300); // Translate the drawing area accordingly }
public override void DrawFromGlyphPlans(UnscaledGlyphPlanList glyphPlanList, int startAt, int len, float x, float y) { Painter painter = this.TargetCanvasPainter; if (StartDrawOnLeftTop) { //version 2 //offset y down y += this.FontLineSpacingPx; } //Typeface typeface = _glyphPathBuilder.Typeface; //3. layout glyphs with selected layout technique //TODO: review this again, we should use pixel? float fontSizePoint = this.FontSizeInPoints; float scale = _currentTypeface.CalculateScaleToPixelFromPointSize(fontSizePoint); //4. render each glyph float ox = painter.OriginX; float oy = painter.OriginY; int endBefore = startAt + len; Typography.OpenFont.Tables.COLR colrTable = _currentTypeface.COLRTable; Typography.OpenFont.Tables.CPAL cpalTable = _currentTypeface.CPALTable; bool hasColorGlyphs = (colrTable != null) && (cpalTable != null); //--------------------------------------------------- _glyphMeshStore.SetFont(_currentTypeface, fontSizePoint); //--------------------------------------------------- float g_x = 0; float g_y = 0; float baseY = (int)y; if (!hasColorGlyphs) { bool savedUseLcdMode = painter.UseSubPixelLcdEffect; //save,restore later RenderQualtity savedRederQuality = painter.RenderQuality; painter.RenderQuality = RenderQualtity.HighQuality; painter.UseSubPixelLcdEffect = true; Agg.Transform.Affine flipY = Agg.Transform.Affine.NewMatix( Agg.Transform.AffinePlan.Scale(1, -1)); //flip Y VertexStore reusableVxs = new VertexStore(); float acc_x = 0; //acummulate x float acc_y = 0; //acummulate y for (int i = startAt; i < endBefore; ++i) { //----------------------------------- //TODO: review here *** //PERFORMANCE revisit here //if we have create a vxs we can cache it for later use? //----------------------------------- UnscaledGlyphPlan glyphPlan = glyphPlanList[i]; float ngx = acc_x + (float)Math.Round(glyphPlan.OffsetX * scale); float ngy = acc_y + (float)Math.Round(glyphPlan.OffsetY * scale); acc_x += (float)Math.Round(glyphPlan.AdvanceX * scale); g_x = ngx; g_y = ngy; painter.SetOrigin(g_x, g_y); //----------------------------------- //invert each glyph //version 3: reusableVxs.Clear(); VertexStore vxs = _glyphMeshStore.GetGlyphMesh(glyphPlan.glyphIndex); flipY.TransformToVxs(vxs, reusableVxs); painter.Fill(reusableVxs); //version2; //VertexStore vsx = _glyphMeshStore.GetGlyphMesh(glyphPlan.glyphIndex); //_vxs1 = _invertY.TransformToVxs(vsx, _vxs1); //painter.Fill(_vxs1); //_vxs1.Clear(); //version1 //painter.Fill(_glyphMeshStore.GetGlyphMesh(glyphPlan.glyphIndex)); } //restore painter.RenderQuality = savedRederQuality; painter.UseSubPixelLcdEffect = savedUseLcdMode; } else { //------------- //this glyph has color information //------------- Color originalFillColor = painter.FillColor; float acc_x = 0; float acc_y = 0; for (int i = startAt; i < endBefore; ++i) { UnscaledGlyphPlan glyphPlan = glyphPlanList[i]; float ngx = acc_x + (float)Math.Round(glyphPlan.OffsetX * scale); float ngy = acc_y + (float)Math.Round(glyphPlan.OffsetY * scale); g_x = ngx; g_y = ngy; acc_x += (float)Math.Round(glyphPlan.AdvanceX * scale); painter.SetOrigin(g_x, g_y); //----------------------------------- ushort colorLayerStart; if (colrTable.LayerIndices.TryGetValue(glyphPlan.glyphIndex, out colorLayerStart)) { //TODO: optimize this //we found color info for this glyph ushort colorLayerCount = colrTable.LayerCounts[glyphPlan.glyphIndex]; byte r, g, b, a; for (int c = colorLayerStart; c < colorLayerStart + colorLayerCount; ++c) { ushort gIndex = colrTable.GlyphLayers[c]; int palette = 0; // FIXME: assume palette 0 for now cpalTable.GetColor( cpalTable.Palettes[palette] + colrTable.GlyphPalettes[c], //index out r, out g, out b, out a); //----------- painter.FillColor = new Color(r, g, b);//? a component painter.Fill(_glyphMeshStore.GetGlyphMesh(gIndex)); } } else { //----------------------------------- //TODO: review here *** //PERFORMANCE revisit here //if we have create a vxs we can cache it for later use? //----------------------------------- painter.Fill(_glyphMeshStore.GetGlyphMesh(glyphPlan.glyphIndex)); } } painter.FillColor = originalFillColor; //restore color } //restore prev origin painter.SetOrigin(ox, oy); }
public void DrawFromGlyphPlans(UnscaledGlyphPlanList glyphPlanList, float x, float y) { DrawFromGlyphPlans(glyphPlanList, 0, glyphPlanList.Count, x, y); }
/// <summary> /// draw glyph plan list at (xpos,ypos) of baseline /// </summary> /// <param name="glyphPlanList"></param> /// <param name="x"></param> /// <param name="y"></param> public abstract void DrawFromGlyphPlans(UnscaledGlyphPlanList glyphPlanList, int startAt, int len, float x, float y);