# platform/desktop/directwriteglyph (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/platform/desktop/directwriteglyph/

Windows DirectWrite glyph mask. (used by the desktop glyph raster)
- A file that does not exist in the original. Original Chrome on Windows paints Canvas2D text with Skia's SkScalerContext_DW through DirectWrite.
  The Rust version also paints with the same DirectWrite calls. (Skia 3279352f of Chrome 158, src/ports/SkScalerContext_win_dw.cpp, SkTypeface_win_dw.cpp)
- A8 mask: IDWriteFactory2::CreateGlyphRunAnalysis is given a rendering mode (picked by the gasp table, size, bytecode hints and bitmap fonts), a measuring mode,
  grid fitting on (hinting kNormal) and grayscale antialiasing, and returns an alpha texture of DWRITE_TEXTURE_ALIASED_1x1.
  On a machine with ClearType on, Chrome treats text as LCD so kGenA8FromLCD applies on the canvas, so a ClearType 3x1 texture is taken from IDWriteFactory and the three channels are averaged. (RGBToA8)
  The subpixel position is dx of the text matrix, and fake italic is m21 (-1/4) of the text matrix. After that it is corrected with the gamma precorrection table like Skia. (GrayscaleToA8)
- Text contrast and gamma: Chrome 158 uses the IDWriteRenderingParams values only if the key HKCU\SOFTWARE\Microsoft\Avalon.Graphics\<primary display device> exists,
  otherwise it uses Skia's defaults (contrast 1, sRGB). (GetTextParameters in ui/gfx/font_util_win.cc, font_render_params_win.cc, SetCachedParams in skia/ext/legacy_display_globals.cc)

Example: This module is used only on Windows, so it is skipped on other operating systems

```rust
#[cfg(windows)]
{
	use vanilla::platform::desktop::directwriteglyph::DirectWriteGlyphMask;
	let _ = std::mem::size_of::<DirectWriteGlyphMask>();
}
```

## DirectWriteGlyphMask (struct)

Glyph mask. (coverage and box, with top_y positive upward)

Example: The bytes per mask row equal the width (Windows only)

```rust
#[cfg(windows)]
fn read_row_length(mask: &vanilla::platform::desktop::directwriteglyph::DirectWriteGlyphMask) -> usize {
	mask.width as usize
}
let _ = 0;
```

- `pub coverage: Vec<u8>` (field, since 0.4.0): Coverage. (width bytes per row, top row first)
  - returns Vec<u8>
- `pub width: i32` (field, since 0.4.0): Width. (pixels)
  - returns i32
- `pub height: i32` (field, since 0.4.0): Height. (pixels)
  - returns i32
- `pub left_x: i32` (field, since 0.4.0): Left edge from the pen origin. (pixels)
  - returns i32
- `pub top_y: i32` (field, since 0.4.0): Top edge from the pen origin. (pixels, positive upward)
  - returns i32

## Functions and constants

- `pub fn read_text_contrast_and_gamma() -> (f32, f32)` (method, since 0.4.0): Text contrast and gamma of Chrome on Windows. ((contrast, gamma), gamma 0 is sRGB)
  - Like Chrome, the DirectWrite rendering values are used only when the Avalon.Graphics key of the primary display device exists, otherwise it is Skia's default of contrast 1 and sRGB.
  - returns (f32, f32): (contrast, gamma). Gamma 0 is sRGB.
- `pub fn is_lcd_text_enabled() -> bool` (method, since 0.4.0): Whether Chrome on Windows treats text as LCD. (If ClearType, Blink sets kSubpixelAntiAlias, and since the canvas does not know the pixel layout Skia applies kGenA8FromLCD to A8)
  - Chrome font_render_params_win.cc: if SPI_GETFONTSMOOTHING is on and SPI_GETFONTSMOOTHINGTYPE is ClearType, GetSubpixelRenderingGeometry is
    PixelStructure of Avalon.Graphics (0 none, 1 RGB, 2 BGR, anything else none), and if that value is missing, SPI_GETFONTSMOOTHINGORIENTATION (RGB, BGR, anything else none).
    The renderer turns on LCD text if this value is not none. (SetLCDTextEnabled in web_view_impl.cc)
  - returns bool: True if LCD text is on.
- `pub fn rasterize_glyph_mask(font_bytes: &Rc<[u8]>, source_path: Option<&Path>, face_index: u32, variation_list: &[(u32, f32)], glyph_index: u16, rendering_mode: i32, measuring_mode: i32, render_size: f32, subpixel_x: f32, skew_x: f32, correcting_table: Option<&[u8; 256]>) -> Option<DirectWriteGlyphMask>` (method, since 0.4.0): Draw a glyph A8 mask with DirectWrite. (Skia SkScalerContext_DW::generateDWMetrics, getDWMaskBits, GrayscaleToA8)
  - `font_bytes` &Rc<[u8]>: Font bytes. (the cache key, and used to build the face when there is no file path)
  - `source_path` Option<&Path>: Font file path.
  - `face_index` u32: Face index.
  - `variation_list` &[(u32, f32)]: Axis values. ((number of a 4-character tag, value), empty for the default instance)
  - `glyph_index` u16: Glyph ID.
  - `rendering_mode` i32: DWRITE_RENDERING_MODE value. (picked from the gasp table and size, like Skia)
  - `measuring_mode` i32: DWRITE_MEASURING_MODE value.
  - `render_size` f32: Font size to draw. (em size, pixels)
  - `subpixel_x` f32: Horizontal subpixel position. (0 to 1)
  - `skew_x` f32: Horizontal slant. (-1/4 for fake italic, Skia fPreSkewX as m21 of the DirectWrite text matrix)
  - `correcting_table` Option<&[u8; 256]>: Gamma precorrection table. (None means no correction)
  - returns Option<DirectWriteGlyphMask>: Mask. None if there is no outline or DirectWrite cannot draw it.
