Characters overlap when they change color and print back

enter image description here

As you can see, the top dark X is cut, although there is room for them.

This is because they have changed color and printed back (from right to left).

Is this a bug, faulty code, bad setup on my system, or (I doubt it) how should it be?

Here is the code that generates this output:

#include <Windows.h> #include <iostream> void moveTo(int x,int y){ COORD kord={x,y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),kord); } void setColor(WORD attributes){ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), attributes); } void main(){ for(int i=9;i+1;i--) { moveTo(i,0); std::cout.put('X'); } for(int i=-10;i;i++) { moveTo(i+10,1); std::cout.put('X'); } setColor(8); for(int i=9;i+1;i--) { moveTo(i,2); std::cout.put('X'); } for(int i=-10;i;i++) { moveTo(i+10,3); std::cout.put('X'); } setColor(7); for(int i=9;i+1;i--) { moveTo(i,4); std::cout.put('X'); } for(int i=-10;i;i++) { moveTo(i+10,5); std::cout.put('X'); } std::cin.get(); } 
+7
source share
1 answer

This is a bug in Windows.

As mentioned in errata hans passant :

I also play VS2008 on Win7. A big mistake. Changing the console font corrects it.

Use this insulation. I recognize this font as a Petite Terminal , which implies that you most likely configured this project as a Win32 console application. An additional repetition with GCC confirms this hypothesis, and from a practical point of view, we assume that you all received a 32-bit console application running inside a Windows terminal.

The question arises as to why he writes exactly one additional column of pixels in the context of the terminal's default font, color 8 and write back to the buffer console screen .

In particular, let me break down this problem into its components:

  • When a record is issued, the character is written to a location in the terminal array
  • When the default color is selected (7), pixels do not overflow into other buffers inside the array
  • When color 8 is selected, an additional column of pixels is written to the next area of ​​the buffer, which is visible only when the text is displayed back

Due to overflow in (3), this is an error.

Quote from Raymond Chen:

The console rendering model assumes that each character is clearly placed inside its fixed-size cell. When a new character is written into a cell, the old cell is sealed by a new character, but if the old character has an overhang or a dungeon, these additional pixels remain because they "spilled" the desired cell and infected neighboring cells. Similarly, if an adjacent character spills, these overflow pixels will be erased.

The set of fonts that can be used in the console window has been cropped to fonts that have been tested and are known to work acceptable in the console window. For English systems, this led us to the Lucida console and terminal.

...

"Well, that’s stupid. You should have stopped me from choosing the font that so obviously leads to bullshit."

And what have we done.

It's not that I blamed Raymond for this, but he authoritatively illustrates this as "can't happen."

Choosing and testing console fonts for Windows should have caught this. The fact that this is even a problem is an aberration.

+4
source

All Articles