Skip to content

Commit

Permalink
calculate diff to determine width and height
Browse files Browse the repository at this point in the history
  • Loading branch information
jburel committed Jan 11, 2024
1 parent b819982 commit 8e4154f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/omero/gateway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9478,7 +9478,8 @@ def _wordwrap(self, width, text, font):
p1 = 0
p2 = 1
while (p2 <= len(tokens) and
font.getbbox(' '.join(tokens[p1:p2]))[2] < width):
(font.getbbox(' '.join(tokens[p1:p2]))[2] -
font.getbbox(' '.join(tokens[p1:p2]))[0]) < width):
p2 += 1
rv.append(' '.join(tokens[p1:p2-1]))
tokens = tokens[p2-1:]
Expand Down
11 changes: 8 additions & 3 deletions src/omero/util/figureUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,12 @@ def getVerticalLabels(labels, font, textGap):

maxWidth = 0
height = 0
textHeight = font.getbbox("testq")[3]
box = font.getbbox("testq")
textHeight = box[3] - box[1]
for label in labels:
maxWidth = max(maxWidth, font.getbbox(label)[2])
box = font.getbbox(label)
width = box[2] - box[0]
maxWidth = max(maxWidth, width)
if height > 0:
height += textGap
height += textHeight
Expand All @@ -292,7 +295,9 @@ def getVerticalLabels(labels, font, textGap):
textdraw = ImageDraw.Draw(textCanvas)
py = 0
for label in labels:
indent = old_div((maxWidth - font.getbbox(label)[2]), 2)
box = font.getbbox(label)
width = box[2] - box[0]
indent = old_div((maxWidth - width), 2)
textdraw.text((indent, py), label, font=font, fill=(0, 0, 0))
py += textHeight + textGap
return textCanvas.rotate(90)
11 changes: 8 additions & 3 deletions src/omero/util/imageUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,14 @@ def paintThumbnailGrid(thumbnailStore, length, spacing, pixelIds, colCount,
fontsize = old_div(length, 10) + 5
font = getFont(fontsize)
if leftLabel:
textWidth, textHeight = font.getbbox(leftLabel)[2:]
box = font.getbbox(leftLabel)
textWidth = box[2] - box[0]
textHeight = box[3] - box[1]
leftSpace = spacing + textHeight + spacing
if topLabel:
textWidth, textHeight = font.getbbox(topLabel)[2:]
box = font.getbbox(topLabel)
textWidth = box[2] - box[0]
textHeight = box[3] - box[1]
topSpace = spacing + textHeight + spacing
minWidth = leftSpace + textWidth + spacing

Expand All @@ -200,7 +204,8 @@ def paintThumbnailGrid(thumbnailStore, length, spacing, pixelIds, colCount,
labelSize = (labelCanvasWidth, labelCanvasHeight)
textCanvas = Image.new(mode, labelSize, bg)
draw = ImageDraw.Draw(textCanvas)
textWidth = font.getbbox(leftLabel)[2]
box = font.getbbox(leftLabel)
textWidth = box[2] - box[0]
textX = old_div((labelCanvasWidth - textWidth), 2)
draw.text((textX, spacing), leftLabel, font=font, fill=textColour)
verticalCanvas = textCanvas.rotate(90)
Expand Down
11 changes: 8 additions & 3 deletions src/omero/util/image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,14 @@ def paint_thumbnail_grid(thumbnail_store, length, spacing, pixel_ids,
fontsize = old_div(length, 10) + 5
font = get_font(fontsize)
if left_label:
text_width, text_height = font.getbbox(left_label)[2:]
box = font.getbbox(leftLabel)
text_width = box[2] - box[0]
text_height = box[3] - box[1]
left_space = spacing + text_height + spacing
if top_label:
text_width, text_height = font.getbbox(top_label)[2:]
box = font.getbbox(top_label)
text_width = box[2] - box[0]
text_height = box[3] - box[1]
top_space = spacing + text_height + spacing
min_width = left_space + text_width + spacing

Expand All @@ -146,7 +150,8 @@ def paint_thumbnail_grid(thumbnail_store, length, spacing, pixel_ids,
label_size = (label_canvas_width, label_canvas_height)
text_canvas = Image.new(mode, label_size, bg)
draw = ImageDraw.Draw(text_canvas)
text_width = font.getbbox(left_label)[2]
box = font.getbbox(leftLabel)
text_width = box[2] - box[0]
text_x = old_div((label_canvas_width - text_width), 2)
draw.text((text_x, spacing), left_label, font=font, fill=text_color)
vertical_canvas = text_canvas.rotate(90)
Expand Down

0 comments on commit 8e4154f

Please sign in to comment.