Skip to content

Commit

Permalink
[41_4] clang-format the Ghostscript C++ plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
da-liii authored Oct 9, 2023
1 parent 2ffb506 commit 5b1c043
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 262 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/clang-format-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ on:
- 'src/Graphics/Types/**.cpp'
- 'src/Typeset/Boxes/Graphics/**.cpp'
- 'src/Plugins/Pdf/**.cpp'
- 'src/Plugins/Pdf/**.hpp'
- 'src/Plugins/Ghostscript/**.hpp'
- 'src/Plugins/Ghostscript/**.hpp'
- 'src/Data/Tree/**.cpp'
- 'src/Data/Tree/**.hpp'
- 'src/Data/Observers/**.cpp'
Expand Down
2 changes: 2 additions & 0 deletions bin/format
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ clang-format -i src/Graphics/Types/**.hpp
clang-format -i src/Typeset/Boxes/Graphics/**.cpp
clang-format -i src/Plugins/Pdf/**.cpp
clang-format -i src/Plugins/Pdf/**.hpp
clang-format -i src/Plugins/Ghostscript/**.cpp
clang-format -i src/Plugins/Ghostscript/**.hpp
clang-format -i src/Mogan/**.cpp
clang-format -i src/Mogan/**.hpp

Expand Down
89 changes: 43 additions & 46 deletions src/Plugins/Ghostscript/ghostscript.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

/******************************************************************************
* MODULE : ghostscript.cpp
* DESCRIPTION: interface with ghostscript
* COPYRIGHT : (C) 1999 Joris van der Hoeven
*******************************************************************************
* This software falls under the GNU general public license version 3 or later.
* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
******************************************************************************/
* MODULE : ghostscript.cpp
* DESCRIPTION: interface with ghostscript
* COPYRIGHT : (C) 1999 Joris van der Hoeven
*******************************************************************************
* This software falls under the GNU general public license version 3 or later.
* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
******************************************************************************/

#include "Ghostscript/ghostscript.hpp"
#ifdef USE_PLUGIN_GS
Expand All @@ -20,13 +20,13 @@
#include "image_files.hpp"

/******************************************************************************
* Special hack for a bug in Ghostscript 6.50, 6.51 and 6.52
******************************************************************************/
* Special hack for a bug in Ghostscript 6.50, 6.51 and 6.52
******************************************************************************/

static int gs_type= -1;
// -1: uninitialized
// 0: OK
// 1: with pixmap bug
// -1: uninitialized
// 0: OK
// 1: with pixmap bug

