aboutsummaryrefslogtreecommitdiff
path: root/Lib/fontTools/ttLib/tables/E_B_D_T_.py
blob: 5d9e7244acad9c723d21df837f34e69348cadf36 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
from fontTools.misc.py23 import bytechr, byteord, bytesjoin, strjoin
from fontTools.misc import sstruct
from fontTools.misc.textTools import safeEval, readHex, hexStr, deHexStr
from .BitmapGlyphMetrics import BigGlyphMetrics, bigGlyphMetricsFormat, SmallGlyphMetrics, smallGlyphMetricsFormat
from . import DefaultTable
import itertools
import os
import struct
import logging


log = logging.getLogger(__name__)

ebdtTableVersionFormat = """
	> # big endian
	version: 16.16F
"""

ebdtComponentFormat = """
	> # big endian
	glyphCode: H
	xOffset:   b
	yOffset:   b
"""

class table_E_B_D_T_(DefaultTable.DefaultTable):

	# Keep a reference to the name of the data locator table.
	locatorName = 'EBLC'

	# This method can be overridden in subclasses to support new formats
	# without changing the other implementation. Also can be used as a
	# convenience method for coverting a font file to an alternative format.
	def getImageFormatClass(self, imageFormat):
		return ebdt_bitmap_classes[imageFormat]

	def decompile(self, data, ttFont):
		# Get the version but don't advance the slice.
		# Most of the lookup for this table is done relative
		# to the begining so slice by the offsets provided
		# in the EBLC table.
		sstruct.unpack2(ebdtTableVersionFormat, data, self)

		# Keep a dict of glyphs that have been seen so they aren't remade.
		# This dict maps intervals of data to the BitmapGlyph.
		glyphDict = {}

		# Pull out the EBLC table and loop through glyphs.
		# A strike is a concept that spans both tables.
		# The actual bitmap data is stored in the EBDT.
		locator = ttFont[self.__class__.locatorName]
		self.strikeData = []
		for curStrike in locator.strikes:
			bitmapGlyphDict = {}
			self.strikeData.append(bitmapGlyphDict)
			for indexSubTable in curStrike.indexSubTables:
				dataIter = zip(indexSubTable.names, indexSubTable.locations)
				for curName, curLoc in dataIter:
					# Don't create duplicate data entries for the same glyphs.
					# Instead just use the structures that already exist if they exist.
					if curLoc in glyphDict:
						curGlyph = glyphDict[curLoc]
					else:
						curGlyphData = data[slice(*curLoc)]
						imageFormatClass = self.getImageFormatClass(indexSubTable.imageFormat)
						curGlyph = imageFormatClass(curGlyphData, ttFont)
						glyphDict[curLoc] = curGlyph
					bitmapGlyphDict[curName] = curGlyph

	def compile(self, ttFont):

		dataList = []
		dataList.append(sstruct.pack(ebdtTableVersionFormat, self))
		dataSize = len(dataList[0])

		# Keep a dict of glyphs that have been seen so they aren't remade.
		# This dict maps the id of the BitmapGlyph to the interval
		# in the data.
		glyphDict = {}

		# Go through the bitmap glyph data. Just in case the data for a glyph
		# changed the size metrics should be recalculated. There are a variety
		# of formats and they get stored in the EBLC table. That is why
		# recalculation is defered to the EblcIndexSubTable class and just
		# pass what is known about bitmap glyphs from this particular table.
		locator = ttFont[self.__class__.locatorName]
		for curStrike, curGlyphDict in zip(locator.strikes, self.strikeData):
			for curIndexSubTable in curStrike.indexSubTables:
				dataLocations = []
				for curName in curIndexSubTable.names:
					# Handle the data placement based on seeing the glyph or not.
					# Just save a reference to the location if the glyph has already
					# been saved in compile. This code assumes that glyphs will only
					# be referenced multiple times from indexFormat5. By luck the
					# code may still work when referencing poorly ordered fonts with
					# duplicate references. If there is a font that is unlucky the
					# respective compile methods for the indexSubTables will fail
					# their assertions. All fonts seem to follow this assumption.
					# More complicated packing may be needed if a counter-font exists.
					glyph = curGlyphDict[curName]
					objectId = id(glyph)
					if objectId not in glyphDict:
						data = glyph.compile(ttFont)
						data = curIndexSubTable.padBitmapData(data)
						startByte = dataSize
						dataSize += len(data)
						endByte = dataSize
						dataList.append(data)
						dataLoc = (startByte, endByte)
						glyphDict[objectId] = dataLoc
					else:
						dataLoc = glyphDict[objectId]
					dataLocations.append(dataLoc)
				# Just use the new data locations in the indexSubTable.
				# The respective compile implementations will take care
				# of any of the problems in the convertion that may arise.
				curIndexSubTable.locations = dataLocations

		return bytesjoin(dataList)

	def toXML(self, writer, ttFont):
		# When exporting to XML if one of the data export formats
		# requires metrics then those metrics may be in the locator.
		# In this case populate the bitmaps with "export metrics".
		if ttFont.bitmapGlyphDataFormat in ('row', 'bitwise'):
			locator = ttFont[self.__class__.locatorName]
			for curStrike, curGlyphDict in zip(locator.strikes, self.strikeData):
				for curIndexSubTable in curStrike.indexSubTables:
					for curName in curIndexSubTable.names:
						glyph = curGlyphDict[curName]
						# I'm not sure which metrics have priority here.
						# For now if both metrics exist go with glyph metrics.
						if hasattr(glyph, 'metrics'):
							glyph.exportMetrics = glyph.metrics
						else:
							glyph.exportMetrics = curIndexSubTable.metrics
						glyph.exportBitDepth = curStrike.bitmapSizeTable.bitDepth

		writer.simpletag("header", [('version', self.version)])
		writer.newline()
		locator = ttFont[self.__class__.locatorName]
		for strikeIndex, bitmapGlyphDict in enumerate(self.strikeData):
			writer.begintag('strikedata', [('index', strikeIndex)])
			writer.newline()
			for curName, curBitmap in bitmapGlyphDict.items():
				curBitmap.toXML(strikeIndex, curName, writer, ttFont)
			writer.endtag('strikedata')
			writer.newline()

	def fromXML(self, name, attrs, content, ttFont):
		if name == 'header':
			self.version = safeEval(attrs['version'])
		elif name == 'strikedata':
			if not hasattr(self, 'strikeData'):
				self.strikeData = []
			strikeIndex = safeEval(attrs['index'])

			bitmapGlyphDict = {}
			for element in content:
				if not isinstance(element, tuple):
					continue
				name, attrs, content = element
				if name[4:].startswith(_bitmapGlyphSubclassPrefix[4:]):
					imageFormat = safeEval(name[len(_bitmapGlyphSubclassPrefix):])
					glyphName = attrs['name']
					imageFormatClass = self.getImageFormatClass(imageFormat)
					curGlyph = imageFormatClass(None, None)
					curGlyph.fromXML(name, attrs, content, ttFont)
					assert glyphName not in bitmapGlyphDict, "Duplicate glyphs with the same name '%s' in the same strike." % glyphName
					bitmapGlyphDict[glyphName] = curGlyph
				else:
					log.warning("%s being ignored by %s", name, self.__class__.__name__)

			# Grow the strike data array to the appropriate size. The XML
			# format allows the strike index value to be out of order.
			if strikeIndex >= len(self.strikeData):
				self.strikeData += [None] * (strikeIndex + 1 - len(self.strikeData))
			assert self.strikeData[strikeIndex] is None, "Duplicate strike EBDT indices."
			self.strikeData[strikeIndex] = bitmapGlyphDict

