|
17 | 17 |
|
18 | 18 | namespace PhpOffice\PhpWord\Shared;
|
19 | 19 |
|
| 20 | +use PhpOffice\PhpWord\Exception\InvalidStyleException; |
| 21 | + |
20 | 22 | /**
|
21 | 23 | * Common converter functions
|
22 | 24 | */
|
@@ -227,6 +229,121 @@ public static function emuToPixel($emu = 1)
|
227 | 229 | return round($emu / self::PIXEL_TO_EMU);
|
228 | 230 | }
|
229 | 231 |
|
| 232 | + /** |
| 233 | + * Convert an absolute CSS measurement to pixels |
| 234 | + * |
| 235 | + * Units for absolute CSS measurements are cm, mm, in, px, pt and pc |
| 236 | + * |
| 237 | + * Note that the result will be rounded to the nearest pixel |
| 238 | + * |
| 239 | + * @param string $cssMeasurement If no measurement unit is included then cm |
| 240 | + * is assumed |
| 241 | + * |
| 242 | + * @throws \PHPOffice\PhpWord\Exception\InvalidStyleException |
| 243 | + * |
| 244 | + * @return float |
| 245 | + */ |
| 246 | + public static function cssToPixel($cssMeasurement = '1cm') |
| 247 | + { |
| 248 | + $units = trim(preg_replace('/^-?(?:\\d+\\.\\d+|\\.?\\d+)/', '', trim($cssMeasurement))); |
| 249 | + $value = preg_replace('/\\D+$/', '', trim($cssMeasurement)); |
| 250 | + if ((strlen($value) > 0) && ($value[0] == '.')) { |
| 251 | + $value = '0' . $value; |
| 252 | + } |
| 253 | + switch (strtolower($units)) { |
| 254 | + case 'in': |
| 255 | + $pixel = $value * static::INCH_TO_PIXEL; |
| 256 | + break; |
| 257 | + case 'cm': |
| 258 | + case '': |
| 259 | + $pixel = ($value / static::INCH_TO_CM) * static::INCH_TO_PIXEL; |
| 260 | + break; |
| 261 | + case 'mm': |
| 262 | + $pixel = ($value / (10 * static::INCH_TO_CM)) * static::INCH_TO_PIXEL; |
| 263 | + break; |
| 264 | + case 'pt': |
| 265 | + $pixel = ($value / static::INCH_TO_POINT) * static::INCH_TO_PIXEL; |
| 266 | + break; |
| 267 | + case 'pc': |
| 268 | + $pixel = ($value / (12 * static::INCH_TO_POINT)) * static::INCH_TO_PIXEL; |
| 269 | + break; |
| 270 | + case 'px': |
| 271 | + $pixel = floatval($value); |
| 272 | + break; |
| 273 | + default: |
| 274 | + throw new InvalidStyleException($cssMeasurement . ' is an unsupported CSS measurement'); |
| 275 | + } |
| 276 | + return $pixel; |
| 277 | + } |
| 278 | + |
| 279 | + /** |
| 280 | + * Convert an absolute CSS measurement to EMU |
| 281 | + * |
| 282 | + * Units for absolute CSS measurements are cm, mm, in, px, pt and pc |
| 283 | + * |
| 284 | + * @param string $cssMeasurement If no measurement unit is included then cm |
| 285 | + * is assumed |
| 286 | + * |
| 287 | + * @throws \PHPOffice\PhpWord\Exception\InvalidStyleException |
| 288 | + * |
| 289 | + * @return float |
| 290 | + */ |
| 291 | + public static function cssToEmu($cssMeasurement = '1cm') |
| 292 | + { |
| 293 | + return static::cssToPixel($cssMeasurement) * static::PIXEL_TO_EMU; |
| 294 | + } |
| 295 | + |
| 296 | + /** |
| 297 | + * Convert an absolute CSS measurement to point |
| 298 | + * |
| 299 | + * Units for absolute CSS measurements are cm, mm, in, px, pt and pc |
| 300 | + * |
| 301 | + * @param string $cssMeasurement If no measurement unit is included then cm |
| 302 | + * is assumed |
| 303 | + * |
| 304 | + * @throws \PHPOffice\PhpWord\Exception\InvalidStyleException |
| 305 | + * |
| 306 | + * @return float |
| 307 | + */ |
| 308 | + public static function cssToPoint($cssMeasurement = '1cm') |
| 309 | + { |
| 310 | + return static::cssToPixel($cssMeasurement) * static::INCH_TO_POINT / static::INCH_TO_PIXEL; |
| 311 | + } |
| 312 | + |
| 313 | + /** |
| 314 | + * Convert an absolute CSS measurement to twip |
| 315 | + * |
| 316 | + * Units for absolute CSS measurements are cm, mm, in, px, pt and pc |
| 317 | + * |
| 318 | + * @param string $cssMeasurement If no measurement unit is included then cm |
| 319 | + * is assumed |
| 320 | + * |
| 321 | + * @throws \PHPOffice\PhpWord\Exception\InvalidStyleException |
| 322 | + * |
| 323 | + * @return float |
| 324 | + */ |
| 325 | + public static function cssToTwip($cssMeasurement = '1cm') |
| 326 | + { |
| 327 | + return static::cssToPixel($cssMeasurement) * static::INCH_TO_TWIP / static::INCH_TO_PIXEL; |
| 328 | + } |
| 329 | + |
| 330 | + /** |
| 331 | + * Convert an absolute CSS measurement to centimeter |
| 332 | + * |
| 333 | + * Units for absolute CSS measurements are cm, mm, in, px, pt and pc |
| 334 | + * |
| 335 | + * @param string $cssMeasurement If no measurement unit is included then cm |
| 336 | + * is assumed |
| 337 | + * |
| 338 | + * @throws \PHPOffice\PhpWord\Exception\InvalidStyleException |
| 339 | + * |
| 340 | + * @return float |
| 341 | + */ |
| 342 | + public static function cssToCm($cssMeasurement = '1cm') |
| 343 | + { |
| 344 | + return static::cssToPixel($cssMeasurement) * static::INCH_TO_CM / static::INCH_TO_PIXEL; |
| 345 | + } |
| 346 | + |
230 | 347 | /**
|
231 | 348 | * Convert degree to angle
|
232 | 349 | *
|
|
0 commit comments