extern crate direct2d;
extern crate direct3d11;
extern crate directwrite;
extern crate dxgi;
extern crate winapi;
use direct2d::brush::SolidColorBrush;
use direct2d::enums::{BitmapOptions, DrawTextOptions};
use direct2d::image::Bitmap;
use direct2d::{Device, DeviceContext, RenderTarget};
use direct3d11::flags::{BindFlags, CreateDeviceFlags};
use directwrite::{TextFormat, TextLayout};
use dxgi::Format;
const TEXTURE_WIDTH: u32 = 512;
const TEXTURE_HEIGHT: u32 = 128;
const DPI: f32 = 2.0;
#[test]
fn draw_to_texture() {
let dwrite = directwrite::Factory::new().unwrap();
let d2d = direct2d::Factory::new().unwrap();
let (_, d3d, _) = direct3d11::device::Device::create()
.with_flags(CreateDeviceFlags::BGRA_SUPPORT)
.build()
.unwrap();
let device = Device::create(&d2d, &d3d.as_dxgi()).unwrap();
let mut context = DeviceContext::create(&device, false).unwrap();
let tex = direct3d11::texture2d::Texture2D::create(&d3d)
.with_size(TEXTURE_WIDTH, TEXTURE_HEIGHT)
.with_format(Format::B8G8R8A8Unorm)
.with_bind_flags(BindFlags::RENDER_TARGET | BindFlags::SHADER_RESOURCE)
.build()
.unwrap();
let target = Bitmap::create(&context)
.with_dxgi_surface(&tex.as_dxgi())
.with_dpi(96.0 * DPI, 96.0 * DPI)
.with_options(BitmapOptions::TARGET)
.build()
.unwrap();
let font = TextFormat::create(&dwrite)
.with_family("Segoe UI")
.with_size(16.0)
.build()
.unwrap();
let text = TextLayout::create(&dwrite)
.with_text("Testing testing! \u{1F604}")
.with_font(&font)
.with_size(TEXTURE_WIDTH as f32 / DPI, TEXTURE_HEIGHT as f32 / DPI)
.build()
.unwrap();
let brush = SolidColorBrush::create(&context)
.with_color(0x00_00_00)
.build()
.unwrap();
context.begin_draw();
context.set_target(&target);
context.clear(0xFF_FF_FF);
context.draw_text_layout(
(0.0, 0.0),
&text,
&brush,
DrawTextOptions::ENABLE_COLOR_FONT,
);
context.end_draw().unwrap();
}