PangoStretch questions.

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

PangoStretch questions.

Robert Heller
I am trying to use the PangoStretch attribute to create an effect that was
typically provided by old dot-matrix impact (and early inkjet) printers.

I want to be able to stretch (to double width) and to compress (to 60%
width) text.  Bascially, asymetrical scaling (horizontal only).  I am trying
to do this with the PangoStretch attribute, but it does not seem to work.  
Either I am doing something wrong or there is a bug.

I am using pango-1.28.1-11.el6 and cairo-1.8.8-6.el6_6, specificly cairo-pdf
and pangocairo.  These are the stock versions on my CentOS 6 system.

I wrote a small test program (in C++) that illustrates the problem. Its first
command line argument is the name of an output PDF file and its second
argument is a file containing a list of fonts, one per line (created with this
command: fc-list | awk -F: '{print $1;}' | awk -F, '{print $1;}' | sort -u >
fonts.list).

Here is the program:

// -!- C++ -!- //////////////////////////////////////////////////////////////
//
//  System        :
//  Module        :
//  Object Name   : $RCSfile$
//  Revision      : $Revision$
//  Date          : $Date$
//  Author        : $Author$
//  Created By    : Robert Heller
//  Created       : Sun Aug 13 00:17:43 2017
//  Last Modified : <170813.1040>
//
//  Description
//
//  Notes
//
//  History
//
/////////////////////////////////////////////////////////////////////////////
//
//    Copyright (C) 2017  Robert Heller D/B/A Deepwoods Software
// 51 Locke Hill Road
// Wendell, MA 01379-9728
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//
//
//////////////////////////////////////////////////////////////////////////////

static const char rcsid[] = "@(#) : $Id$";

#include "config.h"
#include <cairo-pdf.h>
#include <pango/pangocairo.h>

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

class Printer {
private:
    int curline, maxlines;
    cairo_surface_t *pdf_surface;
    cairo_t *pdf_context;
    PangoLayout *layout;
    PangoFontDescription *fontdescr;
    int swidth, sheight;
public:
    Printer(const std::string filename);
    ~Printer();
    void SelectFont(const std::string name, PangoStretch stretch);
    void PrintLine(const std::string text);
    void NewPage() {cairo_show_page (pdf_context);curline = 0;}
};

Printer::Printer(const std::string filename)
{
    swidth = 8.5*72; sheight = 11*72;
    maxlines = (int)((sheight-72)/12.0); curline = 0;
    pdf_surface = cairo_pdf_surface_create(filename.c_str(),swidth,sheight);
    if (!pdf_surface) {
        std::cerr << "Open failed: "<<filename<<std::endl;
        exit(1);
    }
    pdf_context = cairo_create (pdf_surface);
    layout = pango_cairo_create_layout (pdf_context);
}

Printer::~Printer()
{
    g_object_unref (layout);
    cairo_destroy (pdf_context);
    cairo_surface_destroy (pdf_surface);
}

void Printer::SelectFont(const std::string name, PangoStretch stretch)
{
    PangoAttrList *attrs;
    char fontstring[256];
    snprintf(fontstring,sizeof(fontstring),"%s 10px",name.c_str());
    PangoFontDescription *desc = pango_font_description_from_string (fontstring);
    pango_font_description_set_stretch  (desc,stretch);
    pango_layout_set_font_description (layout, desc);
    attrs = pango_layout_get_attributes (layout);
    if (attrs == NULL) {
        attrs = pango_attr_list_new ();
    }
    pango_attr_list_change(attrs,pango_attr_stretch_new (stretch));
    pango_layout_set_attributes (layout,attrs);
}

void Printer::PrintLine(const std::string text)
{
    double x,y;
   
    if (curline >= maxlines) {
        cairo_show_page (pdf_context);
        curline = 0;
    }
    x = 36;
    y = (curline*12)+36;
    curline++;
    cairo_move_to(pdf_context,x,y);
    pango_layout_set_text(layout,text.c_str(),-1);
    pango_cairo_update_layout (pdf_context,layout);
    pango_cairo_show_layout (pdf_context, layout);
}