class EbdtComponent(object):

	def toXML(self, writer, ttFont):
		writer.begintag('ebdtComponent', [('name', self.name)])
		writer.newline()
		for componentName in sstruct.getformat(ebdtComponentFormat)[1][1:]:
			writer.simpletag(componentName, value=getattr(self, componentName))
			writer.newline()
		writer.endtag('ebdtComponent')
		writer.newline()

	def fromXML(self, name, attrs, content, ttFont):
		self.name = attrs['name']
		componentNames = set(sstruct.getformat(ebdtComponentFormat)[1][1:])
		for element in content:
			if not isinstance(element, tuple):
				continue
			name, attrs, content = element
			if name in componentNames:
				vars(self)[name] = safeEval(attrs['value'])
			else:
				log.warning("unknown name '%s' being ignored by EbdtComponent.", name)

# Helper functions for dealing with binary.

def _data2binary(data, numBits):
	binaryList = []
	for curByte in data:
		value = byteord(curByte)
		numBitsCut = min(8, numBits)
		for i in range(numBitsCut):
			if value & 0x1:
				binaryList.append('1')
			else:
				binaryList.append('0')
			value = value >> 1
		numBits -= numBitsCut
	return strjoin(binaryList)

def _binary2data(binary):
	byteList = []
	for bitLoc in range(0, len(binary), 8):
		byteString = binary[bitLoc:bitLoc+8]
		curByte = 0
		for curBit in reversed(byteString):
			curByte = curByte << 1
			if curBit == '1':
				curByte |= 1
		byteList.append(bytechr(curByte))
	return bytesjoin(byteList)

