Spaces:
Running
Running
Update app.py
Browse filesNow? we're cooking with gas? Forgot the copy button
111 unmodified lines1121131141151161151161171181191201211181191201231211221231246 unmodified lines1311321331361341351361371411421381391401411421461471431441451461521531471481561491501591511521537 unmodified lines16116216317316416516616717716816917018017118217217317411 unmodified lines1861871882001891901911921931942071951961972 unmodified lines2002012022162032042052202062072222082092102112262122132148 unmodified lines223224225241226227228229111 unmodified lines
def get_minimum_column_width_in(word_dict, is_reference_block=False, ref_text="", ms_text=""): """ Measures character counts to calculate the physical minimum width in inches needed to hold a column's text without any word-wrapping. Measures character lengths to establish the minimum width in inches needed to hold content cleanly on a single line without wrapping. """ # System assumptions: 1 character in Arial 14.6px is approx 0.075 inches wide. # Hebrew characters at 24px are much wider, approx 0.145 inches per character. char_width_in = 0.075 hebrew_char_width_in = 0.145 char_width_in = 0.072 hebrew_char_width_in = 0.140 # Base cell padding safety allowance: 0.069 inches on both sides = 0.138 inches total # Precise padding safety buffer: 0.069 inches on both sides = 0.138 inches padding_buffer_in = 0.069 * 2 if is_reference_block:6 unmodified lines w_morph = len(word_dict.get("morphology", "")) * char_width_in max_content_width = max(w_hebrew, w_translit, w_trans, w_morph) # Give it a baseline minimum size if it's mostly blank return max(max_content_width, 0.65) + padding_buffer_in
def calculate_recursive_table_chunks(words_data, reference, manuscript): """ ESSENTIAL PROCEDURE: Groups words dynamically. If a 7-slot row's minimum un-wrapped widths exceed 7.0 inches total, the last word drops down to the next row group recursively. ESSENTIAL PROCEDURE: Groups words. If a 7-column layout requires more than 7.0 inches to prevent wrapping, the last word drops down recursively. """ total_allowed_width_in = 7.0 final_row_chunks = [] # Work with a shallow copy of the word array list so we can shift items dynamically word_pool = list(words_data) while len(word_pool) > 0: is_first_row = (len(final_row_chunks) == 0) # First row has 1 slot reserved for reference, leaving up to 6 word positions. # Other rows take up to 7 word positions. max_word_limit = 6 if is_first_row else 7 # Pull out a trial row slice current_row_words = word_pool[:max_word_limit] # Recursive check loop to pull words out until the row fits into 7 inches while len(current_row_words) > 1: minimum_widths = [] 7 unmodified lines total_required_row_width = sum(minimum_widths) # Scenario A: The columns fit safely within 7 inches without wrapping text! # If the combined row width fits comfortably inside 7 inches, we break and lock it if total_required_row_width <= total_allowed_width_in: break # Scenario B: Over 7 inches! Move the last word down to the next table row. # If it exceeds 7 inches, drop the last word to the next row pool current_row_words.pop() # Commit the finalized word slice to this table block final_row_chunks.append(current_row_words) # Delete the processed slice off the front of our remaining word pool word_pool = word_pool[len(current_row_words):] return final_row_chunks11 unmodified lines html_output = ['<div id="table-payload" class="white-canvas" style="background-color: #ffffff; padding: 20px; border-radius: 8px; display: inline-block; width: 100%; box-sizing: border-box; text-align: center;">'] # RUN RECURSIVE LAYOUT SPLITTING PROCEDURE table_chunks = calculate_recursive_table_chunks(words_data, reference, manuscript) for idx, current_chunk in enumerate(table_chunks): is_first = (idx == 0) is_last_table = (idx == len(table_chunks) - 1) # Step 1: Establish baseline minimum requirements to verify absolute protection boundaries slot_minimums = [] if is_first: ref_min = get_minimum_column_width_in({}, is_reference_block=True, ref_text=reference, ms_text=manuscript)2 unmodified lines for word in current_chunk: slot_minimums.append(get_minimum_column_width_in(word)) # Symmetrical filler pads trailing tables up to exactly 7 columns wide if is_last_table: while (len(current_chunk) + (1 if is_first else 0)) < total_slots: current_chunk.append({"hebrew": " ", "transliteration": " ", "translation": " ", "morphology": " "}) slot_minimums.append(0.75) # Standard padding size allocation score for blank boxes slot_minimums.append(0.75)
# Step 2 & 3: Run Proportional Balancing to scale the table footprint to exactly 7.0 inches wide # Proportional balancing math ensures all columns total exactly 7.0 inches overall total_minimum_score = sum(slot_minimums) calculated_widths = [] for min_w in slot_minimums: # Scale proportionally so the combined columns perfectly lock at 7 inches allocated_width = (min_w / total_minimum_score) * total_table_width_in calculated_widths.append(round(allocated_width, 3)) 8 unmodified lines html_output.append(f' <col style="width: {c_width}in !important; min-width: {c_width}in !important; max-width: {c_width}in !important;">') html_output.append(' </colgroup>') # LOCKED REQUIREMENT: Exact 0.069in padding on all sides, text wrap disabled via nowrap, box size clamped # Symmetrical cell padding exactly 0.069in on all 4 sides with wrap suppressed safely base_cell = "border: 1pt solid #b7b7b7; padding: 0.069in !important; height: auto; overflow: hidden; white-space: nowrap !important; box-sizing: border-box !important;" hebrew_cell_style = f"font-family: 'Times New Roman', serif !important; font-size: 24px !important; color: #000000 !important; text-align: right; vertical-align: middle; {base_cell}" english_cell_style = f"font-family: Arial, sans-serif !important; font-size: 14.6667px !important; color: #000000 !important; text-align: center; vertical-align: top; line-height: 1.0 !important; {base_cell}"app.py+12-27