Add Glyph.optical_distance().
This commit is contained in:
parent
49db236899
commit
fac5d738a7
1 changed files with 23 additions and 18 deletions
|
|
@ -130,22 +130,6 @@ class Line(PageObject):
|
||||||
glyphs = self._combine_diacritics(glyphs)
|
glyphs = self._combine_diacritics(glyphs)
|
||||||
return self._insert_spaces(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 _combine_diacritics(self, glyphs):
|
||||||
def find_correspondence(glyphs):
|
def find_correspondence(glyphs):
|
||||||
bodies = defaultdict(list)
|
bodies = defaultdict(list)
|
||||||
|
|
@ -171,8 +155,7 @@ class Line(PageObject):
|
||||||
for glyph, next_glyph in pairwise(glyphs):
|
for glyph, next_glyph in pairwise(glyphs):
|
||||||
yield glyph
|
yield glyph
|
||||||
if next_glyph is not None:
|
if next_glyph is not None:
|
||||||
correction = self._optical_correction(glyph, next_glyph)
|
distance = glyph.optical_distance(next_glyph)
|
||||||
distance = next_glyph.left - glyph.right + correction
|
|
||||||
if distance > 5:
|
if distance > 5:
|
||||||
yield Space(self.image.space(glyph.right, self.top, distance, self.height), self.baseline - self.top)
|
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 True if the glyph is definitely not diacritic."""
|
||||||
return self.height >= self.MIN_BODY_HEIGHT
|
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):
|
def detect_diacritic(self, glyph):
|
||||||
"""Check if the given glyph can be our diacritic and return a numeric score."""
|
"""Check if the given glyph can be our diacritic and return a numeric score."""
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue