ItextSharp smooth background shade - itext

I am working on pdf using iTextSharp and product owner decided to have a "stickers" alike text boxes casting a shadow on background for it. I was searching the Internet and couldn't find a hint to how to achieve this effect. The design provided for it is as follows:
Could anybody more experienced with iText or iTextsharp help me by advising on how to achieve such effect please?
Kind regards
Karol

Using the hints and links in Joris' answer, one can easily implement a helper method like this to draw the shaded rounded rectangles:
void DrawRoundedShadedRectangle(PdfWriter writer, float x, float y, float w, float h, float r, float shade, BaseColor innerColor, BaseColor shadeColor, BaseColor outerColor)
{
PdfContentByte canvas = writer.DirectContent;
canvas.SaveState();
canvas.Rectangle(x - shade, y + r, w + 2 * shade, h - 2*r);
canvas.Clip();
canvas.NewPath();
PdfShading shadingRight = PdfShading.SimpleAxial(writer, x + w, y, x + w + shade, y, shadeColor, outerColor, false, false);
canvas.PaintShading(shadingRight);
PdfShading shadingLeft = PdfShading.SimpleAxial(writer, x, y, x - shade, y, shadeColor, outerColor, false, false);
canvas.PaintShading(shadingLeft);
canvas.RestoreState();
canvas.SaveState();
canvas.Rectangle(x + r, y - shade, w - 2 * r, h + 2 * shade);
canvas.Clip();
canvas.NewPath();
PdfShading shadingTop = PdfShading.SimpleAxial(writer, x, y + h, x, y + h + shade, shadeColor, outerColor, false, false);
canvas.PaintShading(shadingTop);
PdfShading shadingBottom = PdfShading.SimpleAxial(writer, x, y, x, y - shade, shadeColor, outerColor, false, false);
canvas.PaintShading(shadingBottom);
canvas.RestoreState();
canvas.SaveState();
canvas.Rectangle(x + w - r, y + h - r, r + shade, r + shade);
canvas.Clip();
canvas.NewPath();
PdfShading shadingTopRight = PdfShading.SimpleRadial(writer, x + w - r, y + h - r, r, x + w - r, y + h - r, r + shade, shadeColor, outerColor);
canvas.PaintShading(shadingTopRight);
canvas.RestoreState();
canvas.SaveState();
canvas.Rectangle(x - shade, y + h - r, r + shade, r + shade);
canvas.Clip();
canvas.NewPath();
PdfShading shadingTopLeft = PdfShading.SimpleRadial(writer, x + r, y + h - r, r, x + r, y + h - r, r + shade, shadeColor, outerColor);
canvas.PaintShading(shadingTopLeft);
canvas.RestoreState();
canvas.SaveState();
canvas.Rectangle(x - shade, y - shade, r + shade, r + shade);
canvas.Clip();
canvas.NewPath();
PdfShading shadingBottomLeft = PdfShading.SimpleRadial(writer, x + r, y + r, r, x + r, y + r, r + shade, shadeColor, outerColor);
canvas.PaintShading(shadingBottomLeft);
canvas.RestoreState();
canvas.SaveState();
canvas.Rectangle(x + w - r, y - shade, r + shade, r + shade);
canvas.Clip();
canvas.NewPath();
PdfShading shadingBottomRight = PdfShading.SimpleRadial(writer, x + w - r, y + r, r, x + w - r, y + r, r + shade, shadeColor, outerColor);
canvas.PaintShading(shadingBottomRight);
canvas.RestoreState();
canvas.SaveState();
canvas.SetColorFill(innerColor);
canvas.SetColorStroke(innerColor);
canvas.RoundRectangle(x, y, w, h, r);
canvas.FillStroke();
canvas.RestoreState();
}
For example,
DrawRoundedShadedRectangle(writer, 100, 500, 200, 100, 10, 20, BaseColor.GREEN, BaseColor.RED, BaseColor.WHITE);
draws this:
And this code
DrawRoundedShadedRectangle(writer, writer.PageSize.GetLeft(100), 300, writer.PageSize.Width - 200, 100, 10, 10, BaseColor.ORANGE, BaseColor.YELLOW, BaseColor.WHITE);
PdfContentByte canvas = writer.DirectContent;
canvas.SaveState();
canvas.Rectangle(writer.PageSize.Left, writer.PageSize.Bottom, writer.PageSize.Right, 300);
canvas.Clip();
canvas.NewPath();
DrawRoundedShadedRectangle(writer, writer.PageSize.GetLeft(100), 100, writer.PageSize.Width - 200, 200, 10, 10, BaseColor.WHITE, BaseColor.YELLOW, BaseColor.WHITE);
canvas.RestoreState();
draws this
By tweaking the radius, the shade, and all the color parameters this should become very similar to the provided design.

This can be done with iText.
It's a matter of using drawing operations on the canvas to achieve the effect.
in your case
- a rounded rectangle, filled with orange
- a rounded rectangle underneath it, filled with white

Related

Create a g-torus

I'm looking for an easy, elegant way to create a triangle mesh of a torus with an arbitrary number of holes. Here are some examples.
First, the AI had a problem with the question title, then a human decided "Closed. This question needs to be more focused."
Anyway, here is one direction:
% sphere
%fun = #(x, y, z) x.^2 + y.^2 + z.^2 - 1;
% 3-torus
% http://metamathological.blogspot.com/2013/01/today-i-learned-how-to-plot-doughnut.html
fun = #(x, y, z) (x.^2 + (y - 2).^2 - 1) .* ((x - sqrt(3)).^2 + (y + 1).^2 - 1) .* ((x + sqrt(3)).^2 + (y + 1).^2 - 1) - 0.0015*(x.^2 + y.^2 + 7*z.^2).^5;
% 4-torus
%fun = #(x, y, z) ((x - 2).^2 + (y - 2).^2 - 1) .* ((x - 2).^2 + (y + 2).^2 - 1) .* ((x + 2).^2 + (y - 2).^2 - 1) .* ((x + 2).^2 + (y + 2).^2 - 1) - 0.02*(x.^2 + y.^2 + 7*z.^2).^5;
res = 0.1;
r = 8;
x = -r:res:r;
y = -r:res:r;
z = -r:res:r;
[X, Y, Z] = meshgrid(x, y, z);
V = fun(X,Y,Z); % volumetric function (e.g. distance function)
I = isosurface(x ,y , z, V, 0); % extract level set as a mesh
p = patch(I);
isonormals(X, Y, Z, V, p);
p.FaceColor = 'red';
p.EdgeColor = 'none';
daspect([1 1 1])
view(3);
axis tight;
camlight;
lighting gouraud;

Scaling a rotated ellipse

I have a function that draws an ellipse respecting some major axis (vx) and minor axis (vy) scales rotated clockwise by an angle a. I would like to adjust it so that the unrotated ellipse satisfies the equation:
(x / vx)^2 + (y / vy)^2 = s
For some value s which is passed in.
function [] = plotellipse(cx, cy, vx, vy, s, a)
t = linspace(0, 2 * pi);
x = cx + vx * cos(t) * cos(-a) - vy * sin(t) * sin(-a);
y = cy + vy * sin(t) * cos(-a) + vx * cos(t) * sin(-a);
plot(x,y,'y-');
The usual equation for an ellipse, which you have implemented correctly, is
To reduce the desired equation to the same form, divide through by s:
Now x and y become
vxs = vx / sqrt(s)
vys = vy / sqrt(s)
x = cx + vxs * cos(t) * cos(-a) - vys * sin(t) * sin(-a);
y = cy + vys * sin(t) * cos(-a) + vxs * cos(t) * sin(-a);

Replacing symbolic derivatives in MATLAB

