Skip to content

Commit

Permalink
Make sure the pixel aspect ratio isn't too big.
Browse files Browse the repository at this point in the history
  • Loading branch information
j4james committed Jun 16, 2024
1 parent e52eef1 commit 673bef9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/terminal/adapter/SixelParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,6 @@ void SixelParser::_executeNextLine()
_executeCarriageReturn();
_imageLineCount++;
_maybeFlushImageBuffer();
// When we move down a line, we're moving the distance of the current sixel
// height, but the segment height may have been larger than that, so the
// remaining segment height will be inherited by the next line.
_segmentHeight -= _sixelHeight;
// But even if there isn't anything left to inherit, the new segment height
// will be at least as large as the current sixel height.
_segmentHeight = std::max(_segmentHeight, _sixelHeight);
_imageCursor.y += _sixelHeight;
_availablePixelHeight -= _sixelHeight;
_resizeImageBuffer(_sixelHeight);
Expand All @@ -260,9 +253,6 @@ void SixelParser::_executeMoveToHome()
{
_executeCarriageReturn();
_maybeFlushImageBuffer();
// Since we're moving up to the top of the image, the new segment will now
// also include everything above the current cursor position.
_segmentHeight += _imageCursor.y;
_imageCursor.y = 0;
_availablePixelHeight = _textMargins.height() * _cellSize.height;
}
Expand Down Expand Up @@ -296,6 +286,10 @@ bool SixelParser::_initTextBufferBoundaries()
}
_pendingTextScrollCount = 0;

// The pixel aspect ratio can't be so large that it would prevent a sixel
// row from fitting within the margin height, so we need to have a limit.
_maxPixelAspectRatio = _textMargins.height() * _cellSize.height / 6;

// If the cursor is visible, we need to hide it while the sixel data is
// being processed. It will be made visible again when we're done.
_textCursorWasVisible = page.Cursor().IsVisible();
Expand Down Expand Up @@ -370,7 +364,7 @@ void SixelParser::_updateRasterAttributes(const VTParameters& rasterAttributes)
{
// The documentation suggests the aspect ratio is rounded to the nearest
// integer, but on the original VT340 hardware it was rounded up.
_pixelAspectRatio = std::clamp(static_cast<int>(std::ceil(yAspect * 1.0 / xAspect)), 1, 100);
_pixelAspectRatio = std::clamp(static_cast<int>(std::ceil(yAspect * 1.0 / xAspect)), 1, _maxPixelAspectRatio);
_sixelHeight = 6 * _pixelAspectRatio;
// When the sixel height is changed multiple times in a row, the segment
// height has to track the maximum of all the sixel heights used.
Expand Down Expand Up @@ -754,6 +748,11 @@ void SixelParser::_maybeFlushImageBuffer(const bool endOfSequence)
}
}

// Once we've calculated how much scrolling was necessary for the existing
// segment height, we don't need to track that any longer. The next segment
// will start with the active sixel height.
_segmentHeight = _sixelHeight;

// This method is called after every newline (DECGNL), but we don't want to
// render partial output for high speed image sequences like video, so we
// only flush if it has been more than 500ms since the last flush, or it
Expand Down
1 change: 1 addition & 0 deletions src/terminal/adapter/SixelParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ namespace Microsoft::Console::VirtualTerminal
bool _textCursorWasVisible;
til::CoordType _availablePixelWidth;
til::CoordType _availablePixelHeight;
til::CoordType _maxPixelAspectRatio;
til::CoordType _pixelAspectRatio;
til::CoordType _sixelHeight;
til::CoordType _segmentHeight;
Expand Down

0 comments on commit 673bef9

Please sign in to comment.