コード例 #1
0
ファイル: save_pdf.c プロジェクト: kyllikki/netsurf
bool pdf_plot_rectangle(int x0, int y0, int x1, int y1, const plot_style_t *pstyle)
{
	DashPattern_e dash;
#ifdef PDF_DEBUG
	NSLOG(netsurf, INFO, "%d %d %d %d %f %X", x0, y0, x1, y1,
	      page_height - y0, pstyle->fill_colour);
#endif

	if (pstyle->fill_type != PLOT_OP_TYPE_NONE) {

		apply_clip_and_mode(false, pstyle->fill_colour, NS_TRANSPARENT, 0., DashPattern_eNone);

		/* Normalize boundaries of the area - to prevent
		   overflows.  It is needed only in a few functions,
		   where integers are subtracted.  When the whole
		   browser window is meant min and max int values are
		   used what must be handled in paged output.
		*/
		x0 = min(max(x0, 0), page_width);
		y0 = min(max(y0, 0), page_height);
		x1 = min(max(x1, 0), page_width);
		y1 = min(max(y1, 0), page_height);

		HPDF_Page_Rectangle(pdf_page, x0, page_height - y1, x1 - x0, y1 - y0);
		HPDF_Page_Fill(pdf_page);

	}

	if (pstyle->stroke_type != PLOT_OP_TYPE_NONE) {

		switch (pstyle->stroke_type) {
		case PLOT_OP_TYPE_DOT:
			dash = DashPattern_eDotted;
			break;

		case PLOT_OP_TYPE_DASH:
			dash = DashPattern_eDash;
			break;

		default:
			dash = DashPattern_eNone;
			break;

		}

		apply_clip_and_mode(false,
				NS_TRANSPARENT,
				pstyle->stroke_colour,
				plot_style_int_to_fixed(pstyle->stroke_width),
				dash);

		HPDF_Page_Rectangle(pdf_page, x0, page_height - y0, x1 - x0, -(y1 - y0));
		HPDF_Page_Stroke(pdf_page);
	}

	return true;
}
コード例 #2
0
JNIEXPORT void JNICALL Java_com_draekko_libharu_PdfPage_rectangle
  (JNIEnv *env, jobject obj, jfloat jleft, jfloat jtop, jfloat jwidth, jfloat jheight) {

    haru_setup_error_handler(env, __func__);
    HPDF_Page page = get_HPDF_Page(env, obj);
    HPDF_Page_Rectangle (page, jleft, jtop, jwidth, jheight);
    haru_clear_error_handler();
}
コード例 #3
0
/*
 * Class:     org_libharu_PdfPage
 * Method:    rectangle
 * Signature: (FFFF)V
 */