I have a symbolic function that looks like this
syms x y(x) h
fn(x) = y + (h^2*(diff(y(x), x) + 2))/2 + (h^5*diff(y(x), x, x, x, x))/120 + (h^3*diff(y(x), x, x))/6 + (h^4*diff(y(x), x, x, x))/24 + h*(2*x + y(x) - 1)
I'd like to replace all instances of derivatives of y with its first derivative, i.e.
subs(fn, sym('diff(y(x), x)'), dy)
where dy is already defined as
dy(x) = 2*x + y(x) - 1
The result is the following:
ans(x) =
y + (h^2*(2*x + y(x) + 1))/2 + (h^5*diff(y(x), x, x, x, x))/120 + (h^3*diff(y(x), x, x))/6 + (h^4*diff(y(x), x, x, x))/24 + h*(2*x + y(x) - 1)
It replaces the first derivative, but not the higher derivatives. What I want is for the h^5 term to have (h^5*diff(dy(x), x, x, x). Is there any way to do that?
My current method is pretty hackish and involves converting the sym to a string, replacing first derivatives with dy, then converting back to a sym and evaluating to reduce the order of each term of the series by one, but it has to be recursive because at each stage derivatives of dy are then replaced by something containing diff(y, ...). I was hoping there would be a cleaner way to deal with this.
You need to keep in mind that Matlab treats things like diff(y,x) and diff(y,x,2) as distinct variables. It doesn't know how to substitute diff(y,x) into diff(y,x,2) because such a general operation for an abstract function (one with out an explicit definition, e.g., y(x)) is ill-defined.
How about something like this that performs substitution from the opposite end, starting with the highest order derivatives:
syms y(x) h
dy(x) = 2*x + y - 1
fn(x) = y + (h^2*(diff(y, x) + 2))/2 + (h^5*diff(y, x, 4))/120 + (h^3*diff(y, x, 2))/6 + (h^4*diff(y, x, 3))/24 + h*(2*x + y - 1)
fn2 = subs(fn, diff(y, x, 4), diff(dy, x, 3));
fn2 = subs(fn2, diff(y, x, 3), diff(dy, x, 2));
fn2 = subs(fn2, diff(y, x, 2), diff(dy, x));
fn2 = subs(fn2, diff(y, x), dy);
This returns
fn2(x) =
y(x) + (h^2*(2*x + y(x) + 1))/2 + (h^3*(2*x + y(x) + 1))/6 + (h^4*(2*x + y(x) + 1))/24 + (h^5*(2*x + y(x) + 1))/120 + h*(2*x + y(x) - 1)
Or you can leave dy(x) as an abstract symbolic expression initially:
syms y(x) dy(x) h
fn(x) = y + (h^2*(diff(y, x) + 2))/2 + (h^5*diff(y, x, 4))/120 + (h^3*diff(y, x, 2))/6 + (h^4*diff(y, x, 3))/24 + h*(2*x + y - 1)
fn2 = subs(fn, diff(y, x, 4), diff(dy, x, 3));
fn2 = subs(fn2, diff(y, x, 3), diff(dy, x, 2));
fn2 = subs(fn2, diff(y, x, 2), diff(dy, x));
fn2 = subs(fn2, diff(y, x), dy)
which returns
fn2(x) =
y(x) + (h^4*diff(dy(x), x, x))/24 + (h^2*(dy(x) + 2))/2 + (h^5*diff(dy(x), x, x, x))/120 + (h^3*diff(dy(x), x))/6 + h*(2*x + y(x) - 1)

Understanding scipy.integrate,odeint wrapper odeintw

I'm trying to solve a coupled non-linear system of 4 PDEs, here's the code:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from odeintw import odeintw
# constants
global rH, L, q, p, Q, mu
rH = 1.0 ; L = 1.0 ; p = 2.0 ; q = 1.0 ; Q = 1.71 ; mu = Q*rH/L**2
def dfunc(G, kx, ky, w, r):
try:
z = 1.0/(rH - r)
except ZeroDivisionError:
print "Trying to divide zero"
phi = mu*( 1.0 - rH/r)
fr = 1.0 - (rH/r)**3
scale = L**2 * np.sqrt(fr) / r**2 + 0j
vp = (w + q*phi)/(np.sqrt(fr)) + p*mu*rH/r + 0j
vn = (w + q*phi)/(np.sqrt(fr)) - p*mu*rH/r + 0j
# G11 -> G1, G12 -> G2, G21 -> G3, G22 -> G4
G1, G2, G3, G4 = G
G11 = (vp - kx + (vn + kx)*G1**2 - ky*G1*G2 - ky*G1*G3
+ (vp-kx)*G2*G3 )
G12 = (ky + (vn + kx)*G1*G2 - ky*G2**2 - ky*G1*G4
+ (vp-kx)*G2*G4 )
G21 = (ky + (vn + kx)*G1*G3 - ky*G1*G4 - ky*G3**2
+ (vp-kx)*G3*G4 )
G22 = (vn + kx + (vn + kx)*G2*G3 - ky*G2*G4 - ky*G2*G3
+ (vp-kx)*G4**2 )
return np.multiply(scale, np.array([ G11, G12, G21, G22 ]))
# jaccobian
def jac(G, kx, ky, w, r):
G1, G2, G3, G4 = G
jac = np.array([
[2(vn+kx)*G1 - ky*(G2+G3), -ky*G1 + (vp-kx)*G3, -ky*G1+(vp-kx)*G2, 0 ],
[(vn+kx)*G2-ky*G4, (vn+kx)*G1-2*ky*G2+(vp-kx)*G4, 0, -ky*G1+(vp-kx)*G2 ],
[(vn+kx)*G3-ky*G4, 0, (vn+kx)*G1-2*ky*G3+(vp-kx)*G4, -ky*G1+(vp-kx)*G3 ],
[0, (vn+kx)*G3-ky*(G4+G3), (vn+kx)*G2-ky*G2, -ky*G2+2*(vp-kx)*G4 ]
])
return np.multiply(scale, jac)
# Initial value
Gir = np.array([ 1.0j, 0, 0, 1.0j ])
r = np.arange(rH + 0.1, 1000*rH, 1)
kx = 0
ky = 0
w = 0.0001 + 0.0000001j
G, infodict = odeintw(dfunc, Gir, r, args=(kx,ky,w), Dfun=jac, full_output=True )
print 'final', np.size(G), G
However it gives the following error and does not really move from the initial point:
lsoda-- warning..internal t (=r1) and h (=r2) are
such that in the machine, t + h = t on the next step
(h = step size). solver will continue anyway
In above, R1 = .1100000000000E+01 R2 = .3569471009368E-22
And the final solution G is an array of 3996 size, please help me understand this. Thanks!
In the functions dfunc and jac, the second argument must be the independent variable. In your case, the independent variable is r, so the definitions should begin
def dfunc(G, r, kx, ky, w):
and
def jac(G, r, kx, ky, w):

raised border SWT/JFace

i can't seem to find online help on how to add different type of borders in Eclipse RCP. I know Swing has BevelBorder which can be achieved using BorderFactory. Any swt equivalence?
Try this styles: SWT.SHADOW_IN, SWT.SHADOW_OUT, SWT.SHADOW_ETCHED_IN, SWT.SHADOW_ETCHED_OUT
Please use this method for Bevel Border
private void drawBorders(GC gc, int x, int y, int w, int h) {
final Display disp = getDisplay();
final Color topleft = disp.getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW);
final Color bottomright = disp.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
if (topleft != null && bottomright != null) {
gc.setLineWidth(1);
gc.setForeground(bottomright);
gc.drawLine(x + w, y, x + w, y + h);
gc.drawLine(x, y + h, x + w, y + h);
gc.setForeground(topleft);
gc.drawLine(x, y, x + w - 1, y);
gc.drawLine(x, y, x, y + h - 1);
}
}