Adobe 27510753 Scripting Guide - Page 76
Computing with unit values, baseUnit, unitValue
UPC - 718659445294
View all Adobe 27510753 manuals
Add to My Manuals
Save this manual to your list of manuals |
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