improvement: 单位转换的调整

This commit is contained in:
csh
2025-08-06 18:16:15 +08:00
parent 9062457251
commit 16452c8362
6 changed files with 1301 additions and 1019 deletions
+111 -37
View File
@@ -1,70 +1,144 @@
unit TSSafeUnitConverter;
interface
function PointsToTwips(value): real;
function TwipsToPoints(value): real;
function CentimetersToPoints(value): real;
function EmusToPoints(value): real;
function HalfPointToPoints(value): real;
function InchesToPoints(value): real;
function LinesToPoints(value): real;
function MillimetersToPoints(value): real;
function PicasToPoints(value): real;
function PixelsToPoints(value): real; // TODO 不实现,与DPI有关
function TwipsToPoints(value): real;
// points -> other
function PointsToCentimeters(value): real;
function PointsToEmus(value): real;
function PointsToInches(value): real;
function PointsToLines(value): real;
function PointsToMillimeters(value);
function PointsToPicas(value);
function PointsToPixels(value); // TODO
function PointsToTwips(value): real;
// other
function HalfPointsToPoints(value): real;
function PointsToHalfPoints(value): real;
function EighthPointsToPoints(value): real;
function PercentToNumber(value): real;
function NumberToPercent(value): real;
function ToInt(value): integer;
function EighthPointToPoints(value): real;
implementation
uses TSUnitConverter;
function PointsToTwips(value): real;
function SafeConvert(value, convert_func): real;
begin
if ifNil(value) then return 0;
new_value := ifString(value) ? strToFloat(value) : value;
return TSUnitConverter.PointsToTwips(new_value);
new_value := ifString(value) ? strToFloatDef(value, 0) : value;
return ##convert_func(new_value);
end;
function TwipsToPoints(value): real;
function CentimetersToPoints(value): real;
begin
if ifNil(value) then return 0;
new_value := ifString(value) ? strToFloat(value) : value;
return TSUnitConverter.TwipsToPoints(new_value);
end;
function HalfPointToPoints(value): real;
begin
if ifNil(value) then return 0;
new_value := ifString(value) ? strToFloat(value) : value;
return TSUnitConverter.HalfPointToPoints(new_value);
return SafeConvert(value, thisFunction(TSUnitConverter.CentimetersToPoints));
end;
function EmusToPoints(value): real;
begin
if ifNil(value) then return 0;
new_value := ifString(value) ? strToFloat(value) : value;
return TSUnitConverter.EmusToPoints(new_value);
return SafeConvert(value, thisFunction(TSUnitConverter.EmusToPoints));
end;
function InchesToPoints(value): real;
begin
return SafeConvert(value, thisFunction(TSUnitConverter.InchesToPoints));
end;
function LinesToPoints(value): real;
begin
return SafeConvert(value, thisFunction(TSUnitConverter.LinesToPoints));
end;
function MillimetersToPoints(value): real;
begin
return SafeConvert(value, thisFunction(TSUnitConverter.MillimetersToPoints));
end;
function PicasToPoints(value): real;
begin
return SafeConvert(value, thisFunction(TSUnitConverter.PicasToPoints));
end;
function TwipsToPoints(value): real;
begin
return SafeConvert(value, thisFunction(TSUnitConverter.TwipsToPoints));
end;
// points -> other
function PointsToCentimeters(value): real;
begin
return SafeConvert(value, thisFunction(TSUnitConverter.PointsToCentimeters));
end;
function PointsToEmus(value): real;
begin
return SafeConvert(value, thisFunction(TSUnitConverter.PointsToEmus));
end;
function PointsToInches(value): real;
begin
return SafeConvert(value, thisFunction(TSUnitConverter.PointsToInches));
end;
function PointsToLines(value): real;
begin
return SafeConvert(value, thisFunction(TSUnitConverter.PointsToLines));
end;
function PointsToMillimeters(value);
begin
return SafeConvert(value, thisFunction(TSUnitConverter.PointsToMillimeters));
end;
function PointsToPicas(value);
begin
return SafeConvert(value, thisFunction(TSUnitConverter.PointsToPicas));
end;
function PointsToTwips(value): real;
begin
return SafeConvert(value, thisFunction(TSUnitConverter.PointsToTwips));
end;
// other
function HalfPointsToPoints(value): real;
begin
return SafeConvert(value, thisFunction(TSUnitConverter.HalfPointsToPoints));
end;
function PointsToHalfPoints(value): real;
begin
return SafeConvert(value, thisFunction(TSUnitConverter.PointsToHalfPoints));
end;
function EighthPointsToPoints(value): real;
begin
return SafeConvert(value, thisFunction(TSUnitConverter.EighthPointsToPoints));
end;
function PercentToNumber(value): real;
begin
if ifNil(value) then return 0;
new_value := ifString(value) ? strToFloat(value) : value;
return TSUnitConverter.PercentToNumber(new_value);
return SafeConvert(value, thisFunction(TSUnitConverter.PercentToNumber));
end;
function NumberToPercent(value): real;
begin
if ifNil(value) then return 0;
new_value := ifString(value) ? strToFloat(value) : value;
return TSUnitConverter.NumberToPercent(new_value);
return SafeConvert(value, thisFunction(TSUnitConverter.NumberToPercent));
end;
function ToInt(value): integer;
function ToInt(value): real;
begin
if ifNil(value) then return 0;
return strtoIntDef(value, 0);
end;
function EighthPointToPoints(value): real;
begin
if ifNil(value) then return 0;
new_value := ifString(value) ? strToFloat(value) : value;
return TSUnitConverter.EighthPointToPoints(new_value);
if ifnumber(value) then return integer(value);
if ifstring(value) then return strToIntDef(value, 0);
return 0;
end;
end.
+103 -21
View File
@@ -1,48 +1,130 @@
unit TSUnitConverter;
interface
function PointsToTwips(value): real;
function TwipsToPoints(value: real): real;
function EmusToPoints(value: real): real;
function HalfPointToPoints(value: real): real;
function EighthPointToPoints(value: real): real;
function PercentToNumber(value: real): real;
function NumberToPercent(value: real): real;
// other -> points
function CentimetersToPoints(centimeters: real): real;
function EmusToPoints(emus: real): real;
function InchesToPoints(inches: real): real;
function LinesToPoints(lines: real): real;
function MillimetersToPoints(millimeters: real): real;
function PicasToPoints(picas: real): real;
function PixelsToPoints(pixels: real; f_vertical: boolean = false): real; // TODO 不实现,与DPI有关
function TwipsToPoints(twips: real): real;
// points -> other
function PointsToCentimeters(points: real): real;
function PointsToEmus(points: real): real;
function PointsToInches(points: real): real;
function PointsToLines(points: real): real;
function PointsToMillimeters(points: real);
function PointsToPicas(points: real);
function PointsToPixels(points: real); // TODO
function PointsToTwips(points): real;
// other
function HalfPointsToPoints(half_points: real): real;
function PointsToHalfPoints(points: real): real;
function EighthPointsToPoints(eighth_points: real): real;
function PercentToNumber(percent: real): real;
function NumberToPercent(number: real): real;
implementation
function PointsToTwips(value): real;
function CentimetersToPoints(centimeters: real): real;
begin
return value * 20;
return centimeters * 28.346456692913385826771653543307;
end;
function TwipsToPoints(value: real): real;
function EmusToPoints(emus: real): real;
begin
return value / 20;
return emus / 12700;
end;
function EmusToPoints(value: real): real;
function InchesToPoints(inches: real): real;
begin
return value / 12700;
return inches * 72;
end;
function HalfPointToPoints(value: real): real;
function LinesToPoints(lines: real): real;
begin
return value / 2;
return lines * 12; // Assuming 1 line = 12 points
end;
function PercentToNumber(value: real): real;
function MillimetersToPoints(millimeters: real): real;
begin
return value / 100;
return millimeters * 2.8346456692913385826771653543307;
end;
function NumberToPercent(value: real): real;
function PicasToPoints(picas: real): real;
begin
return value * 100;
return picas * 12;
end;
function EighthPointToPoints(value: real): real;
function TwipsToPoints(twips: real): real;
begin
return value / 8;
return twips / 20;
end;
// Points -> others
function PointsToCentimeters(points: real): real;
begin
return points / 28.346456692913385826771653543307;
end;
function PointsToEmus(points: real): real;
begin
return points * 12700;
end;
function PointsToInches(points: real): real;
begin
return points / 72;
end;
function PointsToLines(points: real): real;
begin
return points / 12;
end;
function PointsToMillimeters(points: real);
begin
return points / 2.8346456692913385826771653543307;
end;
function PointsToTwips(points): real;
begin
return points * 20;
end;
function PointsToPicas(points: real);
begin
return points / 12;
end;
// other
function HalfPointsToPoints(half_points: real): real;
begin
return half_points / 2;
end;
function PointsToHalfPoints(points: real): real;
begin
return points * 2;
end;
function EighthPointsToPoints(eighth_points: real): real;
begin
return eighth_points / 8;
end;
function PercentToNumber(percent: real): real;
begin
return percent / 100;
end;
function NumberToPercent(number: real): real;
begin
return number * 100;
end;
end.