2

I am a programmer and I have printing code that draws grid on the paper.

Grid has 4 columns, and they have equal horizontal length. Height of the cell is one tenth of the paper size and is fixed.

Total number of rows is unknown but I know for a fact that there will be at least one row, and that cells will have fixed dimensions.

Maximum number of characters that can fit into cell is 50.

User can choose the paper size on which printing will be done and there I face a problem:

Since the cell dimensions depend on paper size, I do not know how to calculate font size so text can fit into cell.

I have asked for help on StackOverflow but failed, and have tried to find a mathematical formula but that solution didn't work for every case.

I apologize for asking here, but I am desperate at the moment. If further info is required please ask and I will update my post.

Thank you.

  • The technique you accepted here is correct: math.stackexchange.com/a/859906 . I use it extensively using HTML5 Canvas (in browser) and ExtendScript (in PhotoShop). I found that as long as the original text is smaller than the width of the text box, it works flawlessly. I use a font size of 4 for my purposes. Can you verify that the errors that you had cannot be solved by making the width of the text so small that it will never be long enough to wrap when you first measure the text width? – user13497 Jul 17 '14 at 6:55
  • Would you be willing to try an alternate tool (like this cairographics.org/cairomm) to achieve your goals? If so, do you have the same errors? – user13497 Jul 17 '14 at 6:56
  • @TomDworzanski: I am not allowed to use libraries, but I haven't tried with font size 4, so I will try it later and report my results. Thank you for your advice. – AlwaysLearningNewStuff Jul 17 '14 at 8:49
  • @TomDworzanski: Even with font size 4, I was able to find a case where last letter barely exceeds the limit. – AlwaysLearningNewStuff Jul 17 '14 at 9:05
  • Sorry to hear that. I hope someone else can come up with a better solution. If not, your only option may be to work your way around the bug with some extra margin or with multiple re-sizings until you get the correct fit. I wish I could offer you more. – user13497 Jul 17 '14 at 9:19
5

Generally a programming language will have a device context which will allow you to draw some example text and then measure it's width and height. For example in python, using wxPython GUI toolkit:

import wx
dc = wx.ScreenDC()
#yourFont =  wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, True)
#dc.SetFont(yourFont) 
w,h = dc.GetTextExtent('X') 

Regardless of which tools you are using the requirements are generally the same. You need to know your desired font size and desired cell padding:

cell_height = padding_top + text_height + padding_bottom

where units are generally pixels and text_height is a f(fontSize).

Generally it is not advisable to size fonts based on width unless you are willing to wrap the text. Use a '...' or something to cut short long text if you can't use a new line.

Don't write your own text wrapping code either. Again, in python:

import textwrap
a = "This sentence is less than 50 characters"
widthInChars = 20
lines = textwrap.wrap(a,widthInChars)

Now you have a relationship between font size, line spacing, padding, and cell size.

  • I am using pure WinAPI and C++. I do have a device context and a function that measures length of the text but first I need to select font into device context. This means that I need to "guess" font size, calculate text length and only then check if it fits the cell. If it doesn't fit then I need to "do something" to fix that. Generally it is not advisable to size fonts based on width unless you are willing to wrap the text. I am willing to do the wrapping ( but I can't use ... to cut the extra text ). There is nothing like lines = textwrap.wrap(a,widthInChars) in WinAPI. – AlwaysLearningNewStuff Jul 13 '14 at 16:38
  • stackoverflow.com/questions/814954/wordwrap-win32 Is this helpful? My understanding of WinAPI is basic. – q2ra Jul 13 '14 at 17:14
  • Unfortunately there are certain things DrawTextExt does that ruin my plans, but you have understood my problem. I need something along those lines that doesn't modify the passed rectangle if text is too long/high. – AlwaysLearningNewStuff Jul 13 '14 at 17:20
  • If it is an option; simply choose your font and size it such that 50 characters fits a cell perfectly. Maybe the extra white space for cells less than 50 characters will be better than trying to do the wrapping for all cases. – q2ra Jul 14 '14 at 19:15
  • I don't understand what you mean. My problem occurs when font is so big that text can't fit into cell. That is why I need a way to shrink it somehow, which is the purpose of my question. I do not have print-preview or similar, to check the result. – AlwaysLearningNewStuff Jul 14 '14 at 19:18
0

If the user is able to choose the printed paper size from a list before entering any text in the cells, then are you able to loop through an array of paper sizes and assign the table a font size depending on the paper? This would require a lot of testing to see which fonts suit which size of paper.

  • That is all possible, but it is the approach I wish to avoid... :( – AlwaysLearningNewStuff Jul 21 '14 at 19:26
0

1 You Shall get size for any character of font,include all(upercase and ,-+'"&*#$ etc.), because all fonts has separations differents between characters.

<div id='size23' >dd</div>...<div id='size43' >didididi</div>.... <div id='size54' >dwdwdwdw</div>

and use javascript to get size with diferents combinations of characters

Width43 = getElementById('size43').offsetWidth; ....

2.-Every font has a proportion size height/width and Font metrics(custom aditinal proportion), you get proportion and font metric for that font. you can find more here use a image from zoom font text to get it.

3.-Use proportion number to get sizes for cell like as

proportion_number_font = 0.6

size_a = 0.10; size_b = 0.9 ...

total_a = 10, total_u = 5 , ......
Total_size = 34;


cell_width =  total_size 

cell_height =  0.6*34 ;

n = 2 // n is a proportional 

addjust_cell_proportion = cell_width * n * proportion_number_font;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.