def _memoize(f):
	class memodict(dict):
		def __missing__(self, key):
			ret = f(key)
			if len(key) == 1:
				self[key] = ret
			return ret
	return memodict().__getitem__

# 00100111 -> 11100100 per byte, not to be confused with little/big endian.
# Bitmap data per byte is in the order that binary is written on the page
# with the least significant bit as far right as possible. This is the
# opposite of what makes sense algorithmically and hence this function.
@_memoize
def _reverseBytes(data):
	if len(data) != 1:
		return bytesjoin(map(_reverseBytes, data))
	byte = byteord(data)
	result = 0
	for i in range(8):
		result = result << 1
		result |= byte & 1
		byte = byte >> 1
	return bytechr(result)

# This section of code is for reading and writing image data to/from XML.

def _writeRawImageData(strikeIndex, glyphName, bitmapObject, writer, ttFont):
	writer.begintag('rawimagedata')
	writer.newline()
	writer.dumphex(bitmapObject.imageData)
	writer.endtag('rawimagedata')
	writer.newline()

def _readRawImageData(bitmapObject, name, attrs, content, ttFont):
	bitmapObject.imageData = readHex(content)

def _writeRowImageData(strikeIndex, glyphName, bitmapObject, writer, ttFont):
	metrics = bitmapObject.exportMetrics
	del bitmapObject.exportMetrics
	bitDepth = bitmapObject.exportBitDepth
	del bitmapObject.exportBitDepth

	writer.begintag('rowimagedata', bitDepth=bitDepth, width=metrics.width, height=metrics.height)
	writer.newline()
	for curRow in range(metrics.height):
		rowData = bitmapObject.getRow(curRow, bitDepth=bitDepth, metrics=metrics)
		writer.simpletag('row', value=hexStr(rowData))
		writer.newline()
	writer.endtag('rowimagedata')
	writer.newline()

def _readRowImageData(bitmapObject, name, attrs, content, ttFont):
	bitDepth = safeEval(attrs['bitDepth'])
	metrics = SmallGlyphMetrics()
	metrics.width = safeEval(attrs['width'])
	metrics.height = safeEval(attrs['height'])

	dataRows = []
	for element in content:
		if not isinstance(element, tuple):
			continue
		name, attr, content = element
		# Chop off 'imagedata' from the tag to get just the option.
		if name == 'row':
			dataRows.append(deHexStr(attr['value']))
	bitmapObject.setRows(dataRows, bitDepth=bitDepth, metrics=metrics)

def _writeBitwiseImageData(strikeIndex, glyphName, bitmapObject, writer, ttFont):
	metrics = bitmapObject.exportMetrics
	del bitmapObject.exportMetrics
	bitDepth = bitmapObject.exportBitDepth
	del bitmapObject.exportBitDepth

	# A dict for mapping binary to more readable/artistic ASCII characters.
	binaryConv = {'0':'.', '1':'@'}

	writer.begintag('bitwiseimagedata', bitDepth=bitDepth, width=metrics.width, height=metrics.height)
	writer.newline()
	for curRow in range(metrics.height):
		rowData = bitmapObject.getRow(curRow, bitDepth=1, metrics=metrics, reverseBytes=True)
		rowData = _data2binary(rowData, metrics.width)
		# Make the output a readable ASCII art form.
		rowData = strjoin(map(binaryConv.get, rowData))
		writer.simpletag('row', value=rowData)
		writer.newline()
	writer.endtag('bitwiseimagedata')
	writer.newline()