bool
ghostscript_bugged () {
Expand All @@ -35,29 +35,29 @@ ghostscript_bugged () {
#else
if (gs_type == -1) {
string gs_version= var_eval_system ("gs --version");
gs_type= 0;
if ((gs_version == "6.50") ||
(gs_version == "6.51") ||
(gs_version == "6.52"))
gs_type = 0;
if ((gs_version == "6.50") || (gs_version == "6.51") ||
(gs_version == "6.52"))
gs_type= 1;
}
return (gs_type == 1);
#endif
}

/******************************************************************************
* Drawing a postscript image file in a pixmap
******************************************************************************/
* Drawing a postscript image file in a pixmap
******************************************************************************/

static string
encapsulate_postscript (string s) {
int i, n= N(s);
int last_begin= 0;
int i, n= N (s);
int last_begin= 0;
string r;
for (i=0; i<n; i++) {
if ((s[i] != 's') || (i>(n-8)) || (s(i,i+8) != "showpage")) continue;
for (i= 0; i < n; i++) {
if ((s[i] != 's') || (i > (n - 8)) || (s (i, i + 8) != "showpage"))
continue;
if (i > last_begin) r << s (last_begin, i);
i += 8;
i+= 8;
last_begin= i;
}
r << s (last_begin, i);
Expand All @@ -66,8 +66,7 @@ encapsulate_postscript (string s) {

void
ghostscript_run (Display* dpy, Window gs_win, Pixmap pm, url im, SI w, SI h) {
if (DEBUG_VERBOSE)
debug_io << "Running ghostscript " << im << "\n";
if (DEBUG_VERBOSE) debug_io << "Running ghostscript " << im << "\n";

int x1, y1, x2, y2;
ps_bounding_box (im, x1, y1, x2, y2);
Expand All @@ -76,44 +75,43 @@ ghostscript_run (Display* dpy, Window gs_win, Pixmap pm, url im, SI w, SI h) {
// white lines may appear at the borders of the patterns.
// However the hack also causes normal images to be rendered
// slightly incorrectly
if (x1+1 < x2) x1++;
if (y1+1 < y2) y1++;
if (x1 + 1 < x2) x1++;
if (y1 + 1 < y2) y1++;
// End dirty hack

if (ghostscript_bugged ()) {
int scr = DefaultScreen (dpy);
int max_w= 2 * DisplayWidth (dpy, scr);
int max_h= 2 * DisplayHeight (dpy, scr);
w= min (w, max_w);
h= min (h, max_h);
w = min (w, max_w);
h = min (h, max_h);
}

#ifndef OS_WIN
int win_id= (int) gs_win;
int pix_id= (int) pm;
if (ghostscript_bugged ()) set_env ("GHOSTVIEW", as_string (win_id));
else set_env ("GHOSTVIEW", as_string (win_id) * " " * as_string (pix_id));
Atom gh= XInternAtom (dpy, "GHOSTVIEW", false);
Atom st= XA_STRING;
double dpi_x= ((double) (w*72))/((double) (x2-x1));
double dpi_y= ((double) (h*72))/((double) (y2-y1));
string data=
(ghostscript_bugged ()? as_string (pix_id): string ("0")) * " 0 " *
as_string (x1) * " " * as_string (y1) * " " *
as_string (x2) * " " * as_string (y2) * " " *
as_string (dpi_x) * " " * as_string (dpi_y) * " " *
"0 0 0 0";
Atom gh = XInternAtom (dpy, "GHOSTVIEW", false);
Atom st = XA_STRING;
double dpi_x= ((double) (w * 72)) / ((double) (x2 - x1));
double dpi_y= ((double) (h * 72)) / ((double) (y2 - y1));
string data = (ghostscript_bugged () ? as_string (pix_id) : string ("0")) *
" 0 " * as_string (x1) * " " * as_string (y1) * " " *
as_string (x2) * " " * as_string (y2) * " " * as_string (dpi_x) *
" " * as_string (dpi_y) * " " * "0 0 0 0";
{
c_string _data (data);
int _n= N(data);
XChangeProperty (dpy, gs_win, gh, st, 8, PropModeReplace, (unsigned char*)(char*)_data, _n);
int _n= N (data);
XChangeProperty (dpy, gs_win, gh, st, 8, PropModeReplace,
(unsigned char*) (char*) _data, _n);
}
XSync(dpy, false);
XSync (dpy, false);
#endif

string raw_ps, nice_ps;
raw_ps= ps_load (im);
nice_ps= encapsulate_postscript (raw_ps);
raw_ps = ps_load (im);
nice_ps = encapsulate_postscript (raw_ps);
url temp_name= url_temp ();
save_string (temp_name, nice_ps, true);
#ifdef USE_PLUGIN_GS
Expand All @@ -125,4 +123,3 @@ ghostscript_run (Display* dpy, Window gs_win, Pixmap pm, url im, SI w, SI h) {
}

#endif // X11TEXMACS

22 changes: 11 additions & 11 deletions src/Plugins/Ghostscript/ghostscript.hpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@

/******************************************************************************
* MODULE : ghostscript.hpp
* DESCRIPTION: interface with ghostscript
* COPYRIGHT : (C) 1999 Joris van der Hoeven
*******************************************************************************
* This software falls under the GNU general public license version 3 or later.
* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
******************************************************************************/
* MODULE : ghostscript.hpp
* DESCRIPTION: interface with ghostscript
* COPYRIGHT : (C) 1999 Joris van der Hoeven
*******************************************************************************
* This software falls under the GNU general public license version 3 or later.
* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
******************************************************************************/

#ifndef GHOSTSCRIPT_H
#define GHOSTSCRIPT_H

#ifdef X11TEXMACS

#include "url.hpp"
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>

bool ghostscript_bugged ();
void ghostscript_run (Display* dpy, Window gs_win, Pixmap pm,
url image, SI w, SI h);
void ghostscript_run (Display* dpy, Window gs_win, Pixmap pm, url image, SI w,
SI h);

#endif // X11TEXMACS

Expand Down
Loading

0 comments on commit 5b1c043

Please sign in to comment.