Adobe 27510753 Scripting Guide - Page 76

Computing with unit values, baseUnit, unitValue

Page 76 highlights

68 Using ExtendScript Tools and Features Adobe InDesign CS2 Scripting Guide l To convert pixels into length units, you must know the size of a single pixel. The size of a pixel depends on the display resolution. A common resolution measurement is 72 dpi, which means that there are 72 pixels to the inch. The conversion base for pixels at 72 dpi is 0.013889 inches (1/72 inch). l Percentage values are relative to a total measurement. For example, 10% of 100 inches is 10 inches, while 10% of 1 meter is 0.1 meters. The conversion base of a percentage is the unit value corresponding to 100%. The default baseUnit of a unitValue object is 0.013889 inches, the base for pixels at 72 dpi. If the unitValue is for pixels at any other dpi, or for a percentage value, you must set the baseUnit value accordingly. The baseUnit value is itself a unitValue object, containing both a magnitude and a unit. For a system using a different dpi, you can change the baseUnit value in the UnitValue class, thus changing the default for all new unitValue objects. For example, to double the resolution of pixels: UnitValue.baseUnit = UnitValue (1/144, "in"); //144 dpi To restore the default, assign null to the class property: UnitValue.baseUnit = null; //restore default You can override the default value for any particular unitValue object by setting the property in that object. For example, to create a unitValue object for pixels with 96 dpi: pixels = UnitValue (10, "px"); myPixBase = UnitValue (1/96, "in"); pixels.baseUnit = myPixBase; For percentage measurements, set the baseUnit property to the measurement value for 100%. For example, to create a unitValue object for 40 % of 10 feet: myPctVal = UnitValue (40, "%"); myBase = UnitValue (10, "ft") myPctVal.baseUnit = myBase; Use the as method to get to a percentage value as a unit value: myFootVal = myPctVal.as ("ft"); // => 4 myInchVal = myPctVal.as ("in"); // => 36 You can convert a unitValue from an absolute measurement to pixels or percents in the same way: myMeterVal = UnitValue (10, "m"); // 10 meters myBase = UnitValue (1, "km"); myMeterVal.baseUnit = myBase; //as a percentage of 1 kilometer pctOfKm = myMeterVal.as 1 myVal = UnitValue ("1 in"); // Define measurement in inches // convert to pixels using default base myVal.convert ("px"); // => value=72 type=px Computing with unit values UnitValue objects can be used in computational JavaScript expressions. The way the value is used depends on the type of operator. l Unary operators ~unitValue The numeric value is converted to a 32-bit integer with inverted bits. !unitValue Result is true if the numeric value is nonzero, false if it is not. +unitValue Result is the numeric value. -unitValue Result is the negated numeric value. l Binary operators

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184

68
Using ExtendScript Tools and Features
Adobe InDesign CS2 Scripting Guide
To convert pixels into length units, you must know the size of a single pixel. The size of a pixel depends on
the display resolution. A common resolution measurement is 72 dpi, which means that there are 72 pixels
to the inch. The conversion base for pixels at 72 dpi is 0.013889 inches (1/72 inch).
Percentage values are relative to a total measurement. For example, 10% of 100 inches is 10 inches, while
10% of 1 meter is 0.1 meters. The conversion base of a percentage is the unit value corresponding to 100%.
The default
baseUnit
of a
unitValue
object is 0.013889 inches, the base for pixels at 72 dpi. If the
unitValue
is for pixels at any other dpi, or for a percentage value, you must set the
baseUnit
value
accordingly. The
baseUnit
value is itself a
unitValue
object, containing both a magnitude and a unit.
For a system using a different dpi, you can change the
baseUnit
value in the
UnitValue
class, thus
changing the default for all new
unitValue
objects. For example, to double the resolution of pixels:
UnitValue.baseUnit = UnitValue (1/144, "in"); //144 dpi
To restore the default, assign
null
to the class property:
UnitValue.baseUnit = null; //restore default
You can override the default value for any particular
unitValue
object by setting the property in that object.
For example, to create a unitValue object for pixels with 96 dpi:
pixels = UnitValue (10, "px");
myPixBase = UnitValue (1/96, "in");
pixels.baseUnit = myPixBase;
For percentage measurements, set the
baseUnit
property to the measurement value for 100%. For example,
to create a
unitValue
object for 40 % of 10 feet:
myPctVal = UnitValue (40, "%");
myBase = UnitValue (10, "ft")
myPctVal.baseUnit = myBase;
Use the
as
method to get to a percentage value as a unit value:
myFootVal = myPctVal.as ("ft"); // => 4
myInchVal = myPctVal.as ("in"); // => 36
You can convert a
unitValue
from an absolute measurement to pixels or percents in the same way:
myMeterVal = UnitValue (10, "m"); // 10 meters
myBase = UnitValue (1, "km");
myMeterVal.baseUnit = myBase; //as a percentage of 1 kilometer
pctOfKm = myMeterVal.as (‘%’); // => 1
myVal = UnitValue ("1 in"); // Define measurement in inches
// convert to pixels using default base
myVal.convert ("px"); // => value=72 type=px
Computing with unit values
UnitValue
objects can be used in computational JavaScript expressions. The way the value is used depends
on the type of operator.
Unary operators (
~
,
!
,
+
,
-
)
~
unitValue
The numeric value is converted to a 32-bit integer with inverted bits.
!
unitValue
Result is
true
if the numeric value is nonzero,
false
if it is not.
+
unitValue
Result is the numeric value.
-
unitValue
Result is the negated numeric value.
Binary operators (
+
,
-
,
*
,
/
,
%
)
l
l
l
l