text

An Android method to center text when using Canvas drawText

I’m not an expert on this subject just yet, but if you want the source code for an Android method to center text that you want to use with the drawText method of the Canvas class, I know that this code works in two places in my current Android app:

How to test for the existence of a key or value in a Scala Map alvin June 6, 2015 - 3:25pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is one of the shortest recipes, Recipe 11.21, “How to Test for the Existence of a Key or Value in a Scala Map”

Problem

You want to test whether a Scala Map contains a given key or value.

Solution

To test for the existence of a key in a Map, use the contains method:

A Mac OS X text-to-speech web service (Play Framework, Scala)

I’ve written a variety of small Scala apps that take advantage of the “text to speech” capabilities on Mac OS X (Sarah, Wikipedia Page Reader), and a few days ago I started thinking about consolidating these by creating a Mac “text-to-speech service.” I initially created that as an Akka server (here on Github), then thought to make it a little more generic as a REST web service.

An Android GridView of text quotes (Fragment, layout, and AsyncTask source code)

Before I completely delete this code from my current Android application, I want to make a copy of it here. It was intended to show a series of quotes (text phrases) in a “Grid” (GridView), but (a) I never got it working as desired, and (b) I decided I didn’t want it in my application anyway.

Here’s the source code for the Java controller/fragment class:

Converting PDF content to plain text with Scala (or Java) alvin November 15, 2014 - 2:22pm

I recently wrote a little application to convert pages from a PDF to plain text. The GUI portion of the application looks like this:

As you can see, the application just needs the name of a PDF file to convert, along with the page you want to start at and the page you want to end at. There are several ways I could make the application more convenient to use, but since I don't plan to use it that often, I can deal with its limitations.

How to convert a stack trace to a string for printing with a logger

As a quick note, I just got a little bit better about logging stack traces when writing Java or Scala code. In Scala I used to get the text from a stack trace and then log it like this:

// this works, but it's not too useful/readable
logger.error(exception.getStackTrace.mkString("\n"))

In that code, getStackTrace returns a sequence, which I convert to a String before printing it.

A JavaScript function to extract text from an anchor tag (using a regex) alvin May 6, 2014 - 10:22am

There are probably better ways to do this, but here’s a JavaScript function I wrote for a Sencha ExtJS application that extracts the text from an anchor tag (hyperlink):

How to create outlined text using Gimp alvin April 26, 2014 - 10:27am

Here’s a quick look at how to create outlined text using Gimp. I don’t know if that’s the right term, but I’m thinking of the large white text you see with a black border that you usually see on meme images.

Here are the steps I just used:

  • Select the text you entered, such as, “I USED A var”. Make sure the Text Tool is active.
  • Click the “Path From Text” button in the Text Tool panel.
  • Click the Select menu, then “From Path”. You should see some action on screen here.
  • Create a transparent layer, move it below your text, and make it active.
  • Click Select, then Grow...
  • On the popup dialog, enter 3 pixels, 5 pixels, whatever you need for your font. When you click OK you should see the selection area on screen change size.
  • Click Edit menu, choose “Fill with BG Color”, or “Fill with FG Color”.

If you use a white font with a black background color, your image should look like this: