+ /* Find an image size that's no higher than wide, allowing for some
+ imperfections in the packing. */
+ for(font->image.w=1;; font->image.w<<=1)
+ {
+ if(font->image.w<=margin*2)
+ continue;
+ font->image.h = (area*5/4)/(font->image.w-margin*2)+margin*2;
+ if(font->image.h<=font->image.w)
+ break;
+ }
+ font->image.h = round_to_pot(font->image.h);
+
+ /* Allocate arrays for storing the image and keeping track of used pixels and
+ glyphs. Since glyphs are rectangular and the image is filled starting from
+ the top, it's enough to track the number of used pixels at the top of each
+ column. */
+ font->image.data = (char *)alloc_image_data(font->image.w, font->image.h);
+ if(!font->image.data)
+ return -1;
+ memset(font->image.data, 255, font->image.w*font->image.h);
+ used_pixels = (unsigned *)malloc(font->image.w*sizeof(unsigned));
+ memset(used_pixels, 0, font->image.w*sizeof(unsigned));
+ used_glyphs = (char *)malloc(font->n_glyphs);
+ memset(used_glyphs, 0, font->n_glyphs);
+
+ for(cy=margin; cy+margin<font->image.h;)
+ {
+ unsigned w;
+ unsigned x, y;
+ Glyph *glyph = NULL;
+ unsigned best_score = 0;
+ unsigned target_h = 0;
+
+ /* Find the leftmost free pixel on this row. Also record the lowest
+ extent of glyphs to the left of the free position. */
+ for(; (cx+margin<font->image.w && used_pixels[cx]>cy); ++cx)
+ if(used_pixels[cx]-cy-padding>target_h)
+ target_h = used_pixels[cx]-cy-padding;
+
+ if(cx+margin>=font->image.w)
+ {
+ cx = margin;
+ ++cy;
+ continue;
+ }
+
+ /* Count the free pixel at this position. */
+ for(w=0; (cx+w+margin<font->image.w && used_pixels[cx+w]<=cy); ++w) ;
+
+ /* Find a suitable glyph to put here. */
+ for(i=0; i<font->n_glyphs; ++i)
+ {
+ Glyph *g;
+
+ g = &font->glyphs[i];
+ if(!used_glyphs[i] && g->image.w<=w)
+ {
+ unsigned score;
+
+ /* Prefer glyphs that would reach exactly as low as the ones left
+ of here. This aims to create a straight edge at the bottom for
+ lining up further glyphs. */
+ score = g->image.h+padding;
+ if(g->image.h==target_h)
+ score *= g->image.w;
+ else
+ score += g->image.w;
+
+ if(score>best_score)
+ {
+ glyph = g;
+ best_score = score;
+ }
+ }
+ }
+
+ if(!glyph)
+ {
+ cx += w;
+ continue;
+ }
+
+ used_glyphs[glyph-font->glyphs] = 1;
+ glyph->x = cx;
+ glyph->y = cy;
+
+ for(y=0; y<glyph->image.h; ++y) for(x=0; x<glyph->image.w; ++x)
+ {
+ if(cx+x>=font->image.w || cy+y>=font->image.h)
+ continue;
+ font->image.data[cx+x+(cy+y)*font->image.w] = 255-glyph->image.data[x+y*glyph->image.w];
+ }
+ for(x=0; x<glyph->image.w+2*padding; ++x)
+ {
+ if(cx+x<padding || cx+x>=font->image.w+padding)
+ continue;
+ if(used_pixels[cx+x-padding]<cy+glyph->image.h+padding)
+ used_pixels[cx+x-padding] = cy+glyph->image.h+padding;
+ }
+
+ if(cy+glyph->image.h+margin>used_h)
+ used_h = cy+glyph->image.h+margin;
+ }
+
+ /* Trim the image to the actually used size, in case the original estimate
+ was too pessimistic. */
+ font->image.h = round_to_pot(used_h);
+
+ free(used_glyphs);
+ free(used_pixels);
+
+ return 0;