Repository: microsoft/terminal
File: src/renderer/dx/DxSoftFont.cpp (C++)

Code before:
    const auto bitmapHeight = BITMAP_GRID_HEIGHT * (_sourceSize.height + PADDING * 2);
    _bitmapBits = std::vector<UINT32>(bitmapWidth * bitmapHeight);
    _bitmapSize = { gsl::narrow_cast<UINT32>(bitmapWidth), gsl::narrow_cast<UINT32>(bitmapHeight) };

    const auto bitmapScanline = [=](const auto lineNumber) noexcept {
        return _bitmapBits.begin() + lineNumber * bitmapWidth;
    };

    // The source bitPattern is just a list of the scanlines making up the
    // glyphs one after the other, but we want to lay them out in a grid, so
    // we need to process each glyph individually.
    auto srcPointer = bitPattern.begin();
    for (auto glyphNumber = 0u; glyphNumber < _glyphCount; glyphNumber++)
    {
        // We start by calculating the position in the bitmap where the glyph
        // needs to be stored.
        const auto xOffset = _xOffsetForGlyph<size_t>(glyphNumber);
        const auto yOffset = _yOffsetForGlyph<size_t>(glyphNumber);
        auto dstPointer = bitmapScanline(yOffset) + xOffset;
        for (auto y = 0; y < sourceSize.height; y++)
        {
            // Then for each scanline in the source, we need expand the bits
            // into 32-bit BGRA values. For every bit that is set we write out
            // an FFFFFFFF value, and it not set, we write out 00000000. In the
            // end, though, all we care about is the red component, since we'll
            // later remap these values with a color matrix.
            auto srcBits = *(srcPointer++);
            for (auto x = 0; x < sourceSize.width; x++)
            {
                const auto srcBitIsSet = (srcBits & 0x8000) != 0;
                *(dstPointer++) = srcBitIsSet ? 0xFFFFFFFF : 0x00000000;
                srcBits <<= 1;
            }
            // When glyphs in this bitmap are output, they will typically need
            // to scaled, and this can result in some bleed from the surrounding
            // pixels. So to keep the borders clean, we pad the areas to the left
            // and right by repeating the first and last pixels of each scanline.
            std::fill_n(dstPointer, PADDING, til::at(dstPointer, -1));
            dstPointer -= sourceSize.width;
            std::fill_n(dstPointer - PADDING, PADDING, til::at(dstPointer, 0));
            dstPointer += bitmapWidth;
        }
    }

    // In the same way that we padded the left and right of each glyph in the
    // code above, we also need to pad the top and bottom. But in this case we
    // can simply do a whole row of glyphs from the grid at the same time.
    for (auto gridRow = 0u; gridRow < BITMAP_GRID_HEIGHT; gridRow++)
    {
        const auto rowOffset = _yOffsetForGlyph<size_t>(gridRow);
        const auto rowTop = bitmapScanline(rowOffset);

Review comment (suggestion):
If you need a monochrome bitmap why not use `DXGI_FORMAT_R8_UNORM` or `DXGI_FORMAT_A8_UNORM`?
The latter would work like this: https://docs.microsoft.com/en-us/windows/win32/direct2d/how-to-clip-with-bitmap-masks

Code after:
    const auto bitmapHeight = BITMAP_GRID_HEIGHT * (_sourceSize.height + PADDING * 2);
    _bitmapBits = std::vector<byte>(bitmapWidth * bitmapHeight);
    _bitmapSize = { gsl::narrow_cast<UINT32>(bitmapWidth), gsl::narrow_cast<UINT32>(bitmapHeight) };

    const auto bitmapScanline = [=](const auto lineNumber) noexcept {
        return _bitmapBits.begin() + lineNumber * bitmapWidth;
    };

    // The source bitPattern is just a list of the scanlines making up the
    // glyphs one after the other, but we want to lay them out in a grid, so
    // we need to process each glyph individually.
    auto srcPointer = bitPattern.begin();
    for (auto glyphNumber = 0u; glyphNumber < _glyphCount; glyphNumber++)
    {
        // We start by calculating the position in the bitmap where the glyph
        // needs to be stored.
        const auto xOffset = _xOffsetForGlyph<size_t>(glyphNumber);
        const auto yOffset = _yOffsetForGlyph<size_t>(glyphNumber);
        auto dstPointer = bitmapScanline(yOffset) + xOffset;
        for (auto y = 0; y < sourceSize.height; y++)
        {
            // Then for each scanline in the source, we need to expand the bits
            // into 8-bit values. For every bit that is set we write out an FF
            // value, and if not set, we write out 00. In the end, all we care
            // about is a single red component for the R8_UNORM bitmap format,
            // since we'll later remap that to RGBA with a color matrix.
            auto srcBits = *(srcPointer++);
            for (auto x = 0; x < sourceSize.width; x++)
            {
                const auto srcBitIsSet = (srcBits & 0x8000) != 0;
                *(dstPointer++) = srcBitIsSet ? 0xFF : 0x00;
                srcBits <<= 1;
            }
            // When glyphs in this bitmap are output, they will typically need
            // to scaled, and this can result in some bleed from the surrounding
            // pixels. So to keep the borders clean, we pad the areas to the left
            // and right by repeating the first and last pixels of each scanline.
            std::fill_n(dstPointer, PADDING, til::at(dstPointer, -1));
            dstPointer -= sourceSize.width;
            std::fill_n(dstPointer - PADDING, PADDING, til::at(dstPointer, 0));
            dstPointer += bitmapWidth;
        }
    }

    // In the same way that we padded the left and right of each glyph in the
    // code above, we also need to pad the top and bottom. But in this case we
    // can simply do a whole row of glyphs from the grid at the same time.
    for (auto gridRow = 0u; gridRow < BITMAP_GRID_HEIGHT; gridRow++)
    {
        const auto rowOffset = _yOffsetForGlyph<size_t>(gridRow);
        const auto rowTop = bitmapScanline(rowOffset);