Wednesday 30 November 2016

Calibri and Cambria on Linux

I'm no font fanatic. I can't tell the difference between Calibri and Carlito but I can tell the a busted document layout.

Fortunately, Carlito has the same front metrics as Calibiri (and Caladea has the same metrics as Cambria).

The font-substitution table in LibreOffice then allows me to work on documents using these fonts while getting a layout the same as my colleagues on Microsoft Office.

Quoting from the Debian Wiki:

LibreOffice font substitution


To install them, issue these commands as root in a shell:
# apt-get update
# apt-get install fonts-crosextra-carlito fonts-crosextra-caladea


In LibreOffice, you exchange Calibri and Cambria with Carlito and Caladea this way:
  • Open the "Extras" menu
  • Go to "Options"
  • Choose "LibreOffice"
  • Choose "Fonts"
  • Define a substitution for each of the two fonts (Calibri -> Carlito, Cambria -> Caladea).
  • Remember to check "Always" in the substitution lines.

Once the program is restarted, documents sent from MS Office look almost the same on your screen and printouts.

Thursday 24 November 2016

async promises - just in time to be late for the node.js show

I'm late to the node.js show; and that's a good thing.

It means I don't have to worry about CPS callback hell

do_something(..., function(err, result) {
   something_else(result);
});

I means I don't even have to do much overt promising
do_something(...).then( (r) => {
  something_else(result);
}).catch( (e) => {
  ...
});

I can just use async/await with automatic exception propagation.
return await do_something(...);

I'm not averse to the odd promise for the interface between an old-style CPS function; after all only an async function can call await. But an async function is also a promise. So I can convert this:
function expressHandler(req, res, next) {
  do_something(..., function(err, result) {
    if (err) return next(err); // return avoids else
  ...
    next();
  });
} 
...
function do_something(..., callback) {
  try {
  ...
  return callback(undefined, result);
} catch (e) {
  return callback(e);
}

into
function expressHandler(req, res, next) {
  do_something(...).then((r) => {
    ...
    next();
  }).catch(next);
}
...
async function do_something(...) {
  return await something_else(...);
}

do_something is called as a promise and can be an async function that can await on other async functions or on promises. Async functions all the way up; callbacks all the way down (where I can't see).