Dynamically change the font size in Birt - birt

I am using a Dynamic Text in Birt. I want to display a sentence on one line but if it's too width it's displayed on two or more. Is there a way to dynamically decrease the font size when the sentence is too width so it stays on one line?
Thank you for your help.

first choose your cell and then click on the script tab,there choose onRender from the drop-down and put this code
this.getStyle().fontSize this gives you to change the fontsize dynamically while rendering the page...
Cheers..

Here is my solution - building on what #Sundar said.
In my case I have two fields in one cell. It is not perfect, but the only way I have to know when the text is "too big" is to measure the length of the combined strings and then adjust the font size accordingly.
Click on the cell and then the script tab, and enter code like this in the onRender script
// Decide if we need to change font size.
totalLength = 0;
if (row["Field Name 1"]) {
totalLength += row["Field Name 1"].length;
}
if (row["Field Name 2"]) {
totalLength += row["Field Name 2"].length;
}
// Make the font smaller with larger amounts of text.
switch (true) {
case (totalLength > 200):
this.getStyle().fontSize = "6pt";
break;
case (totalLength > 100 && totalLength < 200):
this.getStyle().fontSize = "8pt";
break;
default:
this.getStyle().fontSize = "10pt";
break;
}

Related

Specifying the width of a TaskDialog

I'm using a task dialog via the TaskDialogIndirect function.
Everything works as expected except that the width of the dialog is exactly twice of what I specified in the cxWidth field of the TASKDIALOGCONFIG structure.
Relevant code:
TASKDIALOGCONFIG tdc;
ZeroMemory(&tdc, sizeof(tdc));
tdc.cbSize = sizeof(TASKDIALOGCONFIG);
tdc.hwndParent = hwndParent;
tdc.hInstance = NULL;
tdc.pszWindowTitle = L"Title";
tdc.pszMainInstruction = L"Foo";
tdc.pszContent = L"Bar";
tdc.dwFlags = TDF_POSITION_RELATIVE_TO_WINDOW;
tdc.cxWidth = 150;
int result;
HRESULT hr = TaskDialogIndirect(&tdc, &result, NULL, NULL);
The documentation says that the cxWidth is the width in dialog units.
With the code above the width of the dialog is 300 pixels instead of 150 which means that one horizontal dialog unit is 2 which seems really small.
If I want to specify the width of the task dialog explicitely, how should I proceed? Is suppose I cannot rely on the fact that one horizontal dialog unit is always 2 in this case.

Qt Creator label value

I currently face the following problem:
I have 64 labels. Label_1 all the way up to Label_64.
I also have an int i.
"i" also goes from 1-64
I want that, when i == 1 Label_1 shall display an image. If i == 2, Label_2 shall display that image and so on.
Currently I'd do that with:
if(i == 1)
{
QPixmap pix("...");
ui->label_1->setPixmap(pix);
}
if(i == 2)
{
QPixmap pix("...");
ui->label_2->setPixmap(pix);
}
if(i == 3)
{
QPixmap pix("...");
ui->label_3->setPixmap(pix);
}
...
Is there some way to do that easier? Something like:
QPixmap pix("...");
ui->label_i->setPixmap(pix);
where the choosen label is directly defined by i?
You can store a list of QLabels.
QList<QLabel*> labels;
labels.at(i)->setPixmap(pix)
The disadvantage of this method is that you should manually assign ui->label_i to labels.at(i) for every i from 1 to 64 once:
labels.insert(0, NULL); // empty space to keep numbering the same.
labels.insert(1, ui->labels_1);
labels.insert(2, ui->labels_2);
...
labels.insert(64, ui->labels_64);
Depending on your specific case, you may use a more tricky solution. For example, if all the labels are stored in a QVBoxLayout at position 1 to 64, you can access label i as follows:
QVBoxLayout *layout = ...;
QLabel *label = qobject_cast<QWidget*> (layout->itemAt(i)->widget ());
if (label) // should be true if assumption is correct
label->setPixmap(pix);
You can also use method two to initialise the list of method 1.
See the Qt documentation for more information.

How to indent the first line of a paragraph in CKEditor

