78523c
diff --git a/src/libpaps.c b/src/libpaps.c
78523c
index b6363a7..1b80257 100644
78523c
--- a/src/libpaps.c
78523c
+++ b/src/libpaps.c
78523c
@@ -55,6 +55,9 @@ typedef struct {
78523c
   double last_pos_x;
78523c
   double scale_x;
78523c
   double scale_y;
78523c
+  double width;
78523c
+  double height;
78523c
+  double cpi;
78523c
 } paps_private_t;
78523c
 
78523c
 
78523c
@@ -88,6 +91,26 @@ paps_t *paps_new()
78523c
 }
78523c
 
78523c
 void
78523c
+paps_set_paper_size(paps_t  *paps_,
78523c
+		    gdouble  width,
78523c
+		    gdouble  height)
78523c
+{
78523c
+	paps_private_t *paps = (paps_private_t *)paps_;
78523c
+
78523c
+	paps->width = width;
78523c
+	paps->height = height;
78523c
+}
78523c
+
78523c
+void
78523c
+paps_set_cpi(paps_t  *paps_,
78523c
+	     gdouble  cpi)
78523c
+{
78523c
+	paps_private_t *paps = (paps_private_t *)paps_;
78523c
+
78523c
+	paps->cpi = cpi;
78523c
+}
78523c
+
78523c
+void
78523c
 paps_set_scale(paps_t  *paps_,
78523c
 	       gdouble  scale_x,
78523c
 	       gdouble  scale_y)
