Blame SOURCES/paps-fix-cpi.patch

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