Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/fontTools/ttLib/tables/_l_o_c_a.py: 28%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

46 statements  

1from . import DefaultTable 

2import sys 

3import array 

4import logging 

5 

6 

7log = logging.getLogger(__name__) 

8 

9 

10class table__l_o_c_a(DefaultTable.DefaultTable): 

11 """Index to Location table 

12 

13 The ``loca`` table stores the offsets in the ``glyf`` table that correspond 

14 to the descriptions of each glyph. The glyphs are references by Glyph ID. 

15 

16 See also https://learn.microsoft.com/en-us/typography/opentype/spec/loca 

17 """ 

18 

19 dependencies = ["glyf"] 

20 

21 def decompile(self, data, ttFont): 

22 longFormat = ttFont["head"].indexToLocFormat 

23 if longFormat: 

24 format = "I" 

25 else: 

26 format = "H" 

27 locations = array.array(format) 

28 locations.frombytes(data) 

29 if sys.byteorder != "big": 

30 locations.byteswap() 

31 if not longFormat: 

32 locations = array.array("I", (2 * l for l in locations)) 

33 if len(locations) < (ttFont["maxp"].numGlyphs + 1): 

34 log.warning( 

35 "corrupt 'loca' table, or wrong numGlyphs in 'maxp': %d %d", 

36 len(locations) - 1, 

37 ttFont["maxp"].numGlyphs, 

38 ) 

39 self.locations = locations 

40 

41 def compile(self, ttFont): 

42 try: 

43 max_location = max(self.locations) 

44 except AttributeError: 

45 self.set([]) 

46 max_location = 0 

47 if max_location < 0x20000 and all(l % 2 == 0 for l in self.locations): 

48 locations = array.array("H") 

49 for i in range(len(self.locations)): 

50 locations.append(self.locations[i] // 2) 

51 ttFont["head"].indexToLocFormat = 0 

52 else: 

53 locations = array.array("I", self.locations) 

54 ttFont["head"].indexToLocFormat = 1 

55 if sys.byteorder != "big": 

56 locations.byteswap() 

57 return locations.tobytes() 

58 

59 def set(self, locations): 

60 self.locations = array.array("I", locations) 

61 

62 def toXML(self, writer, ttFont): 

63 writer.comment("The 'loca' table will be calculated by the compiler") 

64 writer.newline() 

65 

66 def __getitem__(self, index): 

67 return self.locations[index] 

68 

69 def __len__(self): 

70 return len(self.locations)