78523c
@@ -401,7 +424,7 @@ static void draw_contour(paps_private_t *paps,
78523c
       FT_Face ft_face = pango_ft2_font_get_face(font);
78523c
       int num_glyphs = glyphs->num_glyphs;
78523c
       int glyph_idx;
78523c
-      
78523c
+
78523c
       for (glyph_idx=0; glyph_idx
78523c
         {
78523c
           PangoGlyphGeometry geometry = glyphs->glyphs[glyph_idx].geometry;
78523c
@@ -410,7 +433,11 @@ static void draw_contour(paps_private_t *paps,
78523c
           glyph_pos_x = x_pos + 1.0*geometry.x_offset * scale;
78523c
           glyph_pos_y = line_start_pos_y - 1.0*geometry.y_offset * scale;
78523c
 
78523c
-	  x_pos += geometry.width * scale * paps->scale_x;
78523c
+	  if (paps->cpi > 0.0L) {
78523c
+		  x_pos += (1 / paps->cpi * 72.0);
78523c
+	  } else {
78523c
+		  x_pos += geometry.width * scale * paps->scale_x;
78523c
+	  }
78523c
 
78523c
           if (glyphs->glyphs[glyph_idx].glyph == PANGO_GLYPH_EMPTY)
78523c
             continue;
78523c
diff --git a/src/libpaps.h b/src/libpaps.h
78523c
index 0b74321..cbb4042 100644
78523c
--- a/src/libpaps.h
78523c
+++ b/src/libpaps.h
78523c
@@ -53,10 +53,16 @@ void paps_free(paps_t *paps);
78523c
  * @param scale_y y-coordinate scale
78523c
  *
78523c
  */
78523c
-void
78523c
-paps_set_scale(paps_t  *paps,
78523c
-	       gdouble  scale_x,
78523c
-	       gdouble  scale_y);
78523c
+void paps_set_scale(paps_t  *paps,
78523c
+		    gdouble  scale_x,
78523c
+		    gdouble  scale_y);
78523c
+
78523c
+void paps_set_paper_size(paps_t  *paps_,
78523c
+			 gdouble  width,
78523c
+			 gdouble  height);
78523c
+
78523c
+void paps_set_cpi(paps_t  *paps_,
78523c
+		  gdouble  cpi);
78523c
 
78523c
 /** 
78523c
  * libpaps may currently be used only with a PangoContext that it
78523c
diff --git a/src/paps.c b/src/paps.c
78523c
index 334d547..72dbaad 100644
78523c
--- a/src/paps.c
78523c
+++ b/src/paps.c
78523c
@@ -33,6 +33,8 @@
78523c
 #include <locale.h>
78523c
 #include <cups/cups.h>
78523c
 #include <cups/ppd.h>
78523c
+#include <math.h>
78523c
+#include <wchar.h>
78523c
 
78523c
 #define BUFSIZE 1024
78523c
 #define DEFAULT_FONT_FAMILY	"Monospace"
78523c
@@ -594,6 +596,8 @@ int main(int argc, char *argv[])
78523c
   page_layout.owner = page_owner;
78523c
   page_layout.cups_mode = cups_mode;
78523c
 
78523c
+  paps_set_paper_size(paps, page_width, page_height);
78523c
+
78523c
   /* calculate x-coordinate scale */
78523c
   if (page_layout.cpi > 0.0L)
78523c
     {
78523c
@@ -606,7 +610,7 @@ int main(int argc, char *argv[])
78523c
       w = pango_font_metrics_get_approximate_digit_width (metrics);
78523c
       if (w > max_width)
78523c
 	  max_width = w;
78523c
-      page_layout.scale_x = 1 / page_layout.cpi * 72.0 * PANGO_SCALE / max_width;
78523c
+      page_layout.scale_x = 1 / page_layout.cpi * 72.0 * (gdouble)PANGO_SCALE / (gdouble)max_width;
78523c
       pango_font_metrics_unref (metrics);
78523c
       g_object_unref (G_OBJECT (fontmap));
78523c
 
78523c
@@ -614,6 +618,8 @@ int main(int argc, char *argv[])
78523c
       // update the font size to that width
78523c
       pango_font_description_set_size (font_description, font_size * page_layout.scale_x);
78523c
       pango_context_set_font_description (pango_context, font_description);
78523c
+
78523c
+      paps_set_cpi(paps, page_layout.cpi);
78523c
     }
78523c
 
78523c
   page_layout.scale_x = page_layout.scale_y = 1.0;
78523c
@@ -727,119 +733,6 @@ read_file (FILE   *file,
78523c
   return text;
78523c
 }
78523c
 
78523c
-#if 0
78523c
-/* Take a UTF8 string and break it into paragraphs on \n characters.
78523c
- *
78523c
- * Sorry. I couldn't figure out what this version was supposed to do
78523c
- *
78523c
- */
78523c
-static GList *
78523c
-split_text_into_paragraphs (PangoContext *pango_context,
78523c
-                            page_layout_t *page_layout,
78523c
-                            int paint_width,  /* In pixels */
78523c
-                            char *text)
78523c
-{
78523c
-  char *p = text;
78523c
-  char *next;
78523c
-  gunichar wc;
78523c
-  GList *result = NULL;
78523c
-  char *last_para = text;
78523c
-  
78523c
-  while (p != NULL && *p)
78523c
-    {
78523c
-      wc = g_utf8_get_char (p);
78523c
-      next = g_utf8_next_char (p);
78523c
-      if (wc == (gunichar)-1)
78523c
-        {
78523c
-          fprintf (stderr, "%s: Invalid character in input\n", g_get_prgname ());
78523c
-          wc = 0;
78523c
-        }
78523c
-      if (!*p || !wc || wc == '\n' || wc == '\f')
78523c
-        {
78523c
-          Paragraph *para = g_new (Paragraph, 1);
78523c
-          para->text = last_para;
78523c
-          para->length = p - last_para;
78523c
-          para->layout = pango_layout_new (pango_context);
78523c
-
78523c
-	  if (cpi > 0.0L && page_layout->do_wordwrap)
78523c
-	    {
78523c
-	      PangoRectangle ink_rect, logical_rect;
78523c
-	      wchar_t *wtext, *wnewtext;
78523c
-	      gchar *newtext;
78523c
-	      size_t i, len, wwidth = 0, n;
78523c
-
78523c
-	      wtext = g_utf8_to_ucs4 (para->text, para->length, NULL, NULL, NULL);
78523c
-	      if (wtext == NULL)
78523c
-	        {
78523c
-		  fprintf (stderr, "Failed to convert UTF-8 to UCS-4.\n");
78523c
-                  exit(1);
78523c
-		}
78523c
-
78523c
-	      len = g_utf8_strlen (para->text, para->length);
78523c
-	      /* the amount of characters to be able to put on the line against CPI */
78523c
-	      n = page_layout->column_width / 72.0 * cpi;
78523c
-	      if (len > n)
78523c
-	        {
78523c
-		  wnewtext = g_new (wchar_t, wcslen (wtext) + 1);
78523c
-		  if (wnewtext == NULL)
78523c
-		    {
78523c
-		      fprintf (stderr, "Failed to allocate a memory.\n");
78523c
-		      g_free (wtext);
78523c
-                      exit(1);
78523c
-		    }
78523c
-		  for (i = 0; i < len; i++)
78523c
-		    {
78523c
-		      wwidth += wcwidth (wtext[i]);
78523c
-		      if (wwidth > n)
78523c
-			  break;
78523c
-		      wnewtext[i] = wtext[i];
78523c
-		    }
78523c
-		  wnewtext[i] = 0L;
78523c
-
78523c
-		  newtext = g_ucs4_to_utf8 ((const gunichar *)wnewtext, i, NULL, NULL, NULL);
78523c
-		  if (newtext == NULL)
78523c
-		    {
78523c
-		      fprintf (stderr, "Failed to convert UCS-4 to UTF-8.\n");
78523c
-                      exit(1);
78523c
-		    }
78523c
-
78523c
-		  pango_layout_set_text (para->layout, newtext, -1);
78523c
-		  pango_layout_get_extents (para->layout, &ink_rect, &logical_rect);
78523c
-		  /* update paint_width to wrap_against CPI */
78523c
-		  paint_width = logical_rect.width / PANGO_SCALE;
78523c
-		  g_free (newtext);
78523c
-		  g_free (wnewtext);
78523c
-		}
78523c
-	      g_free (wtext);
78523c
-	    }
78523c
-          pango_layout_set_text (para->layout, para->text, para->length);
78523c
-          pango_layout_set_justify (para->layout, page_layout->do_justify);
78523c
-          pango_layout_set_alignment (para->layout,
78523c
-                                      page_layout->pango_dir == PANGO_DIRECTION_LTR
78523c
-                                      ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT);
78523c
-          pango_layout_set_width (para->layout, paint_width * PANGO_SCALE);
78523c
-          if (page_layout->do_wordwrap)
78523c
-              pango_layout_set_wrap (para->layout, PANGO_WRAP_WORD_CHAR);
78523c
-          para->height = 0;
78523c
-
78523c
-          if (wc == '\f')
78523c
-              para->formfeed = 1;
78523c
-          else
78523c
-              para->formfeed = 0;
78523c
-
78523c
-          last_para = next;
78523c
-            
78523c
-          result = g_list_prepend (result, para);
78523c
-        }
78523c
-      if (!wc) /* incomplete character at end */
78523c
-        break;
78523c
-      p = next;
78523c
-    }
78523c
-
78523c
-  return g_list_reverse (result);
78523c
-}
78523c
-#endif
78523c
-
78523c
 /* Take a UTF8 string and break it into paragraphs on \n characters
78523c
  */
78523c
 static GList *
78523c
@@ -905,18 +798,85 @@ split_text_into_paragraphs (PangoContext *pango_context,
78523c
               para->text = last_para;
78523c
               para->length = p - last_para;
78523c
               para->layout = pango_layout_new (pango_context);
78523c
-              //          pango_layout_set_font_description (para->layout, font_description);
78523c
-              pango_layout_set_text (para->layout, para->text, para->length);
78523c
+
78523c
+	      if (page_layout->cpi > 0.0L && page_layout->do_wordwrap) {
78523c
+		      /* figuring out the correct width from the pango_font_metrics_get_approximate_width()
78523c
+		       * is really hard and pango_layout_set_wrap() doesn't work properly then.
78523c
+		       * Those are not reliable to render the characters exactly according to the given CPI.
78523c
+		       * So Re-calculate the width to wrap up to be comfortable with CPI.
78523c
+		       */
78523c
+		      wchar_t *wtext = NULL, *wnewtext = NULL;
78523c
+		      gchar *newtext = NULL;
78523c
+		      gsize len, col, i, wwidth = 0;
78523c
+		      PangoRectangle ink_rect, logical_rect;
78523c
+
78523c
+		      wtext = (wchar_t *)g_utf8_to_ucs4(para->text, para->length, NULL, NULL, NULL);
78523c
+		      if (wtext == NULL) {
78523c
+			      fprintf(stderr, "%s: Unable to convert UTF-8 to UCS-4.\n", g_get_prgname());
78523c
+			fail:
78523c
+			      g_free(wtext);
78523c
+			      g_free(wnewtext);
78523c
+			      g_free(newtext);
78523c
+			      if (page_layout->cups_mode) {
78523c
+				      /* try to continue parsing text */
78523c
+				      p = next;
78523c
+				      continue;
78523c
+			      } else {
78523c
+				      exit(1);
78523c
+			      }
78523c
+		      }
78523c
+		      len = g_utf8_strlen(para->text, para->length);
78523c
+		      /* the amount of characters that can be put on the line against CPI */
78523c
+		      col = page_layout->column_width / 72.0 * page_layout->cpi;
78523c
+		      if (len > col) {
78523c
+			      /* need to wrap up them */
78523c
+			      wnewtext = g_new(wchar_t, wcslen(wtext) + 1);
78523c
+			      if (wnewtext == NULL) {
78523c
+				      fprintf(stderr, "%s: Unable to allocate the memory.\n", g_get_prgname());
78523c
+				      goto fail;
78523c
+			      }
78523c
+			      for (i = 0; i < len; i++) {
78523c
+				      wwidth += wcwidth(wtext[i]);
78523c
+				      if (wwidth > col)
78523c
+					      break;
78523c
+				      wnewtext[i] = wtext[i];
78523c
+			      }
78523c
+			      wnewtext[i] = 0L;
78523c
+
78523c
+			      newtext = g_ucs4_to_utf8((const gunichar *)wnewtext, i, NULL, NULL, NULL);
78523c
+			      if (newtext == NULL) {
78523c
+				      fprintf(stderr, "%s: Unable to convert UCS-4 to UTF-8.\n", g_get_prgname());
78523c
+				      goto fail;
78523c
+			      }
78523c
+			      pango_layout_set_text(para->layout, newtext, -1);
78523c
+			      pango_layout_get_extents(para->layout, &ink_rect, &logical_rect);
78523c
+			      paint_width = logical_rect.width / PANGO_SCALE;
78523c
+			      g_free(wnewtext);
78523c
+			      g_free(newtext);
78523c
+
78523c
+			      para->length = i;
78523c
+			      next = g_utf8_offset_to_pointer(para->text, para->length);
78523c
+			      wc = g_utf8_prev_char(next);
78523c
+		      } else {
78523c
+			      pango_layout_set_text(para->layout, para->text, para->length);
78523c
+		      }
78523c
+		      g_free(wtext);
78523c
+		      
78523c
+		      pango_layout_set_width(para->layout, -1);
78523c
+	      } else {
78523c
+		      pango_layout_set_text (para->layout, para->text, para->length);
78523c
+		      if (page_layout->do_wordwrap) {
78523c
+			      pango_layout_set_wrap (para->layout, PANGO_WRAP_WORD_CHAR);
78523c
+			      pango_layout_set_width (para->layout, paint_width * PANGO_SCALE);
78523c
+		      } else {
78523c
+			      pango_layout_set_width (para->layout, -1);
78523c
+		      }
78523c
+	      }
78523c
+					      
78523c
               pango_layout_set_justify (para->layout, page_layout->do_justify);
78523c
               pango_layout_set_alignment (para->layout,
78523c
                                           page_layout->pango_dir == PANGO_DIRECTION_LTR
78523c
                                           ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT);
78523c
-              if (page_layout->do_wordwrap) {
78523c
-                pango_layout_set_wrap (para->layout, PANGO_WRAP_WORD_CHAR);
78523c
-                pango_layout_set_width (para->layout, paint_width * PANGO_SCALE);
78523c
-              } else {
78523c
-                pango_layout_set_width (para->layout, -1);
78523c
-              }
78523c
 
78523c
               para->height = 0;
78523c