Add Glyph.optical_distance().

This commit is contained in:
Andrey Golovizin 2014-08-25 16:59:19 +02:00
parent 49db236899
commit fac5d738a7

View file

@ -130,22 +130,6 @@ class Line(PageObject):
glyphs = self._combine_diacritics(glyphs)
return self._insert_spaces(glyphs)
def _optical_correction(self, glyph1, glyph2):
base = min(glyph1.top, glyph2.top)
height = max(glyph1.bottom, glyph2.bottom) - base
bitmap1 = np.hstack([np.ones((glyph1.height, 1)), glyph1.image.bitmap])
bitmap2 = np.hstack([glyph2.image.bitmap, np.ones((glyph2.height, 1))])
margin1 = np.zeros(height, np.int)
margin1.fill(glyph1.width)
margin2 = np.zeros(height, np.int)
margin2.fill(glyph2.width)
margin1[glyph1.top - base: glyph1.bottom - base] = np.fliplr(bitmap1).argmax(axis=1)
margin2[glyph2.top - base: glyph2.bottom - base] = bitmap2.argmax(axis=1)
margins = margin1 + margin2
return margins.min()
def _combine_diacritics(self, glyphs):
def find_correspondence(glyphs):
bodies = defaultdict(list)
@ -171,8 +155,7 @@ class Line(PageObject):
for glyph, next_glyph in pairwise(glyphs):
yield glyph
if next_glyph is not None:
correction = self._optical_correction(glyph, next_glyph)
distance = next_glyph.left - glyph.right + correction
distance = glyph.optical_distance(next_glyph)
if distance > 5:
yield Space(self.image.space(glyph.right, self.top, distance, self.height), self.baseline - self.top)
@ -198,6 +181,28 @@ class Glyph(PageObject):
"""Return True if the glyph is definitely not diacritic."""
return self.height >= self.MIN_BODY_HEIGHT
def _optical_correction(self, other):
glyph1 = self
glyph2 = other
base = min(glyph1.top, glyph2.top)
height = max(glyph1.bottom, glyph2.bottom) - base
bitmap1 = np.hstack([np.ones((glyph1.height, 1)), glyph1.image.bitmap])
bitmap2 = np.hstack([glyph2.image.bitmap, np.ones((glyph2.height, 1))])
margin1 = np.zeros(height, np.int)
margin1.fill(glyph1.width)
margin2 = np.zeros(height, np.int)
margin2.fill(glyph2.width)
margin1[glyph1.top - base: glyph1.bottom - base] = np.fliplr(bitmap1).argmax(axis=1)
margin2[glyph2.top - base: glyph2.bottom - base] = bitmap2.argmax(axis=1)
margins = margin1 + margin2
return margins.min()
def optical_distance(self, other):
distance = other.left - self.right
return distance + self._optical_correction(other)
def detect_diacritic(self, glyph):
"""Check if the given glyph can be our diacritic and return a numeric score."""