JNIEXPORT void JNICALL
Java_org_libharu_PdfPage_rectangle(JNIEnv *env, jobject obj, jfloat x, jfloat y, jfloat width,
        jfloat height) {
    jint page;
    /* Get mHPDFPagePointer */
    page = (*env)->GetIntField(env, obj, mHPDFPagePointer);
    HPDF_Page_Rectangle((HPDF_Page) page, (HPDF_REAL) x, (HPDF_REAL) y, (HPDF_REAL) width,
            (HPDF_REAL) height);
}
コード例 #4
0
ファイル: WPdfImage.C プロジェクト: ReWeb3D/wt
void WPdfImage::drawImage(const WRectF& rect, const std::string& imgUrl,
			  int imgWidth, int imgHeight,
			  const WRectF& srect)
{
  HPDF_Image img = 0;

  if (Uri::isDataUri(imgUrl)) {
    Uri::Uri uri = Uri::parseDataUri(imgUrl);
    if ("image/png" == uri.mimeType)
      img = HPDF_LoadPngImageFromMem(pdf_, 
				     (HPDF_BYTE*)uri.data.c_str(), 
				     uri.data.size()); 
    else if ("image/jpeg" == uri.mimeType)
      img = HPDF_LoadJpegImageFromMem(pdf_, 
				      (HPDF_BYTE*)uri.data.c_str(), 
				      uri.data.size()); 
  } else {
    std::string mimeType = Image::identifyImageFileMimeType(imgUrl);
    if ("image/png" == mimeType)
      img = HPDF_LoadPngImageFromFile2(pdf_, imgUrl.c_str());
    else if ("image/jpeg" == mimeType)
      img = HPDF_LoadJpegImageFromFile(pdf_, imgUrl.c_str());
  } 

  if (!img)
    throw WException("WPdfImage::drawImage(): cannot load image: " + imgUrl);

  double x = rect.x();
  double y = rect.y();
  double width = rect.width();
  double height = rect.height();

  HPDF_Page_GSave(page_);

  if (srect.x() != 0
      || srect.y() != 0
      || srect.width() != imgWidth
      || srect.height() != imgHeight) {
    double scaleX = width / imgWidth;
    double scaleY = height / imgHeight;

    x -= srect.x() * scaleX;
    y -= srect.y() * scaleY;
    width *= scaleX;
    height *= scaleY;

    HPDF_Page_Rectangle(page_, rect.x(), rect.y(), rect.width(), rect.height());
    HPDF_Page_Clip(page_);
  }

  HPDF_Page_Concat(page_, 1, 0, 0, -1, x, y + height); // revert upside-down

  HPDF_Page_DrawImage(page_, img, 0, 0, width, height);

  HPDF_Page_GRestore(page_);
}
コード例 #5
0
ファイル: text_demo2.c プロジェクト: ffsdmad/gtkmm-printing
void echo2(HPDF_Page page, char *str1, int i, int j){

    HPDF_Page_EndText (page);

    HPDF_Page_Rectangle (page, 10 + (HPDF_Page_GetWidth(page)/100)*i, HPDF_Page_GetHeight(page)-25 - j*10, 200,
                20);
    HPDF_Page_Stroke (page);

    HPDF_Page_BeginText (page);
    HPDF_Page_TextOut (page, 12 + (HPDF_Page_GetWidth(page)/100)*i, HPDF_Page_GetHeight(page)-20 - j*10, str1);
};
コード例 #6
0
ファイル: save_pdf.c プロジェクト: kyllikki/netsurf
/**
 * Enter/leave text mode and update PDF gstate for its clip, fill & stroke
 * colour, line width and dash pattern parameters.
 * \param selectTextMode true if text mode needs to be entered if required;
 * false otherwise.
 * \param fillCol Desired fill colour, use NS_TRANSPARENT if no update is
 * required.
 * \param strokeCol Desired stroke colour, use NS_TRANSPARENT if no update is
 * required.
 * \param lineWidth Desired line width. Only taken into account when strokeCol
 * is different from NS_TRANSPARENT.
 * \param dash Desired dash pattern. Only taken into account when strokeCol
 * is different from NS_TRANSPARENT.
 */
