Skip to content

arkascha/php-urithmetic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Urithmetic

Author: Christian Reiner

Copyright 2013-2014 Christian Reiner, Hamburg, Germany

License: GNU Affero General Public License

A simple library for unit based arithmetic

When layouting things one often has to compute various positions, zoom factors, scalings and the like. When the specific sizes are of dynamical nature the layout code quicky gets a confusing endless series of mathematical equations. You name it: spaghetti code. This library tries to offer two approaches to solve this problem:

  • by taking handling of units from explicit recomputation to implicit consideration, so all you have to do is name what unit a specific value is to be interpreted as
  • by offering well known arithmetical concepts as types which allows to think in a more high level way about layouting instead of just simple numeric values.

The code does not strive to be fast, this library is implemented in php. It only tries to help keeping the layouting code maintainable and robust. It would be great if additional types would be implemented in future. in geometrymetry what I really miss is angles and their consideration on layouting code. Also volumnes (the third dimension...) would be a welcome extension. And obviously aspects outside geometrymetry: electric units, mechanics like forces, accelerations and pressures. Or even currencies and their exchange rates. For performance optimization it might be fun to reimplement this approach in C or C++, for example as a php extension.

Example: This is a short example of a header definition taken from a pdf generating routine:

use arkascha\urithmetic;
// $this is an object holding some document attributes like file paths and the like
try {
  // define a box inside the header by its position(x,y) and size(w,h) and a unit
  $box = urithmetic\geometry\Area::fromScalars($this->leftHeaderPadding,
                                               $this->topHeaderPadding,
                                               $this->pageSize-$this->leftHeaderPadding-$this->rightHeaderPadding,
                                               $this->headerHeight-$this->leftHeaderPadding.$this->rightheaderPadding,
                                               urithmetic\Unit::MM);
  // a logo is placed inside the box on the left hand side
  $pic = urithmetic\geometry\Size::fromImageFile($this->logo1, urithmetic\Unit::MM);
  $pos = $box->fitSize($pic, urithmetic\geometry\Align::TOP | urithmetic\geometry\Align::LEFT);
  list($left,$top,$width,$height) = $pos->getScalars();
  $this->Image($this->logo1, $left, $top, $width, $height, self::identifyImageFormat($this->logo1));
  // a logo is placed inside the box on the right hand side
  $pic = urithmetic\geometry\Size::fromImageFile($this->logo2, urithmetic\Unit::MM);
  $pos = $box->fitSize($pic, urithmetic\geometry\Align::TOP | urithmetic\geometry\Align::RIGHT);
  list($left,$top,$width,$height) = $pos->getScalars();
  $this->Image($this->logo2, $left, $top, $width, $height, self::identifyImageFormat($this->logo2));
} catch (urithmetic\Exception $e) {
  error_log($e->getMessage());
  error_log($e->getTraceAsString());
  throw $e;
}

Explanation:

  • a page of size 'DIN A4' which is 210mm x 297mm (European metric standard page size)
  • a header should be reserved defining a top margin of 40 for all page content
  • inside that header a box is defined by its dimensions in millimeters
  • those dimensions are specified as numerical values here to keep things simple
  • inside that box two logs are placed using an alignment
  • position and size of the images to be created are computed automatically
  • note how there are no dimensions specified for the logos, these are computed by the available space

About

A simple library for unit based mathematical computations

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages