<Swing Dev> [10] RFR JDK-8187957:Tab Size does not work correctly in JTextArea

Prasanta Sadhukhan prasanta.sadhukhan at oracle.com
Thu Nov 9 09:56:24 UTC 2017


I guess by "manual implementation", Sergey was mentioning about 
programmer doing the calculation rather than using system api to do the 
job for us.

Anyway, I saw using Math.ceil and no "nTabs+1", alignment does not work

This is because
For ex, if x = 66.0, tabSize = 48.0, tabBase = 0, then nTabs = 
ceil(66/48) = 2 and tabStop=tabBase + ntabs * tabSize=96
Now, nextTabStop is called from Utilities.getTabbedTextWidth() in a loop 
passing the same value which it gets as return value from nextTabStop, 
for "x"
so x = 96.0, tabSize=48.0,ntabs = ceil(96/48)=2 and tabStop=96 again and 
it go on getting same tabStop.

With
int ntabs = (int) ((x - tabBase) / tabSize);
         return tabBase + ((ntabs + 1) * tabSize);

we get proper alignment
x 66.0 tabSize 48.0 ntabs 1 tabstop 96.0
x 96.0 tabSize 48.0 ntabs 2 tabstop 144.0
x 144.0 tabSize 48.0 ntabs 3 tabstop 192.0

So, I guess, as Semyon suggested, making the "result of the sentence 
should be  truncated to int, not its members" should be the way to go.

Lastly, could you please suggest as to how to automate this test?

Regards
Prasanta
On 11/9/2017 8:36 AM, semyon.sadetsky at oracle.com wrote:
> On 11/8/17 12:24 PM, Sergey Bylokhov wrote:
>
>> On 08/11/2017 11:38, Semyon Sadetsky wrote:
>>>> Yes "(int)+1" is also can be used for positive values, but the 
>>>> "ceil()" is better since this purpose of this method, and in 
>>>> general it works for negative values as well.
>>> There may not be negative values here, so Math.ceil() is redundant.
>>
>> I do not see a reason why standard function can be redundant, and why 
>> manual implementation is better. Since even in the first version of 
>> the fix there is an issues in manual implementation.
> I don't see it standard, just redundant in the case. What did you mean 
> under "manual implementation"?
>
> --Semyon
>>
>>>
>>> --Semyon
>>>>
>>>>>
>>>>>
>>>>> On 11/08/2017 10:24 AM, Sergey Bylokhov wrote:
>>>>>> Hi, Prasanta.
>>>>>> Is it possible that dropping the float part of "tabSize" and "x" 
>>>>>> will cause Off-by-one error? For example:
>>>>>>     float tabSize=3.99f;
>>>>>>     float x=21.9f;
>>>>>>     int tabBase=0;
>>>>>>  649         int ntabs = ((int) x - tabBase) / (int)tabSize;
>>>>>>  650         return tabBase + ((ntabs + 1) * tabSize);
>>>>>>
>>>>>> The result is: ntabs=7 -> 31.92 which is not correct.
>>>>>>
>>>>>> I guess you will need something like this:
>>>>>>     int ntabs = (int) Math.ceil((x - tabBase) / tabSize);
>>>>>>     return tabBase + ntabs * tabSize;
>>>>>> The result is: ntabs=6 -> 23.94
>>>>>>
>>>>>>
>>>>>> On 08/11/2017 03:25, Prasanta Sadhukhan wrote:
>>>>>>> Hi All,
>>>>>>>
>>>>>>> Bug: https://bugs.openjdk.java.net/browse/JDK-8187957
>>>>>>> webrev: http://cr.openjdk.java.net/~psadhukhan/8187957/webrev.00/
>>>>>>>
>>>>>>> Please review a fix for an issue where it is seen string with 
>>>>>>> "tab" in them are not aligned properly.
>>>>>>> This is because while calculating tab stop postion, it is 
>>>>>>> calculating number of tabs in float value (an aftereffect of 
>>>>>>> JDK-8156217 <https://bugs.openjdk.java.net/browse/JDK-8156217>)
>>>>>>> so next tab stop location is coming out wrong.
>>>>>>> Fix is to use "number of tabs" as an integer value in order to 
>>>>>>> calculate the tab position correctly.
>>>>>>>
>>>>>>> Regards
>>>>>>> Prasanta
>>>>>>
>>>>>>
>>>>>
>>>>
>>>>
>>>
>>
>>
>




More information about the swing-dev mailing list