def _readBitwiseImageData(bitmapObject, name, attrs, content, ttFont):
	bitDepth = safeEval(attrs['bitDepth'])
	metrics = SmallGlyphMetrics()
	metrics.width = safeEval(attrs['width'])
	metrics.height = safeEval(attrs['height'])

	# A dict for mapping from ASCII to binary. All characters are considered
	# a '1' except space, period and '0' which maps to '0'.
	binaryConv = {' ':'0', '.':'0', '0':'0'}

	dataRows = []
	for element in content:
		if not isinstance(element, tuple):
			continue
		name, attr, content = element
		if name == 'row':
			mapParams = zip(attr['value'], itertools.repeat('1'))
			rowData = strjoin(itertools.starmap(binaryConv.get, mapParams))
			dataRows.append(_binary2data(rowData))

	bitmapObject.setRows(dataRows, bitDepth=bitDepth, metrics=metrics, reverseBytes=True)

def _writeExtFileImageData(strikeIndex, glyphName, bitmapObject, writer, ttFont):
	try:
		folder = os.path.dirname(writer.file.name)
	except AttributeError:
		# fall back to current directory if output file's directory isn't found
		folder = '.'
	folder = os.path.join(folder, 'bitmaps')
	filename = glyphName + bitmapObject.fileExtension
	if not os.path.isdir(folder):
		os.makedirs(folder)
	folder = os.path.join(folder, 'strike%d' % strikeIndex)
	if not os.path.isdir(folder):
		os.makedirs(folder)

	fullPath = os.path.join(folder, filename)
	writer.simpletag('extfileimagedata', value=fullPath)
	writer.newline()

	with open(fullPath, "wb") as file:
		file.write(bitmapObject.imageData)

def _readExtFileImageData(bitmapObject, name, attrs, content, ttFont):
	fullPath = attrs['value']
	with open(fullPath, "rb") as file:
		bitmapObject.imageData = file.read()

# End of XML writing code.

# Important information about the naming scheme. Used for identifying formats
# in XML.
_bitmapGlyphSubclassPrefix = 'ebdt_bitmap_format_'

class BitmapGlyph(object):

	# For the external file format. This can be changed in subclasses. This way
	# when the extfile option is turned on files have the form: glyphName.ext
	# The default is just a flat binary file with no meaning.
	fileExtension = '.bin'

	# Keep track of reading and writing of various forms.
	xmlDataFunctions = {
		'raw':		(_writeRawImageData, _readRawImageData),
		'row':		(_writeRowImageData, _readRowImageData),
		'bitwise':	(_writeBitwiseImageData, _readBitwiseImageData),
		'extfile':	(_writeExtFileImageData, _readExtFileImageData),
		}

	def __init__(self, data, ttFont):
		self.data = data
		self.ttFont = ttFont
		# TODO Currently non-lazy decompilation is untested here...
		#if not ttFont.lazy:
		#	self.decompile()
		#	del self.data

	def __getattr__(self, attr):
		# Allow lazy decompile.
		if attr[:2] == '__':
			raise AttributeError(attr)
		if not hasattr(self, "data"):
			raise AttributeError(attr)
		self.decompile()
		del self.data
		return getattr(self, attr)

	# Not a fan of this but it is needed for safer safety checking.
	def getFormat(self):
		return safeEval(self.__class__.__name__[len(_bitmapGlyphSubclassPrefix):])

	def toXML(self, strikeIndex, glyphName, writer, ttFont):
		writer.begintag(self.__class__.__name__, [('name', glyphName)])
		writer.newline()

		self.writeMetrics(writer, ttFont)
		# Use the internal write method to write using the correct output format.
		self.writeData(strikeIndex, glyphName, writer, ttFont)

		writer.endtag(self.__class__.__name__)
		writer.newline()

	def fromXML(self, name, attrs, content, ttFont):
		self.readMetrics(name, attrs, content, ttFont)
		for element in content:
			if not isinstance(element, tuple):
				continue
			name, attr, content = element
			if not name.endswith('imagedata'):
				continue
			# Chop off 'imagedata' from the tag to get just the option.
			option = name[:-len('imagedata')]
			assert option in self.__class__.xmlDataFunctions
			self.readData(name, attr, content, ttFont)

	# Some of the glyphs have the metrics. This allows for metrics to be
	# added if the glyph format has them. Default behavior is to do nothing.
	def writeMetrics(self, writer, ttFont):
		pass

	# The opposite of write metrics.
	def readMetrics(self, name, attrs, content, ttFont):
		pass

	def writeData(self, strikeIndex, glyphName, writer, ttFont):
		try:
			writeFunc, readFunc = self.__class__.xmlDataFunctions[ttFont.bitmapGlyphDataFormat]
		except KeyError:
			writeFunc = _writeRawImageData
		writeFunc(strikeIndex, glyphName, self, writer, ttFont)

	def readData(self, name, attrs, content, ttFont):
		# Chop off 'imagedata' from the tag to get just the option.
		option = name[:-len('imagedata')]
		writeFunc, readFunc = self.__class__.xmlDataFunctions[option]
		readFunc(self, name, attrs, content, ttFont)


