Algorithm for Text Wrapping Within a Shape

前端 未结 3 1390
被撕碎了的回忆
被撕碎了的回忆 2020-12-14 08:12

I am looking for an algorithm to wrap text within a non-rectangular shape, preferably based on the Knuth and Plass algorithm. The hardest part of this is that the lines may

3条回答
  •  旧巷少年郎
    2020-12-14 08:32

    Let's say each letter has a specific size(width and height, in this case we probably only care about width because all letters have the same height). Then we need the following:

    1. Wrapped objects of each word - to make sure there's no fragmentation of the word
    2. given width of each word.
    3. Partition the polygon into chunks that are the heights of the word. So you have x horizontal strips that make up the image.
    4. Then, find the width of the inside polygon if you were to inscribe words inside. This means you're removing the round edges of the heart. Where a vertical line can intersect the edge of the heart on the strip is where you remove the round edge.

    Now that we have the size of each strip of the image, let's say we have this (i'm using arbitary unit for width):

    [_________] [__________]  <-- 10 width (5 each)
      [__________________] <-- 9 width
        [_____________] <-- 7 width   
           [_______] <-- 5 width
             [___] <-- 3 width
              [_] <-- 2 width
    

    EDIT: Just realized how ugly the heart is, my bad.

    Now we need the size of each word, and insert them sequentially. Each block has x width, and each word object has y width. If y width > x width, we move to the next line and check there

    considering we already have widths
    while(image.hasNextChunk()){
        currentChunk = image.nextChunk();
        if(currentWord.width < currentChunk.width) //insert here and then change currentWord to nextWord
        ...
    }
    

    I think this is what you want, but I'm not completely sure. Let me know if this helped! :)

提交回复
热议问题