Column Switcher

Yesterday I met a problem where I needed to replace the columns of a csv-file separated by whitespace, since this was some pretty basic stuff I wrote a simple program in Java fairly easily.

Today when I woke up I thought that maybe I should try and see how I could do the same with Shell Scripting, so here goes (the programs are available at my github here):

import java.util.Scanner;
import java.io.*;

public class Cols {

    /**
     * This program takes a csv file separated by whitespace and replaces the first with the second column.
     * @name Cols
     * @author tcarisland
     * @date 6.sep.2017
     */

    public static void main(String args[]) {
	try {
	    Scanner in = new Scanner(new File(args[0]));
	    PrintWriter out = new PrintWriter(new File(args[1]));
	    while(in.hasNextLine()) {
		String cols[] = in.nextLine().split("\\s+");
		if(cols.length > 1)
		    out.println(cols[1] + " " + cols[0]);
	    }
	    in.close();
	    out.close();
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

}

 

And the Bash program:

 

#/bin/bash

#A minimal version of the cols program to do exactly the same as the java program.
#Needs two arguments.

rm -f $2 && touch $2;

while IFS=" " read c1 c2
do
    echo $c2" "$c1 >>$2;
done < $1

HashMapMaker

While working on a problem today I found I wanted to simplify the process of creating predefined HashMaps.

So I made this little utility program to take two files and create a HashMap<String, String> from such a file.

Nothing much, but I put it up on my GitHub here nonetheless.

import java.util.Scanner;
import java.io.*;

public class HashMapMaker {

    /**
     * A simple program that creates a HashMap from a file with fields separated by whitespace.
     * @name HashMapMaker
     * @author tcarisland
     * @date 5.sep.2017
     */
    
    public static void main(String args[]) {
	try {
	    Scanner in = new Scanner(new File(args[0]));
	    PrintWriter out = new PrintWriter(new File(args[1]));
	    out.println("private static final Map<String, String> m = new HashMap<String, String>() {{");
	    while(in.hasNextLine()) {
		String pair[] = in.nextLine().split("\\s+");
		if(pair.length > 1)
		    out.println("put (\"" + pair[0] + "\", \"" + pair[1] + "\");");
	    }
	    in.close();
	    out.println("}};");
	    out.close();
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
    
}

GlyphSize – an SVG resizing tool.

Click here to download the program.

As I’ve learned how to create fonts using Affinity Designer and FontForge, I’ve come across a few situations that require unnecessary monkey work. With some experimenting I’ve found that the simplest way to export multiple curves from Affinity Designer into Fontforge is to use the Export Persona in Affinity Designer and export curves as SVG files.

Note: Because macOS and newer operating systems are extremely paranoid these days, this program will not be able to run unless you set the security settings to allow running programs from “Anywhere”. To make matters worse – newer versions of macOS have completely removed this option. To disable this child protection lock, just follow these instructions. Be aware though, that running programs from “anywhere” is a security risk, but still necessary since both Apple and Microsoft will raise a red flag for very common applications if that particular application is something they don’t sell in their “App Store” or “Microsoft Store”.

The problem here is that when exporting the slices, the viewbox height and width is set to the size of the curve and this will cause FontForge to resize the letter you designed to fit the Glyph.

To fix this problem I made a simple Java batch processing application that resizes the viewbox to a square canvas to the size of your liking.

This application is based on my own workflow which means that to understand the why and how, a tutorial explaining how I design a font is necessary.

Step 1: Design the font in a large Affinity Designer document.

I start out with a canvas the size of an individual glyph, since I prefer working with TrueType fonts, I select a size of 2048X2048px (or 1024X1024px), and define a descender, x-height and ascender by creating semi-transparent coloured rectangles.

Screen Shot 2017-06-08 at 16.39.03

After this, I make the document larger (using “anchor to page” – not “resize”) so as to fit multiple glyphs and design all my characters.

A font designed in AD (Affinity Designer) looks something like this:

designedfont

Step 2: Resize the document

(if you didn’t design the letters according to the proper glyph size). 

If I want to change the size of the letters after this (if – for example, you find you want the cap height to be lower than the ascender – which is common in fonts since some space is required for accents in characters such as “Å” “Ë” and “Ú”, you can create a new document with the desired glyph size (in this case 2048).

2048

After this you select two characters, one upper case (such as A) and one lower case with a descender (such as g, y, p or q), place the g so that it comes at a tangent to the bottom of the canvas and use this to find the baseline, then place the upper case letter (A) on the baseline.

You can now group these together and resize the characters, after resizing them you need to change the position of the baseline.

Ag

With the resized letters, copy one of the selected letters into the document, and compare the height of these letters.

AAg

In the “Transform” window, you can see that the smaller letter has a height of “1344px” and the taller one has a height of “1536px”, with this information you find the the factor you want to resize the document to. My document has a width of 16348px, I can then calculate: 16348 * (1344/1536) = 14304 (approximately, the answer needs to be an integer).

I note this down in my document so that I can resize my document back to its original size if I want.

note

Step 3: Export the individual glyphs using the export persona.

With the document and letters set according to the right size so that they fit into a predetermined em-size (this is what it’s called in FontForge). Select the export persona (also make sure that all letters are in the form of a single curve, use the boolean “add” operation to create a union of all the overlapping shapes).

Export

In the layers tab – select all the relevant curves (all letters, numbers and symbols you want to export), and click “create slice”, the slices should be set to the “SVG” file format, but none of the SVG presets in AD have the correct settings, so I suggest you create a new Preset called something like “GlyphExport”.

The settings for exporting the glyphs are:

  • The “Set viewBox” should be checked
  • File format should be set to “SVG”
  • “Rasterise:” should be set to “Nothing”

Then click Export slices into a separate directory of your choice.

I normally name the directory “glyphs” and put them into a project directory along with the AD document file.

Step 4: Resize the viewBox using GlyphSize.

With the glyphs exported, you can open the folder and see how they look like.

Note: If you planned well, you should have renamed the “curves” in AD to their appropriate letter. When the curves have names, this becomes their filename instead of just “slice1.svg”, “slice2.svg” etc.

My lettering scheme for individual letters is taken from the FontForge Glyph Names, other than that, I name upper case letters according to their letter – so “A” is named simply “A”, but lower case letters have the l-suffix so that “a” is named “al”, this makes importing into FontForge a lot easier.

You can download the GlyphSize program here.

At the moment (June 8 2017), the program looks a little something like this:

Screen Shot 2017-06-08 at 17.04.07

To select your directory, just click the “Choose Folder” button and locate your directory, you can either click the directory and click “choose”, or click on any file inside the directory and it will automatically select the directory that the file is in.

Screen Shot 2017-06-08 at 17.06.05

With the directory selected, all there is left is to supply the desired em-size (labeled GlyphSize) and click “Resize Items”, it will create a directory within that directory named “resized”, take every SVG-file within that directory and write a copy of that SVG file with the desired canvas size.

Screen Shot 2017-06-08 at 17.06.12

Step 5: Import into FontForge.

The individual SVG files are now ready to be imported into FontForge, use the “Ctrl + I” as a hotkey for faster access to the Import dialog.

If you followed Step 1 or Step 2 and made a semi-transparent rectangle representing the descender and baseline, you can use the height of this rectangle as the “Descent” in Element=>Font Info=>General, after this you can fill in the Ascent by calculating 2048 – 450 = 1598.

Do this before you import your SVG files.

Screen Shot 2017-06-08 at 20.10.25

More Barcodes

I’ve been working more with my barcode application and am finally close to a working solution.

Adding start and stop characters is more or less dependent on which charset you want to use, unless you’re going to use special characters or some other special functionality, you’ll be using Code128 B – which supports most normal alphanumeric characters and punctuation characters in the ASCII table. The rest is just basic String concatenation, which you can do in LibreOffice Calc or through programming.

The first stretch was developing the checksum method in Java, which was pretty easy thanks to the information provided by wikipedia here.

What I did was simply to make an array with the characters listed for Code 128 B, disregarding special characters since I won’t be using them anyway. The array looks a little something like this…

private String code128B_table[] = {
" ","!","\"","#","$"," %","&","'","(",")","*","+",",","-",".","/",
"0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?",
"@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
"Q","R","S","T","U","V","W","X","Y","Z","[","\\","]","^","_","`",
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p",
"q","r","s","t","u","v","w","x","y","z","{","|","}","~","DEL","FNC3",
"FNC2","ShiftA","CodeC","FNC4","CodeA","FNC1","StartCodeA","StartCodeB","StartCodeC"
};
private String code128_table[] = code128B_table;

With the Array in place, all I needed to do to calculate the checksum was write this method:

/**
* Calculates the checksum
* @param input String input for calculating checksum
* @return the checksum in integer form
*/
public int checksumInt(String input) {
int checksum = STARTCODE;
for(int i = 0; i < input.length(); i++) {
int value = charNumber(input.charAt(i));
if(value < 0)
return value;
int position = i+1;
checksum += (value * position);
}
return checksum % 103;
}

/**
* Helper function for checksumInt
* Finds the number of the particular character using the Code128B table
* @param c
* @return
*/
public int charNumber(char c) {
for(int i = 0; i < code128_table.length; i++) {
if(code128_table[i].charAt(0) == c)
return i;
}
System.out.println("Could not find " + c);
return -1;
}

But wait, there’s more. The checksum itself isn’t enough, it needs to be in the form of a character, but since we already have the table for Code128B in array form, we just convert the integer to character for printing.

/**
* Converts calculated checksum into character
* @param i calculated checksum
* @return character for printing checksum
*/
public char checksumChar(int i) {
if(i < 95)
return (char) (i + 32);
else
return (char) (i+ 100);
}

Since the computers come with Norwegian keyboards and the barcode scanner will read barcodes as if receiving keyboard input, I had to do a few cheap hacks to remap the characters according to Norwegian keyboards, this isn’t a problem with alphanumeric data, but characters like { [ ] and } will read on a computer set with  a Norwegian keyboard layout as receiving the letters Æ Ø and Å or some other character because of this, I made a function with way too many if/else tests rather than using a HashMap or other key-value pair since it just seemed simpler to write at the time.

The class file is available on github here.

Sudoku

Finished the Sudoku-puzzle solver about two weeks ago, I might publish part of the solution later on when the course is finished, but at the moment here’s a few snapshots of how it looked like.

Bubbles

It’s been over a year. So long, even though I’ve been working on this page several times.

So what’s new?

I’ve started studying computer science at the University of Oslo, and spent most of the year learning about Java.

Only recently did I read up on applets in a book on game programming and found it so much easier than the troublesome JSF stuff I was reading up on, but I guess it’s probably like that for a reason, as I noticed applets are a real pain on the user side.

I’ve also registered for VPS to get a platform for programming experiments

As I haven’t had a chance to secure my server with SSL yet it’s probably “impossible” to run for most people (took me at least five minutes to get my Macbook to accept the applet).

After that I thought I’d try and play with it in photoshop. to my surprise a simple filter managed to turn the letters into bubbles:

Hopefully, later on I’ll be making some more interesting applications or applets. Looking forward to the next challenge at uni where we’re supposed to make an automatic Sudoku-puzzle solver.