# A closure for creating a mixin for the two types of metrics handling.
# Most of the code is very similar so its easier to deal with here.
# Everything works just by passing the class that the mixin is for.
def _createBitmapPlusMetricsMixin(metricsClass):
	# Both metrics names are listed here to make meaningful error messages.
	metricStrings = [BigGlyphMetrics.__name__, SmallGlyphMetrics.__name__]
	curMetricsName = metricsClass.__name__
	# Find which metrics this is for and determine the opposite name.
	metricsId = metricStrings.index(curMetricsName)
	oppositeMetricsName = metricStrings[1-metricsId]

	class BitmapPlusMetricsMixin(object):

		def writeMetrics(self, writer, ttFont):
			self.metrics.toXML(writer, ttFont)

		def readMetrics(self, name, attrs, content, ttFont):
			for element in content:
				if not isinstance(element, tuple):
					continue
				name, attrs, content = element
				if name == curMetricsName:
					self.metrics = metricsClass()
					self.metrics.fromXML(name, attrs, content, ttFont)
				elif name == oppositeMetricsName:
					log.warning("Warning: %s being ignored in format %d.", oppositeMetricsName, self.getFormat())

	return BitmapPlusMetricsMixin

# Since there are only two types of mixin's just create them here.
BitmapPlusBigMetricsMixin = _createBitmapPlusMetricsMixin(BigGlyphMetrics)
BitmapPlusSmallMetricsMixin = _createBitmapPlusMetricsMixin(SmallGlyphMetrics)

# Data that is bit aligned can be tricky to deal with. These classes implement
# helper functionality for dealing with the data and getting a particular row
# of bitwise data. Also helps implement fancy data export/import in XML.
class BitAlignedBitmapMixin(object):

	def _getBitRange(self, row, bitDepth, metrics):
		rowBits = (bitDepth * metrics.width)
		bitOffset = row * rowBits
		return (bitOffset, bitOffset+rowBits)

	def getRow(self, row, bitDepth=1, metrics=None, reverseBytes=False):
		if metrics is None:
			metrics = self.metrics
		assert 0 <= row and row < metrics.height, "Illegal row access in bitmap"

		# Loop through each byte. This can cover two bytes in the original data or
		# a single byte if things happen to be aligned. The very last entry might
		# not be aligned so take care to trim the binary data to size and pad with
		# zeros in the row data. Bit aligned data is somewhat tricky.
		#
		# Example of data cut. Data cut represented in x's.
		# '|' represents byte boundary.
		# data = ...0XX|XXXXXX00|000... => XXXXXXXX
		#		or
		# data = ...0XX|XXXX0000|000... => XXXXXX00
		#   or
		# data = ...000|XXXXXXXX|000... => XXXXXXXX
		#   or
		# data = ...000|00XXXX00|000... => XXXX0000
		#
		dataList = []
		bitRange = self._getBitRange(row, bitDepth, metrics)
		stepRange = bitRange + (8,)
		for curBit in range(*stepRange):
			endBit = min(curBit+8, bitRange[1])
			numBits = endBit - curBit
			cutPoint = curBit % 8
			firstByteLoc = curBit // 8
			secondByteLoc = endBit // 8
			if firstByteLoc < secondByteLoc:
				numBitsCut = 8 - cutPoint
			else:
				numBitsCut = endBit - curBit
			curByte = _reverseBytes(self.imageData[firstByteLoc])
			firstHalf = byteord(curByte) >> cutPoint
			firstHalf = ((1<<numBitsCut)-1) & firstHalf
			newByte = firstHalf
			if firstByteLoc < secondByteLoc and secondByteLoc < len(self.imageData):
				curByte = _reverseBytes(self.imageData[secondByteLoc])
				secondHalf = byteord(curByte) << numBitsCut
				newByte = (firstHalf | secondHalf) & ((1<<numBits)-1)
			dataList.append(bytechr(newByte))

		# The way the data is kept is opposite the algorithm used.
		data = bytesjoin(dataList)
		if not reverseBytes:
			data = _reverseBytes(data)
		return data

	def setRows(self, dataRows, bitDepth=1, metrics=None, reverseBytes=False):
		if metrics is None:
			metrics = self.metrics
		if not reverseBytes:
			dataRows = list(map(_reverseBytes, dataRows))

		# Keep track of a list of ordinal values as they are easier to modify
		# than a list of strings. Map to actual strings later.
		numBytes = (self._getBitRange(len(dataRows), bitDepth, metrics)[0] + 7) // 8
		ordDataList = [0] * numBytes
		for row, data in enumerate(dataRows):
			bitRange = self._getBitRange(row, bitDepth, metrics)
			stepRange = bitRange + (8,)
			for curBit, curByte in zip(range(*stepRange), data):
				endBit = min(curBit+8, bitRange[1])
				cutPoint = curBit % 8
				firstByteLoc = curBit // 8
				secondByteLoc = endBit // 8
				if firstByteLoc < secondByteLoc:
					numBitsCut = 8 - cutPoint
				else:
					numBitsCut = endBit - curBit
				curByte = byteord(curByte)
				firstByte = curByte & ((1<<numBitsCut)-1)
				ordDataList[firstByteLoc] |= (firstByte << cutPoint)
				if firstByteLoc < secondByteLoc and secondByteLoc < numBytes:
					secondByte = (curByte >> numBitsCut) & ((1<<8-numBitsCut)-1)
					ordDataList[secondByteLoc] |= secondByte

		# Save the image data with the bits going the correct way.
		self.imageData = _reverseBytes(bytesjoin(map(bytechr, ordDataList)))