I'm using CKEditor and I want to indent just the first line of the paragraph. What I've done before is click "Source" and edit the <p> style to include text-indent:12.7mm;, but when I click "Source" again to go back to the normal editor, my changes are gone and I have no idea why.
My preference would be to create a custom toolbar button, but I'm not sure how to do so or where to edit so that clicking a custom button would edit the <p> with the style attribute I want it to have.
Depending on which version of CKE you use, your changes most likely disappear because ether the style attribute or the text-indent style is not allowed in the content. This is due to the Allowed Content Filter feature of CKEditor, read more here: http://docs.ckeditor.com/#!/guide/dev_advanced_content_filter
Like Ervald said in the comments, you can also use CSS to do this without adding the code manually - however, your targeting options are limited. Either you have to target all paragraphs or add an id or class property to your paragraph(s) and target that. Or if you use a selector like :first-child you are restricted to always having the first element indented only (which might be what you want, I don't know :D).
To use CSS like that, you have to add the relevant code to contents.css, which is the CSS file used in the Editor contents and also you have to include it wherever you output the Editor contents.
In my opinion the best solution would indeed be making a plugin that places an icon on the toolbar and that button, when clicked, would add or remove a class like "indentMePlease" to the currently active paragraph. Developing said plugin is quite simple and well documented, see the excellent example at http://docs.ckeditor.com/#!/guide/plugin_sdk_sample_1 - if you need more info or have questions about that, ask in the comments :)
If you do do that, you again need to add the "indentMePlease" style implementation in contents.css and the output page.
I've got a way to indent the first line without using style, because I'm using iReport to generate automatic reports. Jasper does not understand styles. So I assign by jQuery an onkeydown method to the main iframe of CKEditor 4.6 and I check the TAB and Shift key to do and undo the first line indentation.
// TAB
$(document).ready(function(){
startTab();
});
function startTab() {
setTimeout(function(){
var $iframe_document;
var $iframe;
$iframe_document = $('.cke_wysiwyg_frame').contents();
$iframe = $iframe_document.find('body');
$iframe.keydown(function(e){
event_onkeydown(e);
});
},300);
}
function event_onkeydown(event){
if(event.keyCode===9) { // key tab
event.preventDefault();
setTimeout(function(){
var editor = CKEDITOR.instances['editor1'], //get your CKEDITOR instance here
range = editor.getSelection().getRanges()[0],
startNode = range.startContainer,
element = startNode.$,
parent;
if(element.parentNode.tagName != 'BODY') // If you take an inner element of the paragraph, get the parentNode (P)
parent = element.parentNode;
else // If it takes BODY as parentNode, it updates the inner element
parent = element;
if(event.shiftKey) { // reverse tab
var res = parent.innerHTML.toString().split(' ');
var aux = [];
var count_space = 0;
for(var i=0;i<res.length;i++) {
// console.log(res[i]);
if(res[i] == "")
count_space++;
if(count_space > 8 || res[i] != "") {
if(!count_space > 8)
count_space = 9;
aux.push(res[i]);
}
}
parent.innerHTML = aux.join(' ');
}
else { // tab
var spaces = " ";
parent.innerHTML = spaces + parent.innerHTML;
}
},200);
}
}

inDesign JSX scripted add of heading and content into textFrame

I'm attempting to use inDesign JSX scripts to insert the following data into a document:
data = [{heading:"Heading 1", content: ["Some content"]},
{heading:"Heading 2", content: ["Some other content with", "Multiple paragraphs"]}]
The data has to be placed into a single TextFrame, but have different styling on the heading and content.
The only way I can see to add the text is in one go via the textFrame.contents variable:
allContent = "";
headingParagraphs = []; // keep track of which paragraphs are headings
paragraph = 0;
for (var i = 0; i < data.length; i++) {
allContent += data.heading + "\r"; // Use a newline to split the paragraph
headingParagraphs.push(paragraph);
paragraph++;
for (var j = 0; j < data.content.length; j++) {
allContent += data.content[j] + "\r"; // Use a newline to split the paragraph
paragraph++;
}
}
textFrame.contents = allContent; // all data is in, but all text is styled the same
Then once the data is in, I iterate the paragraphs and add some style to the headings:
for (var i = 0; i < textFrame.paragraphs.count(); i++) {
if (headingParagraphs.indexOf(i) != -1) { // this is a heading paragraph
textFrame.paragraphs[i].pointSize = 20;
}
}
This works fine for small data sets that fit on one page, but once the contents gets bigger than the frame, paragraphs only returns visible paragraphs. And if I follow on to a new textFrame, paragraphs get split and the headingParagraphs[] array no longer lines up.
Ideally I'd like to append to the contents and set styles before I append the next content - but the API docs aren't very clear on how you might do that (if at all)
// Pseudo code:
for all sections:
append the heading to the frame, split to next page if needed
style all the *new* paragraphs as headings
for all section contents
append the content to the frame, split to next page if needed
style any *new* paragraphs as normal content
Is there a way to achieve this using either an append function or some other way to assign headings to the right place after content has been added? Perhaps special characters in the content to define style?
Your longer text gets messed up because currently you are working inside a single text frame. As soon as the text runs out of this one frame, you can't refer to them as this frame's "owned" paragraphs anymore. Use parentStory instead, as it points to the whole story, inside one text frame or spanning more than one. It also keeps on working if the text gets overset.
So if you have a starting frame called textFrame, set a new variable story to textFrame.parentStory and use that to add text.
As for adding text to this frame(/story): indeed, there is no fast way to add formatted text. Setting contents only works for long swathes with the same formatting. One way I've used is to write INX formatted text to a temporary file and importing that. It's slow for short fragments, but larger stories (up to several hundreds of pages) can be created very efficiently in Javascript itself, and then importing it into ID is .. well, it aint fast but faster than trying to do it "manually".
The other way is to add contents one paragraph at a time. The trick is to set formatting and add your text to story.insertionPoints[-1]. This, in a particularly handy notation, refers to the very last text insertion point of the story. You can think of an insertion point as "the text cursor"; you can 'apply' formatting to it, and any text added will then have this formatting as well.
Your code snippet reworked to add one data item at a time:
for (var i = 0; i < data.length; i++)
{
story.insertionPoints[-1].pointSize = 20;
story.insertionPoints[-1].contents = data[i].heading + "\r"; // Use a newline to split the paragraph
story.insertionPoints[-1].pointSize = 10;
for (var j = 0; j < data[i].content.length; j++)
{
story.insertionPoints[-1].contents = data[i].content[j] + "\r"; // Use a newline to split the paragraph
}
}
One thing to note is that you cannot temporarily override the pointSize here. If you set it to your larger size, you must also set it back to the original size again (the '10' in my snippet).
Can I convince you to look in to using paragraph styles? With paragraph styles, you'd have something like
hdrStyle = app.activeDocument.paragraphStyles.item("Header");
textStyle = app.activeDocument.paragraphStyles.item("Text");
for (var i = 0; i < data.length; i++)
{
story.insertionPoints[-1].contents = data[i].heading + "\r"; // Use a newline to split the paragraph
story.insertionPoints[-2].appliedParagraphStyle = hdrStyle;
for (var j = 0; j < data[i].content.length; j++)
{
story.insertionPoints[-1].contents = data[i].content[j] + "\r"; // Use a newline to split the paragraph
story.insertionPoints[-2].appliedParagraphStyle = textStyle;
}
}
Note that it's worth here to invert inserting contents and applying formatting. This is so any previous 'temporary' formatting gets cleared; applying a paragraph style this way overrides any and all local overrides. As you have to apply the style to the previous paragraph (the one before the hard return you just inserted), you would use insertionPoints[-2] here.
The advantages of using styles over local formatting are countless. You can apply all formatting you want with a single command, safely remove all local overridden formatting, and change any part of the formatting globally if you are not satisfied with it, rather than having to re-run your script with slightly different settings.

JS console error- object not defined

I have the following JS function, which I'm using to detect whether a 'draggable' image on my HTML5 canvas is dragged on top of a static 'description box' image, also displayed on the canvas:
function isHit(mouseX, mouseY){
if(draggingImage == true){
console.log("if dragging image == true statment is being called");
if(mouseY > 420){
if((mouseX > 80) && (mouseX < 200) && (obj.shape.index >= 0) && (obj.shape.index <= numImagesAssets)){
console.log("Correct");
}else{
console.log("Incorrect");
}
}
}
}
The idea is that there will be a number of static images on the canvas (which will be the description boxes), and a greater number of draggable images. The user will be required to drag each of the draggable images to the relevant description box.
At the moment, my if statement is just checking to see whether the image currently being dragged belongs in the first of four description boxes.
Since the description boxes are all displayed in a row towards the bottom of the canvas, I've set my function to only check for a collision when the mouseY variable (y position of the cursor on the canvas) is greater than 420- since 420 is the top 'boundary' of the description box images.
Then, if the draggingImage variable is true, and the cursor's mouseY value is greater than 420, I want to start checking whether the cursor hovers over any of the description boxes. The first one is displayed between the X positions 80 and 200, so I'm checking if mouseX is greater than 80 and less than 200.
If it is, the function should then check whether the value of obj.shape.index is greater than or equal to 0 and less than the value of numImagesAssets. If it is, the console should log "correct", and if not, it should log "incorrect".
The variable obj.shape.index gets the position of the image that has been clicked in my array of images (this array contains all of the draggable images displayed on the canvas). If value of that is between a certain range, then I know that the image belongs to the first description box, if it's between another range, it belongs to the next description box, etc.
In this example, the value of the variable numImagesAssets is 10 (as displayed in the console by a console.log line in my code)
In my mousedown function, I have the line:
console.log("value of variable obj:" + output + ". Shape is " + obj.shape + ". index is " + obj.shape.index);
and when I click on an image on my canvas, this console.log line displays the value of the variable property obj.shape.index, for example, I just clicked on one of the draggable images on the canvas, and this line printed the following output to the console:
value of variable obj:shape: [object Object]; pixel: [object Uint8ClampedArray]; . Shape is [object Object]. index is 8
So I know that the image I clicked that time was at position 8 in my images array (I've checked this manually several times, clicking a different image each time, and it was always displaying the correct position).
Now, the problem I'm having, is that when I actually go and drag an image over its correct description box, I get a console error that says "obj is not defined", and it's complaining about the line:
if((mouseX > 80) && (mouseX < 200) && (obj.shape.index >= 0) && (obj.shape.index <= numImagesAssets)){
But, strangely, if I drag an image to one of the description boxes it doesn't belong to, the else clause in my if statment is triggered correctly, and the word "Incorrect" is printed to the console....
I can't figure out why it's not working when I drag an image to the correct description box, or why it says that "obj" is not defined in that case, but then is happy to use "obj" for the 'else' clause in the if statement.
Anyone have any ideas?
Edit 19/03/2013 # 12:00
Ok, I've edited my isHit function a little, to add another console.log line which will show the value of obj.shape.index within my isHit function. The function now looks like this:
function isHit(mouseX, mouseY, obj){
if(draggingImage == true){
console.log("if dragging image == true statment is being called");
if(mouseY > 420){
console.log("Value of obj.shape.index = " +obj.shape.index);
if((mouseX > 80) && (mouseX < 200) && (obj.shape.index >= 0) && (obj.shape.index <= numImagesAssets)){
console.log("Correct");
}else{
console.log("Incorrect");
}
}
}
}
I have the following console log line just before the call to isHit:
console.log("obj.shape.index = " + obj.shape.index);
This line displays that the value of obj.shape.index is 0 when I first click on the image (which is correct- it is the first image in my array of images), however, as I then drag the image around the canvas, the value of obj.shape.index seem to increase randomly... at one point the console was displaying that it was 23, then that it was 25, and then when I finally drag it to its description box, the console logs the line:
if dragging image == true statment is being called
showing that the isHit function is being called, and the next thing printed to the console is the line:
Value of obj.shape.index = 34
Any idea why the value of obj.shape.index keeps increasing? Is it not actually the position of the element within the array? I'm sure it is though, because when I click on one of the other images, it displays the value of their position within the array, so I don't understand why the value keeps increasing as I move the image around the canvas... it's position within the array isn't being altered at all...
If obj.shape.index is valid before entering isHit(), you probably have a scoping issue.
Try this -- supply obj to isHit:
// call isHit and also supply obj
console.log("obj = "+obj); // Test obj for validity just before calling isHit
var isItHit = isHit(mouseX, mouseY, obj); // Then call isHit WITH obj
// and modify isHit to accept the obj
function isHit(mouseX,mouseY,obj) { ...

Resources