<commit_before>#!/usr/bin/python3

import getopt
import sys
import random
import os
import fontconfig
import textwrap
from PIL import Image, ImageFont, ImageDraw, ImageColor, ImageStat
from PIL.PngImagePlugin import PngInfo
from generator import Generator
from hyphen import Hyphenator

#Exit statuses
#These are copied from my /usr/include/sysexits.h. Only statuses possibly relevant to this program were copied.
EX_OK = 0 #No problems
EX_USAGE = 64 #Command line error
EX_DATAERR = 65 #Data format error
EX_NOINPUT = 66 #Input not openable
EX_CANTCREAT = 73 #Can't create output file
EX_NOPERM = 77 #Permission error

#Set defaults
silence = silenceDefault = False
inDir = inDirDefault = "./"
outTextFileName = outTextFileNameDefault = "default out.txt"
outImageFileName = outImageFileNameDefault = "default out.png"
numberOfComics = numberOfComicsDefault = 1
saveForWeb = saveForWebDefault = False
commentMark = commentMarkDefault = "//" #If in the future we decide to use a different mark for comments, this is the only line we'll need to change.
commandLineFont = "" #If a font file is specified on the command line, this will be set.
topImageFileName = None

def findCharsPerLine( text, font, maxWidth ):
	'''Find how many characters will fit within the specified width.
		Args:
			text: The string whose contents are used to test character width.
			font: The font used to test character width.
			maxWidth: The maximum width in pixels.
		Returns:
			An integer indicating how many characters fit within maxWidth.
	'''
	
	if maxWidth < 1:
		maxWidth = 1
	
	charsPerLine = maxWidth // font.getsize( "L" )[0]
	
	if charsPerLine < 1:
		charsPerLine = 1
	
	while font.getsize( text[ :charsPerLine ] )[0] > maxWidth:
		charsPerLine -= 1
	
	if charsPerLine < 1:
		charsPerLine = 1
	
	return charsPerLine

def rewrap( text, font, maxWidth, center=True ):
	'''Rewrap and center text.
		Args:
			text: A string containing the text to be wrapped.
			font: Gets passed to findCharsPerLine().
			maxWidth: Gets passed to findCharsPerLine().
			center: A Boolean indicating whether text should be centered after wrapping. Spaces will be added around each line of text if true. Defaults to True.
		Returns:
			A list of strings.
	'''
	charsPerLine = findCharsPerLine( text, font, maxWidth )
	temp = textwrap.wrap( text, width = charsPerLine, break_long_words = False )
	
	result = []
	for line in temp:
		if len( line ) > charsPerLine:
			hyp = Hyphenator()
			hyphenationResult = []
			
			stillGoing = True
			while stillGoing:
				temp = hyp.wrap( line, width = charsPerLine )
				hyphenationResult.append( temp[ 0 ] )
				if len( temp[ 1 ] ) > charsPerLine:
					line = temp[ 1 ]
					stillGoing = True
				else:
					stillGoing = False
					hyphenationResult.append( temp[ 1 ] )
			
			for newLine in hyphenationResult:
				if center:
					newLine = newLine.strip().center( charsPerLine )<commit_msg>Revert "This is why I marked the previous commit experimental..."<commit_after>#!/usr/bin/python3

import getopt
import sys
import random
import os
import fontconfig
import textwrap
from PIL import Image, ImageFont, ImageDraw, ImageColor, ImageStat
from PIL.PngImagePlugin import PngInfo
from generator import Generator
from hyphen import Hyphenator

#Exit statuses
#These are copied from my /usr/include/sysexits.h. Only statuses possibly relevant to this program were copied.
EX_OK = 0 #No problems
EX_USAGE = 64 #Command line error
EX_DATAERR = 65 #Data format error
EX_NOINPUT = 66 #Input not openable
EX_CANTCREAT = 73 #Can't create output file
EX_NOPERM = 77 #Permission error

#Set defaults
silence = silenceDefault = False
inDir = inDirDefault = "./"
outTextFileName = outTextFileNameDefault = "default out.txt"
outImageFileName = outImageFileNameDefault = "default out.png"
numberOfComics = numberOfComicsDefault = 1
saveForWeb = saveForWebDefault = False
commentMark = commentMarkDefault = "//" #If in the future we decide to use a different mark for comments, this is the only line we'll need to change.
commandLineFont = "" #If a font file is specified on the command line, this will be set.
topImageFileName = None

def findCharsPerLine( text, font, maxWidth ):
	'''Find how many characters will fit within the specified width.
		Args:
			text: The string whose contents are used to test character width.
			font: The font used to test character width.
			maxWidth: The maximum width in pixels.
		Returns:
			An integer indicating how many characters fit within maxWidth.
	'''
	
	if maxWidth < 1:
		maxWidth = 1
	
	charsPerLine = maxWidth // font.getsize( "L" )[0]
	
	if charsPerLine < 1:
		charsPerLine = 1
	
	while font.getsize( text[ :charsPerLine ] )[0] > maxWidth:
		charsPerLine -= 1
	
	if charsPerLine < 1:
		charsPerLine = 1
	
	return charsPerLine

def rewrap( text, font, maxWidth, center=True ):
	'''Rewrap and center text.
		Args:
			text: A string containing the text to be wrapped.
			font: Gets passed to findCharsPerLine().
			maxWidth: Gets passed to findCharsPerLine().
			center: A Boolean indicating whether text should be centered after wrapping. Spaces will be added around each line of text if true. Defaults to True.
		Returns:
			A list of strings.
	'''
	charsPerLine = findCharsPerLine( text, font, maxWidth )
	temp = textwrap.wrap( text, width = charsPerLine, break_long_words = False )
	
	result = []
	for line in temp:
		if len( line ) > charsPerLine:
			hyp = Hypenator()
			hyphenationResult = []
			
			stillGoing = True
			while stillGoing:
				temp = hyp.wrap( line, width = charsPerLine )
				hyphenationResult.append( temp[ 0 ] )
				if len( temp[ 1 ] ) > charsPerLine:
					line = temp[ 1 ]
					stillGoing = True
				else:
					stillGoing = False
					hyphenationResult.append( temp[ 1 ] )
			
			for newLine in hyphenationResult:
				if center:
					newLine = newLine.strip().center( charsPerLine )
{'fraction_non_alphanumeric': 0.0771335727723619, 'fraction_numerical': 0.008893449632290064, 'mean_word_length': 3.8211046990931576, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '541776', 'n_tokens_mistral': 2010, 'n_tokens_neox': 1719, 'n_words': 695}