int main(int argc, char *argv[])
{
    char buffer[80];
    std::string line;
    Printer *printer = new Printer(argv[1]);
   
    std::fstream s(argv[2], s.in );
   
    while (s.getline(buffer,sizeof(buffer))) {
        line = buffer;
        printer->SelectFont(line,PANGO_STRETCH_ULTRA_CONDENSED);
        printer->PrintLine(line+" PANGO_STRETCH_ULTRA_CONDENSED");
        printer->SelectFont(line,PANGO_STRETCH_EXTRA_CONDENSED);
        printer->PrintLine(line+" PANGO_STRETCH_EXTRA_CONDENSED");
        printer->SelectFont(line,PANGO_STRETCH_CONDENSED);
        printer->PrintLine(line+" PANGO_STRETCH_CONDENSED");
        printer->SelectFont(line,PANGO_STRETCH_SEMI_CONDENSED);
        printer->PrintLine(line+" PANGO_STRETCH_SEMI_CONDENSED");
        printer->SelectFont(line,PANGO_STRETCH_NORMAL);
        printer->PrintLine(line+" PANGO_STRETCH_NORMAL");
        printer->SelectFont(line,PANGO_STRETCH_SEMI_EXPANDED);
        printer->PrintLine(line+" PANGO_STRETCH_SEMI_EXPANDED");
        printer->SelectFont(line,PANGO_STRETCH_EXPANDED);
        printer->PrintLine(line+" PANGO_STRETCH_EXPANDED");
        printer->SelectFont(line,PANGO_STRETCH_EXTRA_EXPANDED);
        printer->PrintLine(line+" PANGO_STRETCH_EXTRA_EXPANDED");
        printer->SelectFont(line,PANGO_STRETCH_ULTRA_EXPANDED);
        printer->PrintLine(line+" PANGO_STRETCH_ULTRA_EXPANDED");
       
        printer->PrintLine("");
    }
   
   
    delete printer;
    return 0;
}


--
Robert Heller             -- 978-544-6933
Deepwoods Software        -- Custom Software Services
http://www.deepsoft.com/  -- Linux Administration Services
[hidden email]       -- Webhosting Services
                                                   
_______________________________________________
gtk-i18n-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-i18n-list
Reply | Threaded
Open this post in threaded view
|

Re: PangoStretch questions.

Behdad Esfahbod-3
On Sun, Aug 13, 2017 at 7:45 AM, Robert Heller <[hidden email]> wrote:
I am trying to use the PangoStretch attribute to create an effect that was
typically provided by old dot-matrix impact (and early inkjet) printers.

I want to be able to stretch (to double width) and to compress (to 60%
width) text.  Bascially, asymetrical scaling (horizontal only).  I am trying
to do this with the PangoStretch attribute, but it does not seem to work.
Either I am doing something wrong or there is a bug.

PangoStretch only works to the extent that you have compressed / expanded fonts installed.  For what you want, use a non-uniform cairo_scale() instead.
 
I am using pango-1.28.1-11.el6 and cairo-1.8.8-6.el6_6, specificly cairo-pdf
and pangocairo.  These are the stock versions on my CentOS 6 system.

I wrote a small test program (in C++) that illustrates the problem. Its first
command line argument is the name of an output PDF file and its second
argument is a file containing a list of fonts, one per line (created with this
command: fc-list | awk -F: '{print $1;}' | awk -F, '{print $1;}' | sort -u >
fonts.list).

Here is the program:

// -!- C++ -!- //////////////////////////////////////////////////////////////
//
//  System        :
//  Module        :
//  Object Name   : $RCSfile$
//  Revision      : $Revision$
//  Date          : $Date$
//  Author        : $Author$
//  Created By    : Robert Heller
//  Created       : Sun Aug 13 00:17:43 2017
//  Last Modified : <170813.1040>
//
//  Description
//
//  Notes
//
//  History
//
/////////////////////////////////////////////////////////////////////////////
//
//    Copyright (C) 2017  Robert Heller D/B/A Deepwoods Software
//                      51 Locke Hill Road
//                      Wendell, MA 01379-9728
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//
//
//////////////////////////////////////////////////////////////////////////////

static const char rcsid[] = "@(#) : $Id$";

#include "config.h"
#include <cairo-pdf.h>
#include <pango/pangocairo.h>

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

class Printer {
private:
    int curline, maxlines;
    cairo_surface_t *pdf_surface;
    cairo_t *pdf_context;
    PangoLayout *layout;
    PangoFontDescription *fontdescr;
    int swidth, sheight;
public:
    Printer(const std::string filename);
    ~Printer();
    void SelectFont(const std::string name, PangoStretch stretch);
    void PrintLine(const std::string text);
    void NewPage() {cairo_show_page (pdf_context);curline = 0;}
};