static void apply_clip_and_mode(bool selectTextMode, colour fillCol,
		colour strokeCol, float lineWidth, DashPattern_e dash)
{
	/* Leave text mode when
	 *  1) we're not setting text anymore
	 *  2) or we need to update the current clippath
	 *  3) or we need to update any fill/stroke colour, linewidth or dash.
	 * Note: the test on stroke parameters (stroke colour, line width and
	 * dash) is commented out as if these need updating we want to be
	 * outside the text mode anyway (i.e. selectTextMode is false).
	 */
	if (in_text_mode && (!selectTextMode || clip_update_needed
		|| (fillCol != NS_TRANSPARENT
			&& fillCol != pdfw_gs[pdfw_gs_level].fillColour)
		/* || (strokeCol != NS_TRANSPARENT
			&& (strokeCol != pdfw_gs[pdfw_gs_level].strokeColour
				|| lineWidth != pdfw_gs[pdfw_gs_level].lineWidth
				|| dash != pdfw_gs[pdfw_gs_level].dash)) */)) {
		HPDF_Page_EndText(pdf_page);
		in_text_mode = false;
	}

	if (clip_update_needed)
		pdfw_gs_restore(pdf_page);

	/* Update fill/stroke colour, linewidth and dash when needed.  */
	if (fillCol != NS_TRANSPARENT)
		pdfw_gs_fillcolour(pdf_page, fillCol);
	if (strokeCol != NS_TRANSPARENT) {
		pdfw_gs_strokecolour(pdf_page, strokeCol);
		pdfw_gs_linewidth(pdf_page, lineWidth);
		pdfw_gs_dash(pdf_page, dash);
	}

	if (clip_update_needed) {
		pdfw_gs_save(pdf_page);

		HPDF_Page_Rectangle(pdf_page, last_clip_x0,
				page_height - last_clip_y1,
				last_clip_x1 - last_clip_x0,
				last_clip_y1 - last_clip_y0);
		HPDF_Page_Clip(pdf_page);
		HPDF_Page_EndPath(pdf_page);

		clip_update_needed = false;
	}

	if (selectTextMode && !in_text_mode) {
		HPDF_Page_BeginText(pdf_page);
		in_text_mode = true;
	}
}
コード例 #7
0
ファイル: sq_haru_pdf.cpp プロジェクト: MangoCats/winglib
int CHaruPdf::Rectangle( int x, int y, int w, int h )
{_STT();

	// Sanity checks
	if ( !m_page )
		return 0;

	// Draw rectangle
	if ( HPDF_OK != HPDF_Page_Rectangle( m_page, x, y, w, h ) )
		return 0;

	return 1;
}
コード例 #8
0
ファイル: text_demo2.c プロジェクト: ffsdmad/gtkmm-printing
void table(HPDF_Page page, HPDF_Font font, int row, int col, int X, int Y, int H,
        int *p, char**text){
    int width = HPDF_Page_GetWidth(page);
    //int height = HPDF_Page_GetHeight(page);
    int r=0, c=0, x, y, W = (width-20) / col;
    char tmp[130], tmp2[130];


    for (r = 0; r < row; r++)
    {
        x = 0; W = 0;
        for (c = 0; c < col; c++)
        {
            x = x + W;
            W = ((width-20)/100)*p[c];
            y = Y - (r * H);
            HPDF_Page_Rectangle (page, x +X , y , W, H);
            HPDF_Page_Stroke (page);

            HPDF_Page_BeginText (page);
            HPDF_Page_SetFontAndSize (page, font, 10);

            sprintf(tmp2, "%dx%d", r,c);
            sprintf(tmp,"[%s] %d %d %d %d | %d %d %d %d", tmp2, x , y, W, y, /*!*/ x+5 , y+10, x+W, y-10);
            sprintf(tmp,"[%s] %s", tmp2, text[(r*col)+c]);
            sprintf(tmp, "%s", text[(r*col)+c]);

            HPDF_Page_TextRect (page, x+2 +X+2 , y+15, x+W, y-10,
                    tmp, HPDF_TALIGN_LEFT, NULL);
            HPDF_Page_EndText (page);

            //HPDF_Page_TextRect (page, 10 , 600, 50, 550,
            //break;
        }
        //break;

    }
};
コード例 #9
0
ファイル: WPdfImage.C プロジェクト: AlexanderKotliar/wt
void WPdfImage::drawImage(const WRectF& rect, const std::string& imgUrl,
			  int imgWidth, int imgHeight,
			  const WRectF& srect)
{
  HPDF_Image img = nullptr;

  if (DataUri::isDataUri(imgUrl)) {
#define HAVE_LOAD_FROM_MEM HPDF_MAJOR_VERSION > 2 || (HPDF_MAJOR_VERSION == 2 && (HPDF_MINOR_VERSION >= 2))

#if HAVE_LOAD_FROM_MEM
    DataUri uri(imgUrl);
    if ("image/png" == uri.mimeType)
      img = HPDF_LoadPngImageFromMem(pdf_, 
				     (HPDF_BYTE*)&uri.data[0], 
				     uri.data.size()); 
    else if ("image/jpeg" == uri.mimeType)
      img = HPDF_LoadJpegImageFromMem(pdf_, 
				      (HPDF_BYTE*)&uri.data[0], 
				      uri.data.size());
#else
      LOG_ERROR("drawImage: data URI support requires libharu 2.2.0 or later");
#endif
  } else {
    std::string mimeType = ImageUtils::identifyMimeType(imgUrl);
    if ("image/png" == mimeType)
      img = HPDF_LoadPngImageFromFile2(pdf_, imgUrl.c_str());
    else if ("image/jpeg" == mimeType)
      img = HPDF_LoadJpegImageFromFile(pdf_, imgUrl.c_str());
  } 

  if (!img)
    throw WException("WPdfImage::drawImage(): cannot load image: " + imgUrl);

  double x = rect.x();
  double y = rect.y();
  double width = rect.width();
  double height = rect.height();

  HPDF_Page_GSave(page_);

  if (srect.x() != 0
      || srect.y() != 0
      || srect.width() != imgWidth
      || srect.height() != imgHeight) {
    double scaleX = width / imgWidth;
    double scaleY = height / imgHeight;

    x -= srect.x() * scaleX;
    y -= srect.y() * scaleY;
    width *= scaleX;
    height *= scaleY;

    HPDF_Page_Rectangle(page_, rect.x(), rect.y(), rect.width(), rect.height());
    HPDF_Page_Clip(page_);
  }

  HPDF_Page_Concat(page_, 1, 0, 0, -1, x, y + height); // revert upside-down

  HPDF_Page_DrawImage(page_, img, 0, 0, width, height);

  HPDF_Page_GRestore(page_);
}
コード例 #10
0
ファイル: testlibhpdf.cpp プロジェクト: ProjectTegano/Tegano
int main (int /* argc */, char **argv)
{
    const char *page_title = "Font Demo";
    HPDF_Doc  pdf;
    char fname[256];
    HPDF_Page page;
    HPDF_Font def_font;
    HPDF_REAL tw;
    HPDF_REAL height;
    HPDF_REAL width;
    HPDF_UINT i;
    HPDF_Image image;
    double x;
    double y;
    double iw;
    double ih;
    HPDF_Stream stream;
    
    strcpy (fname, argv[0]);
    strcat (fname, ".pdf");

    pdf = HPDF_New (error_handler, NULL);
    if (!pdf) {
        printf ("error: cannot create PdfDoc object\n");
        return 1;
    }

    if (setjmp(env)) {
        HPDF_Free (pdf);
        return 1;
    }

    /* Add a new page object. */
    page = HPDF_AddPage (pdf);

    height = HPDF_Page_GetHeight (page);
    width = HPDF_Page_GetWidth (page);

    /* Print the lines of the page. */
    HPDF_Page_SetLineWidth (page, 1);
    HPDF_Page_Rectangle (page, 50, 50, width - 100, height - 110);
    HPDF_Page_Stroke (page);

    /* Print the title of the page (with positioning center). */
    def_font = HPDF_GetFont (pdf, "Helvetica", NULL);
    HPDF_Page_SetFontAndSize (page, def_font, 24);

    tw = HPDF_Page_TextWidth (page, page_title);
    HPDF_Page_BeginText (page);
    HPDF_Page_TextOut (page, (width - tw) / 2, height - 50, page_title);
    HPDF_Page_EndText (page);

    /* output subtitle. */
    HPDF_Page_BeginText (page);
    HPDF_Page_SetFontAndSize (page, def_font, 16);
    HPDF_Page_TextOut (page, 60, height - 80, "<Standerd Type1 fonts samples>");
    HPDF_Page_EndText (page);

    HPDF_Page_BeginText (page);
    HPDF_Page_MoveTextPos (page, 60, height - 105);

    i = 0;
    while (font_list[i]) {
        const char* samp_text = "abcdefgABCDEFG12345!#$%&+-@?";
        HPDF_Font font = HPDF_GetFont (pdf, font_list[i], NULL);

        /* print a label of text */
        HPDF_Page_SetFontAndSize (page, def_font, 9);
        HPDF_Page_ShowText (page, font_list[i]);
        HPDF_Page_MoveTextPos (page, 0, -18);

        /* print a sample text. */
        HPDF_Page_SetFontAndSize (page, font, 20);
        HPDF_Page_ShowText (page, samp_text);
        HPDF_Page_MoveTextPos (page, 0, -20);

        i++;
    }

    HPDF_Page_EndText (page);

    /* Add a new page object. */
    page = HPDF_AddPage (pdf);

    height = HPDF_Page_GetHeight (page);
    width = HPDF_Page_GetWidth (page);
    
    /* load image file. */
    image = HPDF_LoadPngImageFromFile (pdf, "test.png");

    x = 100;
    y = 100;
    iw = HPDF_Image_GetWidth (image);
    ih = HPDF_Image_GetHeight (image);
    
    /* Draw image to the canvas. (normal-mode with actual size.)*/
    HPDF_Page_DrawImage (page, image, x, y, iw, ih);
    
    HPDF_SaveToFile (pdf, fname);
    
    /* write something via zlib */
    stream = HPDF_FileWriter_New( pdf->mmgr, "test2.raw" );    
    HPDF_Stream_WriteToStream( image->stream, stream, HPDF_STREAM_FILTER_FLATE_DECODE, NULL );
    HPDF_Stream_Free (stream);
    
    /* clean up */
    HPDF_Free (pdf);

    return 0;
}
コード例 #11
0
ファイル: font_demo.c プロジェクト: ffsdmad/gtkmm-printing
int main (int argc, char **argv)
{
    const char *page_title = "Font Demo";
    HPDF_Doc  pdf;
    char fname[256];
    HPDF_Page page;
    HPDF_Font def_font;
    HPDF_REAL tw;
    HPDF_REAL height;
    HPDF_REAL width;
    HPDF_UINT i;

    strcpy (fname, argv[0]);
    strcat (fname, ".pdf");

    pdf = HPDF_New (error_handler, NULL);
    if (!pdf) {
        printf ("error: cannot create PdfDoc object\n");
        return 1;
    }

    if (setjmp(env)) {
        HPDF_Free (pdf);
        return 1;
    }

    /* Add a new page object. */
    page = HPDF_AddPage (pdf);

    height = HPDF_Page_GetHeight (page);
    width = HPDF_Page_GetWidth (page);

    /* Print the lines of the page. */
    HPDF_Page_SetLineWidth (page, 1);
    HPDF_Page_Rectangle (page, 50, 50, width - 100, height - 110);
    HPDF_Page_Stroke (page);

    /* Print the title of the page (with positioning center). */
    def_font = HPDF_GetFont (pdf, "Helvetica", NULL);
    HPDF_Page_SetFontAndSize (page, def_font, 24);

    tw = HPDF_Page_TextWidth (page, page_title);
    HPDF_Page_BeginText (page);
    HPDF_Page_TextOut (page, (width - tw) / 2, height - 50, page_title);
    HPDF_Page_EndText (page);

    /* output subtitle. */
    HPDF_Page_BeginText (page);
    HPDF_Page_SetFontAndSize (page, def_font, 16);
    HPDF_Page_TextOut (page, 60, height - 80, "<Standerd Type1 fonts samples>");
    HPDF_Page_EndText (page);

    HPDF_Page_BeginText (page);
    HPDF_Page_MoveTextPos (page, 60, height - 105);

    i = 0;
    while (font_list[i]) {
        const char* samp_text = "ÏðèâåòabcdefgABCDEFG12345!#$%&+-@?";
        HPDF_Font font = HPDF_GetFont (pdf, font_list[i], "CP1251");

        /* print a label of text */
        HPDF_Page_SetFontAndSize (page, def_font, 9);
        HPDF_Page_ShowText (page, font_list[i]);
        HPDF_Page_MoveTextPos (page, 0, -18);

        /* print a sample text. */
        HPDF_Page_SetFontAndSize (page, font, 20);
        HPDF_Page_ShowText (page, samp_text);
        HPDF_Page_MoveTextPos (page, 0, -20);

        i++;
    }

    HPDF_Page_EndText (page);

    HPDF_SaveToFile (pdf, fname);

    /* clean up */
    HPDF_Free (pdf);

    return 0;
}
コード例 #12
0
ファイル: smoothscan.c プロジェクト: ncraun/smoothscan
void
generate_pdf (const char *outname, const char *tmpdirname, int num_fonts,
	      int num_input_files, const JBDATA * data,
	      const struct mapping *maps, int debug_draw_borders)
{
  int i, j;
  /* Create the pdf document */
  l_int32 ncomp = numaGetCount (data->naclass);
  HPDF_Doc pdf = HPDF_New (pdf_error_handler, NULL);

  HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);
  HPDF_Font *fonts = malloc_guarded (num_fonts * (sizeof (HPDF_Font)));

  int dirlen = strlen (tmpdirname);

  /* Load the fonts */
  for (i = 0; i < num_fonts; i++)
    {
      /* 1 for '/', 8 for %08d, 4 for '.ttf' */
      int fontlen = dirlen + 1 + 8 + 4;
      char *font_tfname = malloc_guarded (fontlen + 1);
      sprintf (font_tfname, "%s/%08d.ttf", tmpdirname, i);

      const char *font_name =
	HPDF_LoadTTFontFromFile (pdf, font_tfname, HPDF_TRUE);
      fonts[i] = HPDF_GetFont (pdf, font_name, "KOI8-R");
      free (font_tfname);
    }

  for (j = 0; j < num_input_files; j++)
    {
      /* Add page to document */
      HPDF_Page pg = HPDF_AddPage (pdf);

      HPDF_Page_SetWidth (pg, data->w);
      HPDF_Page_SetHeight (pg, data->h);

      /* TODO: Boost efficiency by making startcomp go to the next page on the next iteration */
      int start_comp = 0;

      for (i = start_comp; i < ncomp; i++)
	{
	  l_int32 ipage;
	  l_int32 iclass;
	  l_int32 x;
	  l_int32 y;

	  numaGetIValue (data->napage, i, &ipage);

	  if (ipage != j)
	    continue;

	  numaGetIValue (data->naclass, i, &iclass);
	  ptaGetIPt (data->ptaul, i, &x, &y);

	  /*double left = x;
	     double top = data->h - y;
	     double right = x + data->latticew;
	     double bottom = data->h - (y + data->latticeh); */

	  char text[2];
	  text[0] = maps[iclass].code_point;
	  text[1] = '\0';

	  HPDF_Font font = fonts[maps[iclass].font_num];

	  HPDF_Page_BeginText (pg);
	  double fontsize = 100;

	  if (fontsize > 300)
	    {
	      error_quit ("This is a known bug.\n"
			  "libharu can't handle fontsizes bigger than 300, which is what is being requested here.\n"
			  "Please report this bug, and the file that produced this error");
	    }

	  HPDF_Page_SetFontAndSize (pg, font, 100);

	  HPDF_Page_MoveTextPos (pg, x, (data->h - y) - data->latticeh);
	  HPDF_Page_ShowText (pg, text);

	  HPDF_Page_EndText (pg);

	  if (debug_draw_borders)
	    {
	      HPDF_Page_SetRGBStroke (pg, 1, 0, 0);
	      /* In this, x, y is the LOWER LEFT, not UPPER LEFT */
	      HPDF_Page_Rectangle (pg, x, (data->h - y) - data->latticeh,
				   data->latticew, data->latticeh);
	      HPDF_Page_Stroke (pg);
	    }
	}
    }

  /* Output */
  HPDF_SaveToFile (pdf, outname);

  /* Cleanup */
  HPDF_Free (pdf);
  free (fonts);
}