ILRDF-Lowking commited on
Commit
2aa2819
·
verified ·
1 Parent(s): 45d3b11

Update visual_renderer.py

Browse files
Files changed (1) hide show
  1. visual_renderer.py +44 -24
visual_renderer.py CHANGED
@@ -5,17 +5,8 @@ import os
5
  def draw_glossing_card(data, output_path):
6
  """
7
  將四行分析畫在畫布上
8
- data 包含: line1, line2, line3, translation
9
  """
10
- # 畫布設定
11
- width, height = 1200, 450 # 💡 高度微調,讓版面更緊湊
12
- bg_color = (253, 248, 241) # 米白色紙張感
13
-
14
- # 💡 修正錯誤:使用 Image.new 而不是 Image.Image.new
15
- img = Image.new('RGB', (width, height), color=bg_color)
16
- draw = ImageDraw.Draw(img)
17
-
18
- # 💡 字體設定 (強烈建議要有中文字型檔,否則中文會變方塊)
19
  font_path = "NotoSansTC-Regular.ttf"
20
  font_size = 32
21
  try:
@@ -24,29 +15,58 @@ def draw_glossing_card(data, output_path):
24
  print(f"⚠️ 找不到中文字型或載入失敗: {e}")
25
  font = ImageFont.load_default()
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  x_offset = 80
28
- y_start = 80
29
- line_spacing = 60 # 四行等距
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- # 畫出前三行 (對齊單詞)
32
  for i in range(len(data['glossing']['line1'])):
33
  w1 = data['glossing']['line1'][i]
34
  w2 = data['glossing']['line2'][i]
35
  w3 = data['glossing']['line3'][i]
36
 
37
- draw.text((x_offset, y_start), w1, fill=(0,0,0), font=font)
38
- draw.text((x_offset, y_start + line_spacing), w2, fill=(139,0,0), font=font) # 暗紅色
39
- draw.text((x_offset, y_start + line_spacing * 2), w3, fill=(85,85,85), font=font) # 深灰色
40
-
41
- # 計算下一個單詞的間距 (取三行中最長者 + 緩衝)
42
- bbox1 = draw.textbbox((0,0), w1, font=font)
43
- bbox2 = draw.textbbox((0,0), w2, font=font)
44
- bbox3 = draw.textbbox((0,0), w3, font=font)
45
 
46
- max_w = max(bbox1[2] - bbox1[0], bbox2[2] - bbox2[0], bbox3[2] - bbox3[0])
47
- x_offset += max_w + 40 # 單詞之間的間距
 
 
 
48
 
49
- # 畫出第四行 (整句意譯) - 無橫線,等距 (拉開半行的距離區隔)
50
  draw.text((80, y_start + line_spacing * 3.5), data['translation'], fill=(0,0,0), font=font)
51
 
52
  img.save(output_path)
 
5
  def draw_glossing_card(data, output_path):
6
  """
7
  將四行分析畫在畫布上
8
+ 包含動態寬度計算,防止長句被截斷
9
  """
 
 
 
 
 
 
 
 
 
10
  font_path = "NotoSansTC-Regular.ttf"
11
  font_size = 32
12
  try:
 
15
  print(f"⚠️ 找不到中文字型或載入失敗: {e}")
16
  font = ImageFont.load_default()
17
 
18
+ # 建立一個臨時畫布來精準測量文字寬度 (相容不同版本的 Pillow)
19
+ temp_img = Image.new('RGB', (1, 1))
20
+ temp_draw = ImageDraw.Draw(temp_img)
21
+
22
+ def get_text_width(text, font):
23
+ if hasattr(temp_draw, 'textlength'):
24
+ return temp_draw.textlength(text, font=font)
25
+ elif hasattr(temp_draw, 'textbbox'):
26
+ return temp_draw.textbbox((0,0), text, font=font)[2]
27
+ else:
28
+ return font.getsize(text)[0]
29
+
30
+ # 💡 1. 動態計算畫布所需的總寬度
31
  x_offset = 80
32
+ for i in range(len(data['glossing']['line1'])):
33
+ w1_len = get_text_width(data['glossing']['line1'][i], font)
34
+ w2_len = get_text_width(data['glossing']['line2'][i], font)
35
+ w3_len = get_text_width(data['glossing']['line3'][i], font)
36
+ x_offset += max(w1_len, w2_len, w3_len) + 50 # 每個單詞群組保留 50px 間距
37
+
38
+ # 計算第四行(翻譯)的寬度
39
+ trans_w = get_text_width(data['translation'], font)
40
+
41
+ # 決定最終畫布寬度 (至少 1200,若更長則取實際寬度加上右側邊距 80)
42
+ canvas_width = int(max(1200, x_offset + 80, trans_w + 160))
43
+ canvas_height = 500 # 固定高度
44
+
45
+ bg_color = (253, 248, 241) # 米白色紙張感
46
+ img = Image.new('RGB', (canvas_width, canvas_height), color=bg_color)
47
+ draw = ImageDraw.Draw(img)
48
+
49
+ # 💡 2. 開始繪製
50
+ curr_x = 80
51
+ y_start = 100
52
+ line_spacing = 80 # 四行等距
53
 
 
54
  for i in range(len(data['glossing']['line1'])):
55
  w1 = data['glossing']['line1'][i]
56
  w2 = data['glossing']['line2'][i]
57
  w3 = data['glossing']['line3'][i]
58
 
59
+ draw.text((curr_x, y_start), w1, fill=(0,0,0), font=font)
60
+ draw.text((curr_x, y_start + line_spacing), w2, fill=(139,0,0), font=font) # 暗紅色
61
+ draw.text((curr_x, y_start + line_spacing * 2), w3, fill=(85,85,85), font=font) # 深灰色
 
 
 
 
 
62
 
63
+ # 推進 X 座標到下一個單詞的起點
64
+ bbox_w1 = get_text_width(w1, font)
65
+ bbox_w2 = get_text_width(w2, font)
66
+ bbox_w3 = get_text_width(w3, font)
67
+ curr_x += max(bbox_w1, bbox_w2, bbox_w3) + 50
68
 
69
+ # 畫出第四行 (整句意譯) - 無橫線,等距
70
  draw.text((80, y_start + line_spacing * 3.5), data['translation'], fill=(0,0,0), font=font)
71
 
72
  img.save(output_path)