class ByteAlignedBitmapMixin(object):

	def _getByteRange(self, row, bitDepth, metrics):
		rowBytes = (bitDepth * metrics.width + 7) // 8
		byteOffset = row * rowBytes
		return (byteOffset, byteOffset+rowBytes)

	def getRow(self, row, bitDepth=1, metrics=None, reverseBytes=False):
		if metrics is None:
			metrics = self.metrics
		assert 0 <= row and row < metrics.height, "Illegal row access in bitmap"
		byteRange = self._getByteRange(row, bitDepth, metrics)
		data = self.imageData[slice(*byteRange)]
		if reverseBytes:
			data = _reverseBytes(data)
		return data

	def setRows(self, dataRows, bitDepth=1, metrics=None, reverseBytes=False):
		if metrics is None:
			metrics = self.metrics
		if reverseBytes:
			dataRows = map(_reverseBytes, dataRows)
		self.imageData = bytesjoin(dataRows)

class ebdt_bitmap_format_1(ByteAlignedBitmapMixin, BitmapPlusSmallMetricsMixin, BitmapGlyph):

	def decompile(self):
		self.metrics = SmallGlyphMetrics()
		dummy, data = sstruct.unpack2(smallGlyphMetricsFormat, self.data, self.metrics)
		self.imageData = data

	def compile(self, ttFont):
		data = sstruct.pack(smallGlyphMetricsFormat, self.metrics)
		return data + self.imageData


class ebdt_bitmap_format_2(BitAlignedBitmapMixin, BitmapPlusSmallMetricsMixin, BitmapGlyph):

	def decompile(self):
		self.metrics = SmallGlyphMetrics()
		dummy, data = sstruct.unpack2(smallGlyphMetricsFormat, self.data, self.metrics)
		self.imageData = data

	def compile(self, ttFont):
		data = sstruct.pack(smallGlyphMetricsFormat, self.metrics)
		return data + self.imageData


class ebdt_bitmap_format_5(BitAlignedBitmapMixin, BitmapGlyph):

	def decompile(self):
		self.imageData = self.data

	def compile(self, ttFont):
		return self.imageData

class ebdt_bitmap_format_6(ByteAlignedBitmapMixin, BitmapPlusBigMetricsMixin, BitmapGlyph):

	def decompile(self):
		self.metrics = BigGlyphMetrics()
		dummy, data = sstruct.unpack2(bigGlyphMetricsFormat, self.data, self.metrics)
		self.imageData = data

	def compile(self, ttFont):
		data = sstruct.pack(bigGlyphMetricsFormat, self.metrics)
		return data + self.imageData


