#!/usr/bin/env fontforge
#
# Dafydd Harries, 2005.
#
# Copy glyphs from one font to another.

if ($argc < 4)
	Print("Usage: " + $0 + " SOURCE DESTINATION GLYPH...")
	Quit(1)
endif

# Open the source font.

Print("Opening " + $1)
Open($1)

# Select the glyphs.

SelectNone()

i = 3

while (i < $argc)
	glyph = $argv[i]

	if (InFont(glyph))
		SelectMore(glyph)
	else
		Print("Glyph " + glyph + " missing in source")

		# Remove the glyph from the list of those to be added/copied.
		$argv[i] = ""
	endif

	i++
endloop

# Copy all the glyphs to the clipboard, close the source font and open the
# destiantion font.

Copy()
Close()
Print("Opening " + $2)
Open($2)

# Make sure all glyphs exist in the destination.

i = 3

while (i < $argc)
	glyph = $argv[i]

	if ($argv[i] != "")
		if (SelectIf(glyph) > 0)
			# The glyph exists already -- it will be replaced.
			Print("Copying glyph " + glyph)
		else
			# The glyph doesn't exist -- create it.
			Print("Adding glyph " + glyph)
			count = CharCnt()
			SetCharCnt(count + 1)
			SelectMore(count)
			SetCharName(glyph, 1)
		endif
	endif

	i++
endloop

# Select glyphs in the destination.

SelectNone()

i = 3

while (i < $argc)
	glyph = $argv[i]

	if ($argv[i] != "")
		SelectMore(glyph)
	endif

	i++
endloop

# Copy all the glyphs across.

Paste()

# Save the modified font to the destination.

Print("Generating " + $2)
Save($2)

