Metafiles and Word
-
I just tracked down a subtle bug in a reworked graphing component. One of the requirements has always been that the graph can be exported as a metafile either by clipboard or a file. This has always worked in the previous version of the component. Since it was redeveloped a random output isssue had been seen where you end up with the graph squeezed into one corner of the output and the rest of the area blank. After fiddling with the code for a while and commenting out sections I tracked it down to the line drawing algorithm within the control. We use the good old y = mx + c equation to clip the lines ourselves to the output rectangle (Regions are invalid for metafiles so cannot be used) but because of the GDI line drawing algorithm we plot the final pixel of the final line manually using SetPixel. This is because if you were drawing a line from (1,1) -> (3,3) using GDI it does not set the final pixel. The only problem was I was not checking to see whether this pixel was within the bounding rectangle before plotting it. In normal use mode (i.e not display etc) this pixel usually never has any affect but for Metafiles it has a big impact. If my target output rect was (0, 0) -> (200, 200) and a line was clipped to it but which wanted to be plotted from (x,x) -> (500, 500), the code would plot that pixel at (500, 500). When Word or anything else came to use the metafile it would determine the display rectangle based on the output of all the GDI function calls and would scale the picture to include that 1 pixel at (500, 500) thus giving the buggy output. Nice to see it fixed. Shame it came to light whilst doing a demo to a company director at the time :omg: The usual case when demoing recent developments. :rolleyes:
If you vote me down, my score will only get lower
-
I just tracked down a subtle bug in a reworked graphing component. One of the requirements has always been that the graph can be exported as a metafile either by clipboard or a file. This has always worked in the previous version of the component. Since it was redeveloped a random output isssue had been seen where you end up with the graph squeezed into one corner of the output and the rest of the area blank. After fiddling with the code for a while and commenting out sections I tracked it down to the line drawing algorithm within the control. We use the good old y = mx + c equation to clip the lines ourselves to the output rectangle (Regions are invalid for metafiles so cannot be used) but because of the GDI line drawing algorithm we plot the final pixel of the final line manually using SetPixel. This is because if you were drawing a line from (1,1) -> (3,3) using GDI it does not set the final pixel. The only problem was I was not checking to see whether this pixel was within the bounding rectangle before plotting it. In normal use mode (i.e not display etc) this pixel usually never has any affect but for Metafiles it has a big impact. If my target output rect was (0, 0) -> (200, 200) and a line was clipped to it but which wanted to be plotted from (x,x) -> (500, 500), the code would plot that pixel at (500, 500). When Word or anything else came to use the metafile it would determine the display rectangle based on the output of all the GDI function calls and would scale the picture to include that 1 pixel at (500, 500) thus giving the buggy output. Nice to see it fixed. Shame it came to light whilst doing a demo to a company director at the time :omg: The usual case when demoing recent developments. :rolleyes:
If you vote me down, my score will only get lower
Yep! There's nothing like a demonstration with you customer to make these little devils rear their heads. :sigh: