summaryrefslogtreecommitdiff
path: root/scripts/newbitmaps/strings/text_to_png
blob: 733e664661b4f84d2fd7caeda2ce6b5785904eaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash -e
# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Render a text file into a bitmap. Files named '*.txt' are normal text files,
# while '*.TXT' files may contain markup.
#
# Options:
#
#    --lan=LANGUAGE     Render language (locale) settings
#    --font=FONTNAME    Use specified font (instead of Droid Sans)
#    --point=POINT      Font size, in points (72dpi)
#    --margin=MARGIN    Set different margin (usually for font bitmaps)
#    --color=COLOR      Override foreground color (in '#ffffff' format)
#    --align=ALING      Override align settings (default: center)
#    --markup           Render text as pango-view markup file
#
font="Droid Sans"
language=""
pointsize=15
margin=3
align="center"
bgcolor="#ffffff"
color="#000000"
params=""

while true ; do
  case "$1" in
    --lan=* | --language=*)
      language="--language=${1##*=}"
      shift
      ;;
    --font=*)
      # Allows empty string = default font.
      param="${1##*=}"
      # TODO(hungte) support "+XXX" = add font description (ex, BOLD).
      [ -n "$param" ] && font="$param"
      shift
      ;;
    --align=*)
      align="${1##*=}"
      shift
      ;;
    --color=*)
      color="${1##*=}"
      shift
      ;;
    --point=*)
      pointsize="${1##*=}"
      shift
      ;;
    --margin=*)
      margin="${1##*=}"
      shift
      ;;
    --markup)
      params="$params --markup"
      shift
      ;;
    *)
      break
      ;;
  esac
done

# Revise color. pango-view color syntax (in #rrggbb) may cause problem in shell
# scripts, so let's also allow 'rrggbb'.
color="${color###}"
bgcolor="${bgcolor###}"
[ -z "$color" ] || color="#$color"
[ -z "$bgcolor" ] || bgcolor="#$bgcolor"

# Image parameters
# - New pango-view has --pixel to assign font size in pixel, but that is not
#   supported by old (ex, 1.24.5 in chroot) so we must assign --dpi 72 for
#   pointsize.

for txtfile in $*; do
  # pango-view does not support assigning output format options for bitmap, so
  # we must create images in PNG format and then post-process it (ex, convert
  # into BMP by ImageMagick).
  pngfile="${txtfile%.*}".png
  file_opt=""
  case "$txtfile" in
    *.txt)
      file_opt=""
      ;;
    *.TXT)
      file_opt="--markup "
      ;;
    *)
      echo "Ignoring $txtfile. Filename should end with .txt or .TXT"
      continue
      ;;
  esac
  pango-view -q $language \
    --hinting=full \
    --background="$bgcolor" --foreground="$color" \
    --font="$font $pointsize" --dpi 72 \
    --margin=$margin \
    --align="$align" \
    $params $file_opt \
    --output "$pngfile" \
    "$txtfile"
  echo "wrote $pngfile"
done