Printer::Printer(const std::string filename)
{
    swidth = 8.5*72; sheight = 11*72;
    maxlines = (int)((sheight-72)/12.0); curline = 0;
    pdf_surface = cairo_pdf_surface_create(filename.c_str(),swidth,sheight);
    if (!pdf_surface) {
        std::cerr << "Open failed: "<<filename<<std::endl;
        exit(1);
    }
    pdf_context = cairo_create (pdf_surface);
    layout = pango_cairo_create_layout (pdf_context);
}

Printer::~Printer()
{
    g_object_unref (layout);
    cairo_destroy (pdf_context);
    cairo_surface_destroy (pdf_surface);
}

void Printer::SelectFont(const std::string name, PangoStretch stretch)
{
    PangoAttrList *attrs;
    char fontstring[256];
    snprintf(fontstring,sizeof(fontstring),"%s 10px",name.c_str());
    PangoFontDescription *desc = pango_font_description_from_string (fontstring);
    pango_font_description_set_stretch  (desc,stretch);
    pango_layout_set_font_description (layout, desc);
    attrs = pango_layout_get_attributes (layout);
    if (attrs == NULL) {
        attrs = pango_attr_list_new ();
    }
    pango_attr_list_change(attrs,pango_attr_stretch_new (stretch));
    pango_layout_set_attributes (layout,attrs);
}

void Printer::PrintLine(const std::string text)
{
    double x,y;

    if (curline >= maxlines) {
        cairo_show_page (pdf_context);
        curline = 0;
    }
    x = 36;
    y = (curline*12)+36;
    curline++;
    cairo_move_to(pdf_context,x,y);
    pango_layout_set_text(layout,text.c_str(),-1);
    pango_cairo_update_layout (pdf_context,layout);
    pango_cairo_show_layout (pdf_context, layout);
}



int main(int argc, char *argv[])
{
    char buffer[80];
    std::string line;
    Printer *printer = new Printer(argv[1]);

    std::fstream s(argv[2], s.in );

    while (s.getline(buffer,sizeof(buffer))) {
        line = buffer;
        printer->SelectFont(line,PANGO_STRETCH_ULTRA_CONDENSED);
        printer->PrintLine(line+" PANGO_STRETCH_ULTRA_CONDENSED");
        printer->SelectFont(line,PANGO_STRETCH_EXTRA_CONDENSED);
        printer->PrintLine(line+" PANGO_STRETCH_EXTRA_CONDENSED");
        printer->SelectFont(line,PANGO_STRETCH_CONDENSED);
        printer->PrintLine(line+" PANGO_STRETCH_CONDENSED");
        printer->SelectFont(line,PANGO_STRETCH_SEMI_CONDENSED);
        printer->PrintLine(line+" PANGO_STRETCH_SEMI_CONDENSED");
        printer->SelectFont(line,PANGO_STRETCH_NORMAL);
        printer->PrintLine(line+" PANGO_STRETCH_NORMAL");
        printer->SelectFont(line,PANGO_STRETCH_SEMI_EXPANDED);
        printer->PrintLine(line+" PANGO_STRETCH_SEMI_EXPANDED");
        printer->SelectFont(line,PANGO_STRETCH_EXPANDED);
        printer->PrintLine(line+" PANGO_STRETCH_EXPANDED");
        printer->SelectFont(line,PANGO_STRETCH_EXTRA_EXPANDED);
        printer->PrintLine(line+" PANGO_STRETCH_EXTRA_EXPANDED");
        printer->SelectFont(line,PANGO_STRETCH_ULTRA_EXPANDED);
        printer->PrintLine(line+" PANGO_STRETCH_ULTRA_EXPANDED");

        printer->PrintLine("");
    }


    delete printer;
    return 0;
}


--
Robert Heller             -- <a href="tel:978-544-6933" value="+19785446933">978-544-6933
Deepwoods Software        -- Custom Software Services
http://www.deepsoft.com/  -- Linux Administration Services
[hidden email]       -- Webhosting Services

_______________________________________________
gtk-i18n-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-i18n-list



--

_______________________________________________
gtk-i18n-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-i18n-list