hello! welcome to episode 2 of "making a text editor, even though we made one before". today's challenge is to justify a text. submissions may be written in any language.
justification is the process of stretching or compressing the spaces between words and between glyphs or letters in order to align both the left and right ends of consecutive lines of text. you may be familiar with it if you've used office programs like LibreOffice, ONLYOFFICE or OpenOffice before.
this challenge implicitly requires you to wrap the text as well, both because IFcoltransG suggested it can be a continuation of the previous text editor task and because you can only justify one line of text for so long. but beware: this is not word wrapping; breaking words up is completely legal and sometimes even required. you can replace space characters with newlines, hyphenate a word or invent creative ways of wrapping your own.
the input may contain multiple paragraphs, which should be wrapped and justified independently. you are free to decide what constitutes a paragraph.
also, the message I got from IFcoltransG mentioned the number 72 several times, so I guess my brand-new code guessing editor v2 is 72 columns wide.
your challenge, given an ASCII string with space characters in it, is to justify (and wrap) the paragraphs in it using the rules above. as any language is allowed, there is no fixed API.
Call me Ishmael. Some years ago- never mind how long precisely- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people's hats off- then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me.
localfunctionmkblank(width,height)localt={}fory=1,heightdolocaltt={};t[y]=ttforx=1,widthdott[x]=falseendendreturntendlocalfunctionoutput(t,width,height)io.write("P1\n"..width.." "..height.."\n")for_,ttinipairs(t)dolocalnr={}forj,vinipairs(tt)donr[j]=(vand"1"or"0")endnr[#nr+1]="\n"io.write(table.concat(nr))endendlocalfunctionlinedraw_naiive(t,px,py,x,y)whilepx~=xorpy~=ydoifpx<xthenpx=px+1endifpx>xthenpx=px-1endifpy<ythenpy=py+1endifpy>ythenpy=py-1endt[py][px]=trueendend-- https://en.wikipedia.org/wiki/Bresenham's_line_algorithm#All_cases the last onelocalfunctionlinedraw_bresenham_combined(t,px,py,x,y)localdx=math.abs(x-px)localsx=(px<x)and1or-1localdy=-math.abs(y-py)localsy=(py<y)and1or-1localerror=dx+dywhiletruedot[py][px]=truelocale2=2*errorife2>=dythenifpx==xthenbreakenderror=error+dypx=px+sxendife2<=dxthenifpy==ythenbreakenderror=error+dxpy=py+syendendend-- local linedraw = linedraw_naiivelocallinedraw=linedraw_bresenham_combinedlocalbyte_R=string.byte("R")localfunctionloadhershey(source)localglyphs={}locali=1whilei<#sourcedowhilesource:sub(i,i)=="\n"doi=i+1endlocalid=assert(tonumber(source:sub(i,i+4)))i=i+5localpointn=assert(tonumber(source:sub(i,i+2)))i=i+3pointin,i=pointn-1,i+2-- not sure what's up with this they're screwedlocalglyph={}whileglyphs[id]doid=id+1end-- maximum siglyphs[id]=glyphlocalpart={}glyph[1]=partforj=1,pointndolocalc1=source:sub(i,i)ifc1=="\n"theni=i+1elseifc1==" "thenpart={}glyph[#glyph+1]=parti=i+2elsepart[#part+1]={x=(c1:byte()-byte_R),y=(source:sub(i+1,i+1):byte()-byte_R),}i=i+2endendendreturnglyphsendlocalspace=19locallinespace=25localwidth,height=space*72,600localgrid=mkblank(width,height)--[=[local glyphs = loadhershey([[ 1 9MWRMNV RRMVV RPSTS 2 16MWOMOV ROMSMUNUPSQ ROQSQURUUSVOV 3 11MXVNTMRMPNOPOSPURVTVVU 4 12MWOMOV ROMRMTNUPUSTURVOV 5 12MWOMOV ROMUM ROQSQ ROVUV 6 9MVOMOV ROMUM ROQSQ 7 15MXVNTMRMPNOPOSPURVTVVUVR RSRVR 8 9MWOMOV RUMUV ROQUQ 9 3PTRMRV 10 7NUSMSTRVPVOTOS 11 9MWOMOV RUMOS RQQUV 12 6MVOMOV ROVUV]])--]=]localglyphsdo-- https://solhsa.com/hershey/fontprev.html-- please reboop files to remove the erroneous newlines in them thankslocalfile=assert(io.open("futural.jhf","r"))glyphs=loadhershey(file:read("*a"))file:close()end-- linedraw(grid, 1,1, width,height/2)localfunctionglyphdraw(grid,id,ox,oy,scale)scale=scaleor1for_,partinipairs(glyphs[id])doifpart[1]thenlocalxx,yy=ox+part[1].x*scale,oy+part[1].y*scalefori=2,#partdolocalx,y=ox+part[i].x*scale,oy+part[i].y*scalelinedraw(grid,xx,yy,x,y)xx,yy=x,yendendendendlocalmessage={}localmessage_s="it's a meow MEOW =^^= world !! i hope you have a nice day. a wheeeeeeee meow why does that meow dissappear? I do not know. oh, i have to make it 72 cols wide i missed that so i am typing lots more text here entropyfilling aaaa don't guess me on this please sorry"localspace_value_from_outer_space=(" "):byte()-32+12345forcinmessage_s:gmatch(".")domessage[#message+1]=c:byte()-32+12345endmessage[#message+1]=space_value_from_outer_spacelocalox,oy=space,linespacelocalwormrowm={}localwormwormlen=0localfunctiondrawtotalwormrowm()locallll=0for_,sworminipairs(wormrowm)dolll=lll+space*#swormendlocalscabamgap=math.floor((width-lll-space)/(#wormrowm-1))io.stderr:write(tostring(scabamgap).."\n")-- it's sligtly fuckedfor_,sworminipairs(wormrowm)dofor_,id2inipairs(sworm)doglyphdraw(grid,id2,ox,oy)ox=ox+spaceendox=ox+scabamgapendwormrowm={}wormwormlen=spaceox,oy=space,oy+linespaceendlocalworm={}for_,idinipairs(message)doifid~=space_value_from_outer_spacethenworm[#worm+1]=idelse--nooonononono theree minutes to submisnitnoi--cant think-- -- it's brokeorenn!!!localwl=(#worm+1)*spaceifwormwormlen+wl>widththendrawtotalwormrowm()elsewormrowm[#wormrowm+1]=wormwormwormlen=wormwormlen+wlendworm={}endenddrawtotalwormrowm()--- iaaa it was the usuall silly last one not triggered issue this is really common in my programmingoutput(grid,width,height)
;; This buffer is for text that is not saved, and for Lisp evaluation.;; To create a file, visit it with ‘C-x C-f’ and enter text in its buffer.(defuncg106(text)(with-temp-buffer(inserttext)(setqfill-column72)(fill-region(point-min)(point-max)'full)(buffer-string)))
post a comment