public DrawSubImageWithMsdf ( |
||
bmp | ||
r | PixelFarm | |
targetLeft | float | |
targetTop | float | |
return | void |
public void DrawString(char[] buffer, int startAt, int len, double x, double y) { int j = buffer.Length; //resolve font from painter? glyphPlans.Clear(); _glyphLayout.Layout(_typeface, buffer, startAt, len, glyphPlans); float scale = _typeface.CalculateToPixelScaleFromPointSize(font.SizeInPoints); //-------------------------- //TODO: //if (x,y) is left top //we need to adjust y again y -= (_typeface.Ascender - _typeface.Descender + _typeface.LineGap) * scale; int n = glyphPlans.Count; EnsureLoadGLBmp(); // float scaleFromTexture = _finalTextureScale; Typography.Rendering.TextureKind textureKind = simpleFontAtlas.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); //-------------------------- GlyphPosPixelSnapKind x_snap = this.GlyphPosPixelSnapX; GlyphPosPixelSnapKind y_snap = this.GlyphPosPixelSnapY; float g_x = 0; float g_y = 0; int baseY = (int)Math.Round(y); for (int i = 0; i < n; ++i) { GlyphPlan glyph = glyphPlans[i]; Typography.Rendering.TextureFontGlyphData glyphData; if (!simpleFontAtlas.TryGetGlyphDataByCodePoint(glyph.glyphIndex, out glyphData)) { continue; } //-------------------------------------- //TODO: review precise height in float //-------------------------------------- PixelFarm.Drawing.Rectangle srcRect = ConvToRect(glyphData.Rect); switch (x_snap) { default: throw new NotSupportedException(); case GlyphPosPixelSnapKind.Integer: { g_x = (float)(x + (glyph.x * scale - glyphData.TextureXOffset) * scaleFromTexture); //ideal x int floor_x = (int)g_x; //round to int 0,1 if (g_x - floor_x >= (1f / 2f)) { g_x = floor_x + 1; } else { g_x = floor_x; } } break; case GlyphPosPixelSnapKind.Half: { g_x = (float)(x + (glyph.x * scale - glyphData.TextureXOffset) * scaleFromTexture); //ideal x //adjust int floor_x = (int)g_x; //round to int 0, 0.5,1.0 if (g_x - floor_x >= (2f / 3f)) { g_x = floor_x + 1; } else if (g_x - floor_x >= (1f / 3f)) { g_x = floor_x + 0.5f; } else { g_x = floor_x; } } break; case GlyphPosPixelSnapKind.None: g_x = (float)(x + (glyph.x * scale - glyphData.TextureXOffset) * scaleFromTexture); break; } // switch (y_snap) { default: throw new NotSupportedException(); case GlyphPosPixelSnapKind.Integer: //use baseY not y { g_y = (float)((glyph.y * scale - glyphData.TextureYOffset + srcRect.Height) * scaleFromTexture); int floor_y = (int)g_y; //round to int 0,1 if (g_y - floor_y >= (1f / 2f)) { g_y = floor_y + 1; } else { g_y = floor_y; } g_y = baseY + g_y; } break; case GlyphPosPixelSnapKind.Half: //review here //use baseY not y { g_y = (float)((glyph.y * scale - glyphData.TextureYOffset + srcRect.Height) * scaleFromTexture); int floor_y = (int)g_y; //round to int 0, 0.5,1.0 if (g_y - floor_y >= (2f / 3f)) { g_y = floor_y + 1; } else if (g_x - floor_y >= (1f / 3f)) { g_y = floor_y + 0.5f; } else { g_y = floor_y; } g_y = baseY + g_y; } break; case GlyphPosPixelSnapKind.None: //use Y not baseY g_y = (float)(y + (glyph.y * scale - glyphData.TextureYOffset + srcRect.Height) * scaleFromTexture); break; } switch (textureKind) { case Typography.Rendering.TextureKind.Msdf: canvas2d.DrawSubImageWithMsdf(_glBmp, ref srcRect, g_x, g_y, scaleFromTexture); break; case Typography.Rendering.TextureKind.AggGrayScale: canvas2d.DrawSubImage(_glBmp, ref srcRect, g_x, g_y, scaleFromTexture); break; case Typography.Rendering.TextureKind.AggSubPixel: canvas2d.DrawGlyphImageWithSubPixelRenderingTechnique(_glBmp, ref srcRect, g_x, g_y, scaleFromTexture); break; } } }
public void DrawString(char[] buffer, double x, double y) { int j = buffer.Length; int buffsize = j * 2; //resolve font from painter? ActualFont fontImp = ff.GetFontAtPointsSize(font.SizeInPoints); var tt = (Typography.OpenFont.Typeface)ff.GetInternalTypeface(); List <GlyphPlan> glyphPlans = new List <GlyphPlan>(); _glyphLayout.Layout(tt, font.SizeInPoints, buffer, glyphPlans); // //un-test version //ActualFont fontImp = nativeFontStore.GetResolvedNativeFont(painter.CurrentFont); //if (properGlyphs == null) //{ // properGlyphs = new ProperGlyph[buffsize]; // TextShapingService.GetGlyphPos(fontImp, buffer, 0, buffsize, properGlyphs); //} //TODO: implement msdf texture //double xpos = x; //for (int i = 0; i < buffsize; ++i) //{ // uint codepoint = properGlyphs[i].codepoint; // if (codepoint == 0) // { // break; // } // //------------------------------------------------------------- // FontGlyph glyph = fontImp.GetGlyphByIndex(codepoint); // //glyph image32 // //------------------------------------------------------------- // GLBitmap bmp = new GLBitmap(new LazyAggBitmapBufferProvider(glyph.glyphImage32)); // var left = glyph.glyphMatrix.img_horiBearingX; // this.canvas2d.DrawImage(bmp, // (float)(xpos + (left >> 6)), // (float)(y + (glyph.glyphMatrix.bboxYmin >> 6))); // int w = (glyph.glyphMatrix.advanceX) >> 6; // xpos += (w); // bmp.Dispose(); //temp here // //------------------------------------------------------------- //} //------------------------------------- //msdf texture version double xpos = x; int n = glyphPlans.Count; Typography.Rendering.GlyphImage glyphImage = simpleFontAtlas.TotalGlyph; GLBitmap glBmp = new GLBitmap(glyphImage.Width, glyphImage.Height, glyphImage.GetImageBuffer(), false); float c_x = (float)x; float c_y = (float)y; //int left = ((int)(glyph.glyphMatrix.img_horiBearingX * scale) >> 6); int left = 0; //float baseline = c_y - 24;//eg line height= 24 //create a list float baseline = c_y - 24;//eg line height= 24 //create a list bool isFlipY = canvas2d.FlipY; if (!isFlipY) { canvas2d.FlipY = true; } for (int i = 0; i < n; ++i) { GlyphPlan glyph = glyphPlans[i]; Typography.Rendering.TextureFontGlyphData glyphData; if (!simpleFontAtlas.GetRectByCodePoint(glyph.glyphIndex, out glyphData)) { //Rectangle r = glyphData.Rect; //float x_min = glyphData.BBoxXMin / 64; ////draw each glyph at specific position ////_canvas.DrawSubImageWithMsdf(glBmp, ref r, c_x + x_min, (float)(baseline + r.Height)); //_canvas.DrawSubImageWithMsdf(glBmp, ref r, c_x + x_min, (float)(baseline + r.Height)); ////c_x += r.Width - 10; //c_x += (glyphData.AdvanceX / 64); continue; } //found PixelFarm.Drawing.Rectangle r = ConvToRect(glyphData.Rect); //test draw full msdf gen img //canvas2d.DrawImage(glBmp, c_x + left, (float)(baseline + ((int)(glyphData.ImgHeight)))); canvas2d.DrawSubImageWithMsdf(glBmp, ref r, c_x + left, (float)(baseline + ((int)(glyphData.ImgHeight))), 1.0f); c_x += glyph.advX; } canvas2d.FlipY = isFlipY; glBmp.Dispose(); //temp here //draw with texture printer *** //char[] chars = text.ToCharArray(); //int j = chars.Length; //int buffsize = j * 2; ////get kerning list ////get actual font for this canvas //TextureFont currentFont = _currentTextureFont; //SimpleFontAtlas fontAtlas = currentFont.FontAtlas; //ProperGlyph[] properGlyphs = new ProperGlyph[buffsize]; //TextShapingService.GetGlyphPos(currentFont, chars, 0, buffsize, properGlyphs); //GLBitmap glBmp = (GLBitmap)currentFont.GLBmp; //if (glBmp == null) //{ // //create glbmp // GlyphImage glyphImage = fontAtlas.TotalGlyph; // int[] buffer = glyphImage.GetImageBuffer(); // glBmp = new GLBitmap(glyphImage.Width, glyphImage.Height, buffer, false); //} ////int j = chars.Length; //// //float c_x = (float)x; //float c_y = (float)y; ////TODO: review here *** ////----------------- ////1. layout each glyph before render *** //// //float baseline = c_y - 24;//eg line height= 24 //create a list ////-------------- //List<float> coords = new List<float>(); //float scale = 1f; //for (int i = 0; i < buffsize; ++i) //{ // ProperGlyph glyph1 = properGlyphs[i]; // uint codepoint = properGlyphs[i].codepoint; // if (codepoint == 0) // { // break; // } // //-------------------------------- // //if (codepoint == 1173 && i > 1) // //{ // // //check prev code point // // codepoint = 1168; // //} // //-------------------------------- // TextureFontGlyphData glyphData; // if (!fontAtlas.GetRectByCodePoint((int)codepoint, out glyphData)) // { // //Rectangle r = glyphData.Rect; // //float x_min = glyphData.BBoxXMin / 64; // ////draw each glyph at specific position // ////_canvas.DrawSubImageWithMsdf(glBmp, ref r, c_x + x_min, (float)(baseline + r.Height)); // //_canvas.DrawSubImageWithMsdf(glBmp, ref r, c_x + x_min, (float)(baseline + r.Height)); // ////c_x += r.Width - 10; // //c_x += (glyphData.AdvanceX / 64); // continue; // } // FontGlyph glyph = currentFont.GetGlyphByIndex(codepoint); // int left = ((int)(glyph.glyphMatrix.img_horiBearingX * scale) >> 6); // Rectangle r = glyphData.Rect; // int adjustX = 0; // int bboxYMin = glyph.glyphMatrix.bboxYmin >> 6; // if (bboxYMin > 1 || bboxYMin < -1) // { // // adjustX = 3; // } // //scale down 0.8; // //_canvas.DrawSubImageWithMsdf(glBmp, ref r, adjustX + c_x + left, // // (float)(baseline + ((int)(glyphData.ImgHeight + glyph.glyphMatrix.bboxYmin) >> 6)), 1.1f); // coords.Add(r.Left); // coords.Add(r.Top); // coords.Add(r.Width); // coords.Add(r.Height); // //------------------------- // coords.Add(adjustX + c_x + left); // //coords.Add(baseline + ((int)((glyphData.ImgHeight + glyph.glyphMatrix.bboxYmin) * scale) >> 6)); // coords.Add(baseline + ((int)((glyphData.ImgHeight + glyphData.BBoxYMin) * scale) >> 6)); // //int w = (int)(glyph.glyphMatrix.advanceX * scale) >> 6; // int w = (int)(glyph.horiz_adv_x * scale) >> 6; // c_x += w; //} //_canvas.DrawSubImageWithMsdf(glBmp, coords.ToArray(), scale); //----------------------- //public override void DrawString(string text, double x, double y) //{ // char[] chars = text.ToCharArray(); // int j = chars.Length; // int buffsize = j * 2; // //get kerning list // TextureFont currentFont = this.CurrentFont as TextureFont; // SimpleFontAtlas fontAtlas = currentFont.FontAtlas; // ProperGlyph[] properGlyphs = new ProperGlyph[buffsize]; // currentFont.GetGlyphPos(chars, 0, buffsize, properGlyphs); // GLBitmap glBmp = currentFont.GLBmp; // if (glBmp == null) // { // //create glbmp // GlyphImage glyphImage = fontAtlas.TotalGlyph; // int[] buffer = glyphImage.GetImageBuffer(); // glBmp = new GLBitmap(glyphImage.Width, glyphImage.Height, buffer, false); // } // //int j = chars.Length; // // // float c_x = (float)x; // float c_y = (float)y; // //TODO: review here // //----------------- // //1. layout each glyph before render *** // float baseline = c_y - 24;//eg line height= 24 // //create a list // for (int i = 0; i < buffsize; ++i) // { // ProperGlyph glyph1 = properGlyphs[i]; // uint codepoint = properGlyphs[i].codepoint; // if (codepoint == 0) // { // break; // } // if (codepoint == 1173 && i > 1) // { // //check prev code point // codepoint = 1168; // } // TextureFontGlyphData glyphData; // if (!fontAtlas.GetRect((int)codepoint, out glyphData)) // { // //Rectangle r = glyphData.Rect; // //float x_min = glyphData.BBoxXMin / 64; // ////draw each glyph at specific position // ////_canvas.DrawSubImageWithMsdf(glBmp, ref r, c_x + x_min, (float)(baseline + r.Height)); // //_canvas.DrawSubImageWithMsdf(glBmp, ref r, c_x + x_min, (float)(baseline + r.Height)); // ////c_x += r.Width - 10; // //c_x += (glyphData.AdvanceX / 64); // continue; // } // //------------------------------------------------------------- // //FontGlyph glyph = this.currentFont.GetGlyphByIndex(codepoint); // FontGlyph glyph = currentFont.GetGlyphByIndex(codepoint); // int left = (glyph.glyphMatrix.img_horiBearingX >> 6); // Rectangle r = glyphData.Rect; // int adjustX = 0; // int bboxYMin = glyph.glyphMatrix.bboxYmin >> 6; // if (bboxYMin > 1 || bboxYMin < -1) // { // // adjustX = 3; // } // //scale down 0.8; // _canvas.DrawSubImageWithMsdf(glBmp, ref r, adjustX + c_x + left, // (float)(baseline + ((int)(glyphData.ImgHeight + glyph.glyphMatrix.bboxYmin) >> 6)), 1.1f); // int w = (glyph.glyphMatrix.advanceX) >> 6; // c_x += (w); // } //} // public override void DrawString(string text, double x, double y) // { // ////in this version we draw string to image // ////and the write the image back to gl surface // //_winGfx.Clear(System.Drawing.Color.White); // //_winGfx.DrawString(text, _winFont, _winGfxBrush, 0, 0); // ////_winGfxBackBmp.Save("d:\\WImageTest\\a00123.png"); // //System.Drawing.SizeF textAreaSize = _winGfx.MeasureString(text, _winFont); // //var bmpData = _winGfxBackBmp.LockBits(new System.Drawing.Rectangle(0, 0, _winGfxBackBmp.Width, _winGfxBackBmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, _winGfxBackBmp.PixelFormat); // //int width = (int)textAreaSize.Width; // //int height = (int)textAreaSize.Height; // //ActualImage actualImg = new ActualImage(width, height, Agg.Image.PixelFormat.ARGB32); // ////------------------------------------------------------ // ////copy bmp from specific bmp area // ////and convert to GLBmp // //int stride = bmpData.Stride; // //byte[] buffer = actualImg.GetBuffer(); // //unsafe // //{ // // byte* header = (byte*)bmpData.Scan0; // // fixed (byte* dest0 = &buffer[0]) // // { // // byte* dest = dest0; // // byte* rowHead = header; // // int rowLen = width * 4; // // for (int h = 0; h < height; ++h) // // { // // header = rowHead; // // for (int n = 0; n < rowLen;) // // { // // //move next // // *(dest + 0) = *(header + 0); // // *(dest + 1) = *(header + 1); // // *(dest + 2) = *(header + 2); // // *(dest + 3) = *(header + 3); // // header += 4; // // dest += 4; // // n += 4; // // } // // //finish one row // // rowHead += stride; // // } // // } // //} // //_winGfxBackBmp.UnlockBits(bmpData); // ////------------------------------------------------------ // //GLBitmap glBmp = new GLBitmap(width, height, buffer, false); // //_canvas.DrawImageWithWhiteTransparent(glBmp, (float)x, (float)y); // //glBmp.Dispose(); // } }