class ebdt_bitmap_format_7(BitAlignedBitmapMixin, BitmapPlusBigMetricsMixin, BitmapGlyph):

	def decompile(self):
		self.metrics = BigGlyphMetrics()
		dummy, data = sstruct.unpack2(bigGlyphMetricsFormat, self.data, self.metrics)
		self.imageData = data

	def compile(self, ttFont):
		data = sstruct.pack(bigGlyphMetricsFormat, self.metrics)
		return data + self.imageData


class ComponentBitmapGlyph(BitmapGlyph):

	def toXML(self, strikeIndex, glyphName, writer, ttFont):
		writer.begintag(self.__class__.__name__, [('name', glyphName)])
		writer.newline()

		self.writeMetrics(writer, ttFont)

		writer.begintag('components')
		writer.newline()
		for curComponent in self.componentArray:
			curComponent.toXML(writer, ttFont)
		writer.endtag('components')
		writer.newline()

		writer.endtag(self.__class__.__name__)
		writer.newline()

	def fromXML(self, name, attrs, content, ttFont):
		self.readMetrics(name, attrs, content, ttFont)
		for element in content:
			if not isinstance(element, tuple):
				continue
			name, attr, content = element
			if name == 'components':
				self.componentArray = []
				for compElement in content:
					if not isinstance(compElement, tuple):
						continue
					name, attrs, content = compElement
					if name == 'ebdtComponent':
						curComponent = EbdtComponent()
						curComponent.fromXML(name, attrs, content, ttFont)
						self.componentArray.append(curComponent)
					else:
						log.warning("'%s' being ignored in component array.", name)


class ebdt_bitmap_format_8(BitmapPlusSmallMetricsMixin, ComponentBitmapGlyph):

	def decompile(self):
		self.metrics = SmallGlyphMetrics()
		dummy, data = sstruct.unpack2(smallGlyphMetricsFormat, self.data, self.metrics)
		data = data[1:]

		(numComponents,) = struct.unpack(">H", data[:2])
		data = data[2:]
		self.componentArray = []
		for i in range(numComponents):
			curComponent = EbdtComponent()
			dummy, data = sstruct.unpack2(ebdtComponentFormat, data, curComponent)
			curComponent.name = self.ttFont.getGlyphName(curComponent.glyphCode)
			self.componentArray.append(curComponent)

	def compile(self, ttFont):
		dataList = []
		dataList.append(sstruct.pack(smallGlyphMetricsFormat, self.metrics))
		dataList.append(b'\0')
		dataList.append(struct.pack(">H", len(self.componentArray)))
		for curComponent in self.componentArray:
			curComponent.glyphCode = ttFont.getGlyphID(curComponent.name)
			dataList.append(sstruct.pack(ebdtComponentFormat, curComponent))
		return bytesjoin(dataList)


class ebdt_bitmap_format_9(BitmapPlusBigMetricsMixin, ComponentBitmapGlyph):

	def decompile(self):
		self.metrics = BigGlyphMetrics()
		dummy, data = sstruct.unpack2(bigGlyphMetricsFormat, self.data, self.metrics)
		(numComponents,) = struct.unpack(">H", data[:2])
		data = data[2:]
		self.componentArray = []
		for i in range(numComponents):
			curComponent = EbdtComponent()
			dummy, data = sstruct.unpack2(ebdtComponentFormat, data, curComponent)
			curComponent.name = self.ttFont.getGlyphName(curComponent.glyphCode)
			self.componentArray.append(curComponent)

	def compile(self, ttFont):
		dataList = []
		dataList.append(sstruct.pack(bigGlyphMetricsFormat, self.metrics))
		dataList.append(struct.pack(">H", len(self.componentArray)))
		for curComponent in self.componentArray:
			curComponent.glyphCode = ttFont.getGlyphID(curComponent.name)
			dataList.append(sstruct.pack(ebdtComponentFormat, curComponent))
		return bytesjoin(dataList)


# Dictionary of bitmap formats to the class representing that format
# currently only the ones listed in this map are the ones supported.
ebdt_bitmap_classes = {
		1: ebdt_bitmap_format_1,
		2: ebdt_bitmap_format_2,
		5: ebdt_bitmap_format_5,
		6: ebdt_bitmap_format_6,
		7: ebdt_bitmap_format_7,
		8: ebdt_bitmap_format_8,
		9: ebdt_bitmap_format_9,
	}