Advertisement
Guest User

Nicest Resolution for DirectWrite

a guest
Jul 30th, 2018
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. {
  2.     // Last week I posted the point where I was stumped working on getting a CPU-side bitmap
  3.     // from DirectWrite rendered text.  At that time the most promising route I had found was through
  4.     // rendering to a D2D render target, backing that render target with a D3D texture, which would then
  5.     // let me get to the bits.  I did solve that version, thanks to help from @rygorous.  It took a lot
  6.     // of code and brought in a lot of dependencies but it worked...
  7.     //
  8.     // Then @mmozeiko sent me a code snippet that showed how to interface with GDI.  His code was simpler than
  9.     // what I was doing and had no extra dependencies.  I stripped out just the very little part that I needed,
  10.     // avoiding all the sub-classing towards into which COM style interfaces generally drive you.
  11.     //
  12.     // This is the result. -Allen
  13.    
  14.     // ... Get the factory, font face, whatever else you need ...
  15.    
  16.     // Magic sauce that I was missing when I made the last post:
  17.     // DWrite GDI Interop
  18.     IDWriteGdiInterop *dwrite_gdi_interop = 0;
  19.     error = dwrite_factory->GetGdiInterop(&dwrite_gdi_interop);
  20.    
  21.     // DWrite Bitmap Render Target
  22.     int32_t width = 200;
  23.     int32_t height = 200;
  24.     IDWriteBitmapRenderTarget *render_target = 0;
  25.     error = dwrite_gdi_interop->CreateBitmapRenderTarget(0, width, height, &render_target);
  26.    
  27.     // Render Glyph
  28.     int32_t index = 10;
  29.     RECT bounding_box = {0};
  30.     DWRITE_GLYPH_RUN glyph_run = {0};
  31.     glyph_run.fontFace = font_face;
  32.     glyph_run.fontEmSize = point_size*96.f/72.f;
  33.     glyph_run.glyphCount = 1;
  34.     glyph_run.glyphIndices = &index;
  35.     error = render_target->DrawGlyphRun(100.f, 100.f,
  36.                                         DWRITE_MEASURING_MODE_NATURAL, &glyph_run, rendering_params, RGB(255, 255, 255),
  37.                                         &bounding_box);
  38.     DWCheck(error);
  39.    
  40.     // Then read it through good ol' GDI
  41.     HDC dc = render_target->GetMemoryDC();
  42.     HBITMAP bitmap = (HBITMAP)GetCurrentObject(dc, OBJ_BITMAP);
  43.     DIBSECTION dib = {0};
  44.     GetObject(bitmap, sizeof(dib), &dib);
  45.    
  46.     // ... do whatever you want with your new bitmap ...
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement